Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 39 and 400] [Versions 400 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          $mform->addElement('text', 'backpackapiurl',  get_string('backpackapiurl', 'core_badges'));
  57          $mform->setType('backpackapiurl', PARAM_URL);
  58          $mform->addRule('backpackapiurl', null, 'required', null, 'client');
  59          $mform->addRule('backpackapiurl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  60  
  61          $mform->addElement('text', 'backpackweburl', get_string('backpackweburl', 'core_badges'));
  62          $mform->setType('backpackweburl', PARAM_URL);
  63          $mform->addRule('backpackweburl', null, 'required', null, 'client');
  64          $mform->addRule('backpackweburl', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  65  
  66          $apiversions = badges_get_badge_api_versions();
  67          $mform->addElement('select', 'apiversion', get_string('apiversion', 'core_badges'), $apiversions);
  68          $mform->setType('apiversion', PARAM_RAW);
  69          $mform->setDefault('apiversion', OPEN_BADGES_V2P1);
  70          $mform->addRule('apiversion', null, 'required', null, 'client');
  71  
  72          $mform->addElement('hidden', 'id', ($backpack->id ?? null));
  73          $mform->setType('id', PARAM_INT);
  74          $mform->addElement('hidden', 'badgebackpack', 0);
  75          $mform->setType('badgebackpack', PARAM_INT);
  76          $mform->addElement('hidden', 'userid', 0);
  77          $mform->setType('userid', PARAM_INT);
  78          $mform->addElement('hidden', 'backpackuid', 0);
  79          $mform->setType('backpackuid', PARAM_INT);
  80  
  81          $mform->addElement('advcheckbox', 'includeauthdetails', null, get_string('includeauthdetails', 'core_badges'));
  82          if (!empty($backpack->backpackemail) || !empty($backpack->password)) {
  83              $mform->setDefault('includeauthdetails', 1);
  84          }
  85  
  86          $issuercontact = $CFG->badges_defaultissuercontact;
  87          $this->add_auth_fields($issuercontact);
  88  
  89          $oauth2options = badges_get_oauth2_service_options();
  90          $mform->addElement('select', 'oauth2_issuerid', get_string('oauth2issuer', 'core_badges'), $oauth2options);
  91          $mform->setType('oauth2_issuerid', PARAM_INT);
  92          $mform->hideIf('oauth2_issuerid', 'apiversion', 'neq', '2.1');
  93  
  94          if ($backpack) {
  95              $this->set_data($backpack);
  96          }
  97  
  98          $mform->hideIf('includeauthdetails', 'apiversion', 'in', [OPEN_BADGES_V2P1]);
  99          $mform->hideIf('backpackemail', 'includeauthdetails');
 100          $mform->hideIf('backpackemail', 'apiversion', 'in', [OPEN_BADGES_V2P1]);
 101          $mform->hideIf('password', 'includeauthdetails');
 102          $mform->hideIf('password', 'apiversion', 'in', [1, OPEN_BADGES_V2P1]);
 103  
 104          // Disable short forms.
 105          $mform->setDisableShortforms();
 106  
 107          $this->add_action_buttons();
 108      }
 109  
 110      /**
 111       * Validate the data from the form.
 112       *
 113       * @param  array $data form data
 114       * @param  array $files form files
 115       * @return array An array of error messages.
 116       */
 117      public function validation($data, $files) {
 118          $errors = parent::validation($data, $files);
 119  
 120          // Ensure backpackapiurl and  are valid URLs.
 121          if (!empty($data['backpackapiurl']) && !preg_match('@^https?://.+@', $data['backpackapiurl'])) {
 122              $errors['backpackapiurl'] = get_string('invalidurl', 'badges');
 123          }
 124          if (!empty($data['backpackweburl']) && !preg_match('@^https?://.+@', $data['backpackweburl'])) {
 125              $errors['backpackweburl'] = get_string('invalidurl', 'badges');
 126          }
 127  
 128          return $errors;
 129      }
 130  
 131      /**
 132       * Return submitted data if properly submitted or returns NULL if validation fails or
 133       * if there is no submitted data.
 134       *
 135       * @return object|void
 136       */
 137      public function get_data() {
 138          $data = parent::get_data();
 139          if ($data ) {
 140              if ((isset($data->includeauthdetails) && !$data->includeauthdetails)
 141                  || (isset($data->apiversion) && $data->apiversion == 2.1)) {
 142                  $data->backpackemail = "";
 143                  $data->password = "";
 144              }
 145  
 146              if ((isset($data->apiversion) && $data->apiversion == 1)) {
 147                  $data->password = "";
 148              }
 149          }
 150  
 151          return $data;
 152      }
 153  
 154      /**
 155       * Add backpack specific auth details.
 156       *
 157       * @param string|null $email The email addressed provided or null if it's new.
 158       * @param bool $includepassword Include the password field. Defaults to true
 159       * @throws \coding_exception
 160       */
 161      protected function add_auth_fields(?string $email, bool $includepassword = true) {
 162          $mform = $this->_form;
 163          $emailstring = get_string('email');
 164          $passwordstring = get_string('password');
 165          if (!isset($this->_customdata['userbackpack'])) {
 166              $emailstring = get_string('defaultissuercontact', 'core_badges');
 167              $passwordstring = get_string('defaultissuerpassword', 'core_badges');
 168          }
 169  
 170          $mform->addElement('text', 'backpackemail', $emailstring);
 171          $mform->setType('backpackemail', PARAM_EMAIL);
 172          $mform->setDefault('backpackemail', $email);
 173  
 174          if ($includepassword) {
 175              $mform->addElement('passwordunmask', 'password', $passwordstring);
 176              $mform->setType('password', PARAM_RAW);
 177              $mform->addHelpButton('password', 'defaultissuerpassword', 'badges');
 178          }
 179      }
 180  }