Proyectos de Subversion Moodle

Rev

Rev 338 | | 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
 
339 ariadna 58
    public function addRegion($regionName)
182 ariadna 59
    {
339 ariadna 60
 
286 ariadna 61
        if (!in_array($this->regionName, $this->blockManager->get_regions())) {
62
            $this->blockManager->add_region($this->regionName);
182 ariadna 63
        }
64
    }
172 efrain 65
 
339 ariadna 66
    public function validateIfExistBlocks($regionName)
182 ariadna 67
    {
68
        $this->blockExists = true;
69
        $this->blocks = $this->blockManager->get_blocks_for_region($this->regionName);
70
        foreach ($this->blockNames as $blockName) {
71
            $blockFound = false;
72
            foreach ($this->blocks as $block) {
73
                $blockclass = get_class($block);
74
                if ($blockclass == 'block_' . $blockName) {
75
                    $blockFound = true;
76
                    break;
77
                }
172 efrain 78
            }
182 ariadna 79
            if (!$blockFound) {
80
                $this->blockExists = false;
81
                break; // Si un bloque no existe, no es necesario seguir buscando
82
            }
172 efrain 83
        }
84
        return $this->blockExists;
182 ariadna 85
    }
172 efrain 86
 
281 ariadna 87
    public function addBlocksIfNotExist($page = 'courses')
182 ariadna 88
    {
89
        if (!$this->blockExists) {
90
            foreach ($this->blockNames as $blockName) {
91
                $this->blockManager->add_block($blockName, $this->regionName, 1, true);
92
            }
172 efrain 93
        }
182 ariadna 94
    }
172 efrain 95
}