| 177 | ariadna | 1 | <?php
 | 
        
           | 279 | ariadna | 2 |   | 
        
           | 379 | ariadna | 3 | class StaticsBlocks
 | 
        
           | 177 | ariadna | 4 | {
 | 
        
           | 379 | ariadna | 5 |     public $user;
 | 
        
           |  |  | 6 |     public $userID;
 | 
        
           |  |  | 7 |     public $currentUser;
 | 
        
           |  |  | 8 |     public $blockManager;
 | 
        
           |  |  | 9 |     public $blockExists;
 | 
        
           |  |  | 10 |     public $blockNames;
 | 
        
           |  |  | 11 |     public $regionName;
 | 
        
           | 261 | ariadna | 12 |   | 
        
           | 383 | ariadna | 13 |     public function __construct($regionName, $blockNames)
 | 
        
           | 177 | ariadna | 14 |     {
 | 
        
           | 379 | ariadna | 15 |         global $USER, $PAGE;
 | 
        
           | 255 | ariadna | 16 |         require_login(null, false);
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 |         if (isguestuser()) {
 | 
        
           |  |  | 19 |             throw new require_login_exception('Guests are not allowed here.');
 | 
        
           |  |  | 20 |         }
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 |         $this->userID = optional_param('userid', $USER->id, PARAM_INT);
 | 
        
           |  |  | 23 |         $this->currentUser = $this->userID == $USER->id;
 | 
        
           |  |  | 24 |         $this->user = core_user::get_user($this->userID);
 | 
        
           |  |  | 25 |         $this->blockManager = $PAGE->blocks;
 | 
        
           | 379 | ariadna | 26 |         $this->blockNames = $blockNames;
 | 
        
           |  |  | 27 |         $this->regionName = $regionName;
 | 
        
           | 261 | ariadna | 28 |   | 
        
           | 255 | ariadna | 29 |         if (!$this->user || !core_user::is_real_user($this->userID)) {
 | 
        
           |  |  | 30 |             throw new moodle_exception('invaliduser', 'error');
 | 
        
           |  |  | 31 |         }
 | 
        
           | 177 | ariadna | 32 |     }
 | 
        
           |  |  | 33 |   | 
        
           | 379 | ariadna | 34 |     public function addRegion()
 | 
        
           | 286 | ariadna | 35 |     {
 | 
        
           | 379 | ariadna | 36 |         if (!in_array($this->regionName, $this->blockManager->get_regions())) {
 | 
        
           |  |  | 37 |             $this->blockManager->add_region($this->regionName);
 | 
        
           | 286 | ariadna | 38 |         }
 | 
        
           |  |  | 39 |     }
 | 
        
           |  |  | 40 |   | 
        
           | 379 | ariadna | 41 |     public function validateIfExistBlocks()
 | 
        
           | 288 | ariadna | 42 |     {
 | 
        
           | 389 | ariadna | 43 |         // Obtener los bloques para la región especificada
 | 
        
           | 379 | ariadna | 44 |         $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
 | 
        
           | 389 | ariadna | 45 |   | 
        
           |  |  | 46 |         // Verificamos que hay bloques disponibles y que el array de blockNames no esté vacío
 | 
        
           |  |  | 47 |         if (empty($this->blockNames) || empty($blocks)) {
 | 
        
           |  |  | 48 |             $this->blockExists = false; // No hay bloques que validar
 | 
        
           |  |  | 49 |             return $this->blockExists;
 | 
        
           |  |  | 50 |         }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |         // Extraer los nombres de los bloques en un array
 | 
        
           |  |  | 53 |         $blockNamesInBlocks = array_map(fn($block) => $block->name(), $blocks);
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |         // Asumimos que todos los bloques existen inicialmente
 | 
        
           | 379 | ariadna | 56 |         $this->blockExists = true;
 | 
        
           | 279 | ariadna | 57 |   | 
        
           | 389 | ariadna | 58 |         // Verificar si cada bloque en blockNames existe en blockNamesInBlocks
 | 
        
           | 288 | ariadna | 59 |         foreach ($this->blockNames as $blockName) {
 | 
        
           | 389 | ariadna | 60 |             if (!in_array($blockName, $blockNamesInBlocks, true)) {
 | 
        
           |  |  | 61 |                 $this->blockExists = false; // Si falta algún bloque, lo marcamos
 | 
        
           |  |  | 62 |                 break; // Salimos al encontrar el primer bloque faltante
 | 
        
           | 290 | ariadna | 63 |             }
 | 
        
           |  |  | 64 |         }
 | 
        
           | 288 | ariadna | 65 |   | 
        
           | 379 | ariadna | 66 |         return $this->blockExists;
 | 
        
           | 290 | ariadna | 67 |     }
 | 
        
           |  |  | 68 |   | 
        
           | 393 | ariadna | 69 |   | 
        
           | 379 | ariadna | 70 |     public function addBlocksIfNotExist()
 | 
        
           | 290 | ariadna | 71 |     {
 | 
        
           | 379 | ariadna | 72 |         if (!$this->blockExists) {
 | 
        
           |  |  | 73 |             foreach ($this->blockNames as $blockName) {
 | 
        
           |  |  | 74 |                 $this->blockManager->add_block($blockName, $this->regionName, 1, true);
 | 
        
           | 290 | ariadna | 75 |             }
 | 
        
           |  |  | 76 |         }
 | 
        
           |  |  | 77 |     }
 | 
        
           |  |  | 78 |   | 
        
           | 269 | ariadna | 79 |     public function renderBlocks()
 | 
        
           |  |  | 80 |     {
 | 
        
           |  |  | 81 |         global $OUTPUT;
 | 
        
           |  |  | 82 |   | 
        
           | 379 | ariadna | 83 |         $this->addRegion(); // Validar si la región existe y añadirla en caso de no existir
 | 
        
           |  |  | 84 |   | 
        
           | 385 | ariadna | 85 |         $this->blockManager->load_blocks(true); // Cargar bloques en la región
 | 
        
           |  |  | 86 |   | 
        
           | 391 | ariadna | 87 |         $this->validateIfExistBlocks(); // Valida si el listado de bloques pasados por parametros coincide con los bloques cargados
 | 
        
           | 392 | ariadna | 88 |         // $this->addBlocksIfNotExist(); // Agrega los bloques en caso de no coincidir
 | 
        
           | 379 | ariadna | 89 |   | 
        
           | 395 | ariadna | 90 |         $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
 | 
        
           | 392 | ariadna | 91 |         $blockNamesInBlocks = array_map(fn($block) => $block->name(), $blocks);
 | 
        
           | 386 | ariadna | 92 |   | 
        
           | 394 | ariadna | 93 |         return $blockNamesInBlocks[0]; // Devolver los bloques de la región
 | 
        
           | 269 | ariadna | 94 |     }
 | 
        
           | 177 | ariadna | 95 | }
 |