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.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  require_once($CFG->libdir.'/formslib.php');
  18  
  19  define('FEEDBACK_ITEM_NAME_TEXTBOX_SIZE', 80);
  20  define('FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE', 20);
  21  abstract class feedback_item_form extends moodleform {
  22      public function definition() {
  23          $item = $this->_customdata['item']; //the item object
  24  
  25          //common is an array like:
  26          //    array('cmid'=>$cm->id,
  27          //         'id'=>isset($item->id) ? $item->id : NULL,
  28          //         'typ'=>$item->typ,
  29          //         'items'=>$feedbackitems,
  30          //         'feedback'=>$feedback->id);
  31          $common = $this->_customdata['common'];
  32  
  33          //positionlist is an array with possible positions for the item location
  34          $positionlist = $this->_customdata['positionlist'];
  35  
  36          //the current position of the item
  37          $position = $this->_customdata['position'];
  38  
  39          $mform =& $this->_form;
  40  
  41          if (array_filter(array_keys($common['items']))) {
  42              $mform->addElement('select',
  43                                  'dependitem',
  44                                  get_string('dependitem', 'feedback').'&nbsp;',
  45                                  $common['items']
  46                                  );
  47              $mform->addHelpButton('dependitem', 'depending', 'feedback');
  48              $mform->addElement('text',
  49                                  'dependvalue',
  50                                  get_string('dependvalue', 'feedback'),
  51                                  array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
  52              $mform->hideIf('dependvalue', 'dependitem', 'eq', '0');
  53          } else {
  54              $mform->addElement('hidden', 'dependitem', 0);
  55              $mform->addElement('hidden', 'dependvalue', '');
  56          }
  57  
  58          $mform->setType('dependitem', PARAM_INT);
  59          $mform->setType('dependvalue', PARAM_RAW);
  60  
  61          $position_select = $mform->addElement('select',
  62                                              'position',
  63                                              get_string('position', 'feedback').'&nbsp;',
  64                                              $positionlist);
  65          $position_select->setValue($position);
  66  
  67          $mform->addElement('hidden', 'cmid', $common['cmid']);
  68          $mform->setType('cmid', PARAM_INT);
  69  
  70          $mform->addElement('hidden', 'id', $common['id']);
  71          $mform->setType('id', PARAM_INT);
  72  
  73          $mform->addElement('hidden', 'feedback', $common['feedback']);
  74          $mform->setType('feedback', PARAM_INT);
  75  
  76          $mform->addElement('hidden', 'template', 0);
  77          $mform->setType('template', PARAM_INT);
  78  
  79          $mform->setType('name', PARAM_RAW);
  80          $mform->setType('label', PARAM_NOTAGS);
  81  
  82          $mform->addElement('hidden', 'typ', $this->type);
  83          $mform->setType('typ', PARAM_ALPHA);
  84  
  85          $mform->addElement('hidden', 'hasvalue', 0);
  86          $mform->setType('hasvalue', PARAM_INT);
  87  
  88          $mform->addElement('hidden', 'options', '');
  89          $mform->setType('options', PARAM_ALPHA);
  90  
  91          $buttonarray = array();
  92          if (!empty($item->id)) {
  93              $buttonarray[] = &$mform->createElement('submit',
  94                                                      'update_item',
  95                                                      get_string('update_item', 'feedback'));
  96  
  97              $buttonarray[] = &$mform->createElement('submit',
  98                                                      'clone_item',
  99                                                      get_string('save_as_new_item', 'feedback'));
 100          } else {
 101              $mform->addElement('hidden', 'clone_item', 0);
 102              $mform->setType('clone_item', PARAM_INT);
 103              $buttonarray[] = &$mform->createElement('submit',
 104                                                      'save_item',
 105                                                      get_string('save_item', 'feedback'));
 106          }
 107          $buttonarray[] = &$mform->createElement('cancel');
 108          $mform->addGroup($buttonarray, 'buttonar', '&nbsp;', array(' '), false);
 109  
 110      }
 111  
 112      /**
 113       * Return submitted data if properly submitted or returns NULL if validation fails or
 114       * if there is no submitted data.
 115       *
 116       * @return object submitted data; NULL if not valid or not submitted or cancelled
 117       */
 118      public function get_data() {
 119          if ($item = parent::get_data()) {
 120              if (!isset($item->dependvalue)) {
 121                  $item->dependvalue = '';
 122              }
 123          }
 124          return $item;
 125      }
 126  }
 127