Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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/ -> field.php (source)

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 ('lib.php');
  28  
  29  $id             = optional_param('id', 0, PARAM_INT);            // course module id
  30  $d              = optional_param('d', 0, PARAM_INT);             // database id
  31  $fid            = optional_param('fid', 0 , PARAM_INT);          // update field id
  32  $newtype        = optional_param('newtype','',PARAM_ALPHA);      // type of the new field
  33  $mode           = optional_param('mode','',PARAM_ALPHA);
  34  $defaultsort    = optional_param('defaultsort', 0, PARAM_INT);
  35  $defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT);
  36  $cancel         = optional_param('cancel', 0, PARAM_BOOL);
  37  
  38  if ($cancel) {
  39      $mode = 'list';
  40  }
  41  
  42  $url = new moodle_url('/mod/data/field.php');
  43  if ($fid !== 0) {
  44      $url->param('fid', $fid);
  45  }
  46  if ($newtype !== '') {
  47      $url->param('newtype', $newtype);
  48  }
  49  if ($mode !== '') {
  50      $url->param('mode', $mode);
  51  }
  52  if ($defaultsort !== 0) {
  53      $url->param('defaultsort', $defaultsort);
  54  }
  55  if ($defaultsortdir !== 0) {
  56      $url->param('defaultsortdir', $defaultsortdir);
  57  }
  58  if ($cancel !== 0) {
  59      $url->param('cancel', $cancel);
  60  }
  61  
  62  if ($id) {
  63      $url->param('id', $id);
  64      $PAGE->set_url($url);
  65      if (! $cm = get_coursemodule_from_id('data', $id)) {
  66          print_error('invalidcoursemodule');
  67      }
  68      if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  69          print_error('coursemisconf');
  70      }
  71      if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
  72          print_error('invalidcoursemodule');
  73      }
  74  
  75  } else {
  76      $url->param('d', $d);
  77      $PAGE->set_url($url);
  78      if (! $data = $DB->get_record('data', array('id'=>$d))) {
  79          print_error('invalidid', 'data');
  80      }
  81      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  82          print_error('invalidcoursemodule');
  83      }
  84      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  85          print_error('invalidcoursemodule');
  86      }
  87  }
  88  
  89  require_login($course, true, $cm);
  90  
  91  $context = context_module::instance($cm->id);
  92  require_capability('mod/data:managetemplates', $context);
  93  
  94  /************************************
  95   *        Data Processing           *
  96   ***********************************/
  97  switch ($mode) {
  98  
  99      case 'add':    ///add a new field
 100          if (confirm_sesskey() and $fieldinput = data_submitted()){
 101  
 102              //$fieldinput->name = data_clean_field_name($fieldinput->name);
 103  
 104          /// Only store this new field if it doesn't already exist.
 105              if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id)) {
 106  
 107                  $displaynoticebad = get_string('invalidfieldname','data');
 108  
 109              } else {
 110  
 111              /// Check for arrays and convert to a comma-delimited string
 112                  data_convert_arrays_to_strings($fieldinput);
 113  
 114              /// Create a field object to collect and store the data safely
 115                  $type = required_param('type', PARAM_FILE);
 116                  $field = data_get_field_new($type, $data);
 117  
 118                  $field->define_field($fieldinput);
 119                  $field->insert_field();
 120  
 121              /// Update some templates
 122                  data_append_new_field_to_templates($data, $fieldinput->name);
 123  
 124                  $displaynoticegood = get_string('fieldadded','data');
 125              }
 126          }
 127          break;
 128  
 129  
 130      case 'update':    ///update a field
 131          if (confirm_sesskey() and $fieldinput = data_submitted()){
 132  
 133              //$fieldinput->name = data_clean_field_name($fieldinput->name);
 134  
 135              if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id, $fieldinput->fid)) {
 136  
 137                  $displaynoticebad = get_string('invalidfieldname','data');
 138  
 139              } else {
 140              /// Check for arrays and convert to a comma-delimited string
 141                  data_convert_arrays_to_strings($fieldinput);
 142  
 143              /// Create a field object to collect and store the data safely
 144                  $field = data_get_field_from_id($fid, $data);
 145                  $oldfieldname = $field->field->name;
 146  
 147                  $field->field->name = $fieldinput->name;
 148                  $field->field->description = $fieldinput->description;
 149                  $field->field->required = !empty($fieldinput->required) ? 1 : 0;
 150  
 151                  for ($i=1; $i<=10; $i++) {
 152                      if (isset($fieldinput->{'param'.$i})) {
 153                          $field->field->{'param'.$i} = $fieldinput->{'param'.$i};
 154                      } else {
 155                          $field->field->{'param'.$i} = '';
 156                      }
 157                  }
 158  
 159                  $field->update_field();
 160  
 161              /// Update the templates.
 162                  data_replace_field_in_templates($data, $oldfieldname, $field->field->name);
 163  
 164                  $displaynoticegood = get_string('fieldupdated','data');
 165              }
 166          }
 167          break;
 168  
 169  
 170      case 'delete':    // Delete a field
 171          if (confirm_sesskey()){
 172  
 173              if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
 174  
 175  
 176                  // Delete the field completely
 177                  if ($field = data_get_field_from_id($fid, $data)) {
 178                      $field->delete_field();
 179  
 180                      // Update the templates.
 181                      data_replace_field_in_templates($data, $field->field->name, '');
 182  
 183                      // Update the default sort field
 184                      if ($fid == $data->defaultsort) {
 185                          $rec = new stdClass();
 186                          $rec->id = $data->id;
 187                          $rec->defaultsort = 0;
 188                          $rec->defaultsortdir = 0;
 189                          $DB->update_record('data', $rec);
 190                      }
 191  
 192                      $displaynoticegood = get_string('fielddeleted', 'data');
 193                  }
 194  
 195              } else {
 196  
 197                  data_print_header($course,$cm,$data, false);
 198  
 199                  // Print confirmation message.
 200                  $field = data_get_field_from_id($fid, $data);
 201  
 202                  echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
 203                               'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
 204                               'field.php?d='.$data->id);
 205  
 206                  echo $OUTPUT->footer();
 207                  exit;
 208              }
 209          }
 210          break;
 211  
 212  
 213      case 'sort':    // Set the default sort parameters
 214          if (confirm_sesskey()) {
 215              $rec = new stdClass();
 216              $rec->id = $data->id;
 217              $rec->defaultsort = $defaultsort;
 218              $rec->defaultsortdir = $defaultsortdir;
 219  
 220              $DB->update_record('data', $rec);
 221              redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
 222              exit;
 223          }
 224          break;
 225  
 226      default:
 227          break;
 228  }
 229  
 230  
 231  
 232  /// Print the browsing interface
 233  
 234  ///get the list of possible fields (plugins)
 235  $plugins = core_component::get_plugin_list('datafield');
 236  $menufield = array();
 237  
 238  foreach ($plugins as $plugin=>$fulldir){
 239      $menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin);    //get from language files
 240  }
 241  asort($menufield);    //sort in alphabetical order
 242  $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
 243  $PAGE->set_heading($course->fullname);
 244  $PAGE->force_settings_menu(true);
 245  
 246  $PAGE->set_pagetype('mod-data-field-' . $newtype);
 247  if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) {          ///  Adding a new field
 248      data_print_header($course, $cm, $data,'fields');
 249  
 250      $field = data_get_field_new($newtype, $data);
 251      $field->display_edit_field();
 252  
 253  } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
 254      data_print_header($course, $cm, $data,'fields');
 255  
 256      $field = data_get_field_from_id($fid, $data);
 257      $field->display_edit_field();
 258  
 259  } else {                                              /// Display the main listing of all fields
 260      data_print_header($course, $cm, $data,'fields');
 261  
 262      if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {
 263          echo $OUTPUT->notification(get_string('nofieldindatabase','data'));  // nothing in database
 264          echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id));      // link to presets
 265  
 266      } else {    //else print quiz style list of fields
 267  
 268          $table = new html_table();
 269          $table->head = array(
 270              get_string('fieldname', 'data'),
 271              get_string('type', 'data'),
 272              get_string('required', 'data'),
 273              get_string('fielddescription', 'data'),
 274              get_string('action', 'data'),
 275          );
 276          $table->align = array('left', 'left', 'left', 'left');
 277          $table->wrap = array(false,false,false,false);
 278  
 279          if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
 280              foreach ($fff as $ff) {
 281  
 282                  $field = data_get_field($ff, $data);
 283  
 284                  $baseurl = new moodle_url('/mod/data/field.php', array(
 285                      'd'         => $data->id,
 286                      'fid'       => $field->field->id,
 287                      'sesskey'   => sesskey(),
 288                  ));
 289  
 290                  $displayurl = new moodle_url($baseurl, array(
 291                      'mode'      => 'display',
 292                  ));
 293  
 294                  $deleteurl = new moodle_url($baseurl, array(
 295                      'mode'      => 'delete',
 296                  ));
 297  
 298                  $table->data[] = array(
 299                      html_writer::link($displayurl, $field->field->name),
 300                      $field->image() . '&nbsp;' . $field->name(),
 301                      $field->field->required ? get_string('yes') : get_string('no'),
 302                      shorten_text($field->field->description, 30),
 303                      html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) .
 304                          '&nbsp;' .
 305                          html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))),
 306                  );
 307              }
 308          }
 309          echo html_writer::table($table);
 310      }
 311  
 312  
 313      echo '<div class="fieldadd">';
 314      $popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='.  sesskey();
 315      echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array('' => 'choosedots'),
 316          'fieldform', array('label' => get_string('newfield', 'data')));
 317      echo $OUTPUT->help_icon('newfield', 'data');
 318      echo '</div>';
 319  
 320      echo '<div class="sortdefault">';
 321      echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
 322      echo '<div>';
 323      echo '<input type="hidden" name="d" value="'.$data->id.'" />';
 324      echo '<input type="hidden" name="mode" value="sort" />';
 325      echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 326      echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
 327      echo '<select id="defaultsort" name="defaultsort" class="custom-select">';
 328      if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
 329          echo '<optgroup label="'.get_string('fields', 'data').'">';
 330          foreach ($fields as $field) {
 331              if ($data->defaultsort == $field->id) {
 332                  echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
 333              } else {
 334                  echo '<option value="'.$field->id.'">'.$field->name.'</option>';
 335              }
 336          }
 337          echo '</optgroup>';
 338      }
 339      $options = array();
 340      $options[DATA_TIMEADDED]    = get_string('timeadded', 'data');
 341  // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
 342  /*        $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
 343      $options[DATA_FIRSTNAME]    = get_string('authorfirstname', 'data');
 344      $options[DATA_LASTNAME]     = get_string('authorlastname', 'data');
 345      if ($data->approval and has_capability('mod/data:approve', $context)) {
 346          $options[DATA_APPROVED] = get_string('approved', 'data');
 347      }*/
 348      echo '<optgroup label="'.get_string('other', 'data').'">';
 349      foreach ($options as $key => $name) {
 350          if ($data->defaultsort == $key) {
 351              echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
 352          } else {
 353              echo '<option value="'.$key.'">'.$name.'</option>';
 354          }
 355      }
 356      echo '</optgroup>';
 357      echo '</select>';
 358  
 359      $options = array(0 => get_string('ascending', 'data'),
 360                       1 => get_string('descending', 'data'));
 361      echo html_writer::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
 362      echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false, array('class' => 'custom-select'));
 363      echo '<input type="submit" class="btn btn-secondary ml-1" value="'.get_string('save', 'data').'" />';
 364      echo '</div>';
 365      echo '</form>';
 366      echo '</div>';
 367  
 368  }
 369  
 370  /// Finish the page
 371  echo $OUTPUT->footer();
 372