-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.php
372 lines (296 loc) · 13.6 KB
/
field.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?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/>.
/**
* This file is part of the Database module for Moodle
*
* @copyright 2005 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package mod_data
*/
require_once('../../config.php');
require_once('lib.php');
$id = optional_param('id', 0, PARAM_INT); // course module id
$d = optional_param('d', 0, PARAM_INT); // database id
$fid = optional_param('fid', 0 , PARAM_INT); // update field id
$newtype = optional_param('newtype','',PARAM_ALPHA); // type of the new field
$mode = optional_param('mode','',PARAM_ALPHA);
$defaultsort = optional_param('defaultsort', 0, PARAM_INT);
$defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT);
$cancel = optional_param('cancel', 0, PARAM_BOOL);
if ($cancel) {
$mode = 'list';
}
$url = new moodle_url('/mod/data/field.php');
if ($fid !== 0) {
$url->param('fid', $fid);
}
if ($newtype !== '') {
$url->param('newtype', $newtype);
}
if ($mode !== '') {
$url->param('mode', $mode);
}
if ($defaultsort !== 0) {
$url->param('defaultsort', $defaultsort);
}
if ($defaultsortdir !== 0) {
$url->param('defaultsortdir', $defaultsortdir);
}
if ($cancel !== 0) {
$url->param('cancel', $cancel);
}
if ($id) {
$url->param('id', $id);
$PAGE->set_url($url);
if (! $cm = get_coursemodule_from_id('data', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
print_error('coursemisconf');
}
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
print_error('invalidcoursemodule');
}
} else {
$url->param('d', $d);
$PAGE->set_url($url);
if (! $data = $DB->get_record('data', array('id'=>$d))) {
print_error('invalidid', 'data');
}
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('invalidcoursemodule');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
print_error('invalidcoursemodule');
}
}
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/data:managetemplates', $context);
/************************************
* Data Processing *
***********************************/
switch ($mode) {
case 'add': ///add a new field
if (confirm_sesskey() and $fieldinput = data_submitted()){
//$fieldinput->name = data_clean_field_name($fieldinput->name);
/// Only store this new field if it doesn't already exist.
if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id)) {
$displaynoticebad = get_string('invalidfieldname','data');
} else {
/// Check for arrays and convert to a comma-delimited string
data_convert_arrays_to_strings($fieldinput);
/// Create a field object to collect and store the data safely
$type = required_param('type', PARAM_FILE);
$field = data_get_field_new($type, $data);
$field->define_field($fieldinput);
$field->insert_field();
/// Update some templates
data_append_new_field_to_templates($data, $fieldinput->name);
$displaynoticegood = get_string('fieldadded','data');
}
}
break;
case 'update': ///update a field
if (confirm_sesskey() and $fieldinput = data_submitted()){
//$fieldinput->name = data_clean_field_name($fieldinput->name);
if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id, $fieldinput->fid)) {
$displaynoticebad = get_string('invalidfieldname','data');
} else {
/// Check for arrays and convert to a comma-delimited string
data_convert_arrays_to_strings($fieldinput);
/// Create a field object to collect and store the data safely
$field = data_get_field_from_id($fid, $data);
$oldfieldname = $field->field->name;
$field->field->name = $fieldinput->name;
$field->field->description = $fieldinput->description;
$field->field->required = !empty($fieldinput->required) ? 1 : 0;
for ($i=1; $i<=10; $i++) {
if (isset($fieldinput->{'param'.$i})) {
$field->field->{'param'.$i} = $fieldinput->{'param'.$i};
} else {
$field->field->{'param'.$i} = '';
}
}
$field->update_field();
/// Update the templates.
data_replace_field_in_templates($data, $oldfieldname, $field->field->name);
$displaynoticegood = get_string('fieldupdated','data');
}
}
break;
case 'delete': // Delete a field
if (confirm_sesskey()){
if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
// Delete the field completely
if ($field = data_get_field_from_id($fid, $data)) {
$field->delete_field();
// Update the templates.
data_replace_field_in_templates($data, $field->field->name, '');
// Update the default sort field
if ($fid == $data->defaultsort) {
$rec = new stdClass();
$rec->id = $data->id;
$rec->defaultsort = 0;
$rec->defaultsortdir = 0;
$DB->update_record('data', $rec);
}
$displaynoticegood = get_string('fielddeleted', 'data');
}
} else {
data_print_header($course,$cm,$data, false);
// Print confirmation message.
$field = data_get_field_from_id($fid, $data);
echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
'field.php?d='.$data->id);
echo $OUTPUT->footer();
exit;
}
}
break;
case 'sort': // Set the default sort parameters
if (confirm_sesskey()) {
$rec = new stdClass();
$rec->id = $data->id;
$rec->defaultsort = $defaultsort;
$rec->defaultsortdir = $defaultsortdir;
$DB->update_record('data', $rec);
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
exit;
}
break;
default:
break;
}
/// Print the browsing interface
///get the list of possible fields (plugins)
$plugins = core_component::get_plugin_list('datafield');
$menufield = array();
foreach ($plugins as $plugin=>$fulldir){
$menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin); //get from language files
}
asort($menufield); //sort in alphabetical order
$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
$PAGE->set_heading($course->fullname);
$PAGE->force_settings_menu(true);
$PAGE->set_pagetype('mod-data-field-' . $newtype);
if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field
data_print_header($course, $cm, $data,'fields');
$field = data_get_field_new($newtype, $data);
$field->display_edit_field();
} else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
data_print_header($course, $cm, $data,'fields');
$field = data_get_field_from_id($fid, $data);
$field->display_edit_field();
} else { /// Display the main listing of all fields
data_print_header($course, $cm, $data,'fields');
if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {
echo $OUTPUT->notification(get_string('nofieldindatabase','data')); // nothing in database
echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id)); // link to presets
} else { //else print quiz style list of fields
$table = new html_table();
$table->head = array(
get_string('fieldname', 'data'),
get_string('type', 'data'),
get_string('required', 'data'),
get_string('fielddescription', 'data'),
get_string('action', 'data'),
);
$table->align = array('left','left','left', 'center');
$table->wrap = array(false,false,false,false);
if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
foreach ($fff as $ff) {
$field = data_get_field($ff, $data);
$baseurl = new moodle_url('/mod/data/field.php', array(
'd' => $data->id,
'fid' => $field->field->id,
'sesskey' => sesskey(),
));
$displayurl = new moodle_url($baseurl, array(
'mode' => 'display',
));
$deleteurl = new moodle_url($baseurl, array(
'mode' => 'delete',
));
$table->data[] = array(
html_writer::link($displayurl, $field->field->name),
$field->image() . ' ' . $field->name(),
$field->field->required ? get_string('yes') : get_string('no'),
shorten_text($field->field->description, 30),
html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) .
' ' .
html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))),
);
}
}
echo html_writer::table($table);
}
echo '<div class="fieldadd">';
$popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='. sesskey();
echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array('' => 'choosedots'),
'fieldform', array('label' => get_string('newfield', 'data')));
echo $OUTPUT->help_icon('newfield', 'data');
echo '</div>';
echo '<div class="sortdefault">';
echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
echo '<div>';
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
echo '<input type="hidden" name="mode" value="sort" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
echo '<select id="defaultsort" name="defaultsort" class="custom-select">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
echo '<optgroup label="'.get_string('fields', 'data').'">';
foreach ($fields as $field) {
if ($data->defaultsort == $field->id) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
}
echo '</optgroup>';
}
$options = array();
$options[DATA_TIMEADDED] = get_string('timeadded', 'data');
// TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
/* $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
$options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
$options[DATA_LASTNAME] = get_string('authorlastname', 'data');
if ($data->approval and has_capability('mod/data:approve', $context)) {
$options[DATA_APPROVED] = get_string('approved', 'data');
}*/
echo '<optgroup label="'.get_string('other', 'data').'">';
foreach ($options as $key => $name) {
if ($data->defaultsort == $key) {
echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
} else {
echo '<option value="'.$key.'">'.$name.'</option>';
}
}
echo '</optgroup>';
echo '</select>';
$options = array(0 => get_string('ascending', 'data'),
1 => get_string('descending', 'data'));
echo html_writer::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false, array('class' => 'custom-select'));
echo '<input type="submit" class="btn btn-secondary m-l-1" value="'.get_string('save', 'data').'" />';
echo '</div>';
echo '</form>';
echo '</div>';
}
/// Finish the page
echo $OUTPUT->footer();