Merge branch '39459-29' of git://github.com/samhemelryk/moodle
[moodle.git] / mod / data / field.php
blobe567fff4285491fadeed74aa05e61649de8060e0
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
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
23 * @package mod_data
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);
38 if ($cancel) {
39 $mode = 'list';
42 $url = new moodle_url('/mod/data/field.php');
43 if ($fid !== 0) {
44 $url->param('fid', $fid);
46 if ($newtype !== '') {
47 $url->param('newtype', $newtype);
49 if ($mode !== '') {
50 $url->param('mode', $mode);
52 if ($defaultsort !== 0) {
53 $url->param('defaultsort', $defaultsort);
55 if ($defaultsortdir !== 0) {
56 $url->param('defaultsortdir', $defaultsortdir);
58 if ($cancel !== 0) {
59 $url->param('cancel', $cancel);
62 if ($id) {
63 $url->param('id', $id);
64 $PAGE->set_url($url);
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');
75 } else {
76 $url->param('d', $d);
77 $PAGE->set_url($url);
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 /************************************
95 * Data Processing *
96 ***********************************/
97 switch ($mode) {
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');
109 } else {
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');
127 break;
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');
139 } else {
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;
150 for ($i=1; $i<=10; $i++) {
151 if (isset($fieldinput->{'param'.$i})) {
152 $field->field->{'param'.$i} = $fieldinput->{'param'.$i};
153 } else {
154 $field->field->{'param'.$i} = '';
158 $field->update_field();
160 /// Update the templates.
161 data_replace_field_in_templates($data, $oldfieldname, $field->field->name);
163 $displaynoticegood = get_string('fieldupdated','data');
166 break;
169 case 'delete': // Delete a field
170 if (confirm_sesskey()){
172 if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
175 // Delete the field completely
176 if ($field = data_get_field_from_id($fid, $data)) {
177 $field->delete_field();
179 // Update the templates.
180 data_replace_field_in_templates($data, $field->field->name, '');
182 // Update the default sort field
183 if ($fid == $data->defaultsort) {
184 $rec = new stdClass();
185 $rec->id = $data->id;
186 $rec->defaultsort = 0;
187 $rec->defaultsortdir = 0;
188 $DB->update_record('data', $rec);
191 $displaynoticegood = get_string('fielddeleted', 'data');
194 } else {
196 data_print_header($course,$cm,$data, false);
198 // Print confirmation message.
199 $field = data_get_field_from_id($fid, $data);
201 echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
202 'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
203 'field.php?d='.$data->id);
205 echo $OUTPUT->footer();
206 exit;
209 break;
212 case 'sort': // Set the default sort parameters
213 if (confirm_sesskey()) {
214 $rec = new stdClass();
215 $rec->id = $data->id;
216 $rec->defaultsort = $defaultsort;
217 $rec->defaultsortdir = $defaultsortdir;
219 $DB->update_record('data', $rec);
220 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
221 exit;
223 break;
225 default:
226 break;
231 /// Print the browsing interface
233 ///get the list of possible fields (plugins)
234 $plugins = core_component::get_plugin_list('datafield');
235 $menufield = array();
237 foreach ($plugins as $plugin=>$fulldir){
238 $menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin); //get from language files
240 asort($menufield); //sort in alphabetical order
241 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
242 $PAGE->set_heading($course->fullname);
244 $PAGE->set_pagetype('mod-data-field-' . $newtype);
245 if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field
246 data_print_header($course, $cm, $data,'fields');
248 $field = data_get_field_new($newtype, $data);
249 $field->display_edit_field();
251 } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
252 data_print_header($course, $cm, $data,'fields');
254 $field = data_get_field_from_id($fid, $data);
255 $field->display_edit_field();
257 } else { /// Display the main listing of all fields
258 data_print_header($course, $cm, $data,'fields');
260 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {
261 echo $OUTPUT->notification(get_string('nofieldindatabase','data')); // nothing in database
262 echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id)); // link to presets
264 } else { //else print quiz style list of fields
266 $table = new html_table();
267 $table->head = array(get_string('fieldname','data'), get_string('type','data'), get_string('fielddescription', 'data'), get_string('action','data'));
268 $table->align = array('left','left','left', 'center');
269 $table->wrap = array(false,false,false,false);
271 if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
272 foreach ($fff as $ff) {
274 $field = data_get_field($ff, $data);
276 $table->data[] = array(
278 '<a href="field.php?mode=display&amp;d='.$data->id.
279 '&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.$field->field->name.'</a>',
281 $field->image().'&nbsp;'.get_string($field->type, 'data'),
283 shorten_text($field->field->description, 30),
285 '<a href="field.php?d='.$data->id.'&amp;mode=display&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.
286 '<img src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>'.
287 '&nbsp;'.
288 '<a href="field.php?d='.$data->id.'&amp;mode=delete&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.
289 '<img src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>'
294 echo html_writer::table($table);
298 echo '<div class="fieldadd">';
299 echo '<label for="fieldform_jump">'.get_string('newfield','data').$OUTPUT->help_icon('newfield', 'data').'</label>';
300 $popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='. sesskey();
301 echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array(''=>'choosedots'), 'fieldform');
302 echo '</div>';
304 echo '<div class="sortdefault">';
305 echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
306 echo '<div>';
307 echo '<input type="hidden" name="d" value="'.$data->id.'" />';
308 echo '<input type="hidden" name="mode" value="sort" />';
309 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
310 echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
311 echo '<select id="defaultsort" name="defaultsort">';
312 if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
313 echo '<optgroup label="'.get_string('fields', 'data').'">';
314 foreach ($fields as $field) {
315 if ($data->defaultsort == $field->id) {
316 echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
317 } else {
318 echo '<option value="'.$field->id.'">'.$field->name.'</option>';
321 echo '</optgroup>';
323 $options = array();
324 $options[DATA_TIMEADDED] = get_string('timeadded', 'data');
325 // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
326 /* $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
327 $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data');
328 $options[DATA_LASTNAME] = get_string('authorlastname', 'data');
329 if ($data->approval and has_capability('mod/data:approve', $context)) {
330 $options[DATA_APPROVED] = get_string('approved', 'data');
332 echo '<optgroup label="'.get_string('other', 'data').'">';
333 foreach ($options as $key => $name) {
334 if ($data->defaultsort == $key) {
335 echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
336 } else {
337 echo '<option value="'.$key.'">'.$name.'</option>';
340 echo '</optgroup>';
341 echo '</select>';
343 $options = array(0 => get_string('ascending', 'data'),
344 1 => get_string('descending', 'data'));
345 echo html_writer::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
346 echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false);
347 echo '<input type="submit" value="'.get_string('save', 'data').'" />';
348 echo '</div>';
349 echo '</form>';
350 echo '</div>';
354 /// Finish the page
355 echo $OUTPUT->footer();