Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/course/ -> modedit.php (source)

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