-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock_rolespecifichtml.php
231 lines (193 loc) · 7.61 KB
/
block_rolespecifichtml.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
/**
* @package block_rolespecifichtml
* @category blocks
* @author Valery Fremaux (valery.fremaux@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/lib/filelib.php');
class block_rolespecifichtml extends block_base {
public function init() {
$this->title = get_string('pluginname', 'block_rolespecifichtml');
}
public function applicable_formats() {
return array('all' => true, 'admin' => false, 'my' => false);
}
public function specialization() {
$this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_rolespecifichtml'));
}
public function instance_allow_multiple() {
return true;
}
public function get_content() {
global $COURSE, $CFG, $USER;
if ($this->content !== NULL) {
return $this->content;
}
$filteropt = new stdClass;
$filteropt->overflowdiv = true;
if ($this->content_is_trusted()) {
// Fancy html allowed only on course, category and system blocks.
$filteropt->noclean = true;
}
$this->content = new stdClass;
$roleids = null;
if (@$this->config->display == 'highest') {
$highest = $this->get_highest_role();
if ($highest) {
$roleids = array($highest);
}
} else {
$roleids = $this->get_user_roles();
}
$this->content = new stdClass;
$this->content->text = '';
$tk = "text_all";
if (!isset($this->config)) {
$this->config = new StdClass();
}
$this->config->$tk = file_rewrite_pluginfile_urls(@$this->config->$tk, 'pluginfile.php', $this->context->id, 'block_rolespecifichtml', 'content', null);
if (is_array($this->config->$tk)) {
$arr = $this->config->$tk;
$this->content->text .= !empty($arr['text']) ? format_text($arr['text'], $arr['format'], $filteropt) : '';
} else {
$this->content->text .= !empty($this->config->$tk) ? format_text($this->config->$tk, FORMAT_HTML, $filteropt) : '';
}
if (!empty($roleids)) {
foreach ($roleids as $roleid) {
$tk = "text_$roleid";
$this->config->$tk = file_rewrite_pluginfile_urls(@$this->config->$tk, 'pluginfile.php', $this->context->id, 'block_rolespecifichtml', 'content', null);
if (is_array($this->config->$tk)) {
$arr = $this->config->$tk;
$this->content->text .= !empty($arr['text']) ? format_text($arr['text'], $arr['format'], $filteropt) : '';
} else {
$this->content->text .= !empty($this->config->$tk) ? format_text($this->config->$tk, FORMAT_HTML, $filteropt) : '';
}
}
}
$this->content->footer = '';
unset($filteropt); // Memory footprint.
if (empty($this->content->text)) $this->content->text = ' ';
return $this->content;
}
/**
* Serialize and store config data
*/
public function instance_config_save($data, $nolongerused = false) {
global $DB, $COURSE, $USER;
$context = context::instance_by_id($this->instance->parentcontextid);
$config = clone($data);
// Move embedded files into a proper filearea and adjust HTML links to match.
$config->text_all = file_save_draft_area_files($data->text_all['itemid'], $this->context->id, 'block_rolespecificthtml', 'content', 0, array('subdirs' => true), $data->text_all['text']);
$config->format_all = $data->text_all['format'];
if (!$context) {
$contextlevel = CONTEXT_COURSE;
} else {
$contextlevel = $context->contextlevel;
}
$roles = get_roles_for_contextlevels($contextlevel);
if (!empty($roles)) {
foreach (array_values($roles) as $rid) {
$tk = 'text_'.$rid;
$fk = 'format_'.$rid;
$config->{$tk} = file_save_draft_area_files(@$data->{$tk}['itemid'], $this->context->id, 'block_rolespecificthtml', 'content', 0, array('subdirs' => true), @$data->{$tk}['text']);
$config->{$fk} = @$data->{$tk}['format'];
}
}
parent::instance_config_save($config, $nolongerused);
}
public function instance_delete() {
global $DB;
$fs = get_file_storage();
$fs->delete_area_files($this->context->id, 'block_rolespecifichtml');
return true;
}
public function content_is_trusted() {
global $SCRIPT;
if (!$context = context::instance_by_id($this->instance->parentcontextid)) {
return false;
}
// Find out if this block is on the profile page.
if ($context->contextlevel == CONTEXT_USER) {
if ($SCRIPT === '/my/index.php') {
/*
* this is exception - page is completely private, nobody else may see content there
* that is why we allow JS here
*/
return true;
} else {
// No JS on public personal pages, it would be a big security issue.
return false;
}
}
return true;
}
/**
* The block should only be dockable when the title of the block is not empty
* and when parent allows docking.
*
* @return bool
*/
public function instance_can_be_docked() {
return (!empty($this->config->title) && parent::instance_can_be_docked());
}
/*
* Hide the title bar when none set..
*/
public function hide_header() {
return empty($this->config->title);
}
/**
* get highest role in context
*/
public function get_highest_role() {
global $COURSE, $USER;
if (empty($this->config) || $this->config->context == 'course') {
$context = context_course::instance($COURSE->id);
} else {
$context = context_system::instance();
}
$roles = get_user_roles($context, $USER->id, false);
if ($roles) {
if ($highest = array_shift($roles)) {
return $highest->roleid;
}
}
return 0;
}
/**
* get all roles in context
*/
public function get_user_roles() {
global $COURSE, $USER;
if (empty($this->config) || $this->config->context == 'course') {
$context = context_course::instance($COURSE->id);
} else {
$context = context_system::instance();
}
$roles = get_user_roles($context, $USER->id, false);
$roleids = array();
if (!empty($roles)) {
foreach($roles as $r) {
$roleids[] = $r->roleid;
}
return $roleids;
}
return null;
}
}