MDL-11515:
[moodle-linuxchix.git] / mod / data / edit.php
blobeb1e791876524e9e473073613312efff57c52f05
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
8 // //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
10 // //
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. //
15 // //
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: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once('../../config.php');
26 require_once('lib.php');
27 require_once("$CFG->libdir/rsslib.php");
29 $id = optional_param('id', 0, PARAM_INT); // course module id
30 $d = optional_param('d', 0, PARAM_INT); // database id
31 $rid = optional_param('rid', 0, PARAM_INT); //record id
32 $import = optional_param('import', 0, PARAM_INT); // show import form
33 $cancel = optional_param('cancel', ''); // cancel an add
34 $mode ='addtemplate'; //define the mode for this page, only 1 mode available
36 if ($id) {
37 if (! $cm = get_coursemodule_from_id('data', $id)) {
38 error('Course Module ID was incorrect');
40 if (! $course = get_record('course', 'id', $cm->course)) {
41 error('Course is misconfigured');
43 if (! $data = get_record('data', 'id', $cm->instance)) {
44 error('Course module is incorrect');
47 } else {
48 if (! $data = get_record('data', 'id', $d)) {
49 error('Data ID is incorrect');
51 if (! $course = get_record('course', 'id', $data->course)) {
52 error('Course is misconfigured');
54 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
55 error('Course Module ID was incorrect');
59 require_login($course->id, false, $cm);
61 if (!isloggedin() or isguest()) {
62 redirect('view.php?d='.$data->id);
65 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
67 /// If it's hidden then it doesn't show anything. :)
68 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
69 $strdatabases = get_string("modulenameplural", "data");
71 $navlinks = array();
72 $navlinks[] = array('name' => $strdatabases, 'link' => "index.php?id=$course->id", 'type' => 'activity');
73 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
74 $navigation = build_navigation($navlinks);
76 print_header_simple(format_string($data->name), "", $navigation, "", "", true, '', navmenu($course, $cm));
77 notice(get_string("activityiscurrentlyhidden"));
80 /// Can't use this if there are no fields
81 if (has_capability('mod/data:managetemplates', $context)) {
82 if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
83 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
87 if ($rid) { // So do you have access?
88 if (!(has_capability('mod/data:manageentries', $context) or data_isowner($rid)) or !confirm_sesskey() ) {
89 error(get_string('noaccess','data'));
93 if ($cancel) {
94 redirect('view.php?d='.$data->id);
98 /// RSS and CSS and JS meta
99 $meta = '';
100 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
101 $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
102 $meta .= '<link rel="alternate" type="application/rss+xml" ';
103 $meta .= 'title ="'. format_string($course->shortname) .': %fullname%" href="'.$rsspath.'" />';
105 if ($data->csstemplate) {
106 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
108 if ($data->jstemplate) {
109 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
113 /// Print the page header
114 $strdata = get_string('modulenameplural','data');
116 $navlinks = array();
117 $navlinks[] = array('name' => $strdata, 'link' => "index.php?id=$course->id", 'type' => 'activity');
118 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
119 $navigation = build_navigation($navlinks);
121 print_header_simple($data->name, '', $navigation,
122 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
123 navmenu($course, $cm), '', '');
125 /// Check to see if groups are being used here
126 groups_print_activity_menu($cm, 'edit.php?d='.$data->id);
127 $currentgroup = groups_get_activity_group($cm);
128 $groupmode = groups_get_activity_groupmode($cm);
130 print_heading(format_string($data->name));
132 if ($currentgroup) {
133 $groupselect = " AND groupid = '$currentgroup'";
134 $groupparam = "&amp;groupid=$currentgroup";
135 } else {
136 $groupselect = "";
137 $groupparam = "";
138 $currentgroup = 0;
141 /// Print the tabs
143 $currenttab = 'add';
144 if ($rid) {
145 $editentry = true; //used in tabs
147 include('tabs.php');
150 /// Process incoming data for adding/updating records
152 if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) {
154 $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
156 if ($rid) { /// Update some records
158 /// All student edits are marked unapproved by default
159 $record = get_record('data_records','id',$rid);
161 /// reset approved flag after student edit
162 if (!has_capability('mod/data:approve', $context)) {
163 $record->approved = 0;
166 $record->groupid = $currentgroup;
167 $record->timemodified = time();
168 update_record('data_records',$record);
170 /// Update all content
171 $field = NULL;
172 foreach ($datarecord as $name => $value) {
173 if (!in_array($name, $ignorenames)) {
174 $namearr = explode('_',$name); // Second one is the field id
175 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
176 $field = data_get_field_from_id($namearr[1], $data);
178 if ($field) {
179 $field->update_content($rid, $value, $name);
184 add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&amp;rid=$rid", $data->id, $cm->id);
186 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$rid);
188 } else { /// Add some new records
190 if (!data_user_can_add_entry($data, $currentgroup, $groupmode)) {
191 error('Can not add entries!');
194 /// Check if maximum number of entry as specified by this database is reached
195 /// Of course, you can't be stopped if you are an editting teacher! =)
197 if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){
198 notify (get_string('atmaxentry','data'));
199 print_footer($course);
200 exit;
203 ///Empty form checking - you can't submit an empty form!
205 $emptyform = true; // assume the worst
207 foreach ($datarecord as $name => $value) {
208 if (!in_array($name, $ignorenames)) {
209 $namearr = explode('_', $name); // Second one is the field id
210 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
211 $field = data_get_field_from_id($namearr[1], $data);
213 if ($field->notemptyfield($value, $name)) {
214 $emptyform = false;
215 break; // if anything has content, this form is not empty, so stop now!
220 if ($emptyform){ //nothing gets written to database
221 notify(get_string('emptyaddform','data'));
224 if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
226 /// Insert a whole lot of empty records to make sure we have them
227 $fields = get_records('data_fields','dataid',$data->id);
228 foreach ($fields as $field) {
229 $content->recordid = $recordid;
230 $content->fieldid = $field->id;
231 insert_record('data_content',$content);
234 //for each field in the add form, add it to the data_content.
235 foreach ($datarecord as $name => $value){
236 if (!in_array($name, $ignorenames)) {
237 $namearr = explode('_', $name); // Second one is the field id
238 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
239 $field = data_get_field_from_id($namearr[1], $data);
241 if ($field) {
242 $field->update_content($recordid, $value, $name);
247 add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);
249 notify(get_string('entrysaved','data'));
251 if (!empty($datarecord->saveandview)) {
252 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$recordid);
256 } // End of form processing
258 /// Print the browsing interface
260 $patterns = array(); //tags to replace
261 $replacement = array(); //html to replace those yucky tags
263 //form goes here first in case add template is empty
264 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
265 echo '<div>';
266 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
267 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
268 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
269 print_simple_box_start('center','80%');
271 if (!$rid){
272 print_heading(get_string('newentry','data'), '', 2);
275 /******************************************
276 * Regular expression replacement section *
277 ******************************************/
278 if ($data->addtemplate){
279 $possiblefields = get_records('data_fields','dataid',$data->id,'id');
281 ///then we generate strings to replace
282 foreach ($possiblefields as $eachfield){
283 $field = data_get_field($eachfield, $data);
284 $patterns[]="[[".$field->field->name."]]";
285 $replacements[] = $field->display_add_field($rid);
286 $patterns[]="[[".$field->field->name."#id]]";
287 $replacements[] = 'field_'.$field->field->id;
289 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
291 } else { //if the add template is not yet defined, print the default form!
292 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
293 $newtext = '';
296 echo $newtext;
297 echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
298 if ($rid) {
299 echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
300 } else {
301 echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
303 echo '</div>';
304 print_simple_box_end();
305 echo '</div></form>';
308 /// Upload records section. Only for teachers and the admin.
310 if (has_capability('mod/data:manageentries',$context)) {
311 if ($import) {
312 print_simple_box_start('center','80%');
313 print_heading(get_string('uploadrecords', 'data'), '', 3);
315 $maxuploadsize = get_max_upload_file_size();
316 echo '<div style="text-align:center">';
317 echo '<form enctype="multipart/form-data" action="import.php" method="post">';
318 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />';
319 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
320 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
321 echo '<table align="center" cellspacing="0" cellpadding="2" border="0">';
322 echo '<tr>';
323 echo '<td align="right">'.get_string('csvfile', 'data').':</td>';
324 echo '<td><input type="file" name="recordsfile" size="30" />';
325 helpbutton('importcsv', get_string('csvimport', 'data'), 'data', true, false);
326 echo '</td><tr>';
327 echo '<td align="right">'.get_string('fielddelimiter', 'data').':</td>';
328 echo '<td><input type="text" name="fielddelimiter" size="6" />';
329 echo get_string('defaultfielddelimiter', 'data').'</td>';
330 echo '</tr>';
331 echo '<td align="right">'.get_string('fieldenclosure', 'data').':</td>';
332 echo '<td><input type="text" name="fieldenclosure" size="6" />';
333 echo get_string('defaultfieldenclosure', 'data').'</td>';
334 echo '</tr>';
335 echo '</table>';
336 echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
337 echo '</form>';
338 echo '</div>';
339 print_simple_box_end();
340 } else {
341 echo '<div style="text-align:center">';
342 echo '<a href="edit.php?d='.$data->id.'&amp;import=1">'.get_string('uploadrecords', 'data').'</a>';
343 echo '</div>';
348 /// Finish the page
350 // Print the stuff that need to come after the form fields.
351 if (!$fields = get_records('data_fields', 'dataid', $data->id)) {
352 error(get_string('nofieldindatabase', 'data'));
354 foreach ($fields as $eachfield) {
355 $field = data_get_field($eachfield, $data);
356 $field->print_after_form();
359 print_footer($course);