Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / data / edit.php
blob3b062e12936401f8ace92f13a2138b9142147728
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 $navlinks = array();
73 $navlinks[] = array('name' => $strdatabases, 'link' => "index.php?id=$course->id", 'type' => 'activity');
74 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
75 $navigation = build_navigation($navlinks);
77 print_header_simple(format_string($data->name), "", $navigation, "", "", true, '', navmenu($course, $cm));
78 notice(get_string("activityiscurrentlyhidden"));
81 /// Can't use this if there are no fields
82 if (has_capability('mod/data:managetemplates', $context)) {
83 if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
84 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
88 if ($rid) { // So do you have access?
89 if (!(has_capability('mod/data:manageentries', $context) or data_isowner($rid)) or !confirm_sesskey() ) {
90 error(get_string('noaccess','data'));
94 if ($cancel) {
95 redirect('view.php?d='.$data->id);
99 /// RSS and CSS and JS meta
100 $meta = '';
101 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
102 $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
103 $meta .= '<link rel="alternate" type="application/rss+xml" ';
104 $meta .= 'title ="'. format_string($course->shortname) .': %fullname%" href="'.$rsspath.'" />';
106 if ($data->csstemplate) {
107 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
109 if ($data->jstemplate) {
110 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
114 /// Print the page header
115 $strdata = get_string('modulenameplural','data');
117 $navlinks = array();
118 $navlinks[] = array('name' => $strdata, 'link' => "index.php?id=$course->id", 'type' => 'activity');
119 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
120 $navigation = build_navigation($navlinks);
122 print_header_simple($data->name, '', $navigation,
123 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
124 navmenu($course, $cm), '', '');
126 /// Check to see if groups are being used here
127 $groupmode = groupmode($course, $cm);
128 $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id);
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 = "";
140 /// Print the tabs
142 $currenttab = 'add';
143 if ($rid) {
144 $editentry = true; //used in tabs
146 include('tabs.php');
149 /// Process incoming data for adding/updating records
151 if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) {
153 $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
155 if ($rid) { /// Update some records
157 /// All student edits are marked unapproved by default
158 $record = get_record('data_records','id',$rid);
160 /// reset approved flag after student edit
161 if (!has_capability('mod/data:approve', $context)) {
162 $record->approved = 0;
165 $record->groupid = $currentgroup;
166 $record->timemodified = time();
167 update_record('data_records',$record);
169 /// Update all content
170 $field = NULL;
171 foreach ($datarecord as $name => $value) {
172 if (!in_array($name, $ignorenames)) {
173 $namearr = explode('_',$name); // Second one is the field id
174 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
175 $field = data_get_field_from_id($namearr[1], $data);
177 if ($field) {
178 $field->update_content($rid, $value, $name);
183 add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&amp;rid=$rid", $data->id, $cm->id);
185 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$rid);
187 } else { /// Add some new records
189 if (!data_user_can_add_entry($data, $currentgroup, $groupmode)) {
190 error('Can not add entries!');
193 /// Check if maximum number of entry as specified by this database is reached
194 /// Of course, you can't be stopped if you are an editting teacher! =)
196 if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){
197 notify (get_string('atmaxentry','data'));
198 print_footer($course);
199 exit;
202 ///Empty form checking - you can't submit an empty form!
204 $emptyform = true; // assume the worst
206 foreach ($datarecord as $name => $value) {
207 if (!in_array($name, $ignorenames)) {
208 $namearr = explode('_', $name); // Second one is the field id
209 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
210 $field = data_get_field_from_id($namearr[1], $data);
212 if ($field->notemptyfield($value, $name)) {
213 $emptyform = false;
214 break; // if anything has content, this form is not empty, so stop now!
219 if ($emptyform){ //nothing gets written to database
220 notify(get_string('emptyaddform','data'));
223 if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
225 /// Insert a whole lot of empty records to make sure we have them
226 $fields = get_records('data_fields','dataid',$data->id);
227 foreach ($fields as $field) {
228 $content->recordid = $recordid;
229 $content->fieldid = $field->id;
230 insert_record('data_content',$content);
233 //for each field in the add form, add it to the data_content.
234 foreach ($datarecord as $name => $value){
235 if (!in_array($name, $ignorenames)) {
236 $namearr = explode('_', $name); // Second one is the field id
237 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
238 $field = data_get_field_from_id($namearr[1], $data);
240 if ($field) {
241 $field->update_content($recordid, $value, $name);
246 add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);
248 notify(get_string('entrysaved','data'));
250 if (!empty($datarecord->saveandview)) {
251 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;rid='.$recordid);
255 } // End of form processing
257 /// Print the browsing interface
259 $patterns = array(); //tags to replace
260 $replacement = array(); //html to replace those yucky tags
262 //form goes here first in case add template is empty
263 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
264 echo '<div>';
265 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
266 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
267 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
268 print_simple_box_start('center','80%');
270 if (!$rid){
271 print_heading(get_string('newentry','data'), '', 2);
274 /******************************************
275 * Regular expression replacement section *
276 ******************************************/
277 if ($data->addtemplate){
278 $possiblefields = get_records('data_fields','dataid',$data->id,'id');
280 ///then we generate strings to replace
281 foreach ($possiblefields as $eachfield){
282 $field = data_get_field($eachfield, $data);
283 $patterns[]="[[".$field->field->name."]]";
284 $replacements[] = $field->display_add_field($rid);
285 $patterns[]="[[".$field->field->name."#id]]";
286 $replacements[] = 'field_'.$field->field->id;
288 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
290 } else { //if the add template is not yet defined, print the default form!
291 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
292 $newtext = '';
295 echo $newtext;
296 echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
297 if ($rid) {
298 echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
299 } else {
300 echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
302 echo '</div>';
303 print_simple_box_end();
304 echo '</div></form>';
307 /// Upload records section. Only for teachers and the admin.
309 if (has_capability('mod/data:manageentries',$context)) {
310 if ($import) {
311 print_simple_box_start('center','80%');
312 print_heading(get_string('uploadrecords', 'data'), '', 3);
314 $maxuploadsize = get_max_upload_file_size();
315 echo '<div style="text-align:center">';
316 echo '<form enctype="multipart/form-data" action="import.php" method="post">';
317 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />';
318 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
319 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
320 echo '<table align="center" cellspacing="0" cellpadding="2" border="0">';
321 echo '<tr>';
322 echo '<td align="right">'.get_string('csvfile', 'data').':</td>';
323 echo '<td><input type="file" name="recordsfile" size="30" />';
324 helpbutton('importcsv', get_string('csvimport', 'data'), 'data', true, false);
325 echo '</td><tr>';
326 echo '<td align="right">'.get_string('fielddelimiter', 'data').':</td>';
327 echo '<td><input type="text" name="fielddelimiter" size="6" />';
328 echo get_string('defaultfielddelimiter', 'data').'</td>';
329 echo '</tr>';
330 echo '<td align="right">'.get_string('fieldenclosure', 'data').':</td>';
331 echo '<td><input type="text" name="fieldenclosure" size="6" />';
332 echo get_string('defaultfieldenclosure', 'data').'</td>';
333 echo '</tr>';
334 echo '</table>';
335 echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
336 echo '</form>';
337 echo '</div>';
338 print_simple_box_end();
339 } else {
340 echo '<div style="text-align:center">';
341 echo '<a href="edit.php?d='.$data->id.'&amp;import=1">'.get_string('uploadrecords', 'data').'</a>';
342 echo '</div>';
347 /// Finish the page
349 // Print the stuff that need to come after the form fields.
350 if (!$fields = get_records('data_fields', 'dataid', $data->id)) {
351 error(get_string('nofieldindatabase', 'data'));
353 foreach ($fields as $eachfield) {
354 $field = data_get_field($eachfield, $data);
355 $field->print_after_form();
358 print_footer($course);