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/ -> modedit.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  * Adds or updates modules in a course using new formslib
  20  *
  21  * @package    moodlecore
  22  * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24  */
  25  
  26  require_once("../config.php");
  27  require_once ("lib.php");
  28  require_once($CFG->libdir.'/filelib.php');
  29  require_once($CFG->libdir.'/gradelib.php');
  30  require_once($CFG->libdir.'/completionlib.php');
  31  require_once($CFG->libdir.'/plagiarismlib.php');
  32  require_once($CFG->dirroot . '/course/modlib.php');
  33  
  34  $add    = optional_param('add', '', PARAM_ALPHANUM);     // Module name.
  35  $update = optional_param('update', 0, PARAM_INT);
  36  $return = optional_param('return', 0, PARAM_BOOL);    //return to course/view.php if false or mod/modname/view.php if true
  37  $type   = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
  38  $sectionreturn = optional_param('sr', null, PARAM_INT);
  39  $beforemod = optional_param('beforemod', 0, PARAM_INT);
  40  $showonly = optional_param('showonly', '', PARAM_TAGLIST); // Settings group to show expanded and hide the rest.
  41  
  42  $url = new moodle_url('/course/modedit.php');
  43  $url->param('sr', $sectionreturn);
  44  if (!empty($return)) {
  45      $url->param('return', $return);
  46  }
  47  if (!empty($showonly)) {
  48      $url->param('showonly', $showonly);
  49  }
  50  
  51  if (!empty($add)) {
  52      $section = required_param('section', PARAM_INT);
  53      $course  = required_param('course', PARAM_INT);
  54  
  55      $url->param('add', $add);
  56      $url->param('section', $section);
  57      $url->param('course', $course);
  58      $PAGE->set_url($url);
  59  
  60      $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
  61      require_login($course);
  62  
  63      // There is no page for this in the navigation. The closest we'll have is the course section.
  64      // If the course section isn't displayed on the navigation this will fall back to the course which
  65      // will be the closest match we have.
  66      navigation_node::override_active_url(course_get_url($course, $section));
  67  
  68      // MDL-69431 Validate that $section (url param) does not exceed the maximum for this course / format.
  69      // If too high (e.g. section *id* not number) non-sequential sections inserted in course_sections table.
  70      // Then on import, backup fills 'gap' with empty sections (see restore_rebuild_course_cache). Avoid this.
  71      $courseformat = course_get_format($course);
  72      $maxsections = $courseformat->get_max_sections();
  73      if ($section > $maxsections) {
  74          throw new \moodle_exception('maxsectionslimit', 'moodle', '', $maxsections);
  75      }
  76  
  77      list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $add, $section);
  78      $data->return = 0;
  79      $data->sr = $sectionreturn;
  80      $data->add = $add;
  81      $data->beforemod = $beforemod;
  82      if (!empty($type)) { //TODO: hopefully will be removed in 2.0
  83          $data->type = $type;
  84      }
  85  
  86      $sectionname = get_section_name($course, $cw);
  87      $fullmodulename = get_string('modulename', $module->name);
  88  
  89      if ($data->section && $course->format != 'site') {
  90          $heading = new stdClass();
  91          $heading->what = $fullmodulename;
  92          $heading->to   = $sectionname;
  93          $pageheading = get_string('addinganewto', 'moodle', $heading);
  94      } else {
  95          $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
  96      }
  97      $navbaraddition = $pageheading;
  98  
  99  } else if (!empty($update)) {
 100  
 101      $url->param('update', $update);
 102      $PAGE->set_url($url);
 103  
 104      // Select the "Edit settings" from navigation.
 105      navigation_node::override_active_url(new moodle_url('/course/modedit.php', array('update'=>$update, 'return'=>1)));
 106  
 107      // Check the course module exists.
 108      $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
 109  
 110      // Check the course exists.
 111      $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 112  
 113      // require_login
 114      require_login($course, false, $cm); // needed to setup proper $COURSE
 115  
 116      list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
 117      $data->return = $return;
 118      $data->sr = $sectionreturn;
 119      $data->update = $update;
 120      if (!empty($showonly)) {
 121          $data->showonly = $showonly;
 122      }
 123  
 124      $sectionname = get_section_name($course, $cw);
 125      $fullmodulename = get_string('modulename', $module->name);
 126  
 127      if ($data->section && $course->format != 'site') {
 128          $heading = new stdClass();
 129          $heading->what = $fullmodulename;
 130          $heading->in   = $sectionname;
 131          $pageheading = get_string('updatingain', 'moodle', $heading);
 132      } else {
 133          $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
 134      }
 135      $navbaraddition = null;
 136  
 137  } else {
 138      require_login();
 139      throw new \moodle_exception('invalidaction');
 140  }
 141  
 142  $pagepath = 'mod-' . $module->name . '-';
 143  if (!empty($type)) { //TODO: hopefully will be removed in 2.0
 144      $pagepath .= $type;
 145  } else {
 146      $pagepath .= 'mod';
 147  }
 148  $PAGE->set_pagetype($pagepath);
 149  $PAGE->set_pagelayout('admin');
 150  $PAGE->add_body_class('limitedwidth');
 151  
 152  
 153  $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
 154  if (file_exists($modmoodleform)) {
 155      require_once($modmoodleform);
 156  } else {
 157      throw new \moodle_exception('noformdesc');
 158  }
 159  
 160  $mformclassname = 'mod_'.$module->name.'_mod_form';
 161  $mform = new $mformclassname($data, $cw->section, $cm, $course);
 162  $mform->set_data($data);
 163  if (!empty($showonly)) {
 164      $mform->filter_shown_headers(explode(',', $showonly));
 165  }
 166  
 167  if ($mform->is_cancelled()) {
 168      if ($return && !empty($cm->id)) {
 169          $urlparams = [
 170              'id' => $cm->id, // We always need the activity id.
 171              'forceview' => 1, // Stop file downloads in resources.
 172          ];
 173          $activityurl = new moodle_url("/mod/$module->name/view.php", $urlparams);
 174          redirect($activityurl);
 175      } else {
 176          redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
 177      }
 178  } else if ($fromform = $mform->get_data()) {
 179      // Mark that this is happening in the front-end UI. This is used to indicate that we are able to
 180      // do regrading with a progress bar and redirect, if necessary.
 181      $fromform->frontend = true;
 182      if (!empty($fromform->update)) {
 183          list($cm, $fromform) = update_moduleinfo($cm, $fromform, $course, $mform);
 184      } else if (!empty($fromform->add)) {
 185          $fromform = add_moduleinfo($fromform, $course, $mform);
 186      } else {
 187          throw new \moodle_exception('invaliddata');
 188      }
 189  
 190      if (isset($fromform->submitbutton)) {
 191          $url = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule, 'forceview' => 1));
 192          if (!empty($fromform->showgradingmanagement)) {
 193              $url = $fromform->gradingman->get_management_url($url);
 194          }
 195      } else {
 196          $url = course_get_url($course, $cw->section, array('sr' => $sectionreturn));
 197      }
 198  
 199      // If we need to regrade the course with a progress bar as a result of updating this module,
 200      // redirect first to the page that will do this.
 201      if (isset($fromform->needsfrontendregrade)) {
 202          $url = new moodle_url('/course/modregrade.php', ['id' => $fromform->coursemodule,
 203                  'url' => $url->out_as_local_url(false)]);
 204      }
 205  
 206      redirect($url);
 207      exit;
 208  
 209  } else {
 210  
 211      $streditinga = get_string('editinga', 'moodle', $fullmodulename);
 212      $strmodulenameplural = get_string('modulenameplural', $module->name);
 213  
 214      if (!empty($cm->id)) {
 215          $context = context_module::instance($cm->id);
 216      } else {
 217          $context = context_course::instance($course->id);
 218      }
 219  
 220      $PAGE->set_heading($course->fullname);
 221      $PAGE->set_title($streditinga);
 222      $PAGE->set_cacheable(false);
 223  
 224      if (isset($navbaraddition)) {
 225          $PAGE->navbar->add($navbaraddition);
 226      }
 227      $PAGE->activityheader->disable();
 228  
 229      echo $OUTPUT->header();
 230  
 231      if (get_string_manager()->string_exists('modulename_help', $module->name)) {
 232          echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'monologo');
 233      } else {
 234          echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'monologo');
 235      }
 236  
 237      $mform->display();
 238  
 239      echo $OUTPUT->footer();
 240  }