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

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