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