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 |
{
|
378 |
ariadna |
7 |
public $regions = ['side-post', 'side-pre', 'bellow-content', 'right'];
|
261 |
ariadna |
8 |
|
254 |
ariadna |
9 |
public function __construct($title)
|
177 |
ariadna |
10 |
{
|
255 |
ariadna |
11 |
global $USER, $PAGE, $SITE;
|
|
|
12 |
require_login(null, false);
|
|
|
13 |
|
|
|
14 |
if (isguestuser()) {
|
|
|
15 |
throw new require_login_exception('Guests are not allowed here.');
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
$this->userID = optional_param('userid', $USER->id, PARAM_INT);
|
|
|
19 |
$this->currentUser = $this->userID == $USER->id;
|
|
|
20 |
$this->user = core_user::get_user($this->userID);
|
|
|
21 |
$this->title = get_string($title);
|
|
|
22 |
$this->blockManager = $PAGE->blocks;
|
261 |
ariadna |
23 |
|
|
|
24 |
// Definimos varias regiones
|
271 |
ariadna |
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
|
291 |
ariadna |
42 |
$this->addBlocksIfNotExist($this->regionName);
|
279 |
ariadna |
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 |
|
288 |
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);
|
279 |
ariadna |
62 |
|
289 |
ariadna |
63 |
// Crear un array para llevar el registro de los bloques encontrados.
|
|
|
64 |
$foundBlocks = [];
|
|
|
65 |
|
|
|
66 |
// Validar si todos los bloques especificados existen en esta región y que no se repitan.
|
|
|
67 |
foreach ($blocks as $block) {
|
|
|
68 |
$blockClass = get_class($block);
|
|
|
69 |
if (in_array($blockClass, $foundBlocks)) {
|
|
|
70 |
// Si el bloque ya fue encontrado antes, devolvemos false.
|
|
|
71 |
return false;
|
|
|
72 |
}
|
|
|
73 |
$foundBlocks[] = $blockClass;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Verificar si todos los bloques especificados están en la región.
|
288 |
ariadna |
77 |
foreach ($this->blockNames as $blockName) {
|
289 |
ariadna |
78 |
$expectedBlockClass = 'block_' . $blockName;
|
|
|
79 |
if (!in_array($expectedBlockClass, $foundBlocks)) {
|
|
|
80 |
// Si algún bloque esperado no se encuentra, devolvemos false.
|
288 |
ariadna |
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
}
|
279 |
ariadna |
84 |
|
289 |
ariadna |
85 |
// Si todos los bloques existen y son únicos, devolvemos true.
|
288 |
ariadna |
86 |
return true;
|
|
|
87 |
}
|
|
|
88 |
|
291 |
ariadna |
89 |
public function addBlocksIfNotExist($page = 'courses')
|
290 |
ariadna |
90 |
{
|
|
|
91 |
// Obtener los bloques actuales en todas las regiones
|
|
|
92 |
$allBlocks = [];
|
|
|
93 |
foreach ($this->regions as $region) {
|
|
|
94 |
$blocks = $this->blockManager->get_blocks_for_region($region);
|
|
|
95 |
foreach ($blocks as $block) {
|
|
|
96 |
$blockClass = get_class($block);
|
|
|
97 |
if (!isset($allBlocks[$blockClass])) {
|
|
|
98 |
$allBlocks[$blockClass] = $region;
|
|
|
99 |
} else {
|
|
|
100 |
// Si el bloque ya existe en otra región, marcarlo como duplicado
|
|
|
101 |
$allBlocks[$blockClass] = 'duplicate';
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|
288 |
ariadna |
105 |
|
292 |
ariadna |
106 |
$count = 1;
|
|
|
107 |
|
290 |
ariadna |
108 |
// Añadir los bloques si no existen en la región actual
|
|
|
109 |
foreach ($this->blockNames as $blockName) {
|
|
|
110 |
$expectedBlockClass = 'block_' . $blockName;
|
|
|
111 |
|
|
|
112 |
// Verificar si el bloque es único y no está marcado como duplicado
|
291 |
ariadna |
113 |
if (!isset($allBlocks[$expectedBlockClass]) || $allBlocks[$expectedBlockClass] === $this->regionName) {
|
290 |
ariadna |
114 |
// Solo añadir el bloque si no está en la lista de bloques existentes de la región actual
|
291 |
ariadna |
115 |
if (!$this->blockExistsInRegion($expectedBlockClass, $this->regionName)) {
|
292 |
ariadna |
116 |
$this->blockManager->add_block($blockName, $this->regionName, $count, true);
|
|
|
117 |
$count = $count + 1;
|
290 |
ariadna |
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
private function blockExistsInRegion($blockClass, $region)
|
|
|
124 |
{
|
|
|
125 |
// Obtener los bloques actuales en la región especificada
|
|
|
126 |
$blocks = $this->blockManager->get_blocks_for_region($region);
|
|
|
127 |
|
|
|
128 |
// Verificar si el bloque ya existe en la región
|
|
|
129 |
foreach ($blocks as $block) {
|
|
|
130 |
if (get_class($block) === $blockClass) {
|
|
|
131 |
return true;
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
return false;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
|
269 |
ariadna |
139 |
public function renderBlocks()
|
|
|
140 |
{
|
|
|
141 |
global $OUTPUT;
|
|
|
142 |
|
|
|
143 |
$blocksView = '';
|
|
|
144 |
// Renderizar bloques para cada región
|
|
|
145 |
foreach ($this->regions as $region) {
|
|
|
146 |
$this->regionName = $region;
|
|
|
147 |
$this->blockManager->load_blocks(true); // Cargar bloques en la región
|
|
|
148 |
$blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
return $blocksView; // Devolver bloques renderizados
|
|
|
152 |
}
|
177 |
ariadna |
153 |
}
|
|
|
154 |
|
269 |
ariadna |
155 |
|
|
|
156 |
|
193 |
ariadna |
157 |
/* // Instanciamos y renderizamos la página con los bloques estáticos
|
177 |
ariadna |
158 |
$statics_blocks = new StaticsBlocks();
|
193 |
ariadna |
159 |
echo $statics_blocks->renderBlocks(); */
|