Merge branch 'MDL-59436-master' of git://github.com/junpataleta/moodle
[moodle.git] / mod / data / edit.php
blob615aa22feb144d8dbc7982da9e42909eb6742246
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
39 $url = new moodle_url('/mod/data/edit.php');
40 if ($rid !== 0) {
41 $record = $DB->get_record('data_records', array(
42 'id' => $rid,
43 'dataid' => $d,
44 ), '*', MUST_EXIST);
45 $url->param('rid', $rid);
47 if ($cancel !== '') {
48 $url->param('cancel', $cancel);
51 if ($id) {
52 $url->param('id', $id);
53 $PAGE->set_url($url);
54 if (! $cm = get_coursemodule_from_id('data', $id)) {
55 print_error('invalidcoursemodule');
57 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
58 print_error('coursemisconf');
60 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
61 print_error('invalidcoursemodule');
64 } else {
65 $url->param('d', $d);
66 $PAGE->set_url($url);
67 if (! $data = $DB->get_record('data', array('id'=>$d))) {
68 print_error('invalidid', 'data');
70 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
71 print_error('coursemisconf');
73 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
74 print_error('invalidcoursemodule');
78 require_login($course, false, $cm);
80 if (isguestuser()) {
81 redirect('view.php?d='.$data->id);
84 $context = context_module::instance($cm->id);
86 /// If it's hidden then it doesn't show anything. :)
87 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
88 $strdatabases = get_string("modulenameplural", "data");
90 $PAGE->set_title($data->name);
91 $PAGE->set_heading($course->fullname);
92 echo $OUTPUT->header();
93 notice(get_string("activityiscurrentlyhidden"));
96 /// Can't use this if there are no fields
97 if (has_capability('mod/data:managetemplates', $context)) {
98 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
99 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
103 if ($rid) {
104 // When editing an existing record, we require the session key
105 require_sesskey();
108 // Get Group information for permission testing and record creation
109 $currentgroup = groups_get_activity_group($cm);
110 $groupmode = groups_get_activity_groupmode($cm);
112 if (!has_capability('mod/data:manageentries', $context)) {
113 if ($rid) {
114 // User is editing an existing record
115 if (!data_user_can_manage_entry($record, $data, $context)) {
116 print_error('noaccess','data');
118 } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
119 // User is trying to create a new record
120 print_error('noaccess','data');
124 if ($cancel) {
125 redirect('view.php?d='.$data->id);
129 /// RSS and CSS and JS meta
130 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
131 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
132 $rsstitle = $courseshortname . ': ' . format_string($data->name);
133 rss_add_http_header($context, 'mod_data', $data, $rsstitle);
135 if ($data->csstemplate) {
136 $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
138 if ($data->jstemplate) {
139 $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
142 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
144 foreach ($possiblefields as $field) {
145 if ($field->type == 'file' || $field->type == 'picture') {
146 require_once($CFG->dirroot.'/repository/lib.php');
147 break;
151 /// Define page variables
152 $strdata = get_string('modulenameplural','data');
154 if ($rid) {
155 $PAGE->navbar->add(get_string('editentry', 'data'));
158 $PAGE->set_title($data->name);
159 $PAGE->set_heading($course->fullname);
161 // Process incoming data for adding/updating records.
163 // Keep track of any notifications.
164 $generalnotifications = array();
165 $fieldnotifications = array();
167 // Process the submitted form.
168 if ($datarecord = data_submitted() and confirm_sesskey()) {
169 if ($rid) {
170 // Updating an existing record.
172 // Retrieve the format for the fields.
173 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
175 // Validate the form to ensure that enough data was submitted.
176 $processeddata = data_process_submission($data, $fields, $datarecord);
178 // Add the new notification data.
179 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
180 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
182 if ($processeddata->validated) {
183 // Enough data to update the record.
184 data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata);
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 if (!empty($datarecord->saveandview)) {
213 $viewurl = new moodle_url('/mod/data/view.php', array(
214 'd' => $data->id,
215 'rid' => $recordid,
217 redirect($viewurl);
218 } else if (!empty($datarecord->saveandadd)) {
219 // User has clicked "Save and add another". Reset all of the fields.
220 $datarecord = null;
225 // End of form processing.
228 /// Print the page header
230 echo $OUTPUT->header();
231 echo $OUTPUT->heading(format_string($data->name), 2);
232 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
233 groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
235 /// Print the tabs
237 $currenttab = 'add';
238 if ($rid) {
239 $editentry = true; //used in tabs
241 include('tabs.php');
244 /// Print the browsing interface
246 $patterns = array(); //tags to replace
247 $replacement = array(); //html to replace those yucky tags
249 //form goes here first in case add template is empty
250 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
251 echo '<div>';
252 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
253 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
254 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
255 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
257 if (!$rid){
258 echo $OUTPUT->heading(get_string('newentry','data'), 3);
261 /******************************************
262 * Regular expression replacement section *
263 ******************************************/
264 if ($data->addtemplate){
265 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
266 $patterns = array();
267 $replacements = array();
269 ///then we generate strings to replace
270 foreach ($possiblefields as $eachfield){
271 $field = data_get_field($eachfield, $data);
273 // To skip unnecessary calls to display_add_field().
274 if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
275 // Replace the field tag.
276 $patterns[] = "[[".$field->field->name."]]";
277 $errors = '';
278 if (!empty($fieldnotifications[$field->field->name])) {
279 foreach ($fieldnotifications[$field->field->name] as $notification) {
280 $errors .= $OUTPUT->notification($notification);
283 $replacements[] = $errors . $field->display_add_field($rid, $datarecord);
286 // Replace the field id tag.
287 $patterns[] = "[[".$field->field->name."#id]]";
288 $replacements[] = 'field_'.$field->field->id;
290 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
292 } else { //if the add template is not yet defined, print the default form!
293 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
294 $newtext = '';
297 foreach ($generalnotifications as $notification) {
298 echo $OUTPUT->notification($notification);
300 echo $newtext;
302 echo '<div class="mdl-align m-t-1"><input type="submit" class="btn btn-primary" name="saveandview" ' .
303 'value="' . get_string('saveandview', 'data') . '" />';
304 if ($rid) {
305 echo '&nbsp;<input type="submit" class="btn btn-primary" name="cancel" ' .
306 'value="' . get_string('cancel') . '" onclick="javascript:history.go(-1)" />';
307 } else {
308 if ((!$data->maxentries) ||
309 has_capability('mod/data:manageentries', $context) ||
310 (data_numentries($data) < ($data->maxentries - 1))) {
311 echo '&nbsp;<input type="submit" class="btn btn-primary" name="saveandadd" ' .
312 'value="' . get_string('saveandadd', 'data') . '" />';
315 echo '</div>';
316 echo $OUTPUT->box_end();
317 echo '</div></form>';
320 /// Finish the page
322 // Print the stuff that need to come after the form fields.
323 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
324 print_error('nofieldindatabase', 'data');
326 foreach ($fields as $eachfield) {
327 $field = data_get_field($eachfield, $data);
328 $field->print_after_form();
331 echo $OUTPUT->footer();