6689a4f7ac085ad5a613a2a5eb5c239eac4a343c
[moodle.git] / badges / classes / form / backpack.php
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/>.
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  */
27 namespace core_badges\form;
29 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->libdir . '/formslib.php');
32 require_once($CFG->libdir . '/badgeslib.php');
34 use html_writer;
35 use moodleform;
36 use stdClass;
38 /**
39  * Form to edit backpack initial details.
40  *
41  */
42 class backpack extends external_backpack {
44     /**
45      * Defines the form
46      */
47     public function definition() {
48         global $USER, $PAGE, $OUTPUT, $CFG;
49         $mform = $this->_form;
51         $mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
52         $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
53         $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
54         $mform->addElement('hidden', 'userid', $USER->id);
55         $mform->setType('userid', PARAM_INT);
56         $mform->addElement('hidden', 'externalbackpackid');
57         $mform->setType('externalbackpackid', PARAM_INT);
58         if (isset($this->_customdata['email'])) {
59             // Email will be passed in when we're in the process of verifying the user's email address,
60             // so set the connection status, lock the email field, and provide options to resend the verification
61             // email or cancel the verification process entirely and start over.
62             $mform->hardFreeze();
63             $status = html_writer::tag('span', get_string('backpackemailverificationpending', 'badges'),
64                 array('class' => 'notconnected', 'id' => 'connection-status'));
65         } else {
66             $status = html_writer::tag('span', get_string('notconnected', 'badges'),
67                 array('class' => 'notconnected', 'id' => 'connection-status'));
68         }
69         $mform->addElement('static', 'status', get_string('status'), $status);
71         parent::definition();
73         $mform->setDefault('backpackemail', $USER->email);
74         $mform->setDisableShortforms(false);
75     }
77     /**
78      * Override add_action_buttons
79      *
80      * @param bool $cancel
81      * @param null|text $submitlabel
82      */
83     public function add_action_buttons($cancel = true, $submitlabel = null) {
84         if ($this->_customdata['email']) {
85             $mform = $this->_form;
86             $buttonarray = [];
87             $buttonarray[] = &$mform->createElement('submit', 'submitbutton',
88                                                     get_string('backpackconnectionresendemail', 'badges'));
89             $buttonarray[] = &$mform->createElement('submit', 'revertbutton',
90                                                     get_string('backpackconnectioncancelattempt', 'badges'));
91             $mform->addGroup($buttonarray, 'buttonar', '', [''], false);
92             $mform->closeHeaderBefore('buttonar');
93         } else {
94             // Email isn't present, so provide an input element to get it and a button to start the verification process.
96             $mform->addElement('static', 'info', get_string('backpackweburl', 'badges'), $sitebackpack->backpackweburl);
97             $mform->addElement('hidden', 'backpackid', $sitebackpack->id);
98             $mform->setType('backpackid', PARAM_INT);
100             $status = html_writer::tag('span', get_string('notconnected', 'badges'),
101                 array('class' => 'notconnected', 'id' => 'connection-status'));
102             $mform->addElement('static', 'status', get_string('status'), $status);
103             if (badges_open_badges_backpack_api() != OPEN_BADGES_V2P1) {
104                 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
105                 $mform->addHelpButton('email', 'backpackemail', 'badges');
106                 $mform->addRule('email', get_string('required'), 'required', null, 'client');
107                 $mform->setType('email', PARAM_EMAIL);
108                 if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
109                     $mform->addElement('passwordunmask', 'backpackpassword', get_string('password'));
110                     $mform->setType('backpackpassword', PARAM_RAW);
111                 } else {
112                     $mform->addElement('hidden', 'backpackpassword', '');
113                     $mform->setType('backpackpassword', PARAM_RAW);
114                 }
115             }
116             parent::add_action_buttons(false, get_string('backpackconnectionconnect', 'badges'));
117         }
118     }
120     /**
121      * Validates form data
122      */
123     public function validation($data, $files) {
124         global $CFG;
126         $errors = parent::validation($data, $files);
127         if (badges_open_badges_backpack_api() == OPEN_BADGES_V2P1) {
128             return $errors;
129         }
130         // We don't need to verify the email address if we're clearing a pending email verification attempt.
131         if (!isset($data['revertbutton'])) {
132             $check = new stdClass();
133             $check->email = $data['backpackemail'];
134             $check->password = $data['password'];
135             $check->externalbackpackid = $data['externalbackpackid'];
137             $bp = new \core_badges\backpack_api((object) $data, $check);
138             $result = $bp->authenticate();
139             if ($result === false || !empty($result->error)) {
140                 $errors['backpackemail'] = get_string('backpackconnectionunexpectedresult', 'badges');
141                 $msg = $bp->get_authentication_error();
142                 if (!empty($msg)) {
143                     $errors['backpackemail'] .= '<br/><br/>';
144                     $errors['backpackemail'] .= get_string('backpackconnectionunexpectedmessage', 'badges', $msg);
145                 }
146             }
147         }
148         return $errors;
149     }