Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/course/ -> mod.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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   * Moves, adds, updates, duplicates or deletes modules in a course
  20   *
  21   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package course
  24   */
  25  
  26  require("../config.php");
  27  require_once ("lib.php");
  28  
  29  $sectionreturn = optional_param('sr', null, PARAM_INT);
  30  $add           = optional_param('add', '', PARAM_ALPHANUM);
  31  $type          = optional_param('type', '', PARAM_ALPHA);
  32  $indent        = optional_param('indent', 0, PARAM_INT);
  33  $update        = optional_param('update', 0, PARAM_INT);
  34  $duplicate     = optional_param('duplicate', 0, PARAM_INT);
  35  $hide          = optional_param('hide', 0, PARAM_INT);
  36  $stealth       = optional_param('stealth', 0, PARAM_INT);
  37  $show          = optional_param('show', 0, PARAM_INT);
  38  $copy          = optional_param('copy', 0, PARAM_INT);
  39  $moveto        = optional_param('moveto', 0, PARAM_INT);
  40  $movetosection = optional_param('movetosection', 0, PARAM_INT);
  41  $delete        = optional_param('delete', 0, PARAM_INT);
  42  $course        = optional_param('course', 0, PARAM_INT);
  43  $groupmode     = optional_param('groupmode', -1, PARAM_INT);
  44  $cancelcopy    = optional_param('cancelcopy', 0, PARAM_BOOL);
  45  $confirm       = optional_param('confirm', 0, PARAM_BOOL);
  46  
  47  // This page should always redirect
  48  $url = new moodle_url('/course/mod.php');
  49  foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
  50      if ($value !== 0) {
  51          $url->param($key, $value);
  52      }
  53  }
  54  $url->param('sr', $sectionreturn);
  55  if ($add !== '') {
  56      $url->param('add', $add);
  57  }
  58  if ($type !== '') {
  59      $url->param('type', $type);
  60  }
  61  if ($groupmode !== '') {
  62      $url->param('groupmode', $groupmode);
  63  }
  64  $PAGE->set_url($url);
  65  
  66  require_login();
  67  
  68  //check if we are adding / editing a module that has new forms using formslib
  69  if (!empty($add)) {
  70      $id          = required_param('id', PARAM_INT);
  71      $section     = required_param('section', PARAM_INT);
  72      $type        = optional_param('type', '', PARAM_ALPHA);
  73      $returntomod = optional_param('return', 0, PARAM_BOOL);
  74      $beforemod   = optional_param('beforemod', 0, PARAM_INT);
  75  
  76      redirect(
  77          new moodle_url(
  78              '/course/modedit.php',
  79              [
  80                  'add' => $add,
  81                  'type' => $type,
  82                  'course' => $id,
  83                  'section' => $section,
  84                  'return' => $returntomod,
  85                  'sr' => $sectionreturn,
  86                  'beforemod' => $beforemod,
  87              ]
  88          )
  89      );
  90  
  91  } else if (!empty($update)) {
  92      $cm = get_coursemodule_from_id('', $update, 0, true, MUST_EXIST);
  93      $returntomod = optional_param('return', 0, PARAM_BOOL);
  94      redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod&sr=$sectionreturn");
  95  
  96  } else if (!empty($duplicate) and confirm_sesskey()) {
  97       $cm     = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST);
  98       $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  99  
 100      require_login($course, false, $cm);
 101      $modcontext = context_module::instance($cm->id);
 102      require_capability('moodle/course:manageactivities', $modcontext);
 103  
 104       // Duplicate the module.
 105       $newcm = duplicate_module($course, $cm);
 106       redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
 107  
 108  } else if (!empty($delete)) {
 109      $cm     = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST);
 110      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 111  
 112      require_login($course, false, $cm);
 113      $modcontext = context_module::instance($cm->id);
 114      require_capability('moodle/course:manageactivities', $modcontext);
 115  
 116      $return = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
 117  
 118      if (!$confirm or !confirm_sesskey()) {
 119          $fullmodulename = get_string('modulename', $cm->modname);
 120  
 121          $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey(), 'sr' => $sectionreturn);
 122  
 123          $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
 124          $strparams = (object)array('type' => $fullmodulename, 'name' => $cm->name);
 125          $strdeletechecktypename = get_string('deletechecktypename', '', $strparams);
 126  
 127          $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
 128          $PAGE->set_title($strdeletecheck);
 129          $PAGE->set_heading($course->fullname);
 130          $PAGE->navbar->add($strdeletecheck);
 131  
 132          echo $OUTPUT->header();
 133          echo $OUTPUT->box_start('noticebox');
 134          $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
 135          $formcancel = new single_button($return, get_string('no'), 'get');
 136          echo $OUTPUT->confirm($strdeletechecktypename, $formcontinue, $formcancel);
 137          echo $OUTPUT->box_end();
 138          echo $OUTPUT->footer();
 139  
 140          exit;
 141      }
 142  
 143      // Delete the module.
 144      course_delete_module($cm->id);
 145  
 146      redirect($return);
 147  }
 148  
 149  
 150  if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
 151      $cm     = get_coursemodule_from_id('', $USER->activitycopy, 0, true, MUST_EXIST);
 152      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 153  
 154      require_login($course, false, $cm);
 155      $coursecontext = context_course::instance($course->id);
 156      $modcontext = context_module::instance($cm->id);
 157      require_capability('moodle/course:manageactivities', $modcontext);
 158  
 159      if (!empty($movetosection)) {
 160          if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
 161              throw new \moodle_exception('sectionnotexist');
 162          }
 163          $beforecm = NULL;
 164  
 165      } else {                      // normal moveto
 166          if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
 167              throw new \moodle_exception('invalidcoursemodule');
 168          }
 169          if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
 170              throw new \moodle_exception('sectionnotexist');
 171          }
 172      }
 173  
 174      if (!ismoving($section->course)) {
 175          throw new \moodle_exception('needcopy', '', "view.php?id=$section->course");
 176      }
 177  
 178      moveto_module($cm, $section, $beforecm);
 179  
 180      $sectionreturn = $USER->activitycopysectionreturn;
 181      unset($USER->activitycopy);
 182      unset($USER->activitycopycourse);
 183      unset($USER->activitycopyname);
 184      unset($USER->activitycopysectionreturn);
 185  
 186      redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
 187  
 188  } else if (!empty($indent) and confirm_sesskey()) {
 189      $id = required_param('id', PARAM_INT);
 190  
 191      $cm     = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
 192      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 193  
 194      require_login($course, false, $cm);
 195      $coursecontext = context_course::instance($course->id);
 196      $modcontext = context_module::instance($cm->id);
 197      require_capability('moodle/course:manageactivities', $modcontext);
 198  
 199      $cm->indent += $indent;
 200  
 201      if ($cm->indent < 0) {
 202          $cm->indent = 0;
 203      }
 204  
 205      $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
 206  
 207      \course_modinfo::purge_course_module_cache($cm->course, $cm->id);
 208      // Rebuild invalidated module cache.
 209      rebuild_course_cache($cm->course, false, true);
 210  
 211      redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
 212  
 213  } else if (!empty($hide) and confirm_sesskey()) {
 214      $cm     = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST);
 215      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 216  
 217      require_login($course, false, $cm);
 218      $coursecontext = context_course::instance($course->id);
 219      $modcontext = context_module::instance($cm->id);
 220      require_capability('moodle/course:activityvisibility', $modcontext);
 221  
 222      if (set_coursemodule_visible($cm->id, 0)) {
 223          \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
 224      }
 225      redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
 226  
 227  } else if (!empty($stealth) and confirm_sesskey()) {
 228      list($course, $cm) = get_course_and_cm_from_cmid($stealth);
 229      require_login($course, false, $cm);
 230      require_capability('moodle/course:activityvisibility', $cm->context);
 231  
 232      if (set_coursemodule_visible($cm->id, 1, 0)) {
 233          \core\event\course_module_updated::create_from_cm($cm)->trigger();
 234      }
 235      redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
 236  
 237  } else if (!empty($show) and confirm_sesskey()) {
 238      list($course, $cm) = get_course_and_cm_from_cmid($show);
 239      require_login($course, false, $cm);
 240      require_capability('moodle/course:activityvisibility', $cm->context);
 241      $section = $cm->get_section_info();
 242  
 243      if (set_coursemodule_visible($cm->id, 1)) {
 244          \core\event\course_module_updated::create_from_cm($cm)->trigger();
 245      }
 246      redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
 247  
 248  } else if ($groupmode > -1 and confirm_sesskey()) {
 249      $id = required_param('id', PARAM_INT);
 250  
 251      $cm     = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
 252      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 253  
 254      require_login($course, false, $cm);
 255      $coursecontext = context_course::instance($course->id);
 256      $modcontext = context_module::instance($cm->id);
 257      require_capability('moodle/course:manageactivities', $modcontext);
 258  
 259      set_coursemodule_groupmode($cm->id, $groupmode);
 260      \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
 261      redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
 262  
 263  } else if (!empty($copy) and confirm_sesskey()) { // value = course module
 264      $cm     = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST);
 265      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 266  
 267      require_login($course, false, $cm);
 268      $coursecontext = context_course::instance($course->id);
 269      $modcontext = context_module::instance($cm->id);
 270      require_capability('moodle/course:manageactivities', $modcontext);
 271  
 272      $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
 273  
 274      $USER->activitycopy              = $copy;
 275      $USER->activitycopycourse        = $cm->course;
 276      $USER->activitycopyname          = $cm->name;
 277      $USER->activitycopysectionreturn = $sectionreturn;
 278  
 279      redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
 280  
 281  } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
 282  
 283      $courseid = $USER->activitycopycourse;
 284      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
 285  
 286      $cm     = get_coursemodule_from_id('', $USER->activitycopy, 0, true, IGNORE_MISSING);
 287      $sectionreturn = $USER->activitycopysectionreturn;
 288      unset($USER->activitycopy);
 289      unset($USER->activitycopycourse);
 290      unset($USER->activitycopyname);
 291      unset($USER->activitycopysectionreturn);
 292      redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
 293  } else {
 294      throw new \moodle_exception('unknowaction');
 295  }