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  /**
  18   * Library functions used by question/preview.php.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionengine
  22   * @copyright  2010 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir . '/formslib.php');
  30  
  31  
  32  /**
  33   * Settings form for the preview options.
  34   *
  35   * @copyright  2009 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class preview_options_form extends moodleform {
  39      public function definition() {
  40          $mform = $this->_form;
  41  
  42          $hiddenofvisible = array(
  43              question_display_options::HIDDEN => get_string('notshown', 'question'),
  44              question_display_options::VISIBLE => get_string('shown', 'question'),
  45          );
  46  
  47          $mform->addElement('header', 'attemptoptionsheader', get_string('attemptoptions', 'question'));
  48  
  49          $behaviours = question_engine::get_behaviour_options(
  50                  $this->_customdata['quba']->get_preferred_behaviour());
  51          $mform->addElement('select', 'behaviour',
  52                  get_string('howquestionsbehave', 'question'), $behaviours);
  53          $mform->addHelpButton('behaviour', 'howquestionsbehave', 'question');
  54  
  55          $mform->addElement('float', 'maxmark', get_string('markedoutof', 'question'),
  56                  array('size' => '5'));
  57  
  58          if ($this->_customdata['maxvariant'] > 1) {
  59              $variants = range(1, $this->_customdata['maxvariant']);
  60              $mform->addElement('select', 'variant', get_string('questionvariant', 'question'),
  61                      array_combine($variants, $variants));
  62          }
  63          $mform->setType('variant', PARAM_INT);
  64  
  65          $mform->addElement('submit', 'saverestart',
  66                  get_string('restartwiththeseoptions', 'question'));
  67  
  68          $mform->addElement('header', 'displayoptionsheader', get_string('displayoptions', 'question'));
  69  
  70          $mform->addElement('select', 'correctness', get_string('whethercorrect', 'question'),
  71                  $hiddenofvisible);
  72  
  73          $marksoptions = array(
  74              question_display_options::HIDDEN => get_string('notshown', 'question'),
  75              question_display_options::MAX_ONLY => get_string('showmaxmarkonly', 'question'),
  76              question_display_options::MARK_AND_MAX => get_string('showmarkandmax', 'question'),
  77          );
  78          $mform->addElement('select', 'marks', get_string('marks', 'question'), $marksoptions);
  79  
  80          $mform->addElement('select', 'markdp', get_string('decimalplacesingrades', 'question'),
  81                  question_engine::get_dp_options());
  82  
  83          $mform->addElement('select', 'feedback',
  84                  get_string('specificfeedback', 'question'), $hiddenofvisible);
  85  
  86          $mform->addElement('select', 'generalfeedback',
  87                  get_string('generalfeedback', 'question'), $hiddenofvisible);
  88  
  89          $mform->addElement('select', 'rightanswer',
  90                  get_string('rightanswer', 'question'), $hiddenofvisible);
  91  
  92          $mform->addElement('select', 'history',
  93                  get_string('responsehistory', 'question'), $hiddenofvisible);
  94  
  95          $mform->addElement('submit', 'saveupdate',
  96                  get_string('updatedisplayoptions', 'question'));
  97      }
  98  }
  99  
 100  
 101  /**
 102   * Displays question preview options as default and set the options
 103   * Setting default, getting and setting user preferences in question preview options.
 104   *
 105   * @copyright  2010 The Open University
 106   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 107   */
 108  class question_preview_options extends question_display_options {
 109      /** @var string the behaviour to use for this preview. */
 110      public $behaviour;
 111  
 112      /** @var number the maximum mark to use for this preview. */
 113      public $maxmark;
 114  
 115      /** @var int the variant of the question to preview. */
 116      public $variant;
 117  
 118      /** @var string prefix to append to field names to get user_preference names. */
 119      const OPTIONPREFIX = 'question_preview_options_';
 120  
 121      /**
 122       * Constructor.
 123       */
 124      public function __construct($question) {
 125          $this->behaviour = 'deferredfeedback';
 126          $this->maxmark = $question->defaultmark;
 127          $this->variant = null;
 128          $this->correctness = self::VISIBLE;
 129          $this->marks = self::MARK_AND_MAX;
 130          $this->markdp = get_config('quiz', 'decimalpoints');
 131          $this->feedback = self::VISIBLE;
 132          $this->numpartscorrect = $this->feedback;
 133          $this->generalfeedback = self::VISIBLE;
 134          $this->rightanswer = self::VISIBLE;
 135          $this->history = self::HIDDEN;
 136          $this->flags = self::HIDDEN;
 137          $this->manualcomment = self::HIDDEN;
 138      }
 139  
 140      /**
 141       * @return array names of the options we store in the user preferences table.
 142       */
 143      protected function get_user_pref_fields() {
 144          return array('behaviour', 'correctness', 'marks', 'markdp', 'feedback',
 145                  'generalfeedback', 'rightanswer', 'history');
 146      }
 147  
 148      /**
 149       * @return array names and param types of the options we read from the request.
 150       */
 151      protected function get_field_types() {
 152          return array(
 153              'behaviour' => PARAM_ALPHA,
 154              'maxmark' => PARAM_LOCALISEDFLOAT,
 155              'variant' => PARAM_INT,
 156              'correctness' => PARAM_BOOL,
 157              'marks' => PARAM_INT,
 158              'markdp' => PARAM_INT,
 159              'feedback' => PARAM_BOOL,
 160              'generalfeedback' => PARAM_BOOL,
 161              'rightanswer' => PARAM_BOOL,
 162              'history' => PARAM_BOOL,
 163          );
 164      }
 165  
 166      /**
 167       * Load the value of the options from the user_preferences table.
 168       */
 169      public function load_user_defaults() {
 170          $defaults = get_config('question_preview');
 171          foreach ($this->get_user_pref_fields() as $field) {
 172              $this->$field = get_user_preferences(
 173                      self::OPTIONPREFIX . $field, $defaults->$field);
 174          }
 175          $this->numpartscorrect = $this->feedback;
 176      }
 177  
 178      /**
 179       * Save a change to the user's preview options to the database.
 180       * @param object $newoptions
 181       */
 182      public function save_user_preview_options($newoptions) {
 183          foreach ($this->get_user_pref_fields() as $field) {
 184              if (isset($newoptions->$field)) {
 185                  set_user_preference(self::OPTIONPREFIX . $field, $newoptions->$field);
 186              }
 187          }
 188      }
 189  
 190      /**
 191       * Set the value of any fields included in the request.
 192       */
 193      public function set_from_request() {
 194          foreach ($this->get_field_types() as $field => $type) {
 195              $this->$field = optional_param($field, $this->$field, $type);
 196          }
 197          $this->numpartscorrect = $this->feedback;
 198      }
 199  
 200      /**
 201       * @return string URL fragment. Parameters needed in the URL when continuing
 202       * this preview.
 203       */
 204      public function get_url_params() {
 205          $params = array();
 206          foreach ($this->get_field_types() as $field => $notused) {
 207              if ($field == 'behaviour' || $field == 'maxmark' || is_null($this->$field)) {
 208                  continue;
 209              }
 210              $params[$field] = $this->$field;
 211          }
 212          return $params;
 213      }
 214  }
 215  
 216  
 217  /**
 218   * Called via pluginfile.php -> question_pluginfile to serve files belonging to
 219   * a question in a question_attempt when that attempt is a preview.
 220   *
 221   * @package  core_question
 222   * @category files
 223   * @param stdClass $course course settings object
 224   * @param stdClass $context context object
 225   * @param string $component the name of the component we are serving files for.
 226   * @param string $filearea the name of the file area.
 227   * @param int $qubaid the question_usage this image belongs to.
 228   * @param int $slot the relevant slot within the usage.
 229   * @param array $args the remaining bits of the file path.
 230   * @param bool $forcedownload whether the user must be forced to download the file.
 231   * @param array $options additional options affecting the file serving
 232   * @return bool false if file not found, does not return if found - justsend the file
 233   */
 234  function question_preview_question_pluginfile($course, $context, $component,
 235          $filearea, $qubaid, $slot, $args, $forcedownload, $fileoptions) {
 236      global $USER, $DB, $CFG;
 237  
 238      list($context, $course, $cm) = get_context_info_array($context->id);
 239      require_login($course, false, $cm);
 240  
 241      $quba = question_engine::load_questions_usage_by_activity($qubaid);
 242  
 243      if (!question_has_capability_on($quba->get_question($slot, false), 'use')) {
 244          send_file_not_found();
 245      }
 246  
 247      $options = new question_display_options();
 248      $options->feedback = question_display_options::VISIBLE;
 249      $options->numpartscorrect = question_display_options::VISIBLE;
 250      $options->generalfeedback = question_display_options::VISIBLE;
 251      $options->rightanswer = question_display_options::VISIBLE;
 252      $options->manualcomment = question_display_options::VISIBLE;
 253      $options->history = question_display_options::VISIBLE;
 254      if (!$quba->check_file_access($slot, $options, $component,
 255              $filearea, $args, $forcedownload)) {
 256          send_file_not_found();
 257      }
 258  
 259      $fs = get_file_storage();
 260      $relativepath = implode('/', $args);
 261      $fullpath = "/{$context->id}/{$component}/{$filearea}/{$relativepath}";
 262      if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
 263          send_file_not_found();
 264      }
 265  
 266      send_stored_file($file, 0, 0, $forcedownload, $fileoptions);
 267  }
 268  
 269  /**
 270   * The the URL to use for actions relating to this preview.
 271   * @param int $questionid the question being previewed.
 272   * @param int $qubaid the id of the question usage for this preview.
 273   * @param question_preview_options $options the options in use.
 274   */
 275  function question_preview_action_url($questionid, $qubaid,
 276          question_preview_options $options, $context) {
 277      $params = array(
 278          'id' => $questionid,
 279          'previewid' => $qubaid,
 280      );
 281      if ($context->contextlevel == CONTEXT_MODULE) {
 282          $params['cmid'] = $context->instanceid;
 283      } else if ($context->contextlevel == CONTEXT_COURSE) {
 284          $params['courseid'] = $context->instanceid;
 285      }
 286      $params = array_merge($params, $options->get_url_params());
 287      return new moodle_url('/question/preview.php', $params);
 288  }
 289  
 290  /**
 291   * The the URL to use for actions relating to this preview.
 292   * @param int $questionid the question being previewed.
 293   * @param context $context the current moodle context.
 294   * @param int $previewid optional previewid to sign post saved previewed answers.
 295   */
 296  function question_preview_form_url($questionid, $context, $previewid = null) {
 297      $params = array(
 298          'id' => $questionid,
 299      );
 300      if ($context->contextlevel == CONTEXT_MODULE) {
 301          $params['cmid'] = $context->instanceid;
 302      } else if ($context->contextlevel == CONTEXT_COURSE) {
 303          $params['courseid'] = $context->instanceid;
 304      }
 305      if ($previewid) {
 306          $params['previewid'] = $previewid;
 307      }
 308      return new moodle_url('/question/preview.php', $params);
 309  }
 310  
 311  /**
 312   * Delete the current preview, if any, and redirect to start a new preview.
 313   * @param int $previewid
 314   * @param int $questionid
 315   * @param object $displayoptions
 316   * @param object $context
 317   */
 318  function restart_preview($previewid, $questionid, $displayoptions, $context) {
 319      global $DB;
 320  
 321      if ($previewid) {
 322          $transaction = $DB->start_delegated_transaction();
 323          question_engine::delete_questions_usage_by_activity($previewid);
 324          $transaction->allow_commit();
 325      }
 326      redirect(question_preview_url($questionid, $displayoptions->behaviour,
 327              $displayoptions->maxmark, $displayoptions, $displayoptions->variant, $context));
 328  }