Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
1 <?php 2 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. 14 // 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/>. 17 18 /** 19 * This file is part of the Database module for Moodle 20 * 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 24 */ 25 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"); 30 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 $mode ='addtemplate'; //define the mode for this page, only 1 mode available 35 $tags = optional_param_array('tags', [], PARAM_TAGLIST); 36 $redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back. 37 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); 45 } 46 47 if ($id) { 48 $url->param('id', $id); 49 $PAGE->set_url($url); 50 if (! $cm = get_coursemodule_from_id('data', $id)) { 51 print_error('invalidcoursemodule'); 52 } 53 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) { 54 print_error('coursemisconf'); 55 } 56 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) { 57 print_error('invalidcoursemodule'); 58 } 59 60 } else { 61 $url->param('d', $d); 62 $PAGE->set_url($url); 63 if (! $data = $DB->get_record('data', array('id'=>$d))) { 64 print_error('invalidid', 'data'); 65 } 66 if (! $course = $DB->get_record('course', array('id'=>$data->course))) { 67 print_error('coursemisconf'); 68 } 69 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { 70 print_error('invalidcoursemodule'); 71 } 72 } 73 74 $url->param('backto', $redirectbackto); 75 76 require_login($course, false, $cm); 77 78 if (isguestuser()) { 79 redirect('view.php?d='.$data->id); 80 } 81 82 $context = context_module::instance($cm->id); 83 84 /// If it's hidden then it doesn't show anything. :) 85 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) { 86 $strdatabases = get_string("modulenameplural", "data"); 87 88 $PAGE->set_title($data->name); 89 $PAGE->set_heading($course->fullname); 90 echo $OUTPUT->header(); 91 notice(get_string("activityiscurrentlyhidden")); 92 } 93 94 /// Can't use this if there are no fields 95 if (has_capability('mod/data:managetemplates', $context)) { 96 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database! 97 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry 98 } 99 } 100 101 if ($rid) { 102 // When editing an existing record, we require the session key 103 require_sesskey(); 104 } 105 106 // Get Group information for permission testing and record creation 107 $currentgroup = groups_get_activity_group($cm); 108 $groupmode = groups_get_activity_groupmode($cm); 109 110 if (!has_capability('mod/data:manageentries', $context)) { 111 if ($rid) { 112 // User is editing an existing record 113 if (!data_user_can_manage_entry($record, $data, $context)) { 114 print_error('noaccess','data'); 115 } 116 } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) { 117 // User is trying to create a new record 118 print_error('noaccess','data'); 119 } 120 } 121 122 /// RSS and CSS and JS meta 123 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { 124 $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id))); 125 $rsstitle = $courseshortname . ': ' . format_string($data->name); 126 rss_add_http_header($context, 'mod_data', $data, $rsstitle); 127 } 128 if ($data->csstemplate) { 129 $PAGE->requires->css('/mod/data/css.php?d='.$data->id); 130 } 131 if ($data->jstemplate) { 132 $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true); 133 } 134 135 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id'); 136 137 foreach ($possiblefields as $field) { 138 if ($field->type == 'file' || $field->type == 'picture') { 139 require_once($CFG->dirroot.'/repository/lib.php'); 140 break; 141 } 142 } 143 144 /// Define page variables 145 $strdata = get_string('modulenameplural','data'); 146 147 if ($rid) { 148 $PAGE->navbar->add(get_string('editentry', 'data')); 149 } 150 151 $PAGE->set_title($data->name); 152 $PAGE->set_heading($course->fullname); 153 $PAGE->force_settings_menu(true); 154 $PAGE->set_secondary_active_tab('modulepage'); 155 $PAGE->activityheader->disable(); 156 157 // Process incoming data for adding/updating records. 158 159 // Keep track of any notifications. 160 $generalnotifications = array(); 161 $fieldnotifications = array(); 162 163 // Process the submitted form. 164 if ($datarecord = data_submitted() and confirm_sesskey()) { 165 if ($rid) { 166 // Updating an existing record. 167 168 // Retrieve the format for the fields. 169 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d)); 170 171 // Validate the form to ensure that enough data was submitted. 172 $processeddata = data_process_submission($data, $fields, $datarecord); 173 174 // Add the new notification data. 175 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications); 176 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications); 177 178 if ($processeddata->validated) { 179 // Enough data to update the record. 180 data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata); 181 core_tag_tag::set_item_tags('mod_data', 'data_records', $rid, $context, $tags); 182 183 $viewurl = new moodle_url('/mod/data/view.php', array( 184 'd' => $data->id, 185 'rid' => $rid, 186 )); 187 redirect($viewurl); 188 } 189 190 } else { 191 // No recordid was specified - creating a new entry. 192 193 // Retrieve the format for the fields. 194 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d)); 195 196 // Validate the form to ensure that enough data was submitted. 197 $processeddata = data_process_submission($data, $fields, $datarecord); 198 199 // Add the new notification data. 200 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications); 201 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications); 202 203 // Add instance to data_record. 204 if ($processeddata->validated && $recordid = data_add_record($data, $currentgroup)) { 205 206 // Now populate the fields contents of the new record. 207 data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata); 208 209 core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags); 210 211 if (!empty($datarecord->saveandview)) { 212 $viewurl = new moodle_url('/mod/data/view.php', array( 213 'd' => $data->id, 214 'rid' => $recordid, 215 )); 216 redirect($viewurl); 217 } else if (!empty($datarecord->saveandadd)) { 218 // User has clicked "Save and add another". Reset all of the fields. 219 $datarecord = null; 220 } 221 } 222 } 223 } 224 // End of form processing. 225 226 227 /// Print the page header 228 229 echo $OUTPUT->header(); 230 231 groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id); 232 233 /// Print the browsing interface 234 235 $patterns = array(); //tags to replace 236 $replacement = array(); //html to replace those yucky tags 237 238 //form goes here first in case add template is empty 239 echo '<form enctype="multipart/form-data" action="edit.php" method="post">'; 240 echo '<div>'; 241 echo '<input name="d" value="'.$data->id.'" type="hidden" />'; 242 echo '<input name="rid" value="'.$rid.'" type="hidden" />'; 243 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />'; 244 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide'); 245 246 if (!$rid){ 247 echo $OUTPUT->heading(get_string('newentry','data')); 248 } else { 249 echo $OUTPUT->heading(get_string('editentry','data')); 250 } 251 252 /****************************************** 253 * Regular expression replacement section * 254 ******************************************/ 255 if ($data->addtemplate){ 256 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id'); 257 $patterns = array(); 258 $replacements = array(); 259 260 ///then we generate strings to replace 261 foreach ($possiblefields as $eachfield) { 262 $field = data_get_field($eachfield, $data); 263 // To skip unnecessary calls to display_add_field(). 264 if (strpos($data->addtemplate, "[[" . $field->field->name . "]]") !== false) { 265 // Display an error in case the field type is not found. 266 $errors = ''; 267 if (!empty($fieldnotifications[$field->field->name])) { 268 foreach ($fieldnotifications[$field->field->name] as $notification) { 269 $errors .= $OUTPUT->notification($notification); 270 } 271 } 272 // Replace the field tag. 273 $fielddisplay = ''; 274 if ($field->type === 'unknown') { 275 if (has_capability('mod/data:manageentries', $context)) { 276 // Display notification for users that can manage entries. 277 $errors .= $OUTPUT->notification(get_string('missingfieldtype', 'data', 278 (object)['name' => $field->field->name])); 279 } 280 } else { 281 $fielddisplay = $field->display_add_field($rid, $datarecord); 282 } 283 $patterns[] = "[[" . $field->field->name . "]]"; 284 $replacements[] = $errors . $fielddisplay; 285 } // Replace the field id tag. 286 $patterns[] = "[[" . $field->field->name . "#id]]"; 287 $replacements[] = 'field_' . $field->field->id; 288 } 289 290 if (core_tag_tag::is_enabled('mod_data', 'data_records')) { 291 $patterns[] = "##tags##"; 292 $replacements[] = data_generate_tag_form($rid); 293 } 294 295 $newtext = str_ireplace($patterns, $replacements, $data->{$mode}); 296 297 } else { //if the add template is not yet defined, print the default form! 298 echo data_generate_default_template($data, 'addtemplate', $rid, true, false); 299 $newtext = ''; 300 } 301 302 foreach ($generalnotifications as $notification) { 303 echo $OUTPUT->notification($notification); 304 } 305 echo $newtext; 306 307 $redirectbackto = !empty($redirectbackto) ? $redirectbackto : 308 new \moodle_url('/mod/data/view.php', ['d' => $data->id]); 309 $actionbuttons = html_writer::link($redirectbackto, get_string('cancel'), ['class' => 'btn btn-secondary']); 310 $actionbuttons .= html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'saveandview', 311 'value' => get_string('save'), 'class' => 'btn btn-primary ml-2']); 312 313 if (!$rid && ((!$data->maxentries) || 314 has_capability('mod/data:manageentries', $context) || 315 (data_numentries($data) < ($data->maxentries - 1)))) { 316 $actionbuttons .= html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'saveandadd', 317 'value' => get_string('saveandadd', 'data'), 'class' => 'btn btn-primary ml-2']); 318 } 319 320 echo html_writer::div($actionbuttons, 'mdl-align mt-2'); 321 echo $OUTPUT->box_end(); 322 echo '</div></form>'; 323 324 325 /// Finish the page 326 327 // Print the stuff that need to come after the form fields. 328 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) { 329 print_error('nofieldindatabase', 'data'); 330 } 331 foreach ($fields as $eachfield) { 332 $field = data_get_field($eachfield, $data); 333 $field->print_after_form(); 334 } 335 336 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body