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 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * mod_feedback data generator.
  19   *
  20   * @package    mod_feedback
  21   * @category   test
  22   * @copyright  2013 Ankit Agarwal
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * mod_feedback data generator class.
  30   *
  31   * @package    mod_feedback
  32   * @category   test
  33   * @copyright  2013 Ankit Agarwal
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mod_feedback_generator extends testing_module_generator {
  37  
  38      public function create_instance($record = null, array $options = null) {
  39          global $CFG;
  40          require_once($CFG->dirroot.'/mod/feedback/lib.php');
  41          $record = (object)(array)$record;
  42  
  43          if (!isset($record->anonymous)) {
  44              $record->anonymous = FEEDBACK_ANONYMOUS_YES;
  45          }
  46          if (!isset($record->email_notification)) {
  47              $record->email_notification = 0;
  48          }
  49          if (!isset($record->multiple_submit)) {
  50              $record->multiple_submit = 0;
  51          }
  52          if (!isset($record->autonumbering)) {
  53              $record->autonumbering = 0;
  54          }
  55          if (!isset($record->site_after_submit)) {
  56              $record->site_after_submit = '';
  57          }
  58          if (!isset($record->page_after_submit)) {
  59              $record->page_after_submit = 'This is page after submit';
  60          }
  61          if (!isset($record->page_after_submitformat)) {
  62              $record->page_after_submitformat = FORMAT_MOODLE;
  63          }
  64          if (!isset($record->publish_stats)) {
  65              $record->publish_stats = 0;
  66          }
  67          if (!isset($record->timeopen)) {
  68              $record->timeopen = 0;
  69          }
  70          if (!isset($record->timeclose)) {
  71              $record->timeclose = 0;
  72          }
  73          if (!isset($record->timemodified)) {
  74              $record->timemodified = time();
  75          }
  76          if (!isset($record->completionsubmit)) {
  77              $record->completionsubmit = 0;
  78          }
  79  
  80          // Hack to bypass draft processing of feedback_add_instance.
  81          $record->page_after_submit_editor['itemid'] = false;
  82  
  83          return parent::create_instance($record, (array)$options);
  84      }
  85  
  86      /**
  87       * Create info question item.
  88       *
  89       * @param object $feedback feedback record
  90       * @param array $record (optional) to override default values
  91       * @return int
  92       */
  93      public function create_item_info($feedback, $record = array()) {
  94          global $DB, $CFG;
  95  
  96          require_once($CFG->dirroot.'/mod/feedback/lib.php');
  97  
  98          $itemobj = feedback_get_item_class('info');
  99          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 100  
 101          $record = (array)$record + array(
 102              'id' => 0,
 103              'feedback' => $feedback->id,
 104              'template' => 0,
 105              'name' => 'Feedback question item ' . $position,
 106              'label' => 'Feedback label ' . $position,
 107              'presentation' => $itemobj::MODE_COURSE,
 108              'typ' => 'info',
 109              'hasvalue' => 0,
 110              'position' => $position,
 111              'required' => 0,
 112              'dependitem' => 0,
 113              'dependvalue' => '',
 114              'options' => '',
 115          );
 116  
 117          $itemobj->set_data((object) $record);
 118          return $itemobj->save_item();
 119      }
 120  
 121      /**
 122       * Create label question item.
 123       *
 124       * @param object $feedback feedback record
 125       * @param array $record (optional) to override default values
 126       * @return int
 127       */
 128      public function create_item_label($feedback, $record = array()) {
 129          global $DB, $CFG;
 130  
 131          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 132  
 133          $itemobj = feedback_get_item_class('label');
 134          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 135  
 136          $record = (array)$record + array(
 137              'id' => 0,
 138              'feedback' => $feedback->id,
 139              'template' => 0,
 140              'name' => 'label',
 141              'label' => '',
 142              'presentation' => '',
 143              'typ' => 'label',
 144              'hasvalue' => 0,
 145              'position' => $position,
 146              'required' => 0,
 147              'dependitem' => 0,
 148              'dependvalue' => '',
 149              'options' => '',
 150          );
 151  
 152          if (!isset($record['presentation_editor'])) {
 153              $record['presentation_editor'] = array(
 154                  'text' => "The label $position text goes here",
 155                  'format' => FORMAT_HTML,
 156                  'itemid' => 0
 157              );
 158          }
 159  
 160          $itemobj->set_data((object) $record);
 161          return $itemobj->save_item();
 162      }
 163  
 164      /**
 165       * Create multichoice question item.
 166       *
 167       * @param object $feedback feedback record
 168       * @param array $record (optional) to override default values
 169       * @return int
 170       */
 171      public function create_item_multichoice($feedback, $record = array()) {
 172          global $DB, $CFG;
 173  
 174          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 175  
 176          $itemobj = feedback_get_item_class('multichoice');
 177          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 178  
 179          $record = (array)$record + array(
 180              'id' => 0,
 181              'feedback' => $feedback->id,
 182              'template' => 0,
 183              'name' => 'Feedback question item ' . $position,
 184              'label' => 'Feedback label ' . $position,
 185              'presentation' => '',
 186              'typ' => 'multichoice',
 187              'hasvalue' => 0,
 188              'position' => $position,
 189              'required' => 0,
 190              'dependitem' => 0,
 191              'dependvalue' => '',
 192              'options' => '',
 193              'subtype' => 'r',
 194              'horizontal' => 0,
 195              'hidenoselect' => 1,
 196              'ignoreempty' => 0,
 197              'values' => "a\nb\nc\nd\ne"
 198          );
 199  
 200          $presentation = str_replace("\n", FEEDBACK_MULTICHOICE_LINE_SEP, trim($record['values']));
 201  
 202          if ($record['horizontal'] == 1 AND $record['subtype'] != 'd') {
 203              $presentation .= FEEDBACK_MULTICHOICE_ADJUST_SEP.'1';
 204          }
 205          $record['presentation'] = $record['subtype'].FEEDBACK_MULTICHOICE_TYPE_SEP.$presentation;
 206  
 207          $itemobj->set_data((object) $record);
 208          return $itemobj->save_item();
 209      }
 210  
 211      /**
 212       * Create multichoicerated question item.
 213       *
 214       * @param object $feedback feedback record
 215       * @param array $record (optional) to override default values
 216       * @return int
 217       */
 218      public function create_item_multichoicerated($feedback, $record = array()) {
 219          global $DB, $CFG;
 220  
 221          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 222  
 223          $itemobj = feedback_get_item_class('multichoicerated');
 224          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 225  
 226          $record = (array)$record + array(
 227              'id' => 0,
 228              'feedback' => $feedback->id,
 229              'template' => 0,
 230              'name' => 'Feedback question item ' . $position,
 231              'label' => 'Feedback label ' . $position,
 232              'presentation' => '',
 233              'typ' => 'multichoicerated',
 234              'hasvalue' => 0,
 235              'position' => $position,
 236              'required' => 0,
 237              'dependitem' => 0,
 238              'dependvalue' => '',
 239              'options' => '',
 240              'subtype' => 'r',
 241              'horizontal' => 0,
 242              'hidenoselect' => 1,
 243              'ignoreempty' => 0,
 244              'values' => "0/a\n1/b\n2/c\n3/d\n4/e"
 245          );
 246  
 247          $itemobj = new feedback_item_multichoicerated();
 248          $presentation = $itemobj->prepare_presentation_values_save(trim($record['values']),
 249              FEEDBACK_MULTICHOICERATED_VALUE_SEP2, FEEDBACK_MULTICHOICERATED_VALUE_SEP);
 250  
 251          if ($record['horizontal'] == 1 AND $record['subtype'] != 'd') {
 252              $presentation .= FEEDBACK_MULTICHOICERATED_ADJUST_SEP.'1';
 253          }
 254          $record['presentation'] = $record['subtype'].FEEDBACK_MULTICHOICERATED_TYPE_SEP.$presentation;
 255  
 256          $itemobj->set_data((object) $record);
 257          return $itemobj->save_item();
 258      }
 259  
 260      /**
 261       * Create numeric question item.
 262       *
 263       * @param object $feedback feedback record
 264       * @param array $record (optional) to override default values
 265       * @return int
 266       */
 267      public function create_item_numeric($feedback, $record = array()) {
 268          global $DB, $CFG;
 269  
 270          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 271  
 272          $itemobj = feedback_get_item_class('numeric');
 273          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 274  
 275          $record = (array)$record + array(
 276              'id' => 0,
 277              'feedback' => $feedback->id,
 278              'template' => 0,
 279              'name' => 'Feedback question item ' . $position,
 280              'label' => 'Feedback label ' . $position,
 281              'presentation' => '',
 282              'typ' => 'numeric',
 283              'hasvalue' => 0,
 284              'position' => $position,
 285              'required' => 0,
 286              'dependitem' => 0,
 287              'dependvalue' => '',
 288              'options' => '',
 289              'rangefrom' => '-',
 290              'rangeto' => '-',
 291          );
 292  
 293          if ($record['rangefrom'] === '-' OR $record['rangeto'] === '-') {
 294              $record['presentation'] = $record['rangefrom'] . '|'. $record['rangeto'];
 295          } else if ($record['rangefrom'] > $record['rangeto']) {
 296              $record['presentation'] = $record['rangeto'] . '|'. $record['rangefrom'];
 297          } else {
 298              $record['presentation'] = $record['rangefrom'] . '|'. $record['rangeto'];
 299          }
 300  
 301          $itemobj->set_data((object) $record);
 302          return $itemobj->save_item();
 303      }
 304  
 305      /**
 306       * Create textarea question item.
 307       *
 308       * @param object $feedback feedback record
 309       * @param array $record (optional) to override default values
 310       * @return int
 311       */
 312      public function create_item_textarea($feedback, $record = array()) {
 313          global $DB, $CFG;
 314  
 315          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 316  
 317          $itemobj = feedback_get_item_class('textarea');
 318          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 319  
 320          $record = (array)$record + array(
 321              'id' => 0,
 322              'feedback' => $feedback->id,
 323              'template' => 0,
 324              'name' => 'Feedback question item ' . $position,
 325              'label' => 'Feedback label ' . $position,
 326              'presentation' => '',
 327              'typ' => 'textarea',
 328              'hasvalue' => 0,
 329              'position' => $position,
 330              'required' => 0,
 331              'dependitem' => 0,
 332              'dependvalue' => '',
 333              'options' => '',
 334              'itemwidth' => '40',
 335              'itemheight' => '20',
 336          );
 337  
 338          $record['presentation'] = $record['itemwidth'] . '|'. $record['itemheight'];
 339  
 340          $itemobj->set_data((object) $record);
 341          return $itemobj->save_item();
 342      }
 343  
 344      /**
 345       * Create textfield question item.
 346       *
 347       * @param object $feedback feedback record
 348       * @param array $record (optional) to override default values
 349       * @return int
 350       */
 351      public function create_item_textfield($feedback, $record = array()) {
 352          global $DB, $CFG;
 353  
 354          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 355  
 356          $itemobj = feedback_get_item_class('textfield');
 357          $position = $DB->count_records('feedback_item', array('feedback' => $feedback->id)) + 1;
 358  
 359          $record = (array)$record + array(
 360              'id' => 0,
 361              'feedback' => $feedback->id,
 362              'template' => 0,
 363              'name' => 'Feedback question item ' . $position,
 364              'label' => 'Feedback label ' . $position,
 365              'presentation' => '',
 366              'typ' => 'textfield',
 367              'hasvalue' => 0,
 368              'position' => $position,
 369              'required' => 0,
 370              'dependitem' => 0,
 371              'dependvalue' => '',
 372              'options' => '',
 373              'itemsize' => '20',
 374              'itemmaxlength' => '30',
 375          );
 376  
 377          $record['presentation'] = $record['itemsize'] . '|'. $record['itemmaxlength'];
 378  
 379          $itemobj->set_data((object) $record);
 380          return $itemobj->save_item();
 381      }
 382  
 383      /**
 384       * Create pagebreak.
 385       *
 386       * @param object $feedback feedback record
 387       * @return mixed false if there already is a pagebreak on last position or the id of the pagebreak-item
 388       */
 389      public function create_item_pagebreak($feedback) {
 390          global $CFG;
 391          require_once($CFG->dirroot.'/mod/feedback/lib.php');
 392  
 393          return feedback_create_pagebreak($feedback->id);
 394      }
 395  }
 396