MDL-10234
[moodle-linuxchix.git] / mod / data / edit.php
blob194ec8acd9b907fd7b0f7a4507dc36fd38cb9810
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's don't show anything. :)
68 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
69 $strdatabases = get_string("modulenameplural", "data");
70 $navigation = "<a href=\"index.php?id=$course->id\">$strdatabases</a> ->";
72 $crumbs[] = array('name' => $strdatabases, 'link' => "index.php?id=$course->id", 'type' => 'activity');
73 $crumbs[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
74 $navigation = build_navigation($crumbs);
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 $crumbs[] = array('name' => $strdata, 'link' => "index.php?id=$course->id", 'type' => 'activity');
117 $crumbs[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
118 $navigation = build_navigation($crumbs);
120 print_header_simple($data->name, '', $navigation,
121 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
122 navmenu($course, $cm), '', '');
124 print_heading(format_string($data->name));
126 /// Check to see if groups are being used here
128 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
129 $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id.'&amp;sesskey='.sesskey().'&amp;');
130 } else {
131 $currentgroup = 0;
134 if ($currentgroup) {
135 $groupselect = " AND groupid = '$currentgroup'";
136 $groupparam = "&amp;groupid=$currentgroup";
137 } else {
138 $groupselect = "";
139 $groupparam = "";
142 /// Print the tabs
144 $currenttab = 'add';
145 if ($rid) {
146 $editentry = true; //used in tabs
148 include('tabs.php');
151 /// Process incoming data for adding/updating records
153 if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) {
155 $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
157 if ($rid) { /// Update some records
159 /// All student edits are marked unapproved by default
160 $record = get_record('data_records','id',$rid);
162 /// reset approved flag after student edit
163 if (!has_capability('mod/data:approve', $context)) {
164 $record->approved = 0;
167 $record->groupid = $currentgroup;
168 $record->timemodified = time();
169 update_record('data_records',$record);
171 /// Update all content
172 $field = NULL;
173 foreach ($datarecord as $name => $value) {
174 if (!in_array($name, $ignorenames)) {
175 $namearr = explode('_',$name); // Second one is the field id
176 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
177 $field = data_get_field_from_id($namearr[1], $data);
179 if ($field) {
180 $field->update_content($rid, $value, $name);
185 add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&amp;rid=$rid", $data->id, $cm->id);
187 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$rid);
189 } else { /// Add some new records
191 if (!data_user_can_add_entry($data, $currentgroup, $groupmode)) {
192 error('Can not add entries!');
195 /// Check if maximum number of entry as specified by this database is reached
196 /// Of course, you can't be stopped if you are an editting teacher! =)
198 if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){
199 notify (get_string('atmaxentry','data'));
200 print_footer($course);
201 exit;
204 ///Empty form checking - you can't submit an empty form!
206 $emptyform = true; // assume the worst
208 foreach ($datarecord as $name => $value) {
209 if (!in_array($name, $ignorenames)) {
210 $namearr = explode('_', $name); // Second one is the field id
211 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
212 $field = data_get_field_from_id($namearr[1], $data);
214 if ($field->notemptyfield($value, $name)) {
215 $emptyform = false;
216 break; // if anything has content, this form is not empty, so stop now!
221 if ($emptyform){ //nothing gets written to database
222 notify(get_string('emptyaddform','data'));
225 if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
227 /// Insert a whole lot of empty records to make sure we have them
228 $fields = get_records('data_fields','dataid',$data->id);
229 foreach ($fields as $field) {
230 $content->recordid = $recordid;
231 $content->fieldid = $field->id;
232 insert_record('data_content',$content);
235 //for each field in the add form, add it to the data_content.
236 foreach ($datarecord as $name => $value){
237 if (!in_array($name, $ignorenames)) {
238 $namearr = explode('_', $name); // Second one is the field id
239 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
240 $field = data_get_field_from_id($namearr[1], $data);
242 if ($field) {
243 $field->update_content($recordid, $value, $name);
248 add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);
250 notify(get_string('entrysaved','data'));
252 if (!empty($datarecord->saveandview)) {
253 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$recordid);
257 } // End of form processing
259 /// Print the browsing interface
261 $patterns = array(); //tags to replace
262 $replacement = array(); //html to replace those yucky tags
264 //form goes here first in case add template is empty
265 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
266 echo '<div>';
267 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
268 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
269 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
270 print_simple_box_start('center','80%');
272 if (!$rid){
273 print_heading(get_string('newentry','data'), '', 2);
276 /******************************************
277 * Regular expression replacement section *
278 ******************************************/
279 if ($data->addtemplate){
280 $possiblefields = get_records('data_fields','dataid',$data->id,'id');
282 ///then we generate strings to replace
283 foreach ($possiblefields as $eachfield){
284 $field = data_get_field($eachfield, $data);
285 $patterns[]="[[".$field->field->name."]]";
286 $replacements[] = $field->display_add_field($rid);
287 $patterns[]="[[".$field->field->name."#id]]";
288 $replacements[] = 'field_'.$field->field->id;
290 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
292 } else { //if the add template is not yet defined, print the default form!
293 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
294 $newtext = '';
297 echo $newtext;
298 echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
299 if ($rid) {
300 echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
301 } else {
302 echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
304 echo '</div>';
305 print_simple_box_end();
306 echo '</div></form>';
309 /// Upload records section. Only for teachers and the admin.
311 if (has_capability('mod/data:manageentries',$context)) {
312 if ($import) {
313 print_simple_box_start('center','80%');
314 print_heading(get_string('uploadrecords', 'data'), '', 3);
316 $maxuploadsize = get_max_upload_file_size();
317 echo '<div style="text-align:center">';
318 echo '<form enctype="multipart/form-data" action="import.php" method="post">';
319 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />';
320 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
321 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
322 echo '<table align="center" cellspacing="0" cellpadding="2" border="0">';
323 echo '<tr>';
324 echo '<td align="right">'.get_string('csvfile', 'data').':</td>';
325 echo '<td><input type="file" name="recordsfile" size="30" />';
326 helpbutton('importcsv', get_string('csvimport', 'data'), 'data', true, false);
327 echo '</td><tr>';
328 echo '<td align="right">'.get_string('fielddelimiter', 'data').':</td>';
329 echo '<td><input type="text" name="fielddelimiter" size="6" />';
330 echo get_string('defaultfielddelimiter', 'data').'</td>';
331 echo '</tr>';
332 echo '<td align="right">'.get_string('fieldenclosure', 'data').':</td>';
333 echo '<td><input type="text" name="fieldenclosure" size="6" />';
334 echo get_string('defaultfieldenclosure', 'data').'</td>';
335 echo '</tr>';
336 echo '</table>';
337 echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
338 echo '</form>';
339 echo '</div>';
340 print_simple_box_end();
341 } else {
342 echo '<div style="text-align:center">';
343 echo '<a href="edit.php?d='.$data->id.'&amp;import=1">'.get_string('uploadrecords', 'data').'</a>';
344 echo '</div>';
349 /// Finish the page
351 // Print the stuff that need to come after the form fields.
352 if (!$fields = get_records('data_fields', 'dataid', $data->id)) {
353 error(get_string('nofieldindatabase', 'data'));
355 foreach ($fields as $eachfield) {
356 $field = data_get_field($eachfield, $data);
357 $field->print_after_form();
360 print_footer($course);