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
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
29 class mod_lesson_renderer
extends plugin_renderer_base
{
31 * Returns the header for the lesson module
33 * @param lesson $lesson
34 * @param string $currenttab
35 * @param bool $extraeditbuttons
36 * @param int $lessonpageid
39 public function header($lesson, $cm, $currenttab = '', $extraeditbuttons = false, $lessonpageid = null) {
42 $activityname = format_string($lesson->name
, true, $lesson->course
);
43 $title = $this->page
->course
->shortname
.": ".$activityname;
46 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
49 $this->page
->set_title($title);
50 $this->page
->set_heading($this->page
->course
->fullname
);
51 lesson_add_header_buttons($cm, $context, $extraeditbuttons, $lessonpageid);
52 $output = $this->output
->header();
54 if (has_capability('mod/lesson:manage', $context)) {
55 $output .= $this->output
->heading_with_help($activityname, 'overview', 'lesson');
57 if (!empty($currenttab)) {
59 include($CFG->dirroot
.'/mod/lesson/tabs.php');
60 $output .= ob_get_contents();
64 $output .= $this->output
->heading($activityname);
67 foreach ($lesson->messages
as $message) {
68 $output .= $this->output
->notification($message[0], $message[1], $message[2]);
78 public function footer() {
79 return $this->output
->footer();
83 * Returns HTML for a lesson inaccessible message
85 * @param string $message
88 public function lesson_inaccessible($message) {
90 $output = $this->output
->box_start('generalbox boxaligncenter');
91 $output .= $this->output
->box_start('center');
93 $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');
94 $output .= $this->output
->box_end();
95 $output .= $this->output
->box_end();
100 * Returns HTML to prompt the user to log in
101 * @param lesson $lesson
102 * @param bool $failedattempt
105 public function login_prompt(lesson
$lesson, $failedattempt = false) {
107 $output = $this->output
->box_start('password-form');
108 $output .= $this->output
->box_start('generalbox boxaligncenter');
109 $output .= '<form id="password" method="post" action="'.$CFG->wwwroot
.'/mod/lesson/view.php" autocomplete="off">';
110 $output .= '<fieldset class="invisiblefieldset center">';
111 $output .= '<input type="hidden" name="id" value="'. $this->page
->cm
->id
.'" />';
112 if ($failedattempt) {
113 $output .= $this->output
->notification(get_string('loginfail', 'lesson'));
115 $output .= get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name
)).'<br /><br />';
116 $output .= get_string('enterpassword', 'lesson')." <input type=\"password\" name=\"userpassword\" /><br /><br />";
117 $output .= "<div class='lessonbutton standardbutton submitbutton'><input type='submit' value='".get_string('continue', 'lesson')."' /></div>";
118 $output .= " <div class='lessonbutton standardbutton submitbutton'><input type='submit' name='backtocourse' value='".get_string('cancel', 'lesson')."' /></div>";
119 $output .= '</fieldset></form>';
120 $output .= $this->output
->box_end();
121 $output .= $this->output
->box_end();
126 * Returns HTML to display dependancy errors
128 * @param object $dependentlesson
129 * @param array $errors
132 public function dependancy_errors($dependentlesson, $errors) {
133 $output = $this->output
->box_start('generalbox boxaligncenter');
134 $output .= get_string('completethefollowingconditions', 'lesson', $dependentlesson->name
);
135 $output .= $this->output
->box(implode('<br />'.get_string('and', 'lesson').'<br />', $errors),'center');
136 $output .= $this->output
->box_end();
141 * Returns HTML to display a message
142 * @param string $message
143 * @param single_button $button
146 public function message($message, single_button
$button = null) {
147 $output = $this->output
->box_start('generalbox boxaligncenter');
149 if ($button !== null) {
150 $output .= $this->output
->box($this->output
->render($button), 'lessonbutton standardbutton');
152 $output .= $this->output
->box_end();
157 * Returns HTML to display a continue button
158 * @param lesson $lesson
159 * @param int $lastpageseen
162 public function continue_links(lesson
$lesson, $lastpageseenid) {
164 $output = $this->output
->box(get_string('youhaveseen','lesson'), 'generalbox boxaligncenter');
165 $output .= $this->output
->box_start('center');
167 $yeslink = html_writer
::link(new moodle_url('/mod/lesson/view.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$lastpageseenid, 'startlastseen'=>'yes')), get_string('yes'));
168 $output .= html_writer
::tag('span', $yeslink, array('class'=>'lessonbutton standardbutton'));
171 $nolink = html_writer
::link(new moodle_url('/mod/lesson/view.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$lesson->firstpageid
, 'startlastseen'=>'no')), get_string('no'));
172 $output .= html_writer
::tag('span', $nolink, array('class'=>'lessonbutton standardbutton'));
174 $output .= $this->output
->box_end();
179 * Returns HTML to display a page to the user
180 * @param lesson $lesson
181 * @param lesson_page $page
182 * @param object $attempt
185 public function display_page(lesson
$lesson, lesson_page
$page, $attempt) {
186 // We need to buffer here as there is an mforms display call
188 echo $page->display($this, $attempt);
189 $output = ob_get_contents();
195 * Returns HTML to display a collapsed edit form
197 * @param lesson $lesson
201 public function display_edit_collapsed(lesson
$lesson, $pageid) {
204 $manager = lesson_page_type_manager
::get($lesson);
205 $qtypes = $manager->get_page_type_strings();
206 $npages = count($lesson->load_all_pages());
208 $table = new html_table();
209 $table->head
= array(get_string('pagetitle', 'lesson'), get_string('qtype', 'lesson'), get_string('jumps', 'lesson'), get_string('actions', 'lesson'));
210 $table->align
= array('left', 'left', 'left', 'center');
211 $table->wrap
= array('', 'nowrap', '', 'nowrap');
212 $table->tablealign
= 'center';
213 $table->cellspacing
= 0;
214 $table->cellpadding
= '2px';
215 $table->width
= '80%';
216 $table->data
= array();
218 $canedit = has_capability('mod/lesson:edit', get_context_instance(CONTEXT_MODULE
, $this->page
->cm
->id
));
220 while ($pageid != 0) {
221 $page = $lesson->load_page($pageid);
223 $data[] = "<a href=\"$CFG->wwwroot/mod/lesson/edit.php?id=".$this->page
->cm
->id
."&mode=single&pageid=".$page->id
."\">".format_string($page->title
,true).'</a>';
224 $data[] = $qtypes[$page->qtype
];
225 $data[] = implode("<br />\n", $page->jumps
);
227 $data[] = $this->page_action_links($page, $npages, true);
231 $table->data
[] = $data;
232 $pageid = $page->nextpageid
;
235 return html_writer
::table($table);
239 * Returns HTML to display the full edit page
241 * @param lesson $lesson
243 * @param int $prevpageid
244 * @param bool $single
247 public function display_edit_full(lesson
$lesson, $pageid, $prevpageid, $single=false) {
250 $manager = lesson_page_type_manager
::get($lesson);
251 $qtypes = $manager->get_page_type_strings();
252 $npages = count($lesson->load_all_pages());
253 $canedit = has_capability('mod/lesson:edit', get_context_instance(CONTEXT_MODULE
, $this->page
->cm
->id
));
257 $content = $this->add_page_links($lesson, $prevpageid);
260 $options = new stdClass
;
261 $options->noclean
= true;
263 while ($pageid != 0 && $single!=='stop') {
264 $page = $lesson->load_page($pageid);
266 $pagetable = new html_table();
267 $pagetable->align
= array('right','left');
268 $pagetable->width
= '100%';
269 $pagetable->tablealign
= 'center';
270 $pagetable->cellspacing
= 0;
271 $pagetable->cellpadding
= '5px';
272 $pagetable->data
= array();
274 $pageheading = new html_table_cell();
276 $pageheading->text
= format_string($page->title
);
278 $pageheading->text
.= ' '.$this->page_action_links($page, $npages);
280 $pageheading->style
= 'text-align:center';
281 $pageheading->colspan
= 2;
282 $pageheading->scope
= 'col';
283 $pagetable->head
= array($pageheading);
285 $cell = new html_table_cell();
287 $cell->style
= 'text-align:left';
288 $cell->text
= $page->contents
;
289 $pagetable->data
[] = new html_table_row(array($cell));
291 $cell = new html_table_cell();
293 $cell->style
= 'text-align:center';
294 $cell->text
= '<strong>'.$qtypes[$page->qtype
] . $page->option_description_string().'</strong>';
295 $pagetable->data
[] = new html_table_row(array($cell));
297 $pagetable = $page->display_answers($pagetable);
299 $content .= html_writer
::table($pagetable);
302 $content .= $this->add_page_links($lesson, $pageid);
305 // check the prev links - fix (silently) if necessary - there was a bug in
306 // versions 1 and 2 when add new pages. Not serious then as the backwards
307 // links were not used in those versions
308 if ($page->prevpageid
!= $prevpageid) {
310 $DB->set_field("lesson_pages", "prevpageid", $prevpageid, array("id" => $page->id
));
311 debugging("<p>***prevpageid of page $page->id set to $prevpageid***");
314 $prevpageid = $page->id
;
315 $pageid = $page->nextpageid
;
317 if ($single === true) {
323 return $this->output
->box($content, 'edit_pages_box');
327 * Returns HTML to display the add page links
329 * @param lesson $lesson
330 * @param int $prevpageid
333 public function add_page_links(lesson
$lesson, $prevpageid=false) {
338 $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
339 $links[] = html_writer
::link($importquestionsurl, get_string('importquestions', 'lesson'));
341 $manager = lesson_page_type_manager
::get($lesson);
342 foreach($manager->get_add_page_type_links($prevpageid) as $link) {
343 $links[] = html_writer
::link($link['addurl'], $link['name']);
346 $addquestionurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
347 $links[] = html_writer
::link($addquestionurl, get_string('addaquestionpagehere', 'lesson'));
349 return $this->output
->box(implode(" | \n", $links), 'addlinks');
353 * Return HTML to display add first page links
354 * @param lesson $lesson
357 public function add_first_page_links(lesson
$lesson) {
361 $output = $this->output
->heading(get_string("whatdofirst", "lesson"), 3);
364 $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
365 $links[] = html_writer
::link($importquestionsurl, get_string('importquestions', 'lesson'));
367 $importppturl = new moodle_url('/mod/lesson/importppt.php',array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid));
368 $links[] = html_writer
::link($importppturl, get_string('importppt', 'lesson'));
370 $manager = lesson_page_type_manager
::get($lesson);
371 foreach ($manager->get_add_page_type_links($prevpageid) as $link) {
372 $link['addurl']->param('firstpage', 1);
373 $links[] = html_writer
::link($link['addurl'], $link['name']);
376 $addquestionurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$prevpageid, 'firstpage'=>1));
377 $links[] = html_writer
::link($addquestionurl, get_string('addaquestionpage', 'lesson'));
379 return $this->output
->box($output.'<p>'.implode('</p><p>', $links).'</p>', 'generalbox firstpageoptions');
383 * Returns HTML to display action links for a page
385 * @param lesson_page $page
386 * @param bool $printmove
387 * @param bool $printaddpage
390 public function page_action_links(lesson_page
$page, $printmove, $printaddpage=false) {
396 $printmovehtml = new moodle_url('/mod/lesson/lesson.php', array('id'=>$this->page
->cm
->id
, 'action'=>'move', 'pageid'=>$page->id
, 'sesskey'=>sesskey()));
397 $actions[] = html_writer
::link($printmovehtml, '<img src="'.$this->output
->pix_url('t/move').'" class="iconsmall" alt="'.get_string('move').'" />');
399 $url = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$page->id
, 'edit'=>1));
400 $actions[] = html_writer
::link($url, '<img src="'.$this->output
->pix_url('t/edit').'" class="iconsmall" alt="'.get_string('update').'" />');
402 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$page->id
));
403 $actions[] = html_writer
::link($url, '<img src="'.$this->output
->pix_url('t/preview').'" class="iconsmall" alt="'.get_string('preview').'" />');
405 $url = new moodle_url('/mod/lesson/lesson.php', array('id'=>$this->page
->cm
->id
, 'action'=>'confirmdelete', 'pageid'=>$page->id
, 'sesskey'=>sesskey()));
406 $actions[] = html_writer
::link($url, '<img src="'.$this->output
->pix_url('t/delete').'" class="iconsmall" alt="'.get_string('delete').'" />');
410 $manager = lesson_page_type_manager
::get($page->lesson
);
411 $links = $manager->get_add_page_type_links($page->id
);
412 foreach ($links as $link) {
413 $options[$link['type']] = $link['name'];
415 $options[0] = get_string('question', 'lesson');
417 $addpageurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page
->cm
->id
, 'pageid'=>$page->id
, 'sesskey'=>sesskey()));
418 $addpageselect = new single_select($addpageurl, 'qtype', $options, null, array(''=>get_string('addanewpage', 'lesson').'...'), 'addpageafter'.$page->id
);
419 $addpageselector = $this->output
->render($addpageselect);
422 if (isset($addpageselector)) {
423 $actions[] = $addpageselector;
426 return implode(' ', $actions);
430 * Prints the on going message to the user.
432 * With custom grading On, displays points
433 * earned out of total points possible thus far.
434 * With custom grading Off, displays number of correct
435 * answers out of total attempted.
437 * @param object $lesson The lesson that the user is taking.
442 * Prints the on going message to the user.
444 * With custom grading On, displays points
445 * earned out of total points possible thus far.
446 * With custom grading Off, displays number of correct
447 * answers out of total attempted.
449 * @param lesson $lesson
452 public function ongoing_score(lesson
$lesson) {
455 $context = get_context_instance(CONTEXT_MODULE
, $this->page
->cm
->id
);
456 if (has_capability('mod/lesson:manage', $context)) {
457 return $this->output
->box(get_string('teacherongoingwarning', 'lesson'), "ongoing center");
459 $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
));
460 if (isset($USER->modattempts
[$lesson->id
])) {
463 $gradeinfo = lesson_grade($lesson, $ntries);
465 if ($lesson->custom
) {
466 $a->score
= $gradeinfo->earned
;
467 $a->currenthigh
= $gradeinfo->total
;
468 return $this->output
->box(get_string("ongoingcustom", "lesson", $a), "ongoing center");
470 $a->correct
= $gradeinfo->earned
;
471 $a->viewed
= $gradeinfo->attempts
;
472 return $this->output
->box(get_string("ongoingnormal", "lesson", $a), "ongoing center");
478 * Returns HTML to display a progress bar of progression through a lesson
480 * @param lesson $lesson
483 public function progress_bar(lesson
$lesson) {
484 global $CFG, $USER, $DB;
486 $context = get_context_instance(CONTEXT_MODULE
, $this->page
->cm
->id
);
488 // lesson setting to turn progress bar on or off
489 if (!$lesson->progressbar
) {
494 if (has_capability('mod/lesson:manage', $context)) {
495 return $this->output
->notification(get_string('progressbarteacherwarning2', 'lesson'));
498 if (!isset($USER->modattempts
[$lesson->id
])) {
499 // all of the lesson pages
500 $pages = $lesson->load_all_pages();
501 foreach ($pages as $page) {
502 if ($page->prevpageid
== 0) {
503 $pageid = $page->id
; // find the first page id
508 // current attempt number
509 if (!$ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
))) {
510 $ntries = 0; // may not be necessary
513 $viewedpageids = array();
514 if ($attempts = $lesson->get_attempts($ntries, false)) {
515 foreach($attempts as $attempt) {
516 $viewedpageids[$attempt->pageid
] = $attempt;
520 $viewedbranches = array();
521 // collect all of the branch tables viewed
522 if ($branches = $DB->get_records("lesson_branch", array ("lessonid"=>$lesson->id
, "userid"=>$USER->id
, "retry"=>$ntries), 'timeseen ASC', 'id, pageid')) {
523 foreach($branches as $branch) {
524 $viewedbranches[$branch->pageid
] = $branch;
526 $viewedpageids = array_merge($viewedpageids, $viewedbranches);
529 // Filter out the following pages:
532 // Pages found inside of Clusters
533 // Do not filter out Cluster Page(s) because we count a cluster as one.
534 // By keeping the cluster page, we get our 1
535 $validpages = array();
536 while ($pageid != 0) {
537 $pageid = $pages[$pageid]->valid_page_and_view($validpages, $viewedpageids);
540 // progress calculation as a percent
541 $progress = round(count($viewedpageids)/count($validpages), 2) * 100;
546 // print out the Progress Bar. Attempted to put as much as possible in the style sheets.
547 $content = '<br />' . html_writer
::tag('div', $progress . '%', array('class' => 'progress_bar_completed', 'style' => 'width: '. $progress . '%;'));
548 $printprogress = html_writer
::tag('div', get_string('progresscompleted', 'lesson', $progress) . $content, array('class' => 'progress_bar'));
550 return $this->output
->box($printprogress, 'progress_bar');
554 * Returns HTML to show the start of a slideshow
555 * @param lesson $lesson
557 public function slideshow_start(lesson
$lesson) {
558 $attributes = array();
559 $attributes['class'] = 'slideshow';
560 $attributes['style'] = 'background-color:'.$lesson->bgcolor
.';height:'.$lesson->height
.'px;width:'.$lesson->width
.'px;';
561 $output = html_writer
::start_tag('div', $attributes);
564 * Returns HTML to show the end of a slideshow
566 public function slideshow_end() {
567 $output = html_writer
::end_tag('div');
570 * Returns a P tag containing contents
571 * @param string $contents
572 * @param string $class
574 public function paragraph($contents, $class='') {
575 $attributes = array();
577 $attributes['class'] = $class;
579 $output = html_writer
::tag('p', $contents, $attributes);
582 * Returns HTML to display add_highscores_form
583 * @param lesson $lesson
586 public function add_highscores_form(lesson
$lesson) {
588 $output = $this->output
->box_start('generalbox boxaligncenter');
589 $output .= $this->output
->box_start('mdl-align');
590 $output .= '<form id="nickname" method ="post" action="'.$CFG->wwwroot
.'/mod/lesson/highscores.php" autocomplete="off">
591 <input type="hidden" name="id" value="'.$this->page
->cm
->id
.'" />
592 <input type="hidden" name="mode" value="save" />
593 <input type="hidden" name="sesskey" value="'.sesskey().'" />';
594 $output .= get_string("entername", "lesson").": <input type=\"text\" name=\"name\" size=\"7\" maxlength=\"5\" />";
595 $output .= $this->output
->box("<input type='submit' value='".get_string('submitname', 'lesson')."' />", 'lessonbutton center');
596 $output .= "</form>";
597 $output .= $this->output
->box_end();
598 $output .= $this->output
->box_end();