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   * 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 moodleform {
  43  
  44      /**
  45       * Defines the form
  46       */
  47      public function definition() {
  48          global $USER, $PAGE, $OUTPUT, $CFG;
  49          $mform = $this->_form;
  50  
  51          $mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
  52          $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
  53          $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
  54          $mform->addElement('hidden', 'userid', $USER->id);
  55          $mform->setType('userid', PARAM_INT);
  56          $sitebackpack = badges_get_site_backpack($CFG->badges_site_backpack);
  57  
  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              $mform->addElement('hidden', 'backpackid', $sitebackpack->id);
  63              $mform->setType('backpackid', PARAM_INT);
  64              $status = html_writer::tag('span', get_string('backpackemailverificationpending', 'badges'),
  65                  array('class' => 'notconnected', 'id' => 'connection-status'));
  66              $mform->addElement('static', 'status', get_string('status'), $status);
  67              $mform->addElement('hidden', 'email', $this->_customdata['email']);
  68              $mform->setType('email', PARAM_EMAIL);
  69              $mform->hardFreeze(['email']);
  70              $emailverify = html_writer::tag('span', s($this->_customdata['email']), []);
  71              $mform->addElement('static', 'emailverify', get_string('email'), $emailverify);
  72              $mform->addElement('hidden', 'backpackpassword', $this->_customdata['backpackpassword']);
  73              $mform->setType('backpackpassword', PARAM_RAW);
  74              $buttonarray = [];
  75              $buttonarray[] = &$mform->createElement('submit', 'submitbutton',
  76                                                      get_string('backpackconnectionresendemail', 'badges'));
  77              $buttonarray[] = &$mform->createElement('submit', 'revertbutton',
  78                                                      get_string('backpackconnectioncancelattempt', 'badges'));
  79              $mform->addGroup($buttonarray, 'buttonar', '', [''], false);
  80              $mform->closeHeaderBefore('buttonar');
  81          } else {
  82              // Email isn't present, so provide an input element to get it and a button to start the verification process.
  83  
  84              $mform->addElement('static', 'info', get_string('backpackweburl', 'badges'), $sitebackpack->backpackweburl);
  85              $mform->addElement('hidden', 'backpackid', $sitebackpack->id);
  86              $mform->setType('backpackid', PARAM_INT);
  87  
  88              $status = html_writer::tag('span', get_string('notconnected', 'badges'),
  89                  array('class' => 'notconnected', 'id' => 'connection-status'));
  90              $mform->addElement('static', 'status', get_string('status'), $status);
  91              if (badges_open_badges_backpack_api() != OPEN_BADGES_V2P1) {
  92                  $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  93                  $mform->addHelpButton('email', 'backpackemail', 'badges');
  94                  $mform->addRule('email', get_string('required'), 'required', null, 'client');
  95                  $mform->setType('email', PARAM_EMAIL);
  96                  if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
  97                      $mform->addElement('passwordunmask', 'backpackpassword', get_string('password'));
  98                      $mform->setType('backpackpassword', PARAM_RAW);
  99                  } else {
 100                      $mform->addElement('hidden', 'backpackpassword', '');
 101                      $mform->setType('backpackpassword', PARAM_RAW);
 102                  }
 103              }
 104              $this->add_action_buttons(false, get_string('backpackconnectionconnect', 'badges'));
 105          }
 106      }
 107  
 108      /**
 109       * Validates form data
 110       */
 111      public function validation($data, $files) {
 112          global $CFG;
 113  
 114          $errors = parent::validation($data, $files);
 115          if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
 116              return $errors;
 117          }
 118          // We don't need to verify the email address if we're clearing a pending email verification attempt.
 119          if (!isset($data['revertbutton'])) {
 120              $check = new stdClass();
 121              $backpack = badges_get_site_backpack($data['backpackid']);
 122              $check->email = $data['email'];
 123              $check->password = $data['backpackpassword'];
 124              $check->externalbackpackid = $backpack->id;
 125  
 126              $bp = new \core_badges\backpack_api($backpack, $check);
 127              $result = $bp->authenticate();
 128              if ($result === false || !empty($result->error)) {
 129                  $errors['email'] = get_string('backpackconnectionunexpectedresult', 'badges');
 130                  $msg = $bp->get_authentication_error();
 131                  if (!empty($msg)) {
 132                      $errors['email'] .= '<br/><br/>';
 133                      $errors['email'] .= get_string('backpackconnectionunexpectedmessage', 'badges', $msg);
 134                  }
 135              }
 136          }
 137          return $errors;
 138      }
 139  }