Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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   * Handles lesson actions
  20   *
  21   * ACTIONS handled are:
  22   *    confirmdelete
  23   *    delete
  24   *    move
  25   *    moveit
  26   *    duplicate
  27   * @package mod_lesson
  28   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   **/
  31  
  32  require_once("../../config.php");
  33  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  34  
  35  $id     = required_param('id', PARAM_INT);         // Course Module ID
  36  $action = required_param('action', PARAM_ALPHA);   // Action
  37  $pageid = required_param('pageid', PARAM_INT);
  38  
  39  $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
  40  $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  41  $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
  42  
  43  require_login($course, false, $cm);
  44  
  45  $url = new moodle_url('/mod/lesson/lesson.php', array('id'=>$id,'action'=>$action));
  46  $PAGE->set_url($url);
  47  
  48  $context = context_module::instance($cm->id);
  49  require_capability('mod/lesson:edit', $context);
  50  require_sesskey();
  51  
  52  $lessonoutput = $PAGE->get_renderer('mod_lesson');
  53  
  54  /// Process the action
  55  switch ($action) {
  56      case 'confirmdelete':
  57          $PAGE->navbar->add(get_string($action, 'lesson'));
  58  
  59          $thispage = $lesson->load_page($pageid);
  60  
  61          echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('deletingpage', 'lesson', format_string($thispage->title)));
  62          echo $OUTPUT->heading(get_string("deletingpage", "lesson", format_string($thispage->title)));
  63          // print the jumps to this page
  64          $params = array("lessonid" => $lesson->id, "pageid" => $pageid);
  65          if ($answers = $DB->get_records_select("lesson_answers", "lessonid = :lessonid AND jumpto = :pageid + 1", $params)) {
  66              echo $OUTPUT->heading(get_string("thefollowingpagesjumptothispage", "lesson"));
  67              echo "<p align=\"center\">\n";
  68              foreach ($answers as $answer) {
  69                  if (!$title = $DB->get_field("lesson_pages", "title", array("id" => $answer->pageid))) {
  70                      throw new \moodle_exception('cannotfindpagetitle', 'lesson');
  71                  }
  72                  echo $title."<br />\n";
  73              }
  74          }
  75          echo $OUTPUT->confirm(get_string("confirmdeletionofthispage","lesson"),"lesson.php?action=delete&id=$cm->id&pageid=$pageid","view.php?id=$cm->id");
  76  
  77          break;
  78      case 'move':
  79          $PAGE->navbar->add(get_string($action, 'lesson'));
  80  
  81          $title = $DB->get_field("lesson_pages", "title", array("id" => $pageid));
  82  
  83          echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('moving', 'lesson', format_String($title)));
  84          $headinglevel = $PAGE->activityheader->get_heading_level();
  85          echo $OUTPUT->heading(get_string("moving", "lesson", format_string($title)), $headinglevel);
  86  
  87          $params = array ("lessonid" => $lesson->id, "prevpageid" => 0);
  88          if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
  89              throw new \moodle_exception('cannotfindfirstpage', 'lesson');
  90          }
  91  
  92          echo html_writer::start_tag('div', array('class' => 'move-page'));
  93  
  94          echo html_writer::start_tag('div', array('class' => 'available-position'));
  95          $moveurl = "lesson.php?id=$cm->id&sesskey=".sesskey()."&action=moveit&pageid=$pageid&after=0";
  96          echo html_writer::link($moveurl, get_string("movepagehere", "lesson"));
  97          echo html_writer::end_tag('div');
  98  
  99          while (true) {
 100              if ($page->id != $pageid) {
 101                  if (!$title = trim(format_string($page->title))) {
 102                      $title = "<< ".get_string("notitle", "lesson")."  >>";
 103                  }
 104                  echo html_writer::tag('div', $title, array('class' => 'page'));
 105  
 106                  echo html_writer::start_tag('div', array('class' => 'available-position'));
 107                  $moveurl = "lesson.php?id=$cm->id&sesskey=".sesskey()."&action=moveit&pageid=$pageid&after={$page->id}";
 108                  echo html_writer::link($moveurl, get_string("movepagehere", "lesson"));
 109                  echo html_writer::end_tag('div');
 110              }
 111              if ($page->nextpageid) {
 112                  if (!$page = $DB->get_record("lesson_pages", array("id" => $page->nextpageid))) {
 113                      throw new \moodle_exception('cannotfindnextpage', 'lesson');
 114                  }
 115              } else {
 116                  // last page reached
 117                  break;
 118              }
 119          }
 120          echo html_writer::end_tag('div');
 121  
 122          break;
 123      case 'delete':
 124          $thispage = $lesson->load_page($pageid);
 125          $thispage->delete();
 126          redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
 127          break;
 128      case 'moveit':
 129          $after = (int)required_param('after', PARAM_INT); // target page
 130  
 131          $lesson->resort_pages($pageid, $after);
 132          redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
 133          break;
 134      case 'duplicate':
 135              $lesson->duplicate_page($pageid);
 136              redirect(new moodle_url('/mod/lesson/edit.php', array('id' => $cm->id)));
 137          break;
 138      default:
 139          throw new \moodle_exception('unknowaction');
 140          break;
 141  }
 142  
 143  echo $lessonoutput->footer();