Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/mod/data/ -> edit.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  use mod_data\manager;
  27  
  28  require_once('../../config.php');
  29  require_once ('locallib.php');
  30  require_once("$CFG->libdir/rsslib.php");
  31  require_once("$CFG->libdir/form/filemanager.php");
  32  
  33  $id = optional_param('id', 0, PARAM_INT); // Course module id.
  34  $d = optional_param('d', 0, PARAM_INT); // Database id.
  35  $rid = optional_param('rid', 0, PARAM_INT); // Record id.
  36  $mode = 'addtemplate'; // Define the mode for this page, only 1 mode available.
  37  $tags = optional_param_array('tags', [], PARAM_TAGLIST);
  38  $redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back.
  39  
  40  $url = new moodle_url('/mod/data/edit.php');
  41  
  42  $record = null;
  43  
  44  if ($id) {
  45      list($course, $cm) = get_course_and_cm_from_cmid($id, manager::MODULE);
  46      $manager = manager::create_from_coursemodule($cm);
  47  } else {   // We must have $d.
  48      $data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
  49      $manager = manager::create_from_instance($data);
  50      $cm = $manager->get_coursemodule();
  51      $course = get_course($cm->course);
  52  }
  53  $data = $manager->get_instance();
  54  $context = $manager->get_context();
  55  $url->param('id', $cm->id);
  56  
  57  if ($rid !== 0) {
  58      $record = $DB->get_record(
  59          'data_records',
  60          ['id' => $rid, 'dataid' => $data->id],
  61          '*',
  62          MUST_EXIST
  63      );
  64      $url->param('rid', $rid);
  65  }
  66  
  67  $PAGE->set_url($url);
  68  require_login($course, false, $cm);
  69  
  70  $url->param('backto', $redirectbackto);
  71  
  72  if (isguestuser()) {
  73      redirect('view.php?d='.$data->id);
  74  }
  75  
  76  /// Can't use this if there are no fields
  77  if ($manager->can_manage_templates()) {
  78      if (!$manager->has_fields()) {
  79          redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry.
  80      }
  81  }
  82  
  83  if ($rid) {
  84      // When editing an existing record, we require the session key.
  85      require_sesskey();
  86  }
  87  
  88  // Get Group information for permission testing and record creation.
  89  $currentgroup = groups_get_activity_group($cm);
  90  $groupmode = groups_get_activity_groupmode($cm);
  91  
  92  if (!has_capability('mod/data:manageentries', $context)) {
  93      if ($rid) {
  94          // User is editing an existing record.
  95          if (!data_user_can_manage_entry($record, $data, $context)) {
  96              throw new \moodle_exception('noaccess', 'data');
  97          }
  98      } else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
  99          // User is trying to create a new record.
 100          throw new \moodle_exception('noaccess', 'data');
 101      }
 102  }
 103  
 104  // RSS and CSS and JS meta.
 105  if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
 106      $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
 107      $rsstitle = $courseshortname . ': ' . format_string($data->name);
 108      rss_add_http_header($context, 'mod_data', $data, $rsstitle);
 109  }
 110  if ($data->csstemplate) {
 111      $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
 112  }
 113  if ($data->jstemplate) {
 114      $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
 115  }
 116  
 117  // Define page variables.
 118  $strdata = get_string('modulenameplural','data');
 119  
 120  if ($rid) {
 121      $PAGE->navbar->add(get_string('editentry', 'data'));
 122  }
 123  
 124  $PAGE->add_body_class('mediumwidth');
 125  if ($rid) {
 126      $pagename = get_string('editentry', 'data');
 127  } else {
 128      $pagename = get_string('newentry', 'data');
 129  }
 130  $PAGE->navbar->add($pagename);
 131  $titleparts = [
 132      $pagename,
 133      format_string($data->name),
 134      format_string($course->fullname),
 135  ];
 136  $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
 137  $PAGE->force_settings_menu(true);
 138  $PAGE->set_secondary_active_tab('modulepage');
 139  $PAGE->activityheader->disable();
 140  
 141  // Process incoming data for adding/updating records.
 142  
 143  // Keep track of any notifications ad submitted data.
 144  $processeddata = null;
 145  $datarecord = data_submitted() ?: null;
 146  
 147  // Process the submitted form.
 148  if ($datarecord && confirm_sesskey()) {
 149      // Validate the form to ensure that enough data was submitted.
 150      $fields = $manager->get_field_records();
 151      $processeddata = data_process_submission($data, $fields, $datarecord);
 152  
 153      if ($processeddata->validated) {
 154          if ($rid) {
 155              $recordid = $rid;
 156              // Updating an existing record.
 157              data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata);
 158          } else {
 159              // Add instance to data_record.
 160              $recordid = data_add_record($data, $currentgroup);
 161              if ($recordid) {
 162                  // Now populate the fields contents of the new record.
 163                  data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
 164              }
 165          }
 166  
 167          if ($recordid) {
 168              core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags);
 169  
 170              if (!empty($datarecord->saveandadd)) {
 171                  // User has clicked "Save and add another". Reset all of the fields.
 172                  $datarecord = null;
 173              } else {
 174                  $viewurl = new moodle_url('/mod/data/view.php', [
 175                      'd' => $data->id,
 176                      'rid' => $recordid,
 177                  ]);
 178                  redirect($viewurl);
 179              }
 180          }
 181      }
 182  }
 183  // End of form processing.
 184  
 185  echo $OUTPUT->header();
 186  
 187  groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
 188  
 189  // Form goes here first in case add template is empty.
 190  echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
 191  echo '<div>';
 192  echo '<input name="d" value="'.$data->id.'" type="hidden" />';
 193  echo '<input name="rid" value="'.$rid.'" type="hidden" />';
 194  echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
 195  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 196  
 197  echo $OUTPUT->heading($pagename);
 198  
 199  $template = $manager->get_template($mode);
 200  echo $template->parse_add_entry($processeddata, $rid, $datarecord);
 201  
 202  if (empty($redirectbackto)) {
 203      $redirectbackto = new \moodle_url('/mod/data/view.php', ['id' => $cm->id]);
 204  }
 205  
 206  $actionbuttons = html_writer::link(
 207      $redirectbackto,
 208      get_string('cancel'),
 209      ['class' => 'btn btn-secondary mx-1', 'role' => 'button']
 210  );
 211  $actionbuttons .= html_writer::empty_tag('input', [
 212      'type' => 'submit',
 213      'name' => 'saveandview',
 214      'value' => get_string('save'),
 215      'class' => 'btn btn-primary mx-1'
 216  ]);
 217  
 218  if (!$rid && ((!$data->maxentries) ||
 219      has_capability('mod/data:manageentries', $context) ||
 220      (data_numentries($data) < ($data->maxentries - 1)))) {
 221      $actionbuttons .= html_writer::empty_tag('input', [
 222          'type' => 'submit', 'name' => 'saveandadd',
 223          'value' => get_string('saveandadd', 'data'), 'class' => 'btn btn-primary mx-1'
 224      ]);
 225  }
 226  
 227  $stickyfooter = new core\output\sticky_footer($actionbuttons);
 228  echo $OUTPUT->render($stickyfooter);
 229  
 230  echo $OUTPUT->box_end();
 231  echo '</div></form>';
 232  
 233  $possiblefields = $manager->get_fields();
 234  foreach ($possiblefields as $field) {
 235      $field->print_after_form();
 236  }
 237  
 238  // Finish the page.
 239  if (empty($possiblefields)) {
 240      throw new \moodle_exception('nofieldindatabase', 'data');
 241  }
 242  echo $OUTPUT->footer();