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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Adds new instance of an enrolment plugin to specified course or edits current instance.
  19   *
  20   * @package    core_enrol
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../config.php');
  26  require_once ('editinstance_form.php');
  27  
  28  $courseid   = required_param('courseid', PARAM_INT);
  29  $type   = required_param('type', PARAM_COMPONENT);
  30  $instanceid = optional_param('id', 0, PARAM_INT);
  31  $return = optional_param('returnurl', 0, PARAM_LOCALURL);
  32  $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  33  $context = context_course::instance($course->id, MUST_EXIST);
  34  
  35  $plugin = enrol_get_plugin($type);
  36  if (!$plugin) {
  37      throw new moodle_exception('invaliddata', 'error');
  38  }
  39  
  40  require_login($course);
  41  require_capability('enrol/' . $type . ':config', $context);
  42  
  43  $url = new moodle_url('/enrol/editinstance.php', ['courseid' => $course->id, 'id' => $instanceid, 'type' => $type]);
  44  $PAGE->set_url($url);
  45  $PAGE->set_pagelayout('admin');
  46  $PAGE->set_docs_path('enrol/' . $type . '/edit');
  47  
  48  if (empty($return)) {
  49      $return = new moodle_url('/enrol/instances.php', array('id' => $course->id));
  50  }
  51  
  52  if (!enrol_is_enabled($type)) {
  53      redirect($return);
  54  }
  55  
  56  if ($instanceid) {
  57      $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => $type, 'id' => $instanceid), '*', MUST_EXIST);
  58      if ($instance->status == ENROL_INSTANCE_DISABLED) { // The instance is currently disabled.
  59          navigation_node::override_active_url(new moodle_url('/enrol/instances.php', ['id' => $course->id]));
  60          $name = $instance->name ?: get_string('pluginname', 'enrol_' . $type);
  61          $PAGE->navbar->add($name, $url);
  62      }
  63  
  64  } else {
  65      require_capability('moodle/course:enrolconfig', $context);
  66      // No instance yet, we have to add new instance.
  67      navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id' => $course->id)));
  68  
  69      $instance = (object)$plugin->get_instance_defaults();
  70      $instance->id       = null;
  71      $instance->courseid = $course->id;
  72      $instance->status   = ENROL_INSTANCE_ENABLED; // Do not use default for automatically created instances here.
  73      $PAGE->navbar->add(get_string('pluginname', 'enrol_' . $type), $url);
  74  }
  75  
  76  $mform = new enrol_instance_edit_form(null, array($instance, $plugin, $context, $type, $return));
  77  
  78  if ($mform->is_cancelled()) {
  79      redirect($return);
  80  
  81  } else if ($data = $mform->get_data()) {
  82  
  83      if ($instance->id) {
  84          $reset = false;
  85          if (isset($data->status)) {
  86              $reset = ($instance->status != $data->status);
  87          }
  88  
  89          foreach ($data as $key => $value) {
  90              $instance->$key = $value;
  91          }
  92  
  93          $instance->timemodified   = time();
  94  
  95          $plugin->update_instance($instance, $data);
  96  
  97          if ($reset) {
  98              $context->mark_dirty();
  99          }
 100  
 101      } else {
 102          $fields = (array) $data;
 103          $plugin->add_instance($course, $fields);
 104      }
 105  
 106      redirect($return);
 107  }
 108  
 109  $PAGE->set_heading($course->fullname);
 110  $PAGE->set_title(get_string('pluginname', 'enrol_' . $type));
 111  
 112  echo $OUTPUT->header();
 113  echo $OUTPUT->heading(get_string('pluginname', 'enrol_' . $type));
 114  $mform->display();
 115  echo $OUTPUT->footer();