2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once('../../config.php');
26 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', '');
44 if (! $cm = get_coursemodule_from_id('data', $id)) {
45 error('Course Module ID was incorrect');
47 if (! $course = get_record('course', 'id', $cm->course
)) {
48 error('Course is misconfigured');
50 if (! $data = get_record('data', 'id', $cm->instance
)) {
51 error('Course module is incorrect');
55 if (! $data = get_record('data', 'id', $d)) {
56 error('Data ID is incorrect');
58 if (! $course = get_record('course', 'id', $data->course
)) {
59 error('Course is misconfigured');
61 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
62 error('Course Module ID was incorrect');
66 require_login($course->id
, true, $cm);
68 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
69 require_capability('mod/data:managetemplates', $context);
71 /************************************
73 ***********************************/
76 case 'add': ///add a new field
77 if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot
.'/mod/data/field.php')){
79 //$fieldinput->name = data_clean_field_name($fieldinput->name);
81 /// Only store this new field if it doesn't already exist.
82 if (($fieldinput->name
== '') or data_fieldname_exists($fieldinput->name
, $data->id
)) {
84 $displaynoticebad = get_string('invalidfieldname','data');
88 /// Check for arrays and convert to a comma-delimited string
89 data_convert_arrays_to_strings($fieldinput);
91 /// Create a field object to collect and store the data safely
92 $type = required_param('type', PARAM_FILE
);
93 $field = data_get_field_new($type, $data);
95 $field->define_field($fieldinput);
96 $field->insert_field();
98 /// Update some templates
99 data_append_new_field_to_templates($data, $field->field
->name
);
101 add_to_log($course->id
, 'data', 'fields add',
102 "field.php?d=$data->id&mode=display&fid=$fid", $fid, $cm->id
);
104 $displaynoticegood = get_string('fieldadded','data');
110 case 'update': ///update a field
111 if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot
.'/mod/data/field.php')){
113 //$fieldinput->name = data_clean_field_name($fieldinput->name);
115 if (($fieldinput->name
== '') or data_fieldname_exists($fieldinput->name
, $data->id
, $fieldinput->fid
)) {
117 $displaynoticebad = get_string('invalidfieldname','data');
120 /// Check for arrays and convert to a comma-delimited string
121 data_convert_arrays_to_strings($fieldinput);
123 /// Create a field object to collect and store the data safely
124 $field = data_get_field_from_id($fid, $data);
125 $oldfieldname = $field->field
->name
;
127 $field->field
->name
= $fieldinput->name
;
128 $field->field
->description
= $fieldinput->description
;
130 for ($i=1; $i<=10; $i++
) {
131 if (isset($fieldinput->{'param'.$i})) {
132 $field->field
->{'param'.$i} = $fieldinput->{'param'.$i};
134 $field->field
->{'param'.$i} = '';
138 $field->update_field();
140 /// Update the templates.
141 data_replace_field_in_templates($data, $oldfieldname, $field->field
->name
);
143 add_to_log($course->id
, 'data', 'fields update',
144 "field.php?d=$data->id&mode=display&fid=$fid", $fid, $cm->id
);
146 $displaynoticegood = get_string('fieldupdated','data');
152 case 'delete': // Delete a field
153 if (confirm_sesskey()){
155 if ($confirm = optional_param('confirm', 0, PARAM_INT
)) {
158 // Delete the field completely
159 if ($field = data_get_field_from_id($fid, $data)) {
160 $field->delete_field();
162 // Update the templates.
163 data_replace_field_in_templates($data, $field->field
->name
, '');
165 // Update the default sort field
166 if ($fid == $data->defaultsort
) {
168 $rec->id
= $data->id
;
169 $rec->defaultsort
= 0;
170 $rec->defaultsortdir
= 0;
171 if (!update_record('data', $rec)) {
172 error('There was an error updating the database');
176 add_to_log($course->id
, 'data', 'fields delete',
177 "field.php?d=$data->id", $field->field
->name
, $cm->id
);
179 $displaynoticegood = get_string('fielddeleted', 'data');
184 data_print_header($course,$cm,$data, false);
186 // Print confirmation message.
187 $field = data_get_field_from_id($fid, $data);
189 notice_yesno('<strong>'.$field->name().': '.$field->field
->name
.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
190 'field.php?d='.$data->id
.'&mode=delete&fid='.$fid.'&sesskey='.sesskey().'&confirm=1',
191 'field.php?d='.$data->id
);
193 print_footer($course);
200 case 'sort': // Set the default sort parameters
201 if (confirm_sesskey()) {
202 $rec->id
= $data->id
;
203 $rec->defaultsort
= $defaultsort;
204 $rec->defaultsortdir
= $defaultsortdir;
206 if (update_record('data', $rec)) {
207 redirect($CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
, get_string('changessaved'), 2);
209 error('There was an error updating the database');
221 /// Print the browsing interface
223 ///get the list of possible fields (plugins)
224 $directories = get_list_of_plugins('mod/data/field/');
225 $menufield = array();
227 foreach ($directories as $directory){
228 $menufield[$directory] = get_string($directory,'data'); //get from language files
230 asort($menufield); //sort in alphabetical order
233 if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field
234 $CFG->pagepath
='mod/data/field/'.$newtype;
235 data_print_header($course,$cm,$data,'fields');
237 $field = data_get_field_new($newtype, $data);
238 $field->display_edit_field();
240 } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
241 $CFG->pagepath
='mod/data/field/'.$newtype;
242 data_print_header($course,$cm,$data,'fields');
244 $field = data_get_field_from_id($fid, $data);
245 $field->display_edit_field();
247 } else { /// Display the main listing of all fields
249 $CFG->pagepath
='mod/data/field/'.$newtype;
250 data_print_header($course,$cm,$data,'fields');
253 if (!record_exists('data_fields','dataid',$data->id
)) {
254 notify(get_string('nofieldindatabase','data')); // nothing in database
255 notify(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id
)); // link to presets
257 } else { //else print quiz style list of fields
259 $table->head
= array(get_string('fieldname','data'), get_string('type','data'), get_string('fielddescription', 'data'), get_string('action','data'));
260 $table->align
= array('left','left','left', 'center');
261 $table->wrap
= array(false,false,false,false);
263 if ($fff = get_records('data_fields','dataid',$data->id
,'id')){
264 foreach ($fff as $ff) {
266 $field = data_get_field($ff, $data);
268 $table->data
[] = array(
270 '<a href="field.php?mode=display&d='.$data->id
.
271 '&fid='.$field->field
->id
.'&sesskey='.sesskey().'">'.$field->field
->name
.'</a>',
273 $field->image().' '.get_string($field->type
, 'data'),
275 shorten_text($field->field
->description
, 30),
277 '<a href="field.php?d='.$data->id
.'&mode=display&fid='.$field->field
->id
.'&sesskey='.sesskey().'">'.
278 '<img src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>'.
280 '<a href="field.php?d='.$data->id
.'&mode=delete&fid='.$field->field
->id
.'&sesskey='.sesskey().'">'.
281 '<img src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>'
290 echo '<div class="fieldadd">';
291 echo get_string('newfield','data').': ';
292 popup_form($CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
.'&mode=new&sesskey='.
293 sesskey().'&newtype=', $menufield, 'fieldform', '', 'choose');
294 helpbutton('fields', get_string('addafield','data'), 'data');
297 if ($fields = get_records('data_fields','dataid',$data->id
)) {
298 echo '<div class="sortdefault">';
299 echo '<form id="sortdefault" action="'.$CFG->wwwroot
.'/mod/data/field.php" method="get">';
300 echo '<fieldset class="invisiblefieldset">';
301 echo '<input type="hidden" name="d" value="'.$data->id
.'" />';
302 echo '<input type="hidden" name="mode" value="sort" />';
303 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
304 echo ' '.get_string('defaultsortfield','data').':';
305 echo '<select name="defaultsort"><option value="0">'.get_string('dateentered','data').'</option>';
306 foreach ($fields as $field) {
307 if ($field->id
== $data->defaultsort
) {
308 echo '<option value="'.$field->id
.'" selected="selected">'.$field->name
.'</option>';
310 echo '<option value="'.$field->id
.'">'.$field->name
.'</option>';
316 $options = array(0 => get_string('ascending', 'data'),
317 1 => get_string('descending', 'data'));
318 choose_from_menu($options, 'defaultsortdir', $data->defaultsortdir
, '');
319 echo '<input type="submit" value="'.get_string('go').'" />';
329 print_footer($course);