Proyectos de Subversion Moodle

Rev

Rev 282 | Rev 284 | 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
 
279 ariadna 47
    public function addRegion($region)
48
    {
49
        // Si la región no está presente en el bloque de la página, la añadimos.
50
        if (!$this->blockManager->region_exists($region)) {
51
            $this->blockManager->add_region($region);
52
        }
53
    }
54
 
55
    public function validateIfExistBlocks($region)
56
    {
57
        // Obtener los bloques de la región actual.
58
        $blocks = $this->blockManager->get_blocks_for_region($region);
59
 
60
        // Validar si todos los bloques especificados existen en esta región.
61
        foreach ($this->blockNames as $blockName) {
62
            $blockFound = false;
63
            foreach ($blocks as $block) {
64
                if (get_class($block) == 'block_' . $blockName) {
65
                    $blockFound = true;
66
                    break;
67
                }
68
            }
69
            // Si algún bloque no se encuentra, devolvemos false.
70
            if (!$blockFound) {
71
                return false;
72
            }
73
        }
74
 
75
        // Si todos los bloques existen, devolvemos true.
76
        return true;
77
    }
78
 
283 ariadna 79
    public function addBlocksIfNotExist()
80
    {
81
        // Obtener los bloques de la región actual.
82
        $blocks = $this->blockManager->get_blocks_for_region($this->$regionName);
279 ariadna 83
 
283 ariadna 84
        foreach ($this->blockNames as $blockName) {
85
            $blockFound = false;
86
 
87
            // Buscar si el bloque ya está presente en la región.
88
            foreach ($blocks as $block) {
89
                if (get_class($block) == 'block_' . $blockName) {
90
                    $blockFound = true;
91
                    break;
92
                }
93
            }
94
 
95
            // Si el bloque no está presente, lo añadimos.
96
            if (!$blockFound) {
97
                $this->blockManager->add_block('block_' . $blockName, $this->$regionName);
98
            }
99
        }
100
    }
101
 
102
 
269 ariadna 103
    public function renderBlocks()
104
    {
105
        global $OUTPUT;
106
 
107
        $blocksView = '';
108
        // Renderizar bloques para cada región
109
        foreach ($this->regions as $region) {
110
            $this->regionName = $region;
111
            $this->blockManager->load_blocks(true); // Cargar bloques en la región
112
            $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
113
        }
114
 
115
        return $blocksView; // Devolver bloques renderizados
116
    }
177 ariadna 117
}
118
 
269 ariadna 119
 
120
 
193 ariadna 121
/* // Instanciamos y renderizamos la página con los bloques estáticos
177 ariadna 122
$statics_blocks = new StaticsBlocks();
193 ariadna 123
echo $statics_blocks->renderBlocks(); */