[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/data/ -> edit.php (source)

   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  
  38  
  39  $url = new moodle_url('/mod/data/edit.php');
  40  if ($rid !== 0) {
  41      $record = $DB->get_record('data_records', array(
  42              'id' => $rid,
  43              'dataid' => $d,
  44          ), '*', MUST_EXIST);
  45      $url->param('rid', $rid);
  46  }
  47  if ($cancel !== '') {
  48      $url->param('cancel', $cancel);
  49  }
  50  
  51  if ($id) {
  52      $url->param('id', $id);
  53      $PAGE->set_url($url);
  54      if (! $cm = get_coursemodule_from_id('data', $id)) {
  55          print_error('invalidcoursemodule');
  56      }
  57      if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  58          print_error('coursemisconf');
  59      }
  60      if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
  61          print_error('invalidcoursemodule');
  62      }
  63  
  64  } else {
  65      $url->param('d', $d);
  66      $PAGE->set_url($url);
  67      if (! $data = $DB->get_record('data', array('id'=>$d))) {
  68          print_error('invalidid', 'data');
  69      }
  70      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  71          print_error('coursemisconf');
  72      }
  73      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  74          print_error('invalidcoursemodule');
  75      }
  76  }
  77  
  78  require_login($course, false, $cm);
  79  
  80  if (isguestuser()) {
  81      redirect('view.php?d='.$data->id);
  82  }
  83  
  84  $context = context_module::instance($cm->id);
  85  
  86  /// If it's hidden then it doesn't show anything.  :)
  87  if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
  88      $strdatabases = get_string("modulenameplural", "data");
  89  
  90      $PAGE->set_title($data->name);
  91      $PAGE->set_heading($course->fullname);
  92      echo $OUTPUT->header();
  93      notice(get_string("activityiscurrentlyhidden"));
  94  }
  95  
  96  /// Can't use this if there are no fields
  97  if (has_capability('mod/data:managetemplates', $context)) {
  98      if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
  99          redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
 100      }
 101  }
 102  
 103  if ($rid) {
 104      // When editing an existing record, we require the session key
 105      require_sesskey();
 106  }
 107  
 108  // Get Group information for permission testing and record creation
 109  $currentgroup = groups_get_activity_group($cm);
 110  $groupmode = groups_get_activity_groupmode($cm);
 111  
 112  if (!has_capability('mod/data:manageentries', $context)) {
 113      if ($rid) {
 114          // User is editing an existing record
 115          if (!data_user_can_manage_entry($record, $data, $context)) {
 116              print_error('noaccess','data');
 117          }
 118      } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
 119          // User is trying to create a new record
 120          print_error('noaccess','data');
 121      }
 122  }
 123  
 124  if ($cancel) {
 125      redirect('view.php?d='.$data->id);
 126  }
 127  
 128  
 129  /// RSS and CSS and JS meta
 130  if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
 131      $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
 132      $rsstitle = $courseshortname . ': ' . format_string($data->name);
 133      rss_add_http_header($context, 'mod_data', $data, $rsstitle);
 134  }
 135  if ($data->csstemplate) {
 136      $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
 137  }
 138  if ($data->jstemplate) {
 139      $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
 140  }
 141  
 142  $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
 143  
 144  foreach ($possiblefields as $field) {
 145      if ($field->type == 'file' || $field->type == 'picture') {
 146          require_once($CFG->dirroot.'/repository/lib.php');
 147          break;
 148      }
 149  }
 150  
 151  /// Define page variables
 152  $strdata = get_string('modulenameplural','data');
 153  
 154  if ($rid) {
 155      $PAGE->navbar->add(get_string('editentry', 'data'));
 156  }
 157  
 158  $PAGE->set_title($data->name);
 159  $PAGE->set_heading($course->fullname);
 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  
 185              // Obtain the record to be updated.
 186  
 187              // Reset the approved flag after edit if the user does not have permission to approve their own entries.
 188              if (!has_capability('mod/data:approve', $context)) {
 189                  $record->approved = 0;
 190              }
 191  
 192              // Update the parent record.
 193              $record->timemodified = time();
 194              $DB->update_record('data_records', $record);
 195  
 196              // Update all content.
 197              foreach ($processeddata->fields as $fieldname => $field) {
 198                  $field->update_content($rid, $datarecord->$fieldname, $fieldname);
 199              }
 200  
 201              // Trigger an event for updating this record.
 202              $event = \mod_data\event\record_updated::create(array(
 203                  'objectid' => $rid,
 204                  'context' => $context,
 205                  'courseid' => $course->id,
 206                  'other' => array(
 207                      'dataid' => $data->id
 208                  )
 209              ));
 210              $event->add_record_snapshot('data', $data);
 211              $event->trigger();
 212  
 213              $viewurl = new moodle_url('/mod/data/view.php', array(
 214                  'd' => $data->id,
 215                  'rid' => $rid,
 216              ));
 217              redirect($viewurl);
 218          }
 219  
 220      } else {
 221          // No recordid was specified - creating a new entry.
 222  
 223          // Retrieve the format for the fields.
 224          $fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
 225  
 226          // Validate the form to ensure that enough data was submitted.
 227          $processeddata = data_process_submission($data, $fields, $datarecord);
 228  
 229          // Add the new notification data.
 230          $generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
 231          $fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
 232  
 233          // Add instance to data_record.
 234          if ($processeddata->validated && $recordid = data_add_record($data, $currentgroup)) {
 235  
 236              // Insert a whole lot of empty records to make sure we have them.
 237              $records = array();
 238              foreach ($fields as $field) {
 239                  $content = new stdClass();
 240                  $content->recordid = $recordid;
 241                  $content->fieldid = $field->id;
 242                  $records[] = $content;
 243              }
 244  
 245              // Bulk insert the records now. Some records may have no data but all must exist.
 246              $DB->insert_records('data_content', $records);
 247  
 248              // Add all provided content.
 249              foreach ($processeddata->fields as $fieldname => $field) {
 250                  $field->update_content($recordid, $datarecord->$fieldname, $fieldname);
 251              }
 252  
 253              // Trigger an event for updating this record.
 254              $event = \mod_data\event\record_created::create(array(
 255                  'objectid' => $rid,
 256                  'context' => $context,
 257                  'courseid' => $course->id,
 258                  'other' => array(
 259                      'dataid' => $data->id
 260                  )
 261              ));
 262              $event->add_record_snapshot('data', $data);
 263              $event->trigger();
 264  
 265              if (!empty($datarecord->saveandview)) {
 266                  $viewurl = new moodle_url('/mod/data/view.php', array(
 267                      'd' => $data->id,
 268                      'rid' => $recordid,
 269                  ));
 270                  redirect($viewurl);
 271              } else if (!empty($datarecord->saveandadd)) {
 272                  // User has clicked "Save and add another". Reset all of the fields.
 273                  $datarecord = null;
 274              }
 275          }
 276      }
 277  }
 278  // End of form processing.
 279  
 280  
 281  /// Print the page header
 282  
 283  echo $OUTPUT->header();
 284  echo $OUTPUT->heading(format_string($data->name), 2);
 285  echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 286  groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
 287  
 288  /// Print the tabs
 289  
 290  $currenttab = 'add';
 291  if ($rid) {
 292      $editentry = true;  //used in tabs
 293  }
 294  include ('tabs.php');
 295  
 296  
 297  /// Print the browsing interface
 298  
 299  $patterns = array();    //tags to replace
 300  $replacement = array();    //html to replace those yucky tags
 301  
 302  //form goes here first in case add template is empty
 303  echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
 304  echo '<div>';
 305  echo '<input name="d" value="'.$data->id.'" type="hidden" />';
 306  echo '<input name="rid" value="'.$rid.'" type="hidden" />';
 307  echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
 308  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 309  
 310  if (!$rid){
 311      echo $OUTPUT->heading(get_string('newentry','data'), 3);
 312  }
 313  
 314  /******************************************
 315   * Regular expression replacement section *
 316   ******************************************/
 317  if ($data->addtemplate){
 318      $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
 319      $patterns = array();
 320      $replacements = array();
 321  
 322      ///then we generate strings to replace
 323      foreach ($possiblefields as $eachfield){
 324          $field = data_get_field($eachfield, $data);
 325  
 326          // To skip unnecessary calls to display_add_field().
 327          if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
 328              // Replace the field tag.
 329              $patterns[] = "[[".$field->field->name."]]";
 330              $errors = '';
 331              if (!empty($fieldnotifications[$field->field->name])) {
 332                  foreach ($fieldnotifications[$field->field->name] as $notification) {
 333                      $errors .= $OUTPUT->notification($notification);
 334                  }
 335              }
 336              $replacements[] = $errors . $field->display_add_field($rid, $datarecord);
 337          }
 338  
 339          // Replace the field id tag.
 340          $patterns[] = "[[".$field->field->name."#id]]";
 341          $replacements[] = 'field_'.$field->field->id;
 342      }
 343      $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
 344  
 345  } else {    //if the add template is not yet defined, print the default form!
 346      echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
 347      $newtext = '';
 348  }
 349  
 350  foreach ($generalnotifications as $notification) {
 351      echo $OUTPUT->notification($notification);
 352  }
 353  echo $newtext;
 354  
 355  echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
 356  if ($rid) {
 357      echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
 358  } else {
 359      if ((!$data->maxentries) || has_capability('mod/data:manageentries', $context) || (data_numentries($data) < ($data->maxentries - 1))) {
 360          echo '&nbsp;<input type="submit" name="saveandadd" value="' . get_string('saveandadd', 'data') . '" />';
 361      }
 362  }
 363  echo '</div>';
 364  echo $OUTPUT->box_end();
 365  echo '</div></form>';
 366  
 367  
 368  /// Finish the page
 369  
 370  // Print the stuff that need to come after the form fields.
 371  if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
 372      print_error('nofieldindatabase', 'data');
 373  }
 374  foreach ($fields as $eachfield) {
 375      $field = data_get_field($eachfield, $data);
 376      $field->print_after_form();
 377  }
 378  
 379  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1