|
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_ENDOFBRANCH", "21"); 00031 00032 class lesson_page_type_endofbranch extends lesson_page { 00033 00034 protected $type = lesson_page::TYPE_STRUCTURE; 00035 protected $typeidstring = 'endofbranch'; 00036 protected $typeid = LESSON_PAGE_ENDOFBRANCH; 00037 protected $string = null; 00038 protected $jumpto = null; 00039 00040 public function display($renderer, $attempt) { 00041 return ''; 00042 } 00043 public function get_typeid() { 00044 return $this->typeid; 00045 } 00046 public function get_typestring() { 00047 if ($this->string===null) { 00048 $this->string = get_string($this->typeidstring, 'lesson'); 00049 } 00050 return $this->string; 00051 } 00052 public function get_idstring() { 00053 return $this->typeidstring; 00054 } 00055 public function callback_on_view($canmanage) { 00056 $this->redirect_to_first_answer($canmanage); 00057 exit; 00058 } 00059 00060 public function redirect_to_first_answer($canmanage) { 00061 global $USER, $PAGE; 00062 $answer = array_shift($this->get_answers()); 00063 $jumpto = $answer->jumpto; 00064 if ($jumpto == LESSON_RANDOMBRANCH) { 00065 00066 $jumpto = lesson_unseen_branch_jump($this->lesson, $USER->id); 00067 00068 } elseif ($jumpto == LESSON_CLUSTERJUMP) { 00069 00070 if (!$canmanage) { 00071 $jumpto = $this->lesson->cluster_jump($this->properties->id); 00072 } else { 00073 if ($this->properties->nextpageid == 0) { 00074 $jumpto = LESSON_EOL; 00075 } else { 00076 $jumpto = $this->properties->nextpageid; 00077 } 00078 } 00079 00080 } else if ($answer->jumpto == LESSON_NEXTPAGE) { 00081 00082 if ($this->properties->nextpageid == 0) { 00083 $jumpto = LESSON_EOL; 00084 } else { 00085 $jumpto = $this->properties->nextpageid; 00086 } 00087 00088 } else if ($jumpto == 0) { 00089 00090 $jumpto = $this->properties->id; 00091 00092 } else if ($jumpto == LESSON_PREVIOUSPAGE) { 00093 00094 $jumpto = $this->properties->prevpageid; 00095 00096 } 00097 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$jumpto))); 00098 } 00099 public function get_grayout() { 00100 return 1; 00101 } 00102 public function update($properties) { 00103 global $DB, $PAGE; 00104 00105 $properties->id = $this->properties->id; 00106 $properties->lessonid = $this->lesson->id; 00107 if (empty($properties->qoption)) { 00108 $properties->qoption = '0'; 00109 } 00110 $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); 00111 $DB->update_record("lesson_pages", $properties); 00112 00113 $answers = $this->get_answers(); 00114 if (count($answers)>1) { 00115 $answer = array_shift($answers); 00116 foreach ($answers as $a) { 00117 $DB->delete_record('lesson_answers', array('id'=>$a->id)); 00118 } 00119 } else if (count($answers)==1) { 00120 $answer = array_shift($answers); 00121 } else { 00122 $answer = new stdClass; 00123 } 00124 00125 $answer->timemodified = time();; 00126 if (isset($properties->jumpto[0])) { 00127 $answer->jumpto = $properties->jumpto[0]; 00128 } 00129 if (isset($properties->score[0])) { 00130 $answer->score = $properties->score[0]; 00131 } 00132 if (!empty($answer->id)) { 00133 $DB->update_record("lesson_answers", $answer->properties()); 00134 } else { 00135 $DB->insert_record("lesson_answers", $answer); 00136 } 00137 return true; 00138 } 00139 public function add_page_link($previd) { 00140 global $PAGE, $CFG; 00141 if ($previd != 0) { 00142 $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFBRANCH)); 00143 return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFBRANCH, 'name'=>get_string('addanendofbranch', 'lesson')); 00144 } 00145 return false; 00146 } 00147 public function valid_page_and_view(&$validpages, &$pageviews) { 00148 return $this->properties->nextpageid; 00149 } 00150 } 00151 00152 class lesson_add_page_form_endofbranch extends lesson_add_page_form_base { 00153 00154 public $qtype = LESSON_PAGE_ENDOFBRANCH; 00155 public $qtypestring = 'endofbranch'; 00156 protected $standard = false; 00157 00158 public function custom_definition() { 00159 global $PAGE; 00160 00161 $mform = $this->_form; 00162 $lesson = $this->_customdata['lesson']; 00163 $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson); 00164 00165 $mform->addElement('hidden', 'firstpage'); 00166 $mform->setType('firstpage', PARAM_BOOL); 00167 00168 $mform->addElement('hidden', 'qtype'); 00169 $mform->setType('qtype', PARAM_TEXT); 00170 00171 $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70)); 00172 $mform->setType('title', PARAM_TEXT); 00173 00174 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes); 00175 $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions); 00176 $mform->setType('contents_editor', PARAM_RAW); 00177 00178 $this->add_jumpto(0); 00179 } 00180 00181 public function construction_override($pageid, $lesson) { 00182 global $DB, $CFG, $PAGE; 00183 require_sesskey(); 00184 00185 // first get the preceeding page 00186 00187 $timenow = time(); 00188 00189 // the new page is not the first page (end of branch always comes after an existing page) 00190 if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) { 00191 print_error('cannotfindpagerecord', 'lesson'); 00192 } 00193 // chain back up to find the (nearest branch table) 00194 $btpage = clone($page); 00195 $btpageid = $btpage->id; 00196 while (($btpage->qtype != LESSON_PAGE_BRANCHTABLE) && ($btpage->prevpageid > 0)) { 00197 $btpageid = $btpage->prevpageid; 00198 if (!$btpage = $DB->get_record("lesson_pages", array("id" => $btpageid))) { 00199 print_error('cannotfindpagerecord', 'lesson'); 00200 } 00201 } 00202 00203 if ($btpage->qtype == LESSON_PAGE_BRANCHTABLE) { 00204 $newpage = new stdClass; 00205 $newpage->lessonid = $lesson->id; 00206 $newpage->prevpageid = $pageid; 00207 $newpage->nextpageid = $page->nextpageid; 00208 $newpage->qtype = $this->qtype; 00209 $newpage->timecreated = $timenow; 00210 $newpage->title = get_string("endofbranch", "lesson"); 00211 $newpage->contents = get_string("endofbranch", "lesson"); 00212 $newpageid = $DB->insert_record("lesson_pages", $newpage); 00213 // update the linked list... 00214 $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid)); 00215 if ($page->nextpageid) { 00216 // the new page is not the last page 00217 $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid)); 00218 } 00219 // ..and the single "answer" 00220 $newanswer = new stdClass; 00221 $newanswer->lessonid = $lesson->id; 00222 $newanswer->pageid = $newpageid; 00223 $newanswer->timecreated = $timenow; 00224 $newanswer->jumpto = $btpageid; 00225 $newanswerid = $DB->insert_record("lesson_answers", $newanswer); 00226 $lesson->add_message(get_string('addedanendofbranch', 'lesson'), 'notifysuccess'); 00227 } else { 00228 $lesson->add_message(get_string('nobranchtablefound', 'lesson')); 00229 } 00230 00231 redirect($CFG->wwwroot."/mod/lesson/edit.php?id=".$PAGE->cm->id); 00232 } 00233 }