Proyectos de Subversion Moodle

Rev

Rev 378 | Rev 380 | 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 $title;
7
    public $userID;
8
    public $currentUser;
9
    public $blockManager;
10
    public $blockExists;
11
    public $blockNames;
12
    public $regionName;
261 ariadna 13
 
379 ariadna 14
    public function __construct($title, $blockNames, $regionName)
177 ariadna 15
    {
379 ariadna 16
        global $USER, $PAGE;
255 ariadna 17
        require_login(null, false);
18
 
19
        if (isguestuser()) {
20
            throw new require_login_exception('Guests are not allowed here.');
21
        }
22
 
23
        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
24
        $this->currentUser = $this->userID == $USER->id;
25
        $this->user = core_user::get_user($this->userID);
26
        $this->title = get_string($title);
27
        $this->blockManager = $PAGE->blocks;
379 ariadna 28
        $this->blockNames = $blockNames;
29
        $this->regionName = $regionName;
261 ariadna 30
 
255 ariadna 31
        if (!$this->user || !core_user::is_real_user($this->userID)) {
32
            throw new moodle_exception('invaliduser', 'error');
33
        }
177 ariadna 34
    }
35
 
379 ariadna 36
    public function addRegion()
286 ariadna 37
    {
379 ariadna 38
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
39
            $this->blockManager->add_region($this->regionName);
286 ariadna 40
        }
41
    }
42
 
379 ariadna 43
    public function validateIfExistBlocks()
288 ariadna 44
    {
379 ariadna 45
        $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
46
        $this->blockExists = true;
279 ariadna 47
 
288 ariadna 48
        foreach ($this->blockNames as $blockName) {
379 ariadna 49
            $blockFound = false;
290 ariadna 50
            foreach ($blocks as $block) {
379 ariadna 51
                $blockclass = get_class($block);
52
                if ($blockclass == 'block_' . $blockName) {
53
                    $blockFound = true;
54
                    break;
290 ariadna 55
                }
56
            }
379 ariadna 57
            if (!$blockFound) {
58
                $this->blockExists = false;
59
                break; // Si un bloque no existe, no es necesario seguir buscando
60
            }
290 ariadna 61
        }
288 ariadna 62
 
379 ariadna 63
        return $this->blockExists;
290 ariadna 64
    }
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
 
79
        $blocksView = '';
80
 
379 ariadna 81
        $this->addRegion(); // Validar si la región existe y añadirla en caso de no existir
82
 
83
        $this->blockManager->load_blocks(true); // Cargar bloques en la región
84
 
85
        $this->validateIfExistBlocks(); // Valida si el listado de bloques pasados por parametros coincide con los bloques cargados
86
        $this->addBlocksIfNotExist(); // Agrega los bloques en caso de no coincidir
87
 
88
        $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar los bloques de la región
89
 
269 ariadna 90
        return $blocksView; // Devolver bloques renderizados
91
    }
177 ariadna 92
}