|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once('../../config.php'); 00027 require_once('lib.php'); 00028 00029 $id = optional_param('id', 0, PARAM_INT); // course module id 00030 $d = optional_param('d', 0, PARAM_INT); // database id 00031 $fid = optional_param('fid', 0 , PARAM_INT); // update field id 00032 $newtype = optional_param('newtype','',PARAM_ALPHA); // type of the new field 00033 $mode = optional_param('mode','',PARAM_ALPHA); 00034 $defaultsort = optional_param('defaultsort', 0, PARAM_INT); 00035 $defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT); 00036 $cancel = optional_param('cancel', 0, PARAM_BOOL); 00037 00038 if ($cancel) { 00039 $mode = 'list'; 00040 } 00041 00042 $url = new moodle_url('/mod/data/field.php'); 00043 if ($fid !== 0) { 00044 $url->param('fid', $fid); 00045 } 00046 if ($newtype !== '') { 00047 $url->param('newtype', $newtype); 00048 } 00049 if ($mode !== '') { 00050 $url->param('mode', $mode); 00051 } 00052 if ($defaultsort !== 0) { 00053 $url->param('defaultsort', $defaultsort); 00054 } 00055 if ($defaultsortdir !== 0) { 00056 $url->param('defaultsortdir', $defaultsortdir); 00057 } 00058 if ($cancel !== 0) { 00059 $url->param('cancel', $cancel); 00060 } 00061 00062 if ($id) { 00063 $url->param('id', $id); 00064 $PAGE->set_url($url); 00065 if (! $cm = get_coursemodule_from_id('data', $id)) { 00066 print_error('invalidcoursemodule'); 00067 } 00068 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) { 00069 print_error('coursemisconf'); 00070 } 00071 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) { 00072 print_error('invalidcoursemodule'); 00073 } 00074 00075 } else { 00076 $url->param('d', $d); 00077 $PAGE->set_url($url); 00078 if (! $data = $DB->get_record('data', array('id'=>$d))) { 00079 print_error('invalidid', 'data'); 00080 } 00081 if (! $course = $DB->get_record('course', array('id'=>$data->course))) { 00082 print_error('invalidcoursemodule'); 00083 } 00084 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { 00085 print_error('invalidcoursemodule'); 00086 } 00087 } 00088 00089 require_login($course->id, true, $cm); 00090 00091 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00092 require_capability('mod/data:managetemplates', $context); 00093 00094 /************************************ 00095 * Data Processing * 00096 ***********************************/ 00097 switch ($mode) { 00098 00099 case 'add': 00100 if (confirm_sesskey() and $fieldinput = data_submitted()){ 00101 00102 //$fieldinput->name = data_clean_field_name($fieldinput->name); 00103 00105 if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id)) { 00106 00107 $displaynoticebad = get_string('invalidfieldname','data'); 00108 00109 } else { 00110 00112 data_convert_arrays_to_strings($fieldinput); 00113 00115 $type = required_param('type', PARAM_FILE); 00116 $field = data_get_field_new($type, $data); 00117 00118 $field->define_field($fieldinput); 00119 $field->insert_field(); 00120 00122 data_append_new_field_to_templates($data, $fieldinput->name); 00123 00124 add_to_log($course->id, 'data', 'fields add', 00125 "field.php?d=$data->id&mode=display&fid=$fid", $fid, $cm->id); 00126 00127 $displaynoticegood = get_string('fieldadded','data'); 00128 } 00129 } 00130 break; 00131 00132 00133 case 'update': 00134 if (confirm_sesskey() and $fieldinput = data_submitted()){ 00135 00136 //$fieldinput->name = data_clean_field_name($fieldinput->name); 00137 00138 if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id, $fieldinput->fid)) { 00139 00140 $displaynoticebad = get_string('invalidfieldname','data'); 00141 00142 } else { 00144 data_convert_arrays_to_strings($fieldinput); 00145 00147 $field = data_get_field_from_id($fid, $data); 00148 $oldfieldname = $field->field->name; 00149 00150 $field->field->name = $fieldinput->name; 00151 $field->field->description = $fieldinput->description; 00152 00153 for ($i=1; $i<=10; $i++) { 00154 if (isset($fieldinput->{'param'.$i})) { 00155 $field->field->{'param'.$i} = $fieldinput->{'param'.$i}; 00156 } else { 00157 $field->field->{'param'.$i} = ''; 00158 } 00159 } 00160 00161 $field->update_field(); 00162 00164 data_replace_field_in_templates($data, $oldfieldname, $field->field->name); 00165 00166 add_to_log($course->id, 'data', 'fields update', 00167 "field.php?d=$data->id&mode=display&fid=$fid", $fid, $cm->id); 00168 00169 $displaynoticegood = get_string('fieldupdated','data'); 00170 } 00171 } 00172 break; 00173 00174 00175 case 'delete': // Delete a field 00176 if (confirm_sesskey()){ 00177 00178 if ($confirm = optional_param('confirm', 0, PARAM_INT)) { 00179 00180 00181 // Delete the field completely 00182 if ($field = data_get_field_from_id($fid, $data)) { 00183 $field->delete_field(); 00184 00185 // Update the templates. 00186 data_replace_field_in_templates($data, $field->field->name, ''); 00187 00188 // Update the default sort field 00189 if ($fid == $data->defaultsort) { 00190 $rec = new stdClass(); 00191 $rec->id = $data->id; 00192 $rec->defaultsort = 0; 00193 $rec->defaultsortdir = 0; 00194 $DB->update_record('data', $rec); 00195 } 00196 00197 add_to_log($course->id, 'data', 'fields delete', 00198 "field.php?d=$data->id", $field->field->name, $cm->id); 00199 00200 $displaynoticegood = get_string('fielddeleted', 'data'); 00201 } 00202 00203 } else { 00204 00205 data_print_header($course,$cm,$data, false); 00206 00207 // Print confirmation message. 00208 $field = data_get_field_from_id($fid, $data); 00209 00210 echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'), 00211 'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1', 00212 'field.php?d='.$data->id); 00213 00214 echo $OUTPUT->footer(); 00215 exit; 00216 } 00217 } 00218 break; 00219 00220 00221 case 'sort': // Set the default sort parameters 00222 if (confirm_sesskey()) { 00223 $rec = new stdClass(); 00224 $rec->id = $data->id; 00225 $rec->defaultsort = $defaultsort; 00226 $rec->defaultsortdir = $defaultsortdir; 00227 00228 $DB->update_record('data', $rec); 00229 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2); 00230 exit; 00231 } 00232 break; 00233 00234 default: 00235 break; 00236 } 00237 00238 00239 00241 00243 $directories = get_list_of_plugins('mod/data/field/'); 00244 $menufield = array(); 00245 00246 foreach ($directories as $directory){ 00247 $menufield[$directory] = get_string($directory,'data'); //get from language files 00248 } 00249 asort($menufield); //sort in alphabetical order 00250 $PAGE->set_title(get_string('course') . ': ' . $course->fullname); 00251 $PAGE->set_heading($course->fullname); 00252 00253 $PAGE->set_pagetype('mod-data-field-' . $newtype); 00254 if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { 00255 data_print_header($course, $cm, $data,'fields'); 00256 00257 $field = data_get_field_new($newtype, $data); 00258 $field->display_edit_field(); 00259 00260 } else if ($mode == 'display' && confirm_sesskey()) { 00261 data_print_header($course, $cm, $data,'fields'); 00262 00263 $field = data_get_field_from_id($fid, $data); 00264 $field->display_edit_field(); 00265 00266 } else { 00267 data_print_header($course, $cm, $data,'fields'); 00268 00269 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { 00270 echo $OUTPUT->notification(get_string('nofieldindatabase','data')); // nothing in database 00271 echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id)); // link to presets 00272 00273 } else { //else print quiz style list of fields 00274 00275 $table = new html_table(); 00276 $table->head = array(get_string('fieldname','data'), get_string('type','data'), get_string('fielddescription', 'data'), get_string('action','data')); 00277 $table->align = array('left','left','left', 'center'); 00278 $table->wrap = array(false,false,false,false); 00279 00280 if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){ 00281 foreach ($fff as $ff) { 00282 00283 $field = data_get_field($ff, $data); 00284 00285 $table->data[] = array( 00286 00287 '<a href="field.php?mode=display&d='.$data->id. 00288 '&fid='.$field->field->id.'&sesskey='.sesskey().'">'.$field->field->name.'</a>', 00289 00290 $field->image().' '.get_string($field->type, 'data'), 00291 00292 shorten_text($field->field->description, 30), 00293 00294 '<a href="field.php?d='.$data->id.'&mode=display&fid='.$field->field->id.'&sesskey='.sesskey().'">'. 00295 '<img src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>'. 00296 ' '. 00297 '<a href="field.php?d='.$data->id.'&mode=delete&fid='.$field->field->id.'&sesskey='.sesskey().'">'. 00298 '<img src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>' 00299 00300 ); 00301 } 00302 } 00303 echo html_writer::table($table); 00304 } 00305 00306 00307 echo '<div class="fieldadd">'; 00308 echo '<label for="fieldform_jump">'.get_string('newfield','data').'</label>'; 00309 $popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='. sesskey(); 00310 echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array(''=>'choosedots'), 'fieldform'); 00311 echo $OUTPUT->help_icon('newfield', 'data'); 00312 echo '</div>'; 00313 00314 echo '<div class="sortdefault">'; 00315 echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">'; 00316 echo '<div>'; 00317 echo '<input type="hidden" name="d" value="'.$data->id.'" />'; 00318 echo '<input type="hidden" name="mode" value="sort" />'; 00319 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'; 00320 echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>'; 00321 echo '<select id="defaultsort" name="defaultsort">'; 00322 if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) { 00323 echo '<optgroup label="'.get_string('fields', 'data').'">'; 00324 foreach ($fields as $field) { 00325 if ($data->defaultsort == $field->id) { 00326 echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>'; 00327 } else { 00328 echo '<option value="'.$field->id.'">'.$field->name.'</option>'; 00329 } 00330 } 00331 echo '</optgroup>'; 00332 } 00333 $options = array(); 00334 $options[DATA_TIMEADDED] = get_string('timeadded', 'data'); 00335 // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0 00336 /* $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data'); 00337 $options[DATA_FIRSTNAME] = get_string('authorfirstname', 'data'); 00338 $options[DATA_LASTNAME] = get_string('authorlastname', 'data'); 00339 if ($data->approval and has_capability('mod/data:approve', $context)) { 00340 $options[DATA_APPROVED] = get_string('approved', 'data'); 00341 }*/ 00342 echo '<optgroup label="'.get_string('other', 'data').'">'; 00343 foreach ($options as $key => $name) { 00344 if ($data->defaultsort == $key) { 00345 echo '<option value="'.$key.'" selected="selected">'.$name.'</option>'; 00346 } else { 00347 echo '<option value="'.$key.'">'.$name.'</option>'; 00348 } 00349 } 00350 echo '</optgroup>'; 00351 echo '</select>'; 00352 00353 $options = array(0 => get_string('ascending', 'data'), 00354 1 => get_string('descending', 'data')); 00355 echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false); 00356 echo '<input type="submit" value="'.get_string('save', 'data').'" />'; 00357 echo '</div>'; 00358 echo '</form>'; 00359 echo '</div>'; 00360 00361 } 00362 00364 echo $OUTPUT->footer(); 00365