[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

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


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