MDL-35073 badges: fix xmldb editor diff
[moodle.git] / badges / mybackpack.php
blob6797c7d26a8426ce50dc7107d40cdf15b827c134
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 $clear = optional_param('clear', false, PARAM_BOOL);
43 if (!$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 navigation_node::override_active_url(new moodle_url('/badges/mybadges.php'));
56 $PAGE->navbar->add(get_string('mybackpack', 'badges'));
58 if ($clear) {
59 $DB->delete_records('badge_backpack', array('userid' => $USER->id));
60 redirect(new moodle_url('/badges/mybadges.php'));
63 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
65 if ($backpack) {
66 $bp = new OpenBadgesBackpackHandler($backpack);
67 $request = $bp->get_groups();
69 if (empty($request->groups)) {
70 unset($SESSION->badgesparams);
71 redirect(new moodle_url('/badges/mybadges.php'), get_string('error:nogroups', 'badges'), 20);
74 $params = array(
75 'data' => $backpack,
76 'backpackuid' => $request->userId,
77 'groups' => $request->groups
79 $groupform = new edit_backpack_group_form(new moodle_url('/badges/mybackpack.php'), $params);
81 if ($groupform->is_cancelled()) {
82 redirect(new moodle_url('/badges/mybadges.php'));
83 } else if ($groupdata = $groupform->get_data()) {
84 $obj = new stdClass();
85 $obj->userid = $groupdata->userid;
86 $obj->email = $groupdata->email;
87 $obj->backpackurl = $groupdata->backpackurl;
88 $obj->backpackuid = $groupdata->backpackuid;
89 $obj->backpackgid = $groupdata->backpackgid;
90 $obj->autosync = 0;
91 $obj->password = '';
92 if ($rec = $DB->get_record('badge_backpack', array('userid' => $groupdata->userid))) {
93 $obj->id = $rec->id;
94 $DB->update_record('badge_backpack', $obj);
95 } else {
96 $DB->insert_record('badge_backpack', $obj);
98 redirect(new moodle_url('/badges/mybadges.php'));
101 echo $OUTPUT->header();
102 $groupform->display();
103 echo $OUTPUT->footer();
104 die();
105 } else {
106 $form = new edit_backpack_form();
108 if ($form->is_cancelled()) {
109 redirect(new moodle_url('/badges/mybadges.php'));
110 } else if (($data = $form->get_data()) || !empty($SESSION->badgesparams)) {
111 if (empty($SESSION->badgesparams)) {
112 $bp = new OpenBadgesBackpackHandler($data);
113 $request = $bp->get_groups();
115 // If there is an error, start over.
116 if (is_array($request) && $request['status'] == 'missing') {
117 unset($SESSION->badgesparams);
118 redirect(new moodle_url('/badges/mybackpack.php'), $request['message'], 10);
119 } else if (empty($request->groups)) {
120 unset($SESSION->badgesparams);
121 redirect(new moodle_url('/badges/mybadges.php'), get_string('error:nogroups', 'badges'), 20);
124 $params = array(
125 'data' => $data,
126 'backpackuid' => $request->userId,
127 'groups' => $request->groups
129 $SESSION->badgesparams = $params;
131 $groupform = new edit_backpack_group_form(new moodle_url('/badges/mybackpack.php', array('addgroups' => true)), $SESSION->badgesparams);
133 if ($groupform->is_cancelled()) {
134 unset($SESSION->badgesparams);
135 redirect(new moodle_url('/badges/mybadges.php'));
136 } else if ($groupdata = $groupform->get_data()) {
137 $obj = new stdClass();
138 $obj->userid = $groupdata->userid;
139 $obj->email = $groupdata->email;
140 $obj->backpackurl = $groupdata->backpackurl;
141 $obj->backpackuid = $groupdata->backpackuid;
142 $obj->backpackgid = $groupdata->backpackgid;
143 $obj->autosync = 0;
144 $obj->password = '';
145 if ($rec = $DB->get_record('badge_backpack', array('userid' => $groupdata->userid))) {
146 $obj->id = $rec->id;
147 $DB->update_record('badge_backpack', $obj);
148 } else {
149 $DB->insert_record('badge_backpack', $obj);
151 unset($SESSION->badgesparams);
152 redirect(new moodle_url('/badges/mybadges.php'));
155 echo $OUTPUT->header();
156 $groupform->display();
157 echo $OUTPUT->footer();
158 die();
161 echo $OUTPUT->header();
162 $form->display();
163 echo $OUTPUT->footer();