2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
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
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');
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'));
90 redirect('view.php?d='.$data->id
);
94 /// RSS and CSS and JS 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
.'&sesskey='.sesskey().'&');
127 $groupselect = " AND groupid = '$currentgroup'";
128 $groupparam = "&groupid=$currentgroup";
138 $editentry = true; //used in tabs
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;
157 $record->approved
= 0;
160 $record->groupid
= $currentgroup;
161 $record->timemodified
= time();
162 update_record('data_records',$record);
164 /// Update all content
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);
173 $field->update_content($rid, $value, $name);
178 add_to_log($course->id
, 'data', 'update', "view.php?d=$data->id&rid=$rid", $data->id
, $cm->id
);
180 redirect($CFG->wwwroot
.'/mod/data/view.php?d='.$data->id
.'&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);
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)) {
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);
236 $field->update_content($recordid, $value, $name);
241 add_to_log($course->id
, 'data', 'add', "view.php?d=$data->id&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
.'&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">';
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%');
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);
291 echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
293 echo ' <input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
295 echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
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)) {
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">';
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);
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>';
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>';
330 echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
333 print_simple_box_end();
335 echo '<div style="text-align:center">';
336 echo '<a href="edit.php?d='.$data->id
.'&import=1">'.get_string('uploadrecords', 'data').'</a>';
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);