[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/report/responses/ -> first_or_all_responses_table.php (source)

   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 file defines the quiz responses table for showing first or all tries at a question.
  19   *
  20   * @package   quiz_responses
  21   * @copyright 2014 The Open University
  22   * @author    Jamie Pratt <me@jamiep.org>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * This is a table subclass for displaying the quiz responses report, showing first or all tries.
  30   *
  31   * @package   quiz_responses
  32   * @copyright 2014 The Open University
  33   * @author    Jamie Pratt <me@jamiep.org>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class quiz_first_or_all_responses_table extends quiz_last_responses_table {
  37  
  38      /**
  39       * The full question usage object for each try shown in report.
  40       *
  41       * @var question_usage_by_activity[]
  42       */
  43      protected $questionusagesbyactivity;
  44  
  45      protected function field_from_extra_data($tablerow, $slot, $field) {
  46          $questionattempt = $this->get_question_attempt($tablerow->usageid, $slot);
  47          switch($field) {
  48              case 'questionsummary' :
  49                  return $questionattempt->get_question_summary();
  50              case 'responsesummary' :
  51                  return $this->get_summary_after_try($tablerow, $slot);
  52              case 'rightanswer' :
  53                  return $questionattempt->get_right_answer_summary();
  54              default :
  55                  throw new coding_exception('Unknown question attempt field.');
  56          }
  57      }
  58  
  59  
  60      protected function load_extra_data() {
  61          if (count($this->rawdata) === 0) {
  62              return;
  63          }
  64          $qubaids = $this->get_qubaids_condition();
  65          $dm = new question_engine_data_mapper();
  66          $this->questionusagesbyactivity = $dm->load_questions_usages_by_activity($qubaids);
  67  
  68          // Insert an extra field in attempt data and extra rows where necessary.
  69          $newrawdata = array();
  70          foreach ($this->rawdata as $attempt) {
  71              $maxtriesinanyslot = 1;
  72              foreach ($this->questionusagesbyactivity[$attempt->usageid]->get_slots() as $slot) {
  73                  $tries = $this->get_no_of_tries($attempt, $slot);
  74                  $maxtriesinanyslot = max($maxtriesinanyslot, $tries);
  75              }
  76              for ($try = 1; $try <= $maxtriesinanyslot; $try++) {
  77                  $newtablerow = clone($attempt);
  78                  $newtablerow->lasttryforallparts = ($try == $maxtriesinanyslot);
  79                  if ($try !== $maxtriesinanyslot) {
  80                      $newtablerow->state = quiz_attempt::IN_PROGRESS;
  81                  }
  82                  $newtablerow->try = $try;
  83                  $newrawdata[] = $newtablerow;
  84                  if ($this->options->whichtries == question_attempt::FIRST_TRY) {
  85                      break;
  86                  }
  87              }
  88          }
  89          $this->rawdata = $newrawdata;
  90      }
  91  
  92      /**
  93       * Return the question attempt object.
  94       *
  95       * @param int $questionusagesid
  96       * @param int $slot
  97       * @return question_attempt
  98       */
  99      protected function get_question_attempt($questionusagesid, $slot) {
 100          return $this->questionusagesbyactivity[$questionusagesid]->get_question_attempt($slot);
 101      }
 102  
 103      /**
 104       * Find the state for $slot given after this try.
 105       *
 106       * @param object $tablerow row data
 107       * @param int $slot Slot number.
 108       * @return question_state The question state after the attempt.
 109       */
 110      protected function slot_state($tablerow, $slot) {
 111          $qa = $this->get_question_attempt($tablerow->usageid, $slot);
 112          $submissionsteps = $qa->get_steps_with_submitted_response_iterator();
 113          $step = $submissionsteps[$tablerow->try];
 114          if ($step === null) {
 115              return null;
 116          }
 117          if ($this->is_last_try($tablerow, $slot, $tablerow->try)) {
 118              // If this is the last try then the step with the try data does not contain the correct state. We need to
 119              // use the last step's state, after the attempt has been finished.
 120              return $qa->get_state();
 121          }
 122          return $step->get_state();
 123      }
 124  
 125  
 126      /**
 127       * Get the summary of the response after the try.
 128       *
 129       * @param object $tablerow row data
 130       * @param int $slot Slot number.
 131       * @return string summary for the question after this try.
 132       */
 133      public function get_summary_after_try($tablerow, $slot) {
 134          $qa = $this->get_question_attempt($tablerow->usageid, $slot);
 135          if (!($qa->get_question() instanceof question_manually_gradable)) {
 136              // No responses, and we cannot call summarise_response below.
 137              return null;
 138          }
 139          $submissionsteps = $qa->get_steps_with_submitted_response_iterator();
 140          $step = $submissionsteps[$tablerow->try];
 141          if ($step === null) {
 142              return null;
 143          }
 144          $qtdata = $step->get_qt_data();
 145          return $qa->get_question()->summarise_response($qtdata);
 146      }
 147  
 148      /**
 149       * Has this question usage been flagged?
 150       *
 151       * @param int $questionusageid Question usage id.
 152       * @param int $slot Slot number
 153       * @return bool Has it been flagged?
 154       */
 155      protected function is_flagged($questionusageid, $slot) {
 156          return $this->get_question_attempt($questionusageid, $slot)->is_flagged();
 157      }
 158  
 159      /**
 160       * The grade for this slot after this try.
 161       *
 162       * @param object $tablerow attempt data from db.
 163       * @param int $slot Slot number.
 164       * @return float The fraction.
 165       */
 166      protected function slot_fraction($tablerow, $slot) {
 167          $qa = $this->get_question_attempt($tablerow->usageid, $slot);
 168          $submissionsteps = $qa->get_steps_with_submitted_response_iterator();
 169          $step = $submissionsteps[$tablerow->try];
 170          if ($step === null) {
 171              return null;
 172          }
 173          if ($this->is_last_try($tablerow, $slot, $tablerow->try)) {
 174              // If this is the last try then the step with the try data does not contain the correct fraction. We need to
 175              // use the last step's fraction, after the attempt has been finished.
 176              return $qa->get_fraction();
 177          }
 178          return $step->get_fraction();
 179      }
 180  
 181      /**
 182       * Is this the last try in the question attempt?
 183       *
 184       * @param object $tablerow attempt data from db.
 185       * @param int $slot Slot number
 186       * @param int $tryno try no
 187       * @return bool Is it the last try?
 188       */
 189      protected function is_last_try($tablerow, $slot, $tryno) {
 190          return $tryno == $this->get_no_of_tries($tablerow, $slot);
 191      }
 192  
 193      /**
 194       * How many tries were attempted at this question in this slot, during this usage?
 195       *
 196       * @param object $tablerow attempt data from db.
 197       * @param int $slot Slot number
 198       * @return int the number of tries in the question attempt for slot $slot.
 199       */
 200      public function get_no_of_tries($tablerow, $slot) {
 201          return count($this->get_question_attempt($tablerow->usageid, $slot)->get_steps_with_submitted_response_iterator());
 202      }
 203  
 204  
 205      /**
 206       * What is the step no this try was seen in?
 207       *
 208       * @param int $questionusageid The question usage id.
 209       * @param int $slot Slot number
 210       * @param int $tryno Try no
 211       * @return int the step no or zero if not found
 212       */
 213      protected function step_no_for_try($questionusageid, $slot, $tryno) {
 214          $qa = $this->get_question_attempt($questionusageid, $slot);
 215          return $qa->get_steps_with_submitted_response_iterator()->step_no_for_try($tryno);
 216      }
 217  
 218      public function col_checkbox($tablerow) {
 219          if ($tablerow->try != 1) {
 220              return '';
 221          } else {
 222              return parent::col_checkbox($tablerow);
 223          }
 224      }
 225  
 226      /**
 227       * Cell value function for email column. This extracts the contents for any cell in the email column from the row data.
 228       *
 229       * @param object $tablerow Row data.
 230       * @return string   What to put in the cell for this column, for this row data.
 231       */
 232      public function col_email($tablerow) {
 233          if ($tablerow->try != 1) {
 234              return '';
 235          } else {
 236              return $tablerow->email;
 237          }
 238      }
 239  
 240      /**
 241       * Cell value function for sumgrades column. This extracts the contents for any cell in the sumgrades column from the row data.
 242       *
 243       * @param object $tablerow Row data.
 244       * @return string   What to put in the cell for this column, for this row data.
 245       */
 246      public function col_sumgrades($tablerow) {
 247          if (!$tablerow->lasttryforallparts) {
 248              return '';
 249          } else {
 250              return parent::col_sumgrades($tablerow);
 251          }
 252      }
 253  
 254  
 255      public function col_state($tablerow) {
 256          if (!$tablerow->lasttryforallparts) {
 257              return '';
 258          } else {
 259              return parent::col_state($tablerow);
 260          }
 261      }
 262  
 263      public function get_row_class($tablerow) {
 264          if ($this->options->whichtries == question_attempt::ALL_TRIES && $tablerow->lasttryforallparts) {
 265              return 'lastrowforattempt';
 266          } else {
 267              return '';
 268          }
 269      }
 270  
 271      public function make_review_link($data, $tablerow, $slot) {
 272          if ($this->slot_state($tablerow, $slot) === null) {
 273              return $data;
 274          } else {
 275              return parent::make_review_link($data, $tablerow, $slot);
 276          }
 277      }
 278  }
 279  
 280  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1