[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lesson/pagetypes/ -> endofcluster.php (source)

   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * End of cluster
  20   *
  21   * @package mod_lesson
  22   * @copyright  2009 Sam Hemelryk
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   **/
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28   /** End of Cluster page */
  29  define("LESSON_PAGE_ENDOFCLUSTER",   "31");
  30  
  31  class lesson_page_type_endofcluster extends lesson_page {
  32  
  33      protected $type = lesson_page::TYPE_STRUCTURE;
  34      protected $typeidstring = 'endofcluster';
  35      protected $typeid = LESSON_PAGE_ENDOFCLUSTER;
  36      protected $string = null;
  37      protected $jumpto = null;
  38  
  39      public function display($renderer, $attempt) {
  40          return '';
  41      }
  42      public function get_typeid() {
  43          return $this->typeid;
  44      }
  45      public function get_typestring() {
  46          if ($this->string===null) {
  47              $this->string = get_string($this->typeidstring, 'lesson');
  48          }
  49          return $this->string;
  50      }
  51      public function get_idstring() {
  52          return $this->typeidstring;
  53      }
  54      public function callback_on_view($canmanage) {
  55          $this->redirect_to_next_page($canmanage);
  56          exit;
  57      }
  58      public function redirect_to_next_page() {
  59          global $PAGE;
  60          if ($this->properties->nextpageid == 0) {
  61              $nextpageid = LESSON_EOL;
  62          } else {
  63              $nextpageid = $this->properties->nextpageid;
  64          }
  65          redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$nextpageid)));
  66      }
  67      public function get_grayout() {
  68          return 1;
  69      }
  70      public function update($properties, $context = null, $maxbytes = null) {
  71          global $DB, $PAGE;
  72  
  73          $properties->id = $this->properties->id;
  74          $properties->lessonid = $this->lesson->id;
  75          if (empty($properties->qoption)) {
  76              $properties->qoption = '0';
  77          }
  78          $properties->timemodified = time();
  79          $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
  80          $DB->update_record("lesson_pages", $properties);
  81  
  82          $answers  = $this->get_answers();
  83          if (count($answers)>1) {
  84              $answer = array_shift($answers);
  85              foreach ($answers as $a) {
  86                  $DB->delete_record('lesson_answers', array('id'=>$a->id));
  87              }
  88          } else if (count($answers)==1) {
  89              $answer = array_shift($answers);
  90          } else {
  91              $answer = new stdClass;
  92          }
  93  
  94          $answer->timemodified = time();
  95          if (isset($properties->jumpto[0])) {
  96              $answer->jumpto = $properties->jumpto[0];
  97          }
  98          if (isset($properties->score[0])) {
  99              $answer->score = $properties->score[0];
 100          }
 101          if (!empty($answer->id)) {
 102              $DB->update_record("lesson_answers", $answer->properties());
 103          } else {
 104              $DB->insert_record("lesson_answers", $answer);
 105          }
 106          return true;
 107      }
 108      public function override_next_page() {
 109          global $DB;
 110          $jump = $DB->get_field("lesson_answers", "jumpto", array("pageid" => $this->properties->id, "lessonid" => $this->lesson->id));
 111          if ($jump == LESSON_NEXTPAGE) {
 112              if ($this->properties->nextpageid == 0) {
 113                  return LESSON_EOL;
 114              } else {
 115                  return $this->properties->nextpageid;
 116              }
 117          } else {
 118              return $jump;
 119          }
 120      }
 121      public function add_page_link($previd) {
 122          global $PAGE, $CFG;
 123          if ($previd != 0) {
 124              $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'sesskey'=>sesskey(), 'qtype'=>LESSON_PAGE_ENDOFCLUSTER));
 125              return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_ENDOFCLUSTER, 'name'=>get_string('addendofcluster', 'lesson'));
 126          }
 127          return false;
 128      }
 129      public function valid_page_and_view(&$validpages, &$pageviews) {
 130          return $this->properties->nextpageid;
 131      }
 132  }
 133  
 134  class lesson_add_page_form_endofcluster extends lesson_add_page_form_base {
 135  
 136      public $qtype = LESSON_PAGE_ENDOFCLUSTER;
 137      public $qtypestring = 'endofcluster';
 138      protected $standard = false;
 139  
 140      public function custom_definition() {
 141          global $PAGE;
 142  
 143          $mform = $this->_form;
 144          $lesson = $this->_customdata['lesson'];
 145          $jumptooptions = lesson_page_type_branchtable::get_jumptooptions(optional_param('firstpage', false, PARAM_BOOL), $lesson);
 146  
 147          $mform->addElement('hidden', 'firstpage');
 148          $mform->setType('firstpage', PARAM_BOOL);
 149  
 150          $mform->addElement('hidden', 'qtype');
 151          $mform->setType('qtype', PARAM_TEXT);
 152  
 153          $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
 154          $mform->setType('title', PARAM_TEXT);
 155  
 156          $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
 157          $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
 158          $mform->setType('contents_editor', PARAM_RAW);
 159  
 160          $this->add_jumpto(0);
 161      }
 162  
 163      public function construction_override($pageid, lesson $lesson) {
 164          global $CFG, $PAGE, $DB;
 165          require_sesskey();
 166  
 167          $timenow = time();
 168  
 169          // the new page is not the first page (end of cluster always comes after an existing page)
 170          if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
 171              print_error('cannotfindpages', 'lesson');
 172          }
 173  
 174          // could put code in here to check if the user really can insert an end of cluster
 175  
 176          $newpage = new stdClass;
 177          $newpage->lessonid = $lesson->id;
 178          $newpage->prevpageid = $pageid;
 179          $newpage->nextpageid = $page->nextpageid;
 180          $newpage->qtype = $this->qtype;
 181          $newpage->timecreated = $timenow;
 182          $newpage->title = get_string("endofclustertitle", "lesson");
 183          $newpage->contents = get_string("endofclustertitle", "lesson");
 184          $newpageid = $DB->insert_record("lesson_pages", $newpage);
 185          // update the linked list...
 186          $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
 187          if ($page->nextpageid) {
 188              // the new page is not the last page
 189              $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->nextpageid));
 190          }
 191          // ..and the single "answer"
 192          $newanswer = new stdClass;
 193          $newanswer->lessonid = $lesson->id;
 194          $newanswer->pageid = $newpageid;
 195          $newanswer->timecreated = $timenow;
 196          $newanswer->jumpto = LESSON_NEXTPAGE;
 197          $newanswerid = $DB->insert_record("lesson_answers", $newanswer);
 198          $lesson->add_message(get_string('addedendofcluster', 'lesson'), 'notifysuccess');
 199          redirect($CFG->wwwroot.'/mod/lesson/edit.php?id='.$PAGE->cm->id);
 200      }
 201  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1