Proyectos de Subversion Moodle

Rev

Rev 395 | Rev 397 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
    {
379 ariadna 43
        $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
389 ariadna 44
 
45
        $blockNamesInBlocks = array_map(fn($block) => $block->name(), $blocks);
46
 
379 ariadna 47
        $this->blockExists = true;
279 ariadna 48
 
396 ariadna 49
 
288 ariadna 50
        foreach ($this->blockNames as $blockName) {
396 ariadna 51
            if (!in_array($blockName, $blockNamesInBlocks)) {
52
                $this->blockExists = false;
53
                break;
290 ariadna 54
            }
55
        }
288 ariadna 56
 
379 ariadna 57
        return $this->blockExists;
290 ariadna 58
    }
59
 
393 ariadna 60
 
379 ariadna 61
    public function addBlocksIfNotExist()
290 ariadna 62
    {
379 ariadna 63
        if (!$this->blockExists) {
64
            foreach ($this->blockNames as $blockName) {
65
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
290 ariadna 66
            }
67
        }
68
    }
69
 
269 ariadna 70
    public function renderBlocks()
71
    {
72
        global $OUTPUT;
73
 
379 ariadna 74
        $this->addRegion(); // Validar si la región existe y añadirla en caso de no existir
75
 
385 ariadna 76
        $this->blockManager->load_blocks(true); // Cargar bloques en la región
77
 
391 ariadna 78
        $this->validateIfExistBlocks(); // Valida si el listado de bloques pasados por parametros coincide con los bloques cargados
392 ariadna 79
        // $this->addBlocksIfNotExist(); // Agrega los bloques en caso de no coincidir
379 ariadna 80
 
395 ariadna 81
        $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
392 ariadna 82
        $blockNamesInBlocks = array_map(fn($block) => $block->name(), $blocks);
386 ariadna 83
 
396 ariadna 84
 
85
        if($this->blockExists){
86
            return "Existen todos los bloques"
87
        }
88
 
394 ariadna 89
        return $blockNamesInBlocks[0]; // Devolver los bloques de la región
269 ariadna 90
    }
177 ariadna 91
}