MDL-8804 two digests per day - merged from MOODLE_18_STABLE
[moodle.git] / mod / quiz / report / analysis / report.php
CommitLineData
0fc59399 1<?php // $Id$
0fc59399 2/// Item analysis displays a table of quiz questions and their performance
3
0fc59399 4 require_once($CFG->libdir.'/tablelib.php');
5
0fc59399 6/// Item analysis displays a table of quiz questions and their performance
7
8class quiz_report extends quiz_default_report {
9
10 function display($quiz, $cm, $course) { /// This function just displays the report
f02c6f01 11 global $CFG, $SESSION, $db, $QTYPES;
9f701b4c 12 $strnoquiz = get_string('noquiz','quiz');
0fc59399 13 $strnoattempts = get_string('noattempts','quiz');
376b8cd2 14 /// Only print headers if not asked to download data
15 $download = optional_param('download', NULL);
16 if (!$download) {
17 $this->print_header_and_tabs($cm, $course, $quiz, $reportmode="analysis");
18 }
19 /// Construct the table for this particular report
0fc59399 20
21 if (!$quiz->questions) {
22 print_heading($strnoattempts);
23 return true;
24 }
39395fd6 25
0fc59399 26 /// Check to see if groups are being used in this quiz
27 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
376b8cd2 28 if (!$download) {
29 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&amp;mode=analysis");
30 } else {
f3f7610c 31 $changegroup = optional_param('group', -1, PARAM_INT);
376b8cd2 32
33 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
34 }
0fc59399 35 } else {
36 $currentgroup = false;
37 }
38
7bbe08a2 39 // set Table and Analysis stats options
0fc59399 40 if(!isset($SESSION->quiz_analysis_table)) {
43f14820 41 $SESSION->quiz_analysis_table = array('attemptselection' => 0, 'lowmarklimit' => 0, 'pagesize' => 10);
0fc59399 42 }
43
44 foreach($SESSION->quiz_analysis_table as $option => $value) {
45 $urlparam = optional_param($option, NULL);
46 if($urlparam === NULL) {
47 $$option = $value;
48 }
49 else {
50 $$option = $SESSION->quiz_analysis_table[$option] = $urlparam;
51 }
52 }
53
376b8cd2 54 $scorelimit = $quiz->sumgrades * $lowmarklimit/ 100;
0fc59399 55
a472ce59 56 // ULPGC ecastro DEBUG this is here to allow for different SQL to select attempts
0fc59399 57 switch ($attemptselection) {
58 case QUIZ_ALLATTEMPTS :
59 $limit = '';
60 $group = '';
61 break;
62 case QUIZ_HIGHESTATTEMPT :
63 $limit = ', max(qa.sumgrades) ';
64 $group = ' GROUP BY qa.userid ';
65 break;
66 case QUIZ_FIRSTATTEMPT :
67 $limit = ', min(qa.timemodified) ';
68 $group = ' GROUP BY qa.userid ';
69 break;
70 case QUIZ_LASTATTEMPT :
71 $limit = ', max(qa.timemodified) ';
72 $group = ' GROUP BY qa.userid ';
73 break;
74 }
75
376b8cd2 76 if ($attemptselection != QUIZ_ALLATTEMPTS) {
77 $sql = 'SELECT qa.userid '.$limit.'FROM '.$CFG->prefix.'user u LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid '. 'WHERE ( qa.quiz = '.$quiz->id.') '.$group;
78 $usermax = get_records_sql_menu($sql);
79 }
80
81 $sql = 'SELECT qa.* FROM '.$CFG->prefix.'user u '.
39a27614 82 'JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid ';
83 if (!empty($currentgroup)) {
f3f7610c 84 $sql .= groups_members_join_sql($currentgroup);
39a27614 85 }
86 $sql .= ' WHERE qa.quiz = '.$quiz->id. // ULPGC ecastro
87 ' AND ( qa.sumgrades >= '.$scorelimit.' ) ';
80a5e194 88 // ^^^^^^ es posible seleccionar aqu� TODOS los quizzes, como quiere Jussi,
89 // pero habr�a que llevar la cuenta ed cada quiz para restaura las preguntas (quizquestions, states)
0fc59399 90 /// Fetch the attempts
376b8cd2 91 $attempts = get_records_sql($sql);
a472ce59 92
0fc59399 93 if(empty($attempts)) {
083fc55c 94 ($strnoattempts);
43f14820 95 $this->print_options_form($quiz, $cm, $attemptselection, $lowmarklimit, $pagesize);
0fc59399 96 return true;
97 }
98
99 /// Here we rewiew all attempts and record data to construct the table
a472ce59 100 $questions = array();
101 $statstable = array();
102 $questionarray = array();
103 foreach ($attempts as $attempt) {
dc2ff7c3 104 $questionarray[] = quiz_questions_in_quiz($attempt->layout);
a472ce59 105 }
106 $questionlist = quiz_questions_in_quiz(implode(",", $questionarray));
107 $questionarray = array_unique(explode(",",$questionlist));
108 $questionlist = implode(",", $questionarray);
109 unset($questionarray);
a472ce59 110
0fc59399 111 foreach ($attempts as $attempt) {
376b8cd2 112 switch ($attemptselection) {
113 case QUIZ_ALLATTEMPTS :
114 $userscore = 0; // can be anything, not used
115 break;
116 case QUIZ_HIGHESTATTEMPT :
117 $userscore = $attempt->sumgrades;
118 break;
119 case QUIZ_FIRSTATTEMPT :
120 $userscore = $attempt->timemodified;
121 break;
122 case QUIZ_LASTATTEMPT :
123 $userscore = $attempt->timemodified;
124 break;
125 }
126
127 if ($attemptselection == QUIZ_ALLATTEMPTS || $userscore == $usermax[$attempt->userid]) {
128
0fc59399 129 $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
4f48fb42 130 " FROM {$CFG->prefix}question q,".
0fc59399 131 " {$CFG->prefix}quiz_question_instances i".
132 " WHERE i.quiz = '$quiz->id' AND q.id = i.question".
133 " AND q.id IN ($questionlist)";
a472ce59 134
0fc59399 135 if (!$quizquestions = get_records_sql($sql)) {
136 error('No questions found');
137 }
138
139 // Load the question type specific information
4f48fb42 140 if (!get_question_options($quizquestions)) {
0fc59399 141 error('Could not load question options');
142 }
0fc59399 143 // Restore the question sessions to their most recent states
144 // creating new sessions where required
4f48fb42 145 if (!$states = get_question_states($quizquestions, $quiz, $attempt)) {
0fc59399 146 error('Could not restore question sessions');
147 }
148 $numbers = explode(',', $questionlist);
43f14820 149 $statsrow = array();
083fc55c 150 foreach ($numbers as $i) {
151 if (!isset($quizquestions[$i]) or !isset($states[$i])) {
152 continue;
153 }
97bf7d62 154 $qtype = ($quizquestions[$i]->qtype=='random') ? $states[$i]->options->question->qtype : $quizquestions[$i]->qtype;
7baff8aa 155 $q = get_question_responses($quizquestions[$i], $states[$i]);
156 if (empty($q)){
0fc59399 157 continue;
a472ce59 158 }
0fc59399 159 $qid = $q->id;
160 if (!isset($questions[$qid])) {
43f14820 161 $questions[$qid]['id'] = $qid;
162 $questions[$qid]['qname'] = $quizquestions[$i]->name;
0fc59399 163 foreach ($q->responses as $answer => $r) {
164 $r->count = 0;
43f14820 165 $questions[$qid]['responses'][$answer] = $r->answer;
166 $questions[$qid]['rcounts'][$answer] = 0;
167 $questions[$qid]['credits'][$answer] = $r->credit;
0fc59399 168 $statsrow[$qid] = 0;
169 }
170 }
4f48fb42 171 $responses = get_question_actual_response($quizquestions[$i], $states[$i]);
0fc59399 172 foreach ($responses as $resp){
a472ce59 173 if ($resp) {
174 if ($key = array_search($resp, $questions[$qid]['responses'])) {
175 $questions[$qid]['rcounts'][$key]++;
0fc59399 176 } else {
f02c6f01 177 $test->responses = $QTYPES[$quizquestions[$i]->qtype]->get_correct_responses($quizquestions[$i], $states[$i]);
178 if ($key = $QTYPES[$quizquestions[$i]->qtype]->check_response($quizquestions[$i], $states[$i], $test)) {
a472ce59 179 $questions[$qid]['rcounts'][$key]++;
180 } else {
181 $questions[$qid]['responses'][] = $resp;
182 $questions[$qid]['rcounts'][] = 1;
183 $questions[$qid]['credits'][] = 0;
184 }
0fc59399 185 }
186 }
187 }
4f48fb42 188 $statsrow[$qid] = get_question_fraction_grade($quizquestions[$i], $states[$i]);
0fc59399 189 }
190 $attemptscores[$attempt->id] = $attempt->sumgrades;
191 $statstable[$attempt->id] = $statsrow;
376b8cd2 192 }
0fc59399 193 } // Statistics Data table built
43f14820 194
195 unset($attempts);
196 unset($quizquestions);
197 unset($states);
198
199 // now calculate statistics and set the values in the $questions array
200 $top = max($attemptscores);
201 $bottom = min($attemptscores);
202 $gap = ($top - $bottom)/3;
203 $top -=$gap;
204 $bottom +=$gap;
205 foreach ($questions as $qid=>$q) {
77c7f0f5 206 $questions[$qid] = $this->report_question_stats($q, $attemptscores, $statstable, $top, $bottom);
43f14820 207 }
208 unset($attemptscores);
209 unset($statstable);
1ddb7f4e 210
43f14820 211 /// Now check if asked download of data
212 if ($download = optional_param('download', NULL)) {
43f14820 213 $filename = clean_filename("$course->shortname ".format_string($quiz->name,true));
214 switch ($download) {
215 case "Excel" :
a472ce59 216 $this->Export_Excel($questions, $filename);
43f14820 217 break;
77c7f0f5 218 case "ODS":
219 $this->Export_ODS($questions, $filename);
43f14820 220 break;
221 case "CSV":
a472ce59 222 $this->Export_CSV($questions, $filename);
43f14820 223 break;
224 }
225 }
1ddb7f4e 226
227 /// Construct the table for this particular report
228
3b62b2ba 229 $tablecolumns = array('id', 'qname', 'responses', 'credits', 'rcounts', 'rpercent', 'facility', 'qsd','disc_index', 'disc_coeff');
43f14820 230 $tableheaders = array(get_string('qidtitle','quiz_analysis'), get_string('qtexttitle','quiz_analysis'),
231 get_string('responsestitle','quiz_analysis'), get_string('rfractiontitle','quiz_analysis'),
232 get_string('rcounttitle','quiz_analysis'), get_string('rpercenttitle','quiz_analysis'),
233 get_string('facilitytitle','quiz_analysis'), get_string('stddevtitle','quiz_analysis'),
234 get_string('dicsindextitle','quiz_analysis'), get_string('disccoefftitle','quiz_analysis'));
235
236 $table = new flexible_table('mod-quiz-report-itemanalysis');
0fc59399 237
238 $table->define_columns($tablecolumns);
239 $table->define_headers($tableheaders);
a567590b 240 $table->define_baseurl($CFG->wwwroot.'/mod/quiz/report.php?q='.$quiz->id.'&amp;mode=analysis');
0fc59399 241
242 $table->sortable(true);
243 $table->collapsible(true);
083fc55c 244 $table->initialbars(false);
0fc59399 245
43f14820 246 $table->column_class('id', 'numcol');
247 $table->column_class('credits', 'numcol');
248 $table->column_class('rcounts', 'numcol');
249 $table->column_class('rpercent', 'numcol');
0fc59399 250 $table->column_class('facility', 'numcol');
3b62b2ba 251 $table->column_class('qsd', 'numcol');
252 $table->column_class('disc_index', 'numcol');
253 $table->column_class('disc_coeff', 'numcol');
43f14820 254
255 $table->column_suppress('id');
256 $table->column_suppress('qname');
257 $table->column_suppress('facility');
3b62b2ba 258 $table->column_suppress('qsd');
259 $table->column_suppress('disc_index');
260 $table->column_suppress('disc_coeff');
43f14820 261
0fc59399 262 $table->set_attribute('cellspacing', '0');
0fc59399 263 $table->set_attribute('id', 'itemanalysis');
264 $table->set_attribute('class', 'generaltable generalbox');
0fc59399 265
266 // Start working -- this is necessary as soon as the niceties are over
267 $table->setup();
43f14820 268
269 $tablesort = $table->get_sql_sort();
270 $sorts = explode(",",trim($tablesort));
271 if ($tablesort and is_array($sorts)) {
a472ce59 272 $sortindex = array();
273 $sortorder = array ();
43f14820 274 foreach ($sorts as $sort) {
275 $data = explode(" ",trim($sort));
276 $sortindex[] = trim($data[0]);
277 $s = trim($data[1]);
278 if ($s=="ASC") {
279 $sortorder[] = SORT_ASC;
280 } else {
281 $sortorder[] = SORT_DESC;
282 }
283 }
284 if (count($sortindex)>0) {
285 $sortindex[] = "id";
286 $sortorder[] = SORT_ASC;
287 foreach($questions as $qid => $row){
288 $index1[$qid] = $row[$sortindex[0]];
289 $index2[$qid] = $row[$sortindex[1]];
290 }
291 array_multisort($index1, $sortorder[0], $index2, $sortorder[1], $questions);
292 }
293 }
294
295 // Now it is time to page the data, simply slice the keys in the array
3db6659d 296 if (!isset($pagesize) || ((int)$pagesize < 1) ){
43f14820 297 $pagesize = 10;
298 }
299 $table->pagesize($pagesize, count($questions));
300 $start = $table->get_page_start();
301 $pagequestions = array_slice(array_keys($questions), $start, $pagesize);
0fc59399 302
43f14820 303 foreach($pagequestions as $qnum) {
304 $q = $questions[$qnum];
305 $qid = $q['id'];
4f48fb42 306 $question = get_record('question', 'id', $qid);
80a5e194 307 $qnumber = " (".link_to_popup_window('/question/question.php?id='.$qid,'editquestion', $qid, 450, 550, get_string('edit'), 'none', true ).") ";
0fc59399 308 $qname = '<div class="qname">'.format_text($question->name." : ", $question->questiontextformat, NULL, $quiz->course).'</div>';
4f48fb42 309 $qicon = print_question_icon($question, false, true);
0fc59399 310 $qreview = quiz_get_question_review($quiz, $question);
43f14820 311 $qtext = format_text($question->questiontext, $question->questiontextformat, NULL, $quiz->course);
312 $qquestion = $qname."\n".$qtext."\n";
0fc59399 313
43f14820 314 $format_options->para = false;
315 $format_options->newlines = false;
316 unset($responses);
317 foreach ($q['responses'] as $aid=>$resp){
318 unset($response);
319 if ($q['credits'][$aid] <= 0) {
0fc59399 320 $qclass = 'uncorrect';
43f14820 321 } elseif ($q['credits'][$aid] == 1) {
0fc59399 322 $qclass = 'correct';
323 } else {
324 $qclass = 'partialcorrect';
325 }
083fc55c 326 $response->credit = '<span class="'.$qclass.'">('.format_float($q['credits'][$aid],2).') </span>';
327 $response->text = '<span class="'.$qclass.'">'.format_text("$resp", FORMAT_MOODLE, $format_options, $quiz->course).' </span>';
43f14820 328 $count = $q['rcounts'][$aid].'/'.$q['count'];
329 $response->rcount = $count; // format_text("$count", FORMAT_MOODLE, $format_options, $quiz->course);
330 $response->rpercent = '('.format_float($q['rcounts'][$aid]/$q['count']*100,0).'%)';
331 $responses[] = $response;
332 }
333
334 $facility = format_float($q['facility']*100,0)." %";
335 $qsd = format_float($q['qsd'],3);
336 $di = format_float($q['disc_index'],2);
337 $dc = format_float($q['disc_coeff'],2);
43f14820 338
083fc55c 339 $response = array_shift($responses);
a567590b 340 $table->add_data(array($qnumber."\n<br />".$qicon."\n ".$qreview, $qquestion, $response->text, $response->credit, $response->rcount, $response->rpercent, $facility, $qsd, $di, $dc));
083fc55c 341 foreach($responses as $response) {
342 $table->add_data(array('', '', $response->text, $response->credit, $response->rcount, $response->rpercent, '', '', '', ''));
0fc59399 343 }
0fc59399 344 }
345
31e95855 346 print_heading_with_help(get_string("analysistitle", "quiz_analysis"),"itemanalysis", "quiz");
0fc59399 347
348 echo '<div id="tablecontainer">';
349 $table->print_html();
a567590b 350 echo '</div>';
0fc59399 351
43f14820 352 $this->print_options_form($quiz, $cm, $attemptselection, $lowmarklimit, $pagesize);
0fc59399 353 return true;
354 }
355
356
43f14820 357 function print_options_form($quiz, $cm, $attempts, $lowlimit=0, $pagesize=10) {
43f14820 358 global $CFG, $USER;
0fc59399 359 echo '<div class="controls">';
b7dc2256 360 echo '<form id="options" action="report.php" method="post">';
09275894 361 echo '<fieldset class="invisiblefieldset">';
083fc55c 362 echo '<p class="quiz-report-options">'.get_string('analysisoptions', 'quiz').': </p>';
0fc59399 363 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
364 echo '<input type="hidden" name="q" value="'.$quiz->id.'" />';
365 echo '<input type="hidden" name="mode" value="analysis" />';
09275894 366 echo '<table id="analysis-options" class="boxaligncenter">';
367 echo '<tr align="left"><td><label for="menuattemptselection">'.get_string('attemptselection', 'quiz_analysis').'</label></td><td>';
43f14820 368 $options = array ( QUIZ_ALLATTEMPTS => get_string("attemptsall", 'quiz_analysis'),
369 QUIZ_HIGHESTATTEMPT => get_string("attemptshighest", 'quiz_analysis'),
370 QUIZ_FIRSTATTEMPT => get_string("attemptsfirst", 'quiz_analysis'),
371 QUIZ_LASTATTEMPT => get_string("attemptslast", 'quiz_analysis'));
0fc59399 372 choose_from_menu($options, "attemptselection", "$attempts", "");
9f701b4c 373 echo '</td></tr>';
374 echo '<tr align="left">';
083fc55c 375 echo '<td><label for="lowmarklimit">'.get_string('lowmarkslimit', 'quiz_analysis').'</label></td>';
376 echo '<td><input type="text" id="lowmarklimit" name="lowmarklimit" size="1" value="'.$lowlimit.'" /> % </td>';
377 echo '</tr>';
9f701b4c 378 echo '<tr align="left">';
083fc55c 379 echo '<td><label for="pagesize">'.get_string('pagesize', 'quiz_analysis').'</label></td>';
380 echo '<td><input type="text" id="pagesize" name="pagesize" size="1" value="'.$pagesize.'" /></td>';
381 echo '</tr>';
382 echo '<tr><td colspan="2" align="center">';
0fc59399 383 echo '<input type="submit" value="'.get_string('go').'" />';
376b8cd2 384 helpbutton("analysisoptions", get_string("analysisoptions",'quiz_analysis'), 'quiz');
083fc55c 385 echo '</td></tr></table>';
09275894 386 echo '</fieldset>';
0fc59399 387 echo '</form>';
388 echo '</div>';
43f14820 389 echo "\n";
390
09275894 391 echo '<table class="boxaligncenter"><tr>';
77c7f0f5 392 $options = array();
43f14820 393 $options["id"] = "$cm->id";
394 $options["q"] = "$quiz->id";
395 $options["mode"] = "analysis";
396 $options['sesskey'] = $USER->sesskey;
397 $options["noheader"] = "yes";
398 echo '<td>';
77c7f0f5 399 $options["download"] = "ODS";
400 print_single_button("report.php", $options, get_string("downloadods"));
401 echo "</td>\n";
402 echo '<td>';
43f14820 403 $options["download"] = "Excel";
404 print_single_button("report.php", $options, get_string("downloadexcel"));
3082b744 405 echo "</td>\n";
a472ce59 406
407 if (file_exists("$CFG->libdir/phpdocwriter/lib/include.php")) {
3082b744 408 echo '<td>';
a472ce59 409 $options["download"] = "OOo";
410 print_single_button("report.php", $options, get_string("downloadooo", "quiz_analysis"));
411 echo "</td>\n";
412 }
43f14820 413 echo '<td>';
414 $options["download"] = "CSV";
415 print_single_button('report.php', $options, get_string("downloadtext"));
416 echo "</td>\n";
417 echo "<td>";
c358d620 418 helpbutton('analysisdownload', get_string('analysisdownload', 'quiz_analysis'), 'quiz');
43f14820 419 echo "</td>\n";
420 echo '</tr></table>';
421}
0fc59399 422
43f14820 423 function report_question_stats(&$q, &$attemptscores, &$questionscores, $top, $bottom) {
a472ce59 424 $qstats = array();
43f14820 425 $qid = $q['id'];
0fc59399 426 $top_scores = $top_count = 0;
427 $bottom_scores = $bottom_count = 0;
428 foreach ($questionscores as $aid => $qrow){
429 if (isset($qrow[$qid])){
430 $qstats[] = array($attemptscores[$aid],$qrow[$qid]);
431 if ($attemptscores[$aid]>=$top){
432 $top_scores +=$qrow[$qid];
433 $top_count++;
434 }
435 if ($attemptscores[$aid]<=$bottom){
436 $bottom_scores +=$qrow[$qid];
437 $bottom_count++;
438 }
439 }
440 }
0fc59399 441 $n = count($qstats);
376b8cd2 442 $sumx = stats_sumx($qstats, array(0,0));
39395fd6 443 $sumg = $sumx[0];
444 $sumq = $sumx[1];
376b8cd2 445 $sumx2 = stats_sumx2($qstats, array(0,0));
39395fd6 446 $sumg2 = $sumx2[0];
447 $sumq2 = $sumx2[1];
376b8cd2 448 $sumxy = stats_sumxy($qstats, array(0,0));
39395fd6 449 $sumgq = $sumxy[0];
0fc59399 450
43f14820 451 $q['count'] = $n;
452 $q['facility'] = $sumq/$n;
0fc59399 453 if ($n<2) {
43f14820 454 $q['qsd'] = sqrt(($sumq2 - $sumq*$sumq/$n)/($n));
455 $gsd = sqrt(($sumg2 - $sumg*$sumg/$n)/($n));
0fc59399 456 } else {
43f14820 457 $q['qsd'] = sqrt(($sumq2 - $sumq*$sumq/$n)/($n-1));
458 $gsd = sqrt(($sumg2 - $sumg*$sumg/$n)/($n-1));
0fc59399 459 }
43f14820 460 $q['disc_index'] = ($top_scores - $bottom_scores)/max($top_count, $bottom_count, 1);
461 $div = $n*$gsd*$q['qsd'];
0fc59399 462 if ($div!=0) {
39395fd6 463 $q['disc_coeff'] = ($sumgq - $sumg*$sumq/$n)/$div;
0fc59399 464 } else {
43f14820 465 $q['disc_coeff'] = -999;
0fc59399 466 }
43f14820 467 return $q;
468 }
469
470 function Export_Excel(&$questions, $filename) {
471 global $CFG;
8867a3bb 472 require_once("$CFG->libdir/excellib.class.php");
473
474 /// Calculate file name
a472ce59 475 $filename .= ".xls";
8867a3bb 476 /// Creating a workbook
477 $workbook = new MoodleExcelWorkbook("-");
478 /// Sending HTTP headers
479 $workbook->send($filename);
480 /// Creating the first worksheet
43f14820 481 $sheettitle = get_string('reportanalysis','quiz_analysis');
d8247a98 482 $myxls =& $workbook->add_worksheet($sheettitle);
8867a3bb 483 /// format types
43f14820 484 $format =& $workbook->add_format();
485 $format->set_bold(0);
d8247a98 486 $formatbc =& $workbook->add_format();
487 $formatbc->set_bold(1);
488 $formatb =& $workbook->add_format();
489 $formatb->set_bold(1);
43f14820 490 $formaty =& $workbook->add_format();
491 $formaty->set_bg_color('yellow');
492 $formatyc =& $workbook->add_format();
493 $formatyc->set_bg_color('yellow'); //bold text on yellow bg
494 $formatyc->set_bold(1);
495 $formatyc->set_align('center');
496 $formatc =& $workbook->add_format();
497 $formatc->set_align('center');
43f14820 498 $formatbc->set_align('center');
499 $formatbpct =& $workbook->add_format();
500 $formatbpct->set_bold(1);
501 $formatbpct->set_num_format('0.0%');
502 $formatbrt =& $workbook->add_format();
503 $formatbrt->set_bold(1);
504 $formatbrt->set_align('right');
505 $formatred =& $workbook->add_format();
506 $formatred->set_bold(1);
507 $formatred->set_color('red');
508 $formatred->set_align('center');
509 $formatblue =& $workbook->add_format();
510 $formatblue->set_bold(1);
511 $formatblue->set_color('blue');
512 $formatblue->set_align('center');
8867a3bb 513 /// Here starts workshhet headers
43f14820 514 $myxls->write_string(0,0,$sheettitle,$formatb);
515
516 $headers = array(get_string('qidtitle','quiz_analysis'), get_string('qtypetitle','quiz_analysis'),
517 get_string('qnametitle','quiz_analysis'), get_string('qtexttitle','quiz_analysis'),
518 get_string('responsestitle','quiz_analysis'), get_string('rfractiontitle','quiz_analysis'),
519 get_string('rcounttitle','quiz_analysis'), get_string('rpercenttitle','quiz_analysis'),
520 get_string('qcounttitle','quiz_analysis'),
521 get_string('facilitytitle','quiz_analysis'), get_string('stddevtitle','quiz_analysis'),
522 get_string('dicsindextitle','quiz_analysis'), get_string('disccoefftitle','quiz_analysis'));
523
524 $col = 0;
525 foreach ($headers as $item) {
526 $myxls->write(2,$col,$item,$formatbc);
527 $col++;
528 }
529
530 $row = 3;
531 foreach($questions as $q) {
77c7f0f5 532 $rows = $this->print_row_stats_data($q);
43f14820 533 foreach($rows as $rowdata){
534 $col = 0;
535 foreach($rowdata as $item){
d8247a98 536 $myxls->write($row,$col,$item,$format);
43f14820 537 $col++;
538 }
539 $row++;
540 }
541 }
8867a3bb 542 /// Close the workbook
43f14820 543 $workbook->close();
8867a3bb 544
d8247a98 545 exit;
0fc59399 546 }
547
43f14820 548
77c7f0f5 549 function Export_ODS(&$questions, $filename) {
43f14820 550 global $CFG;
77c7f0f5 551 require_once("$CFG->libdir/odslib.class.php");
552
553 /// Calculate file name
554 $filename .= ".ods";
555 /// Creating a workbook
556 $workbook = new MoodleODSWorkbook("-");
557 /// Sending HTTP headers
558 $workbook->send($filename);
559 /// Creating the first worksheet
560 $sheettitle = get_string('reportanalysis','quiz_analysis');
561 $myxls =& $workbook->add_worksheet($sheettitle);
562 /// format types
563 $format =& $workbook->add_format();
564 $format->set_bold(0);
565 $formatbc =& $workbook->add_format();
566 $formatbc->set_bold(1);
567 $formatb =& $workbook->add_format();
568 $formatb->set_bold(1);
569 $formaty =& $workbook->add_format();
570 $formaty->set_bg_color('yellow');
571 $formatyc =& $workbook->add_format();
572 $formatyc->set_bg_color('yellow'); //bold text on yellow bg
573 $formatyc->set_bold(1);
574 $formatyc->set_align('center');
575 $formatc =& $workbook->add_format();
576 $formatc->set_align('center');
577 $formatbc->set_align('center');
578 $formatbpct =& $workbook->add_format();
579 $formatbpct->set_bold(1);
580 $formatbpct->set_num_format('0.0%');
581 $formatbrt =& $workbook->add_format();
582 $formatbrt->set_bold(1);
583 $formatbrt->set_align('right');
584 $formatred =& $workbook->add_format();
585 $formatred->set_bold(1);
586 $formatred->set_color('red');
587 $formatred->set_align('center');
588 $formatblue =& $workbook->add_format();
589 $formatblue->set_bold(1);
590 $formatblue->set_color('blue');
591 $formatblue->set_align('center');
592 /// Here starts workshhet headers
593 $myxls->write_string(0,0,$sheettitle,$formatb);
43f14820 594
595 $headers = array(get_string('qidtitle','quiz_analysis'), get_string('qtypetitle','quiz_analysis'),
596 get_string('qnametitle','quiz_analysis'), get_string('qtexttitle','quiz_analysis'),
597 get_string('responsestitle','quiz_analysis'), get_string('rfractiontitle','quiz_analysis'),
598 get_string('rcounttitle','quiz_analysis'), get_string('rpercenttitle','quiz_analysis'),
599 get_string('qcounttitle','quiz_analysis'),
600 get_string('facilitytitle','quiz_analysis'), get_string('stddevtitle','quiz_analysis'),
601 get_string('dicsindextitle','quiz_analysis'), get_string('disccoefftitle','quiz_analysis'));
602
77c7f0f5 603 $col = 0;
604 foreach ($headers as $item) {
605 $myxls->write(2,$col,$item,$formatbc);
606 $col++;
43f14820 607 }
77c7f0f5 608
609 $row = 3;
43f14820 610 foreach($questions as $q) {
77c7f0f5 611 $rows = $this->print_row_stats_data($q);
612 foreach($rows as $rowdata){
613 $col = 0;
614 foreach($rowdata as $item){
615 $myxls->write($row,$col,$item,$format);
616 $col++;
617 }
618 $row++;
43f14820 619 }
620 }
77c7f0f5 621 /// Close the workbook
622 $workbook->close();
623
d8247a98 624 exit;
43f14820 625 }
626
627 function Export_CSV(&$questions, $filename) {
628
43f14820 629 $headers = array(get_string('qidtitle','quiz_analysis'), get_string('qtypetitle','quiz_analysis'),
630 get_string('qnametitle','quiz_analysis'), get_string('qtexttitle','quiz_analysis'),
631 get_string('responsestitle','quiz_analysis'), get_string('rfractiontitle','quiz_analysis'),
632 get_string('rcounttitle','quiz_analysis'), get_string('rpercenttitle','quiz_analysis'),
633 get_string('qcounttitle','quiz_analysis'),
634 get_string('facilitytitle','quiz_analysis'), get_string('stddevtitle','quiz_analysis'),
635 get_string('dicsindextitle','quiz_analysis'), get_string('disccoefftitle','quiz_analysis'));
1ddb7f4e 636
637 $text = implode("\t", $headers)." \n";
a472ce59 638
639 $filename .= ".txt";
1ddb7f4e 640
641 header("Content-Type: application/download\n");
642 header("Content-Disposition: attachment; filename=\"$filename\"");
643 header("Expires: 0");
644 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
645 header("Pragma: public");
646
647 echo $text;
43f14820 648
649 foreach($questions as $q) {
77c7f0f5 650 $rows = $this->print_row_stats_data($q);
43f14820 651 foreach($rows as $row){
652 $text = implode("\t", $row);
1ddb7f4e 653 echo $text." \n";
43f14820 654 }
655 }
1ddb7f4e 656 exit;
43f14820 657 }
658
659 function print_row_stats_data(&$q) {
660 $qid = $q['id'];
4f48fb42 661 $question = get_record('question', 'id', $qid);
43f14820 662
663 $options->para = false;
664 $options->newlines = false;
665
666 $qtype = $question->qtype;
667
668 $qname = format_text($question->name, FORMAT_MOODLE, $options);
669 $qtext = format_text($question->questiontext, FORMAT_MOODLE, $options);
670
671 unset($responses);
672 foreach ($q['responses'] as $aid=>$resp){
673 unset($response);
674 if ($q['credits'][$aid] <= 0) {
675 $qclass = 'uncorrect';
676 } elseif ($q['credits'][$aid] == 1) {
677 $qclass = 'correct';
678 } else {
679 $qclass = 'partialcorrect';
680 }
681 $response->credit = " (".format_float($q['credits'][$aid],2).") ";
682 $response->text = format_text("$resp", FORMAT_MOODLE, $options);
683 $count = $q['rcounts'][$aid].'/'.$q['count'];
684 $response->rcount = $count;
685 $response->rpercent = '('.format_float($q['rcounts'][$aid]/$q['count']*100,0).'%)';
686 $responses[] = $response;
687 }
688 $count = format_float($q['count'],0);
689 $facility = format_float($q['facility']*100,0);
690 $qsd = format_float($q['qsd'],4);
691 $di = format_float($q['disc_index'],3);
692 $dc = format_float($q['disc_coeff'],3);
693
694 unset($result);
695 $response = array_shift($responses);
696 $result[] = array($qid, $qtype, $qname, $qtext, $response->text, $response->credit, $response->rcount, $response->rpercent, $count, $facility, $qsd, $di, $dc);
697 foreach($responses as $response){
698 $result[] = array('', '', '', '', $response->text, $response->credit, $response->rcount, $response->rpercent, '', '', '', '', '');
699 }
700 return $result;
701 }
a472ce59 702}
703
083fc55c 704define('QUIZ_ALLATTEMPTS', 0);
705define('QUIZ_HIGHESTATTEMPT', 1);
706define('QUIZ_FIRSTATTEMPT', 2);
707define('QUIZ_LASTATTEMPT', 3);
708
376b8cd2 709function stats_sumx($data, $initsum){
710 $accum = $initsum;
711 foreach ($data as $v) {
712 $accum[0] += $v[0];
713 $accum[1] += $v[1];
714 }
715 return $accum;
a472ce59 716}
717
376b8cd2 718function stats_sumx2($data, $initsum){
719 $accum = $initsum;
720 foreach ($data as $v) {
721 $accum[0] += $v[0]*$v[0];
722 $accum[1] += $v[1]*$v[1];
723 }
724 return $accum;
a472ce59 725}
726
376b8cd2 727function stats_sumxy($data, $initsum){
728 $accum = $initsum;
729 foreach ($data as $v) {
730 $accum[0] += $v[0]*$v[1];
731 $accum[1] += $v[1]*$v[0];
732 }
733 return $accum;
0fc59399 734}
a472ce59 735
ad7e7ba8 736?>