[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lesson/pagetypes/ -> branchtable.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   * Branch Table
  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   /** Branch Table page */
  29  define("LESSON_PAGE_BRANCHTABLE",   "20");
  30  
  31  class lesson_page_type_branchtable extends lesson_page {
  32  
  33      protected $type = lesson_page::TYPE_STRUCTURE;
  34      protected $typeid = LESSON_PAGE_BRANCHTABLE;
  35      protected $typeidstring = 'branchtable';
  36      protected $string = null;
  37      protected $jumpto = null;
  38  
  39      public function get_typeid() {
  40          return $this->typeid;
  41      }
  42      public function get_typestring() {
  43          if ($this->string===null) {
  44              $this->string = get_string($this->typeidstring, 'lesson');
  45          }
  46          return $this->string;
  47      }
  48  
  49      /**
  50       * Gets an array of the jumps used by the answers of this page
  51       *
  52       * @return array
  53       */
  54      public function get_jumps() {
  55          global $DB;
  56          $jumps = array();
  57          $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id);
  58          if ($answers = $this->get_answers()) {
  59              foreach ($answers as $answer) {
  60                  if ($answer->answer === '') {
  61                      // show only jumps for real branches (==have description)
  62                      continue;
  63                  }
  64                  $jumps[] = $this->get_jump_name($answer->jumpto);
  65              }
  66          } else {
  67              // We get here is the lesson was created on a Moodle 1.9 site and
  68              // the lesson contains question pages without any answers.
  69              $jumps[] = $this->get_jump_name($this->properties->nextpageid);
  70          }
  71          return $jumps;
  72      }
  73  
  74      public static function get_jumptooptions($firstpage, lesson $lesson) {
  75          global $DB, $PAGE;
  76          $jump = array();
  77          $jump[0] = get_string("thispage", "lesson");
  78          $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson");
  79          $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson");
  80          $jump[LESSON_EOL] = get_string("endoflesson", "lesson");
  81          $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson");
  82          $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson");
  83          $jump[LESSON_RANDOMBRANCH] = get_string("randombranch", "lesson");
  84  
  85          if (!$firstpage) {
  86              if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
  87                  print_error('cannotfindfirstpage', 'lesson');
  88              }
  89              while (true) {
  90                  if ($apageid) {
  91                      $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid));
  92                      $jump[$apageid] = $title;
  93                      $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid));
  94                  } else {
  95                      // last page reached
  96                      break;
  97                  }
  98              }
  99           }
 100          return $jump;
 101      }
 102      public function get_idstring() {
 103          return $this->typeidstring;
 104      }
 105      public function display($renderer, $attempt) {
 106          global $PAGE, $CFG;
 107  
 108          $output = '';
 109          $options = new stdClass;
 110          $options->para = false;
 111          $options->noclean = true;
 112  
 113          if ($this->lesson->slideshow) {
 114              $output .= $renderer->slideshow_start($this->lesson);
 115          }
 116          // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
 117          $output .= $renderer->heading(format_string($this->properties->title), 3);
 118          $output .= $renderer->box($this->get_contents(), 'contents');
 119  
 120          $buttons = array();
 121          $i = 0;
 122          foreach ($this->get_answers() as $answer) {
 123              if ($answer->answer === '') {
 124                  // not a branch!
 125                  continue;
 126              }
 127              $params = array();
 128              $params['id'] = $PAGE->cm->id;
 129              $params['pageid'] = $this->properties->id;
 130              $params['sesskey'] = sesskey();
 131              $params['jumpto'] = $answer->jumpto;
 132              $url = new moodle_url('/mod/lesson/continue.php', $params);
 133              $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)));
 134              $i++;
 135          }
 136          // Set the orientation
 137          if ($this->properties->layout) {
 138              $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
 139          } else {
 140              $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
 141          }
 142          $output .= $buttonshtml;
 143  
 144          if ($this->lesson->slideshow) {
 145              $output .= $renderer->slideshow_end();
 146          }
 147  
 148          // Trigger an event: content page viewed.
 149          $eventparams = array(
 150              'context' => context_module::instance($PAGE->cm->id),
 151              'objectid' => $this->properties->id
 152              );
 153  
 154          $event = \mod_lesson\event\content_page_viewed::create($eventparams);
 155          $event->trigger();
 156  
 157          return $output;
 158      }
 159  
 160      public function check_answer() {
 161          global $USER, $DB, $PAGE, $CFG;
 162  
 163          require_sesskey();
 164          $newpageid = optional_param('jumpto', null, PARAM_INT);
 165          // going to insert into lesson_branch
 166          if ($newpageid == LESSON_RANDOMBRANCH) {
 167              $branchflag = 1;
 168          } else {
 169              $branchflag = 0;
 170          }
 171          if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
 172              $retries = count($grades);
 173          } else {
 174              $retries = 0;
 175          }
 176  
 177          //  this is called when jumping to random from a branch table
 178          $context = context_module::instance($PAGE->cm->id);
 179          if($newpageid == LESSON_UNSEENBRANCHPAGE) {
 180              if (has_capability('mod/lesson:manage', $context)) {
 181                   $newpageid = LESSON_NEXTPAGE;
 182              } else {
 183                   $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id);  // this may return 0
 184              }
 185          }
 186          // convert jumpto page into a proper page id
 187          if ($newpageid == 0) {
 188              $newpageid = $this->properties->id;
 189          } elseif ($newpageid == LESSON_NEXTPAGE) {
 190              if (!$newpageid = $this->nextpageid) {
 191                  // no nextpage go to end of lesson
 192                  $newpageid = LESSON_EOL;
 193              }
 194          } elseif ($newpageid == LESSON_PREVIOUSPAGE) {
 195              $newpageid = $this->prevpageid;
 196          } elseif ($newpageid == LESSON_RANDOMPAGE) {
 197              $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id);
 198          } elseif ($newpageid == LESSON_RANDOMBRANCH) {
 199              $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id);
 200          }
 201  
 202          // Record this page in lesson_branch.
 203          $branch = new stdClass;
 204          $branch->lessonid = $this->lesson->id;
 205          $branch->userid = $USER->id;
 206          $branch->pageid = $this->properties->id;
 207          $branch->retry = $retries;
 208          $branch->flag = $branchflag;
 209          $branch->timeseen = time();
 210          $branch->nextpageid = $newpageid;
 211          $DB->insert_record("lesson_branch", $branch);
 212  
 213          redirect(new moodle_url('/mod/lesson/view.php', array('id' => $PAGE->cm->id, 'pageid' => $newpageid)));
 214      }
 215  
 216      public function display_answers(html_table $table) {
 217          $answers = $this->get_answers();
 218          $options = new stdClass;
 219          $options->noclean = true;
 220          $options->para = false;
 221          $i = 1;
 222          foreach ($answers as $answer) {
 223              if ($answer->answer === '') {
 224                  // not a branch!
 225                  continue;
 226              }
 227              $cells = array();
 228              $cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: ";
 229              $cells[] = format_text($answer->answer, $answer->answerformat, $options);
 230              $table->data[] = new html_table_row($cells);
 231  
 232              $cells = array();
 233              $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
 234              $cells[] = $this->get_jump_name($answer->jumpto);
 235              $table->data[] = new html_table_row($cells);
 236  
 237              if ($i === 1){
 238                  $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
 239              }
 240              $i++;
 241          }
 242          return $table;
 243      }
 244      public function get_grayout() {
 245          return 1;
 246      }
 247      public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
 248          $answers = $this->get_answers();
 249          $formattextdefoptions = new stdClass;
 250          $formattextdefoptions->para = false;  //I'll use it widely in this page
 251          $formattextdefoptions->context = $answerpage->context;
 252  
 253          foreach ($answers as $answer) {
 254              $data = "<input type=\"button\" name=\"$answer->id\" value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE,$formattextdefoptions)))."\" disabled=\"disabled\"> ";
 255              $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto));
 256              $answerdata->answers[] = array($data, "");
 257              $answerpage->answerdata = $answerdata;
 258          }
 259          return $answerpage;
 260      }
 261  
 262      public function update($properties, $context = null, $maxbytes = null) {
 263          if (empty($properties->display)) {
 264              $properties->display = '0';
 265          }
 266          if (empty($properties->layout)) {
 267              $properties->layout = '0';
 268          }
 269          return parent::update($properties);
 270      }
 271      public function add_page_link($previd) {
 272          global $PAGE, $CFG;
 273          $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE));
 274          return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson'));
 275      }
 276      protected function get_displayinmenublock() {
 277          return true;
 278      }
 279      public function is_unseen($param) {
 280          global $USER, $DB;
 281          if (is_array($param)) {
 282              $seenpages = $param;
 283              $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
 284              foreach ($branchpages as $branchpage) {
 285                  if (array_key_exists($branchpage->id, $seenpages)) {  // check if any of the pages have been viewed
 286                      return false;
 287                  }
 288              }
 289              return true;
 290          } else {
 291              $nretakes = $param;
 292              if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
 293                  return true;
 294              }
 295              return false;
 296          }
 297      }
 298  }
 299  
 300  class lesson_add_page_form_branchtable extends lesson_add_page_form_base {
 301  
 302      public $qtype = LESSON_PAGE_BRANCHTABLE;
 303      public $qtypestring = 'branchtable';
 304      protected $standard = false;
 305  
 306      public function custom_definition() {
 307          global $PAGE;
 308  
 309          $mform = $this->_form;
 310          $lesson = $this->_customdata['lesson'];
 311  
 312          $firstpage = optional_param('firstpage', false, PARAM_BOOL);
 313  
 314          $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson);
 315  
 316          $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson'));
 317  
 318          $mform->addElement('hidden', 'firstpage');
 319          $mform->setType('firstpage', PARAM_BOOL);
 320          $mform->setDefault('firstpage', $firstpage);
 321  
 322          $mform->addElement('hidden', 'qtype');
 323          $mform->setType('qtype', PARAM_INT);
 324  
 325          $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
 326          $mform->setType('title', PARAM_TEXT);
 327          $mform->addRule('title', null, 'required', null, 'server');
 328  
 329          $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
 330          $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
 331          $mform->setType('contents_editor', PARAM_RAW);
 332  
 333          $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson"));
 334          $mform->setDefault('layout', true);
 335  
 336          $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson"));
 337          $mform->setDefault('display', true);
 338  
 339          for ($i = 0; $i < $lesson->maxanswers; $i++) {
 340              $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1));
 341              $this->add_answer($i, get_string("description", "lesson"), $i == 0);
 342  
 343              $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions);
 344              if ($i === 0) {
 345                  $mform->setDefault('jumpto['.$i.']', 0);
 346              } else {
 347                  $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);
 348              }
 349          }
 350      }
 351  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1