3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This file is part of the Database module for Moodle
21 * @copyright 2005 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../../config.php');
27 require_once('lib.php');
29 $id = optional_param('id', 0, PARAM_INT
); // course module id
30 $d = optional_param('d', 0, PARAM_INT
); // database id
31 $fid = optional_param('fid', 0 , PARAM_INT
); // update field id
32 $newtype = optional_param('newtype','',PARAM_ALPHA
); // type of the new field
33 $mode = optional_param('mode','',PARAM_ALPHA
);
34 $defaultsort = optional_param('defaultsort', 0, PARAM_INT
);
35 $defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT
);
36 $cancel = optional_param('cancel', 0, PARAM_BOOL
);
42 $url = new moodle_url('/mod/data/field.php');
44 $url->param('fid', $fid);
46 if ($newtype !== '') {
47 $url->param('newtype', $newtype);
50 $url->param('mode', $mode);
52 if ($defaultsort !== 0) {
53 $url->param('defaultsort', $defaultsort);
55 if ($defaultsortdir !== 0) {
56 $url->param('defaultsortdir', $defaultsortdir);
59 $url->param('cancel', $cancel);
63 $url->param('id', $id);
65 if (! $cm = get_coursemodule_from_id('data', $id)) {
66 print_error('invalidcoursemodule');
68 if (! $course = $DB->get_record('course', array('id'=>$cm->course
))) {
69 print_error('coursemisconf');
71 if (! $data = $DB->get_record('data', array('id'=>$cm->instance
))) {
72 print_error('invalidcoursemodule');
78 if (! $data = $DB->get_record('data', array('id'=>$d))) {
79 print_error('invalidid', 'data');
81 if (! $course = $DB->get_record('course', array('id'=>$data->course
))) {
82 print_error('invalidcoursemodule');
84 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
85 print_error('invalidcoursemodule');
89 require_login($course, true, $cm);
91 $context = context_module
::instance($cm->id
);
92 require_capability('mod/data:managetemplates', $context);
94 /************************************
96 ***********************************/
99 case 'add': ///add a new field
100 if (confirm_sesskey() and $fieldinput = data_submitted()){
102 //$fieldinput->name = data_clean_field_name($fieldinput->name);
104 /// Only store this new field if it doesn't already exist.
105 if (($fieldinput->name
== '') or data_fieldname_exists($fieldinput->name
, $data->id
)) {
107 $displaynoticebad = get_string('invalidfieldname','data');
111 /// Check for arrays and convert to a comma-delimited string
112 data_convert_arrays_to_strings($fieldinput);
114 /// Create a field object to collect and store the data safely
115 $type = required_param('type', PARAM_FILE
);
116 $field = data_get_field_new($type, $data);
118 $field->define_field($fieldinput);
119 $field->insert_field();
121 /// Update some templates
122 data_append_new_field_to_templates($data, $fieldinput->name
);
124 $displaynoticegood = get_string('fieldadded','data');
130 case 'update': ///update a field
131 if (confirm_sesskey() and $fieldinput = data_submitted()){
133 //$fieldinput->name = data_clean_field_name($fieldinput->name);
135 if (($fieldinput->name
== '') or data_fieldname_exists($fieldinput->name
, $data->id
, $fieldinput->fid
)) {
137 $displaynoticebad = get_string('invalidfieldname','data');
140 /// Check for arrays and convert to a comma-delimited string
141 data_convert_arrays_to_strings($fieldinput);
143 /// Create a field object to collect and store the data safely
144 $field = data_get_field_from_id($fid, $data);
145 $oldfieldname = $field->field
->name
;
147 $field->field
->name
= $fieldinput->name
;
148 $field->field
->description
= $fieldinput->description
;
149 $field->field
->required
= !empty($fieldinput->required
) ?
1 : 0;
151 for ($i=1; $i<=10; $i++
) {
152 if (isset($fieldinput->{'param'.$i})) {
153 $field->field
->{'param'.$i} = $fieldinput->{'param'.$i};
155 $field->field
->{'param'.$i} = '';
159 $field->update_field();
161 /// Update the templates.
162 data_replace_field_in_templates($data, $oldfieldname, $field->field
->name
);
164 $displaynoticegood = get_string('fieldupdated','data');
170 case 'delete': // Delete a field
171 if (confirm_sesskey()){
173 if ($confirm = optional_param('confirm', 0, PARAM_INT
)) {
176 // Delete the field completely
177 if ($field = data_get_field_from_id($fid, $data)) {
178 $field->delete_field();
180 // Update the templates.
181 data_replace_field_in_templates($data, $field->field
->name
, '');
183 // Update the default sort field
184 if ($fid == $data->defaultsort
) {
185 $rec = new stdClass();
186 $rec->id
= $data->id
;
187 $rec->defaultsort
= 0;
188 $rec->defaultsortdir
= 0;
189 $DB->update_record('data', $rec);
192 $displaynoticegood = get_string('fielddeleted', 'data');
197 data_print_header($course,$cm,$data, false);
199 // Print confirmation message.
200 $field = data_get_field_from_id($fid, $data);
202 echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field
->name
.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
203 'field.php?d='.$data->id
.'&mode=delete&fid='.$fid.'&confirm=1',
204 'field.php?d='.$data->id
);
206 echo $OUTPUT->footer();
213 case 'sort': // Set the default sort parameters
214 if (confirm_sesskey()) {
215 $rec = new stdClass();
216 $rec->id
= $data->id
;
217 $rec->defaultsort
= $defaultsort;
218 $rec->defaultsortdir
= $defaultsortdir;
220 $DB->update_record('data', $rec);
221 redirect($CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
, get_string('changessaved'), 2);
232 /// Print the browsing interface
234 ///get the list of possible fields (plugins)
235 $plugins = core_component
::get_plugin_list('datafield');
236 $menufield = array();
238 foreach ($plugins as $plugin=>$fulldir){
239 $menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin); //get from language files
241 asort($menufield); //sort in alphabetical order
242 $PAGE->set_title(get_string('course') . ': ' . $course->fullname
);
243 $PAGE->set_heading($course->fullname
);
245 $PAGE->set_pagetype('mod-data-field-' . $newtype);
246 if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field
247 data_print_header($course, $cm, $data,'fields');
249 $field = data_get_field_new($newtype, $data);
250 $field->display_edit_field();
252 } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
253 data_print_header($course, $cm, $data,'fields');
255 $field = data_get_field_from_id($fid, $data);
256 $field->display_edit_field();
258 } else { /// Display the main listing of all fields
259 data_print_header($course, $cm, $data,'fields');
261 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id
))) {
262 echo $OUTPUT->notification(get_string('nofieldindatabase','data')); // nothing in database
263 echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id
)); // link to presets
265 } else { //else print quiz style list of fields
267 $table = new html_table();
268 $table->head
= array(
269 get_string('fieldname', 'data'),
270 get_string('type', 'data'),
271 get_string('required', 'data'),
272 get_string('fielddescription', 'data'),
273 get_string('action', 'data'),
275 $table->align
= array('left','left','left', 'center');
276 $table->wrap
= array(false,false,false,false);
278 if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id
),'id')){
279 foreach ($fff as $ff) {
281 $field = data_get_field($ff, $data);
283 $baseurl = new moodle_url('/mod/data/field.php', array(
285 'fid' => $field->field
->id
,
286 'sesskey' => sesskey(),
289 $displayurl = new moodle_url($baseurl, array(
293 $deleteurl = new moodle_url($baseurl, array(
297 $table->data
[] = array(
298 html_writer
::link($displayurl, $field->field
->name
),
299 $field->image() . ' ' . $field->name(),
300 $field->field
->required ?
get_string('yes') : get_string('no'),
301 shorten_text($field->field
->description
, 30),
302 html_writer
::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) .
304 html_writer
::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))),
308 echo html_writer
::table($table);
312 echo '<div class="fieldadd">';
313 $popupurl = $CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
.'&mode=new&sesskey='. sesskey();
314 echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array('' => 'choosedots'),
315 'fieldform', array('label' => get_string('newfield', 'data')));
316 echo $OUTPUT->help_icon('newfield', 'data');
319 echo '<div class="sortdefault">';
320 echo '<form id="sortdefault" action="'.$CFG->wwwroot
.'/mod/data/field.php" method="get">';
322 echo '<input type="hidden" name="d" value="'.$data->id
.'" />';
323 echo '<input type="hidden" name="mode" value="sort" />';
324 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
325 echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
326 echo '<select id="defaultsort" name="defaultsort" class="custom-select">';
327 if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id
))) {
328 echo '<optgroup label="'.get_string('fields', 'data').'">';
329 foreach ($fields as $field) {
330 if ($data->defaultsort
== $field->id
) {
331 echo '<option value="'.$field->id
.'" selected="selected">'.$field->name
.'</option>';
333 echo '<option value="'.$field->id
.'">'.$field->name
.'</option>';
339 $options[DATA_TIMEADDED
] = get_string('timeadded', 'data');
340 // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
341 /* $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
342 $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
343 $options[DATA_LASTNAME] = get_string('authorlastname', 'data');
344 if ($data->approval and has_capability('mod/data:approve', $context)) {
345 $options[DATA_APPROVED] = get_string('approved', 'data');
347 echo '<optgroup label="'.get_string('other', 'data').'">';
348 foreach ($options as $key => $name) {
349 if ($data->defaultsort
== $key) {
350 echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
352 echo '<option value="'.$key.'">'.$name.'</option>';
358 $options = array(0 => get_string('ascending', 'data'),
359 1 => get_string('descending', 'data'));
360 echo html_writer
::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
361 echo html_writer
::select($options, 'defaultsortdir', $data->defaultsortdir
, false, array('class' => 'custom-select'));
362 echo '<input type="submit" class="btn btn-secondary m-l-1" value="'.get_string('save', 'data').'" />';
370 echo $OUTPUT->footer();