Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/renderer.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 class mod_lesson_renderer extends plugin_renderer_base {
00039     public function header($lesson, $cm, $currenttab = '', $extraeditbuttons = false, $lessonpageid = null) {
00040         global $CFG;
00041 
00042         $activityname = format_string($lesson->name, true, $lesson->course);
00043         $title = $this->page->course->shortname.": ".$activityname;
00044 
00045         // Build the buttons
00046         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00047 
00049         $this->page->set_title($title);
00050         $this->page->set_heading($this->page->course->fullname);
00051         lesson_add_header_buttons($cm, $context, $extraeditbuttons, $lessonpageid);
00052         $output = $this->output->header();
00053 
00054         if (has_capability('mod/lesson:manage', $context)) {
00055             $output .= $this->output->heading_with_help($activityname, 'overview', 'lesson');
00056 
00057             if (!empty($currenttab)) {
00058                 ob_start();
00059                 include($CFG->dirroot.'/mod/lesson/tabs.php');
00060                 $output .= ob_get_contents();
00061                 ob_end_clean();
00062             }
00063         } else {
00064             $output .= $this->output->heading($activityname);
00065         }
00066 
00067         foreach ($lesson->messages as $message) {
00068             $output .= $this->output->notification($message[0], $message[1], $message[2]);
00069         }
00070 
00071         return $output;
00072     }
00073 
00078     public function footer() {
00079         return $this->output->footer();
00080     }
00081 
00088     public function lesson_inaccessible($message) {
00089         global $CFG;
00090         $output  =  $this->output->box_start('generalbox boxaligncenter');
00091         $output .=  $this->output->box_start('center');
00092         $output .=  $message;
00093         $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');
00094         $output .=  $this->output->box_end();
00095         $output .=  $this->output->box_end();
00096         return $output;
00097     }
00098 
00105     public function login_prompt(lesson $lesson, $failedattempt = false) {
00106         global $CFG;
00107         $output  = $this->output->box_start('password-form');
00108         $output .= $this->output->box_start('generalbox boxaligncenter');
00109         $output .=  '<form id="password" method="post" action="'.$CFG->wwwroot.'/mod/lesson/view.php" autocomplete="off">';
00110         $output .=  '<fieldset class="invisiblefieldset center">';
00111         $output .=  '<input type="hidden" name="id" value="'. $this->page->cm->id .'" />';
00112         if ($failedattempt) {
00113             $output .=  $this->output->notification(get_string('loginfail', 'lesson'));
00114         }
00115         $output .= get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)).'<br /><br />';
00116         $output .= get_string('enterpassword', 'lesson')." <input type=\"password\" name=\"userpassword\" /><br /><br />";
00117         $output .= "<div class='lessonbutton standardbutton submitbutton'><input type='submit' value='".get_string('continue', 'lesson')."' /></div>";
00118         $output .= " <div class='lessonbutton standardbutton submitbutton'><input type='submit' name='backtocourse' value='".get_string('cancel', 'lesson')."' /></div>";
00119         $output .=  '</fieldset></form>';
00120         $output .=  $this->output->box_end();
00121         $output .=  $this->output->box_end();
00122         return $output;
00123     }
00124 
00132     public function dependancy_errors($dependentlesson, $errors) {
00133         $output  = $this->output->box_start('generalbox boxaligncenter');
00134         $output .= get_string('completethefollowingconditions', 'lesson', $dependentlesson->name);
00135         $output .= $this->output->box(implode('<br />'.get_string('and', 'lesson').'<br />', $errors),'center');
00136         $output .= $this->output->box_end();
00137         return $output;
00138     }
00139 
00146     public function message($message, single_button $button = null) {
00147         $output  = $this->output->box_start('generalbox boxaligncenter');
00148         $output .= $message;
00149         if ($button !== null) {
00150             $output .= $this->output->box($this->output->render($button), 'lessonbutton standardbutton');
00151         }
00152         $output .= $this->output->box_end();
00153         return $output;
00154     }
00155 
00162     public function continue_links(lesson $lesson, $lastpageseenid) {
00163         global $CFG;
00164         $output = $this->output->box(get_string('youhaveseen','lesson'), 'generalbox boxaligncenter');
00165         $output .= $this->output->box_start('center');
00166 
00167         $yeslink = html_writer::link(new moodle_url('/mod/lesson/view.php', array('id'=>$this->page->cm->id, 'pageid'=>$lastpageseenid, 'startlastseen'=>'yes')), get_string('yes'));
00168         $output .= html_writer::tag('span', $yeslink, array('class'=>'lessonbutton standardbutton'));
00169         $output .= '&nbsp;';
00170 
00171         $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'));
00172         $output .= html_writer::tag('span', $nolink, array('class'=>'lessonbutton standardbutton'));
00173 
00174         $output .= $this->output->box_end();
00175         return $output;
00176     }
00177 
00185     public function display_page(lesson $lesson, lesson_page $page, $attempt) {
00186         // We need to buffer here as there is an mforms display call
00187         ob_start();
00188         echo $page->display($this, $attempt);
00189         $output = ob_get_contents();
00190         ob_end_clean();
00191         return $output;
00192     }
00193 
00201     public function display_edit_collapsed(lesson $lesson, $pageid) {
00202         global $DB, $CFG;
00203 
00204         $manager = lesson_page_type_manager::get($lesson);
00205         $qtypes = $manager->get_page_type_strings();
00206         $npages = count($lesson->load_all_pages());
00207 
00208         $table = new html_table();
00209         $table->head = array(get_string('pagetitle', 'lesson'), get_string('qtype', 'lesson'), get_string('jumps', 'lesson'), get_string('actions', 'lesson'));
00210         $table->align = array('left', 'left', 'left', 'center');
00211         $table->wrap = array('', 'nowrap', '', 'nowrap');
00212         $table->tablealign = 'center';
00213         $table->cellspacing = 0;
00214         $table->cellpadding = '2px';
00215         $table->width = '80%';
00216         $table->data = array();
00217 
00218         $canedit = has_capability('mod/lesson:edit', get_context_instance(CONTEXT_MODULE, $this->page->cm->id));
00219 
00220         while ($pageid != 0) {
00221             $page = $lesson->load_page($pageid);
00222             $data = array();
00223             $data[] = "<a href=\"$CFG->wwwroot/mod/lesson/edit.php?id=".$this->page->cm->id."&amp;mode=single&amp;pageid=".$page->id."\">".format_string($page->title,true).'</a>';
00224             $data[] = $qtypes[$page->qtype];
00225             $data[] = implode("<br />\n", $page->jumps);
00226             if ($canedit) {
00227                 $data[] = $this->page_action_links($page, $npages, true);
00228             } else {
00229                 $data[] = '';
00230             }
00231             $table->data[] = $data;
00232             $pageid = $page->nextpageid;
00233         }
00234 
00235         return html_writer::table($table);
00236     }
00237 
00247     public function display_edit_full(lesson $lesson, $pageid, $prevpageid, $single=false) {
00248         global $DB, $CFG;
00249 
00250         $manager = lesson_page_type_manager::get($lesson);
00251         $qtypes = $manager->get_page_type_strings();
00252         $npages = count($lesson->load_all_pages());
00253         $canedit = has_capability('mod/lesson:edit', get_context_instance(CONTEXT_MODULE, $this->page->cm->id));
00254 
00255         $content = '';
00256         if ($canedit) {
00257             $content = $this->add_page_links($lesson, $prevpageid);
00258         }
00259 
00260         $options = new stdClass;
00261         $options->noclean = true;
00262 
00263         while ($pageid != 0 && $single!=='stop') {
00264             $page = $lesson->load_page($pageid);
00265 
00266             $pagetable = new html_table();
00267             $pagetable->align = array('right','left');
00268             $pagetable->width = '100%';
00269             $pagetable->tablealign = 'center';
00270             $pagetable->cellspacing = 0;
00271             $pagetable->cellpadding = '5px';
00272             $pagetable->data = array();
00273 
00274             $pageheading = new html_table_cell();
00275 
00276             $pageheading->text = format_string($page->title);
00277             if ($canedit) {
00278                 $pageheading->text .= ' '.$this->page_action_links($page, $npages);
00279             }
00280             $pageheading->style = 'text-align:center';
00281             $pageheading->colspan = 2;
00282             $pageheading->scope = 'col';
00283             $pagetable->head = array($pageheading);
00284 
00285             $cell = new html_table_cell();
00286             $cell->colspan = 2;
00287             $cell->style = 'text-align:left';
00288             $cell->text = $page->contents;
00289             $pagetable->data[] = new html_table_row(array($cell));
00290 
00291             $cell = new html_table_cell();
00292             $cell->colspan = 2;
00293             $cell->style = 'text-align:center';
00294             $cell->text = '<strong>'.$qtypes[$page->qtype] . $page->option_description_string().'</strong>';
00295             $pagetable->data[] = new html_table_row(array($cell));
00296 
00297             $pagetable = $page->display_answers($pagetable);
00298 
00299             $content .= html_writer::table($pagetable);
00300 
00301             if ($canedit) {
00302                 $content .= $this->add_page_links($lesson, $pageid);
00303             }
00304 
00305             // check the prev links - fix (silently) if necessary - there was a bug in
00306             // versions 1 and 2 when add new pages. Not serious then as the backwards
00307             // links were not used in those versions
00308             if ($page->prevpageid != $prevpageid) {
00309                 // fix it
00310                 $DB->set_field("lesson_pages", "prevpageid", $prevpageid, array("id" => $page->id));
00311                 debugging("<p>***prevpageid of page $page->id set to $prevpageid***");
00312             }
00313 
00314             $prevpageid = $page->id;
00315             $pageid = $page->nextpageid;
00316 
00317             if ($single === true) {
00318                 $single = 'stop';
00319             }
00320 
00321         }
00322 
00323         return $this->output->box($content, 'edit_pages_box');
00324     }
00325 
00333     public function add_page_links(lesson $lesson, $prevpageid=false) {
00334         global $CFG;
00335 
00336         $links = array();
00337 
00338         $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid));
00339         $links[] = html_writer::link($importquestionsurl, get_string('importquestions', 'lesson'));
00340 
00341         $manager = lesson_page_type_manager::get($lesson);
00342         foreach($manager->get_add_page_type_links($prevpageid) as $link) {
00343             $links[] = html_writer::link($link['addurl'], $link['name']);
00344         }
00345 
00346         $addquestionurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid));
00347         $links[] = html_writer::link($addquestionurl, get_string('addaquestionpagehere', 'lesson'));
00348 
00349         return $this->output->box(implode(" | \n", $links), 'addlinks');
00350     }
00351 
00357     public function add_first_page_links(lesson $lesson) {
00358         global $CFG;
00359         $prevpageid = 0;
00360 
00361         $output = $this->output->heading(get_string("whatdofirst", "lesson"), 3);
00362         $links = array();
00363 
00364         $importquestionsurl = new moodle_url('/mod/lesson/import.php',array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid));
00365         $links[] = html_writer::link($importquestionsurl, get_string('importquestions', 'lesson'));
00366 
00367         $importppturl = new moodle_url('/mod/lesson/importppt.php',array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid));
00368         $links[] = html_writer::link($importppturl, get_string('importppt', 'lesson'));
00369 
00370         $manager = lesson_page_type_manager::get($lesson);
00371         foreach ($manager->get_add_page_type_links($prevpageid) as $link) {
00372             $link['addurl']->param('firstpage', 1);
00373             $links[] = html_writer::link($link['addurl'], $link['name']);
00374         }
00375 
00376         $addquestionurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page->cm->id, 'pageid'=>$prevpageid, 'firstpage'=>1));
00377         $links[] = html_writer::link($addquestionurl, get_string('addaquestionpage', 'lesson'));
00378 
00379         return $this->output->box($output.'<p>'.implode('</p><p>', $links).'</p>', 'generalbox firstpageoptions');
00380     }
00381 
00390     public function page_action_links(lesson_page $page, $printmove, $printaddpage=false) {
00391         global $CFG;
00392 
00393         $actions = array();
00394 
00395         if ($printmove) {
00396             $printmovehtml = new moodle_url('/mod/lesson/lesson.php', array('id'=>$this->page->cm->id, 'action'=>'move', 'pageid'=>$page->id, 'sesskey'=>sesskey()));
00397             $actions[] = html_writer::link($printmovehtml, '<img src="'.$this->output->pix_url('t/move').'" class="iconsmall" alt="'.get_string('move').'" />');
00398         }
00399         $url = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page->cm->id, 'pageid'=>$page->id, 'edit'=>1));
00400         $actions[] = html_writer::link($url, '<img src="'.$this->output->pix_url('t/edit').'" class="iconsmall" alt="'.get_string('update').'" />');
00401 
00402         $url = new moodle_url('/mod/lesson/view.php', array('id'=>$this->page->cm->id, 'pageid'=>$page->id));
00403         $actions[] = html_writer::link($url, '<img src="'.$this->output->pix_url('t/preview').'" class="iconsmall" alt="'.get_string('preview').'" />');
00404 
00405         $url = new moodle_url('/mod/lesson/lesson.php', array('id'=>$this->page->cm->id, 'action'=>'confirmdelete', 'pageid'=>$page->id, 'sesskey'=>sesskey()));
00406         $actions[] = html_writer::link($url, '<img src="'.$this->output->pix_url('t/delete').'" class="iconsmall" alt="'.get_string('delete').'" />');
00407 
00408         if ($printaddpage) {
00409             $options = array();
00410             $manager = lesson_page_type_manager::get($page->lesson);
00411             $links = $manager->get_add_page_type_links($page->id);
00412             foreach ($links as $link) {
00413                 $options[$link['type']] = $link['name'];
00414             }
00415             $options[0] = get_string('question', 'lesson');
00416 
00417             $addpageurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$this->page->cm->id, 'pageid'=>$page->id, 'sesskey'=>sesskey()));
00418             $addpageselect = new single_select($addpageurl, 'qtype', $options, null, array(''=>get_string('addanewpage', 'lesson').'...'), 'addpageafter'.$page->id);
00419             $addpageselector = $this->output->render($addpageselect);
00420         }
00421 
00422         if (isset($addpageselector)) {
00423             $actions[] = $addpageselector;
00424         }
00425 
00426         return implode(' ', $actions);
00427     }
00428 
00452     public function ongoing_score(lesson $lesson) {
00453         global $USER, $DB;
00454 
00455         $context = get_context_instance(CONTEXT_MODULE, $this->page->cm->id);
00456         if (has_capability('mod/lesson:manage', $context)) {
00457             return $this->output->box(get_string('teacherongoingwarning', 'lesson'), "ongoing center");
00458         } else {
00459             $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
00460             if (isset($USER->modattempts[$lesson->id])) {
00461                 $ntries--;
00462             }
00463             $gradeinfo = lesson_grade($lesson, $ntries);
00464             $a = new stdClass;
00465             if ($lesson->custom) {
00466                 $a->score = $gradeinfo->earned;
00467                 $a->currenthigh = $gradeinfo->total;
00468                 return $this->output->box(get_string("ongoingcustom", "lesson", $a), "ongoing center");
00469             } else {
00470                 $a->correct = $gradeinfo->earned;
00471                 $a->viewed = $gradeinfo->attempts;
00472                 return $this->output->box(get_string("ongoingnormal", "lesson", $a), "ongoing center");
00473             }
00474         }
00475     }
00476 
00483     public function progress_bar(lesson $lesson) {
00484         global $CFG, $USER, $DB;
00485 
00486         $context = get_context_instance(CONTEXT_MODULE, $this->page->cm->id);
00487 
00488         // lesson setting to turn progress bar on or off
00489         if (!$lesson->progressbar) {
00490             return '';
00491         }
00492 
00493         // catch teachers
00494         if (has_capability('mod/lesson:manage', $context)) {
00495             return $this->output->notification(get_string('progressbarteacherwarning2', 'lesson'));
00496         }
00497 
00498         if (!isset($USER->modattempts[$lesson->id])) {
00499             // all of the lesson pages
00500             $pages = $lesson->load_all_pages();
00501             foreach ($pages as $page) {
00502                 if ($page->prevpageid == 0) {
00503                     $pageid = $page->id;  // find the first page id
00504                     break;
00505                 }
00506             }
00507 
00508             // current attempt number
00509             if (!$ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id))) {
00510                 $ntries = 0;  // may not be necessary
00511             }
00512 
00513 
00514             $viewedpageids = array();
00515             if ($attempts = $lesson->get_attempts($ntries, true)) {
00516                 $viewedpageids = array_merge($viewedpageids, array_keys($attempts));
00517             }
00518 
00519             // collect all of the branch tables viewed
00520             if ($viewedbranches = $DB->get_records("lesson_branch", array ("lessonid"=>$lesson->id, "userid"=>$USER->id, "retry"=>$ntries), 'timeseen DESC', 'id, pageid')) {
00521                 $viewedpageids = array_merge($viewedpageids, array_keys($viewedbranches));
00522             }
00523 
00524             // Filter out the following pages:
00525             //      End of Cluster
00526             //      End of Branch
00527             //      Pages found inside of Clusters
00528             // Do not filter out Cluster Page(s) because we count a cluster as one.
00529             // By keeping the cluster page, we get our 1
00530             $validpages = array();
00531             while ($pageid != 0) {
00532                 $pageid = $pages[$pageid]->valid_page_and_view($validpages, $viewedpageids);
00533             }
00534 
00535             // progress calculation as a percent
00536             $progress = round(count($viewedpageids)/count($validpages), 2) * 100;
00537         } else {
00538             $progress = 100;
00539         }
00540 
00541         // print out the Progress Bar.  Attempted to put as much as possible in the style sheets.
00542         $content = '<br />' . html_writer::tag('div', $progress . '%', array('class' => 'progress_bar_completed', 'style' => 'width: '. $progress . '%;'));
00543         $printprogress = html_writer::tag('div', get_string('progresscompleted', 'lesson', $progress) . $content, array('class' => 'progress_bar'));
00544 
00545         return $this->output->box($printprogress, 'progress_bar');
00546     }
00547 
00552     public function slideshow_start(lesson $lesson) {
00553         $attributes = array();
00554         $attributes['class'] = 'slideshow';
00555         $attributes['style'] = 'background-color:'.$lesson->bgcolor.';height:'.$lesson->height.'px;width:'.$lesson->width.'px;';
00556         $output = html_writer::start_tag('div', $attributes);
00557     }
00561     public function slideshow_end() {
00562         $output = html_writer::end_tag('div');
00563     }
00569     public function paragraph($contents, $class='') {
00570         $attributes = array();
00571         if ($class !== '') {
00572             $attributes['class'] = $class;
00573         }
00574         $output = html_writer::tag('p', $contents, $attributes);
00575     }
00581     public function add_highscores_form(lesson $lesson) {
00582         global $CFG;
00583         $output  = $this->output->box_start('generalbox boxaligncenter');
00584         $output .= $this->output->box_start('mdl-align');
00585         $output .= '<form id="nickname" method ="post" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php" autocomplete="off">
00586              <input type="hidden" name="id" value="'.$this->page->cm->id.'" />
00587              <input type="hidden" name="mode" value="save" />
00588              <input type="hidden" name="sesskey" value="'.sesskey().'" />';
00589         $output .= get_string("entername", "lesson").": <input type=\"text\" name=\"name\" size=\"7\" maxlength=\"5\" />";
00590         $output .= $this->output->box("<input type='submit' value='".get_string('submitname', 'lesson')."' />", 'lessonbutton center');
00591         $output .= "</form>";
00592         $output .= $this->output->box_end();
00593         $output .= $this->output->box_end();
00594         return $output;
00595     }
00596 }
 All Data Structures Namespaces Files Functions Variables Enumerations