3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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');
40 $record = $DB->get_record('data_records', array(
44 $url->param('rid', $rid);
47 $url->param('cancel', $cancel);
51 $url->param('id', $id);
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');
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);
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
103 // When editing an existing record, we require the session key
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)) {
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');
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');
150 /// Define page variables
151 $strdata = get_string('modulenameplural','data');
154 $PAGE->navbar
->add(get_string('editentry', 'data'));
157 $PAGE->set_title($data->name
);
158 $PAGE->set_heading($course->fullname
);
159 $PAGE->force_settings_menu(true);
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()) {
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);
185 core_tag_tag
::set_item_tags('mod_data', 'data_records', $rid, $context, $tags);
187 $viewurl = new moodle_url('/mod/data/view.php', array(
195 // No recordid was specified - creating a new entry.
197 // Retrieve the format for the fields.
198 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d
));
200 // Validate the form to ensure that enough data was submitted.
201 $processeddata = data_process_submission($data, $fields, $datarecord);
203 // Add the new notification data.
204 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications
);
205 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications
);
207 // Add instance to data_record.
208 if ($processeddata->validated
&& $recordid = data_add_record($data, $currentgroup)) {
210 // Now populate the fields contents of the new record.
211 data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
213 core_tag_tag
::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags);
215 if (!empty($datarecord->saveandview
)) {
216 $viewurl = new moodle_url('/mod/data/view.php', array(
221 } else if (!empty($datarecord->saveandadd
)) {
222 // User has clicked "Save and add another". Reset all of the fields.
228 // End of form processing.
231 /// Print the page header
233 echo $OUTPUT->header();
234 echo $OUTPUT->heading(format_string($data->name
), 2);
235 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id
), 'generalbox', 'intro');
236 groups_print_activity_menu($cm, $CFG->wwwroot
.'/mod/data/edit.php?d='.$data->id
);
242 $editentry = true; //used in tabs
247 /// Print the browsing interface
249 $patterns = array(); //tags to replace
250 $replacement = array(); //html to replace those yucky tags
252 //form goes here first in case add template is empty
253 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
255 echo '<input name="d" value="'.$data->id
.'" type="hidden" />';
256 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
257 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
258 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
261 echo $OUTPUT->heading(get_string('newentry','data'), 3);
264 /******************************************
265 * Regular expression replacement section *
266 ******************************************/
267 if ($data->addtemplate
){
268 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id
), 'id');
270 $replacements = array();
272 ///then we generate strings to replace
273 foreach ($possiblefields as $eachfield){
274 $field = data_get_field($eachfield, $data);
276 // To skip unnecessary calls to display_add_field().
277 if (strpos($data->addtemplate
, "[[".$field->field
->name
."]]") !== false) {
278 // Replace the field tag.
279 $patterns[] = "[[".$field->field
->name
."]]";
281 if (!empty($fieldnotifications[$field->field
->name
])) {
282 foreach ($fieldnotifications[$field->field
->name
] as $notification) {
283 $errors .= $OUTPUT->notification($notification);
286 $replacements[] = $errors . $field->display_add_field($rid, $datarecord);
289 // Replace the field id tag.
290 $patterns[] = "[[".$field->field
->name
."#id]]";
291 $replacements[] = 'field_'.$field->field
->id
;
294 if (core_tag_tag
::is_enabled('mod_data', 'data_records')) {
295 $patterns[] = "##tags##";
296 $replacements[] = data_generate_tag_form($rid);
299 $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
301 } else { //if the add template is not yet defined, print the default form!
302 echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
306 foreach ($generalnotifications as $notification) {
307 echo $OUTPUT->notification($notification);
311 echo '<div class="mdl-align mt-1"><input type="submit" class="btn btn-primary" name="saveandview" ' .
312 'value="' . get_string('saveandview', 'data') . '" />';
314 echo ' <input type="submit" class="btn btn-primary" name="cancel" ' .
315 'value="' . get_string('cancel') . '" onclick="javascript:history.go(-1)" />';
317 if ((!$data->maxentries
) ||
318 has_capability('mod/data:manageentries', $context) ||
319 (data_numentries($data) < ($data->maxentries
- 1))) {
320 echo ' <input type="submit" class="btn btn-primary" name="saveandadd" ' .
321 'value="' . get_string('saveandadd', 'data') . '" />';
325 echo $OUTPUT->box_end();
326 echo '</div></form>';
331 // Print the stuff that need to come after the form fields.
332 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id
))) {
333 print_error('nofieldindatabase', 'data');
335 foreach ($fields as $eachfield) {
336 $field = data_get_field($eachfield, $data);
337 $field->print_after_form();
340 echo $OUTPUT->footer();