Proyectos de Subversion Moodle

Rev

Rev 284 | Rev 287 | 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
 
177 ariadna 3
require_once(__DIR__ . '/cesa.php');
183 ariadna 4
 
177 ariadna 5
class StaticsBlocks extends Cesa
6
{
261 ariadna 7
 
254 ariadna 8
    public function __construct($title)
177 ariadna 9
    {
255 ariadna 10
        global $USER, $PAGE, $SITE;
11
        require_login(null, false);
12
 
13
        if (isguestuser()) {
14
            throw new require_login_exception('Guests are not allowed here.');
15
        }
16
 
17
        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
18
        $this->currentUser = $this->userID == $USER->id;
19
        $this->user = core_user::get_user($this->userID);
20
        $this->title = get_string($title);
21
        $this->blockManager = $PAGE->blocks;
261 ariadna 22
 
23
        // Definimos varias regiones
271 ariadna 24
        $this->regions = ['side-post'];
25
        $this->blockNames = ['cesa_course_rating', 'comments', 'messageteacher', 'mynotes'];
255 ariadna 26
        $this->blockExists = true;
27
 
28
        if (!$this->user || !core_user::is_real_user($this->userID)) {
29
            throw new moodle_exception('invaliduser', 'error');
30
        }
261 ariadna 31
 
264 ariadna 32
        // Asignar y validar los bloques en todas las regiones
33
        foreach ($this->regions as $region) {
34
            $this->regionName = $region;
279 ariadna 35
 
36
            // Añadir la región si no existe
37
            $this->addRegion($this->regionName);
38
 
39
            // Validar si los bloques existen
40
            if (!$this->validateIfExistBlocks($this->regionName)) {
41
                // Añadir bloques si no existen
42
                $this->addBlocksIfNotExist($this->regionName);
43
            }
261 ariadna 44
        }
177 ariadna 45
    }
46
 
286 ariadna 47
    public function addRegion($region)
48
    {
49
        // Validación alternativa: Verificamos si la región está en una lista predefinida de regiones.
50
        $existingRegions = $this->blockManager->get_regions();
51
 
52
        // Si la región no está presente en las regiones existentes, la añadimos.
53
        if (!in_array($region, $existingRegions)) {
54
            $this->blockManager->add_region($region);
55
        }
56
    }
57
 
279 ariadna 58
    public function validateIfExistBlocks($region)
59
    {
60
        // Obtener los bloques de la región actual.
61
        $blocks = $this->blockManager->get_blocks_for_region($region);
62
 
63
        // Validar si todos los bloques especificados existen en esta región.
64
        foreach ($this->blockNames as $blockName) {
65
            $blockFound = false;
66
            foreach ($blocks as $block) {
67
                if (get_class($block) == 'block_' . $blockName) {
68
                    $blockFound = true;
69
                    break;
70
                }
71
            }
72
            // Si algún bloque no se encuentra, devolvemos false.
73
            if (!$blockFound) {
74
                return false;
75
            }
76
        }
77
 
78
        // Si todos los bloques existen, devolvemos true.
79
        return true;
80
    }
81
 
286 ariadna 82
    public function addBlocksIfNotExist($region)
283 ariadna 83
    {
286 ariadna 84
        // Si la variable blockExists es false, intentamos agregar los bloques.
85
        if (!$this->blockExists) {
86
            // Obtener los bloques de la región actual.
87
            $blocks = $this->blockManager->get_blocks_for_region($region);
279 ariadna 88
 
286 ariadna 89
            // Solo añadimos bloques si no existe ninguno de los especificados en blockNames.
90
            foreach ($this->blockNames as $blockName) {
91
                $blockFound = false;
283 ariadna 92
 
286 ariadna 93
                // Buscar si el bloque ya está presente en la región.
94
                foreach ($blocks as $block) {
95
                    if (get_class($block) == 'block_' . $blockName) {
96
                        $blockFound = true;
97
                        break;
98
                    }
283 ariadna 99
                }
100
 
286 ariadna 101
                // Si el bloque no está presente, lo añadimos en la región actual.
102
                if (!$blockFound) {
103
                    $this->blockManager->add_block($blockName, $region, 1, true);
104
                }
283 ariadna 105
            }
106
        }
107
    }
108
 
109
 
286 ariadna 110
 
269 ariadna 111
    public function renderBlocks()
112
    {
113
        global $OUTPUT;
114
 
115
        $blocksView = '';
116
        // Renderizar bloques para cada región
117
        foreach ($this->regions as $region) {
118
            $this->regionName = $region;
119
            $this->blockManager->load_blocks(true); // Cargar bloques en la región
120
            $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
121
        }
122
 
123
        return $blocksView; // Devolver bloques renderizados
124
    }
177 ariadna 125
}
126
 
269 ariadna 127
 
128
 
193 ariadna 129
/* // Instanciamos y renderizamos la página con los bloques estáticos
177 ariadna 130
$statics_blocks = new StaticsBlocks();
193 ariadna 131
echo $statics_blocks->renderBlocks(); */