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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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   * Form class for mybackpack.php
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  25   */
  26  
  27  namespace core_badges\form;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->libdir . '/formslib.php');
  32  require_once($CFG->libdir . '/badgeslib.php');
  33  
  34  use html_writer;
  35  use moodleform;
  36  use stdClass;
  37  
  38  /**
  39   * Form to edit backpack initial details.
  40   *
  41   */
  42  class backpack extends external_backpack {
  43  
  44      /**
  45       * Defines the form
  46       */
  47      public function definition() {
  48          global $USER, $PAGE, $OUTPUT, $CFG;
  49          $mform = $this->_form;
  50          $this->_customdata['userbackpack'] = 1;
  51  
  52          $mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
  53          $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
  54          $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
  55          $mform->addElement('hidden', 'userid', $USER->id);
  56          $mform->setType('userid', PARAM_INT);
  57          $freeze = [];
  58          if (isset($this->_customdata['email'])) {
  59              // Email will be passed in when we're in the process of verifying the user's email address,
  60              // so set the connection status, lock the email field, and provide options to resend the verification
  61              // email or cancel the verification process entirely and start over.
  62              $freeze = ['backpackemail'];
  63              $mform->addElement('hidden', 'password', $this->_customdata['backpackpassword']);
  64              $mform->setType('password', PARAM_RAW);
  65              $mform->addElement('hidden', 'externalbackpackid', $this->_customdata['backpackid']);
  66              $mform->setType('externalbackpackid', PARAM_INT);
  67              $status = html_writer::tag('span', get_string('backpackemailverificationpending', 'badges'),
  68                  array('class' => 'notconnected', 'id' => 'connection-status'));
  69          } else {
  70              $sitebackpacks = badges_get_site_backpacks();
  71              $choices = [];
  72              $restrictedoptions = [];
  73              foreach ($sitebackpacks as $backpack) {
  74                  $choices[$backpack->id] = $backpack->backpackweburl;
  75                  if ($backpack->apiversion == OPEN_BADGES_V2P1) {
  76                      $restrictedoptions[] = $backpack->id;
  77                  }
  78              }
  79              $mform->addElement('select', 'externalbackpackid', get_string('backpackprovider', 'badges'), $choices);
  80              $mform->setType('externalbackpackid', PARAM_INT);
  81              $defaultbackpack = badges_get_site_primary_backpack();
  82              $mform->setDefault('externalbackpackid', $defaultbackpack->id);
  83              $mform->hideIf('password', 'externalbackpackid', 'in', $restrictedoptions);
  84              $mform->hideIf('backpackemail', 'externalbackpackid', 'in', $restrictedoptions);
  85  
  86              $status = html_writer::tag('span', get_string('notconnected', 'badges'),
  87                  array('class' => 'notconnected', 'id' => 'connection-status'));
  88          }
  89          $mform->addElement('static', 'status', get_string('status'), $status);
  90  
  91          $this->add_auth_fields($this->_customdata['email'] ?? $USER->email, !isset($this->_customdata['email']));
  92  
  93          $mform->setDisableShortforms(false);
  94  
  95          // Freeze any elemnts after definition.
  96          if ($freeze) {
  97              $mform->freeze($freeze);
  98          }
  99          $this->add_action_buttons();
 100      }
 101  
 102      /**
 103       * Override add_action_buttons
 104       *
 105       * @param bool $cancel
 106       * @param null|text $submitlabel
 107       */
 108      public function add_action_buttons($cancel = true, $submitlabel = null) {
 109          $mform = $this->_form;
 110          if (isset($this->_customdata['email'])) {
 111              $buttonarray = [];
 112              $buttonarray[] = &$mform->createElement('submit', 'submitbutton',
 113                                                      get_string('backpackconnectionresendemail', 'badges'));
 114              $buttonarray[] = &$mform->createElement('submit', 'revertbutton',
 115                                                      get_string('backpackconnectioncancelattempt', 'badges'));
 116              $mform->addGroup($buttonarray, 'buttonar', '', [''], false);
 117              $mform->closeHeaderBefore('buttonar');
 118          } else {
 119              // Email isn't present, so provide an input element to get it and a button to start the verification process.
 120              parent::add_action_buttons(false, get_string('backpackconnectionconnect', 'badges'));
 121          }
 122      }
 123  
 124      /**
 125       * Validates form data
 126       */
 127      public function validation($data, $files) {
 128          if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
 129              return [];
 130          }
 131  
 132          // We don't need to verify the email address if we're clearing a pending email verification attempt.
 133          if (isset($data['revertbutton'])) {
 134              return [];
 135          }
 136  
 137          $errors = [];
 138          // Email and password can't be blank.
 139          if (empty($data['backpackemail'])) {
 140              $errors['backpackemail'] = get_string('backpackemail_required', 'badges');
 141          }
 142          if (empty($data['password'])) {
 143              $errors['password'] = get_string('password_required', 'badges');
 144          }
 145          if (!empty($errors)) {
 146              return $errors;
 147          }
 148  
 149          // Check the given credentials (email and password) are valid for this backpack.
 150          $check = new stdClass();
 151          $check->email = $data['backpackemail'];
 152          $check->password = $data['password'];
 153          $sitebackpack = badges_get_site_backpack($data['externalbackpackid']);
 154          $bp = new \core_badges\backpack_api($sitebackpack, $check);
 155  
 156          $result = $bp->authenticate();
 157          if ($result === false || !empty($result->error)) {
 158              $msg = $bp->get_authentication_error();
 159              $errors['backpackemail'] = get_string('backpackconnectionunexpectedresult', 'badges', $msg);
 160          }
 161          return $errors;
 162      }
 163  }