Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 $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); 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 if ($cancel !== '') { 47 $url->param('cancel', $cancel); 48 } 49 50 if ($id) { 51 $url->param('id', $id); 52 $PAGE->set_url($url); 53 if (! $cm = get_coursemodule_from_id('data', $id)) { 54 print_error('invalidcoursemodule'); 55 } 56 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) { 57 print_error('coursemisconf'); 58 } 59 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) { 60 print_error('invalidcoursemodule'); 61 } 62 63 } else { 64 $url->param('d', $d); 65 $PAGE->set_url($url); 66 if (! $data = $DB->get_record('data', array('id'=>$d))) { 67 print_error('invalidid', 'data'); 68 } 69 if (! $course = $DB->get_record('course', array('id'=>$data->course))) { 70 print_error('coursemisconf'); 71 } 72 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { 73 print_error('invalidcoursemodule'); 74 } 75 } 76 77 require_login($course, false, $cm); 78 79 if (isguestuser()) { 80 redirect('view.php?d='.$data->id); 81 } 82 83 $context = context_module::instance($cm->id); 84 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"); 88 89 $PAGE->set_title($data->name); 90 $PAGE->set_heading($course->fullname); 91 echo $OUTPUT->header(); 92 notice(get_string("activityiscurrentlyhidden")); 93 } 94 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 99 } 100 } 101 102 if ($rid) { 103 // When editing an existing record, we require the session key 104 require_sesskey(); 105 } 106 107 // Get Group information for permission testing and record creation 108 $currentgroup = groups_get_activity_group($cm); 109 $groupmode = groups_get_activity_groupmode($cm); 110 111 if (!has_capability('mod/data:manageentries', $context)) { 112 if ($rid) { 113 // User is editing an existing record 114 if (!data_user_can_manage_entry($record, $data, $context)) { 115 print_error('noaccess','data'); 116 } 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'); 120 } 121 } 122 123 if ($cancel) { 124 redirect('view.php?d='.$data->id); 125 } 126 127 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); 133 } 134 if ($data->csstemplate) { 135 $PAGE->requires->css('/mod/data/css.php?d='.$data->id); 136 } 137 if ($data->jstemplate) { 138 $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true); 139 } 140 141 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id'); 142 143 foreach ($possiblefields as $field) { 144 if ($field->type == 'file' || $field->type == 'picture') { 145 require_once($CFG->dirroot.'/repository/lib.php'); 146 break; 147 } 148 } 149 150 /// Define page variables 151 $strdata = get_string('modulenameplural','data'); 152 153 if ($rid) { 154 $PAGE->navbar->add(get_string('editentry', 'data')); 155 } 156 157 $PAGE->set_title($data->name); 158 $PAGE->set_heading($course->fullname); 159 $PAGE->force_settings_menu(true); 160 161 // Process incoming data for adding/updating records. 162 163 // Keep track of any notifications. 164 $generalnotifications = array(); 165 $fieldnotifications = array(); 166 167 // Process the submitted form. 168 if ($datarecord = data_submitted() and confirm_sesskey()) { 169 if ($rid) { 170 // Updating an existing record. 171 172 // Retrieve the format for the fields. 173 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d)); 174 175 // Validate the form to ensure that enough data was submitted. 176 $processeddata = data_process_submission($data, $fields, $datarecord); 177 178 // Add the new notification data. 179 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications); 180 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications); 181 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); 186 187 $viewurl = new moodle_url('/mod/data/view.php', array( 188 'd' => $data->id, 189 'rid' => $rid, 190 )); 191 redirect($viewurl); 192 } 193 194 } else { 195 // No recordid was specified - creating a new entry. 196 197 // Retrieve the format for the fields. 198 $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d)); 199 200 // Validate the form to ensure that enough data was submitted. 201 $processeddata = data_process_submission($data, $fields, $datarecord); 202 203 // Add the new notification data. 204 $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications); 205 $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications); 206 207 // Add instance to data_record. 208 if ($processeddata->validated && $recordid = data_add_record($data, $currentgroup)) { 209 210 // Now populate the fields contents of the new record. 211 data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata); 212 213 core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags); 214 215 if (!empty($datarecord->saveandview)) { 216 $viewurl = new moodle_url('/mod/data/view.php', array( 217 'd' => $data->id, 218 'rid' => $recordid, 219 )); 220 redirect($viewurl); 221 } else if (!empty($datarecord->saveandadd)) { 222 // User has clicked "Save and add another". Reset all of the fields. 223 $datarecord = null; 224 } 225 } 226 } 227 } 228 // End of form processing. 229 230 231 /// Print the page header 232 233 echo $OUTPUT->header(); 234 echo $OUTPUT->heading(format_string($data->name), 2); 235 236 // Render the activity information. 237 $cminfo = cm_info::create($cm); 238 $completiondetails = \core_completion\cm_completion_details::get_instance($cminfo, $USER->id); 239 $activitydates = \core\activity_dates::get_dates_for_module($cminfo, $USER->id); 240 echo $OUTPUT->activity_information($cminfo, $completiondetails, $activitydates); 241 242 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro'); 243 groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id); 244 245 /// Print the tabs 246 247 $currenttab = 'add'; 248 if ($rid) { 249 $editentry = true; //used in tabs 250 } 251 include ('tabs.php'); 252 253 254 /// Print the browsing interface 255 256 $patterns = array(); //tags to replace 257 $replacement = array(); //html to replace those yucky tags 258 259 //form goes here first in case add template is empty 260 echo '<form enctype="multipart/form-data" action="edit.php" method="post">'; 261 echo '<div>'; 262 echo '<input name="d" value="'.$data->id.'" type="hidden" />'; 263 echo '<input name="rid" value="'.$rid.'" type="hidden" />'; 264 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />'; 265 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide'); 266 267 if (!$rid){ 268 echo $OUTPUT->heading(get_string('newentry','data'), 3); 269 } 270 271 /****************************************** 272 * Regular expression replacement section * 273 ******************************************/ 274 if ($data->addtemplate){ 275 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id'); 276 $patterns = array(); 277 $replacements = array(); 278 279 ///then we generate strings to replace 280 foreach ($possiblefields as $eachfield) { 281 $field = data_get_field($eachfield, $data); 282 283 // To skip unnecessary calls to display_add_field(). 284 if (strpos($data->addtemplate, "[[" . $field->field->name . "]]") !== false) { 285 // Display an error in case the field type is not found. 286 $errors = ''; 287 if (!empty($fieldnotifications[$field->field->name])) { 288 foreach ($fieldnotifications[$field->field->name] as $notification) { 289 $errors .= $OUTPUT->notification($notification); 290 } 291 } 292 293 // Replace the field tag. 294 $fielddisplay = ''; 295 if ($field->type === 'unknown') { 296 if (has_capability('mod/data:manageentries', $context)) { 297 // Display notification for users that can manage entries. 298 $errors .= $OUTPUT->notification(get_string('missingfieldtype', 'data', 299 (object)['name' => $field->field->name])); 300 } 301 } else { 302 $fielddisplay = $field->display_add_field($rid, $datarecord); 303 } 304 305 $patterns[] = "[[" . $field->field->name . "]]"; 306 $replacements[] = $errors . $fielddisplay; 307 } // Replace the field id tag. 308 $patterns[] = "[[" . $field->field->name . "#id]]"; 309 $replacements[] = 'field_' . $field->field->id; 310 } 311 312 if (core_tag_tag::is_enabled('mod_data', 'data_records')) { 313 $patterns[] = "##tags##"; 314 $replacements[] = data_generate_tag_form($rid); 315 } 316 317 $newtext = str_ireplace($patterns, $replacements, $data->{$mode}); 318 319 } else { //if the add template is not yet defined, print the default form! 320 echo data_generate_default_template($data, 'addtemplate', $rid, true, false); 321 $newtext = ''; 322 } 323 324 foreach ($generalnotifications as $notification) { 325 echo $OUTPUT->notification($notification); 326 } 327 echo $newtext; 328 329 echo '<div class="mdl-align mt-1"><input type="submit" class="btn btn-primary" name="saveandview" ' . 330 'value="' . get_string('saveandview', 'data') . '" />'; 331 if ($rid) { 332 echo ' <input type="submit" class="btn btn-primary" name="cancel" ' . 333 'value="' . get_string('cancel') . '" onclick="javascript:history.go(-1)" />'; 334 } else { 335 if ((!$data->maxentries) || 336 has_capability('mod/data:manageentries', $context) || 337 (data_numentries($data) < ($data->maxentries - 1))) { 338 echo ' <input type="submit" class="btn btn-primary" name="saveandadd" ' . 339 'value="' . get_string('saveandadd', 'data') . '" />'; 340 } 341 } 342 echo '</div>'; 343 echo $OUTPUT->box_end(); 344 echo '</div></form>'; 345 346 347 /// Finish the page 348 349 // Print the stuff that need to come after the form fields. 350 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) { 351 print_error('nofieldindatabase', 'data'); 352 } 353 foreach ($fields as $eachfield) { 354 $field = data_get_field($eachfield, $data); 355 $field->print_after_form(); 356 } 357 358 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body