Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/mod/data/ -> edit.php (source)

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  $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  echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 236  groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
 237  
 238  /// Print the tabs
 239  
 240  $currenttab = 'add';
 241  if ($rid) {
 242      $editentry = true;  //used in tabs
 243  }
 244  include ('tabs.php');
 245  
 246  
 247  /// Print the browsing interface
 248  
 249  $patterns = array();    //tags to replace
 250  $replacement = array();    //html to replace those yucky tags
 251  
 252  //form goes here first in case add template is empty
 253  echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
 254  echo '<div>';
 255  echo '<input name="d" value="'.$data->id.'" type="hidden" />';
 256  echo '<input name="rid" value="'.$rid.'" type="hidden" />';
 257  echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
 258  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 259  
 260  if (!$rid){
 261      echo $OUTPUT->heading(get_string('newentry','data'), 3);
 262  }
 263  
 264  /******************************************
 265   * Regular expression replacement section *
 266   ******************************************/
 267  if ($data->addtemplate){
 268      $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
 269      $patterns = array();
 270      $replacements = array();
 271  
 272      ///then we generate strings to replace
 273      foreach ($possiblefields as $eachfield){
 274          $field = data_get_field($eachfield, $data);
 275  
 276          // To skip unnecessary calls to display_add_field().
 277          if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
 278              // Replace the field tag.
 279              $patterns[] = "[[".$field->field->name."]]";
 280              $errors = '';
 281              if (!empty($fieldnotifications[$field->field->name])) {
 282                  foreach ($fieldnotifications[$field->field->name] as $notification) {
 283                      $errors .= $OUTPUT->notification($notification);
 284                  }
 285              }
 286              $replacements[] = $errors . $field->display_add_field($rid, $datarecord);
 287          }
 288  
 289          // Replace the field id tag.
 290          $patterns[] = "[[".$field->field->name."#id]]";
 291          $replacements[] = 'field_'.$field->field->id;
 292      }
 293  
 294      if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 295          $patterns[] = "##tags##";
 296          $replacements[] = data_generate_tag_form($rid);
 297      }
 298  
 299      $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
 300  
 301  } else {    //if the add template is not yet defined, print the default form!
 302      echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
 303      $newtext = '';
 304  }
 305  
 306  foreach ($generalnotifications as $notification) {
 307      echo $OUTPUT->notification($notification);
 308  }
 309  echo $newtext;
 310  
 311  echo '<div class="mdl-align mt-1"><input type="submit" class="btn btn-primary" name="saveandview" ' .
 312       'value="' . get_string('saveandview', 'data') . '" />';
 313  if ($rid) {
 314      echo '&nbsp;<input type="submit" class="btn btn-primary" name="cancel" ' .
 315           'value="' . get_string('cancel') . '" onclick="javascript:history.go(-1)" />';
 316  } else {
 317      if ((!$data->maxentries) ||
 318              has_capability('mod/data:manageentries', $context) ||
 319              (data_numentries($data) < ($data->maxentries - 1))) {
 320          echo '&nbsp;<input type="submit" class="btn btn-primary" name="saveandadd" ' .
 321               'value="' . get_string('saveandadd', 'data') . '" />';
 322      }
 323  }
 324  echo '</div>';
 325  echo $OUTPUT->box_end();
 326  echo '</div></form>';
 327  
 328  
 329  /// Finish the page
 330  
 331  // Print the stuff that need to come after the form fields.
 332  if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
 333      print_error('nofieldindatabase', 'data');
 334  }
 335  foreach ($fields as $eachfield) {
 336      $field = data_get_field($eachfield, $data);
 337      $field->print_after_form();
 338  }
 339  
 340  echo $OUTPUT->footer();