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