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.
   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   * Question behaviour for questions that can only be graded manually.
  19   *
  20   * @package    qbehaviour
  21   * @subpackage manualgraded
  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 for questions that can only be graded manually.
  32   *
  33   * The student enters their response during the attempt, and it is saved. Later,
  34   * when the whole attempt is finished, the attempt goes into the NEEDS_GRADING
  35   * state, and the teacher must grade it manually.
  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_manualgraded extends question_behaviour_with_save {
  41  
  42      public function is_compatible_question(question_definition $question) {
  43          return $question instanceof question_with_responses;
  44      }
  45  
  46      public function adjust_display_options(question_display_options $options) {
  47          parent::adjust_display_options($options);
  48  
  49          if ($this->qa->get_state()->is_finished()) {
  50              // Hide all feedback except genfeedback and manualcomment.
  51              $save = clone($options);
  52              $options->hide_all_feedback();
  53              $options->generalfeedback = $save->generalfeedback;
  54              $options->manualcomment = $save->manualcomment;
  55          }
  56      }
  57  
  58      public function process_action(question_attempt_pending_step $pendingstep) {
  59          if ($pendingstep->has_behaviour_var('comment')) {
  60              return $this->process_comment($pendingstep);
  61          } else if ($pendingstep->has_behaviour_var('finish')) {
  62              return $this->process_finish($pendingstep);
  63          } else {
  64              return $this->process_save($pendingstep);
  65          }
  66      }
  67  
  68      /**
  69       * Like the parent method, except that when a response is gradable, but not
  70       * completely, we move it to the invalid state.
  71       * @param question_attempt_pending_step $pendingstep a partially initialised step
  72       *      containing all the information about the action that is being performed.
  73       * @return bool either {@link question_attempt::KEEP} or {@link question_attempt::DISCARD}
  74       */
  75      public function process_save(question_attempt_pending_step $pendingstep) {
  76          if ($this->qa->get_state()->is_finished()) {
  77              return question_attempt::DISCARD;
  78          } else if (!$this->qa->get_state()->is_active()) {
  79              throw new coding_exception('Question is not active, cannot process_actions.');
  80          }
  81  
  82          if ($this->is_same_response($pendingstep)) {
  83              return question_attempt::DISCARD;
  84          }
  85  
  86          if ($this->is_complete_response($pendingstep)) {
  87              $pendingstep->set_state(question_state::$complete);
  88          } else if ($this->question->is_gradable_response($pendingstep->get_qt_data())) {
  89              $pendingstep->set_state(question_state::$invalid);
  90          } else {
  91              $pendingstep->set_state(question_state::$todo);
  92          }
  93          return question_attempt::KEEP;
  94      }
  95  
  96      public function summarise_action(question_attempt_step $step) {
  97          if ($step->has_behaviour_var('comment')) {
  98              return $this->summarise_manual_comment($step);
  99          } else if ($step->has_behaviour_var('finish')) {
 100              return $this->summarise_finish($step);
 101          } else {
 102              return $this->summarise_save($step);
 103          }
 104      }
 105  
 106      public function process_finish(question_attempt_pending_step $pendingstep) {
 107          if ($this->qa->get_state()->is_finished()) {
 108              return question_attempt::DISCARD;
 109          }
 110  
 111          $response = $this->qa->get_last_step()->get_qt_data();
 112          if (!$this->question->is_gradable_response($response)) {
 113              $pendingstep->set_state(question_state::$gaveup);
 114          } else {
 115              $pendingstep->set_state(question_state::$needsgrading);
 116          }
 117          $pendingstep->set_new_response_summary($this->question->summarise_response($response));
 118          return question_attempt::KEEP;
 119      }
 120  }