1 |
efrain |
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 |
* Form class for mybackpack.php
|
|
|
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 |
|
|
|
27 |
namespace core_badges\form;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
32 |
require_once($CFG->libdir . '/badgeslib.php');
|
|
|
33 |
|
|
|
34 |
use html_writer;
|
|
|
35 |
use moodleform;
|
|
|
36 |
use stdClass;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Form to edit backpack initial details.
|
|
|
40 |
*
|
|
|
41 |
*/
|
|
|
42 |
class backpack extends external_backpack {
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Defines the form
|
|
|
46 |
*/
|
|
|
47 |
public function definition() {
|
|
|
48 |
global $USER, $PAGE, $OUTPUT, $CFG;
|
|
|
49 |
$mform = $this->_form;
|
|
|
50 |
$this->_customdata['userbackpack'] = 1;
|
|
|
51 |
|
|
|
52 |
$mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
|
|
|
53 |
$mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
|
|
|
54 |
$mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
|
|
|
55 |
$mform->addElement('hidden', 'userid', $USER->id);
|
|
|
56 |
$mform->setType('userid', PARAM_INT);
|
|
|
57 |
$freeze = [];
|
|
|
58 |
$status = null;
|
|
|
59 |
if (isset($this->_customdata['email'])) {
|
|
|
60 |
// Email will be passed in when we're in the process of verifying the user's email address,
|
|
|
61 |
// so set the connection status, lock the email field, and provide options to resend the verification
|
|
|
62 |
// email or cancel the verification process entirely and start over.
|
|
|
63 |
$freeze = ['backpackemail'];
|
|
|
64 |
$mform->addElement('hidden', 'password', $this->_customdata['backpackpassword']);
|
|
|
65 |
$mform->setType('password', PARAM_RAW);
|
|
|
66 |
$mform->addElement('hidden', 'externalbackpackid', $this->_customdata['backpackid']);
|
|
|
67 |
$mform->setType('externalbackpackid', PARAM_INT);
|
|
|
68 |
$status = html_writer::tag('span', get_string('backpackemailverificationpending', 'badges'),
|
|
|
69 |
array('class' => 'notconnected', 'id' => 'connection-status'));
|
|
|
70 |
} else {
|
|
|
71 |
$sitebackpacks = badges_get_site_backpacks();
|
|
|
72 |
$choices = [
|
|
|
73 |
'' => get_string('choosedots'),
|
|
|
74 |
];
|
|
|
75 |
$restrictedoptions = [''];
|
|
|
76 |
foreach ($sitebackpacks as $backpack) {
|
|
|
77 |
$choices[$backpack->id] = $backpack->backpackweburl;
|
|
|
78 |
if ($backpack->apiversion == OPEN_BADGES_V2P1) {
|
|
|
79 |
$restrictedoptions[] = $backpack->id;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
$mform->addElement('select', 'externalbackpackid', get_string('backpackprovider', 'badges'), $choices);
|
|
|
83 |
$mform->setType('externalbackpackid', PARAM_INT);
|
|
|
84 |
$mform->addRule('externalbackpackid', get_string('required'), 'required');
|
|
|
85 |
$mform->hideIf('password', 'externalbackpackid', 'in', $restrictedoptions);
|
|
|
86 |
$mform->hideIf('backpackemail', 'externalbackpackid', 'in', $restrictedoptions);
|
|
|
87 |
|
|
|
88 |
// Static form element can't be used because they don't support hideIf. This is a workaround until MDL-66251 is fixed.
|
|
|
89 |
$group = [];
|
|
|
90 |
$group[] = $mform->createElement('static', 'loginbackpackgroup', '', get_string('loginbackpacktitle', 'badges'));
|
|
|
91 |
$mform->addGroup($group, 'loginbackpackgroup', '', '', false);
|
|
|
92 |
$mform->hideIf('loginbackpackgroup', 'externalbackpackid', 'in', $restrictedoptions);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if ($status) {
|
|
|
96 |
// Only display the status if it's set.
|
|
|
97 |
$mform->addElement('static', 'status', get_string('status'), $status);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$this->add_auth_fields($this->_customdata['email'] ?? $USER->email, !isset($this->_customdata['email']));
|
|
|
101 |
// Only display email and password when the user has selected a backpack.
|
|
|
102 |
$mform->hideIf('backpackemail', 'externalbackpackid', 'eq', '');
|
|
|
103 |
$mform->hideIf('password', 'externalbackpackid', 'eq', '');
|
|
|
104 |
|
|
|
105 |
$mform->setDisableShortforms(false);
|
|
|
106 |
|
|
|
107 |
// Freeze any elemnts after definition.
|
|
|
108 |
if ($freeze) {
|
|
|
109 |
$mform->freeze($freeze);
|
|
|
110 |
}
|
|
|
111 |
$this->add_action_buttons();
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Override add_action_buttons
|
|
|
116 |
*
|
|
|
117 |
* @param bool $cancel
|
|
|
118 |
* @param null|text $submitlabel
|
|
|
119 |
*/
|
|
|
120 |
public function add_action_buttons($cancel = true, $submitlabel = null) {
|
|
|
121 |
$mform = $this->_form;
|
|
|
122 |
if (isset($this->_customdata['email'])) {
|
|
|
123 |
$buttonarray = [];
|
|
|
124 |
$buttonarray[] = &$mform->createElement('submit', 'submitbutton',
|
|
|
125 |
get_string('backpackconnectionresendemail', 'badges'));
|
|
|
126 |
$buttonarray[] = &$mform->createElement('submit', 'revertbutton',
|
|
|
127 |
get_string('backpackconnectioncancelattempt', 'badges'));
|
|
|
128 |
$mform->addGroup($buttonarray, 'buttonar', '', [''], false);
|
|
|
129 |
$mform->closeHeaderBefore('buttonar');
|
|
|
130 |
} else {
|
|
|
131 |
// Email isn't present, so provide an input element to get it and a button to start the verification process.
|
|
|
132 |
parent::add_action_buttons(false, get_string('backpackconnectionconnect', 'badges'));
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Validates form data
|
|
|
138 |
*/
|
|
|
139 |
public function validation($data, $files) {
|
|
|
140 |
// Verify that the user has selected a backpack.
|
|
|
141 |
if (empty($data['externalbackpackid'])) {
|
|
|
142 |
$errors['externalbackpackid'] = get_string('externalbackpack_required', 'badges');
|
|
|
143 |
return $errors;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// We don't need to verify anything for OBv2.1.
|
|
|
147 |
if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
|
|
|
148 |
return [];
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// We don't need to verify the email address if we're clearing a pending email verification attempt.
|
|
|
152 |
if (isset($data['revertbutton'])) {
|
|
|
153 |
return [];
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
$errors = [];
|
|
|
157 |
// Email and password can't be blank.
|
|
|
158 |
if (empty($data['backpackemail'])) {
|
|
|
159 |
$errors['backpackemail'] = get_string('backpackemail_required', 'badges');
|
|
|
160 |
}
|
|
|
161 |
if (empty($data['password'])) {
|
|
|
162 |
$errors['password'] = get_string('password_required', 'badges');
|
|
|
163 |
}
|
|
|
164 |
if (!empty($errors)) {
|
|
|
165 |
return $errors;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
// Check the given credentials (email and password) are valid for this backpack.
|
|
|
169 |
$check = new stdClass();
|
|
|
170 |
$check->email = $data['backpackemail'];
|
|
|
171 |
$check->password = $data['password'];
|
|
|
172 |
$sitebackpack = badges_get_site_backpack($data['externalbackpackid']);
|
|
|
173 |
$bp = new \core_badges\backpack_api($sitebackpack, $check);
|
|
|
174 |
|
|
|
175 |
$result = $bp->authenticate();
|
|
|
176 |
if ($result === false || !empty($result->error)) {
|
|
|
177 |
$msg = $bp->get_authentication_error();
|
|
|
178 |
$errors['backpackemail'] = get_string('backpackconnectionunexpectedresult', 'badges', $msg);
|
|
|
179 |
}
|
|
|
180 |
return $errors;
|
|
|
181 |
}
|
|
|
182 |
}
|