-
Notifications
You must be signed in to change notification settings - Fork 5
/
mod_form.php
195 lines (169 loc) · 8.12 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
<?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/>.
/**
* GoToWebinar module form
*
* @package mod_gotowebinar
* @copyright 2017 Alok Kumar Rai <alokr.mail@gmail.com,alokkumarrai@outlook.in>
* @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');
require_once($CFG->dirroot . '/mod/gotowebinar/locallib.php');
/**
* GoToWebinar module form
*
* @package mod_gotowebinar
* @copyright 2017 Alok Kumar Rai <alokr.mail@gmail.com,alokkumarrai@outlook.in>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_gotowebinar_mod_form extends moodleform_mod {
/**
* Form definition
* @throws moodle_exception
*/
public function definition() {
$mform = $this->_form;
$licences = $this->get_gotowebinar_licence();
if (!$licences) {
$link = new moodle_url('/admin/settings.php?section=modsettinggotowebinar');
throw new moodle_exception('incompletesetup', 'gotowebinar', $link);
}
$gotowebinarconfig = get_config('gotowebinar');
$mform->addElement('header', 'general', get_string('generalsetting', 'gotowebinar'));
// Adding a text element.
$mform->addElement('text', 'name', get_string('meetingname', 'gotowebinar'));
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', get_string('meetingnamerequired', 'gotowebinar'), 'required', '', 'server');
$mform->addElement('select', 'gotowebinar_licence', get_string('licence', 'gotowebinar'), $licences);
if (isset($this->get_current()->update)) {
$mform->disabledIf('gotowebinar_licence', null);
} else {
$mform->addRule('gotowebinar_licence', get_string('licencerequired', 'gotowebinar'), 'required', '', 'client');
}
$this->standard_intro_elements();
$mform->addElement('header', 'meetingheader', get_string('meetingheader', 'gotowebinar'));
$mform->addElement('date_time_selector', 'startdatetime', get_string('startdatetime', 'gotowebinar'));
$mform->setDefault('startdatetime', time() + 300);
$mform->addRule('startdatetime', 'Occurs required', 'required', 'client');
$mform->addElement('date_time_selector', 'enddatetime', get_string('enddatetime', 'gotowebinar'));
$mform->setDefault('enddatetime', time() + 3900);
$mform->addRule('enddatetime', 'Occurs required', 'required', 'client');
$mform->addElement('selectyesno', 'confirmationemail', get_string('confirmationemail', 'gotowebinar'));
$mform->addElement('selectyesno', 'reminderemail', get_string('reminderemail', 'gotowebinar'));
$mform->addElement('selectyesno', 'absenteefollowupemail', get_string('absenteefollowupemail', 'gotowebinar'));
$mform->addElement('selectyesno', 'attendeefollowupemail', get_string('attendeefollowupemail', 'gotowebinar'));
$mform->addElement('selectyesno', 'sendcancellationemails', get_string('sendcancellationemails', 'gotowebinar'));
// Adding hidden items.
$mform->addElement('hidden', 'meetingpublic', 1);
$mform->setType('meetingpublic', PARAM_INT);
$this->standard_coursemodule_elements();
$this->add_action_buttons(true, false, null);
}
/**
* 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 = [];
$group[] = & $mform->createElement('checkbox', 'completionparticipationenabled', '',
get_string('completiongotowebinar', 'gotowebinar'));
$group[] = & $mform->createElement('text', 'completionparticipation', '', ['size' => 3, 'value' => 50]);
$mform->setType('completionparticipation', PARAM_INT);
$mform->addGroup($group, 'completiongotowebinargroup', get_string('completiongotowebinargroup', 'gotowebinar'),
[' '], false);
$mform->addHelpButton('completiongotowebinargroup', 'completiongotowebinargroup', 'gotowebinar');
$mform->disabledIf('completiongotowebinargroup', 'completionparticipationenabled', 'notchecked');
return ['completiongotowebinargroup'];
}
/**
* Completion rules need to evaluated.
* @param array $data
* @return boolean
*/
public function completion_rule_enabled($data) {
return (!empty($data['completionparticipationenabled']) && $data['completionparticipation'] != 0);
}
/**
* Validation method of the module creation form.
* @param array $data
* @param array $files
* @return array $$errors
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['startdatetime'] < time()) {
$errors['startdatetime'] = 'Start date time must be a future time';
}
if ($data['enddatetime'] < time()) {
$errors['enddatetime'] = 'End date time must be future time';
}
if ($data['startdatetime'] >= $data['enddatetime']) {
$errors['enddatetime'] = 'End date time should be more that Start date time';
}
$course = get_course($data['course']);
if ($course->format == 'weeks') {
$dates = course_get_format($course)->get_section_dates($this->current->section);
if (($data['startdatetime'] < $dates->start) || ($data['startdatetime'] > $dates->end)) {
$errors['startdatetime'] = "Start date must be in the range of the course week";
}
if (($data['enddatetime'] < $dates->start) && ($data['enddatetime'] < $dates->end)) {
$errors['enddatetime'] = "Start date must be in the range of the course week";
}
}
if (!empty($data['completionunlocked']) && (!empty($data['completionparticipationenabled']))) {
// Turn off completion settings if the checkboxes aren't ticked.
$autocompletion = !empty($data['completion']) && $data['completion'] == COMPLETION_TRACKING_AUTOMATIC;
if ($autocompletion && ($data['completionparticipation'] > 100 || $data['completionparticipation'] <= 0)) {
$errors['completiongotowebinargroup'] = 'Please enter a valid percentage value between 1 and 100';
}
}
return $errors;
}
/**
* Get Data of completion.
* @return array $data
*/
public function get_data() {
$data = parent::get_data();
if (!$data) {
return $data;
}
if (!empty($data->completionunlocked)) {
// Turn off completion settings if the checkboxes aren't ticked.
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completionparticipationenabled) || !$autocompletion) {
$data->completionparticipation = 0;
}
}
return $data;
}
/**
* Get GoToWebinar license
* @return array $licences
*/
private function get_gotowebinar_licence() {
global $DB;
$licences = [];
$gotomeetinglicences = $DB->get_records('gotowebinar_licence', ['active' => 1], 'email');
foreach ($gotomeetinglicences as $gotomeetinglicence) {
$licences[$gotomeetinglicence->id] = $gotomeetinglicence->email;
}
return $licences;
}
}