forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
340 lines (277 loc) · 11.7 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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file is part of the Database module for Moodle
*
* @copyright 2005 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package mod_data
*/
require_once('../../config.php');
require_once('locallib.php');
require_once("$CFG->libdir/rsslib.php");
require_once("$CFG->libdir/form/filemanager.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
$cancel = optional_param('cancel', '', PARAM_RAW); // cancel an add
$mode ='addtemplate'; //define the mode for this page, only 1 mode available
$tags = optional_param_array('tags', [], PARAM_TAGLIST);
$url = new moodle_url('/mod/data/edit.php');
if ($rid !== 0) {
$record = $DB->get_record('data_records', array(
'id' => $rid,
'dataid' => $d,
), '*', MUST_EXIST);
$url->param('rid', $rid);
}
if ($cancel !== '') {
$url->param('cancel', $cancel);
}
if ($id) {
$url->param('id', $id);
$PAGE->set_url($url);
if (! $cm = get_coursemodule_from_id('data', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
print_error('coursemisconf');
}
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
print_error('invalidcoursemodule');
}
} else {
$url->param('d', $d);
$PAGE->set_url($url);
if (! $data = $DB->get_record('data', array('id'=>$d))) {
print_error('invalidid', 'data');
}
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('coursemisconf');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
print_error('invalidcoursemodule');
}
}
require_login($course, false, $cm);
if (isguestuser()) {
redirect('view.php?d='.$data->id);
}
$context = context_module::instance($cm->id);
/// If it's hidden then it doesn't show anything. :)
if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
$strdatabases = get_string("modulenameplural", "data");
$PAGE->set_title($data->name);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
notice(get_string("activityiscurrentlyhidden"));
}
/// Can't use this if there are no fields
if (has_capability('mod/data:managetemplates', $context)) {
if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
}
}
if ($rid) {
// When editing an existing record, we require the session key
require_sesskey();
}
// Get Group information for permission testing and record creation
$currentgroup = groups_get_activity_group($cm);
$groupmode = groups_get_activity_groupmode($cm);
if (!has_capability('mod/data:manageentries', $context)) {
if ($rid) {
// User is editing an existing record
if (!data_user_can_manage_entry($record, $data, $context)) {
print_error('noaccess','data');
}
} else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
// User is trying to create a new record
print_error('noaccess','data');
}
}
if ($cancel) {
redirect('view.php?d='.$data->id);
}
/// RSS and CSS and JS meta
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
$rsstitle = $courseshortname . ': ' . format_string($data->name);
rss_add_http_header($context, 'mod_data', $data, $rsstitle);
}
if ($data->csstemplate) {
$PAGE->requires->css('/mod/data/css.php?d='.$data->id);
}
if ($data->jstemplate) {
$PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
}
$possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
foreach ($possiblefields as $field) {
if ($field->type == 'file' || $field->type == 'picture') {
require_once($CFG->dirroot.'/repository/lib.php');
break;
}
}
/// Define page variables
$strdata = get_string('modulenameplural','data');
if ($rid) {
$PAGE->navbar->add(get_string('editentry', 'data'));
}
$PAGE->set_title($data->name);
$PAGE->set_heading($course->fullname);
$PAGE->force_settings_menu(true);
// Process incoming data for adding/updating records.
// Keep track of any notifications.
$generalnotifications = array();
$fieldnotifications = array();
// Process the submitted form.
if ($datarecord = data_submitted() and confirm_sesskey()) {
if ($rid) {
// Updating an existing record.
// Retrieve the format for the fields.
$fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
// Validate the form to ensure that enough data was submitted.
$processeddata = data_process_submission($data, $fields, $datarecord);
// Add the new notification data.
$generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
$fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
if ($processeddata->validated) {
// Enough data to update the record.
data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata);
core_tag_tag::set_item_tags('mod_data', 'data_records', $rid, $context, $tags);
$viewurl = new moodle_url('/mod/data/view.php', array(
'd' => $data->id,
'rid' => $rid,
));
redirect($viewurl);
}
} else {
// No recordid was specified - creating a new entry.
// Retrieve the format for the fields.
$fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
// Validate the form to ensure that enough data was submitted.
$processeddata = data_process_submission($data, $fields, $datarecord);
// Add the new notification data.
$generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
$fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
// Add instance to data_record.
if ($processeddata->validated && $recordid = data_add_record($data, $currentgroup)) {
// Now populate the fields contents of the new record.
data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags);
if (!empty($datarecord->saveandview)) {
$viewurl = new moodle_url('/mod/data/view.php', array(
'd' => $data->id,
'rid' => $recordid,
));
redirect($viewurl);
} else if (!empty($datarecord->saveandadd)) {
// User has clicked "Save and add another". Reset all of the fields.
$datarecord = null;
}
}
}
}
// End of form processing.
/// Print the page header
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($data->name), 2);
echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
/// Print the tabs
$currenttab = 'add';
if ($rid) {
$editentry = true; //used in tabs
}
include('tabs.php');
/// 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" />';
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
if (!$rid){
echo $OUTPUT->heading(get_string('newentry','data'), 3);
}
/******************************************
* Regular expression replacement section *
******************************************/
if ($data->addtemplate){
$possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
$patterns = array();
$replacements = array();
///then we generate strings to replace
foreach ($possiblefields as $eachfield){
$field = data_get_field($eachfield, $data);
// To skip unnecessary calls to display_add_field().
if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
// Replace the field tag.
$patterns[] = "[[".$field->field->name."]]";
$errors = '';
if (!empty($fieldnotifications[$field->field->name])) {
foreach ($fieldnotifications[$field->field->name] as $notification) {
$errors .= $OUTPUT->notification($notification);
}
}
$replacements[] = $errors . $field->display_add_field($rid, $datarecord);
}
// Replace the field id tag.
$patterns[] = "[[".$field->field->name."#id]]";
$replacements[] = 'field_'.$field->field->id;
}
if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
$patterns[] = "##tags##";
$replacements[] = data_generate_tag_form($rid);
}
$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 = '';
}
foreach ($generalnotifications as $notification) {
echo $OUTPUT->notification($notification);
}
echo $newtext;
echo '<div class="mdl-align mt-1"><input type="submit" class="btn btn-primary" name="saveandview" ' .
'value="' . get_string('saveandview', 'data') . '" />';
if ($rid) {
echo ' <input type="submit" class="btn btn-primary" name="cancel" ' .
'value="' . get_string('cancel') . '" onclick="javascript:history.go(-1)" />';
} else {
if ((!$data->maxentries) ||
has_capability('mod/data:manageentries', $context) ||
(data_numentries($data) < ($data->maxentries - 1))) {
echo ' <input type="submit" class="btn btn-primary" name="saveandadd" ' .
'value="' . get_string('saveandadd', 'data') . '" />';
}
}
echo '</div>';
echo $OUTPUT->box_end();
echo '</div></form>';
/// Finish the page
// Print the stuff that need to come after the form fields.
if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
print_error('nofieldindatabase', 'data');
}
foreach ($fields as $eachfield) {
$field = data_get_field($eachfield, $data);
$field->print_after_form();
}
echo $OUTPUT->footer();