-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparameter_form.class.php
242 lines (219 loc) · 10.1 KB
/
parameter_form.class.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
<?php
/**
* ELIS(TM): Enterprise Learning Intelligence Suite
* Copyright (C) 2008-2014 Remote-Learner.net Inc (http://www.remote-learner.net)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package local_elisreports
* @author Remote-Learner.net Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2008-2014 Remote-Learner.net Inc (http://www.remote-learner.net)
*
*/
defined('MOODLE_INTERNAL') || die();
//needed for moodleform definition
require_once($CFG->libdir . '/formslib.php');
class parameter_form extends moodleform {
//stores the identifiers of elements added so far to prevent
//double-adding elements if they appear in multiple filterings
var $uniqueids = array();
/**
* Standard form definition
*/
public function definition() {
$mform =& $this->_form;
$mform->addElement('header', 'newfilter', get_string('newfilter','filters'));
// Just define the id field - UI fields added in definition_after_data
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_TEXT);
}
/**
* Adds conditional fields to the form
*/
function definition_after_data() {
global $SESSION;
$mform =& $this->_form;
//used to persist the display of the cancel button when re-displaying
$mform->addElement('hidden', 'showcancel');
$mform->setType('showcancel', PARAM_INT);
$reset_js = '';
if (isset($this->_customdata['filterobject'])) {
//filter object was passed, because the report uses filters
$filter_object = $this->_customdata['filterobject'];
$reset_js = $this->get_reset_js($filter_object);
//error_log("parameter_form::reset_js => {$reset_js}");
//handle adding of UI fields for secondary filterings
if (!empty($filter_object->secondary_filterings)) {
foreach ($filter_object->secondary_filterings as $key => $filtering) {
$this->add_filtering_elements($filtering);
}
}
//add all filter form elements to this form for the main filtering
$this->add_filtering_elements($filter_object);
//add any required field rules
foreach ($filter_object->_fields as $key=>$fields) {
if (!empty($fields->_isrequired)) {
$required_rule_method = $filter_object->reportname . '_report::apply_filter_required_rule';
if (is_callable($required_rule_method)) {
// We have a custom requirement rule so let's use it
$mform = call_user_func_array($required_rule_method, array($mform,$key,$fields));
} elseif ($mform->elementExists($key)) {
// Basic requirement rule
$mform->addRule($key, get_string('required'), 'required', null, 'client');
}
}
}
//add the necessary buttons
$elements = array();
$elements[] = $mform->createElement('submit', 'reset_form', get_string('button_reset_form', 'local_elisreports'));
//remove "Save Values as Defaults" until we clean up some parameter-related functionality (ELIS-2733)
//$elements[] = $mform->createElement('submit', 'save_defaults', get_string('button_save_defaults', 'local_elisreports'));
//determine whether to display the cancel button (not shown on first view of this form)
if (!empty($this->_customdata['showcancel'])) {
$elements[] = $mform->createElement('cancel', 'canceltest', get_string('button_cancel', 'local_elisreports'));
}
$elements[] = $mform->createElement('submit', 'show_report', get_string('button_show_report', 'local_elisreports'));
$mform->addGroup($elements, 'buttonar', '', array(' '), false);
} else {
//report does not use filters
$mform->addElement('static', 'noparams', '', get_string('label_no_parameters', 'local_elisreports'));
$mform->addElement('submit', 'show_report', get_string('button_show_report', 'local_elisreports'));
}
$mform->addElement('html',"
<script type=\"text/javascript\">
//<![CDATA[
var submitname = null;
if ((reportdiv = document.getElementById('php_report_block')) &&
(reportforms = reportdiv.getElementsByTagName('form')) )
{
buttons = document.getElementsByTagName('input');
for (i = 0; i < buttons.length; ++i) {
if (buttons[i].type == 'submit') {
if (buttons[i].name == 'mform_showadvanced') {
//alert('Found Hide/Show Advanced button ... adding onclick!');
buttons[i].onclick = function () {
window.submitname = 'mform_showadvanced';
return true;
};
} else if (buttons[i].name == 'reset_form') {
//alert('parameter_form::reset_form submit input ... {$reset_js}');
buttons[i].onclick = function () {
{$reset_js}
if (window.customfieldpickerinstance) {
window.customfieldpickerinstance.hide();
}
window.submitname = '';
return true;
};
} else {
//alert('Found non-advanced, non-reset button ... adding onclick!');
buttons[i].onclick = function () {
if (window.customfieldpickerinstance) {
window.customfieldpickerinstance.hide();
}
window.submitname = '';
return true;
};
}
}
}
}
//]]>
</script>
");
}
/**
* Adds UI fields to this form based on the fields as defined in the
* provided filtering
*
* @param php_report_default_capable_filtering $filter_object The filtering definition
*/
protected function add_filtering_elements($filter_object) {
$mform =& $this->_form;
//iterate through the defined fields
if (!empty($filter_object->_fields)) {
foreach ($filter_object->_fields as $field) {
//add fields this form if they do not already exist
if (!in_array($field->_uniqueid, $this->uniqueids)) {
$field->setupForm($mform);
$this->uniqueids[] = $field->_uniqueid;
}
}
}
}
/**
* Forces particular values to be set on the form regardless
* of defaults and submit values
*
* @param object $constant_values The values to set
*/
function set_constants($constant_values) {
$this->_form->setConstants($constant_values);
}
/**
* Get any reset javascript code required by filters
* @param object $filterobj The php_report_default_capable_filtering object
* @return string The combined javascript code
*/
function get_reset_js($filterobj) {
$reset_js = '';
foreach ($filterobj->_fields as $filter) {
$reset_js .= $filter->reset_js();
}
return $reset_js;
}
/**
* Load in existing data as form defaults ...
* Overloads /lib/formslib.php::moodleform::set_data() to convert certain fields to group fields
*
* @param stdClass|array $values object or array of default values
*/
public function set_data($values) {
if (isset($this->_customdata['filterobject'])) {
// filter object was passed, because the report uses filters
$filterobject = $this->_customdata['filterobject'];
if (isset($filterobject->_fields)) {
if (is_object($values)) {
$values = (array)$values;
}
foreach ($values as $field => $val) {
if (isset($filterobject->_fields[$field])) {
$fieldfilter = $filterobject->_fields[$field];
// Convert array fields to serialized as required
if (is_array($val)) {
$fixed = false;
// Check if filter default or options are arrays
if (isset($fieldfilter->_default)) {
if (is_array($fieldfilter->_default)) {
$values[$field] = serialize($val);
$fixed = true;
}
}
if (!$fixed && isset($fieldfilter->_options) && is_array($fieldfilter->_options)) {
foreach ($fieldfilter->_options as $optkey => $optval) {
if (is_array($optval)) {
$values[$field] = serialize($val);
break;
}
}
}
}
}
}
}
}
parent::set_data($values);
}
}