Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/mod/data/ -> field.php (source)

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

   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                  if ($field->type === 'unknown') {
 203                      $fieldtypename = get_string('unknown', 'data');
 204                  } else {
 205                      $fieldtypename = $field->name();
 206                  }
 207                  echo $OUTPUT->confirm('<strong>'.$fieldtypename.': '.$field->field->name.'</strong><br /><br />'.
 208                              get_string('confirmdeletefield', 'data'),
 209                              'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
 210                              'field.php?d='.$data->id);
 211  
 212                  echo $OUTPUT->footer();
 213                  exit;
 214              }
 215          }
 216          break;
 217  
 218  
 219      case 'sort':    // Set the default sort parameters
 220          if (confirm_sesskey()) {
 221              $rec = new stdClass();
 222              $rec->id = $data->id;
 223              $rec->defaultsort = $defaultsort;
 224              $rec->defaultsortdir = $defaultsortdir;
 225  
 226              $DB->update_record('data', $rec);
 227              redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
 228              exit;
 229          }
 230          break;
 231  
 232      default:
 233          break;
 234  }
 235  
 236  
 237  
 238  /// Print the browsing interface
 239  
 240  ///get the list of possible fields (plugins)
 241  $plugins = core_component::get_plugin_list('datafield');
 242  $menufield = array();
 243  
 244  foreach ($plugins as $plugin=>$fulldir){
 245      if (!is_dir($fulldir)) {
 246          continue;
 247      }
 248      $menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin);    //get from language files
 249  }
 250  asort($menufield);    //sort in alphabetical order
 251  $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
 252  $PAGE->set_heading($course->fullname);
 253  $PAGE->force_settings_menu(true);
 254  
 255  $PAGE->set_pagetype('mod-data-field-' . $newtype);
 256  if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) {          ///  Adding a new field
 257      data_print_header($course, $cm, $data,'fields');
 258  
 259      $field = data_get_field_new($newtype, $data);
 260      $field->display_edit_field();
 261  
 262  } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
 263      data_print_header($course, $cm, $data,'fields');
 264  
 265      $field = data_get_field_from_id($fid, $data);
 266      $field->display_edit_field();
 267  
 268  } else {                                              /// Display the main listing of all fields
 269      data_print_header($course, $cm, $data,'fields');
 270  
 271      if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {
 272          echo $OUTPUT->notification(get_string('nofieldindatabase','data'));  // nothing in database
 273          echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id));      // link to presets
 274  
 275      } else {    //else print quiz style list of fields
 276  
 277          $table = new html_table();
 278          $table->head = array(
 279              get_string('fieldname', 'data'),
 280              get_string('type', 'data'),
 281              get_string('required', 'data'),
 282              get_string('fielddescription', 'data'),
 283              get_string('action', 'data'),
 284          );
 285          $table->align = array('left', 'left', 'left', 'left');
 286          $table->wrap = array(false,false,false,false);
 287  
 288          if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
 289              $missingfieldtypes = [];
 290              foreach ($fff as $ff) {
 291  
 292                  $field = data_get_field($ff, $data);
 293  
 294                  $baseurl = new moodle_url('/mod/data/field.php', array(
 295                      'd'         => $data->id,
 296                      'fid'       => $field->field->id,
 297                      'sesskey'   => sesskey(),
 298                  ));
 299  
 300                  $displayurl = new moodle_url($baseurl, array(
 301                      'mode'      => 'display',
 302                  ));
 303  
 304                  $deleteurl = new moodle_url($baseurl, array(
 305                      'mode'      => 'delete',
 306                  ));
 307  
 308                  // It display a notification when the field type does not exist.
 309                  $deletelink = html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete')));
 310                  $editlink = html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit')));
 311                  if ($field->type === 'unknown') {
 312                      $missingfieldtypes[] = $field->field->name;
 313                      $fieldnamedata = $field->field->name;
 314                      $fieltypedata = $field->field->type;
 315                      $fieldlinkdata = $deletelink;
 316                  } else {
 317                      $fieldnamedata = html_writer::link($displayurl, $field->field->name);
 318                      $fieltypedata = $field->image() . '&nbsp;' . $field->name();
 319                      $fieldlinkdata = $editlink . '&nbsp;' . $deletelink;
 320                  }
 321  
 322                  $table->data[] = [
 323                      $fieldnamedata,
 324                      $fieltypedata,
 325                      $field->field->required ? get_string('yes') : get_string('no'),
 326                      shorten_text($field->field->description, 30),
 327                      $fieldlinkdata
 328                  ];
 329              }
 330              if (!empty($missingfieldtypes)) {
 331                  echo $OUTPUT->notification(get_string('missingfieldtypes', 'data') . html_writer::alist($missingfieldtypes));
 332              }
 333          }
 334          echo html_writer::table($table);
 335      }
 336  
 337  
 338      echo '<div class="fieldadd">';
 339      $popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='.  sesskey();
 340      echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array('' => 'choosedots'),
 341          'fieldform', array('label' => get_string('newfield', 'data')));
 342      echo $OUTPUT->help_icon('newfield', 'data');
 343      echo '</div>';
 344  
 345      echo '<div class="sortdefault">';
 346      echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
 347      echo '<div>';
 348      echo '<input type="hidden" name="d" value="'.$data->id.'" />';
 349      echo '<input type="hidden" name="mode" value="sort" />';
 350      echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 351      echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
 352      echo '<select id="defaultsort" name="defaultsort" class="custom-select">';
 353      if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
 354          echo '<optgroup label="'.get_string('fields', 'data').'">';
 355          foreach ($fields as $field) {
 356              if ($data->defaultsort == $field->id) {
 357                  echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
 358              } else {
 359                  echo '<option value="'.$field->id.'">'.$field->name.'</option>';
 360              }
 361          }
 362          echo '</optgroup>';
 363      }
 364      $options = array();
 365      $options[DATA_TIMEADDED]    = get_string('timeadded', 'data');
 366  // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
 367  /*        $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
 368      $options[DATA_FIRSTNAME]    = get_string('authorfirstname', 'data');
 369      $options[DATA_LASTNAME]     = get_string('authorlastname', 'data');
 370      if ($data->approval and has_capability('mod/data:approve', $context)) {
 371          $options[DATA_APPROVED] = get_string('approved', 'data');
 372      }*/
 373      echo '<optgroup label="'.get_string('other', 'data').'">';
 374      foreach ($options as $key => $name) {
 375          if ($data->defaultsort == $key) {
 376              echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
 377          } else {
 378              echo '<option value="'.$key.'">'.$name.'</option>';
 379          }
 380      }
 381      echo '</optgroup>';
 382      echo '</select>';
 383  
 384      $options = array(0 => get_string('ascending', 'data'),
 385                       1 => get_string('descending', 'data'));
 386      echo html_writer::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
 387      echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false, array('class' => 'custom-select'));
 388      echo '<input type="submit" class="btn btn-secondary ml-1" value="'.get_string('save', 'data').'" />';
 389      echo '</div>';
 390      echo '</form>';
 391      echo '</div>';
 392  
 393  }
 394  
 395  /// Finish the page
 396  echo $OUTPUT->footer();
 397