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.
   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 enrol_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  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->libdir.'/formslib.php');
  28  
  29  /**
  30   * Standard edit form shared by all enrol plugins.
  31   *
  32   * @package    core_enrol
  33   * @copyright  2015 Damyon Wiese
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class enrol_instance_edit_form extends moodleform {
  37  
  38      /**
  39       * Called to define this moodle form
  40       *
  41       * @return void
  42       */
  43      public function definition() {
  44          global $DB;
  45  
  46          $mform = $this->_form;
  47  
  48          list($instance, $plugin, $context, $type, $returnurl) = $this->_customdata;
  49  
  50          $mform->addElement('header', 'header', get_string('pluginname', 'enrol_' . $type));
  51  
  52          $plugin->edit_instance_form($instance, $mform, $context);
  53  
  54          $mform->addElement('hidden', 'id');
  55          $mform->setType('id', PARAM_INT);
  56          $mform->addElement('hidden', 'courseid');
  57          $mform->setType('courseid', PARAM_INT);
  58  
  59          $mform->addElement('hidden', 'type');
  60          $mform->setType('type', PARAM_COMPONENT);
  61          $instance->type = $type;
  62  
  63          $mform->addElement('hidden', 'returnurl');
  64          $mform->setType('returnurl', PARAM_LOCALURL);
  65          $mform->setConstant('returnurl', $returnurl);
  66  
  67          $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
  68  
  69          $this->set_data($instance);
  70      }
  71  
  72      /**
  73       * Validate this form. Calls plugin validation method.
  74       *
  75       * @param array $data
  76       * @param array $files
  77       * @return array
  78       */
  79      public function validation($data, $files) {
  80          $errors = parent::validation($data, $files);
  81  
  82          list($instance, $plugin, $context, $type) = $this->_customdata;
  83  
  84          $pluginerrors = $plugin->edit_instance_validation($data, $files, $instance, $context);
  85  
  86          $errors = array_merge($errors, $pluginerrors);
  87  
  88          return $errors;
  89      }
  90  
  91  }