forked from DigiDago/moodle-format_softcourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
362 lines (316 loc) · 13.5 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
<?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/>.
/**
* Renderer for outputting the Soft Course format.
*
* @package format_softcourse
* @copyright 2018 Pimenko <contact@pimneko.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.5
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/course/format/renderer.php');
/**
* Basic renderer for softcourse format.
*
* @copyright 2012 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class format_softcourse_renderer extends format_section_renderer_base {
/**
* @var stdClass Course
*/
private $course;
/**
* @var stdClass Course format
*/
private $courseformat = null;
/**
* @var stdClass Mod info
*/
private $modinfo = null;
/**
* Constructor method, calls the parent constructor
*
* @param moodle_page $page
* @param string $target one of rendering target constants
* @throws moodle_exception
*/
public function __construct(moodle_page $page, $target) {
parent::__construct($page, $target);
$this->course = $page->course;
$this->courseformat = course_get_format($this->course);
$this->modinfo = get_fast_modinfo($this->course);
// Since format_softcourse_renderer::section_edit_controls() only displays
// the 'Set current section' control when editing mode is on
// we need to be sure that the link 'Turn editing mode on' is available for a user
// who does not have any other managing capability.
$page->set_other_editing_capability('moodle/course:setcurrentsection');
}
/**
* Generate the starting container html for a list of sections
* @return string HTML to output.
*/
protected function start_section_list() {
return html_writer::start_tag('ul', array('class' => 'softcourse'));
}
/**
* Generate the closing container html for a list of sections
* @return string HTML to output.
*/
protected function end_section_list() {
return html_writer::end_tag('ul');
}
/**
* Generate the title for this section page
* @return string the page title
*/
protected function page_title() {
return get_string('topicoutline');
}
/**
* Generate the section title, wraps it in a link to the section page if page is to be displayed on a separate page
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course The course entry from DB
* @return string HTML to output.
*/
public function section_title($section, $course) {
return $this->render(course_get_format($course)->inplace_editable_render_section_name($section));
}
/**
* Generate the section title to be displayed on the section page, without a link
*
* @param stdClass $section The course_section entry from DB
* @param stdClass $course The course entry from DB
* @return string HTML to output.
*/
public function section_title_without_link($section, $course) {
return $this->render(course_get_format($course)->inplace_editable_render_section_name($section, false));
}
/**
* Generate the edit control items of a section
*
* @param stdClass $course The course entry from DB
* @param stdClass $section The course_section entry from DB
* @param bool $onsectionpage true if being printed on a section page
* @return array of edit control items
*/
protected function section_edit_control_items($course, $section, $onsectionpage = false) {
global $PAGE;
if (!$PAGE->user_is_editing()) {
return array();
}
$coursecontext = context_course::instance($course->id);
if ($onsectionpage) {
$url = course_get_url($course, $section->section);
} else {
$url = course_get_url($course);
}
$url->param('sesskey', sesskey());
$controls = array();
if ($section->section && has_capability('moodle/course:setcurrentsection', $coursecontext)) {
if ($course->marker == $section->section) { // Show the "light globe" on/off.
$url->param('marker', 0);
$markedthissection = get_string('markedthistopic');
$highlightoff = get_string('highlightoff');
$controls['highlight'] = array('url' => $url, "icon" => 'i/marked',
'name' => $highlightoff,
'pixattr' => array('class' => '', 'alt' => $markedthissection),
'attr' => array('class' => 'editing_highlight', 'title' => $markedthissection,
'data-action' => 'removemarker'));
} else {
$url->param('marker', $section->section);
$markthissection = get_string('markedthistopic');
$highlight = get_string('highlight');
$controls['highlight'] = array('url' => $url, "icon" => 'i/marker',
'name' => $highlight,
'pixattr' => array('class' => '', 'alt' => $markthissection),
'attr' => array('class' => 'editing_highlight', 'title' => $markthissection,
'data-action' => 'setmarker'));
}
}
$parentcontrols = parent::section_edit_control_items($course, $section, $onsectionpage);
// If the edit key exists, we are going to insert our controls after it.
if (array_key_exists("edit", $parentcontrols)) {
$merged = array();
// We can't use splice because we are using associative arrays.
// Step through the array and merge the arrays.
foreach ($parentcontrols as $key => $action) {
$merged[$key] = $action;
if ($key == "edit") {
// If we have come to the edit key, merge these controls here.
$merged = array_merge($merged, $controls);
}
}
return $merged;
} else {
return array_merge($controls, $parentcontrols);
}
}
/**
* Output the html for a single section page .
*
* @param stdClass $course The course entry from DB
* @param array $sections (argument not used)
* @param array $mods (argument not used)
* @param array $modnames (argument not used)
* @param array $modnamesused (argument not used)
* @param int $displaysection The section number in the course which is being displayed
* @throws moodle_exception
*/
public function print_single_section_page($course, $sections, $mods, $modnames, $modnamesused, $displaysection) {
echo $this->course_introduction();
}
/**
* Output the html for a multiple section page.
*
* @param stdClass $course The course entry from DB
* @param array $sections (argument not used)
* @param array $mods (argument not used)
* @param array $modnames (argument not used)
* @param array $modnamesused (argument not used)
* @throws moodle_exception
*/
public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
global $PAGE;
$context = context_course::instance($this->course->id);
if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
parent::print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused);
} else {
echo $this->course_introduction();
if ($this->courseformat->get_format_options()['hideallsections'] == 0) {
echo $this->course_sections();
}
}
}
/**
* Return le template rendered of the introduction of the course
*
* @throws moodle_exception
*/
public function course_introduction() {
$template = new stdClass();
$template->start_url = "";
foreach ($this->modinfo->get_cms() as $cm) {
if ($cm->uservisible && !$cm->is_stealth() || !empty($cm->url)) {
$template->start_url = $cm->url;
break;
}
}
if ($template->start_url == "") {
$template->disabledStart = "disabled";
}
// Get the introduction.
$summary = $this->courseformat->get_format_options()['introduction'];
// Rewrite url of files (like pictures).
$context = context_course::instance($this->course->id);
$summarytext = file_rewrite_pluginfile_urls($summary, 'pluginfile.php',
$context->id, 'course', 'introduction', 0);
$summarytext = $summary;
$options = new stdClass();
$options->noclean = true;
$options->overflowdiv = true;
$template->summary = format_text($summarytext, 1, $options);
// Button Start.
$template->start = get_string('startcourse', 'format_softcourse');
return $this->render_from_template('format_softcourse/introduction', $template);
}
/**
* Return le template rendered of the sections of the course
*
* @throws moodle_exception
*/
public function course_sections() {
global $COURSE;
$template = $template = new stdClass();
$sections = [];
$sectionmods = [];
$completioninfo = new completion_info($COURSE);
$context = context_course::instance($this->course->id);
// Assiociation tabs : section[id_of_section] = cm.
foreach ($this->modinfo->get_cms() as $cm) {
$idsection = $cm->get_section_info()->section;
// Hide the section 0 if the course format option is set to "Hide the section 0".
if (!($idsection == 0 && $this->courseformat->get_format_options()['hidesectionzero'] == 1)) {
$info = $this->modinfo->get_section_info_all()[$idsection];
if (!isset($sectionmods[$idsection])) {
$sectionmods[$idsection] = new stdClass();
$sectionmods[$idsection]->cm = [];
$sectionmods[$idsection]->id = $idsection;
$sectionmods[$idsection]->name = $info->name;
$sectionmods[$idsection]->summary = $info->summary;
}
$sectionmods[$idsection]->cm[] = $cm;
}
}
// Put tabs into a tabs readable by mustache.
foreach ($sectionmods as $section) {
$s = new stdClass();
$s->name = $section->name;
$s->id = $section->id;
$s->courseid = $this->course->id;
$s->summary = $section->summary;
$s->start = get_string('startcourse', 'format_softcourse');
$s->countactivitiestooltip = get_string('countactivities', 'format_softcourse');
$s->countactivities = 0;
if (has_capability('moodle/course:update', $context)) {
$s->update_img = get_string('update_img', 'format_softcourse');
$s->delete_img = get_string('delete_img', 'format_softcourse');
}
// Render the iamge section.
$fs = get_file_storage();
$file = $fs->get_area_files($context->id, 'format_softcourse', 'sectionimage', $s->id,
"itemid, filepath, filename", false);
if ($file) {
$s->urlimg = \moodle_url::make_pluginfile_url(
end($file)->get_contextid(),
end($file)->get_component(),
end($file)->get_filearea(),
end($file)->get_itemid(),
end($file)->get_filepath(),
end($file)->get_filename()
);
}
$nbcomplete = 0;
$nbcompletion = 0;
$s->first_cm_url = '';
$s->countactivities = 0;
// Get completion of cms.
foreach ($section->cm as $cm) {
if (($cm->uservisible && !$cm->is_stealth() || !empty($cm->url)) && $s->first_cm_url == '') {
$s->first_cm_url = $cm->url;
}
if ($cm->completion > 0) {
$nbcompletion++;
}
$nbcomplete += $completioninfo->get_data($cm, true)->completionstate;
if ($cm->deletioninprogress == 0 && $cm->visible == 1 && $cm->modname != "label") {
$s->countactivities += 1;
}
}
// Count the percent of cm complete.
if ($nbcompletion != 0) {
$s->progression = get_string('progression', 'format_softcourse');
$percentcomplete = $nbcomplete * 100 / $nbcompletion;
$s->progression_percent = intval($percentcomplete);
}
$sections[] = $s;
}
$template->sections = $sections;
return $this->render_from_template('format_softcourse/sections', $template);
}
}