MDL-52136 mod_forum: forum_post is templatable
[moodle.git] / mod / lesson / renderer.php
blob56e071af212daa62326288e884c6ddfa308c3221
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Moodle renderer used to display special elements of the lesson module
21 * @package mod_lesson
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 defined('MOODLE_INTERNAL') || die();
28 class mod_lesson_renderer extends plugin_renderer_base {
29 /**
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.
37 * @return string
39 public function header($lesson, $cm, $currenttab = '', $extraeditbuttons = false, $lessonpageid = null, $extrapagetitle = null) {
40 global $CFG;
42 $activityname = format_string($lesson->name, true, $lesson->course);
43 if (empty($extrapagetitle)) {
44 $title = $this->page->course->shortname.": ".$activityname;
45 } else {
46 $title = $this->page->course->shortname.": ".$activityname.": ".$extrapagetitle;
49 // Build the buttons
50 $context = context_module::instance($cm->id);
52 /// Header setup
53 $this->page->set_title($title);
54 $this->page->set_heading($this->page->course->fullname);
55 lesson_add_header_buttons($cm, $context, $extraeditbuttons, $lessonpageid);
56 $output = $this->output->header();
58 if (has_capability('mod/lesson:manage', $context)) {
59 $output .= $this->output->heading_with_help($activityname, 'overview', 'lesson');
61 if (!empty($currenttab)) {
62 ob_start();
63 include($CFG->dirroot.'/mod/lesson/tabs.php');
64 $output .= ob_get_contents();
65 ob_end_clean();
67 } else {
68 $output .= $this->output->heading($activityname);
71 foreach ($lesson->messages as $message) {
72 $output .= $this->output->notification($message[0], $message[1], $message[2]);
75 return $output;
78 /**
79 * Returns the footer
80 * @return string
82 public function footer() {
83 return $this->output->footer();
86 /**
87 * Returns HTML for a lesson inaccessible message
89 * @param string $message
90 * @return <type>
92 public function lesson_inaccessible($message) {
93 global $CFG;
94 $output = $this->output->box_start('generalbox boxaligncenter');
95 $output .= $this->output->box_start('center');
96 $output .= $message;
97 $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');
98 $output .= $this->output->box_end();
99 $output .= $this->output->box_end();
100 return $output;
104 * Returns HTML to prompt the user to log in
105 * @param lesson $lesson
106 * @param bool $failedattempt
107 * @return string
109 public function login_prompt(lesson $lesson, $failedattempt = false) {
110 global $CFG;
111 $output = $this->output->box_start('password-form');
112 $output .= $this->output->box_start('generalbox boxaligncenter');
113 $output .= '<form id="password" method="post" action="'.$CFG->wwwroot.'/mod/lesson/view.php" autocomplete="off">';
114 $output .= '<fieldset class="invisiblefieldset center">';
115 $output .= '<input type="hidden" name="id" value="'. $this->page->cm->id .'" />';
116 $output .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
117 if ($failedattempt) {
118 $output .= $this->output->notification(get_string('loginfail', 'lesson'));
120 $output .= get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)).'<br /><br />';
121 $output .= get_string('enterpassword', 'lesson')." <input type=\"password\" name=\"userpassword\" /><br /><br />";
122 $output .= "<div class='lessonbutton standardbutton submitbutton'><input type='submit' value='".get_string('continue', 'lesson')."' /></div>";
123 $output .= " <div class='lessonbutton standardbutton submitbutton'><input type='submit' name='backtocourse' value='".get_string('cancel', 'lesson')."' /></div>";
124 $output .= '</fieldset></form>';
125 $output .= $this->output->box_end();
126 $output .= $this->output->box_end();
127 return $output;
131 * Returns HTML to display dependancy errors
133 * @param object $dependentlesson
134 * @param array $errors
135 * @return string
137 public function dependancy_errors($dependentlesson, $errors) {
138 $output = $this->output->box_start('generalbox boxaligncenter');
139 $output .= get_string('completethefollowingconditions', 'lesson', $dependentlesson->name);
140 $output .= $this->output->box(implode('<br />'.get_string('and', 'lesson').'<br />', $errors),'center');
141 $output .= $this->output->box_end();
142 return $output;
146 * Returns HTML to display a message
147 * @param string $message
148 * @param single_button $button
149 * @return string
151 public function message($message, single_button $button = null) {
152 $output = $this->output->box_start('generalbox boxaligncenter');
153 $output .= $message;
154 if ($button !== null) {
155 $output .= $this->output->box($this->output->render($button), 'lessonbutton standardbutton');
157 $output .= $this->output->box_end();
158 return $output;
162 * Returns HTML to display a continue button
163 * @param lesson $lesson
164 * @param int $lastpageseen
165 * @return string
167 public function continue_links(lesson $lesson, $lastpageseenid) {
168 global $CFG;
169 $output = $this->output->box(get_string('youhaveseen','lesson'), 'generalbox boxaligncenter');
170 $output .= $this->output->box_start('center');
172 $yeslink = html_writer::link(new moodle_url('/mod/lesson/view.php', array('id'=>$this->page->cm->id, 'pageid'=>$lastpageseenid, 'startlastseen'=>'yes')), get_string('yes'));
173 $output .= html_writer::tag('span', $yeslink, array('class'=>'lessonbutton standardbutton'));
174 $output .= '&nbsp;';
176 $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'));
177 $output .= html_writer::tag('span', $nolink, array('class'=>'lessonbutton standardbutton'));
179 $output .= $this->output->box_end();
180 return $output;
184 * Returns HTML to display a page to the user
185 * @param lesson $lesson
186 * @param lesson_page $page
187 * @param object $attempt
188 * @return string
190 public function display_page(lesson $lesson, lesson_page $page, $attempt) {
191 // We need to buffer here as there is an mforms display call
192 ob_start();
193 echo $page->display($this, $attempt);
194 $output = ob_get_contents();
195 ob_end_clean();
196 return $output;
200 * Returns HTML to display a collapsed edit form
202 * @param lesson $lesson
203 * @param int $pageid
204 * @return string
206 public function display_edit_collapsed(lesson $lesson, $pageid) {
207 global $DB, $CFG;
209 $manager = lesson_page_type_manager::get($lesson);
210 $qtypes = $manager->get_page_type_strings();
211 $npages = count($lesson->load_all_pages());
213 $table = new html_table();
214 $table->head = array(get_string('pagetitle', 'lesson'), get_string('qtype', 'lesson'), get_string('jumps', 'lesson'), get_string('actions', 'lesson'));
215 $table->align = array('left', 'left', 'left', 'center');
216 $table->wrap = array('', 'nowrap', '', 'nowrap');
217 $table->tablealign = 'center';
218 $table->cellspacing = 0;
219 $table->cellpadding = '2px';
220 $table->width = '80%';
221 $table->data = array();
223 $canedit = has_capability('mod/lesson:edit', context_module::instance($this->page->cm->id));
225 while ($pageid != 0) {
226 $page = $lesson->load_page($pageid);
227 $data = array();
228 $url = new moodle_url('/mod/lesson/edit.php', array(
229 'id' => $this->page->cm->id,
230 'mode' => 'single',
231 'pageid' => $page->id
233 $data[] = html_writer::link($url, format_string($page->title, true), array('id' => 'lesson-' . $page->id));
234 $data[] = $qtypes[$page->qtype];
235 $data[] = implode("<br />\n", $page->jumps);
236 if ($canedit) {
237 $data[] = $this->page_action_links($page, $npages, true);
238 } else {
239 $data[] = '';
241 $table->data[] = $data;
242 $pageid = $page->nextpageid;
245 return html_writer::table($table);
249 * Returns HTML to display the full edit page
251 * @param lesson $lesson
252 * @param int $pageid
253 * @param int $prevpageid
254 * @param bool $single
255 * @return string
257 public function display_edit_full(lesson $lesson, $pageid, $prevpageid, $single=false) {
258 global $DB, $CFG;
260 $manager = lesson_page_type_manager::get($lesson);
261 $qtypes = $manager->get_page_type_strings();
262 $npages = count($lesson->load_all_pages());
263 $canedit = has_capability('mod/lesson:edit', context_module::instance($this->page->cm->id));
265 $content = '';
266 if ($canedit) {
267 $content = $this->add_page_links($lesson, $prevpageid);
270 $options = new stdClass;
271 $options->noclean = true;
273 while ($pageid != 0 && $single!=='stop') {
274 $page = $lesson->load_page($pageid);
276 $pagetable = new html_table();
277 $pagetable->align = array('right','left');
278 $pagetable->width = '100%';
279 $pagetable->tablealign = 'center';
280 $pagetable->cellspacing = 0;
281 $pagetable->cellpadding = '5px';
282 $pagetable->data = array();
283 $pagetable->id = 'lesson-' . $pageid;
285 $pageheading = new html_table_cell();
287 $pageheading->text = format_string($page->title);
288 if ($canedit) {
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();
297 $cell->colspan = 2;
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();
303 $cell->colspan = 2;
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', array('class' => 'no-overflow'));
311 $content .= html_writer::table($pagetable);
312 $content .= html_writer::end_tag('div');
314 if ($canedit) {
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) {
322 // fix it
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) {
331 $single = 'stop';
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
344 * @return string
346 public function add_page_links(lesson $lesson, $prevpageid=false) {
347 global $CFG;
349 $links = array();
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
368 * @return string
370 public function add_first_page_links(lesson $lesson) {
371 global $CFG;
372 $prevpageid = 0;
374 $output = $this->output->heading(get_string("whatdofirst", "lesson"), 3);
375 $links = array();
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
398 * @return string
400 public function page_action_links(lesson_page $page, $printmove, $printaddpage=false) {
401 global $CFG;
403 $actions = array();
405 if ($printmove) {
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 = html_writer::img($this->output->pix_url('t/move'), $label, array('class' => 'iconsmall'));
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 = html_writer::img($this->output->pix_url('t/edit'), $label, array('class' => 'iconsmall'));
415 $actions[] = html_writer::link($url, $img, array('title' => $label));
417 $url = new moodle_url('/mod/lesson/view.php', array('id' => $this->page->cm->id, 'pageid' => $page->id));
418 $label = get_string('previewpagenamed', 'lesson', format_string($page->title));
419 $img = html_writer::img($this->output->pix_url('t/preview'), $label, array('class' => 'iconsmall'));
420 $actions[] = html_writer::link($url, $img, array('title' => $label));
422 $url = new moodle_url('/mod/lesson/lesson.php',
423 array('id' => $this->page->cm->id, 'action' => 'confirmdelete', 'pageid' => $page->id, 'sesskey' => sesskey()));
424 $label = get_string('deletepagenamed', 'lesson', format_string($page->title));
425 $img = html_writer::img($this->output->pix_url('t/delete'), $label, array('class' => 'iconsmall'));
426 $actions[] = html_writer::link($url, $img, array('title' => $label));
428 if ($printaddpage) {
429 $options = array();
430 $manager = lesson_page_type_manager::get($page->lesson);
431 $links = $manager->get_add_page_type_links($page->id);
432 foreach ($links as $link) {
433 $options[$link['type']] = $link['name'];
435 $options[0] = get_string('question', 'lesson');
437 $addpageurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page->cm->id, 'pageid'=>$page->id, 'sesskey'=>sesskey()));
438 $addpageselect = new single_select($addpageurl, 'qtype', $options, null, array(''=>get_string('addanewpage', 'lesson').'...'), 'addpageafter'.$page->id);
439 $addpageselector = $this->output->render($addpageselect);
442 if (isset($addpageselector)) {
443 $actions[] = $addpageselector;
446 return implode(' ', $actions);
450 * Prints the on going message to the user.
452 * With custom grading On, displays points
453 * earned out of total points possible thus far.
454 * With custom grading Off, displays number of correct
455 * answers out of total attempted.
457 * @param object $lesson The lesson that the user is taking.
458 * @return void
462 * Prints the on going message to the user.
464 * With custom grading On, displays points
465 * earned out of total points possible thus far.
466 * With custom grading Off, displays number of correct
467 * answers out of total attempted.
469 * @param lesson $lesson
470 * @return string
472 public function ongoing_score(lesson $lesson) {
473 global $USER, $DB;
475 $context = context_module::instance($this->page->cm->id);
476 if (has_capability('mod/lesson:manage', $context)) {
477 return $this->output->box(get_string('teacherongoingwarning', 'lesson'), "ongoing center");
478 } else {
479 $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
480 if (isset($USER->modattempts[$lesson->id])) {
481 $ntries--;
483 $gradeinfo = lesson_grade($lesson, $ntries);
484 $a = new stdClass;
485 if ($lesson->custom) {
486 $a->score = $gradeinfo->earned;
487 $a->currenthigh = $gradeinfo->total;
488 return $this->output->box(get_string("ongoingcustom", "lesson", $a), "ongoing center");
489 } else {
490 $a->correct = $gradeinfo->earned;
491 $a->viewed = $gradeinfo->attempts;
492 return $this->output->box(get_string("ongoingnormal", "lesson", $a), "ongoing center");
498 * Returns HTML to display a progress bar of progression through a lesson
500 * @param lesson $lesson
501 * @return string
503 public function progress_bar(lesson $lesson) {
504 global $CFG, $USER, $DB;
506 $context = context_module::instance($this->page->cm->id);
508 // lesson setting to turn progress bar on or off
509 if (!$lesson->progressbar) {
510 return '';
513 // catch teachers
514 if (has_capability('mod/lesson:manage', $context)) {
515 return $this->output->notification(get_string('progressbarteacherwarning2', 'lesson'));
518 if (!isset($USER->modattempts[$lesson->id])) {
519 // all of the lesson pages
520 $pages = $lesson->load_all_pages();
521 foreach ($pages as $page) {
522 if ($page->prevpageid == 0) {
523 $pageid = $page->id; // find the first page id
524 break;
528 // current attempt number
529 if (!$ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id))) {
530 $ntries = 0; // may not be necessary
533 $viewedpageids = array();
534 if ($attempts = $lesson->get_attempts($ntries, false)) {
535 foreach($attempts as $attempt) {
536 $viewedpageids[$attempt->pageid] = $attempt;
540 $viewedbranches = array();
541 // collect all of the branch tables viewed
542 if ($branches = $DB->get_records("lesson_branch", array ("lessonid"=>$lesson->id, "userid"=>$USER->id, "retry"=>$ntries), 'timeseen ASC', 'id, pageid')) {
543 foreach($branches as $branch) {
544 $viewedbranches[$branch->pageid] = $branch;
546 $viewedpageids = array_merge($viewedpageids, $viewedbranches);
549 // Filter out the following pages:
550 // End of Cluster
551 // End of Branch
552 // Pages found inside of Clusters
553 // Do not filter out Cluster Page(s) because we count a cluster as one.
554 // By keeping the cluster page, we get our 1
555 $validpages = array();
556 while ($pageid != 0) {
557 $pageid = $pages[$pageid]->valid_page_and_view($validpages, $viewedpageids);
560 // progress calculation as a percent
561 $progress = round(count($viewedpageids)/count($validpages), 2) * 100;
562 } else {
563 $progress = 100;
566 // print out the Progress Bar. Attempted to put as much as possible in the style sheets.
567 $content = '<br />' . html_writer::tag('div', $progress . '%', array('class' => 'progress_bar_completed', 'style' => 'width: '. $progress . '%;'));
568 $printprogress = html_writer::tag('div', get_string('progresscompleted', 'lesson', $progress) . $content, array('class' => 'progress_bar'));
570 return $this->output->box($printprogress, 'progress_bar');
574 * Returns HTML to show the start of a slideshow
575 * @param lesson $lesson
577 public function slideshow_start(lesson $lesson) {
578 $attributes = array();
579 $attributes['class'] = 'slideshow';
580 $attributes['style'] = 'background-color:'.$lesson->properties()->bgcolor.';height:'.
581 $lesson->properties()->height.'px;width:'.$lesson->properties()->width.'px;';
582 $output = html_writer::start_tag('div', $attributes);
583 return $output;
586 * Returns HTML to show the end of a slideshow
588 public function slideshow_end() {
589 $output = html_writer::end_tag('div');
590 return $output;
593 * Returns a P tag containing contents
594 * @param string $contents
595 * @param string $class
597 public function paragraph($contents, $class='') {
598 $attributes = array();
599 if ($class !== '') {
600 $attributes['class'] = $class;
602 $output = html_writer::tag('p', $contents, $attributes);
603 return $output;