Proyectos de Subversion Moodle

Rev

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