forked from udima-university/moodle-mod_jitsi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_form.php
244 lines (213 loc) · 9.7 KB
/
mod_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The main jitsi configuration form
*
* It uses the standard core Moodle formslib. For more info about them, please
* visit: http://docs.moodle.org/en/Development:lib/formslib.php
*
* @package mod_jitsi
* @copyright 2019 Sergio Comerón Sánchez-Paniagua <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/course/moodleform_mod.php');
/**
* Jitsi settings form.
*
* @package mod_jitsi
* @copyright 2019 Sergio Comerón Sánchez-Paniagua <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_jitsi_mod_form extends moodleform_mod {
/**
* Defines forms elements
*/
public function definition() {
global $CFG, $PAGE;
$mform = $this->_form;
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'name', get_string('jitsiname', 'jitsi'), ['size' => '64']);
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'modulename', 'jitsi');
$mform->addElement('advcheckbox', 'sessionwithtoken', get_string('sharedsessionwithtoken', 'jitsi'));
$mform->setDefault('sessionwithtoken', 0);
if (!isset($this->current->tokeninterno)) {
$tokeninterno = bin2hex(random_bytes(32));
} else {
$tokeninterno = $this->current->tokeninterno;
}
$mform->addElement('hidden', 'tokeninterno', $tokeninterno);
$mform->setDefault('tokeninterno', $tokeninterno);
$mform->addElement('static', 'tokeninternocompartir', get_string('tokeninterno', 'jitsi'), $tokeninterno);
$mform->addHelpButton('tokeninternocompartir', 'tokeninternocompartir', 'jitsi');
$mform->setType('tokeninterno', PARAM_TEXT);
if ($mform->getElementValue('sessionwithtoken') == 0) {
$mform->setDefault('tokeninvitacion', '');
}
$mform->addElement('text', 'tokeninvitacion', get_string('tokeninvitacion', 'jitsi'), ['size' => '70']);
$mform->hideIf('tokeninvitacion', 'sessionwithtoken', 'notchecked');
$mform->addHelpButton('tokeninvitacion', 'tokeninvitacion', 'jitsi');
$mform->setType('tokeninvitacion', PARAM_TEXT);
$this->standard_intro_elements();
$mform->addElement('header', 'availability', get_string('availability', 'assign'));
$name = get_string('allow', 'jitsi');
$options = ['optional' => true];
$mform->addElement('date_time_selector', 'timeopen', $name, $options);
$mform->disabledIf('timeopen', 'sessionwithtoken', 'checked');
$mform->disabledIf('timeclose', 'sessionwithtoken', 'checked');
$name = get_string('close', 'jitsi');
$options = ['optional' => true];
$mform->addElement('date_time_selector', 'timeclose', $name, $options);
$choicesminspre = [
5 => 5,
10 => 10,
15 => 15,
20 => 20,
30 => 30,
];
$mform->addElement('select', 'minpretime', get_string('minpretime', 'jitsi'), $choicesminspre);
$mform->disabledIf('minpretime', 'timeopen[enabled]');
$mform->addHelpButton('minpretime', 'minpretime', 'jitsi');
if ($CFG->jitsi_invitebuttons == 1) {
$optionsinvitation = ['defaulttime' => time() + 86400, 'optional' => true];
$mform->addElement('header', 'invitations', get_string('externalinvitations', 'jitsi'));
$mform->addElement('static', 'linkexplication', '', get_string('staticinvitationlinkcapabilityex', 'jitsi'));
$mform->addElement('date_time_selector', 'validitytime',
get_string('finishinvitation', 'jitsi'), $optionsinvitation);
if (!has_capability('mod/jitsi:createlink', $PAGE->context)) {
$mform->hardFreeze('validitytime');
}
if ($mform->getElementValue('validitytime') < time()) {
$mform->addElement('static', 'linkexpired', '', 'linkExpired2');
}
if (!isset($this->current->token)) {
$token = bin2hex(random_bytes(32));
} else {
$token = $this->current->token;
}
$mform->addElement('hidden', 'token', $token);
$mform->setDefault('token', $token);
$urlinvitacion = $CFG->wwwroot.'/mod/jitsi/formuniversal.php?t='.$token;
$mform->addElement('static', 'urltoken', get_string('urlinvitacion', 'jitsi'), $urlinvitacion);
$mform->setType('token', PARAM_TEXT);
} else {
if (!isset($this->current->token)) {
$token = bin2hex(random_bytes(32));
} else {
$token = $this->current->token;
}
$mform->addElement('hidden', 'token', $token);
$mform->setDefault('token', $token);
$mform->setType('token', PARAM_TEXT);
}
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
/**
* Add elements for setting the custom completion rules.
*
* @category completion
* @return array List of added element names, or names of wrapping group elements.
*/
public function add_completion_rules() {
$mform =& $this->_form;
$group = [
$mform->createElement('checkbox', 'completionminutesenabled', ' ', get_string('completionminutesex', 'jitsi')),
$mform->createElement('text', 'completionminutes', ' ', ['size' => 3]),
];
$mform->setType('completionminutes', PARAM_INT);
$mform->addGroup($group, 'completionminutesgroup', get_string('completionminutes', 'jitsi'), [' '], false);
$mform->addHelpButton('completionminutesgroup', 'completionminutes', 'jitsi');
$mform->disabledIf('completionminutes', 'completionminutesenabled', 'notchecked');
return ['completionminutesgroup'];
}
/**
* Called during validation to see whether some module-specific completion rules are selected.
*
* @param array $data Input data not yet validated.
* @return bool True if one or more rules is enabled, false if none are.
*/
public function completion_rule_enabled($data) {
return (!empty($data['completionminutesenabled']) && $data['completionminutes'] != 0);
}
/**
* Get Data
*/
public function get_data() {
$data = parent::get_data();
if (!$data) {
return $data;
}
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completionminutesenabled) || !$autocompletion) {
$data->completionminutes = 0;
}
}
return $data;
}
/**
* Data validation
*
* @param array $data Input data to validated.
* @param array $files Files uploaded.
* @return String error message, if any.
*/
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
// Check open and close times are consistent.
if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
$data['timeclose'] < $data['timeopen']) {
$errors['timeclose'] = get_string('closebeforeopen', 'jitsi');
}
// Check validitity time is consistent with open and close times.
if (isset($data['validitytime']) && $data['validitytime'] != 0) {
if (($data['timeopen'] != 0 && $data['validitytime'] < $data['timeopen']) ||
($data['timeclose'] != 0 && $data['validitytime'] > $data['timeclose'])) {
$errors['validitytime'] = get_string('validitytimevalidation', 'jitsi');
}
}
if (isset($data['validitytime']) && $data['validitytime'] <= time() && $data['validitytime'] != 0) {
$errors['validitytime'] = get_string('tokeninvitationnotvalid', 'jitsi');
}
if ($data['sessionwithtoken'] == 1) {
$sql = "select * from {jitsi} where tokeninterno = '".$data['tokeninvitacion']."'";
if ($DB->get_record_sql($sql) == null) {
$errors['tokeninvitacion'] = get_string('tokeninvitationvalidation', 'jitsi');
}
}
return $errors;
}
/**
* Processing data
* @param array $defaultvalues - default values
*/
public function data_preprocessing(&$defaultvalues) {
$defaultvalues['completionminutesenabled'] =
!empty($defaultvalues['completionminutes']) ? 1 : 0;
if (empty($defaultvalues['completionminutes'])) {
$defaultvalues['completionminutes'] = 1;
}
}
}