Proyectos de Subversion Moodle

Rev

Rev 387 | Rev 389 | 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);
44
        $this->blockExists = true;
279 ariadna 45
 
288 ariadna 46
        foreach ($this->blockNames as $blockName) {
379 ariadna 47
            $blockFound = false;
384 ariadna 48
 
290 ariadna 49
            foreach ($blocks as $block) {
388 ariadna 50
                $blockclass = $block->name();
51
                if ($blockclass == $blockName) {
379 ariadna 52
                    $blockFound = true;
53
                    break;
290 ariadna 54
                }
55
            }
379 ariadna 56
            if (!$blockFound) {
57
                $this->blockExists = false;
58
                break; // Si un bloque no existe, no es necesario seguir buscando
59
            }
290 ariadna 60
        }
288 ariadna 61
 
379 ariadna 62
        return $this->blockExists;
290 ariadna 63
    }
64
 
384 ariadna 65
 
379 ariadna 66
    public function addBlocksIfNotExist()
290 ariadna 67
    {
379 ariadna 68
        if (!$this->blockExists) {
69
            foreach ($this->blockNames as $blockName) {
70
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
290 ariadna 71
            }
72
        }
73
    }
74
 
269 ariadna 75
    public function renderBlocks()
76
    {
77
        global $OUTPUT;
78
 
379 ariadna 79
        $this->addRegion(); // Validar si la región existe y añadirla en caso de no existir
80
 
385 ariadna 81
        $this->blockManager->load_blocks(true); // Cargar bloques en la región
82
 
386 ariadna 83
        $blocksExist = $this->validateIfExistBlocks(); // Valida si el listado de bloques pasados por parametros coincide con los bloques cargados
379 ariadna 84
 
386 ariadna 85
        if (!$blocksExist) {
86
            $this->addBlocksIfNotExist(); // Agrega los bloques en caso de no coincidir
87
        }
88
 
385 ariadna 89
        return $OUTPUT->blocks_for_region($this->regionName); // Devolver los bloques de la región
269 ariadna 90
    }
177 ariadna 91
}