|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->libdir . '/questionlib.php'); 00030 require_once($CFG->dirroot . '/mod/quiz/report/statistics/qstats.php'); 00031 require_once($CFG->dirroot . '/mod/quiz/locallib.php'); 00032 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php'); 00033 00034 00041 class testable_quiz_statistics_question_stats extends quiz_statistics_question_stats { 00042 public function set_step_data($states) { 00043 $this->lateststeps = $states; 00044 } 00045 00046 protected function get_random_guess_score($questiondata) { 00047 return 0; 00048 } 00049 } 00050 00051 00058 class quiz_statistics_question_stats_test extends UnitTestCase { 00059 public static $includecoverage = array('mod/quiz/report/reportlib.php'); 00060 00062 protected $qstats; 00063 00064 public function test_qstats() { 00065 global $CFG; 00066 // Data is taken from randomly generated attempts data generated by 00067 // contrib/tools/generators/qagenerator/ 00068 $steps = $this->get_records_from_csv($CFG->dirroot . 00069 '/mod/quiz/report/statistics/simpletest/mdl_question_states.csv'); 00070 // Data is taken from questions mostly generated by 00071 // contrib/tools/generators/generator.php 00072 $questions = $this->get_records_from_csv($CFG->dirroot . 00073 '/mod/quiz/report/statistics/simpletest/mdl_question.csv'); 00074 $this->qstats = new testable_quiz_statistics_question_stats($questions, 22, 10045.45455); 00075 $this->qstats->set_step_data($steps); 00076 $this->qstats->compute_statistics(); 00077 00078 // Values expected are taken from contrib/tools/quiz_tools/stats.xls 00079 $facility = array(0, 0, 0, 0, null, null, null, 41.19318182, 81.36363636, 00080 71.36363636, 65.45454545, 65.90909091, 36.36363636, 59.09090909, 50, 00081 59.09090909, 63.63636364, 45.45454545, 27.27272727, 50); 00082 $this->qstats_q_fields('facility', $facility, 100); 00083 $sd = array(0, 0, 0, 0, null, null, null, 1912.733589, 251.2738111, 00084 322.6312277, 333.4199022, 337.5811591, 492.3659639, 503.2362797, 00085 511.7663157, 503.2362797, 492.3659639, 509.6471914, 455.8423058, 511.7663157); 00086 $this->qstats_q_fields('sd', $sd, 1000); 00087 $effectiveweight = array(0, 0, 0, 0, 0, 0, 0, 26.58464457, 3.368456046, 00088 3.253955259, 7.584083694, 3.79658376, 3.183278505, 4.532356904, 00089 7.78856243, 10.08351572, 8.381139345, 8.727645713, 7.946277111, 4.769500946); 00090 $this->qstats_q_fields('effectiveweight', $effectiveweight); 00091 $discriminationindex = array(null, null, null, null, null, null, null, 00092 25.88327077, 1.170256965, -4.207816809, 28.16930644, -2.513606859, 00093 -12.99017581, -8.900638238, 8.670004606, 29.63337745, 15.18945843, 00094 16.21079629, 15.52451404, -8.396734802); 00095 $this->qstats_q_fields('discriminationindex', $discriminationindex); 00096 $discriminativeefficiency = array(null, null, null, null, null, null, null, 00097 27.23492723, 1.382386552, -4.691171307, 31.12404354, -2.877487579, 00098 -17.5074184, -10.27568922, 10.86956522, 34.58997279, 17.4790556, 00099 20.14359793, 22.06477733, -10); 00100 $this->qstats_q_fields('discriminativeefficiency', $discriminativeefficiency); 00101 } 00102 00103 public function qstats_q_fields($fieldname, $values, $multiplier=1) { 00104 foreach ($this->qstats->questions as $question) { 00105 $value = array_shift($values); 00106 if ($value !== null) { 00107 $this->assertWithinMargin($question->_stats->{$fieldname} * $multiplier, 00108 $value, 1E-6); 00109 } else { 00110 $this->assertEqual($question->_stats->{$fieldname} * $multiplier, $value); 00111 } 00112 } 00113 } 00114 00115 public function get_fields_from_csv($line) { 00116 $line = trim($line); 00117 $items = preg_split('!,!', $line); 00118 while (list($key) = each($items)) { 00119 if ($items[$key]!='') { 00120 if ($start = ($items[$key]{0}=='"')) { 00121 $items[$key] = substr($items[$key], 1); 00122 while (!$end = ($items[$key]{strlen($items[$key])-1}=='"')) { 00123 $item = $items[$key]; 00124 unset($items[$key]); 00125 list($key) = each($items); 00126 $items[$key] = $item . ',' . $items[$key]; 00127 } 00128 $items[$key] = substr($items[$key], 0, strlen($items[$key])-1); 00129 } 00130 00131 } 00132 } 00133 return $items; 00134 } 00135 00136 public function get_records_from_csv($filename) { 00137 $filecontents = file($filename, FILE_IGNORE_NEW_LINES); 00138 $records = array(); 00139 $keys = $this->get_fields_from_csv(array_shift($filecontents));//first line is field names 00140 while (null !== ($line = array_shift($filecontents))) { 00141 $data = $this->get_fields_from_csv($line); 00142 $arraykey = reset($data); 00143 $object = new stdClass(); 00144 foreach ($keys as $key) { 00145 $value = array_shift($data); 00146 if ($value !== null) { 00147 $object->{$key} = $value; 00148 } else { 00149 $object->{$key} = ''; 00150 } 00151 } 00152 $records[$arraykey] = $object; 00153 } 00154 return $records; 00155 } 00156 }