Merge branch 'MDL-63625-master' of git://github.com/marinaglancy/moodle
[moodle.git] / badges / backpack_form.php
blobe1b51957e708c2ee731517e2e7857770d8651a68
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'), BADGE_BACKPACKURL);
50 $mform->addElement('hidden', 'userid', $USER->id);
51 $mform->setType('userid', PARAM_INT);
53 $mform->addElement('hidden', 'backpackurl', BADGE_BACKPACKURL);
54 $mform->setType('backpackurl', PARAM_URL);
56 if (isset($this->_customdata['email'])) {
57 // Email will be passed in when we're in the process of verifying the user's email address,
58 // so set the connection status, lock the email field, and provide options to resend the verification
59 // email or cancel the verification process entirely and start over.
60 $status = html_writer::tag('span', get_string('backpackemailverificationpending', 'badges'),
61 array('class' => 'notconnected', 'id' => 'connection-status'));
62 $mform->addElement('static', 'status', get_string('status'), $status);
63 $mform->addElement('hidden', 'email', $this->_customdata['email']);
64 $mform->setType('email', PARAM_EMAIL);
65 $mform->hardFreeze(['email']);
66 $emailverify = html_writer::tag('span', s($this->_customdata['email']), []);
67 $mform->addElement('static', 'emailverify', get_string('email'), $emailverify);
68 $buttonarray = [];
69 $buttonarray[] = &$mform->createElement('submit', 'submitbutton',
70 get_string('backpackconnectionresendemail', 'badges'));
71 $buttonarray[] = &$mform->createElement('submit', 'revertbutton',
72 get_string('backpackconnectioncancelattempt', 'badges'));
73 $mform->addGroup($buttonarray, 'buttonar', '', [''], false);
74 $mform->closeHeaderBefore('buttonar');
75 } else {
76 // Email isn't present, so provide an input element to get it and a button to start the verification process.
77 $status = html_writer::tag('span', get_string('notconnected', 'badges'),
78 array('class' => 'notconnected', 'id' => 'connection-status'));
79 $mform->addElement('static', 'status', get_string('status'), $status);
80 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
81 $mform->addHelpButton('email', 'backpackemail', 'badges');
82 $mform->addRule('email', get_string('required'), 'required', null, 'client');
83 $mform->setType('email', PARAM_EMAIL);
84 $this->add_action_buttons(false, get_string('backpackconnectionconnect', 'badges'));
88 /**
89 * Validates form data
91 public function validation($data, $files) {
92 $errors = parent::validation($data, $files);
94 // We don't need to verify the email address if we're clearing a pending email verification attempt.
95 if (!isset($data['revertbutton'])) {
96 $check = new stdClass();
97 $check->backpackurl = $data['backpackurl'];
98 $check->email = $data['email'];
100 $bp = new OpenBadgesBackpackHandler($check);
101 $request = $bp->curl_request('user');
102 if (isset($request->status) && $request->status == 'missing') {
103 $errors['email'] = get_string('error:nosuchuser', 'badges');
104 } else if (!isset($request->status) || $request->status !== 'okay') {
105 $errors['email'] = get_string('backpackconnectionunexpectedresult', 'badges');
108 return $errors;
113 * Form to select backpack collections.
116 class edit_collections_form extends moodleform {
119 * Defines the form
121 public function definition() {
122 global $USER;
123 $mform = $this->_form;
124 $email = $this->_customdata['email'];
125 $bid = $this->_customdata['backpackid'];
126 $selected = $this->_customdata['selected'];
128 if (isset($this->_customdata['groups'])) {
129 $groups = $this->_customdata['groups'];
130 $nogroups = null;
131 } else {
132 $groups = null;
133 $nogroups = $this->_customdata['nogroups'];
136 $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
137 $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
138 $mform->addElement('static', 'url', get_string('url'), BADGE_BACKPACKURL);
140 $status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
141 $mform->addElement('static', 'status', get_string('status'), $status);
142 $mform->addElement('static', 'email', get_string('email'), $email);
143 $mform->addHelpButton('email', 'backpackemail', 'badges');
144 $mform->addElement('submit', 'disconnect', get_string('disconnect', 'badges'));
146 $mform->addElement('header', 'collectionheader', get_string('backpackimport', 'badges'));
147 $mform->addHelpButton('collectionheader', 'backpackimport', 'badges');
149 if (!empty($groups)) {
150 $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
151 foreach ($groups as $group) {
152 $name = $group->name . ' (' . $group->badges . ')';
153 $mform->addElement('advcheckbox', 'group[' . $group->groupId . ']', null, $name, array('group' => 1), array(false, $group->groupId));
154 if (in_array($group->groupId, $selected)) {
155 $mform->setDefault('group[' . $group->groupId . ']', $group->groupId);
158 $mform->addElement('static', 'selectgroup', '', get_string('selectgroup_end', 'badges'));
159 } else {
160 $mform->addElement('static', 'selectgroup', '', $nogroups);
163 $mform->addElement('hidden', 'backpackid', $bid);
164 $mform->setType('backpackid', PARAM_INT);
166 $this->add_action_buttons();