Moodle release 2.7.4
[moodle.git] / badges / backpack_form.php
blob1941738e248a039cd132213c3452e27bb669f68b
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 * Form class for mybackpack.php
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 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/formslib.php');
30 require_once($CFG->libdir . '/badgeslib.php');
32 /**
33 * Form to edit backpack initial details.
36 class edit_backpack_form extends moodleform {
38 /**
39 * Defines the form
41 public function definition() {
42 global $USER, $PAGE, $OUTPUT;
43 $mform = $this->_form;
45 $mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
46 $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
47 $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
48 $mform->addElement('static', 'url', get_string('url'), 'http://' . BADGE_BACKPACKURL);
49 $status = html_writer::tag('span', get_string('notconnected', 'badges'),
50 array('class' => 'notconnected', 'id' => 'connection-status'));
51 $mform->addElement('static', 'status', get_string('status'), $status);
53 $nojs = html_writer::tag('noscript', get_string('error:personaneedsjs', 'badges'),
54 array('class' => 'notconnected'));
55 $personadiv = $OUTPUT->container($nojs, null, 'persona-container');
57 $mform->addElement('static', 'persona', '', $personadiv);
58 $mform->addHelpButton('persona', 'personaconnection', 'badges');
60 $PAGE->requires->js(new moodle_url('https://login.persona.org/include.js'));
61 $PAGE->requires->js('/badges/backpack.js');
62 $PAGE->requires->js_init_call('badges_init_persona_login_button', null, false);
63 $PAGE->requires->strings_for_js(array('error:backpackloginfailed', 'signinwithyouremail',
64 'error:noassertion', 'error:connectionunknownreason', 'error:badjson', 'connecting',
65 'notconnected'), 'badges');
67 $mform->addElement('hidden', 'userid', $USER->id);
68 $mform->setType('userid', PARAM_INT);
70 $mform->addElement('hidden', 'backpackurl', 'http://' . BADGE_BACKPACKURL);
71 $mform->setType('backpackurl', PARAM_URL);
75 /**
76 * Validates form data
78 public function validation($data, $files) {
79 global $DB;
80 $errors = parent::validation($data, $files);
82 $check = new stdClass();
83 $check->backpackurl = $data['backpackurl'];
84 $check->email = $data['email'];
86 $bp = new OpenBadgesBackpackHandler($check);
87 $request = $bp->curl_request('user');
88 if (isset($request->status) && $request->status == 'missing') {
89 $errors['email'] = get_string('error:nosuchuser', 'badges');
91 return $errors;
95 /**
96 * Form to select backpack collections.
99 class edit_collections_form extends moodleform {
102 * Defines the form
104 public function definition() {
105 global $USER;
106 $mform = $this->_form;
107 $email = $this->_customdata['email'];
108 $bid = $this->_customdata['backpackid'];
109 $selected = $this->_customdata['selected'];
111 if (isset($this->_customdata['groups'])) {
112 $groups = $this->_customdata['groups'];
113 $nogroups = null;
114 } else {
115 $groups = null;
116 $nogroups = $this->_customdata['nogroups'];
119 $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
120 $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
121 $mform->addElement('static', 'url', get_string('url'), 'http://' . BADGE_BACKPACKURL);
123 $status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
124 $mform->addElement('static', 'status', get_string('status'), $status);
125 $mform->addElement('static', 'email', get_string('email'), $email);
126 $mform->addHelpButton('email', 'backpackemail', 'badges');
127 $mform->addElement('submit', 'disconnect', get_string('disconnect', 'badges'));
129 $mform->addElement('header', 'collectionheader', get_string('backpackimport', 'badges'));
130 $mform->addHelpButton('collectionheader', 'backpackimport', 'badges');
132 if (!empty($groups)) {
133 $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
134 foreach ($groups as $group) {
135 $name = $group->name . ' (' . $group->badges . ')';
136 $mform->addElement('advcheckbox', 'group[' . $group->groupId . ']', null, $name, array('group' => 1), array(false, $group->groupId));
137 if (in_array($group->groupId, $selected)) {
138 $mform->setDefault('group[' . $group->groupId . ']', $group->groupId);
141 $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_end', 'badges'));
142 } else {
143 $mform->addElement('static', 'selectgroup', '', $nogroups);
146 $mform->addElement('hidden', 'backpackid', $bid);
147 $mform->setType('backpackid', PARAM_INT);
149 $this->add_action_buttons();