Merge branch 'MDL-61578-master' of git://github.com/mickhawkins/moodle
[moodle.git] / badges / mybackpack.php
blob5a403386e94be61b09cdeb6636e525b74679e890
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');
29 require_once($CFG->dirroot . '/badges/backpack_form.php');
30 require_once($CFG->dirroot . '/badges/lib/backpacklib.php');
32 require_login();
34 if (empty($CFG->enablebadges)) {
35 print_error('badgesdisabled', 'badges');
38 $context = context_user::instance($USER->id);
39 require_capability('moodle/badges:manageownbadges', $context);
41 $disconnect = optional_param('disconnect', false, PARAM_BOOL);
43 if (empty($CFG->badges_allowexternalbackpack)) {
44 redirect($CFG->wwwroot);
47 $PAGE->set_url(new moodle_url('/badges/mybackpack.php'));
48 $PAGE->set_context($context);
50 $title = get_string('backpackdetails', 'badges');
51 $PAGE->set_title($title);
52 $PAGE->set_heading(fullname($USER));
53 $PAGE->set_pagelayout('standard');
55 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
56 $badgescache = cache::make('core', 'externalbadges');
58 if ($disconnect && $backpack) {
59 require_sesskey();
60 $DB->delete_records('badge_external', array('backpackid' => $backpack->id));
61 $DB->delete_records('badge_backpack', array('userid' => $USER->id));
62 $badgescache->delete($USER->id);
63 redirect(new moodle_url('/badges/mybackpack.php'));
66 if ($backpack) {
67 // If backpack is connected, need to select collections.
68 $bp = new OpenBadgesBackpackHandler($backpack);
69 $request = $bp->get_collections();
70 if (empty($request->groups)) {
71 $params['nogroups'] = get_string('error:nogroups', 'badges');
72 } else {
73 $params['groups'] = $request->groups;
75 $params['email'] = $backpack->email;
76 $params['selected'] = $DB->get_fieldset_select('badge_external', 'collectionid', 'backpackid = :bid', array('bid' => $backpack->id));
77 $params['backpackid'] = $backpack->id;
78 $form = new edit_collections_form(new moodle_url('/badges/mybackpack.php'), $params);
80 if ($form->is_cancelled()) {
81 redirect(new moodle_url('/badges/mybadges.php'));
82 } else if ($data = $form->get_data()) {
83 if (empty($data->group)) {
84 redirect(new moodle_url('/badges/mybadges.php'));
85 } else {
86 $groups = array_filter($data->group);
89 // Remove all unselected collections if there are any.
90 $sqlparams = array('backpack' => $backpack->id);
91 $select = 'backpackid = :backpack ';
92 if (!empty($groups)) {
93 list($grouptest, $groupparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED, 'col', false);
94 $select .= ' AND collectionid ' . $grouptest;
95 $sqlparams = array_merge($sqlparams, $groupparams);
97 $DB->delete_records_select('badge_external', $select, $sqlparams);
99 // Insert selected collections if they are not in database yet.
100 foreach ($groups as $group) {
101 $obj = new stdClass();
102 $obj->backpackid = $data->backpackid;
103 $obj->collectionid = (int) $group;
104 if (!$DB->record_exists('badge_external', array('backpackid' => $obj->backpackid, 'collectionid' => $obj->collectionid))) {
105 $DB->insert_record('badge_external', $obj);
108 $badgescache->delete($USER->id);
109 redirect(new moodle_url('/badges/mybadges.php'));
111 } else {
112 // If backpack is not connected, need to connect first.
113 // To create a new connection to the backpack, first we need to verify the user's email address:
114 // 1. User enters email and clicks 'Connect to backpack'.
115 // 2. After cross-checking the email address against the backpack provider, an email is sent to the specified address,
116 // and the email and secret are stored in user preferences. These will be cleared upon successful verification.
117 // 3. User clicks verification link in the email to confirm the backpack connection.
118 // 4. User redirected to the mybackpack page.
119 // While the verification process is pending, the edit_backpack_form form will present the user with options to resend the
120 // verification email, and to cancel the current verification attempt and start over.
122 // To pass through the current state of the verification attempt to the form.
123 $params['email'] = get_user_preferences('badges_email_verify_address');
125 $form = new edit_backpack_form(new moodle_url('/badges/mybackpack.php'), $params);
126 if ($form->is_cancelled()) {
127 redirect(new moodle_url('/badges/mybadges.php'));
128 } else if ($data = $form->get_data()) {
129 // The form may have been submitted under one of the following circumstances:
130 // 1. After clicking 'Connect to backpack'. We'll have $data->email.
131 // 2. After clicking 'Resend verification email'. We'll have $data->email.
132 // 3. After clicking 'Connect using a different email' to cancel the verification process. We'll have $data->revertbutton.
133 if (isset($data->revertbutton)) {
134 unset_user_preference('badges_email_verify_secret');
135 unset_user_preference('badges_email_verify_address');
136 redirect(new moodle_url('/badges/mybackpack.php'));
137 } else if (isset($data->email)) {
138 if (send_verification_email($data->email)) {
139 redirect(new moodle_url('/badges/mybackpack.php'),
140 get_string('backpackemailverifypending', 'badges', $data->email),
141 null, \core\output\notification::NOTIFY_INFO);
142 } else {
143 print_error ('backpackcannotsendverification', 'badges');
149 echo $OUTPUT->header();
150 echo $OUTPUT->heading($title);
151 $form->display();
152 echo $OUTPUT->footer();