Proyectos de Subversion Moodle

Rev

Rev 266 | Rev 268 | 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
2
require_once(__DIR__ . '/cesa.php');
183 ariadna 3
 
177 ariadna 4
class StaticsBlocks extends Cesa
5
{
261 ariadna 6
 
254 ariadna 7
    public function __construct($title)
177 ariadna 8
    {
255 ariadna 9
        global $USER, $PAGE, $SITE;
10
        require_login(null, false);
11
 
12
        if (isguestuser()) {
13
            throw new require_login_exception('Guests are not allowed here.');
14
        }
15
 
16
        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
17
        $this->currentUser = $this->userID == $USER->id;
18
        $this->user = core_user::get_user($this->userID);
19
        $this->title = get_string($title);
20
        $this->blockManager = $PAGE->blocks;
261 ariadna 21
 
22
        // Definimos varias regiones
267 ariadna 23
        $this->regions = ['side-pre'];
264 ariadna 24
        $this->blockNames = ['cesa_course_rating', 'comments', 'mynotes'];
255 ariadna 25
        $this->blockExists = true;
26
 
27
        if (!$this->user || !core_user::is_real_user($this->userID)) {
28
            throw new moodle_exception('invaliduser', 'error');
29
        }
261 ariadna 30
 
264 ariadna 31
        // Asignar y validar los bloques en todas las regiones
32
        foreach ($this->regions as $region) {
33
            $this->regionName = $region;
34
            $this->addRegion(); // Añadir la región si no existe
35
            $this->validateIfExistBlocks(); // Validar si los bloques existen
36
            $this->addBlocksIfNotExist(); // Añadir bloques si no existen
261 ariadna 37
        }
177 ariadna 38
    }
39
 
187 ariadna 40
    public function renderBlocks()
177 ariadna 41
    {
224 ariadna 42
        global $OUTPUT;
177 ariadna 43
 
264 ariadna 44
        $blocksView = '';
45
        // Renderizar bloques para cada región
46
        foreach ($this->regions as $region) {
47
            $this->regionName = $region;
48
            $this->blockManager->load_blocks(true); // Cargar bloques en la región
49
            $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
50
        }
177 ariadna 51
 
264 ariadna 52
        return $blocksView; // Devolver bloques renderizados
53
    }
177 ariadna 54
 
264 ariadna 55
    public function addRegion()
56
    {
57
        // Añadir la región si no existe en blockManager
58
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
59
            $this->blockManager->add_region($this->regionName);
60
        }
61
    }
177 ariadna 62
 
264 ariadna 63
    public function validateIfExistBlocks()
64
    {
65
        $this->blockExists = true;
66
        $this->blocks = $this->blockManager->get_blocks_for_region($this->regionName);
177 ariadna 67
 
264 ariadna 68
        foreach ($this->blockNames as $blockName) {
69
            $blockFound = false;
70
            foreach ($this->blocks as $block) {
71
                $blockclass = get_class($block);
72
                if ($blockclass == 'block_' . $blockName) {
73
                    $blockFound = true;
74
                    break;
75
                }
76
            }
77
            if (!$blockFound) {
78
                $this->blockExists = false;
79
                break;
80
            }
81
        }
82
        return $this->blockExists;
177 ariadna 83
    }
264 ariadna 84
 
85
    public function addBlocksIfNotExist($page = 'courses')
86
    {
87
        if (!$this->blockExists) {
88
            foreach ($this->blockNames as $blockName) {
89
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
90
            }
91
        }
92
    }
177 ariadna 93
}
94
 
193 ariadna 95
/* // Instanciamos y renderizamos la página con los bloques estáticos
177 ariadna 96
$statics_blocks = new StaticsBlocks();
193 ariadna 97
echo $statics_blocks->renderBlocks(); */