Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * @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       */
 107      public function cache($qubaids, $whichtries, $questionid, $variantno, $subpartid) {
 108          foreach ($this->get_responses() as $response) {
 109              $analysisforactualresponse = $this->get_response($response);
 110              $analysisforactualresponse->cache($qubaids, $whichtries, $questionid, $variantno, $subpartid, $this->responseclassid);
 111          }
 112      }
 113  
 114      /**
 115       * Add an actual response to the data structure.
 116       *
 117       * @param string $response A string representing the actual response.
 118       * @param float  $fraction The fraction of grade awarded for this response.
 119       */
 120      public function add_response($response, $fraction) {
 121          $this->actualresponses[$response] = new analysis_for_actual_response($response, $fraction);
 122      }
 123  
 124      /**
 125       * Used when loading cached counts.
 126       *
 127       * @param string $response
 128       * @param int $try the try number, will be zero if not keeping track of try.
 129       * @param int $count the count
 130       */
 131      public function set_response_count($response, $try, $count) {
 132          $this->actualresponses[$response]->set_count($try, $count);
 133      }
 134  
 135      /**
 136       * Are there actual responses to sub parts that where classified into this class?
 137       *
 138       * @return bool whether this analysis has a response class with more than one
 139       *      different actual response, or if the actual response is different from
 140       *      the model response.
 141       */
 142      public function has_actual_responses() {
 143          $actualresponses = $this->get_responses();
 144          if (count($actualresponses) > 1) {
 145              return true;
 146          } else if (count($actualresponses) === 1) {
 147              $singleactualresponse = reset($actualresponses);
 148              return (string)$singleactualresponse !== (string)$this->modelresponse;
 149          }
 150          return false;
 151      }
 152  
 153      /**
 154       * Return the data to display in the response analysis table.
 155       *
 156       * @param bool $responseclasscolumn
 157       * @param string $partid
 158       * @return object[]
 159       */
 160      public function data_for_question_response_table($responseclasscolumn, $partid) {
 161          $return = array();
 162          if (count($this->get_responses()) == 0) {
 163              $rowdata = new \stdClass();
 164              $rowdata->part = $partid;
 165              $rowdata->responseclass = $this->modelresponse;
 166              if (!$responseclasscolumn) {
 167                  $rowdata->response = $this->modelresponse;
 168              } else {
 169                  $rowdata->response = '';
 170              }
 171              $rowdata->fraction = $this->fraction;
 172              $rowdata->totalcount = 0;
 173              $rowdata->trycount = array();
 174              $return[] = $rowdata;
 175          } else {
 176              foreach ($this->get_responses() as $actualresponse) {
 177                  $response = $this->get_response($actualresponse);
 178                  $return[] = $response->data_for_question_response_table($partid, $this->modelresponse);
 179              }
 180          }
 181          return $return;
 182      }
 183  
 184      /**
 185       * What is the highest try number that an actual response of this response class has been seen?
 186       *
 187       * @return int try number
 188       */
 189      public function get_maximum_tries() {
 190          $max = 1;
 191          foreach ($this->get_responses() as $actualresponse) {
 192              $max = max($max, $this->get_response($actualresponse)->get_maximum_tries());
 193          }
 194          return $max;
 195      }
 196  
 197      /**
 198       * Return array of the actual responses to this sub part that were classified into this class.
 199       *
 200       * @return string[] the actual responses we are counting tries at.
 201       */
 202      protected function get_responses() {
 203          return array_keys($this->actualresponses);
 204      }
 205  
 206      /**
 207       * Get the data structure used to count the responses that match an actual response within this class of responses.
 208       *
 209       * @param string $response
 210       * @return analysis_for_actual_response the instance for keeping count of tries for $response.
 211       */
 212      protected function get_response($response) {
 213          return $this->actualresponses[$response];
 214      }
 215  }