Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / badges / mybackpack.php
blob1f524539900830380b6e2bb108c292545cf5b7bc
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(dirname(dirname(__FILE__)) . '/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('mybackpack', 'badges');
51 $PAGE->set_title($title);
52 $PAGE->set_heading($title);
53 $PAGE->set_pagelayout('mydashboard');
55 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
57 if ($disconnect && $backpack) {
58 require_sesskey();
59 $DB->delete_records('badge_external', array('backpackid' => $backpack->id));
60 $DB->delete_records('badge_backpack', array('userid' => $USER->id));
61 redirect(new moodle_url('/badges/mybackpack.php'));
64 if ($backpack) {
65 // If backpack is connected, need to select collections.
66 $bp = new OpenBadgesBackpackHandler($backpack);
67 $request = $bp->get_collections();
68 if (empty($request->groups)) {
69 $params['nogroups'] = get_string('error:nogroups', 'badges');
70 } else {
71 $params['groups'] = $request->groups;
73 $params['email'] = $backpack->email;
74 $params['selected'] = $DB->get_fieldset_select('badge_external', 'collectionid', 'backpackid = :bid', array('bid' => $backpack->id));
75 $params['backpackid'] = $backpack->id;
76 $form = new edit_collections_form(new moodle_url('/badges/mybackpack.php'), $params);
78 if ($form->is_cancelled()) {
79 redirect(new moodle_url('/badges/mybadges.php'));
80 } else if ($data = $form->get_data()) {
81 if (empty($data->group)) {
82 redirect(new moodle_url('/badges/mybadges.php'));
83 } else {
84 $groups = array_filter($data->group);
87 // Remove all unselected collections if there are any.
88 $sqlparams = array('backpack' => $backpack->id);
89 $select = 'backpackid = :backpack ';
90 if (!empty($groups)) {
91 list($grouptest, $groupparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED, 'col', false);
92 $select .= ' AND collectionid ' . $grouptest;
93 $sqlparams = array_merge($sqlparams, $groupparams);
95 $DB->delete_records_select('badge_external', $select, $sqlparams);
97 // Insert selected collections if they are not in database yet.
98 foreach ($groups as $group) {
99 $obj = new stdClass();
100 $obj->backpackid = $data->backpackid;
101 $obj->collectionid = (int) $group;
102 if (!$DB->record_exists('badge_external', array('backpackid' => $obj->backpackid, 'collectionid' => $obj->collectionid))) {
103 $DB->insert_record('badge_external', $obj);
106 redirect(new moodle_url('/badges/mybadges.php'));
108 } else {
109 // If backpack is not connected, need to connect first.
110 $form = new edit_backpack_form();
112 if ($form->is_cancelled()) {
113 redirect(new moodle_url('/badges/mybadges.php'));
114 } else if ($data = $form->get_data()) {
115 $bp = new OpenBadgesBackpackHandler($data);
117 $obj = new stdClass();
118 $obj->userid = $data->userid;
119 $obj->email = $data->email;
120 $obj->backpackurl = $data->backpackurl;
121 $obj->backpackuid = $bp->curl_request('user')->userId;
122 $obj->autosync = 0;
123 $obj->password = '';
124 $DB->insert_record('badge_backpack', $obj);
126 redirect(new moodle_url('/badges/mybackpack.php'));
130 echo $OUTPUT->header();
131 $form->display();
132 echo $OUTPUT->footer();