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