Proyectos de Subversion Moodle

Rev

Rev 285 | Rev 338 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
172 efrain 1
<?php
2
//namespace Cesa;
3
require_once(__DIR__ . '/../config.php');
4
require_once($CFG->libdir . '/navigationlib.php');
182 ariadna 5
require_once($CFG->libdir . '/blocklib.php');
6
 
172 efrain 7
use \require_login_exception;
8
use \core_user;
9
use \moodle_exception;
10
use \context_user;
11
 
12
/**
13
 *
14
 * Cesa
15
 *
16
 */
17
 
182 ariadna 18
class Cesa
19
{
172 efrain 20
 
182 ariadna 21
    public $user;
172 efrain 22
    public $title;
23
    public $blocks;
24
    public $userID;
25
    public $currentUser;
26
    public $blockManager;
27
    public $blockExists;
207 ariadna 28
    public $blockNames;
172 efrain 29
    public $regionName;
30
 
182 ariadna 31
    public function __construct($title, $blockNames, $regionName)
32
    {
33
        global $USER, $PAGE, $SITE;
172 efrain 34
        require_login(null, false);
35
 
36
        if (isguestuser()) {
37
            throw new require_login_exception('Guests are not allowed here.');
38
        }
39
 
40
        $this->userID = optional_param('userid', $USER->id, PARAM_INT);
41
        $this->currentUser = $this->userID == $USER->id;
42
        $this->user = core_user::get_user($this->userID);
43
        $this->title = get_string($title);
44
        $this->blockManager = $PAGE->blocks;
45
        $this->regionName = $regionName;
182 ariadna 46
        $this->blockNames = is_array($blockNames) ? $blockNames : [$blockNames];
47
        $this->blockExists = true;
172 efrain 48
 
49
        if (!$this->user || !core_user::is_real_user($this->userID)) {
50
            throw new moodle_exception('invaliduser', 'error');
51
        }
52
 
253 ariadna 53
        $PAGE->set_context(context_user::instance($this->userID));
54
        $PAGE->set_pagelayout('mydashboard');
55
        $PAGE->set_title("{$SITE->shortname}: " . $this->title);
182 ariadna 56
    }
172 efrain 57
 
286 ariadna 58
    public function addRegion($regionName)
182 ariadna 59
    {
286 ariadna 60
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
61
            $this->blockManager->add_region($this->regionName);
182 ariadna 62
        }
63
    }
172 efrain 64
 
286 ariadna 65
    public function validateIfExistBlocks($regionName)
182 ariadna 66
    {
67
        $this->blockExists = true;
68
        $this->blocks = $this->blockManager->get_blocks_for_region($this->regionName);
69
        foreach ($this->blockNames as $blockName) {
70
            $blockFound = false;
71
            foreach ($this->blocks as $block) {
72
                $blockclass = get_class($block);
73
                if ($blockclass == 'block_' . $blockName) {
74
                    $blockFound = true;
75
                    break;
76
                }
172 efrain 77
            }
182 ariadna 78
            if (!$blockFound) {
79
                $this->blockExists = false;
80
                break; // Si un bloque no existe, no es necesario seguir buscando
81
            }
172 efrain 82
        }
83
        return $this->blockExists;
182 ariadna 84
    }
172 efrain 85
 
281 ariadna 86
    public function addBlocksIfNotExist($page = 'courses')
182 ariadna 87
    {
88
        if (!$this->blockExists) {
89
            foreach ($this->blockNames as $blockName) {
90
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
91
            }
172 efrain 92
        }
182 ariadna 93
    }
172 efrain 94
}