[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/report/overview/ -> report.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 overview report class.
  19   *
  20   * @package   quiz_overview
  21   * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport.php');
  29  require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_options.php');
  30  require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_form.php');
  31  require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_table.php');
  32  
  33  
  34  /**
  35   * Quiz report subclass for the overview (grades) report.
  36   *
  37   * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class quiz_overview_report extends quiz_attempts_report {
  41  
  42      public function display($quiz, $cm, $course) {
  43          global $CFG, $DB, $OUTPUT, $PAGE;
  44  
  45          list($currentgroup, $students, $groupstudents, $allowed) =
  46                  $this->init('overview', 'quiz_overview_settings_form', $quiz, $cm, $course);
  47          $options = new quiz_overview_options('overview', $quiz, $cm, $course);
  48  
  49          if ($fromform = $this->form->get_data()) {
  50              $options->process_settings_from_form($fromform);
  51  
  52          } else {
  53              $options->process_settings_from_params();
  54          }
  55  
  56          $this->form->set_data($options->get_initial_form_data());
  57  
  58          if ($options->attempts == self::ALL_WITH) {
  59              // This option is only available to users who can access all groups in
  60              // groups mode, so setting allowed to empty (which means all quiz attempts
  61              // are accessible, is not a security porblem.
  62              $allowed = array();
  63          }
  64  
  65          // Load the required questions.
  66          $questions = quiz_report_get_significant_questions($quiz);
  67  
  68          // Prepare for downloading, if applicable.
  69          $courseshortname = format_string($course->shortname, true,
  70                  array('context' => context_course::instance($course->id)));
  71          $table = new quiz_overview_table($quiz, $this->context, $this->qmsubselect,
  72                  $options, $groupstudents, $students, $questions, $options->get_url());
  73          $filename = quiz_report_download_filename(get_string('overviewfilename', 'quiz_overview'),
  74                  $courseshortname, $quiz->name);
  75          $table->is_downloading($options->download, $filename,
  76                  $courseshortname . ' ' . format_string($quiz->name, true));
  77          if ($table->is_downloading()) {
  78              raise_memory_limit(MEMORY_EXTRA);
  79          }
  80  
  81          $this->course = $course; // Hack to make this available in process_actions.
  82          $this->process_actions($quiz, $cm, $currentgroup, $groupstudents, $allowed, $options->get_url());
  83  
  84          // Start output.
  85          if (!$table->is_downloading()) {
  86              // Only print headers if not asked to download data.
  87              $this->print_header_and_tabs($cm, $course, $quiz, $this->mode);
  88          }
  89  
  90          if ($groupmode = groups_get_activity_groupmode($cm)) {
  91              // Groups are being used, so output the group selector if we are not downloading.
  92              if (!$table->is_downloading()) {
  93                  groups_print_activity_menu($cm, $options->get_url());
  94              }
  95          }
  96  
  97          // Print information on the number of existing attempts.
  98          if (!$table->is_downloading()) {
  99              // Do not print notices when downloading.
 100              if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, true, $currentgroup)) {
 101                  echo '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
 102              }
 103          }
 104  
 105          $hasquestions = quiz_has_questions($quiz->id);
 106          if (!$table->is_downloading()) {
 107              if (!$hasquestions) {
 108                  echo quiz_no_questions_message($quiz, $cm, $this->context);
 109              } else if (!$students) {
 110                  echo $OUTPUT->notification(get_string('nostudentsyet'));
 111              } else if ($currentgroup && !$groupstudents) {
 112                  echo $OUTPUT->notification(get_string('nostudentsingroup'));
 113              }
 114  
 115              // Print the display options.
 116              $this->form->display();
 117          }
 118  
 119          $hasstudents = $students && (!$currentgroup || $groupstudents);
 120          if ($hasquestions && ($hasstudents || $options->attempts == self::ALL_WITH)) {
 121              // Construct the SQL.
 122              $fields = $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') .
 123                      ' AS uniqueid, ';
 124  
 125              list($fields, $from, $where, $params) = $table->base_sql($allowed);
 126  
 127              $table->set_count_sql("SELECT COUNT(1) FROM $from WHERE $where", $params);
 128  
 129              // Test to see if there are any regraded attempts to be listed.
 130              $fields .= ", COALESCE((
 131                                  SELECT MAX(qqr.regraded)
 132                                    FROM {quiz_overview_regrades} qqr
 133                                   WHERE qqr.questionusageid = quiza.uniqueid
 134                            ), -1) AS regraded";
 135              if ($options->onlyregraded) {
 136                  $where .= " AND COALESCE((
 137                                      SELECT MAX(qqr.regraded)
 138                                        FROM {quiz_overview_regrades} qqr
 139                                       WHERE qqr.questionusageid = quiza.uniqueid
 140                                  ), -1) <> -1";
 141              }
 142              $table->set_sql($fields, $from, $where, $params);
 143  
 144              if (!$table->is_downloading()) {
 145                  // Output the regrade buttons.
 146                  if (has_capability('mod/quiz:regrade', $this->context)) {
 147                      $regradesneeded = $this->count_question_attempts_needing_regrade(
 148                              $quiz, $groupstudents);
 149                      if ($currentgroup) {
 150                          $a= new stdClass();
 151                          $a->groupname = groups_get_group_name($currentgroup);
 152                          $a->coursestudents = get_string('participants');
 153                          $a->countregradeneeded = $regradesneeded;
 154                          $regradealldrydolabel =
 155                                  get_string('regradealldrydogroup', 'quiz_overview', $a);
 156                          $regradealldrylabel =
 157                                  get_string('regradealldrygroup', 'quiz_overview', $a);
 158                          $regradealllabel =
 159                                  get_string('regradeallgroup', 'quiz_overview', $a);
 160                      } else {
 161                          $regradealldrydolabel =
 162                                  get_string('regradealldrydo', 'quiz_overview', $regradesneeded);
 163                          $regradealldrylabel =
 164                                  get_string('regradealldry', 'quiz_overview');
 165                          $regradealllabel =
 166                                  get_string('regradeall', 'quiz_overview');
 167                      }
 168                      $displayurl = new moodle_url($options->get_url(), array('sesskey' => sesskey()));
 169                      echo '<div class="mdl-align">';
 170                      echo '<form action="'.$displayurl->out_omit_querystring().'">';
 171                      echo '<div>';
 172                      echo html_writer::input_hidden_params($displayurl);
 173                      echo '<input type="submit" name="regradeall" value="'.$regradealllabel.'"/>';
 174                      echo '<input type="submit" name="regradealldry" value="' .
 175                              $regradealldrylabel . '"/>';
 176                      if ($regradesneeded) {
 177                          echo '<input type="submit" name="regradealldrydo" value="' .
 178                                  $regradealldrydolabel . '"/>';
 179                      }
 180                      echo '</div>';
 181                      echo '</form>';
 182                      echo '</div>';
 183                  }
 184                  // Print information on the grading method.
 185                  if ($strattempthighlight = quiz_report_highlighting_grading_method(
 186                          $quiz, $this->qmsubselect, $options->onlygraded)) {
 187                      echo '<div class="quizattemptcounts">' . $strattempthighlight . '</div>';
 188                  }
 189              }
 190  
 191              // Define table columns.
 192              $columns = array();
 193              $headers = array();
 194  
 195              if (!$table->is_downloading() && $options->checkboxcolumn) {
 196                  $columns[] = 'checkbox';
 197                  $headers[] = null;
 198              }
 199  
 200              $this->add_user_columns($table, $columns, $headers);
 201              $this->add_state_column($columns, $headers);
 202              $this->add_time_columns($columns, $headers);
 203  
 204              $this->add_grade_columns($quiz, $options->usercanseegrades, $columns, $headers, false);
 205  
 206              if (!$table->is_downloading() && has_capability('mod/quiz:regrade', $this->context) &&
 207                      $this->has_regraded_questions($from, $where, $params)) {
 208                  $columns[] = 'regraded';
 209                  $headers[] = get_string('regrade', 'quiz_overview');
 210              }
 211  
 212              if ($options->slotmarks) {
 213                  foreach ($questions as $slot => $question) {
 214                      // Ignore questions of zero length.
 215                      $columns[] = 'qsgrade' . $slot;
 216                      $header = get_string('qbrief', 'quiz', $question->number);
 217                      if (!$table->is_downloading()) {
 218                          $header .= '<br />';
 219                      } else {
 220                          $header .= ' ';
 221                      }
 222                      $header .= '/' . quiz_rescale_grade($question->maxmark, $quiz, 'question');
 223                      $headers[] = $header;
 224                  }
 225              }
 226  
 227              $this->set_up_table_columns($table, $columns, $headers, $this->get_base_url(), $options, false);
 228              $table->set_attribute('class', 'generaltable generalbox grades');
 229  
 230              $table->out($options->pagesize, true);
 231          }
 232  
 233          if (!$table->is_downloading() && $options->usercanseegrades) {
 234              $output = $PAGE->get_renderer('mod_quiz');
 235              list($bands, $bandwidth) = self::get_bands_count_and_width($quiz);
 236              $labels = self::get_bands_labels($bands, $bandwidth, $quiz);
 237  
 238              if ($currentgroup && $groupstudents) {
 239                  list($usql, $params) = $DB->get_in_or_equal($groupstudents);
 240                  $params[] = $quiz->id;
 241                  if ($DB->record_exists_select('quiz_grades', "userid $usql AND quiz = ?", $params)) {
 242                      $data = quiz_report_grade_bands($bandwidth, $bands, $quiz->id, $groupstudents);
 243                      $chart = self::get_chart($labels, $data);
 244                      $graphname = get_string('overviewreportgraphgroup', 'quiz_overview', groups_get_group_name($currentgroup));
 245                      echo $output->chart($chart, $graphname);
 246                  }
 247              }
 248  
 249              if ($DB->record_exists('quiz_grades', array('quiz'=> $quiz->id))) {
 250                  $data = quiz_report_grade_bands($bandwidth, $bands, $quiz->id, []);
 251                  $chart = self::get_chart($labels, $data);
 252                  $graphname = get_string('overviewreportgraph', 'quiz_overview');
 253                  echo $output->chart($chart, $graphname);
 254              }
 255          }
 256          return true;
 257      }
 258  
 259      protected function process_actions($quiz, $cm, $currentgroup, $groupstudents, $allowed, $redirecturl) {
 260          parent::process_actions($quiz, $cm, $currentgroup, $groupstudents, $allowed, $redirecturl);
 261  
 262          if (empty($currentgroup) || $groupstudents) {
 263              if (optional_param('regrade', 0, PARAM_BOOL) && confirm_sesskey()) {
 264                  if ($attemptids = optional_param_array('attemptid', array(), PARAM_INT)) {
 265                      $this->start_regrade($quiz, $cm);
 266                      $this->regrade_attempts($quiz, false, $groupstudents, $attemptids);
 267                      $this->finish_regrade($redirecturl);
 268                  }
 269              }
 270          }
 271  
 272          if (optional_param('regradeall', 0, PARAM_BOOL) && confirm_sesskey()) {
 273              $this->start_regrade($quiz, $cm);
 274              $this->regrade_attempts($quiz, false, $groupstudents);
 275              $this->finish_regrade($redirecturl);
 276  
 277          } else if (optional_param('regradealldry', 0, PARAM_BOOL) && confirm_sesskey()) {
 278              $this->start_regrade($quiz, $cm);
 279              $this->regrade_attempts($quiz, true, $groupstudents);
 280              $this->finish_regrade($redirecturl);
 281  
 282          } else if (optional_param('regradealldrydo', 0, PARAM_BOOL) && confirm_sesskey()) {
 283              $this->start_regrade($quiz, $cm);
 284              $this->regrade_attempts_needing_it($quiz, $groupstudents);
 285              $this->finish_regrade($redirecturl);
 286          }
 287      }
 288  
 289      /**
 290       * Check necessary capabilities, and start the display of the regrade progress page.
 291       * @param object $quiz the quiz settings.
 292       * @param object $cm the cm object for the quiz.
 293       */
 294      protected function start_regrade($quiz, $cm) {
 295          global $OUTPUT, $PAGE;
 296          require_capability('mod/quiz:regrade', $this->context);
 297          $this->print_header_and_tabs($cm, $this->course, $quiz, $this->mode);
 298      }
 299  
 300      /**
 301       * Finish displaying the regrade progress page.
 302       * @param moodle_url $nexturl where to send the user after the regrade.
 303       * @uses exit. This method never returns.
 304       */
 305      protected function finish_regrade($nexturl) {
 306          global $OUTPUT;
 307          \core\notification::success(get_string('regradecomplete', 'quiz_overview'));
 308          echo $OUTPUT->continue_button($nexturl);
 309          echo $OUTPUT->footer();
 310          die();
 311      }
 312  
 313      /**
 314       * Unlock the session and allow the regrading process to run in the background.
 315       */
 316      protected function unlock_session() {
 317          \core\session\manager::write_close();
 318          ignore_user_abort(true);
 319      }
 320  
 321      /**
 322       * Regrade a particular quiz attempt. Either for real ($dryrun = false), or
 323       * as a pretend regrade to see which fractions would change. The outcome is
 324       * stored in the quiz_overview_regrades table.
 325       *
 326       * Note, $attempt is not upgraded in the database. The caller needs to do that.
 327       * However, $attempt->sumgrades is updated, if this is not a dry run.
 328       *
 329       * @param object $attempt the quiz attempt to regrade.
 330       * @param bool $dryrun if true, do a pretend regrade, otherwise do it for real.
 331       * @param array $slots if null, regrade all questions, otherwise, just regrade
 332       *      the quetsions with those slots.
 333       */
 334      protected function regrade_attempt($attempt, $dryrun = false, $slots = null) {
 335          global $DB;
 336          // Need more time for a quiz with many questions.
 337          core_php_time_limit::raise(300);
 338  
 339          $transaction = $DB->start_delegated_transaction();
 340  
 341          $quba = question_engine::load_questions_usage_by_activity($attempt->uniqueid);
 342  
 343          if (is_null($slots)) {
 344              $slots = $quba->get_slots();
 345          }
 346  
 347          $finished = $attempt->state == quiz_attempt::FINISHED;
 348          foreach ($slots as $slot) {
 349              $qqr = new stdClass();
 350              $qqr->oldfraction = $quba->get_question_fraction($slot);
 351  
 352              $quba->regrade_question($slot, $finished);
 353  
 354              $qqr->newfraction = $quba->get_question_fraction($slot);
 355  
 356              if (abs($qqr->oldfraction - $qqr->newfraction) > 1e-7) {
 357                  $qqr->questionusageid = $quba->get_id();
 358                  $qqr->slot = $slot;
 359                  $qqr->regraded = empty($dryrun);
 360                  $qqr->timemodified = time();
 361                  $DB->insert_record('quiz_overview_regrades', $qqr, false);
 362              }
 363          }
 364  
 365          if (!$dryrun) {
 366              question_engine::save_questions_usage_by_activity($quba);
 367          }
 368  
 369          $transaction->allow_commit();
 370  
 371          // Really, PHP should not need this hint, but without this, we just run out of memory.
 372          $quba = null;
 373          $transaction = null;
 374          gc_collect_cycles();
 375      }
 376  
 377      /**
 378       * Regrade attempts for this quiz, exactly which attempts are regraded is
 379       * controlled by the parameters.
 380       * @param object $quiz the quiz settings.
 381       * @param bool $dryrun if true, do a pretend regrade, otherwise do it for real.
 382       * @param array $groupstudents blank for all attempts, otherwise regrade attempts
 383       * for these users.
 384       * @param array $attemptids blank for all attempts, otherwise only regrade
 385       * attempts whose id is in this list.
 386       */
 387      protected function regrade_attempts($quiz, $dryrun = false,
 388              $groupstudents = array(), $attemptids = array()) {
 389          global $DB;
 390          $this->unlock_session();
 391  
 392          $where = "quiz = ? AND preview = 0";
 393          $params = array($quiz->id);
 394  
 395          if ($groupstudents) {
 396              list($usql, $uparams) = $DB->get_in_or_equal($groupstudents);
 397              $where .= " AND userid $usql";
 398              $params = array_merge($params, $uparams);
 399          }
 400  
 401          if ($attemptids) {
 402              list($asql, $aparams) = $DB->get_in_or_equal($attemptids);
 403              $where .= " AND id $asql";
 404              $params = array_merge($params, $aparams);
 405          }
 406  
 407          $attempts = $DB->get_records_select('quiz_attempts', $where, $params);
 408          if (!$attempts) {
 409              return;
 410          }
 411  
 412          $this->clear_regrade_table($quiz, $groupstudents);
 413  
 414          $progressbar = new progress_bar('quiz_overview_regrade', 500, true);
 415          $a = array(
 416              'count' => count($attempts),
 417              'done'  => 0,
 418          );
 419          foreach ($attempts as $attempt) {
 420              $this->regrade_attempt($attempt, $dryrun);
 421              $a['done']++;
 422              $progressbar->update($a['done'], $a['count'],
 423                      get_string('regradingattemptxofy', 'quiz_overview', $a));
 424          }
 425  
 426          if (!$dryrun) {
 427              $this->update_overall_grades($quiz);
 428          }
 429      }
 430  
 431      /**
 432       * Regrade those questions in those attempts that are marked as needing regrading
 433       * in the quiz_overview_regrades table.
 434       * @param object $quiz the quiz settings.
 435       * @param array $groupstudents blank for all attempts, otherwise regrade attempts
 436       * for these users.
 437       */
 438      protected function regrade_attempts_needing_it($quiz, $groupstudents) {
 439          global $DB;
 440          $this->unlock_session();
 441  
 442          $where = "quiza.quiz = ? AND quiza.preview = 0 AND qqr.regraded = 0";
 443          $params = array($quiz->id);
 444  
 445          // Fetch all attempts that need regrading.
 446          if ($groupstudents) {
 447              list($usql, $uparams) = $DB->get_in_or_equal($groupstudents);
 448              $where .= " AND quiza.userid $usql";
 449              $params = array_merge($params, $uparams);
 450          }
 451  
 452          $toregrade = $DB->get_recordset_sql("
 453                  SELECT quiza.uniqueid, qqr.slot
 454                  FROM {quiz_attempts} quiza
 455                  JOIN {quiz_overview_regrades} qqr ON qqr.questionusageid = quiza.uniqueid
 456                  WHERE $where", $params);
 457  
 458          $attemptquestions = array();
 459          foreach ($toregrade as $row) {
 460              $attemptquestions[$row->uniqueid][] = $row->slot;
 461          }
 462          $toregrade->close();
 463  
 464          if (!$attemptquestions) {
 465              return;
 466          }
 467  
 468          $attempts = $DB->get_records_list('quiz_attempts', 'uniqueid',
 469                  array_keys($attemptquestions));
 470  
 471          $this->clear_regrade_table($quiz, $groupstudents);
 472  
 473          $progressbar = new progress_bar('quiz_overview_regrade', 500, true);
 474          $a = array(
 475              'count' => count($attempts),
 476              'done'  => 0,
 477          );
 478          foreach ($attempts as $attempt) {
 479              $this->regrade_attempt($attempt, false, $attemptquestions[$attempt->uniqueid]);
 480              $a['done']++;
 481              $progressbar->update($a['done'], $a['count'],
 482                      get_string('regradingattemptxofy', 'quiz_overview', $a));
 483          }
 484  
 485          $this->update_overall_grades($quiz);
 486      }
 487  
 488      /**
 489       * Count the number of attempts in need of a regrade.
 490       * @param object $quiz the quiz settings.
 491       * @param array $groupstudents user ids. If this is given, only data relating
 492       * to these users is cleared.
 493       */
 494      protected function count_question_attempts_needing_regrade($quiz, $groupstudents) {
 495          global $DB;
 496  
 497          $usertest = '';
 498          $params = array();
 499          if ($groupstudents) {
 500              list($usql, $params) = $DB->get_in_or_equal($groupstudents);
 501              $usertest = "quiza.userid $usql AND ";
 502          }
 503  
 504          $params[] = $quiz->id;
 505          $sql = "SELECT COUNT(DISTINCT quiza.id)
 506                  FROM {quiz_attempts} quiza
 507                  JOIN {quiz_overview_regrades} qqr ON quiza.uniqueid = qqr.questionusageid
 508                  WHERE
 509                      $usertest
 510                      quiza.quiz = ? AND
 511                      quiza.preview = 0 AND
 512                      qqr.regraded = 0";
 513          return $DB->count_records_sql($sql, $params);
 514      }
 515  
 516      /**
 517       * Are there any pending regrades in the table we are going to show?
 518       * @param string $from tables used by the main query.
 519       * @param string $where where clause used by the main query.
 520       * @param array $params required by the SQL.
 521       * @return bool whether there are pending regrades.
 522       */
 523      protected function has_regraded_questions($from, $where, $params) {
 524          global $DB;
 525          return $DB->record_exists_sql("
 526                  SELECT 1
 527                    FROM {$from}
 528                    JOIN {quiz_overview_regrades} qor ON qor.questionusageid = quiza.uniqueid
 529                   WHERE {$where}", $params);
 530      }
 531  
 532      /**
 533       * Remove all information about pending/complete regrades from the database.
 534       * @param object $quiz the quiz settings.
 535       * @param array $groupstudents user ids. If this is given, only data relating
 536       * to these users is cleared.
 537       */
 538      protected function clear_regrade_table($quiz, $groupstudents) {
 539          global $DB;
 540  
 541          // Fetch all attempts that need regrading.
 542          $where = '';
 543          $params = array();
 544          if ($groupstudents) {
 545              list($usql, $params) = $DB->get_in_or_equal($groupstudents);
 546              $where = "userid $usql AND ";
 547          }
 548  
 549          $params[] = $quiz->id;
 550          $DB->delete_records_select('quiz_overview_regrades',
 551                  "questionusageid IN (
 552                      SELECT uniqueid
 553                      FROM {quiz_attempts}
 554                      WHERE $where quiz = ?
 555                  )", $params);
 556      }
 557  
 558      /**
 559       * Update the final grades for all attempts. This method is used following
 560       * a regrade.
 561       * @param object $quiz the quiz settings.
 562       * @param array $userids only update scores for these userids.
 563       * @param array $attemptids attemptids only update scores for these attempt ids.
 564       */
 565      protected function update_overall_grades($quiz) {
 566          quiz_update_all_attempt_sumgrades($quiz);
 567          quiz_update_all_final_grades($quiz);
 568          quiz_update_grades($quiz);
 569      }
 570  
 571      /**
 572       * Get the bands configuration for the quiz.
 573       *
 574       * This returns the configuration for having between 11 and 20 bars in
 575       * a chart based on the maximum grade to be given on a quiz. The width of
 576       * a band is the number of grade points it encapsulates.
 577       *
 578       * @param object $quiz The quiz object.
 579       * @return array Contains the number of bands, and their width.
 580       */
 581      public static function get_bands_count_and_width($quiz) {
 582          $bands = $quiz->grade;
 583          while ($bands > 20 || $bands <= 10) {
 584              if ($bands > 50) {
 585                  $bands /= 5;
 586              } else if ($bands > 20) {
 587                  $bands /= 2;
 588              }
 589              if ($bands < 4) {
 590                  $bands *= 5;
 591              } else if ($bands <= 10) {
 592                  $bands *= 2;
 593              }
 594          }
 595          // See MDL-34589. Using doubles as array keys causes problems in PHP 5.4, hence the explicit cast to int.
 596          $bands = (int) ceil($bands);
 597          return [$bands, $quiz->grade / $bands];
 598      }
 599  
 600      /**
 601       * Get the bands labels.
 602       *
 603       * @param int $bands The number of bands.
 604       * @param int $bandwidth The band width.
 605       * @param object $quiz The quiz object.
 606       * @return string[] The labels.
 607       */
 608      public static function get_bands_labels($bands, $bandwidth, $quiz) {
 609          $bandlabels = [];
 610          for ($i = 1; $i <= $bands; $i++) {
 611              $bandlabels[] = quiz_format_grade($quiz, ($i - 1) * $bandwidth) . ' - ' . quiz_format_grade($quiz, $i * $bandwidth);
 612          }
 613          return $bandlabels;
 614      }
 615  
 616      /**
 617       * Get a chart.
 618       *
 619       * @param string[] $labels Chart labels.
 620       * @param int[] $data The data.
 621       * @return \core\chart_base
 622       */
 623      protected static function get_chart($labels, $data) {
 624          $chart = new \core\chart_bar();
 625          $chart->set_labels($labels);
 626          $chart->get_xaxis(0, true)->set_label(get_string('grade'));
 627  
 628          $yaxis = $chart->get_yaxis(0, true);
 629          $yaxis->set_label(get_string('participants'));
 630          $yaxis->set_stepsize(max(1, round(max($data) / 10)));
 631  
 632          $series = new \core\chart_series(get_string('participants'), $data);
 633          $chart->add_series($series);
 634          return $chart;
 635      }
 636  }


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