2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * User backpack settings page.
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');
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
));
56 $badgescache = cache
::make('core', 'externalbadges');
58 if ($disconnect && $backpack) {
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'));
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');
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'));
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'));
112 // If backpack is not connected, need to connect first.
113 $form = new edit_backpack_form();
115 if ($form->is_cancelled()) {
116 redirect(new moodle_url('/badges/mybadges.php'));
117 } else if ($data = $form->get_data()) {
118 $bp = new OpenBadgesBackpackHandler($data);
120 $obj = new stdClass();
121 $obj->userid
= $data->userid
;
122 $obj->email
= $data->email
;
123 $obj->backpackurl
= $data->backpackurl
;
124 $obj->backpackuid
= $bp->curl_request('user')->userId
;
127 $DB->insert_record('badge_backpack', $obj);
129 redirect(new moodle_url('/badges/mybackpack.php'));
133 echo $OUTPUT->header();
135 echo $OUTPUT->footer();