Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Action for adding a question page.  Prints an HTML form.
  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  require_once("../../config.php");
  27  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  28  require_once ('editpage_form.php');
  29  
  30  // first get the preceeding page
  31  $pageid = required_param('pageid', PARAM_INT);
  32  $id     = required_param('id', PARAM_INT);         // Course Module ID
  33  $qtype  = optional_param('qtype', 0, PARAM_INT);
  34  $edit   = optional_param('edit', false, PARAM_BOOL);
  35  $returnto = optional_param('returnto', null, PARAM_LOCALURL);
  36  
  37  if (!empty($returnto)) {
  38      $returnto = new moodle_url($returnto);
  39  } else {
  40      $returnto = new moodle_url('/mod/lesson/edit.php', array('id' => $id));
  41      $returnto->set_anchor('lesson-' . $pageid);
  42  }
  43  
  44  $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
  45  $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  46  $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
  47  
  48  require_login($course, false, $cm);
  49  
  50  $context = context_module::instance($cm->id);
  51  require_capability('mod/lesson:edit', $context);
  52  
  53  $PAGE->set_url('/mod/lesson/editpage.php', array('pageid'=>$pageid, 'id'=>$id, 'qtype'=>$qtype));
  54  $PAGE->set_pagelayout('admin');
  55  
  56  if ($edit) {
  57      $editpage = lesson_page::load($pageid, $lesson);
  58      $qtype = $editpage->qtype;
  59      $edit = true;
  60  } else {
  61      $edit = false;
  62  }
  63  
  64  $jumpto = lesson_page::get_jumptooptions($pageid, $lesson);
  65  $manager = lesson_page_type_manager::get($lesson);
  66  $editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes);
  67  
  68  // If the previous page was the Question type selection form, this form
  69  // will have a different name (e.g. _qf__lesson_add_page_form_selection
  70  // versus _qf__lesson_add_page_form_multichoice). This causes confusion
  71  // in moodleform::_process_submission because the array key check doesn't
  72  // tie up with the current form name, which in turn means the "submitted"
  73  // check ends up evaluating as false, thus it's not possible to check whether
  74  // the Question type selection was cancelled. For this reason, a dummy form
  75  // is created here solely to check whether the selection was cancelled.
  76  if ($qtype) {
  77      $mformdummy = $manager->get_page_form(0, array(
  78          'editoroptions' => $editoroptions,
  79          'jumpto'        => $jumpto,
  80          'lesson'        => $lesson,
  81          'edit'          => $edit,
  82          'maxbytes'      => $PAGE->course->maxbytes,
  83          'returnto'      => $returnto
  84      ));
  85      if ($mformdummy->is_cancelled()) {
  86          redirect($returnto);
  87          exit;
  88      }
  89  }
  90  
  91  $mform = $manager->get_page_form($qtype, array(
  92      'editoroptions' => $editoroptions,
  93      'jumpto'        => $jumpto,
  94      'lesson'        => $lesson,
  95      'edit'          => $edit,
  96      'maxbytes'      => $PAGE->course->maxbytes,
  97      'returnto'      => $returnto
  98  ));
  99  
 100  if ($mform->is_cancelled()) {
 101      redirect($returnto);
 102      exit;
 103  }
 104  
 105  if ($edit) {
 106      $data = $editpage->properties();
 107      $data->pageid = $editpage->id;
 108      $data->id = $cm->id;
 109      $editoroptions['context'] = $context;
 110      $data = file_prepare_standard_editor($data, 'contents', $editoroptions, $context, 'mod_lesson', 'page_contents',  $editpage->id);
 111  
 112      $answerscount = 0;
 113      $answers = $editpage->get_answers();
 114      foreach ($answers as $answer) {
 115          $answereditor = 'answer_editor['.$answerscount.']';
 116          if (is_array($data->$answereditor)) {
 117              $answerdata = $data->$answereditor;
 118              if ($mform->get_answer_format() === LESSON_ANSWER_HTML) {
 119                  $answerdraftid = file_get_submitted_draft_itemid($answereditor);
 120                  $answertext = file_prepare_draft_area($answerdraftid, $PAGE->cm->context->id,
 121                          'mod_lesson', 'page_answers', $answer->id, $editoroptions, $answerdata['text']);
 122                  $data->$answereditor = array('text' => $answertext, 'format' => $answerdata['format'], 'itemid' => $answerdraftid);
 123              } else {
 124                  $data->$answereditor = $answerdata['text'];
 125              }
 126          }
 127  
 128          $responseeditor = 'response_editor['.$answerscount.']';
 129          if (is_array($data->$responseeditor)) {
 130              $responsedata = $data->$responseeditor;
 131              if ($mform->get_response_format() === LESSON_ANSWER_HTML) {
 132                  $responsedraftid = file_get_submitted_draft_itemid($responseeditor);
 133                  $responsetext = file_prepare_draft_area($responsedraftid, $PAGE->cm->context->id,
 134                          'mod_lesson', 'page_responses', $answer->id, $editoroptions, $responsedata['text']);
 135                  $data->$responseeditor = array('text' => $responsetext, 'format' => $responsedata['format'],
 136                          'itemid' => $responsedraftid);
 137              } else {
 138                  $data->$responseeditor = $responsedata['text'];
 139              }
 140          }
 141          $answerscount++;
 142      }
 143      // Let the lesson pages make updates if required.
 144      $data = $editpage->update_form_data($data);
 145  
 146      $mform->set_data($data);
 147      $PAGE->navbar->add(get_string('edit'), new moodle_url('/mod/lesson/edit.php', array('id'=>$id)));
 148      $PAGE->navbar->add(get_string('editingquestionpage', 'lesson', get_string($mform->qtypestring, 'lesson')));
 149  } else {
 150      // Give the page type being created a chance to override the creation process
 151      // this is used by endofbranch, cluster, and endofcluster to skip the creation form.
 152      // IT SHOULD ALWAYS CALL require_sesskey();
 153      $mform->construction_override($pageid, $lesson);
 154  
 155      $data = new stdClass;
 156      $data->id = $cm->id;
 157      $data->pageid = $pageid;
 158      if ($qtype) {
 159          //TODO: the handling of form for the selection of question type is a bloody hack! (skodak)
 160          $data->qtype = $qtype;
 161      }
 162      $data = file_prepare_standard_editor($data, 'contents', $editoroptions, null);
 163      $mform->set_data($data);
 164      $PAGE->navbar->add(get_string('addanewpage', 'lesson'), $PAGE->url);
 165      if ($qtype !== 'unknown') {
 166          $PAGE->navbar->add(get_string($mform->qtypestring, 'lesson'));
 167      }
 168  }
 169  
 170  if ($data = $mform->get_data()) {
 171      require_sesskey();
 172      if ($edit) {
 173          $data->lessonid = $data->id;
 174          $data->id = $data->pageid;
 175          unset($data->pageid);
 176          unset($data->edit);
 177          $editpage->update($data, $context, $PAGE->course->maxbytes);
 178      } else {
 179          $editpage = lesson_page::create($data, $lesson, $context, $PAGE->course->maxbytes);
 180      }
 181      redirect($returnto);
 182  }
 183  
 184  $lessonoutput = $PAGE->get_renderer('mod_lesson');
 185  echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('edit', 'lesson'));
 186  $mform->display();
 187  echo $lessonoutput->footer();