forked from moodleou/moodle-mod_subpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
426 lines (400 loc) · 20.1 KB
/
renderer.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?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/>.
/**
* Moodle subpage Renderer
*
* @package mod
* @subpackage subpage
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Dan Marsden <dan@danmarsden.com>
*/
defined('MOODLE_INTERNAL') || die();
class mod_subpage_renderer extends plugin_renderer_base {
protected $subpagecm;
/**
* Render contents of main part of subpage
* @param mod_subpage $subpage
* @param course_modinfo $modinfo
* @param array $sections
* @param boolean $editing whether the user is allowed to edit this page
* @param int $moveitem (currently not used)
* @param boolean $movesection whether the user is allowed to move sections
* @return string html for display
*/
public function render_subpage($subpage, $modinfo, $sections, $editing,
$moveitem, $movesection) {
global $PAGE, $OUTPUT, $CFG, $USER;
$this->subpagecm = $subpage->get_course_module()->id;
if (!empty($USER->activitycopy) && $movesection) {
$content = $this->render_cancel_link($this->subpagecm);
} else {
$content = '';
}
$content .= $this->render_intro($subpage);
$streditsummary = get_string('editsummary');
$strdelete = get_string('delete');
if ($editing) {
$strmoveup = get_string('moveup');
$strmovedown = get_string('movedown');
$strhide = get_string('hide');
$strshow = get_string('show');
$strstealth = get_string('stealth', 'subpage');
$strunstealth = get_string('unstealth', 'subpage');
}
$coursecontext = get_context_instance(CONTEXT_COURSE, $subpage->get_course()->id);
get_all_mods($subpage->get_course()->id, $mods, $modnames, $modnamesplural, $modnamesused);
foreach ($mods as $modid => $unused) {
if (!isset($modinfo->cms[$modid])) {
rebuild_course_cache($subpage->get_course()->id);
$modinfo =& get_fast_modinfo($subpage->get_course());
debugging('Rebuilding course cache', DEBUG_DEVELOPER);
break;
}
}
$lastpageorder = $subpage->get_last_section_pageorder();
if ($subpage->get_course()->format == 'weeks') {
$content .= html_writer::start_tag('ul', array('class' => 'weeks'));
} else {
$content .= html_writer::start_tag('ul', array('class' => 'topics'));
}
foreach ($sections as $section) {
// Check to see whether cms within the section are visible or not
// If all cms are not visible then we don't show the section at all,
// unless editing
$visible = false;
if ($section->sequence) {
// get cm_info for this resources
$instances = explode(',', $section->sequence);
} else {
$instances = array();
}
foreach ($instances as $instance) {
$cm = $modinfo->get_cm($instance);
// check to see whether cm is visible
if ($cm->uservisible) {
$visible = true;
break;
}
}
// If section is empty so should be hidden, record that in object
$section->autohide = !$visible;
$content .= html_writer::start_tag('li',
array('class' => 'section main clearfix', 'id'=>'section-'.$section->section));
$content .= html_writer::tag('div', ' ', array('class' => 'left side'));
$content .= html_writer::start_tag('div', array('class' => 'right side'));
if ($editing && has_capability('moodle/course:update', $coursecontext)) {
// Show the hide/show eye
if ($section->visible) {
$content .= html_writer::start_tag('a', array(
'href' => 'view.php?id=' . $subpage->get_course_module()->id .
'&hide=' . $section->section . '&sesskey=' . sesskey() .
'#section-'.$section->id,
'title'=> $strhide));
$content .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('i/hide'), 'class'=>'icon hide',
'alt'=>$strhide));
$content .= html_writer::end_tag('a');
} else {
$content .= html_writer::start_tag('a', array(
'href' => 'view.php?id=' . $subpage->get_course_module()->id .
'&show=' . $section->section . '&sesskey=' . sesskey() .
'#section-' . $section->id,
'title'=> $strshow));
$content .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('i/show'), 'class'=>'icon show',
'alt'=>$strshow));
$content .= html_writer::end_tag('a');
}
// Show the stealth/unstealth section link
if ($section->stealth) {
$content .= html_writer::start_tag('form',
array('method' => 'post', 'action' => 'stealth.php'));
$content .= html_writer::start_tag('div');
$content .= html_writer::empty_tag('input',
array('name' => 'id', 'value' => $subpage->get_course_module()->id,
'type' => 'hidden'));
$content .= html_writer::empty_tag('input',
array('name' => 'sesskey', 'value' => sesskey(), 'type' => 'hidden'));
$content .= html_writer::empty_tag('input',
array('name' => 'unstealth', 'value' => $section->id,
'type' => 'hidden'));
$content .= html_writer::empty_tag('input',
array('name' => 'icon',
'src' => $OUTPUT->pix_url('unstealth', 'mod_subpage'),
'type' => 'image', 'title' => $strunstealth, 'alt' => $strunstealth));
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('form');
} else {
$content .= html_writer::start_tag('form',
array('method' => 'post', 'action' => 'stealth.php'));
$content .= html_writer::start_tag('div');
$content .= html_writer::empty_tag('input',
array('name' => 'id', 'value' => $subpage->get_course_module()->id,
'type' => 'hidden'));
$content .= html_writer::empty_tag('input',
array('name' => 'sesskey', 'value' => sesskey(), 'type' => 'hidden'));
$content .= html_writer::empty_tag('input',
array('name' => 'stealth', 'value' => $section->id,
'type' => 'hidden'));
$content .= html_writer::empty_tag('input',
array('name' => 'icon',
'src' => $OUTPUT->pix_url('stealth', 'mod_subpage'),
'type' => 'image', 'title' => $strstealth, 'alt' => $strstealth));
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('form');
}
$content .= html_writer::empty_tag('br', array());
if ($movesection) {
$content .= html_writer::start_tag('span', array(
'class' => 'section_move_commands'));
if ($section->pageorder > 1) {
// Add a arrow to move section up
$content .= html_writer::start_tag('a', array(
'href' => 'view.php?id=' . $subpage->get_course_module()->id .
'&random=' . rand(1, 10000) . '§ion=' . $section->id .
'&move=-1&sesskey=' . sesskey() .
'#section-' . ($section->id-1),
'title'=> $strmoveup));
$content .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('t/up'), 'class'=>'icon up',
'alt'=>$strmoveup));
$content .= html_writer::end_tag('a');
$content .= html_writer::empty_tag('br', array());
}
if ($section->pageorder < $lastpageorder) {
// Add an arrow to move section down
$content .= html_writer::start_tag('a', array(
'href' => 'view.php?id=' . $subpage->get_course_module()->id .
'&random=' . rand(1, 10000) . '§ion=' . $section->id .
'&move=1&sesskey=' . sesskey() . '#section-'.($section->id+1),
'title'=> $strmovedown));
$content .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('t/down'), 'class'=>'icon down',
'alt'=>$strmovedown));
$content .= html_writer::end_tag('a');
$content .= html_writer::empty_tag('br', array());
}
$content .= html_writer::end_tag('span');
}
}
$content .= html_writer::end_tag('div');
$content .= html_writer::start_tag('div', array('class' => 'content'));
// Only show the section if visible and not stealthed or to users with permission
if ((($section->visible && !$section->stealth) ||
has_capability('moodle/course:viewhiddensections', $coursecontext)) &&
($editing || !$section->autohide)) {
if ($section->stealth) {
$content .= html_writer::start_tag('div', array('class' => 'stealthed'));
}
if (!empty($section->name)) {
$content .= html_writer::tag('h3', format_string($section->name),
array('class' => 'sectionname'));
}
$summary = '';
if ($section->summary) {
$summarytext = file_rewrite_pluginfile_urls($section->summary,
'pluginfile.php', $coursecontext->id, 'course',
'section', $section->id);
$summaryformatoptions = new stdClass();
$summaryformatoptions->noclean = true;
$summaryformatoptions->overflowdiv = true;
$summary .= format_text($summarytext, $section->summaryformat,
$summaryformatoptions);
}
if ($editing && has_capability('moodle/course:update', $coursecontext)) {
$summary .= html_writer::start_tag('a', array(
'href' => $CFG->wwwroot . '/course/editsection.php?id=' .
$section->id . '&returnurl=' . urlencode($CFG->wwwroot .
'/mod/subpage/view.php?id=' . $subpage->get_course_module()->id .
'&recache=1&sesskey=' . sesskey()),
'title'=> $streditsummary));
$summary .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('t/edit'), 'class'=>'icon edit',
'alt'=>$streditsummary));
$summary .= html_writer::end_tag('a');
$summary .= html_writer::empty_tag('br', array());
$summary .= html_writer::start_tag('a', array(
'href' => $CFG->wwwroot . '/mod/subpage/view.php?id=' .
$subpage->get_course_module()->id . '&delete=' .
$section->id . '&sesskey=' . sesskey(),
'title'=> $strdelete));
if (empty($section->sequence)) {
$summary .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('t/delete'), 'class'=>'icon delete',
'alt'=>$strdelete));
}
$summary .= html_writer::end_tag('a');
$summary .= html_writer::empty_tag('br', array());
$summary .= html_writer::empty_tag('br', array());
}
if ($summary !== '') {
$content .= html_writer::tag('div', $summary, array('class' => 'summary'));
}
$content .= $this->render_section($subpage, $modinfo, $section,
$editing, $moveitem, $mods, $modnamesused);
if ($editing) {
$content .= print_section_add_menus($subpage->get_course(),
$section->section, $modnames, false, true);
if (!empty($CFG->enablecourseajax) and $PAGE->theme->enablecourseajax) {
// hacky way to add list to empty section to allow drag/drop into
// empty sections
$content = str_replace('</div><div class="section_add_menus">',
'</div><ul class="section img-text"><li></li></ul>' .
'<div class="section_add_menus">' , $content);
}
}
//now add returnto links to editing links:
$pattern = '/mod.php\?[A-Za-z0-9-&;=%:\/\-.]+/';
$content = preg_replace_callback($pattern,
'mod_subpage_renderer::subpage_url_regex', $content);
if ($section->stealth) {
$content .= html_writer::end_tag('div');
}
}
$content .= html_writer::end_tag('div'); //end of div class=content
$content .= html_writer::end_tag('li');
}
$content .= html_writer::end_tag('ul');
if ($editing) {
$content .= $this->render_add_button($subpage);
$content .= $this->render_bulkmove_buttons($subpage);
}
return $content;
}
/**
* helper function to add returnurl to strings.
* @param $matches
* @return string
*/
protected function subpage_url_regex($matches) {
global $CFG;
if (strpos($matches[0], 'returnurl')=== false) {
return $matches[0] . "&returnurl=" . $CFG->wwwroot . '/mod/subpage/view.php?id=' .
$this->subpagecm;
} else {
return $matches[0];
}
}
/**
* Display intro section.
* @param mod_subpage $subpage Module object
* @return string Intro HTML or '' if none
*/
public function render_intro(mod_subpage $subpage) {
// Don't output anything if no text, so we don't get styling around
// something blank
if (!$subpage->has_intro()) {
return '';
}
// Make fake activity object in required format, and use to format
// intro for module with standard function (which handles images etc.)
$activity = (object)array('intro' => $subpage->get_intro(),
'introformat' => $subpage->get_intro_format());
$intro = format_module_intro(
'subpage', $activity, $subpage->get_course_module()->id);
// Box styling appears to be consistent with some other modules
$intro = html_writer::tag('div', $intro, array('class' => 'generalbox box',
'id' => 'intro'));
return $intro;
}
/**
* Displays information about the item currently being moved, and a cancel link
* @param int $cmid coursemodule id
* @return string
*/
public function render_cancel_link($cmid) {
global $USER;
$sesskey = sesskey();
$stractivityclipboard =
strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
$cancelurl = new moodle_url('view.php', array('cancelcopy' => true, 'id' => $cmid,
'sesskey' => $sesskey));
$cancellink = html_writer::link($cancelurl, get_string('cancel'));
$content = $stractivityclipboard . ' (' . $cancellink . ')';
return html_writer::tag('div', $content, array('class' => 'clipboard'));
}
/**
* Render single section
*
* @param
* @return
*/
protected function render_section($subpage, $modinfo, $section, $editing,
$moveitem, $mods, $modnamesused) {
global $CFG;
ob_start();
print_section($subpage->get_course(), $section, $mods, $modnamesused, true);
$content = ob_get_contents();
ob_end_clean(); //end buffering.
$content = str_replace($CFG->wwwroot.'/course/mod.php?copy',
'view.php?id='.$subpage->get_course_module()->id.'&copy', $content);
$content = str_replace("'togglecompletion.php'", "'" . $CFG->wwwroot .
"/course/togglecompletion.php'", $content);
$findcontent = "<input type='hidden' name='completionstate'";
$content = str_replace($findcontent, "<input type='hidden' name='backto' value='" .
$CFG->wwwroot.'/mod/subpage/view.php?id=' . $subpage->get_course_module()->id .
"'/>" . $findcontent, $content);
return $content;
}
/**
* Render add button used to add a new section.
*
* @param
* @return
*/
protected function render_add_button($subpage) {
$addsection = new single_button(new moodle_url('view.php', array(
'id'=>$subpage->get_course_module()->id,
'addsection'=>1,
'sesskey'=>sesskey())),
get_string('addsection', 'mod_subpage'), 'get');
return $this->output->render($addsection);
}
/**
* Render buttons used for bulk move.
*
* @param
* @return
*/
protected function render_bulkmove_buttons($subpage) {
$moveto = new single_button(new moodle_url('move.php', array(
'id'=>$subpage->get_course_module()->id,
'move'=>'to',
'sesskey'=>sesskey())),
get_string('movetopage', 'mod_subpage'), 'get');
$buttons = $this->output->render($moveto);
// can only move from if there are sections with modules
$sections = $subpage->get_sections();
if (!empty($sections)) {
$sequence = '';
foreach ($sections as $section) {
if ($section->sequence !== '') {
$sequence .= ','.$section->sequence;
}
}
if ($sequence !== '') {
$movefrom = new single_button(new moodle_url('move.php', array(
'id'=>$subpage->get_course_module()->id,
'move'=>'from',
'sesskey'=>sesskey())),
get_string('movefrompage', 'mod_subpage'), 'get');
$buttons .= $this->output->render($movefrom);
}
}
return $buttons;
}
}