mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
edit.php
executable file
·349 lines (287 loc) · 14.9 KB
/
edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
require_once('../../config.php');
require_once('lib.php');
require_once("$CFG->libdir/rsslib.php");
$id = optional_param('id', 0, PARAM_INT); // course module id
$d = optional_param('d', 0, PARAM_INT); // database id
$rid = optional_param('rid', 0, PARAM_INT); //record id
$import = optional_param('import', 0, PARAM_INT); // show import form
$cancel = optional_param('cancel', ''); // cancel an add
$mode ='addtemplate'; //define the mode for this page, only 1 mode available
if ($id) {
if (! $cm = get_coursemodule_from_id('data', $id)) {
error('Course Module ID was incorrect');
}
if (! $course = get_record('course', 'id', $cm->course)) {
error('Course is misconfigured');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
error('Course module is incorrect');
}
} else {
if (! $data = get_record('data', 'id', $d)) {
error('Data ID is incorrect');
}
if (! $course = get_record('course', 'id', $data->course)) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
error('Course Module ID was incorrect');
}
}
require_login($course->id, false, $cm);
if (!isloggedin() or isguest()) {
redirect('view.php?d='.$data->id);
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
/// If it's hidden then it's don't show anything. :)
if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
$strdatabases = get_string("modulenameplural", "data");
$navigation = "<a href=\"index.php?id=$course->id\">$strdatabases</a> ->";
print_header_simple(format_string($data->name), "",
"$navigation ".format_string($data->name), "", "", true, '', navmenu($course, $cm));
notice(get_string("activityiscurrentlyhidden"));
}
/// Can't use this if there are no fields
if (has_capability('mod/data:managetemplates', $context)) {
if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
}
}
if ($rid) { // So do you have access?
if (!(has_capability('mod/data:manageentries', $context) or data_isowner($rid)) or !confirm_sesskey() ) {
error(get_string('noaccess','data'));
}
}
if ($cancel) {
redirect('view.php?d='.$data->id);
}
/// RSS and CSS and JS meta
$meta = '';
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
$rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
$meta .= '<link rel="alternate" type="application/rss+xml" ';
$meta .= 'title ="'. format_string($course->shortname) .': %fullname%" href="'.$rsspath.'" />';
}
if ($data->csstemplate) {
$meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
}
if ($data->jstemplate) {
$meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
}
/// Print the page header
$strdata = get_string('modulenameplural','data');
print_header_simple($data->name, '', "<a href='index.php?id=$course->id'>$strdata</a> -> $data->name",
'', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
navmenu($course, $cm), '', '');
/// Check to see if groups are being used here
$groupmode = groupmode($course, $cm);
$currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id);
print_heading(format_string($data->name));
if ($currentgroup) {
$groupselect = " AND groupid = '$currentgroup'";
$groupparam = "&groupid=$currentgroup";
} else {
$groupselect = "";
$groupparam = "";
}
/// Print the tabs
$currenttab = 'add';
if ($rid) {
$editentry = true; //used in tabs
}
include('tabs.php');
/// Process incoming data for adding/updating records
if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) {
$ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
if ($rid) { /// Update some records
/// All student edits are marked unapproved by default
$record = get_record('data_records','id',$rid);
/// reset approved flag after student edit
if (!has_capability('mod/data:approve', $context)) {
$record->approved = 0;
}
$record->groupid = $currentgroup;
$record->timemodified = time();
update_record('data_records',$record);
/// Update all content
$field = NULL;
foreach ($datarecord as $name => $value) {
if (!in_array($name, $ignorenames)) {
$namearr = explode('_',$name); // Second one is the field id
if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
$field = data_get_field_from_id($namearr[1], $data);
}
if ($field) {
$field->update_content($rid, $value, $name);
}
}
}
add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&rid=$rid", $data->id, $cm->id);
redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid);
} else { /// Add some new records
if (!data_user_can_add_entry($data, $currentgroup, $groupmode)) {
error('Can not add entries!');
}
/// Check if maximum number of entry as specified by this database is reached
/// Of course, you can't be stopped if you are an editting teacher! =)
if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){
notify (get_string('atmaxentry','data'));
print_footer($course);
exit;
}
///Empty form checking - you can't submit an empty form!
$emptyform = true; // assume the worst
foreach ($datarecord as $name => $value) {
if (!in_array($name, $ignorenames)) {
$namearr = explode('_', $name); // Second one is the field id
if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
$field = data_get_field_from_id($namearr[1], $data);
}
if ($field->notemptyfield($value, $name)) {
$emptyform = false;
break; // if anything has content, this form is not empty, so stop now!
}
}
}
if ($emptyform){ //nothing gets written to database
notify(get_string('emptyaddform','data'));
}
if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
/// Insert a whole lot of empty records to make sure we have them
$fields = get_records('data_fields','dataid',$data->id);
foreach ($fields as $field) {
$content->recordid = $recordid;
$content->fieldid = $field->id;
insert_record('data_content',$content);
}
//for each field in the add form, add it to the data_content.
foreach ($datarecord as $name => $value){
if (!in_array($name, $ignorenames)) {
$namearr = explode('_', $name); // Second one is the field id
if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
$field = data_get_field_from_id($namearr[1], $data);
}
if ($field) {
$field->update_content($recordid, $value, $name);
}
}
}
add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&rid=$recordid", $data->id, $cm->id);
notify(get_string('entrysaved','data'));
if (!empty($datarecord->saveandview)) {
redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid);
}
}
}
} // End of form processing
/// Print the browsing interface
$patterns = array(); //tags to replace
$replacement = array(); //html to replace those yucky tags
//form goes here first in case add template is empty
echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
echo '<div>';
echo '<input name="d" value="'.$data->id.'" type="hidden" />';
echo '<input name="rid" value="'.$rid.'" type="hidden" />';
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
print_simple_box_start('center','80%');
if (!$rid){
print_heading(get_string('newentry','data'), '', 2);
}
/******************************************
* Regular expression replacement section *
******************************************/
if ($data->addtemplate){
$possiblefields = get_records('data_fields','dataid',$data->id,'id');
///then we generate strings to replace
foreach ($possiblefields as $eachfield){
$field = data_get_field($eachfield, $data);
$patterns[]="[[".$field->field->name."]]";
$replacements[] = $field->display_add_field($rid);
$patterns[]="[[".$field->field->name."#id]]";
$replacements[] = 'field_'.$field->field->id;
}
$newtext = str_ireplace($patterns, $replacements, $data->{$mode});
} else { //if the add template is not yet defined, print the default form!
echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
$newtext = '';
}
echo $newtext;
echo '<div style="text-align:center"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
if ($rid) {
echo ' <input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
} else {
echo '<input type="submit" value="'.get_string('saveandadd','data').'" />';
}
echo '</div>';
print_simple_box_end();
echo '</div></form>';
/// Upload records section. Only for teachers and the admin.
if (has_capability('mod/data:manageentries',$context)) {
if ($import) {
print_simple_box_start('center','80%');
print_heading(get_string('uploadrecords', 'data'), '', 3);
$maxuploadsize = get_max_upload_file_size();
echo '<div style="text-align:center">';
echo '<form enctype="multipart/form-data" action="import.php" method="post">';
echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'" />';
echo '<input name="d" value="'.$data->id.'" type="hidden" />';
echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
echo '<table align="center" cellspacing="0" cellpadding="2" border="0">';
echo '<tr>';
echo '<td align="right">'.get_string('csvfile', 'data').':</td>';
echo '<td><input type="file" name="recordsfile" size="30" />';
helpbutton('importcsv', get_string('csvimport', 'data'), 'data', true, false);
echo '</td><tr>';
echo '<td align="right">'.get_string('fielddelimiter', 'data').':</td>';
echo '<td><input type="text" name="fielddelimiter" size="6" />';
echo get_string('defaultfielddelimiter', 'data').'</td>';
echo '</tr>';
echo '<td align="right">'.get_string('fieldenclosure', 'data').':</td>';
echo '<td><input type="text" name="fieldenclosure" size="6" />';
echo get_string('defaultfieldenclosure', 'data').'</td>';
echo '</tr>';
echo '</table>';
echo '<input type="submit" value="'.get_string('uploadfile', 'data').'" />';
echo '</form>';
echo '</div>';
print_simple_box_end();
} else {
echo '<div style="text-align:center">';
echo '<a href="edit.php?d='.$data->id.'&import=1">'.get_string('uploadrecords', 'data').'</a>';
echo '</div>';
}
}
/// Finish the page
// Print the stuff that need to come after the form fields.
if (!$fields = get_records('data_fields', 'dataid', $data->id)) {
error(get_string('nofieldindatabase', 'data'));
}
foreach ($fields as $eachfield) {
$field = data_get_field($eachfield, $data);
$field->print_after_form();
}
print_footer($course);
?>