Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * This behaviour is for information items.
  19   *
  20   * @package    qbehaviour
  21   * @subpackage informationitem
  22   * @copyright  2009 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  
  30  /**
  31   * Question behaviour informaiton items.
  32   *
  33   * For example for the 'Description' 'Question type'. There is no grade,
  34   * and the question type is marked complete the first time the user navigates
  35   * away from a page that contains that question.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qbehaviour_informationitem extends question_behaviour {
  41  
  42      public function is_compatible_question(question_definition $question) {
  43          return true;
  44      }
  45  
  46      public function get_expected_data() {
  47          if ($this->qa->get_state() == question_state::$todo) {
  48              return array('seen' => PARAM_BOOL);
  49          }
  50          return parent::get_expected_data();
  51      }
  52  
  53      public function get_correct_response() {
  54          if ($this->qa->get_state() == question_state::$todo) {
  55              return array('seen' => 1);
  56          }
  57          return array();
  58      }
  59  
  60      public function adjust_display_options(question_display_options $options) {
  61          parent::adjust_display_options($options);
  62  
  63          $options->marks = question_display_options::HIDDEN;
  64  
  65          // At the moment, the code exists to process a manual comment on an
  66          // information item, but we don't display the UI unless there is already
  67          // a comment.
  68          if (!$this->qa->get_state()->is_commented()) {
  69              $options->manualcomment = question_display_options::HIDDEN;
  70          }
  71      }
  72  
  73      public function get_state_string($showcorrectness) {
  74          return '';
  75      }
  76  
  77      public function process_action(question_attempt_pending_step $pendingstep) {
  78          if ($pendingstep->has_behaviour_var('comment')) {
  79              return $this->process_comment($pendingstep);
  80          } else if ($pendingstep->has_behaviour_var('finish')) {
  81              return $this->process_finish($pendingstep);
  82          } else if ($pendingstep->has_behaviour_var('seen')) {
  83              return $this->process_seen($pendingstep);
  84          } else {
  85              return question_attempt::DISCARD;
  86          }
  87      }
  88  
  89      public function summarise_action(question_attempt_step $step) {
  90          if ($step->has_behaviour_var('comment')) {
  91              return $this->summarise_manual_comment($step);
  92          } else if ($step->has_behaviour_var('finish')) {
  93              return $this->summarise_finish($step);
  94          } else if ($step->has_behaviour_var('seen')) {
  95              return get_string('seen', 'qbehaviour_informationitem');
  96          }
  97          return $this->summarise_start($step);
  98      }
  99  
 100      public function process_comment(question_attempt_pending_step $pendingstep) {
 101          if ($pendingstep->has_behaviour_var('mark')) {
 102              throw new coding_exception('Information items cannot be graded.');
 103          }
 104          return parent::process_comment($pendingstep);
 105      }
 106  
 107      /**
 108       * Handle the 'finish' case of {@see process_action()}.
 109       *
 110       * @param question_attempt_pending_step $pendingstep step representing the action.
 111       * @return bool either {@see question_attempt::KEEP} or {@see question_attempt::DISCARD}.
 112       */
 113      public function process_finish(question_attempt_pending_step $pendingstep) {
 114          if ($this->qa->get_state()->is_finished()) {
 115              return question_attempt::DISCARD;
 116          }
 117  
 118          if (!$this->qa->get_state()->is_active()) {
 119              throw new coding_exception('It should be impossible for question_attempt ' . $this->qa->get_database_id() .
 120                      ' using ' . get_class($this) . " to receive a 'finish' action while not active. " .
 121                      'Failing with an error, rather than continuing and doing undefined processing, ' .
 122                      'so the bug can be identified.');
 123          }
 124  
 125          $pendingstep->set_state(question_state::$finished);
 126          return question_attempt::KEEP;
 127      }
 128  
 129      /**
 130       * Handle the 'seen' case of {@see process_action()}.
 131       *
 132       * @param question_attempt_pending_step $pendingstep step representing the action.
 133       * @return bool either {@see question_attempt::KEEP} or {@see question_attempt::DISCARD}.
 134       */
 135      public function process_seen(question_attempt_pending_step $pendingstep) {
 136          if ($this->qa->get_state()->is_finished()) {
 137              return question_attempt::DISCARD;
 138          }
 139  
 140          // Assert so we get a clear error message if the assumptions on which this code relies is invalid.
 141          if (!$this->qa->get_state()->is_active()) {
 142              throw new coding_exception('It should be impossible for question_attempt ' . $this->qa->get_database_id() .
 143                      ' using ' . get_class($this) . " to receive a 'seen' action while not active. " .
 144                      'Failing with an error, rather than continuing and doing undefined processing, ' .
 145                      'so the bug can be identified.');
 146          }
 147  
 148          $pendingstep->set_state(question_state::$complete);
 149          return question_attempt::KEEP;
 150      }
 151  }