source: moodle/trunk/fuentes/badges/mybackpack.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 5.0 KB
Line 
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
27require_once(dirname(dirname(__FILE__)) . '/config.php');
28require_once($CFG->libdir . '/badgeslib.php');
29require_once($CFG->dirroot . '/badges/backpack_form.php');
30require_once($CFG->dirroot . '/badges/lib/backpacklib.php');
31
32require_login();
33
34if (empty($CFG->enablebadges)) {
35    print_error('badgesdisabled', 'badges');
36}
37
38$context = context_user::instance($USER->id);
39require_capability('moodle/badges:manageownbadges', $context);
40
41$disconnect = optional_param('disconnect', false, PARAM_BOOL);
42
43if (empty($CFG->badges_allowexternalbackpack)) {
44    redirect($CFG->wwwroot);
45}
46
47$PAGE->set_url(new moodle_url('/badges/mybackpack.php'));
48$PAGE->set_context($context);
49
50$title = get_string('backpackdetails', 'badges');
51$PAGE->set_title($title);
52$PAGE->set_heading(fullname($USER));
53$PAGE->set_pagelayout('mydashboard');
54
55$backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
56$badgescache = cache::make('core', 'externalbadges');
57
58if ($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'));
64}
65
66if ($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;
74    }
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);
79
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);
87        }
88
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);
96        }
97        $DB->delete_records_select('badge_external', $select, $sqlparams);
98
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);
106            }
107        }
108        $badgescache->delete($USER->id);
109        redirect(new moodle_url('/badges/mybadges.php'));
110    }
111} else {
112    // If backpack is not connected, need to connect first.
113    $form = new edit_backpack_form();
114
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);
119
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;
125        $obj->autosync = 0;
126        $obj->password = '';
127        $DB->insert_record('badge_backpack', $obj);
128
129        redirect(new moodle_url('/badges/mybackpack.php'));
130    }
131}
132
133echo $OUTPUT->header();
134echo $OUTPUT->heading($title);
135$form->display();
136echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.