Search moodle.org's
Developer Documentation


/mod/data/ -> field.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  
  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  
 150                  for ($i=1; $i<=10; $i++) {
 151                      if (isset($fieldinput->{'param'.$i})) {
 152                          $field->field->{'param'.$i} = $fieldinput->{'param'.$i};
 153                      } else {
 154                          $field->field->{'param'.$i} = '';
 155                      }
 156                  }
 157  
 158                  $field->update_field();
 159  
 160              /// Update the templates.
 161                  data_replace_field_in_templates($data, $oldfieldname, $field->field->name);
 162  
 163                  $displaynoticegood = get_string('fieldupdated','data');
 164              }
 165          }
 166          break;
 167  
 168  
 169      case 'delete':    // Delete a field
 170          if (confirm_sesskey()){
 171  
 172              if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
 173  
 174  
 175                  // Delete the field completely
 176                  if ($field = data_get_field_from_id($fid, $data)) {
 177                      $field->delete_field();
 178  
 179                      // Update the templates.
 180                      data_replace_field_in_templates($data, $field->field->name, '');
 181  
 182                      // Update the default sort field
 183                      if ($fid == $data->defaultsort) {
 184                          $rec = new stdClass();
 185                          $rec->id = $data->id;
 186                          $rec->defaultsort = 0;
 187                          $rec->defaultsortdir = 0;
 188                          $DB->update_record('data', $rec);
 189                      }
 190  
 191                      $displaynoticegood = get_string('fielddeleted', 'data');
 192                  }
 193  
 194              } else {
 195  
 196                  data_print_header($course,$cm,$data, false);
 197  
 198                  // Print confirmation message.
 199                  $field = data_get_field_from_id($fid, $data);
 200  
 201                  echo $OUTPUT->confirm('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
 202                               'field.php?d='.$data->id.'&mode=delete&fid='.$fid.'&confirm=1',
 203                               'field.php?d='.$data->id);
 204  
 205                  echo $OUTPUT->footer();
 206                  exit;
 207              }
 208          }
 209          break;
 210  
 211  
 212      case 'sort':    // Set the default sort parameters
 213          if (confirm_sesskey()) {
 214              $rec = new stdClass();
 215              $rec->id = $data->id;
 216              $rec->defaultsort = $defaultsort;
 217              $rec->defaultsortdir = $defaultsortdir;
 218  
 219              $DB->update_record('data', $rec);
 220              redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
 221              exit;
 222          }
 223          break;
 224  
 225      default:
 226          break;
 227  }
 228  
 229  
 230  
 231  /// Print the browsing interface
 232  
 233  ///get the list of possible fields (plugins)
 234  $plugins = core_component::get_plugin_list('datafield');
 235  $menufield = array();
 236  
 237  foreach ($plugins as $plugin=>$fulldir){
 238      $menufield[$plugin] = get_string('pluginname', 'datafield_'.$plugin);    //get from language files
 239  }
 240  asort($menufield);    //sort in alphabetical order
 241  $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
 242  $PAGE->set_heading($course->fullname);
 243  
 244  $PAGE->set_pagetype('mod-data-field-' . $newtype);
 245  if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) {          ///  Adding a new field
 246      data_print_header($course, $cm, $data,'fields');
 247  
 248      $field = data_get_field_new($newtype, $data);
 249      $field->display_edit_field();
 250  
 251  } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field
 252      data_print_header($course, $cm, $data,'fields');
 253  
 254      $field = data_get_field_from_id($fid, $data);
 255      $field->display_edit_field();
 256  
 257  } else {                                              /// Display the main listing of all fields
 258      data_print_header($course, $cm, $data,'fields');
 259  
 260      if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {
 261          echo $OUTPUT->notification(get_string('nofieldindatabase','data'));  // nothing in database
 262          echo $OUTPUT->notification(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id));      // link to presets
 263  
 264      } else {    //else print quiz style list of fields
 265  
 266          $table = new html_table();
 267          $table->head = array(get_string('fieldname','data'), get_string('type','data'), get_string('fielddescription', 'data'), get_string('action','data'));
 268          $table->align = array('left','left','left', 'center');
 269          $table->wrap = array(false,false,false,false);
 270  
 271          if ($fff = $DB->get_records('data_fields', array('dataid'=>$data->id),'id')){
 272              foreach ($fff as $ff) {
 273  
 274                  $field = data_get_field($ff, $data);
 275  
 276                  $table->data[] = array(
 277  
 278                  '<a href="field.php?mode=display&amp;d='.$data->id.
 279                  '&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.$field->field->name.'</a>',
 280  
 281                  $field->image().'&nbsp;'.get_string($field->type, 'data'),
 282  
 283                  shorten_text($field->field->description, 30),
 284  
 285                  '<a href="field.php?d='.$data->id.'&amp;mode=display&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.
 286                  '<img src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>'.
 287                  '&nbsp;'.
 288                  '<a href="field.php?d='.$data->id.'&amp;mode=delete&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.
 289                  '<img src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>'
 290  
 291                  );
 292              }
 293          }
 294          echo html_writer::table($table);
 295      }
 296  
 297  
 298      echo '<div class="fieldadd">';
 299      echo '<label for="fieldform_jump">'.get_string('newfield','data').$OUTPUT->help_icon('newfield', 'data').'</label>';
 300      $popupurl = $CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&mode=new&sesskey='.  sesskey();
 301      echo $OUTPUT->single_select(new moodle_url($popupurl), 'newtype', $menufield, null, array(''=>'choosedots'), 'fieldform');
 302      echo '</div>';
 303  
 304      echo '<div class="sortdefault">';
 305      echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
 306      echo '<div>';
 307      echo '<input type="hidden" name="d" value="'.$data->id.'" />';
 308      echo '<input type="hidden" name="mode" value="sort" />';
 309      echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 310      echo '<label for="defaultsort">'.get_string('defaultsortfield','data').'</label>';
 311      echo '<select id="defaultsort" name="defaultsort">';
 312      if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
 313          echo '<optgroup label="'.get_string('fields', 'data').'">';
 314          foreach ($fields as $field) {
 315              if ($data->defaultsort == $field->id) {
 316                  echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
 317              } else {
 318                  echo '<option value="'.$field->id.'">'.$field->name.'</option>';
 319              }
 320          }
 321          echo '</optgroup>';
 322      }
 323      $options = array();
 324      $options[DATA_TIMEADDED]    = get_string('timeadded', 'data');
 325  // TODO: we will need to change defaultsort db to unsinged to make these work in 2.0
 326  /*        $options[DATA_TIMEMODIFIED] = get_string('timemodified', 'data');
 327      $options[DATA_FIRSTNAME]    = get_string('authorfirstname', 'data');
 328      $options[DATA_LASTNAME]     = get_string('authorlastname', 'data');
 329      if ($data->approval and has_capability('mod/data:approve', $context)) {
 330          $options[DATA_APPROVED] = get_string('approved', 'data');
 331      }*/
 332      echo '<optgroup label="'.get_string('other', 'data').'">';
 333      foreach ($options as $key => $name) {
 334          if ($data->defaultsort == $key) {
 335              echo '<option value="'.$key.'" selected="selected">'.$name.'</option>';
 336          } else {
 337              echo '<option value="'.$key.'">'.$name.'</option>';
 338          }
 339      }
 340      echo '</optgroup>';
 341      echo '</select>';
 342  
 343      $options = array(0 => get_string('ascending', 'data'),
 344                       1 => get_string('descending', 'data'));
 345      echo html_writer::label(get_string('sortby'), 'menudefaultsortdir', false, array('class' => 'accesshide'));
 346      echo html_writer::select($options, 'defaultsortdir', $data->defaultsortdir, false);
 347      echo '<input type="submit" value="'.get_string('save', 'data').'" />';
 348      echo '</div>';
 349      echo '</form>';
 350      echo '</div>';
 351  
 352  }
 353  
 354  /// Finish the page
 355  echo $OUTPUT->footer();
 356  

Search This Site: