MDL-43987 core: Remove port numbers in cleanremoteaddr
[moodle.git] / mod / data / edit.php
blob6dd59f75e698bd28d6dd3c71af30be84cf5ebcf0
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('lib.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
37 $url = new moodle_url('/mod/data/edit.php');
38 if ($rid !== 0) {
39 $url->param('rid', $rid);
41 if ($cancel !== '') {
42 $url->param('cancel', $cancel);
45 if ($id) {
46 $url->param('id', $id);
47 $PAGE->set_url($url);
48 if (! $cm = get_coursemodule_from_id('data', $id)) {
49 print_error('invalidcoursemodule');
51 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
52 print_error('coursemisconf');
54 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
55 print_error('invalidcoursemodule');
58 } else {
59 $url->param('d', $d);
60 $PAGE->set_url($url);
61 if (! $data = $DB->get_record('data', array('id'=>$d))) {
62 print_error('invalidid', 'data');
64 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
65 print_error('coursemisconf');
67 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
68 print_error('invalidcoursemodule');
72 require_login($course, false, $cm);
74 if (isguestuser()) {
75 redirect('view.php?d='.$data->id);
78 $context = context_module::instance($cm->id);
80 /// If it's hidden then it doesn't show anything. :)
81 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
82 $strdatabases = get_string("modulenameplural", "data");
84 $PAGE->set_title($data->name);
85 $PAGE->set_heading($course->fullname);
86 echo $OUTPUT->header();
87 notice(get_string("activityiscurrentlyhidden"));
90 /// Can't use this if there are no fields
91 if (has_capability('mod/data:managetemplates', $context)) {
92 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
93 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
97 if ($rid) {
98 // When editing an existing record, we require the session key
99 require_sesskey();
102 // Get Group information for permission testing and record creation
103 $currentgroup = groups_get_activity_group($cm);
104 $groupmode = groups_get_activity_groupmode($cm);
106 if (!has_capability('mod/data:manageentries', $context)) {
107 if ($rid) {
108 // User is editing an existing record
109 if (!data_isowner($rid) || data_in_readonly_period($data)) {
110 print_error('noaccess','data');
112 } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
113 // User is trying to create a new record
114 print_error('noaccess','data');
118 if ($cancel) {
119 redirect('view.php?d='.$data->id);
123 /// RSS and CSS and JS meta
124 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
125 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
126 $rsstitle = $courseshortname . ': ' . format_string($data->name);
127 rss_add_http_header($context, 'mod_data', $data, $rsstitle);
129 if ($data->csstemplate) {
130 $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
132 if ($data->jstemplate) {
133 $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
136 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
138 foreach ($possiblefields as $field) {
139 if ($field->type == 'file' || $field->type == 'picture') {
140 require_once($CFG->dirroot.'/repository/lib.php');
141 break;
145 /// Define page variables
146 $strdata = get_string('modulenameplural','data');
148 if ($rid) {
149 $PAGE->navbar->add(get_string('editentry', 'data'));
152 $PAGE->set_title($data->name);
153 $PAGE->set_heading($course->fullname);
155 /// Process incoming data for adding/updating records
157 if ($datarecord = data_submitted() and confirm_sesskey()) {
159 $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
161 if ($rid) { /// Update some records
163 /// All student edits are marked unapproved by default
164 $record = $DB->get_record('data_records', array('id'=>$rid));
166 /// reset approved flag after student edit
167 if (!has_capability('mod/data:approve', $context)) {
168 $record->approved = 0;
171 $record->timemodified = time();
172 $DB->update_record('data_records', $record);
174 /// Update all content
175 $field = NULL;
176 foreach ($datarecord as $name => $value) {
177 if (!in_array($name, $ignorenames)) {
178 $namearr = explode('_',$name); // Second one is the field id
179 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
180 $field = data_get_field_from_id($namearr[1], $data);
182 if ($field) {
183 $field->update_content($rid, $value, $name);
188 // Trigger an event for updating this record.
189 $event = \mod_data\event\record_updated::create(array(
190 'objectid' => $rid,
191 'context' => $context,
192 'courseid' => $course->id,
193 'other' => array(
194 'dataid' => $data->id
197 $event->add_record_snapshot('data', $data);
198 $event->trigger();
200 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid);
202 } else { /// Add some new records
203 ///Empty form checking - you can't submit an empty form!
205 $emptyform = true; // assume the worst
207 foreach ($datarecord as $name => $value) {
208 if (!in_array($name, $ignorenames)) {
209 $namearr = explode('_', $name); // Second one is the field id
210 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
211 $field = data_get_field_from_id($namearr[1], $data);
213 if ($field->notemptyfield($value, $name)) {
214 $emptyform = false;
215 break; // if anything has content, this form is not empty, so stop now!
220 if ($emptyform){ //nothing gets written to database
221 echo $OUTPUT->notification(get_string('emptyaddform','data'));
224 if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
226 /// Insert a whole lot of empty records to make sure we have them
227 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
228 foreach ($fields as $field) {
229 $content = new stdClass();
230 $content->recordid = $recordid;
231 $content->fieldid = $field->id;
232 $DB->insert_record('data_content',$content);
235 /// For each field in the add form, add it to the data_content.
236 foreach ($datarecord as $name => $value){
237 if (!in_array($name, $ignorenames)) {
238 $namearr = explode('_', $name); // Second one is the field id
239 if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes
240 $field = data_get_field_from_id($namearr[1], $data);
242 if ($field) {
243 $field->update_content($recordid, $value, $name);
248 if (!empty($datarecord->saveandview)) {
249 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid);
253 } // End of form processing
256 /// Print the page header
258 echo $OUTPUT->header();
259 echo $OUTPUT->heading(format_string($data->name), 2);
260 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
261 groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
263 /// Print the tabs
265 $currenttab = 'add';
266 if ($rid) {
267 $editentry = true; //used in tabs
269 include('tabs.php');
272 /// Print the browsing interface
274 $patterns = array(); //tags to replace
275 $replacement = array(); //html to replace those yucky tags
277 //form goes here first in case add template is empty
278 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
279 echo '<div>';
280 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
281 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
282 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
283 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
285 if (!$rid){
286 echo $OUTPUT->heading(get_string('newentry','data'), 3);
289 /******************************************
290 * Regular expression replacement section *
291 ******************************************/
292 if ($data->addtemplate){
293 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
294 $patterns = array();
295 $replacements = array();
297 ///then we generate strings to replace
298 foreach ($possiblefields as $eachfield){
299 $field = data_get_field($eachfield, $data);
301 // To skip unnecessary calls to display_add_field().
302 if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
303 $patterns[] = "[[".$field->field->name."]]";
304 $replacements[] = $field->display_add_field($rid);
306 $patterns[] = "[[".$field->field->name."#id]]";
307 $replacements[] = 'field_'.$field->field->id;
309 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
311 } else { //if the add template is not yet defined, print the default form!
312 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
313 $newtext = '';
316 echo $newtext;
318 echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
319 if ($rid) {
320 echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
321 } else {
322 if ((!$data->maxentries) || has_capability('mod/data:manageentries', $context) || (data_numentries($data) < ($data->maxentries - 1))) {
323 echo '&nbsp;<input type="submit" value="'.get_string('saveandadd','data').'" />';
326 echo '</div>';
327 echo $OUTPUT->box_end();
328 echo '</div></form>';
331 /// Finish the page
333 // Print the stuff that need to come after the form fields.
334 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
335 print_error('nofieldindatabase', 'data');
337 foreach ($fields as $eachfield) {
338 $field = data_get_field($eachfield, $data);
339 $field->print_after_form();
342 echo $OUTPUT->footer();