3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Moodle renderer used to display special elements of the lesson module
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 class mod_lesson_renderer
extends plugin_renderer_base
{
30 * Returns the header for the lesson module
32 * @param lesson $lesson a lesson object.
33 * @param string $currenttab current tab that is shown.
34 * @param bool $extraeditbuttons if extra edit buttons should be displayed.
35 * @param int $lessonpageid id of the lesson page that needs to be displayed.
36 * @param string $extrapagetitle String to appent to the page title.
39 public function header($lesson, $cm, $currenttab = '', $extraeditbuttons = false, $lessonpageid = null, $extrapagetitle = null) {
42 $activityname = format_string($lesson->name
, true, $lesson->course
);
43 if (empty($extrapagetitle)) {
44 $title = $this->page
->course
->shortname
.": ".$activityname;
46 $title = $this->page
->course
->shortname
.": ".$activityname.": ".$extrapagetitle;
50 $context = context_module
::instance($cm->id
);
53 $this->page
->set_title($title);
54 $this->page
->set_heading($this->page
->course
->fullname
);
56 $canmanage = has_capability('mod/lesson:manage', $context);
57 $activityheader = $this->page
->activityheader
;
58 $activitypage = new moodle_url('/mod/' . $this->page
->activityname
. '/view.php');
59 $setactive = $activitypage->compare($this->page
->url
, URL_MATCH_BASE
);
60 if ($activityheader->is_title_allowed()) {
61 $title = $canmanage && $setactive ?
62 $this->output
->heading_with_help($activityname, 'overview', 'lesson') :
64 $activityheader->set_title($title);
67 // If we have the capability to manage the lesson but not within the view page,
68 // there's no reason to show activity/completion information.
69 if ($canmanage && !$setactive) {
70 $activityheader->set_hidecompletion(true);
73 $output = $this->output
->header();
75 foreach ($lesson->messages
as $message) {
76 $output .= $this->output
->notification($message[0], $message[1], $message[2]);
86 public function footer() {
87 return $this->output
->footer();
91 * Returns HTML for a lesson inaccessible message
93 * @param string $message
96 public function lesson_inaccessible($message) {
98 $output = $this->output
->box_start('generalbox boxaligncenter');
99 $output .= $this->output
->box_start('center');
101 $output .= $this->output
->box('<a href="'.$CFG->wwwroot
.'/course/view.php?id='. $this->page
->course
->id
.'">'. get_string('returnto', 'lesson', format_string($this->page
->course
->fullname
, true)) .'</a>', 'lessonbutton standardbutton');
102 $output .= $this->output
->box_end();
103 $output .= $this->output
->box_end();
108 * Returns HTML to prompt the user to log in
109 * @param lesson $lesson
110 * @param bool $failedattempt
113 public function login_prompt(lesson
$lesson, $failedattempt = false) {
115 $output = $this->output
->box_start('password-form');
116 $output .= $this->output
->box_start('generalbox boxaligncenter');
117 $output .= '<form id="password" method="post" action="'.$CFG->wwwroot
.'/mod/lesson/view.php" autocomplete="off">';
118 $output .= '<fieldset class="invisiblefieldset center">';
119 $output .= '<input type="hidden" name="id" value="'. $this->page
->cm
->id
.'" />';
120 $output .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
121 if ($failedattempt) {
122 $output .= $this->output
->notification(get_string('loginfail', 'lesson'));
124 $output .= get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name
)).'<br /><br />';
125 $output .= get_string('enterpassword', 'lesson')." <input type=\"password\" name=\"userpassword\" /><br /><br />";
126 $output .= "<div class='lessonbutton standardbutton submitbutton'><input type='submit' value='".get_string('continue', 'lesson')."' /></div>";
127 $output .= " <div class='lessonbutton standardbutton submitbutton'><input type='submit' name='backtocourse' value='".get_string('cancel', 'lesson')."' /></div>";
128 $output .= '</fieldset></form>';
129 $output .= $this->output
->box_end();
130 $output .= $this->output
->box_end();
135 * Returns HTML to display dependancy errors
137 * @param object $dependentlesson
138 * @param array $errors
141 public function dependancy_errors($dependentlesson, $errors) {
142 $output = $this->output
->box_start('generalbox boxaligncenter');
143 $output .= get_string('completethefollowingconditions', 'lesson', $dependentlesson->name
);
144 $output .= $this->output
->box(implode('<br />'.get_string('and', 'lesson').'<br />', $errors),'center');
145 $output .= $this->output
->box_end();
150 * Returns HTML to display a message
151 * @param string $message
152 * @param single_button $button
155 public function message($message, single_button
$button = null) {
156 $output = $this->output
->box_start('generalbox boxaligncenter');
158 if ($button !== null) {
159 $output .= $this->output
->box($this->output
->render($button), 'lessonbutton standardbutton');
161 $output .= $this->output
->box_end();
166 * Returns HTML to display a continue button
167 * @param lesson $lesson
168 * @param int $lastpageseen
171 public function continue_links(lesson
$lesson, $lastpageseenid) {
173 $output = $this->output
->box(get_string('youhaveseen','lesson'), 'generalbox boxaligncenter');
174 $output .= $this->output
->box_start('center');
176 $yeslink = html_writer
::link(new moodle_url('/mod/lesson/view.php', array('id' => $this->page
->cm
->id
,
177 'pageid' => $lastpageseenid, 'startlastseen' => 'yes')), get_string('yes'), array('class' => 'btn btn-primary'));
178 $output .= html_writer
::tag('span', $yeslink, array('class'=>'lessonbutton standardbutton'));
181 $nolink = html_writer
::link(new moodle_url('/mod/lesson/view.php', array('id' => $this->page
->cm
->id
,
182 'pageid' => $lesson->firstpageid
, 'startlastseen' => 'no')), get_string('no'), array('class' => 'btn btn-secondary'));
183 $output .= html_writer
::tag('span', $nolink, array('class'=>'lessonbutton standardbutton'));
185 $output .= $this->output
->box_end();
190 * Returns HTML to display a page to the user
191 * @param lesson $lesson
192 * @param lesson_page $page
193 * @param object $attempt
196 public function display_page(lesson
$lesson, lesson_page
$page, $attempt) {
197 // We need to buffer here as there is an mforms display call
199 echo $page->display($this, $attempt);
200 $output = ob_get_contents();
206 * Returns HTML to display a collapsed edit form
208 * @param lesson $lesson
212 public function display_edit_collapsed(lesson
$lesson, $pageid) {
215 $manager = lesson_page_type_manager
::get($lesson);
216 $qtypes = $manager->get_page_type_strings();
217 $npages = count($lesson->load_all_pages());
219 $table = new html_table();
220 $table->head
= array(get_string('pagetitle', 'lesson'), get_string('qtype', 'lesson'), get_string('jumps', 'lesson'), get_string('actions', 'lesson'));
221 $table->align
= array('left', 'left', 'left', 'center');
222 $table->data
= array();
224 $canedit = has_capability('mod/lesson:edit', context_module
::instance($this->page
->cm
->id
));
226 while ($pageid != 0) {
227 $page = $lesson->load_page($pageid);
229 $url = new moodle_url('/mod/lesson/edit.php', array(
230 'id' => $this->page
->cm
->id
,
232 'pageid' => $page->id
234 $data[] = html_writer
::link($url, format_string($page->title
, true), array('id' => 'lesson-' . $page->id
));
235 $data[] = $qtypes[$page->qtype
];
236 $data[] = implode("<br />\n", $page->jumps
);
238 $data[] = $this->page_action_links($page, $npages, true);
242 $table->data
[] = $data;
243 $pageid = $page->nextpageid
;
246 return html_writer
::table($table);
250 * Returns HTML to display the full edit page
252 * @param lesson $lesson
254 * @param int $prevpageid
255 * @param bool $single
258 public function display_edit_full(lesson
$lesson, $pageid, $prevpageid, $single=false) {
261 $manager = lesson_page_type_manager
::get($lesson);
262 $qtypes = $manager->get_page_type_strings();
263 $npages = count($lesson->load_all_pages());
264 $canedit = has_capability('mod/lesson:edit', context_module
::instance($this->page
->cm
->id
));
268 $content = $this->add_page_links($lesson, $prevpageid);
271 $options = new stdClass
;
272 $options->noclean
= true;
274 while ($pageid != 0 && $single!=='stop') {
275 $page = $lesson->load_page($pageid);
277 $pagetable = new html_table();
278 $pagetable->align
= array('right','left');
279 $pagetable->width
= '100%';
280 $pagetable->tablealign
= 'center';
281 $pagetable->cellspacing
= 0;
282 $pagetable->cellpadding
= '5px';
283 $pagetable->data
= array();
285 $pageheading = new html_table_cell();
287 $pageheading->text
= html_writer
::tag('a', '', array('id' => 'lesson-' . $pageid)) . format_string($page->title
);
289 $pageheading->text
.= ' '.$this->page_action_links($page, $npages);
291 $pageheading->style
= 'text-align:center';
292 $pageheading->colspan
= 2;
293 $pageheading->scope
= 'col';
294 $pagetable->head
= array($pageheading);
296 $cell = new html_table_cell();
298 $cell->style
= 'text-align:left';
299 $cell->text
= $page->contents
;
300 $pagetable->data
[] = new html_table_row(array($cell));
302 $cell = new html_table_cell();
304 $cell->style
= 'text-align:center';
305 $cell->text
= '<strong>'.$qtypes[$page->qtype
] . $page->option_description_string().'</strong>';
306 $pagetable->data
[] = new html_table_row(array($cell));
308 $pagetable = $page->display_answers($pagetable);
310 $content .= html_writer
::start_tag('div');
311 $content .= html_writer
::table($pagetable);
312 $content .= html_writer
::end_tag('div');
315 $content .= $this->add_page_links($lesson, $pageid);
318 // check the prev links - fix (silently) if necessary - there was a bug in
319 // versions 1 and 2 when add new pages. Not serious then as the backwards
320 // links were not used in those versions
321 if ($page->prevpageid
!= $prevpageid) {
323 $DB->set_field("lesson_pages", "prevpageid", $prevpageid, array("id" => $page->id
));
324 debugging("<p>***prevpageid of page $page->id set to $prevpageid***");
327 $prevpageid = $page->id
;
328 $pageid = $page->nextpageid
;
330 if ($single === true) {
336 return $this->output
->box($content, 'edit_pages_box');
340 * Returns HTML to display the add page links
342 * @param lesson $lesson
343 * @param int $prevpageid
346 public function add_page_links(lesson
$lesson, $prevpageid=false) {
351 $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
352 $links[] = html_writer
::link($importquestionsurl, get_string('importquestions', 'lesson'));
354 $manager = lesson_page_type_manager
::get($lesson);
355 foreach($manager->get_add_page_type_links($prevpageid) as $link) {
356 $links[] = html_writer
::link($link['addurl'], $link['name']);
359 $addquestionurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
360 $links[] = html_writer
::link($addquestionurl, get_string('addaquestionpagehere', 'lesson'));
362 return $this->output
->box(implode(" | \n", $links), 'addlinks');
366 * Return HTML to display add first page links
367 * @param lesson $lesson
370 public function add_first_page_links(lesson
$lesson) {
373 $headinglevel = $this->page
->activityheader
->get_heading_level(3);
374 $output = $this->output
->heading(get_string("whatdofirst", "lesson"), $headinglevel);
377 $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
378 $links[] = html_writer
::link($importquestionsurl, get_string('importquestions', 'lesson'));
380 $manager = lesson_page_type_manager
::get($lesson);
381 foreach ($manager->get_add_page_type_links($prevpageid) as $link) {
382 $link['addurl']->param('firstpage', 1);
383 $links[] = html_writer
::link($link['addurl'], $link['name']);
386 $addquestionurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid, 'firstpage'=>1));
387 $links[] = html_writer
::link($addquestionurl, get_string('addaquestionpage', 'lesson'));
389 return $this->output
->box($output.'<p>'.implode('</p><p>', $links).'</p>', 'generalbox firstpageoptions');
393 * Returns HTML to display action links for a page
395 * @param lesson_page $page
396 * @param bool $printmove
397 * @param bool $printaddpage
400 public function page_action_links(lesson_page
$page, $printmove, $printaddpage=false) {
406 $url = new moodle_url('/mod/lesson/lesson.php',
407 array('id' => $this->page
->cm
->id
, 'action' => 'move', 'pageid' => $page->id
, 'sesskey' => sesskey()));
408 $label = get_string('movepagenamed', 'lesson', format_string($page->title
));
409 $img = $this->output
->pix_icon('t/move', $label);
410 $actions[] = html_writer
::link($url, $img, array('title' => $label));
412 $url = new moodle_url('/mod/lesson/editpage.php', array('id' => $this->page
->cm
->id
, 'pageid' => $page->id
, 'edit' => 1));
413 $label = get_string('updatepagenamed', 'lesson', format_string($page->title
));
414 $img = $this->output
->pix_icon('t/edit', $label);
415 $actions[] = html_writer
::link($url, $img, array('title' => $label));
418 $url = new moodle_url('/mod/lesson/lesson.php', array('id' => $this->page
->cm
->id
, 'pageid' => $page->id
,
419 'action' => 'duplicate', 'sesskey' => sesskey()));
420 $label = get_string('duplicatepagenamed', 'lesson', format_string($page->title
));
421 $img = $this->output
->pix_icon('e/copy', $label, 'mod_lesson');
422 $actions[] = html_writer
::link($url, $img, array('title' => $label));
424 $url = new moodle_url('/mod/lesson/view.php', array('id' => $this->page
->cm
->id
, 'pageid' => $page->id
));
425 $label = get_string('previewpagenamed', 'lesson', format_string($page->title
));
426 $img = $this->output
->pix_icon('t/preview', $label);
427 $actions[] = html_writer
::link($url, $img, array('title' => $label));
429 $url = new moodle_url('/mod/lesson/lesson.php',
430 array('id' => $this->page
->cm
->id
, 'action' => 'confirmdelete', 'pageid' => $page->id
, 'sesskey' => sesskey()));
431 $label = get_string('deletepagenamed', 'lesson', format_string($page->title
));
432 $img = $this->output
->pix_icon('t/delete', $label);
433 $actions[] = html_writer
::link($url, $img, array('title' => $label));
437 $manager = lesson_page_type_manager
::get($page->lesson
);
438 $links = $manager->get_add_page_type_links($page->id
);
439 foreach ($links as $link) {
440 $options[$link['type']] = $link['name'];
442 $options[0] = get_string('addaquestionpage', 'lesson');
444 $addpageurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$page->id
, 'sesskey'=>sesskey()));
445 $addpageselect = new single_select($addpageurl, 'qtype', $options, null, array(''=>get_string('addanewpage', 'lesson').'...'), 'addpageafter'.$page->id
);
446 $addpageselect->attributes
= ['aria-label' => get_string('actions', 'lesson')];
447 $addpageselector = $this->output
->render($addpageselect);
450 if (isset($addpageselector)) {
451 $actions[] = $addpageselector;
454 return implode(' ', $actions);
458 * Prints the on going message to the user.
460 * With custom grading On, displays points
461 * earned out of total points possible thus far.
462 * With custom grading Off, displays number of correct
463 * answers out of total attempted.
465 * @param object $lesson The lesson that the user is taking.
470 * Prints the on going message to the user.
472 * With custom grading On, displays points
473 * earned out of total points possible thus far.
474 * With custom grading Off, displays number of correct
475 * answers out of total attempted.
477 * @param lesson $lesson
480 public function ongoing_score(lesson
$lesson) {
481 return $this->output
->box($lesson->get_ongoing_score_message(), "ongoing center");
485 * Returns HTML to display a progress bar of progression through a lesson
487 * @param lesson $lesson
488 * @param int $progress optional, if empty it will be calculated
491 public function progress_bar(lesson
$lesson, $progress = null) {
492 $context = context_module
::instance($this->page
->cm
->id
);
494 // lesson setting to turn progress bar on or off
495 if (!$lesson->progressbar
) {
500 if (has_capability('mod/lesson:manage', $context)) {
501 return $this->output
->notification(get_string('progressbarteacherwarning2', 'lesson'));
504 if ($progress === null) {
505 $progress = $lesson->calculate_progress();
508 $content = html_writer
::start_tag('div');
509 $content .= html_writer
::start_tag('div', array('class' => 'progress'));
510 $content .= html_writer
::start_tag('div', array('class' => 'progress-bar bar', 'role' => 'progressbar',
511 'style' => 'width: ' . $progress .'%', 'aria-valuenow' => $progress, 'aria-valuemin' => 0, 'aria-valuemax' => 100));
512 $content .= $progress . "%";
513 $content .= html_writer
::end_tag('div');
514 $content .= html_writer
::end_tag('div');
515 $printprogress = html_writer
::tag('div', get_string('progresscompleted', 'lesson', $progress) . $content);
516 return $this->output
->box($printprogress, 'progress_bar');
520 * Returns HTML to show the start of a slideshow
521 * @param lesson $lesson
523 public function slideshow_start(lesson
$lesson) {
524 $attributes = array();
525 $attributes['class'] = 'slideshow';
526 $attributes['style'] = 'background-color:'.$lesson->properties()->bgcolor
.';height:'.
527 $lesson->properties()->height
.'px;width:'.$lesson->properties()->width
.'px;';
528 $output = html_writer
::start_tag('div', $attributes);
532 * Returns HTML to show the end of a slideshow
534 public function slideshow_end() {
535 $output = html_writer
::end_tag('div');
539 * Returns a P tag containing contents
540 * @param string $contents
541 * @param string $class
543 public function paragraph($contents, $class='') {
544 $attributes = array();
546 $attributes['class'] = $class;
548 $output = html_writer
::tag('p', $contents, $attributes);
553 * Returns the HTML for displaying the end of lesson page.
555 * @param lesson $lesson lesson instance
556 * @param stdclass $data lesson data to be rendered
557 * @return string HTML contents
559 public function display_eol_page(lesson
$lesson, $data) {
562 $canmanage = $lesson->can_manage();
563 $course = $lesson->courserecord
;
565 if ($lesson->custom
&& !$canmanage && (($data->gradeinfo
->nquestions
< $lesson->minquestions
))) {
566 $output .= $this->box_start('generalbox boxaligncenter');
569 if ($data->gradelesson
) {
570 $headinglevel = $this->page
->activityheader
->get_heading_level();
571 $output .= $this->heading(get_string("congratulations", "lesson"), $headinglevel);
572 $output .= $this->box_start('generalbox boxaligncenter');
575 if ($data->notenoughtimespent
!== false) {
576 $output .= $this->paragraph(get_string("notenoughtimespent", "lesson", $data->notenoughtimespent
), 'center');
579 if ($data->numberofpagesviewed
!== false) {
580 $output .= $this->paragraph(get_string("numberofpagesviewed", "lesson", $data->numberofpagesviewed
), 'center');
582 if ($data->youshouldview
!== false) {
583 $output .= $this->paragraph(get_string("youshouldview", "lesson", $data->youshouldview
), 'center');
585 if ($data->numberofcorrectanswers
!== false) {
586 $output .= $this->paragraph(get_string("numberofcorrectanswers", "lesson", $data->numberofcorrectanswers
), 'center');
589 if ($data->displayscorewithessays
!== false) {
590 $output .= $this->box(get_string("displayscorewithessays", "lesson", $data->displayscorewithessays
), 'center');
591 } else if ($data->displayscorewithoutessays
!== false) {
592 $output .= $this->box(get_string("displayscorewithoutessays", "lesson", $data->displayscorewithoutessays
), 'center');
595 if ($data->yourcurrentgradeisoutof
!== false) {
596 $output .= $this->paragraph(get_string("yourcurrentgradeisoutof", "lesson", $data->yourcurrentgradeisoutof
), 'center');
598 if ($data->yourcurrentgradeis
!== false) {
599 $output .= $this->paragraph(get_string("yourcurrentgradeis", "lesson", $data->yourcurrentgradeis
), 'center');
601 if ($data->eolstudentoutoftimenoanswers
!== false) {
602 $output .= $this->paragraph(get_string("eolstudentoutoftimenoanswers", "lesson"));
604 if ($data->welldone
!== false) {
605 $output .= $this->paragraph(get_string("welldone", "lesson"));
608 if ($data->progresscompleted
!== false) {
609 $output .= $this->progress_bar($lesson, $data->progresscompleted
);
612 if ($data->displayofgrade
!== false) {
613 $output .= $this->paragraph(get_string("displayofgrade", "lesson"), 'center');
616 $output .= $this->box_end(); // End of Lesson button to Continue.
618 if ($data->reviewlesson
!== false) {
619 $output .= html_writer
::link($data->reviewlesson
, get_string('reviewlesson', 'lesson'),
620 array('class' => 'centerpadded lessonbutton standardbutton pr-3'));
622 if ($data->modattemptsnoteacher
!== false) {
623 $output .= $this->paragraph(get_string("modattemptsnoteacher", "lesson"), 'centerpadded');
626 if ($data->activitylink
!== false) {
627 $output .= $data->activitylink
;
630 $url = new moodle_url('/course/view.php', array('id' => $course->id
));
631 $output .= html_writer
::link($url, get_string('returnto', 'lesson', format_string($course->fullname
, true)),
632 array('class' => 'centerpadded lessonbutton standardbutton pr-3'));
634 if (has_capability('gradereport/user:view', context_course
::instance($course->id
))
635 && $course->showgrades
&& $lesson->grade
!= 0 && !$lesson->practice
) {
636 $url = new moodle_url('/grade/index.php', array('id' => $course->id
));
637 $output .= html_writer
::link($url, get_string('viewgrades', 'lesson'),
638 array('class' => 'centerpadded lessonbutton standardbutton pr-3'));
644 * Render the override action menu.
646 * @param \mod_lesson\output\override_action_menu $overrideactionmenu The overrideactionmenu
648 * @return string The rendered override action menu.
650 public function render_override_action_menu(\mod_lesson\output\override_action_menu
$overrideactionmenu): string {
651 $context = $overrideactionmenu->export_for_template($this);
652 return $this->render_from_template('mod_lesson/override_action_menu', $context);
656 * Render the edit action buttons.
658 * @param \mod_lesson\output\edit_action_buttons $editbuttons The editbuttons
660 * @return string The rendered edit action buttons.
662 public function render_edit_action_buttons(\mod_lesson\output\edit_action_buttons
$editbuttons): string {
663 $context = $editbuttons->export_for_template($this);
664 return $this->render_from_template('mod_lesson/edit_action_buttons', $context);
668 * Render the edit action area.
670 * @param \mod_lesson\output\edit_action_area $editarea The edit area.
671 * @return string The rendered edit action area.
673 public function render_edit_action_area(\mod_lesson\output\edit_action_area
$editarea): string {
674 $context = $editarea->export_for_template($this);
675 return $this->render_from_template('mod_lesson/edit_action_area', $context);
679 * Render the report action menu
681 * @param \mod\lesson\output\report_action_menu $reportmenu The reportmenu.
682 * @return string The rendered report action menu.
684 public function render_report_action_menu(\mod_lesson\output\report_action_menu
$reportmenu): string {
685 $context = $reportmenu->export_for_template($this);
686 return $this->render_from_template('mod_lesson/report_action_menu', $context);