| 177 | ariadna | 1 | <?php
 | 
        
           | 279 | ariadna | 2 |   | 
        
           |  |  | 3 | use lbuchs\WebAuthn\Attestation\Format\None;
 | 
        
           |  |  | 4 |   | 
        
           | 177 | ariadna | 5 | require_once(__DIR__ . '/cesa.php');
 | 
        
           | 183 | ariadna | 6 |   | 
        
           | 177 | ariadna | 7 | class StaticsBlocks extends Cesa
 | 
        
           |  |  | 8 | {
 | 
        
           | 261 | ariadna | 9 |   | 
        
           | 254 | ariadna | 10 |     public function __construct($title)
 | 
        
           | 177 | ariadna | 11 |     {
 | 
        
           | 255 | ariadna | 12 |         global $USER, $PAGE, $SITE;
 | 
        
           |  |  | 13 |         require_login(null, false);
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 |         if (isguestuser()) {
 | 
        
           |  |  | 16 |             throw new require_login_exception('Guests are not allowed here.');
 | 
        
           |  |  | 17 |         }
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 |         $this->userID = optional_param('userid', $USER->id, PARAM_INT);
 | 
        
           |  |  | 20 |         $this->currentUser = $this->userID == $USER->id;
 | 
        
           |  |  | 21 |         $this->user = core_user::get_user($this->userID);
 | 
        
           |  |  | 22 |         $this->title = get_string($title);
 | 
        
           |  |  | 23 |         $this->blockManager = $PAGE->blocks;
 | 
        
           | 261 | ariadna | 24 |   | 
        
           |  |  | 25 |         // Definimos varias regiones
 | 
        
           | 271 | ariadna | 26 |         $this->regions = ['side-post'];
 | 
        
           |  |  | 27 |         $this->blockNames = ['cesa_course_rating', 'comments', 'messageteacher', 'mynotes'];
 | 
        
           | 255 | ariadna | 28 |         $this->blockExists = true;
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |         if (!$this->user || !core_user::is_real_user($this->userID)) {
 | 
        
           |  |  | 31 |             throw new moodle_exception('invaliduser', 'error');
 | 
        
           |  |  | 32 |         }
 | 
        
           | 261 | ariadna | 33 |   | 
        
           | 264 | ariadna | 34 |         // Asignar y validar los bloques en todas las regiones
 | 
        
           |  |  | 35 |         foreach ($this->regions as $region) {
 | 
        
           |  |  | 36 |             $this->regionName = $region;
 | 
        
           | 279 | ariadna | 37 |   | 
        
           |  |  | 38 |             // Añadir la región si no existe
 | 
        
           |  |  | 39 |             $this->addRegion($this->regionName);
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |             // Validar si los bloques existen
 | 
        
           |  |  | 42 |             if (!$this->validateIfExistBlocks($this->regionName)) {
 | 
        
           |  |  | 43 |                 // Añadir bloques si no existen
 | 
        
           |  |  | 44 |                 $this->addBlocksIfNotExist($this->regionName);
 | 
        
           |  |  | 45 |             }
 | 
        
           | 261 | ariadna | 46 |         }
 | 
        
           | 177 | ariadna | 47 |     }
 | 
        
           |  |  | 48 |   | 
        
           | 279 | ariadna | 49 |     public function addRegion($region)
 | 
        
           |  |  | 50 |     {
 | 
        
           |  |  | 51 |         // Si la región no está presente en el bloque de la página, la añadimos.
 | 
        
           |  |  | 52 |         if (!$this->blockManager->region_exists($region)) {
 | 
        
           |  |  | 53 |             $this->blockManager->add_region($region);
 | 
        
           |  |  | 54 |         }
 | 
        
           |  |  | 55 |     }
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |     public function validateIfExistBlocks($region)
 | 
        
           |  |  | 58 |     {
 | 
        
           |  |  | 59 |         // Obtener los bloques de la región actual.
 | 
        
           |  |  | 60 |         $blocks = $this->blockManager->get_blocks_for_region($region);
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |         // Validar si todos los bloques especificados existen en esta región.
 | 
        
           |  |  | 63 |         foreach ($this->blockNames as $blockName) {
 | 
        
           |  |  | 64 |             $blockFound = false;
 | 
        
           |  |  | 65 |             foreach ($blocks as $block) {
 | 
        
           |  |  | 66 |                 if (get_class($block) == 'block_' . $blockName) {
 | 
        
           |  |  | 67 |                     $blockFound = true;
 | 
        
           |  |  | 68 |                     break;
 | 
        
           |  |  | 69 |                 }
 | 
        
           |  |  | 70 |             }
 | 
        
           |  |  | 71 |             // Si algún bloque no se encuentra, devolvemos false.
 | 
        
           |  |  | 72 |             if (!$blockFound) {
 | 
        
           |  |  | 73 |                 return false;
 | 
        
           |  |  | 74 |             }
 | 
        
           |  |  | 75 |         }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         // Si todos los bloques existen, devolvemos true.
 | 
        
           |  |  | 78 |         return true;
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     public function addBlocksIfNotExist($region)
 | 
        
           |  |  | 82 |     {
 | 
        
           |  |  | 83 |         // Obtener los bloques de la región actual.
 | 
        
           |  |  | 84 |         $blocks = $this->blockManager->get_blocks_for_region($region);
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         foreach ($this->blockNames as $blockName) {
 | 
        
           |  |  | 87 |             $blockFound = false;
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |             // Buscar si el bloque ya está presente en la región.
 | 
        
           |  |  | 90 |             foreach ($blocks as $block) {
 | 
        
           |  |  | 91 |                 if (get_class($block) == 'block_' . $blockName) {
 | 
        
           |  |  | 92 |                     $blockFound = true;
 | 
        
           |  |  | 93 |                     break;
 | 
        
           |  |  | 94 |                 }
 | 
        
           |  |  | 95 |             }
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |             // Si el bloque no está presente, lo añadimos.
 | 
        
           |  |  | 98 |             if (!$blockFound) {
 | 
        
           |  |  | 99 |                 $this->blockManager->add_block('block_' . $blockName, $region);
 | 
        
           |  |  | 100 |             }
 | 
        
           |  |  | 101 |         }
 | 
        
           |  |  | 102 |     }
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |   | 
        
           | 269 | ariadna | 105 |     public function renderBlocks()
 | 
        
           |  |  | 106 |     {
 | 
        
           |  |  | 107 |         global $OUTPUT;
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |         $blocksView = '';
 | 
        
           |  |  | 110 |         // Renderizar bloques para cada región
 | 
        
           |  |  | 111 |         foreach ($this->regions as $region) {
 | 
        
           |  |  | 112 |             $this->regionName = $region;
 | 
        
           |  |  | 113 |             $this->blockManager->load_blocks(true); // Cargar bloques en la región
 | 
        
           |  |  | 114 |             $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
 | 
        
           |  |  | 115 |         }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |         return $blocksView; // Devolver bloques renderizados
 | 
        
           |  |  | 118 |     }
 | 
        
           | 177 | ariadna | 119 | }
 | 
        
           |  |  | 120 |   | 
        
           | 269 | ariadna | 121 |   | 
        
           |  |  | 122 |   | 
        
           | 193 | ariadna | 123 | /* // Instanciamos y renderizamos la página con los bloques estáticos
 | 
        
           | 177 | ariadna | 124 | $statics_blocks = new StaticsBlocks();
 | 
        
           | 193 | ariadna | 125 | echo $statics_blocks->renderBlocks(); */
 |