Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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   * @package    core_question
  19   * @copyright  2013 The Open University
  20   * @author     James Pratt me@jamiep.org
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  namespace core_question\statistics\responses;
  25  
  26  
  27  
  28  /**
  29   * Counts a class of responses for this sub part of the question.
  30   *
  31   * No response is one possible class of response to a question.
  32   *
  33   * - There is a separate data structure for each question or sub question's analysis
  34   * {@link \core_question\statistics\responses\analysis_for_question}
  35   * or {@link \core_question\statistics\responses\analysis_for_question_all_tries}.
  36   * - There are separate analysis for each variant in this top level instance.
  37   * - Then there are class instances representing the analysis of each of the sub parts of each variant of the question.
  38   * {@link \core_question\statistics\responses\analysis_for_subpart}.
  39   * - Then within the sub part analysis there are response class analysis
  40   * {@link \core_question\statistics\responses\analysis_for_class}.
  41   * - Then within each class analysis there are analysis for each actual response
  42   * {@link \core_question\statistics\responses\analysis_for_actual_response}.
  43   *
  44   * @package    core_question
  45   * @copyright  2014 The Open University
  46   * @author     James Pratt me@jamiep.org
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class analysis_for_class {
  50  
  51      /**
  52       * @var string must be unique for each response class within this sub part.
  53       */
  54      protected $responseclassid;
  55  
  56      /**
  57       * @var string represent this class in the response analysis table.
  58       */
  59      protected $modelresponse;
  60  
  61      /** @var string the (partial) credit awarded for this responses. */
  62      protected $fraction;
  63  
  64      /** @var analysis_for_actual_response[] key is the actual response represented as a string as it will be displayed in report.
  65       */
  66      protected $actualresponses = array();
  67  
  68      /**
  69       * Constructor, just an easy way to set the fields.
  70       *
  71       * @param \question_possible_response $possibleresponse
  72       * @param string                      $responseclassid
  73       */
  74      public function __construct($possibleresponse, $responseclassid) {
  75          $this->modelresponse = $possibleresponse->responseclass;
  76          $this->fraction = $possibleresponse->fraction;
  77          $this->responseclassid = $responseclassid;
  78      }
  79  
  80      /**
  81       * Keep a count of a response to this question sub part that falls within this class.
  82       *
  83       * @param string     $actualresponse
  84       * @param float|null $fraction
  85       * @param int        $try
  86       * @return \core_question\statistics\responses\analysis_for_actual_response
  87       */
  88      public function count_response($actualresponse, $fraction, $try) {
  89          if (!isset($this->actualresponses[$actualresponse])) {
  90              if ($fraction === null) {
  91                  $fraction = $this->fraction;
  92              }
  93              $this->add_response($actualresponse, $fraction);
  94          }
  95          $this->get_response($actualresponse)->increment_count($try);
  96      }
  97  
  98      /**
  99       * Cache analysis for class.
 100       *
 101       * @param \qubaid_condition $qubaids    which question usages have been analysed.
 102       * @param string            $whichtries which tries have been analysed?
 103       * @param int               $questionid which question.
 104       * @param int               $variantno  which variant.
 105       * @param string            $subpartid  which sub part.
 106       * @param int|null          $calculationtime time when the analysis was done. (Defaults to time()).
 107       */
 108      public function cache($qubaids, $whichtries, $questionid, $variantno, $subpartid, $calculationtime = null) {
 109          foreach ($this->get_responses() as $response) {
 110              $analysisforactualresponse = $this->get_response($response);
 111              $analysisforactualresponse->cache($qubaids, $whichtries, $questionid, $variantno, $subpartid,
 112                      $this->responseclassid, $calculationtime);
 113          }
 114      }
 115  
 116      /**
 117       * Add an actual response to the data structure.
 118       *
 119       * @param string $response A string representing the actual response.
 120       * @param float  $fraction The fraction of grade awarded for this response.
 121       */
 122      public function add_response($response, $fraction) {
 123          $this->actualresponses[$response] = new analysis_for_actual_response($response, $fraction);
 124      }
 125  
 126      /**
 127       * Used when loading cached counts.
 128       *
 129       * @param string $response
 130       * @param int $try the try number, will be zero if not keeping track of try.
 131       * @param int $count the count
 132       */
 133      public function set_response_count($response, $try, $count) {
 134          $this->actualresponses[$response]->set_count($try, $count);
 135      }
 136  
 137      /**
 138       * Are there actual responses to sub parts that where classified into this class?
 139       *
 140       * @return bool whether this analysis has a response class with more than one
 141       *      different actual response, or if the actual response is different from
 142       *      the model response.
 143       */
 144      public function has_actual_responses() {
 145          $actualresponses = $this->get_responses();
 146          if (count($actualresponses) > 1) {
 147              return true;
 148          } else if (count($actualresponses) === 1) {
 149              $singleactualresponse = reset($actualresponses);
 150              return (string)$singleactualresponse !== (string)$this->modelresponse;
 151          }
 152          return false;
 153      }
 154  
 155      /**
 156       * Return the data to display in the response analysis table.
 157       *
 158       * @param bool $responseclasscolumn
 159       * @param string $partid
 160       * @return object[]
 161       */
 162      public function data_for_question_response_table($responseclasscolumn, $partid) {
 163          $return = array();
 164          if (count($this->get_responses()) == 0) {
 165              $rowdata = new \stdClass();
 166              $rowdata->part = $partid;
 167              $rowdata->responseclass = $this->modelresponse;
 168              if (!$responseclasscolumn) {
 169                  $rowdata->response = $this->modelresponse;
 170              } else {
 171                  $rowdata->response = '';
 172              }
 173              $rowdata->fraction = $this->fraction;
 174              $rowdata->totalcount = 0;
 175              $rowdata->trycount = array();
 176              $return[] = $rowdata;
 177          } else {
 178              foreach ($this->get_responses() as $actualresponse) {
 179                  $response = $this->get_response($actualresponse);
 180                  $return[] = $response->data_for_question_response_table($partid, $this->modelresponse);
 181              }
 182          }
 183          return $return;
 184      }
 185  
 186      /**
 187       * What is the highest try number that an actual response of this response class has been seen?
 188       *
 189       * @return int try number
 190       */
 191      public function get_maximum_tries() {
 192          $max = 1;
 193          foreach ($this->get_responses() as $actualresponse) {
 194              $max = max($max, $this->get_response($actualresponse)->get_maximum_tries());
 195          }
 196          return $max;
 197      }
 198  
 199      /**
 200       * Return array of the actual responses to this sub part that were classified into this class.
 201       *
 202       * @return string[] the actual responses we are counting tries at.
 203       */
 204      protected function get_responses() {
 205          return array_keys($this->actualresponses);
 206      }
 207  
 208      /**
 209       * Get the data structure used to count the responses that match an actual response within this class of responses.
 210       *
 211       * @param string $response
 212       * @return analysis_for_actual_response the instance for keeping count of tries for $response.
 213       */
 214      protected function get_response($response) {
 215          return $this->actualresponses[$response];
 216      }
 217  }