Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 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  /**
  18   * prints the forms to choose an item-typ to create items and to choose a template to use
  19   *
  20   * @author Andreas Grabs
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package mod_feedback
  23   */
  24  
  25  //It must be included from a Moodle page
  26  if (!defined('MOODLE_INTERNAL')) {
  27      die('Direct access to this script is forbidden.');
  28  }
  29  
  30  require_once($CFG->libdir.'/formslib.php');
  31  
  32  /**
  33   * The feedback_edit_use_template_form
  34   *
  35   * @deprecated since 4.0 new dynamic forms created
  36   */
  37  class feedback_edit_use_template_form extends moodleform {
  38      public function __construct($action = null, $customdata = null, $method = 'post', $target = '',
  39              $attributes = null, $editable = true, $ajaxformdata = null) {
  40          debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  41          parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
  42      }
  43  
  44      /**
  45       * Overrides parent static method for deprecation purposes.
  46       *
  47       * @deprecated since 4.0
  48       * @return array
  49       */
  50      public static function get_js_module() {
  51          debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  52          return parent::get_js_module();
  53      }
  54  
  55      /**
  56       * Overrides parent static method for deprecation purposes.
  57       *
  58       * @deprecated since 4.0
  59       * @param array $simulatedsubmitteddata
  60       * @param array $simulatedsubmittedfiles
  61       * @param string $method
  62       * @param null $formidentifier
  63       * @return array
  64       */
  65      public static function mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
  66              $method = 'post', $formidentifier = null) {
  67          debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  68          return parent::mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
  69      }
  70  
  71      /**
  72       * Overrides parent static method for deprecation purposes.
  73       *
  74       * @deprecated since 4.0
  75       * @param array $data
  76       * @return array
  77       */
  78      public static function mock_generate_submit_keys($data = []) {
  79          debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  80          return parent::mock_generate_submit_keys($data);
  81      }
  82  
  83      /**
  84       * Overrides parent static method for deprecation purposes.
  85       *
  86       * @deprecated since 4.0
  87       * @param array $simulatedsubmitteddata
  88       * @param array $simulatedsubmittedfiles
  89       * @param string $method
  90       * @param null $formidentifier
  91       */
  92      public static function mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
  93              $method = 'post', $formidentifier = null) {
  94          debugging('Class feedback_edit_use_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
  95          parent::mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
  96      }
  97  
  98      /**
  99       * Form definition
 100       */
 101      public function definition() {
 102          $mform =& $this->_form;
 103  
 104          $course = $this->_customdata['course'];
 105  
 106          $elementgroup = array();
 107          //headline
 108          $mform->addElement('header', 'using_templates', get_string('using_templates', 'feedback'));
 109          // hidden elements
 110          $mform->addElement('hidden', 'id');
 111          $mform->setType('id', PARAM_INT);
 112  
 113          // visible elements
 114          $templates_options = array();
 115          $owntemplates = feedback_get_template_list($course, 'own');
 116          $publictemplates = feedback_get_template_list($course, 'public');
 117  
 118          $options = array();
 119          if ($owntemplates or $publictemplates) {
 120              $options[''] = array('' => get_string('choosedots'));
 121  
 122              if ($owntemplates) {
 123                  $courseoptions = array();
 124                  foreach ($owntemplates as $template) {
 125                      $courseoptions[$template->id] = format_string($template->name);
 126                  }
 127                  $options[get_string('course')] = $courseoptions;
 128              }
 129  
 130              if ($publictemplates) {
 131                  $publicoptions = array();
 132                  foreach ($publictemplates as $template) {
 133                      $publicoptions[$template->id] = format_string($template->name);
 134                  }
 135                  $options[get_string('public', 'feedback')] = $publicoptions;
 136              }
 137  
 138              $attributes = [
 139                  'onChange="this.form.submit()"',
 140                  'data-form-change-checker-override="1"',
 141              ];
 142              $elementgroup[] = $mform->createElement(
 143                  'selectgroups',
 144                  'templateid',
 145                  get_string('using_templates', 'feedback'),
 146                  $options,
 147                  implode(' ', $attributes)
 148              );
 149  
 150              $elementgroup[] = $mform->createElement('submit',
 151                                                       'use_template',
 152                                                       get_string('use_this_template', 'feedback'),
 153                                                       array('class' => 'hiddenifjs'));
 154  
 155              $mform->addGroup($elementgroup, 'elementgroup', '', array(' '), false);
 156          } else {
 157              $mform->addElement('static', 'info', get_string('no_templates_available_yet', 'feedback'));
 158          }
 159  
 160          $this->set_data(array('id' => $this->_customdata['id']));
 161      }
 162  }
 163  
 164  /**
 165   * The feedback_edit_create_template_form
 166   *
 167   * @deprecated since 4.0, new dynamic forms have been created instead.
 168   */
 169  class feedback_edit_create_template_form extends moodleform {
 170      public function __construct($action = null, $customdata = null, $method = 'post',
 171              $target = '', $attributes = null, $editable = true, $ajaxformdata = null) {
 172          debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
 173          parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
 174      }
 175  
 176      /**
 177       * Overrides parent static method for deprecation purposes.
 178       *
 179       * @deprecated since 4.0
 180       * @return array
 181       */
 182      public static function get_js_module() {
 183          debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
 184          return parent::get_js_module();
 185      }
 186  
 187      /**
 188       * Overrides parent static method for deprecation purposes.
 189       *
 190       * @deprecated since 4.0
 191       * @param array $simulatedsubmitteddata
 192       * @param array $simulatedsubmittedfiles
 193       * @param string $method
 194       * @param null $formidentifier
 195       * @return array
 196       */
 197      public static function mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
 198              $method = 'post', $formidentifier = null) {
 199          debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
 200          return parent::mock_ajax_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
 201      }
 202  
 203      /**
 204       * Overrides parent static method for deprecation purposes.
 205       *
 206       * @deprecated since 4.0
 207       * @param array $data
 208       * @return array
 209       */
 210      public static function mock_generate_submit_keys($data = []) {
 211          debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
 212          return parent::mock_generate_submit_keys($data);
 213      }
 214  
 215      /**
 216       * Overrides parent static method for deprecation purposes.
 217       *
 218       * @deprecated since 4.0
 219       * @param array $simulatedsubmitteddata
 220       * @param array $simulatedsubmittedfiles
 221       * @param string $method
 222       * @param null $formidentifier
 223       */
 224      public static function mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(),
 225              $method = 'post', $formidentifier = null) {
 226          debugging('Class feedback_edit_create_template_form is deprecated. Replaced with dynamic forms.', DEBUG_DEVELOPER);
 227          parent::mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles, $method, $formidentifier);
 228      }
 229  
 230      /**
 231       * Form definition
 232       */
 233      public function definition() {
 234          $mform =& $this->_form;
 235  
 236          // hidden elements
 237          $mform->addElement('hidden', 'id');
 238          $mform->setType('id', PARAM_INT);
 239          $mform->addElement('hidden', 'do_show');
 240          $mform->setType('do_show', PARAM_ALPHANUMEXT);
 241          $mform->setConstant('do_show', 'edit');
 242  
 243          // visible elements
 244          $elementgroup = array();
 245  
 246          $elementgroup[] = $mform->createElement('text',
 247                                                   'templatename',
 248                                                   get_string('name', 'feedback'),
 249                                                   ['maxlength' => '200']);
 250  
 251          if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
 252              $elementgroup[] = $mform->createElement('checkbox',
 253                                                       'ispublic', '',
 254                                                       get_string('public', 'feedback'));
 255          }
 256  
 257  
 258          $mform->addGroup($elementgroup,
 259                           'elementgroup',
 260                           get_string('name', 'feedback'),
 261                           array(' '),
 262                           false);
 263  
 264          // Buttons.
 265          $mform->addElement('submit', 'create_template', get_string('save_as_new_template', 'feedback'));
 266  
 267          $mform->setType('templatename', PARAM_TEXT);
 268  
 269          $this->set_data(array('id' => $this->_customdata['id']));
 270      }
 271  
 272      /**
 273       * Form validation
 274       *
 275       * @param array $data array of ("fieldname"=>value) of submitted data
 276       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 277       * @return array of "element_name"=>"error_description" if there are errors,
 278       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 279       */
 280      public function validation($data, $files) {
 281          $errors = parent::validation($data, $files);
 282          if (!isset($data['templatename']) || trim(strval($data['templatename'])) === '') {
 283              $errors['elementgroup'] = get_string('name_required', 'feedback');
 284          }
 285          return $errors;
 286      }
 287  }
 288