forked from vfremaux/moodle-block_groupspecifichtml
/
block_groupspecifichtml.php
190 lines (155 loc) · 6.36 KB
/
block_groupspecifichtml.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
<?php //$Id: block_groupspecifichtml.php,v 1.2 2012-07-10 16:42:00 vf Exp $
class block_groupspecifichtml extends block_base {
function init() {
$this->title = get_string('pluginname', 'block_groupspecifichtml');
}
function applicable_formats() {
return array('all' => false, 'course' => true);
}
function specialization() {
$this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_groupspecifichtml'));
}
function instance_allow_multiple() {
return true;
}
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;
$this->content->text = '';
$usegroupmenu = true;
if (has_capability('moodle/site:accessallgroups', context_course::instance($COURSE->id))){
$usegroupmenu = true;
} else {
$usergroupings = groups_get_user_groups($COURSE->id, $USER->id);
if (count($usergroupings) == 1){
$usergroups = array_pop($usergroupings);
if (count($usergroups) == 1){
$usegroupmenu = false;
$uniquegroup = array_pop($usergroups);
}
}
}
$coursegroups = groups_get_all_groups($COURSE->id);
if (($coursegroups) && $usegroupmenu){
$this->content->text .= groups_print_course_menu($COURSE, $CFG->wwwroot.'/course/view.php?id='.$COURSE->id, true);
}
$gid = 0 + groups_get_course_group($COURSE, $USER->id);
if (@$uniquegroup && !$gid){
$gid = $uniquegroup;
}
$textkeys = array('text_all');
if (!empty($coursegroups)){
$textkeys[] = 'text_'.$gid;
}
foreach($textkeys as $tk){
if (isset($this->config->$tk)) {
$format = FORMAT_HTML;
// Check to see if the format has been properly set on the config
$formatkey = str_replace('text_', 'format_', $tk);
if (isset($this->config->$formatkey)) {
$format = $this->config->$formatkey;
}
// rewrite url
$this->config->$tk = file_rewrite_pluginfile_urls($this->config->$tk, 'pluginfile.php', $this->context->id, 'block_groupspecifichtml', 'content', NULL);
// Default to FORMAT_HTML which is what will have been used before the
// editor was properly implemented for the block.
$this->content->text .= format_text($this->config->$tk, $format, $filteropt);
} else {
$this->content->text .= '';
}
}
$this->content->footer = '';
unset($filteropt); // memory footprint
if (empty($this->content->text)) $this->content->text = ' ';
return $this->content;
}
/**
* Serialize and store config data
*/
function instance_config_save($data, $nolongerused = false) {
global $DB, $COURSE;
$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_groupspecificthtml', 'content', 0, array('subdirs' => true), $data->text_all['text']);
$config->format_all = $data->text_all['format'];
$config->text_0 = file_save_draft_area_files($data->text_all['itemid'], $this->context->id, 'block_groupspecificthtml', 'content', 0, array('subdirs' => true), $data->text_0['text']);
$config->format_0 = $data->text_0['format'];
$groups = groups_get_all_groups($COURSE->id);
if (!empty($groups)){
foreach($groups as $g){
$textkey = 'text_'.$g->id;
$formatkey = 'format_'.$g->id;
$config->{$textkey} = file_save_draft_area_files($data->{$textkey}['itemid'], $this->context->id, 'block_groupspecificthtml', 'content', 0, array('subdirs' => true), $data->{$textkey}['text']);
$config->{$formatkey} = $data->{$textkey}['format'];
}
}
parent::instance_config_save($config, $nolongerused);
}
function instance_delete() {
global $DB;
$fs = get_file_storage();
$fs->delete_area_files($this->context->id, 'block_groupspecifichtml');
return true;
}
function content_is_trusted() {
global $SCRIPT;
if (!$context = get_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..
*/
function hide_header(){
return empty($this->config->title);
}
/*
function get_group_content($gid, $grouponly = false){
$text = '';
if ($this->instance->pagetype === 'course-view') {
// fancy html allowed only on course page and in pinned blocks for security reasons
$filteropt = new stdClass;
$filteropt->noclean = true;
} else {
$filteropt = null;
}
if (!$grouponly){
$textkey = "text_all";
$text .= !empty($this->config->$textkey) ? format_text($this->config->$textkey, FORMAT_HTML, $filteropt) : '';
}
$textkey = "text_$gid";
$text .= isset($this->config->$textkey) ? format_text($this->config->$textkey, FORMAT_HTML, $filteropt) : '';
return $text;
}
*/
}