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('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');
39 $url->param('rid', $rid);
42 $url->param('cancel', $cancel);
46 $url->param('id', $id);
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');
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);
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
98 // When editing an existing record, we require the session key
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)) {
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');
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');
145 /// Define page variables
146 $strdata = get_string('modulenameplural','data');
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
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);
183 $field->update_content($rid, $value, $name);
188 // Trigger an event for updating this record.
189 $event = \mod_data\event\record_updated
::create(array(
191 'context' => $context,
192 'courseid' => $course->id
,
194 'dataid' => $data->id
197 $event->add_record_snapshot('data', $data);
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)) {
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);
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
);
267 $editentry = true; //used in tabs
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">';
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');
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');
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);
318 echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
320 echo ' <input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
322 if ((!$data->maxentries
) ||
has_capability('mod/data:manageentries', $context) ||
(data_numentries($data) < ($data->maxentries
- 1))) {
323 echo ' <input type="submit" value="'.get_string('saveandadd','data').'" />';
327 echo $OUTPUT->box_end();
328 echo '</div></form>';
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();