forked from learnweb/moodle-mod_ratingallocate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_form.php
262 lines (232 loc) · 11.5 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?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 ratingallocate 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_ratingallocate
* @copyright 2014 T Reischmann, C Usener
* @copyright based on code by M Schulze copyright (C) 2014 M Schulze
* @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(dirname(__FILE__) . '/locallib.php');
/**
* Module instance settings form
*/
class mod_ratingallocate_mod_form extends moodleform_mod {
const MOD_NAME = 'ratingallocate';
const CHOICE_PLACEHOLDER_IDENTIFIER = 'placeholder_for_choices';
const STRATEGY_OPTIONS = 'strategyopt';
const STRATEGY_OPTIONS_PLACEHOLDER = 'placeholder_strategyopt';
private $newchoicecounter = 0;
private $msgerrorrequired;
/**
* constructor
* @see moodleform_mod::moodleform_mod
*/
public function __construct($current, $section, $cm, $course) {
// Pre parse mod data if exists (in case not new).
if ($current && property_exists($current, 'setting')) {
$strategyoptions = json_decode($current->setting, true);
foreach ($strategyoptions as $stratkey => $strategy) {
foreach ($strategy as $key => $option) {
$current->{$this->get_settingsfield_identifier($stratkey, $key)} = $option;
}
}
}
parent::__construct($current, $section, $cm, $course);
$this->msgerrorrequired = get_string('err_required', 'form');
}
/**
* Defines forms elements
*/
public function definition() {
global $CFG, $PAGE;
$mform = $this->_form;
// Adding the "general" fieldset, where all the common settings are showed.
$mform->addElement('header', 'general', get_string('general', 'form'));
// Adding the standard "name" field.
$mform->addElement('text', 'name', get_string('ratingallocatename', self::MOD_NAME), array(
'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', 'ratingallocatename', self::MOD_NAME);
// Adding the standard "intro" and "introformat" fields.
$this->standard_intro_elements();
// -------------------------------------------------------------------------------
$elementname = 'strategy';
// Define options for select.
$selectoptions = array();
foreach (\strategymanager::get_strategies() as $strategy) {
$selectoptions[$strategy] = get_string($strategy . '_name', self::MOD_NAME);
}
$mform->addElement('select', $elementname, get_string('select_strategy', self::MOD_NAME), $selectoptions);
$mform->addHelpButton($elementname, 'select_strategy', self::MOD_NAME);
$mform->addRule('strategy', null, 'required', null, 'client');
// Start/end time.
$elementname = 'accesstimestart';
$mform->addElement('date_time_selector', $elementname, get_string('rating_begintime', self::MOD_NAME));
$mform->setDefault($elementname, time() + 24 * 60 * 60);
$elementname = 'accesstimestop';
$mform->addElement('date_time_selector', $elementname, get_string('rating_endtime', self::MOD_NAME));
$mform->setDefault($elementname, time() + 7 * 24 * 60 * 60); // Default: now + one week.
$elementname = 'publishdate';
$mform->addElement('date_time_selector', $elementname, get_string($elementname, self::MOD_NAME),
array('optional' => true));
$mform->setDefault($elementname, time() + 9 * 24 * 60 * 60);
$elementname = 'runalgorithmbycron';
$mform->addElement('advcheckbox', $elementname, get_string($elementname, self::MOD_NAME), null, null, array(0, 1));
$mform->addHelpButton($elementname, $elementname, self::MOD_NAME);
$mform->setDefault($elementname, 1);
$headerid = 'strategy_fieldset';
$mform->addElement('header', $headerid, get_string('strategyspecificoptions', ratingallocate_MOD_NAME));
$mform->setExpanded($headerid);
foreach (\strategymanager::get_strategies() as $strategy) {
// Load strategy class.
$strategyclassp = 'ratingallocate\\' . $strategy . '\\strategy';
/* @var $strategyclass \strategytemplate */
$strategyclass = new $strategyclassp();
// Add options fields.
foreach ($strategyclass->get_static_settingfields() as $key => $value) {
$fieldid = $this->get_settingsfield_identifier($strategy, $key);
$this->add_settings_field($fieldid, $value, $strategy, $mform);
}
$mform->addElement('static', self::STRATEGY_OPTIONS_PLACEHOLDER.'[' . $strategy . ']', '', '');
}
// Add standard elements, common to all modules.
$this->standard_coursemodule_elements();
// Add standard buttons, common to all modules.
$this->add_action_buttons();
}
/**
* Add an settings element to the form. It is enabled only if the strategy it belongs to is selected.
* @param string $stratfieldid id of the element to be added
* @param array $value array with the element type and its caption
* (usually returned by the strategys get settingsfields methods).
* @param string $strategyid id of the strategy it belongs to.
* @param $mform MoodleQuickForm form object the settings field should be added to.
*/
private function add_settings_field($stratfieldid, array $value, $strategyid, MoodleQuickForm $mform) {
$attributes = array('size' => '20');
if ($value[0] != "select" && isset($value[3])) {
$attributes['placeholder'] = ($value[3]);
}
if ($value[0] == "text") {
$mform->addElement('text', $stratfieldid, $value[1], $attributes);
$mform->setType($stratfieldid, PARAM_TEXT);
} else if ($value[0] == "int") {
$mform->addElement('text', $stratfieldid, $value[1], $attributes);
$mform->setType($stratfieldid, PARAM_TEXT);
$mform->addRule($stratfieldid, null, 'numeric'); // TODO: Only validate if not disabled.
} else if ($value[0] == "select") {
$mform->addElement('select', $stratfieldid, $value[1], $value[3], $attributes);
}
if (isset($value[2])) {
$mform->setDefault($stratfieldid, $value[2]);
}
if (isset($value[4])) {
$mform->addHelpButton($stratfieldid, $value[4], self::MOD_NAME);
}
$mform->hideIf($stratfieldid, 'strategy', 'neq', $strategyid);
}
// Override if you need to setup the form depending on current values.
public function definition_after_data() {
parent::definition_after_data();
$mform = & $this->_form;
$data = $this->current;
if ($this->is_submitted()) {
$subdata = $this->get_submitted_data();
$allstrategyoptions = $subdata->{self::STRATEGY_OPTIONS};
} else if (isset($data->setting)) {
$allstrategyoptions = json_decode($data->setting, true);
}
// Add dynamic settings fields.
foreach (\strategymanager::get_strategies() as $strategy) {
// Load strategy class.
$strategyclassp = 'ratingallocate\\' . $strategy . '\\strategy';
/* @var $strategyclass \strategytemplate */
if (isset($allstrategyoptions) && array_key_exists($strategy, $allstrategyoptions)) {
$strategyclass = new $strategyclassp($allstrategyoptions[$strategy]);
} else {
$strategyclass = new $strategyclassp();
}
$strategyplaceholder = self::STRATEGY_OPTIONS_PLACEHOLDER . '[' . $strategy . ']';
// Add options fields.
$dynamicsettingsfields = $strategyclass->get_dynamic_settingfields();
foreach ($dynamicsettingsfields as $key => $value) {
$fieldid = $this->get_settingsfield_identifier($strategy, $key);
$this->add_settings_field($fieldid, $value, $strategy, $mform);
$mform->insertElementBefore($mform->removeElement($fieldid, false),
$strategyplaceholder);
}
// If any dynamic field is present, add a no submit button to refresh the page.
if (count($dynamicsettingsfields) > 0) {
$buttonname = self::STRATEGY_OPTIONS.$strategy.'refresh';
$mform->registerNoSubmitButton($buttonname);
$mform->addElement('submit', $buttonname, get_string('refresh'));
$mform->insertElementBefore($mform->removeElement($buttonname, false),
$strategyplaceholder);
$mform->hideIf($buttonname, 'strategy', 'neq', $strategy);
}
$mform->removeElement($strategyplaceholder);
}
}
/**
* Checks that accesstimestart is before accesstimestop
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['accesstimestop'] <= $data['accesstimestart']) {
$errors['accesstimestart'] = get_string('invalid_dates', self::MOD_NAME);
}
if ($data['publishdate'] && $data['publishdate'] <= $data['accesstimestart']) {
$errors['publishdate'] = get_string('invalid_publishdate', self::MOD_NAME);
}
// User has to select one strategy.
if (empty($data['strategy'])) {
$errors['strategy'] = get_string('strategy_not_specified', self::MOD_NAME);
} else {
$strategyclassp = 'ratingallocate\\' . $data['strategy'] . '\\strategy';
if (array_key_exists($data['strategy'], $data['strategyopt'])) {
$strategyclass = new $strategyclassp($data['strategyopt'][$data['strategy']]);
$settingerrors = $strategyclass->validate_settings();
foreach ($settingerrors as $id => $error) {
$errors[$this->get_settingsfield_identifier($data['strategy'], $id)] = $error;
}
}
}
return $errors;
}
/**
* Returns a valid identifier for a settings field
* @param $strategy identifier of the strategy
* @param $key identifier of the key
* @return string
*/
private function get_settingsfield_identifier($strategy, $key) {
return self::STRATEGY_OPTIONS . '[' . $strategy . '][' . $key . ']';
}
}