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