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   * User backpack settings page.
  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  require_once(__DIR__ . '/../config.php');
  28  require_once($CFG->libdir . '/badgeslib.php');
  29  
  30  require_login();
  31  
  32  if (empty($CFG->enablebadges)) {
  33      print_error('badgesdisabled', 'badges');
  34  }
  35  
  36  $context = context_user::instance($USER->id);
  37  require_capability('moodle/badges:manageownbadges', $context);
  38  
  39  $disconnect = optional_param('disconnect', false, PARAM_BOOL);
  40  
  41  if (empty($CFG->badges_allowexternalbackpack)) {
  42      redirect($CFG->wwwroot);
  43  }
  44  
  45  $PAGE->set_url(new moodle_url('/badges/mybackpack.php'));
  46  $PAGE->set_context($context);
  47  
  48  $title = get_string('backpackdetails', 'badges');
  49  $PAGE->set_title($title);
  50  $PAGE->set_heading(fullname($USER));
  51  $PAGE->set_pagelayout('standard');
  52  
  53  $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
  54  $badgescache = cache::make('core', 'externalbadges');
  55  
  56  if ($disconnect && $backpack) {
  57      require_sesskey();
  58      $sitebackpack = badges_get_site_backpack($backpack->externalbackpackid);
  59      if ($sitebackpack->apiversion == OPEN_BADGES_V2P1) {
  60          $bp = new \core_badges\backpack_api2p1($sitebackpack);
  61          $bp->disconnect_backpack($backpack);
  62          redirect(new moodle_url('/badges/mybackpack.php'), get_string('backpackdisconnected', 'badges'), null,
  63              \core\output\notification::NOTIFY_SUCCESS);
  64      } else {
  65          // If backpack is connected, need to select collections.
  66          $bp = new \core_badges\backpack_api($sitebackpack, $backpack);
  67          $bp->disconnect_backpack($USER->id, $backpack->id);
  68          redirect(new moodle_url('/badges/mybackpack.php'));
  69      }
  70  }
  71  $warning = '';
  72  if ($backpack) {
  73  
  74      $sitebackpack = badges_get_site_backpack($backpack->externalbackpackid);
  75  
  76      if ($sitebackpack->id != $CFG->badges_site_backpack) {
  77          $warning = $OUTPUT->notification(get_string('backpackneedsupdate', 'badges'), 'warning');
  78      }
  79  
  80      // If backpack is connected, need to select collections.
  81      $bp = new \core_badges\backpack_api($sitebackpack, $backpack);
  82      $request = $bp->get_collections();
  83      $groups = $request;
  84      if (isset($request->groups)) {
  85          $groups = $request->groups;
  86      }
  87      if (empty($groups)) {
  88          $err = get_string('error:nogroupssummary', 'badges');
  89          $err .= get_string('error:nogroupslink', 'badges', $sitebackpack->backpackweburl);
  90          $params['nogroups'] = $err;
  91      } else {
  92          $params['groups'] = $groups;
  93      }
  94      $params['email'] = $backpack->email;
  95      $params['selected'] = $bp->get_collection_record($backpack->id);
  96      $params['backpackweburl'] = $sitebackpack->backpackweburl;
  97      $form = new \core_badges\form\collections(new moodle_url('/badges/mybackpack.php'), $params);
  98  
  99      if ($form->is_cancelled()) {
 100          redirect(new moodle_url('/badges/mybadges.php'));
 101      } else if ($data = $form->get_data()) {
 102          if (empty($data->group)) {
 103              redirect(new moodle_url('/badges/mybadges.php'));
 104          } else {
 105              $groups = array_filter($data->group);
 106          }
 107          $bp->set_backpack_collections($backpack->id, $groups);
 108          redirect(new moodle_url('/badges/mybadges.php'));
 109      }
 110  } else if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
 111      // If backpack is version 2.1 to redirect on the backpack site to login.
 112      // User input username/email/password on the backpack site
 113      // After confirm the scopes.
 114      $form = new \core_badges\form\backpack(new moodle_url('/badges/mybackpack.php'));
 115      if ($form->is_cancelled()) {
 116          redirect(new moodle_url('/badges/mybadges.php'));
 117      } else if ($data = $form->get_submitted_data()) {
 118          redirect(new moodle_url('/badges/backpack-connect.php'));
 119      }
 120  } else {
 121      // If backpack is not connected, need to connect first.
 122      // To create a new connection to the backpack, first we need to verify the user's email address:
 123      // 1. User enters email and clicks 'Connect to backpack'.
 124      // 2. After cross-checking the email address against the backpack provider, an email is sent to the specified address,
 125      // and the email and secret are stored in user preferences. These will be cleared upon successful verification.
 126      // 3. User clicks verification link in the email to confirm the backpack connection.
 127      // 4. User redirected to the mybackpack page.
 128      // While the verification process is pending, the edit_backpack_form form will present the user with options to resend the
 129      // verification email, and to cancel the current verification attempt and start over.
 130  
 131      // To pass through the current state of the verification attempt to the form.
 132      $params['email'] = get_user_preferences('badges_email_verify_address');
 133      $params['backpackpassword'] = get_user_preferences('badges_email_verify_password');
 134      $params['backpackid'] = get_user_preferences('badges_email_verify_backpackid');
 135  
 136      $form = new \core_badges\form\backpack(new moodle_url('/badges/mybackpack.php'), $params);
 137      if ($form->is_cancelled()) {
 138          redirect(new moodle_url('/badges/mybadges.php'));
 139      } else if ($data = $form->get_data()) {
 140          // The form may have been submitted under one of the following circumstances:
 141          // 1. After clicking 'Connect to backpack'. We'll have $data->email.
 142          // 2. After clicking 'Resend verification email'. We'll have $data->email.
 143          // 3. After clicking 'Connect using a different email' to cancel the verification process. We'll have $data->revertbutton.
 144  
 145          if (isset($data->revertbutton)) {
 146              badges_disconnect_user_backpack($USER->id);
 147              redirect(new moodle_url('/badges/mybackpack.php'));
 148          } else if (isset($data->email)) {
 149              if (badges_send_verification_email($data->email, $data->backpackid, $data->backpackpassword)) {
 150                  $a = get_user_preferences('badges_email_verify_backpackid');
 151                  redirect(new moodle_url('/badges/mybackpack.php'),
 152                      get_string('backpackemailverifypending', 'badges', $data->email),
 153                      null, \core\output\notification::NOTIFY_INFO);
 154              } else {
 155                  print_error ('backpackcannotsendverification', 'badges');
 156              }
 157          }
 158      }
 159  }
 160  
 161  echo $OUTPUT->header();
 162  echo $OUTPUT->heading($title);
 163  echo $warning;
 164  $form->display();
 165  echo $OUTPUT->footer();