Proyectos de Subversion Moodle

Rev

Rev 268 | Rev 270 | 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'];
268 ariadna 24
        $this->blockNames = ['cesa_course_rating', 'comments'];
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
 
264 ariadna 40
    public function addRegion()
41
    {
42
        // Añadir la región si no existe en blockManager
43
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
44
            $this->blockManager->add_region($this->regionName);
45
        }
46
    }
177 ariadna 47
 
264 ariadna 48
    public function validateIfExistBlocks()
49
    {
50
        $this->blockExists = true;
51
        $this->blocks = $this->blockManager->get_blocks_for_region($this->regionName);
177 ariadna 52
 
264 ariadna 53
        foreach ($this->blockNames as $blockName) {
54
            $blockFound = false;
55
            foreach ($this->blocks as $block) {
56
                $blockclass = get_class($block);
57
                if ($blockclass == 'block_' . $blockName) {
58
                    $blockFound = true;
59
                    break;
60
                }
61
            }
62
            if (!$blockFound) {
63
                $this->blockExists = false;
64
                break;
65
            }
66
        }
67
        return $this->blockExists;
177 ariadna 68
    }
264 ariadna 69
 
70
    public function addBlocksIfNotExist($page = 'courses')
71
    {
72
        if (!$this->blockExists) {
73
            foreach ($this->blockNames as $blockName) {
74
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
75
            }
76
        }
77
    }
269 ariadna 78
 
79
    public function renderBlocks()
80
    {
81
        global $OUTPUT;
82
 
83
        $blocksView = '';
84
        // Renderizar bloques para cada región
85
        foreach ($this->regions as $region) {
86
            $this->regionName = $region;
87
            $this->blockManager->load_blocks(true); // Cargar bloques en la región
88
            $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
89
        }
90
 
91
        return $blocksView; // Devolver bloques renderizados
92
    }
177 ariadna 93
}
94
 
269 ariadna 95
 
96
 
193 ariadna 97
/* // Instanciamos y renderizamos la página con los bloques estáticos
177 ariadna 98
$statics_blocks = new StaticsBlocks();
193 ariadna 99
echo $statics_blocks->renderBlocks(); */