[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 ('lib.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 37 $url = new moodle_url('/mod/data/edit.php'); 38 if ($rid !== 0) { 39 $url->param('rid', $rid); 40 } 41 if ($cancel !== '') { 42 $url->param('cancel', $cancel); 43 } 44 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'); 50 } 51 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) { 52 print_error('coursemisconf'); 53 } 54 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) { 55 print_error('invalidcoursemodule'); 56 } 57 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'); 63 } 64 if (! $course = $DB->get_record('course', array('id'=>$data->course))) { 65 print_error('coursemisconf'); 66 } 67 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { 68 print_error('invalidcoursemodule'); 69 } 70 } 71 72 require_login($course, false, $cm); 73 74 if (isguestuser()) { 75 redirect('view.php?d='.$data->id); 76 } 77 78 $context = context_module::instance($cm->id); 79 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"); 83 84 $PAGE->set_title($data->name); 85 $PAGE->set_heading($course->fullname); 86 echo $OUTPUT->header(); 87 notice(get_string("activityiscurrentlyhidden")); 88 } 89 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 94 } 95 } 96 97 if ($rid) { 98 // When editing an existing record, we require the session key 99 require_sesskey(); 100 } 101 102 // Get Group information for permission testing and record creation 103 $currentgroup = groups_get_activity_group($cm); 104 $groupmode = groups_get_activity_groupmode($cm); 105 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'); 111 } 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'); 115 } 116 } 117 118 if ($cancel) { 119 redirect('view.php?d='.$data->id); 120 } 121 122 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); 128 } 129 if ($data->csstemplate) { 130 $PAGE->requires->css('/mod/data/css.php?d='.$data->id); 131 } 132 if ($data->jstemplate) { 133 $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true); 134 } 135 136 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id'); 137 138 foreach ($possiblefields as $field) { 139 if ($field->type == 'file' || $field->type == 'picture') { 140 require_once($CFG->dirroot.'/repository/lib.php'); 141 break; 142 } 143 } 144 145 /// Define page variables 146 $strdata = get_string('modulenameplural','data'); 147 148 if ($rid) { 149 $PAGE->navbar->add(get_string('editentry', 'data')); 150 } 151 152 $PAGE->set_title($data->name); 153 $PAGE->set_heading($course->fullname); 154 155 /// Process incoming data for adding/updating records 156 157 if ($datarecord = data_submitted() and confirm_sesskey()) { 158 159 $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data 160 161 if ($rid) { /// Update some records 162 163 /// All student edits are marked unapproved by default 164 $record = $DB->get_record('data_records', array('id'=>$rid)); 165 166 /// reset approved flag after student edit 167 if (!has_capability('mod/data:approve', $context)) { 168 $record->approved = 0; 169 } 170 171 $record->timemodified = time(); 172 $DB->update_record('data_records', $record); 173 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); 181 } 182 if ($field) { 183 $field->update_content($rid, $value, $name); 184 } 185 } 186 } 187 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 195 ) 196 )); 197 $event->add_record_snapshot('data', $data); 198 $event->trigger(); 199 200 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid); 201 202 } else { /// Add some new records 203 ///Empty form checking - you can't submit an empty form! 204 205 $emptyform = true; // assume the worst 206 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); 212 } 213 if ($field->notemptyfield($value, $name)) { 214 $emptyform = false; 215 break; // if anything has content, this form is not empty, so stop now! 216 } 217 } 218 } 219 220 if ($emptyform){ //nothing gets written to database 221 echo $OUTPUT->notification(get_string('emptyaddform','data')); 222 } 223 224 if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record 225 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); 233 } 234 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); 241 } 242 if ($field) { 243 $field->update_content($recordid, $value, $name); 244 } 245 } 246 } 247 248 if (!empty($datarecord->saveandview)) { 249 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid); 250 } 251 } 252 } 253 } // End of form processing 254 255 256 /// Print the page header 257 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); 262 263 /// Print the tabs 264 265 $currenttab = 'add'; 266 if ($rid) { 267 $editentry = true; //used in tabs 268 } 269 include ('tabs.php'); 270 271 272 /// Print the browsing interface 273 274 $patterns = array(); //tags to replace 275 $replacement = array(); //html to replace those yucky tags 276 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'); 284 285 if (!$rid){ 286 echo $OUTPUT->heading(get_string('newentry','data'), 3); 287 } 288 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(); 296 297 ///then we generate strings to replace 298 foreach ($possiblefields as $eachfield){ 299 $field = data_get_field($eachfield, $data); 300 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); 305 } 306 $patterns[] = "[[".$field->field->name."#id]]"; 307 $replacements[] = 'field_'.$field->field->id; 308 } 309 $newtext = str_ireplace($patterns, $replacements, $data->{$mode}); 310 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 = ''; 314 } 315 316 echo $newtext; 317 318 echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />'; 319 if ($rid) { 320 echo ' <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 ' <input type="submit" value="'.get_string('saveandadd','data').'" />'; 324 } 325 } 326 echo '</div>'; 327 echo $OUTPUT->box_end(); 328 echo '</div></form>'; 329 330 331 /// Finish the page 332 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'); 336 } 337 foreach ($fields as $eachfield) { 338 $field = data_get_field($eachfield, $data); 339 $field->print_after_form(); 340 } 341 342 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |