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   * Form for creating/updating a custom license.
  19   *
  20   * @package    tool_licensemanager
  21   * @copyright  2019 Tom Dickman <tom.dickman@catalyst-au.net>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_licensemanager\form;
  26  
  27  use moodleform;
  28  use tool_licensemanager\helper;
  29  use tool_licensemanager\manager;
  30  
  31  defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  32  
  33  global $CFG;
  34  require_once($CFG->libdir . '/formslib.php');
  35  
  36  /**
  37   * Form for creating/updating a custom license.
  38   *
  39   * @package    tool_licensemanager
  40   * @copyright  2019 Tom Dickman <tom.dickman@catalyst-au.net>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class edit_license extends moodleform {
  44  
  45      /**
  46       * @var string the action form is taking.
  47       */
  48      private $action;
  49  
  50      /**
  51       * @var string license shortname if editing or empty string if creating license.
  52       */
  53      private $licenseshortname;
  54  
  55      /**
  56       * edit_license constructor.
  57       *
  58       * @param string $action the license_manager action to be taken by form.
  59       * @param string $licenseshortname the shortname of the license to edit.
  60       */
  61      public function __construct(string $action, string $licenseshortname) {
  62          $this->action = $action;
  63          $this->licenseshortname = $licenseshortname;
  64  
  65          if ($action == manager::ACTION_UPDATE && !empty($licenseshortname)) {
  66              parent::__construct(helper::get_update_license_url($licenseshortname));
  67          } else {
  68              parent::__construct(helper::get_create_license_url());
  69          }
  70      }
  71  
  72      /**
  73       * Form definition for creation and editing of licenses.
  74       */
  75      public function definition() {
  76  
  77          $mform = $this->_form;
  78  
  79          $mform->addElement('text', 'shortname', get_string('shortname', 'tool_licensemanager'));
  80          $mform->setType('shortname', PARAM_ALPHANUMEXT);
  81          // Shortname is only editable when user is creating a license.
  82          if ($this->action != manager::ACTION_CREATE) {
  83              $mform->freeze('shortname');
  84          } else {
  85              $mform->addRule('shortname', get_string('shortnamerequirederror', 'tool_licensemanager'), 'required');
  86          }
  87  
  88          $mform->addElement('text', 'fullname', get_string('fullname', 'tool_licensemanager'));
  89          $mform->setType('fullname', PARAM_TEXT);
  90          $mform->addRule('fullname', get_string('fullnamerequirederror', 'tool_licensemanager'), 'required');
  91  
  92          $mform->addElement('text', 'source', get_string('source', 'tool_licensemanager'));
  93          $mform->setType('source', PARAM_URL);
  94          $mform->addHelpButton('source', 'source', 'tool_licensemanager');
  95          $mform->addRule('source', get_string('sourcerequirederror', 'tool_licensemanager'), 'required');
  96  
  97          $mform->addElement('date_selector', 'version', get_string('version', 'tool_licensemanager'), get_string('from'));
  98          $mform->addHelpButton('version', 'version', 'tool_licensemanager');
  99  
 100          $this->add_action_buttons();
 101      }
 102  
 103      /**
 104       * Validate form data and return errors (if any).
 105       *
 106       * @param array $data array of ("fieldname"=>value) of submitted data
 107       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 108       * @return array of "element_name"=>"error_description" if there are errors,
 109       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 110       */
 111      public function validation($data, $files) {
 112          $errors = parent::validation($data, $files);
 113  
 114          if (array_key_exists('source', $data)  && !filter_var($data['source'], FILTER_VALIDATE_URL)) {
 115              $errors['source'] = get_string('invalidurl', 'tool_licensemanager');
 116          }
 117  
 118          if (array_key_exists('version', $data) && $data['version'] > time()) {
 119              $errors['version'] = get_string('versioncannotbefuture', 'tool_licensemanager');
 120          }
 121  
 122          return $errors;
 123      }
 124  }