|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00030 define("LESSON_PAGE_BRANCHTABLE", "20"); 00031 00032 class lesson_page_type_branchtable extends lesson_page { 00033 00034 protected $type = lesson_page::TYPE_STRUCTURE; 00035 protected $typeid = LESSON_PAGE_BRANCHTABLE; 00036 protected $typeidstring = 'branchtable'; 00037 protected $string = null; 00038 protected $jumpto = null; 00039 00040 public function get_typeid() { 00041 return $this->typeid; 00042 } 00043 public function get_typestring() { 00044 if ($this->string===null) { 00045 $this->string = get_string($this->typeidstring, 'lesson'); 00046 } 00047 return $this->string; 00048 } 00049 00055 public function get_jumps() { 00056 global $DB; 00057 $jumps = array(); 00058 $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id); 00059 if ($answers = $this->get_answers()) { 00060 foreach ($answers as $answer) { 00061 if ($answer->answer === '') { 00062 // show only jumps for real branches (==have description) 00063 continue; 00064 } 00065 $jumps[] = $this->get_jump_name($answer->jumpto); 00066 } 00067 } else { 00068 // We get here is the lesson was created on a Moodle 1.9 site and 00069 // the lesson contains question pages without any answers. 00070 $jumps[] = $this->get_jump_name($this->properties->nextpageid); 00071 } 00072 return $jumps; 00073 } 00074 00075 public static function get_jumptooptions($firstpage, $lesson) { 00076 global $DB, $PAGE; 00077 $jump = array(); 00078 $jump[0] = get_string("thispage", "lesson"); 00079 $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson"); 00080 $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson"); 00081 $jump[LESSON_EOL] = get_string("endoflesson", "lesson"); 00082 if (!$firstpage) { 00083 if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) { 00084 print_error('cannotfindfirstpage', 'lesson'); 00085 } 00086 while (true) { 00087 if ($apageid) { 00088 $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid)); 00089 $jump[$apageid] = $title; 00090 $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid)); 00091 } else { 00092 // last page reached 00093 break; 00094 } 00095 } 00096 } 00097 return $jump; 00098 } 00099 public function get_idstring() { 00100 return $this->typeidstring; 00101 } 00102 public function display($renderer, $attempt) { 00103 global $PAGE, $CFG; 00104 00105 $output = ''; 00106 $options = new stdClass; 00107 $options->para = false; 00108 $options->noclean = true; 00109 00110 if ($this->lesson->slideshow) { 00111 $output .= $renderer->slideshow_start($this->lesson); 00112 } 00113 // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911). 00114 $output .= $renderer->heading(format_string($this->properties->title), 3); 00115 $output .= $renderer->box($this->get_contents(), 'contents'); 00116 00117 $buttons = array(); 00118 $i = 0; 00119 foreach ($this->get_answers() as $answer) { 00120 if ($answer->answer === '') { 00121 // not a branch! 00122 continue; 00123 } 00124 $params = array(); 00125 $params['id'] = $PAGE->cm->id; 00126 $params['pageid'] = $this->properties->id; 00127 $params['sesskey'] = sesskey(); 00128 $params['jumpto'] = $answer->jumpto; 00129 $url = new moodle_url('/mod/lesson/continue.php', $params); 00130 $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options))); 00131 $i++; 00132 } 00133 // Set the orientation 00134 if ($this->properties->layout) { 00135 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal'); 00136 } else { 00137 $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical'); 00138 } 00139 $output .= $buttonshtml; 00140 00141 if ($this->lesson->slideshow) { 00142 $output .= $renderer->slideshow_end(); 00143 } 00144 00145 return $output; 00146 } 00147 00148 public function check_answer() { 00149 global $USER, $DB, $PAGE, $CFG; 00150 00151 require_sesskey(); 00152 $newpageid = optional_param('jumpto', NULL, PARAM_INT); 00153 // going to insert into lesson_branch 00154 if ($newpageid == LESSON_RANDOMBRANCH) { 00155 $branchflag = 1; 00156 } else { 00157 $branchflag = 0; 00158 } 00159 if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) { 00160 $retries = count($grades); 00161 } else { 00162 $retries = 0; 00163 } 00164 $branch = new stdClass; 00165 $branch->lessonid = $this->lesson->id; 00166 $branch->userid = $USER->id; 00167 $branch->pageid = $this->properties->id; 00168 $branch->retry = $retries; 00169 $branch->flag = $branchflag; 00170 $branch->timeseen = time(); 00171 00172 $DB->insert_record("lesson_branch", $branch); 00173 00174 // this is called when jumping to random from a branch table 00175 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id); 00176 if($newpageid == LESSON_UNSEENBRANCHPAGE) { 00177 if (has_capability('mod/lesson:manage', $context)) { 00178 $newpageid = LESSON_NEXTPAGE; 00179 } else { 00180 $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0 00181 } 00182 } 00183 // convert jumpto page into a proper page id 00184 if ($newpageid == 0) { 00185 $newpageid = $this->properties->id; 00186 } elseif ($newpageid == LESSON_NEXTPAGE) { 00187 if (!$newpageid = $this->nextpageid) { 00188 // no nextpage go to end of lesson 00189 $newpageid = LESSON_EOL; 00190 } 00191 } elseif ($newpageid == LESSON_PREVIOUSPAGE) { 00192 $newpageid = $this->prevpageid; 00193 } elseif ($newpageid == LESSON_RANDOMPAGE) { 00194 $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id); 00195 } elseif ($newpageid == LESSON_RANDOMBRANCH) { 00196 $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id); 00197 } 00198 // no need to record anything in lesson_attempts 00199 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$newpageid))); 00200 } 00201 00202 public function display_answers($table) { 00203 $answers = $this->get_answers(); 00204 $options = new stdClass; 00205 $options->noclean = true; 00206 $options->para = false; 00207 $i = 1; 00208 foreach ($answers as $answer) { 00209 if ($answer->answer === '') { 00210 // not a branch! 00211 continue; 00212 } 00213 $cells = array(); 00214 $cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: "; 00215 $cells[] = format_text($answer->answer, $answer->answerformat, $options); 00216 $table->data[] = new html_table_row($cells); 00217 00218 $cells = array(); 00219 $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: "; 00220 $cells[] = $this->get_jump_name($answer->jumpto); 00221 $table->data[] = new html_table_row($cells); 00222 00223 if ($i === 1){ 00224 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;'; 00225 } 00226 $i++; 00227 } 00228 return $table; 00229 } 00230 public function get_grayout() { 00231 return 1; 00232 } 00233 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) { 00234 $answers = $this->get_answers(); 00235 $formattextdefoptions = new stdClass; 00236 $formattextdefoptions->para = false; //I'll use it widely in this page 00237 foreach ($answers as $answer) { 00238 $data = "<input type=\"button\" name=\"$answer->id\" value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE,$formattextdefoptions)))."\" disabled=\"disabled\"> "; 00239 $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto)); 00240 $answerdata->answers[] = array($data, ""); 00241 $answerpage->answerdata = $answerdata; 00242 } 00243 return $answerpage; 00244 } 00245 00246 public function update($properties) { 00247 if (empty($properties->display)) { 00248 $properties->display = '0'; 00249 } 00250 if (empty($properties->layout)) { 00251 $properties->layout = '0'; 00252 } 00253 return parent::update($properties); 00254 } 00255 public function add_page_link($previd) { 00256 global $PAGE, $CFG; 00257 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE)); 00258 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson')); 00259 } 00260 protected function get_displayinmenublock() { 00261 return true; 00262 } 00263 public function is_unseen($param) { 00264 global $USER, $DB; 00265 if (is_array($param)) { 00266 $seenpages = $param; 00267 $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH)); 00268 foreach ($branchpages as $branchpage) { 00269 if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed 00270 return false; 00271 } 00272 } 00273 return true; 00274 } else { 00275 $nretakes = $param; 00276 if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) { 00277 return true; 00278 } 00279 return false; 00280 } 00281 } 00282 } 00283 00284 class lesson_add_page_form_branchtable extends lesson_add_page_form_base { 00285 00286 public $qtype = LESSON_PAGE_BRANCHTABLE; 00287 public $qtypestring = 'branchtable'; 00288 protected $standard = false; 00289 00290 public function custom_definition() { 00291 global $PAGE; 00292 00293 $mform = $this->_form; 00294 $lesson = $this->_customdata['lesson']; 00295 00296 $firstpage = optional_param('firstpage', false, PARAM_BOOL); 00297 00298 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson); 00299 00300 $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson')); 00301 00302 $mform->addElement('hidden', 'firstpage'); 00303 $mform->setType('firstpage', PARAM_BOOL); 00304 $mform->setDefault('firstpage', $firstpage); 00305 00306 $mform->addElement('hidden', 'qtype'); 00307 $mform->setType('qtype', PARAM_INT); 00308 00309 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70)); 00310 $mform->setType('title', PARAM_TEXT); 00311 $mform->addRule('title', null, 'required', null, 'server'); 00312 00313 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes); 00314 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions); 00315 $mform->setType('contents_editor', PARAM_RAW); 00316 00317 $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson")); 00318 $mform->setDefault('layout', true); 00319 00320 $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson")); 00321 $mform->setDefault('display', true); 00322 00323 for ($i = 0; $i < $lesson->maxanswers; $i++) { 00324 $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1)); 00325 $this->add_answer($i, get_string("description", "lesson"), $i == 0); 00326 00327 $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions); 00328 if ($i === 0) { 00329 $mform->setDefault('jumpto['.$i.']', 0); 00330 } else { 00331 $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE); 00332 } 00333 } 00334 } 00335 }