Proyectos de Subversion Moodle

Rev

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