Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [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   * External backpack form
  19   *
  20   * @package    core_badges
  21   * @copyright  2019 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_badges\form;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/formslib.php');
  29  
  30  /**
  31   * Backpack form class.
  32   *
  33   * @package    core_badges
  34   * @copyright  2019 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class external_backpack extends \moodleform {
  38  
  39      /**
  40       * Create the form.
  41       *
  42       */
  43      public function definition() {
  44          global $CFG;
  45  
  46          $mform = $this->_form;
  47          $backpack = false;
  48  
  49          if (isset($this->_customdata['externalbackpack'])) {
  50              $backpack = $this->_customdata['externalbackpack'];
  51          }
  52  
  53          $mform->addElement('hidden', 'action', 'edit');
  54          $mform->setType('action', PARAM_ALPHA);
  55  
  56          if ($backpack) {
  57              $mform->addElement('hidden', 'id', $backpack->id);
  58              $mform->setType('id', PARAM_INTEGER);
  59          }
  60  
  61          $mform->addElement('text', 'backpackapiurl',  get_string('backpackapiurl', 'core_badges'));
  62          $mform->setType('backpackapiurl', PARAM_URL);
  63          $mform->addRule('backpackapiurl', null, 'required', null, 'client');
  64          $mform->addRule('backpackapiurl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  65  
  66          $mform->addElement('text', 'backpackweburl', get_string('backpackweburl', 'core_badges'));
  67          $mform->setType('backpackweburl', PARAM_URL);
  68          $mform->addRule('backpackweburl', null, 'required', null, 'client');
  69          $mform->addRule('backpackweburl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  70  
  71          $apiversions = badges_get_badge_api_versions();
  72          $mform->addElement('select', 'apiversion', get_string('apiversion', 'core_badges'), $apiversions);
  73          $mform->setType('apiversion', PARAM_RAW);
  74          $mform->setDefault('apiversion', OPEN_BADGES_V2P1);
  75          $mform->addRule('apiversion', null, 'required', null, 'client');
  76  
  77          $issuername = $CFG->badges_defaultissuername;
  78          $mform->addElement('static', 'issuerinfo', get_string('defaultissuername', 'core_badges'), $issuername);
  79  
  80          $issuercontact = $CFG->badges_defaultissuercontact;
  81          $mform->addElement('static', 'issuerinfo', get_string('defaultissuercontact', 'core_badges'), $issuercontact);
  82  
  83          $mform->addElement('passwordunmask', 'password', get_string('defaultissuerpassword', 'core_badges'));
  84          $mform->setType('password', PARAM_RAW);
  85          $mform->addHelpButton('password', 'defaultissuerpassword', 'badges');
  86          $mform->hideIf('password', 'apiversion', 'neq', 2);
  87  
  88          $oauth2options = badges_get_oauth2_service_options();
  89          $mform->addElement('select', 'oauth2_issuerid', get_string('oauth2issuer', 'core_badges'), $oauth2options);
  90          $mform->setType('oauth2_issuerid', PARAM_INT);
  91          $mform->hideIf('oauth2_issuerid', 'apiversion', 'neq', '2.1');
  92  
  93          if ($backpack) {
  94              $this->set_data($backpack);
  95          }
  96  
  97          // Disable short forms.
  98          $mform->setDisableShortforms();
  99  
 100          $this->add_action_buttons();
 101      }
 102  
 103      /**
 104       * Validate the data from the form.
 105       *
 106       * @param  array $data form data
 107       * @param  array $files form files
 108       * @return array An array of error messages.
 109       */
 110      public function validation($data, $files) {
 111          $errors = parent::validation($data, $files);
 112  
 113          // Ensure backpackapiurl and  are valid URLs.
 114          if (!empty($data['backpackapiurl']) && !preg_match('@^https?://.+@', $data['backpackapiurl'])) {
 115              $errors['backpackapiurl'] = get_string('invalidurl', 'badges');
 116          }
 117          if (!empty($data['backpackweburl']) && !preg_match('@^https?://.+@', $data['backpackweburl'])) {
 118              $errors['backpackweburl'] = get_string('invalidurl', 'badges');
 119          }
 120  
 121          return $errors;
 122      }
 123  }