Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / mod / data / edit.php
blobe2b884a92567ef30a5bc39bbdb1d77b93fc72b2b
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> ->";
71 print_header_simple(format_string($data->name), "",
72 "$navigation ".format_string($data->name), "", "", true, '', navmenu($course, $cm));
73 notice(get_string("activityiscurrentlyhidden"));
76 /// Can't use this if there are no fields
77 if (has_capability('mod/data:managetemplates', $context)) {
78 if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
79 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
83 if ($rid) { // So do you have access?
84 if (!(has_capability('mod/data:manageentries', $context) or data_isowner($rid)) or !confirm_sesskey() ) {
85 error(get_string('noaccess','data'));
89 if ($cancel) {
90 redirect('view.php?d='.$data->id);
94 /// RSS and CSS and JS meta
95 $meta = '';
96 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
97 $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
98 $meta .= '<link rel="alternate" type="application/rss+xml" ';
99 $meta .= 'title ="'.$course->shortname.': %fullname%" href="'.$rsspath.'" />';
101 if ($data->csstemplate) {
102 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
104 if ($data->jstemplate) {
105 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
109 /// Print the page header
110 $strdata = get_string('modulenameplural','data');
112 print_header_simple($data->name, '', "<a href='index.php?id=$course->id'>$strdata</a> -> $data->name",
113 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
114 navmenu($course, $cm), '', '');
116 print_heading(format_string($data->name));
118 /// Check to see if groups are being used here
120 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
121 $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id.'&amp;sesskey='.sesskey().'&amp;');
122 } else {
123 $currentgroup = 0;
126 if ($currentgroup) {
127 $groupselect = " AND groupid = '$currentgroup'";
128 $groupparam = "&amp;groupid=$currentgroup";
129 } else {
130 $groupselect = "";
131 $groupparam = "";
134 /// Print the tabs
136 $currenttab = 'add';
137 if ($rid) {
138 $editentry = true; //used in tabs
140 include('tabs.php');
143 /// Process incoming data for adding/updating records
145 if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) {
147 $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
149 if ($rid) { /// Update some records
151 /// All student edits are marked unapproved by default
152 $record = get_record('data_records','id',$rid);
154 if ($data->approval == 1 || has_capability('mod/data:approve', $context)) {
155 $record->approved = 1;
156 } else {
157 $record->approved = 0;
160 $record->groupid = $currentgroup;
161 $record->timemodified = time();
162 update_record('data_records',$record);
164 /// Update all content
165 $field = NULL;
166 foreach ($datarecord as $name => $value) {
167 if (!in_array($name, $ignorenames)) {
168 $namearr = explode('_',$name); // Second one is the field id
169 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
170 $field = data_get_field_from_id($namearr[1], $data);
172 if ($field) {
173 $field->update_content($rid, $value, $name);
178 add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&amp;rid=$rid", $data->id, $cm->id);
180 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$rid);
182 } else { /// Add some new records
184 if (!data_user_can_add_entry($data, $currentgroup, $groupmode)) {
185 error('Can not add entries!');
188 /// Check if maximum number of entry as specified by this database is reached
189 /// Of course, you can't be stopped if you are an editting teacher! =)
191 if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){
192 notify (get_string('atmaxentry','data'));
193 print_footer($course);
194 exit;
197 ///Empty form checking - you can't submit an empty form!
199 $emptyform = true; // assume the worst
201 foreach ($datarecord as $name => $value) {
202 if (!in_array($name, $ignorenames)) {
203 $namearr = explode('_', $name); // Second one is the field id
204 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
205 $field = data_get_field_from_id($namearr[1], $data);
207 if ($field->notemptyfield($value, $name)) {
208 $emptyform = false;
209 break; // if anything has content, this form is not empty, so stop now!
214 if ($emptyform){ //nothing gets written to database
215 notify(get_string('emptyaddform','data'));
218 if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
220 /// Insert a whole lot of empty records to make sure we have them
221 $fields = get_records('data_fields','dataid',$data->id);
222 foreach ($fields as $field) {
223 $content->recordid = $recordid;
224 $content->fieldid = $field->id;
225 insert_record('data_content',$content);
228 //for each field in the add form, add it to the data_content.
229 foreach ($datarecord as $name => $value){
230 if (!in_array($name, $ignorenames)) {
231 $namearr = explode('_', $name); // Second one is the field id
232 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
233 $field = data_get_field_from_id($namearr[1], $data);
235 if ($field) {
236 $field->update_content($recordid, $value, $name);
241 add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);
243 notify(get_string('entrysaved','data'));
245 if (!empty($datarecord->saveandview)) {
246 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$recordid);
250 } // End of form processing
252 /// Print the browsing interface
254 $patterns = array(); //tags to replace
255 $replacement = array(); //html to replace those yucky tags
257 //form goes here first in case add template is empty
258 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
259 echo '<fieldset>';
260 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
261 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
262 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
263 print_simple_box_start('center','80%');
265 if (!$rid){
266 print_heading(get_string('newentry','data'), '', 2);
269 /******************************************
270 * Regular expression replacement section *
271 ******************************************/
272 if ($data->addtemplate){
273 $possiblefields = get_records('data_fields','dataid',$data->id,'id');
275 ///then we generate strings to replace
276 foreach ($possiblefields as $eachfield){
277 $field = data_get_field($eachfield, $data);
278 $patterns[]="[[".$field->field->name."]]";
279 $replacements[] = $field->display_add_field($rid);
280 $patterns[]="[[".$field->field->name."#id]]";
281 $replacements[] = 'field_'.$field->field->id;
283 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
285 } else { //if the add template is not yet defined, print the default form!
286 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
287 $newtext = '';
290 echo $newtext;
291 echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
292 if ($rid) {
293 echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
294 } else {
295 echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
297 echo '</div>';
298 print_simple_box_end();
299 echo '</fieldset></form>';
302 /// Upload records section. Only for teachers and the admin.
304 if (has_capability('mod/data:manageentries',$context)) {
305 if ($import) {
306 print_simple_box_start('center','80%');
307 print_heading(get_string('uploadrecords', 'data'), '', 3);
309 $maxuploadsize = get_max_upload_file_size();
310 echo '<div style="text-align:center">';
311 echo '<form enctype="multipart/form-data" action="import.php" method="post">';
312 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />';
313 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
314 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
315 echo '<table align="center" cellspacing="0" cellpadding="2" border="0">';
316 echo '<tr>';
317 echo '<td align="right">'.get_string('csvfile', 'data').':</td>';
318 echo '<td><input type="file" name="recordsfile" size="30" />';
319 helpbutton('importcsv', get_string('csvimport', 'data'), 'data', true, false);
320 echo '</td><tr>';
321 echo '<td align="right">'.get_string('fielddelimiter', 'data').':</td>';
322 echo '<td><input type="text" name="fielddelimiter" size="6" />';
323 echo get_string('defaultfielddelimiter', 'data').'</td>';
324 echo '</tr>';
325 echo '<td align="right">'.get_string('fieldenclosure', 'data').':</td>';
326 echo '<td><input type="text" name="fieldenclosure" size="6" />';
327 echo get_string('defaultfieldenclosure', 'data').'</td>';
328 echo '</tr>';
329 echo '</table>';
330 echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
331 echo '</form>';
332 echo '</div>';
333 print_simple_box_end();
334 } else {
335 echo '<div style="text-align:center">';
336 echo '<a href="edit.php?d='.$data->id.'&amp;import=1">'.get_string('uploadrecords', 'data').'</a>';
337 echo '</div>';
342 /// Finish the page
344 // Print the stuff that need to come after the form fields.
345 if (!$fields = get_records('data_fields', 'dataid', $data->id)) {
346 error(get_string('nofieldindatabase', 'data'));
348 foreach ($fields as $eachfield) {
349 $field = data_get_field($eachfield, $data);
350 $field->print_after_form();
353 print_footer($course);