Proyectos de Subversion Moodle

Rev

Rev 378 | Rev 380 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 378 Rev 379
Línea 1... Línea 1...
1
<?php
1
<?php
Línea 2... Línea -...
2
 
-
 
3
require_once(__DIR__ . '/cesa.php');
-
 
4
 
2
 
5
class StaticsBlocks extends Cesa
3
class StaticsBlocks
-
 
4
{
-
 
5
    public $user;
-
 
6
    public $title;
-
 
7
    public $userID;
6
{
8
    public $currentUser;
-
 
9
    public $blockManager;
-
 
10
    public $blockExists;
-
 
11
    public $blockNames;
Línea 7... Línea 12...
7
    public $regions = ['side-post', 'side-pre', 'bellow-content', 'right'];
12
    public $regionName;
8
 
13
 
9
    public function __construct($title)
14
    public function __construct($title, $blockNames, $regionName)
10
    {
15
    {
Línea 11... Línea 16...
11
        global $USER, $PAGE, $SITE;
16
        global $USER, $PAGE;
12
        require_login(null, false);
17
        require_login(null, false);
13
 
18
 
Línea 18... Línea 23...
18
        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
23
        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
19
        $this->currentUser = $this->userID == $USER->id;
24
        $this->currentUser = $this->userID == $USER->id;
20
        $this->user = core_user::get_user($this->userID);
25
        $this->user = core_user::get_user($this->userID);
21
        $this->title = get_string($title);
26
        $this->title = get_string($title);
22
        $this->blockManager = $PAGE->blocks;
27
        $this->blockManager = $PAGE->blocks;
23
 
-
 
24
        // Definimos varias regiones
28
        $this->blockNames = $blockNames;
25
        $this->blockNames = ['cesa_course_rating', 'comments', 'messageteacher', 'mynotes'];
-
 
26
        $this->blockExists = true;
29
        $this->regionName = $regionName;
Línea 27... Línea 30...
27
 
30
 
28
        if (!$this->user || !core_user::is_real_user($this->userID)) {
31
        if (!$this->user || !core_user::is_real_user($this->userID)) {
29
            throw new moodle_exception('invaliduser', 'error');
32
            throw new moodle_exception('invaliduser', 'error');
30
        }
-
 
31
 
-
 
32
        // Asignar y validar los bloques en todas las regiones
-
 
33
        foreach ($this->regions as $region) {
-
 
34
            $this->regionName = $region;
-
 
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
            }
-
 
44
        }
33
        }
Línea 45... Línea 34...
45
    }
34
    }
46
 
35
 
47
    public function addRegion($region)
-
 
48
    {
36
    public function addRegion()
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.
37
    {
53
        if (!in_array($region, $existingRegions)) {
38
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
54
            $this->blockManager->add_region($region);
39
            $this->blockManager->add_region($this->regionName);
Línea 55... Línea 40...
55
        }
40
        }
56
    }
41
    }
57
 
-
 
58
    public function validateIfExistBlocks($region)
42
 
59
    {
-
 
60
        // Obtener los bloques de la región actual.
-
 
61
        $blocks = $this->blockManager->get_blocks_for_region($region);
43
    public function validateIfExistBlocks()
62
 
-
 
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;
-
 
Línea 72... Línea -...
72
            }
-
 
73
            $foundBlocks[] = $blockClass;
44
    {
74
        }
-
 
75
 
-
 
76
        // Verificar si todos los bloques especificados están en la región.
-
 
77
        foreach ($this->blockNames as $blockName) {
45
        $blocks = $this->blockManager->get_blocks_for_region($this->regionName);
78
            $expectedBlockClass = 'block_' . $blockName;
-
 
79
            if (!in_array($expectedBlockClass, $foundBlocks)) {
-
 
80
                // Si algún bloque esperado no se encuentra, devolvemos false.
-
 
81
                return false;
-
 
82
            }
-
 
83
        }
-
 
84
 
-
 
85
        // Si todos los bloques existen y son únicos, devolvemos true.
-
 
86
        return true;
-
 
87
    }
-
 
88
 
-
 
89
    public function addBlocksIfNotExist($page = 'courses')
-
 
90
    {
-
 
91
        // Obtener los bloques actuales en todas las regiones
46
        $this->blockExists = true;
92
        $allBlocks = [];
47
 
93
        foreach ($this->regions as $region) {
48
        foreach ($this->blockNames as $blockName) {
94
            $blocks = $this->blockManager->get_blocks_for_region($region);
49
            $blockFound = false;
95
            foreach ($blocks as $block) {
50
            foreach ($blocks as $block) {
96
                $blockClass = get_class($block);
-
 
97
                if (!isset($allBlocks[$blockClass])) {
-
 
98
                    $allBlocks[$blockClass] = $region;
51
                $blockclass = get_class($block);
99
                } else {
52
                if ($blockclass == 'block_' . $blockName) {
100
                    // Si el bloque ya existe en otra región, marcarlo como duplicado
-
 
101
                    $allBlocks[$blockClass] = 'duplicate';
-
 
102
                }
53
                    $blockFound = true;
103
            }
-
 
104
        }
-
 
105
 
54
                    break;
106
        $count = 1;
-
 
107
 
-
 
108
        // Añadir los bloques si no existen en la región actual
55
                }
109
        foreach ($this->blockNames as $blockName) {
-
 
110
            $expectedBlockClass = 'block_' . $blockName;
-
 
111
 
-
 
112
            // Verificar si el bloque es único y no está marcado como duplicado
-
 
113
            if (!isset($allBlocks[$expectedBlockClass]) || $allBlocks[$expectedBlockClass] === $this->regionName) {
-
 
114
                // Solo añadir el bloque si no está en la lista de bloques existentes de la región actual
-
 
115
                if (!$this->blockExistsInRegion($expectedBlockClass, $this->regionName)) {
56
            }
116
                    $this->blockManager->add_block($blockName, $this->regionName, $count, true);
57
            if (!$blockFound) {
-
 
58
                $this->blockExists = false;
-
 
59
                break; // Si un bloque no existe, no es necesario seguir buscando
117
                    $count = $count + 1;
60
            }
Línea 118... Línea 61...
118
                }
61
        }
119
            }
62
 
120
        }
-
 
121
    }
-
 
122
 
-
 
123
    private function blockExistsInRegion($blockClass, $region)
63
        return $this->blockExists;
124
    {
64
    }
125
        // Obtener los bloques actuales en la región especificada
65
 
126
        $blocks = $this->blockManager->get_blocks_for_region($region);
-
 
127
 
66
    public function addBlocksIfNotExist()
128
        // Verificar si el bloque ya existe en la región
67
    {
129
        foreach ($blocks as $block) {
-
 
130
            if (get_class($block) === $blockClass) {
-
 
131
                return true;
68
        if (!$this->blockExists) {
Línea 132... Línea -...
132
            }
-
 
133
        }
69
            foreach ($this->blockNames as $blockName) {
134
 
70
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
135
        return false;
71
            }
Línea 136... Línea 72...
136
    }
72
        }
137
 
-
 
138
 
-
 
139
    public function renderBlocks()
-
 
140
    {
-
 
141
        global $OUTPUT;
-
 
142
 
-
 
Línea 143... Línea 73...
143
        $blocksView = '';
73
    }
144
        // Renderizar bloques para cada región
-
 
145
        foreach ($this->regions as $region) {
-
 
Línea -... Línea 74...
-
 
74
 
Línea -... Línea 75...
-
 
75
    public function renderBlocks()
-
 
76
    {
Línea 146... Línea 77...
146
            $this->regionName = $region;
77
        global $OUTPUT;
147
            $this->blockManager->load_blocks(true); // Cargar bloques en la región
-
 
-
 
78
 
148
            $blocksView .= $OUTPUT->blocks_for_region($this->regionName); // Renderizar bloques
79
        $blocksView = '';
-
 
80
 
-
 
81
        $this->addRegion(); // Validar si la región existe y añadirla en caso de no existir