[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/report/overview/tests/ -> report_test.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   * Tests for the quiz overview report.
  19   *
  20   * @package   quiz_overview
  21   * @copyright 2014 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  29  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  30  require_once($CFG->dirroot . '/mod/quiz/report/default.php');
  31  require_once($CFG->dirroot . '/mod/quiz/report/overview/report.php');
  32  
  33  
  34  /**
  35   * Tests for the quiz overview report.
  36   *
  37   * @copyright  2014 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class quiz_overview_report_testcase extends advanced_testcase {
  41  
  42      public function test_report_sql() {
  43          global $DB, $SITE;
  44          $this->resetAfterTest(true);
  45  
  46          $generator = $this->getDataGenerator();
  47          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
  48          $quiz = $quizgenerator->create_instance(array('course' => $SITE->id,
  49                  'grademethod' => QUIZ_GRADEHIGHEST, 'grade' => 100.0, 'sumgrades' => 10.0,
  50                  'attempts' => 10));
  51  
  52          $student1 = $generator->create_user();
  53          $student2 = $generator->create_user();
  54          $student3 = $generator->create_user();
  55  
  56          $quizid = 123;
  57          $timestamp = 1234567890;
  58  
  59          // The test data.
  60          $fields = array('quiz', 'userid', 'attempt', 'sumgrades', 'state');
  61          $attempts = array(
  62              array($quiz->id, $student1->id, 1, 0.0,  quiz_attempt::FINISHED),
  63              array($quiz->id, $student1->id, 2, 5.0,  quiz_attempt::FINISHED),
  64              array($quiz->id, $student1->id, 3, 8.0,  quiz_attempt::FINISHED),
  65              array($quiz->id, $student1->id, 4, null, quiz_attempt::ABANDONED),
  66              array($quiz->id, $student1->id, 5, null, quiz_attempt::IN_PROGRESS),
  67              array($quiz->id, $student2->id, 1, null, quiz_attempt::ABANDONED),
  68              array($quiz->id, $student2->id, 2, null, quiz_attempt::ABANDONED),
  69              array($quiz->id, $student2->id, 3, 7.0,  quiz_attempt::FINISHED),
  70              array($quiz->id, $student2->id, 4, null, quiz_attempt::ABANDONED),
  71              array($quiz->id, $student2->id, 5, null, quiz_attempt::ABANDONED),
  72          );
  73  
  74          // Load it in to quiz attempts table.
  75          $uniqueid = 1;
  76          foreach ($attempts as $attempt) {
  77              $data = array_combine($fields, $attempt);
  78              $data['timestart'] = $timestamp + 3600 * $data['attempt'];
  79              $data['timemodifed'] = $data['timestart'];
  80              if ($data['state'] == quiz_attempt::FINISHED) {
  81                  $data['timefinish'] = $data['timestart'] + 600;
  82                  $data['timemodifed'] = $data['timefinish'];
  83              }
  84              $data['layout'] = ''; // Not used, but cannot be null.
  85              $data['uniqueid'] = $uniqueid++;
  86              $data['preview'] = 0;
  87              $DB->insert_record('quiz_attempts', $data);
  88          }
  89  
  90          // Actually getting the SQL to run is quit hard. Do a minimal set up of
  91          // some objects.
  92          $context = context_module::instance($quiz->cmid);
  93          $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
  94          $qmsubselect = quiz_report_qm_filter_select($quiz);
  95          $reportstudents = array($student1->id, $student2->id, $student3->id);
  96  
  97          // Set the options.
  98          $reportoptions = new quiz_overview_options('overview', $quiz, $cm, null);
  99          $reportoptions->attempts = quiz_attempts_report::ENROLLED_ALL;
 100          $reportoptions->onlygraded = true;
 101          $reportoptions->states = array(quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE, quiz_attempt::FINISHED);
 102  
 103          // Now do a minimal set-up of the table class.
 104          $table = new quiz_overview_table($quiz, $context, $qmsubselect, $reportoptions,
 105                  array(), $reportstudents, array(1), null);
 106          $table->define_columns(array('attempt'));
 107          $table->sortable(true, 'uniqueid');
 108          $table->define_baseurl(new moodle_url('/mod/quiz/report.php'));
 109          $table->setup();
 110  
 111          // Run the query.
 112          list($fields, $from, $where, $params) = $table->base_sql($reportstudents);
 113          $table->set_sql($fields, $from, $where, $params);
 114          $table->query_db(30, false);
 115  
 116          // Verify what was returned: Student 1's best and in progress attempts.
 117          // Stuent 2's finshed attempt, and Student 3 with no attempt.
 118          // The array key is {student id}#{attempt number}.
 119          $this->assertEquals(4, count($table->rawdata));
 120          $this->assertArrayHasKey($student1->id . '#3', $table->rawdata);
 121          $this->assertEquals(1, $table->rawdata[$student1->id . '#3']->gradedattempt);
 122          $this->assertArrayHasKey($student1->id . '#3', $table->rawdata);
 123          $this->assertEquals(0, $table->rawdata[$student1->id . '#5']->gradedattempt);
 124          $this->assertArrayHasKey($student2->id . '#3', $table->rawdata);
 125          $this->assertEquals(1, $table->rawdata[$student2->id . '#3']->gradedattempt);
 126          $this->assertArrayHasKey($student3->id . '#0', $table->rawdata);
 127          $this->assertEquals(0, $table->rawdata[$student3->id . '#0']->gradedattempt);
 128      }
 129  
 130      /**
 131       * Bands provider.
 132       * @return array
 133       */
 134      public function get_bands_count_and_width_provider() {
 135          return [
 136              [10, [20, .5]],
 137              [20, [20, 1]],
 138              [30, [15, 2]],
 139              // TODO MDL-55068 Handle bands better when grade is 50.
 140              // [50, [10, 5]],
 141              [100, [20, 5]],
 142              [200, [20, 10]],
 143          ];
 144      }
 145  
 146      /**
 147       * Test bands.
 148       *
 149       * @dataProvider get_bands_count_and_width_provider
 150       * @param int $grade grade
 151       * @param array $expected
 152       */
 153      public function test_get_bands_count_and_width($grade, $expected) {
 154          $this->resetAfterTest(true);
 155          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 156          $quiz = $quizgenerator->create_instance(['course' => SITEID, 'grade' => $grade]);
 157          $this->assertEquals($expected, quiz_overview_report::get_bands_count_and_width($quiz));
 158      }
 159  
 160  }


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