MDL-62275 qtype_calculatedmulti: Fix typo
[moodle.git] / mod / data / edit.php
blob5c6a6bf944cfaf0f472a0773274d192564a6e15a
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file is part of the Database module for Moodle
21 * @copyright 2005 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package mod_data
26 require_once('../../config.php');
27 require_once('locallib.php');
28 require_once("$CFG->libdir/rsslib.php");
29 require_once("$CFG->libdir/form/filemanager.php");
31 $id = optional_param('id', 0, PARAM_INT); // course module id
32 $d = optional_param('d', 0, PARAM_INT); // database id
33 $rid = optional_param('rid', 0, PARAM_INT); //record id
34 $cancel = optional_param('cancel', '', PARAM_RAW); // cancel an add
35 $mode ='addtemplate'; //define the mode for this page, only 1 mode available
36 $tags = optional_param_array('tags', [], PARAM_TAGLIST);
38 $url = new moodle_url('/mod/data/edit.php');
39 if ($rid !== 0) {
40 $record = $DB->get_record('data_records', array(
41 'id' => $rid,
42 'dataid' => $d,
43 ), '*', MUST_EXIST);
44 $url->param('rid', $rid);
46 if ($cancel !== '') {
47 $url->param('cancel', $cancel);
50 if ($id) {
51 $url->param('id', $id);
52 $PAGE->set_url($url);
53 if (! $cm = get_coursemodule_from_id('data', $id)) {
54 print_error('invalidcoursemodule');
56 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
57 print_error('coursemisconf');
59 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
60 print_error('invalidcoursemodule');
63 } else {
64 $url->param('d', $d);
65 $PAGE->set_url($url);
66 if (! $data = $DB->get_record('data', array('id'=>$d))) {
67 print_error('invalidid', 'data');
69 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
70 print_error('coursemisconf');
72 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
73 print_error('invalidcoursemodule');
77 require_login($course, false, $cm);
79 if (isguestuser()) {
80 redirect('view.php?d='.$data->id);
83 $context = context_module::instance($cm->id);
85 /// If it's hidden then it doesn't show anything. :)
86 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
87 $strdatabases = get_string("modulenameplural", "data");
89 $PAGE->set_title($data->name);
90 $PAGE->set_heading($course->fullname);
91 echo $OUTPUT->header();
92 notice(get_string("activityiscurrentlyhidden"));
95 /// Can't use this if there are no fields
96 if (has_capability('mod/data:managetemplates', $context)) {
97 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
98 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
102 if ($rid) {
103 // When editing an existing record, we require the session key
104 require_sesskey();
107 // Get Group information for permission testing and record creation
108 $currentgroup = groups_get_activity_group($cm);
109 $groupmode = groups_get_activity_groupmode($cm);
111 if (!has_capability('mod/data:manageentries', $context)) {
112 if ($rid) {
113 // User is editing an existing record
114 if (!data_user_can_manage_entry($record, $data, $context)) {
115 print_error('noaccess','data');
117 } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
118 // User is trying to create a new record
119 print_error('noaccess','data');
123 if ($cancel) {
124 redirect('view.php?d='.$data->id);
128 /// RSS and CSS and JS meta
129 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
130 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
131 $rsstitle = $courseshortname . ': ' . format_string($data->name);
132 rss_add_http_header($context, 'mod_data', $data, $rsstitle);
134 if ($data->csstemplate) {
135 $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
137 if ($data->jstemplate) {
138 $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
141 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
143 foreach ($possiblefields as $field) {
144 if ($field->type == 'file' || $field->type == 'picture') {
145 require_once($CFG->dirroot.'/repository/lib.php');
146 break;
150 /// Define page variables
151 $strdata = get_string('modulenameplural','data');
153 if ($rid) {
154 $PAGE->navbar->add(get_string('editentry', 'data'));
157 $PAGE->set_title($data->name);
158 $PAGE->set_heading($course->fullname);
160 // Process incoming data for adding/updating records.
162 // Keep track of any notifications.
163 $generalnotifications = array();
164 $fieldnotifications = array();
166 // Process the submitted form.
167 if ($datarecord = data_submitted() and confirm_sesskey()) {
168 if ($rid) {
169 // Updating an existing record.
171 // Retrieve the format for the fields.
172 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
174 // Validate the form to ensure that enough data was submitted.
175 $processeddata = data_process_submission($data, $fields, $datarecord);
177 // Add the new notification data.
178 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
179 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
181 if ($processeddata->validated) {
182 // Enough data to update the record.
183 data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata);
184 core_tag_tag::set_item_tags('mod_data', 'data_records', $rid, $context, $tags);
186 $viewurl = new moodle_url('/mod/data/view.php', array(
187 'd' => $data->id,
188 'rid' => $rid,
190 redirect($viewurl);
193 } else {
194 // No recordid was specified - creating a new entry.
196 // Retrieve the format for the fields.
197 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
199 // Validate the form to ensure that enough data was submitted.
200 $processeddata = data_process_submission($data, $fields, $datarecord);
202 // Add the new notification data.
203 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
204 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
206 // Add instance to data_record.
207 if ($processeddata->validated && $recordid = data_add_record($data, $currentgroup)) {
209 // Now populate the fields contents of the new record.
210 data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
212 core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags);
214 if (!empty($datarecord->saveandview)) {
215 $viewurl = new moodle_url('/mod/data/view.php', array(
216 'd' => $data->id,
217 'rid' => $recordid,
219 redirect($viewurl);
220 } else if (!empty($datarecord->saveandadd)) {
221 // User has clicked "Save and add another". Reset all of the fields.
222 $datarecord = null;
227 // End of form processing.
230 /// Print the page header
232 echo $OUTPUT->header();
233 echo $OUTPUT->heading(format_string($data->name), 2);
234 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
235 groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
237 /// Print the tabs
239 $currenttab = 'add';
240 if ($rid) {
241 $editentry = true; //used in tabs
243 include('tabs.php');
246 /// Print the browsing interface
248 $patterns = array(); //tags to replace
249 $replacement = array(); //html to replace those yucky tags
251 //form goes here first in case add template is empty
252 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
253 echo '<div>';
254 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
255 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
256 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
257 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
259 if (!$rid){
260 echo $OUTPUT->heading(get_string('newentry','data'), 3);
263 /******************************************
264 * Regular expression replacement section *
265 ******************************************/
266 if ($data->addtemplate){
267 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
268 $patterns = array();
269 $replacements = array();
271 ///then we generate strings to replace
272 foreach ($possiblefields as $eachfield){
273 $field = data_get_field($eachfield, $data);
275 // To skip unnecessary calls to display_add_field().
276 if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
277 // Replace the field tag.
278 $patterns[] = "[[".$field->field->name."]]";
279 $errors = '';
280 if (!empty($fieldnotifications[$field->field->name])) {
281 foreach ($fieldnotifications[$field->field->name] as $notification) {
282 $errors .= $OUTPUT->notification($notification);
285 $replacements[] = $errors . $field->display_add_field($rid, $datarecord);
288 // Replace the field id tag.
289 $patterns[] = "[[".$field->field->name."#id]]";
290 $replacements[] = 'field_'.$field->field->id;
293 if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
294 $patterns[] = "##tags##";
295 $replacements[] = data_generate_tag_form($rid);
298 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
300 } else { //if the add template is not yet defined, print the default form!
301 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
302 $newtext = '';
305 foreach ($generalnotifications as $notification) {
306 echo $OUTPUT->notification($notification);
308 echo $newtext;
310 echo '<div class="mdl-align m-t-1"><input type="submit" class="btn btn-primary" name="saveandview" ' .
311 'value="' . get_string('saveandview', 'data') . '" />';
312 if ($rid) {
313 echo '&nbsp;<input type="submit" class="btn btn-primary" name="cancel" ' .
314 'value="' . get_string('cancel') . '" onclick="javascript:history.go(-1)" />';
315 } else {
316 if ((!$data->maxentries) ||
317 has_capability('mod/data:manageentries', $context) ||
318 (data_numentries($data) < ($data->maxentries - 1))) {
319 echo '&nbsp;<input type="submit" class="btn btn-primary" name="saveandadd" ' .
320 'value="' . get_string('saveandadd', 'data') . '" />';
323 echo '</div>';
324 echo $OUTPUT->box_end();
325 echo '</div></form>';
328 /// Finish the page
330 // Print the stuff that need to come after the form fields.
331 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
332 print_error('nofieldindatabase', 'data');
334 foreach ($fields as $eachfield) {
335 $field = data_get_field($eachfield, $data);
336 $field->print_after_form();
339 echo $OUTPUT->footer();