3 * This script lists student attempts
6 * @author Martin Dougiamas, Tim Hunt and others.
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once($CFG->libdir
.'/tablelib.php');
12 require_once($CFG->dirroot
.'/mod/quiz/report/overview/overviewsettings_form.php');
14 class quiz_report
extends quiz_default_report
{
19 function display($quiz, $cm, $course) {
21 // Define some strings
22 $strreallydel = addslashes(get_string('deleteattemptcheck','quiz'));
23 $strtimeformat = get_string('strftimedatetime');
24 $strreviewquestion = get_string('reviewresponse', 'quiz');
26 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
28 // Only print headers if not asked to download data
29 if (!$download = optional_param('download', NULL)) {
30 $this->print_header_and_tabs($cm, $course, $quiz, "overview");
33 if($attemptids = optional_param('attemptid', array(), PARAM_INT
)) {
34 //attempts need to be deleted
35 require_capability('mod/quiz:deleteattempts', $context);
36 $attemptids = optional_param('attemptid', array(), PARAM_INT
);
37 foreach($attemptids as $attemptid) {
38 add_to_log($course->id
, 'quiz', 'delete attempt', 'report.php?id=' . $cm->id
,
40 quiz_delete_attempt($attemptid, $quiz);
42 //No need for a redirect, any attemptids that do not exist are ignored.
43 //So no problem if the user refreshes and tries to delete the same attempts
47 // Work out some display options - whether there is feedback, and whether scores should be shown.
48 $hasfeedback = quiz_has_feedback($quiz->id
) && $quiz->grade
> 1.e
-7 && $quiz->sumgrades
> 1.e
-7;
49 $fakeattempt = new stdClass();
50 $fakeattempt->preview
= false;
51 $fakeattempt->timefinish
= $quiz->timeopen
;
52 $reviewoptions = quiz_get_reviewoptions($quiz, $fakeattempt, $context);
53 $showgrades = $quiz->grade
&& $quiz->sumgrades
&& $reviewoptions->scores
;
55 $pageoptions = array();
56 $pageoptions['id'] = $cm->id
;
57 $pageoptions['q'] = $quiz->id
;
58 $pageoptions['mode'] = 'overview';
60 /// find out current groups mode
61 $currentgroup = groups_get_activity_group($cm, true);
63 $reporturl = new moodle_url($CFG->wwwroot
.'/mod/quiz/report.php', $pageoptions);
64 $qmsubselect = quiz_report_qm_filter_select($quiz);
65 $mform = new mod_quiz_report_overview_settings($reporturl, compact('qmsubselect', 'quiz', 'currentgroup'));
66 if ($fromform = $mform->get_data()){
67 $attemptsmode = $fromform->attemptsmode
;
69 //control is not on the form if
70 //the grading method is not set
71 //to grade one attempt per user eg. for average attempt grade.
72 $qmfilter = $fromform->qmfilter
;
76 set_user_preference('quiz_report_overview_detailedmarks', $fromform->detailedmarks
);
77 set_user_preference('quiz_report_pagesize', $fromform->pagesize
);
78 $detailedmarks = $fromform->detailedmarks
;
79 $pagesize = $fromform->pagesize
;
81 $qmfilter = optional_param('qmfilter', 0, PARAM_INT
);
82 $attemptsmode = optional_param('attemptsmode', QUIZ_REPORT_ATTEMPTS_ALL
, PARAM_INT
);
83 $detailedmarks = get_user_preferences('quiz_report_overview_detailedmarks', 1);
84 $pagesize = get_user_preferences('quiz_report_pagesize', 0);
87 if ($attemptsmode == QUIZ_REPORT_ATTEMPTS_ALL
&& $currentgroup){
88 $attemptsmode = QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH
;
91 if (!$reviewoptions->scores
) {
95 $pagesize = QUIZ_REPORT_DEFAULT_PAGE_SIZE
;
97 // We only want to show the checkbox to delete attempts
98 // if the user has permissions and if the report mode is showing attempts.
99 $candelete = has_capability('mod/quiz:deleteattempts', $context)
100 && ($attemptsmode!= QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO
);
103 $displayoptions = array();
104 $displayoptions['attemptsmode'] = $attemptsmode;
105 $displayoptions['qmfilter'] = $qmfilter;
106 $reporturlwithdisplayoptions = new moodle_url($CFG->wwwroot
.'/mod/quiz/report.php', $pageoptions +
$displayoptions);
109 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
111 groups_print_activity_menu($cm, $reporturlwithdisplayoptions->out());
115 // Print information on the number of existing attempts
116 if (!$download) { //do not print notices when downloading
117 if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, true, $currentgroup)) {
118 echo '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
122 if (!$students = get_users_by_capability($context, 'mod/quiz:attempt','','','','','','',false)){
123 notify(get_string('nostudentsyet'));
127 $studentslist = join(',',array_keys($students));
130 if (empty($currentgroup)) {
131 // all users who can attempt quizzes
132 $groupstudentslist = '';
133 $allowedlist = $studentslist;
135 // all users who can attempt quizzes and who are in the currently selected group
136 if (!$groupstudents = get_users_by_capability($context, 'mod/quiz:attempt','','','','',$currentgroup,'',false)){
137 notify(get_string('nostudentsingroup'));
139 $groupstudents = array();
141 $groupstudentslist = join(',', array_keys($groupstudents));
142 $allowedlist = $groupstudentslist;
145 if (!$nostudents ||
($attemptsmode == QUIZ_REPORT_ATTEMPTS_ALL
)){
147 // Print information on the grading method and whether we are displaying
149 if (!$download) { //do not print notices when downloading
150 if ($strattempthighlight = quiz_report_highlighting_grading_method($quiz, $qmsubselect, $qmfilter)) {
151 echo '<div class="quizattemptcounts">' . $strattempthighlight . '</div>';
155 // Now check if asked download of data
157 $filename = clean_filename("$course->shortname ".format_string($quiz->name
,true));
160 // Define table columns
165 if (!$download && $candelete) {
166 $columns[]= 'checkbox';
170 if (!$download && $CFG->grade_report_showuserimage
) {
171 $columns[]= 'picture';
175 $columns[]= 'fullname';
176 $headers[]= get_string('name');
178 if ($CFG->grade_report_showuseridnumber
) {
179 $columns[]= 'idnumber';
180 $headers[]= get_string('idnumber');
183 $columns[]= 'timestart';
184 $headers[]= get_string('startedon', 'quiz');
186 $columns[]= 'timefinish';
187 $headers[]= get_string('timecompleted','quiz');
189 $columns[]= 'duration';
190 $headers[]= get_string('attemptduration', 'quiz');
193 $columns[] = 'sumgrades';
194 $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade
;
197 if ($detailedmarks) {
198 // we want to display marks for all questions
199 $questions = quiz_report_load_questions($quiz);
200 foreach ($questions as $id => $question) {
201 // Ignore questions of zero length
202 $columns[] = 'qsgrade'.$id;
203 $headers[] = '#'.$question->number
;
208 $columns[] = 'feedbacktext';
209 $headers[] = get_string('feedback', 'quiz');
215 $table = new flexible_table('mod-quiz-report-overview-report');
217 $table->define_columns($columns);
218 $table->define_headers($headers);
219 $table->define_baseurl($reporturlwithdisplayoptions->out());
221 $table->sortable(true);
222 $table->collapsible(true);
224 $table->column_suppress('picture');
225 $table->column_suppress('fullname');
226 $table->column_suppress('idnumber');
228 $table->no_sorting('feedbacktext');
230 $table->column_class('picture', 'picture');
231 $table->column_class('fullname', 'bold');
232 $table->column_class('sumgrades', 'bold');
234 $table->set_attribute('cellspacing', '0');
235 $table->set_attribute('id', 'attempts');
236 $table->set_attribute('class', 'generaltable generalbox');
238 // Start working -- this is necessary as soon as the niceties are over
240 } else if ($download =='ODS') {
241 require_once("$CFG->libdir/odslib.class.php");
244 // Creating a workbook
245 $workbook = new MoodleODSWorkbook("-");
246 // Sending HTTP headers
247 $workbook->send($filename);
248 // Creating the first worksheet
249 $sheettitle = get_string('reportoverview','quiz');
250 $myxls =& $workbook->add_worksheet($sheettitle);
252 $format =& $workbook->add_format();
253 $format->set_bold(0);
254 $formatbc =& $workbook->add_format();
255 $formatbc->set_bold(1);
256 $formatbc->set_align('center');
257 $formatb =& $workbook->add_format();
258 $formatb->set_bold(1);
259 $formaty =& $workbook->add_format();
260 $formaty->set_bg_color('yellow');
261 $formatc =& $workbook->add_format();
262 $formatc->set_align('center');
263 $formatr =& $workbook->add_format();
264 $formatr->set_bold(1);
265 $formatr->set_color('red');
266 $formatr->set_align('center');
267 $formatg =& $workbook->add_format();
268 $formatg->set_bold(1);
269 $formatg->set_color('green');
270 $formatg->set_align('center');
271 // Here starts workshhet headers
274 foreach ($headers as $item) {
275 $myxls->write(0,$colnum,$item,$formatbc);
279 } else if ($download =='Excel') {
280 require_once("$CFG->libdir/excellib.class.php");
283 // Creating a workbook
284 $workbook = new MoodleExcelWorkbook("-");
285 // Sending HTTP headers
286 $workbook->send($filename);
287 // Creating the first worksheet
288 $sheettitle = get_string('reportoverview','quiz');
289 $myxls =& $workbook->add_worksheet($sheettitle);
291 $format =& $workbook->add_format();
292 $format->set_bold(0);
293 $formatbc =& $workbook->add_format();
294 $formatbc->set_bold(1);
295 $formatbc->set_align('center');
296 $formatb =& $workbook->add_format();
297 $formatb->set_bold(1);
298 $formaty =& $workbook->add_format();
299 $formaty->set_bg_color('yellow');
300 $formatc =& $workbook->add_format();
301 $formatc->set_align('center');
302 $formatr =& $workbook->add_format();
303 $formatr->set_bold(1);
304 $formatr->set_color('red');
305 $formatr->set_align('center');
306 $formatg =& $workbook->add_format();
307 $formatg->set_bold(1);
308 $formatg->set_color('green');
309 $formatg->set_align('center');
312 foreach ($headers as $item) {
313 $myxls->write(0,$colnum,$item,$formatbc);
317 } else if ($download=='CSV') {
320 header("Content-Type: application/download\n");
321 header("Content-Disposition: attachment; filename=\"$filename\"");
322 header("Expires: 0");
323 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
324 header("Pragma: public");
326 echo implode("\t", $headers)." \n";
331 $select = 'SELECT '.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS uniqueid, ';
335 " WHEN $qmsubselect THEN 1" .
337 "END) AS gradedattempt, ";
340 $select .='qa.uniqueid AS attemptuniqueid, qa.id AS attempt, u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, '.
341 'qa.sumgrades, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
343 // This part is the same for all cases - join users and quiz_attempts tables
344 $from = 'FROM '.$CFG->prefix
.'user u ';
345 $from .= 'LEFT JOIN '.$CFG->prefix
.'quiz_attempts qa ON qa.userid = u.id AND qa.quiz = '.$quiz->id
;
346 if ($qmsubselect && $qmfilter){
347 $from .= ' AND '.$qmsubselect;
349 switch ($attemptsmode){
350 case QUIZ_REPORT_ATTEMPTS_ALL
:
351 // Show all attempts, including students who are no longer in the course
352 $where = ' WHERE qa.id IS NOT NULL AND qa.preview = 0';
354 case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH
:
355 // Show only students with attempts
356 $where = ' WHERE u.id IN (' .$allowedlist. ') AND qa.preview = 0 AND qa.id IS NOT NULL';
358 case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO
:
359 // Show only students without attempts
360 $where = ' WHERE u.id IN (' .$allowedlist. ') AND qa.id IS NULL';
362 case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS
:
363 // Show all students with or without attempts
364 $where = ' WHERE u.id IN (' .$allowedlist. ') AND (qa.preview = 0 OR qa.preview IS NULL)';
368 $countsql = 'SELECT COUNT(DISTINCT('.sql_concat('u.id', '\'#\'', 'COALESCE(qa.attempt, 0)').')) '.$from.$where;
371 // Add table joins so we can sort by question grade
372 // unfortunately can't join all tables necessary to fetch all grades
373 // to get the state for one question per attempt row we must join two tables
374 // and there is a limit to how many joins you can have in one query. In MySQL it
375 // is 61. This means that when having more than 29 questions the query will fail.
376 // So we join just the tables needed to sort the attempts.
377 if(!$download && $sort = $table->get_sql_sort()) {
378 if (!$download && $detailedmarks) {
380 $sortparts = explode(',', $sort);
382 foreach($sortparts as $sortpart) {
383 $sortpart = trim($sortpart);
384 if (preg_match('/^qsgrade([0-9]+)/', $sortpart, $matches)){
385 $qid = intval($matches[1]);
386 $select .= ", qs$qid.grade AS qsgrade$qid, qs$qid.event AS qsevent$qid, qs$qid.id AS qsid$qid";
387 $from .= "LEFT JOIN {$CFG->prefix}question_sessions qns$qid ON qns$qid.attemptid = qa.uniqueid AND qns$qid.questionid = $qid ";
388 $from .= "LEFT JOIN {$CFG->prefix}question_states qs$qid ON qs$qid.id = qns$qid.newgraded ";
390 $newsort[] = $sortpart;
401 // Fix some wired sorting
403 $sort = ' ORDER BY uniqueid';
405 $sort = ' ORDER BY '.$sort;
409 // Add extra limits due to initials bar
410 if($table->get_sql_where()) {
411 $where .= ' AND '.$table->get_sql_where();
414 if (!empty($countsql)) {
415 $totalinitials = count_records_sql($countsql);
416 if ($table->get_sql_where()) {
417 $countsql .= ' AND '.$table->get_sql_where();
419 $total = count_records_sql($countsql);
423 $table->pagesize($pagesize, $total);
426 // Fetch the attempts
428 $attempts = get_records_sql($select.$from.$where.$sort,
429 $table->get_page_start(), $table->get_page_size());
431 $attempts = get_records_sql($select.$from.$where.$sort);
436 $table->initialbars($totalinitials>20);
440 //get all the attempt ids we want to display on this page
441 //or to export for download.
442 $attemptids = array();
443 foreach ($attempts as $attempt){
444 if ($attempt->attemptuniqueid
> 0){
445 $attemptids[] = $attempt->attemptuniqueid
;
448 $gradedstatesbyattempt = quiz_get_newgraded_states($attemptids, true, 'qs.id, qs.grade, qs.event, qs.question, qs.attempt');
450 foreach ($attempts as $attempt) {
454 if (in_array('checkbox', $columns)){
455 if ($attempt->attempt
){
456 $row[] = '<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt
.'" />';
461 if (in_array('picture', $columns)){
462 $picture = print_user_picture($attempt->userid
, $course->id
, $attempt->picture
, false, true);
466 $userlink = '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$attempt->userid
.
467 '&course='.$course->id
.'">'.fullname($attempt).'</a>';
470 $row[] = fullname($attempt);
473 if (in_array('idnumber', $columns)){
474 $row[] = $attempt->idnumber
;
478 if ($attempt->attempt
) {
479 $startdate = userdate($attempt->timestart
, $strtimeformat);
481 $row[] = '<a href="review.php?q='.$quiz->id
.'&attempt='.$attempt->attempt
.'">'.$startdate.'</a>';
485 if ($attempt->timefinish
) {
486 $timefinish = userdate($attempt->timefinish
, $strtimeformat);
487 $duration = format_time($attempt->duration
);
489 $row[] = '<a href="review.php?q='.$quiz->id
.'&attempt='.$attempt->attempt
.'">'.$timefinish.'</a>';
491 $row[] = $timefinish;
496 $row[] = get_string('unfinished', 'quiz');
506 if ($attempt->timefinish
) {
507 $grade = quiz_rescale_grade($attempt->sumgrades
, $quiz);
509 $gradehtml = '<a href="review.php?q='.$quiz->id
.'&attempt='.$attempt->attempt
.'">'.$grade.'</a>';
510 if ($qmsubselect && $attempt->gradedattempt
){
511 $gradehtml = '<div class="highlight">'.$gradehtml.'</div>';
524 if(empty($attempt->attempt
)) {
525 foreach($questions as $question) {
529 foreach($questions as $questionid => $question) {
530 $stateforqinattempt = $gradedstatesbyattempt[$attempt->attemptuniqueid
][$questionid];
531 if (question_state_is_graded($stateforqinattempt)) {
532 $grade = quiz_rescale_grade($stateforqinattempt->grade
, $quiz);
537 $grade = $grade.'/'.quiz_rescale_grade($question->grade
, $quiz);
538 $row[] = link_to_popup_window('/mod/quiz/reviewquestion.php?state='.
539 $stateforqinattempt->id
.'&number='.$question->number
,
540 'reviewquestion', $grade, 450, 650, $strreviewquestion, 'none', true);
550 if ($attempt->timefinish
) {
551 $row[] = quiz_report_feedback_for_grade(quiz_rescale_grade($attempt->sumgrades
, $quiz), $quiz->id
);
557 $table->add_data($row);
558 } else if ($download == 'Excel' or $download == 'ODS') {
560 foreach($row as $item){
561 $myxls->write($rownum,$colnum,$item,$format);
565 } else if ($download=='CSV') {
566 $text = implode("\t", $row);
570 //end of adding data from attempts data to table / download
572 if (!$download && $attempts){
574 $averagesql = "SELECT AVG(qg.grade) AS grade " .
575 "FROM {$CFG->prefix}quiz_grades qg " .
576 "WHERE quiz=".$quiz->id
;
578 $table->add_separator();
579 if ($groupstudentslist){
580 $groupaveragesql = $averagesql." AND qg.userid IN ($groupstudentslist)";
581 $groupaverage = get_record_sql($groupaveragesql);
582 $groupaveragerow = array('fullname' => get_string('groupavg', 'grades'),
583 'sumgrades' => round($groupaverage->grade
, $quiz->decimalpoints
),
584 'feedbacktext'=> quiz_report_feedback_for_grade($groupaverage->grade
, $quiz->id
));
585 if($detailedmarks && $qmsubselect) {
586 $avggradebyq = quiz_get_average_grade_for_questions($quiz, $groupstudentslist);
587 $groupaveragerow +
= quiz_format_average_grade_for_questions($avggradebyq, $questions, $quiz, $download);
589 $table->add_data_keyed($groupaveragerow);
591 $overallaverage = get_record_sql($averagesql." AND qg.userid IN ($studentslist)");
592 $overallaveragerow = array('fullname' => get_string('overallaverage', 'grades'),
593 'sumgrades' => round($overallaverage->grade
, $quiz->decimalpoints
),
594 'feedbacktext'=> quiz_report_feedback_for_grade($overallaverage->grade
, $quiz->id
));
595 if($detailedmarks && $qmsubselect) {
596 $avggradebyq = quiz_get_average_grade_for_questions($quiz, $studentslist);
597 $overallaveragerow +
= quiz_format_average_grade_for_questions($avggradebyq, $questions, $quiz, $download);
599 $table->add_data_keyed($overallaveragerow);
603 echo '<div id="tablecontainer">';
604 echo '<form id="attemptsform" method="post" action="' . $reporturlwithdisplayoptions->out(true) .
605 '" onsubmit="confirm(\''.$strreallydel.'\');">';
606 echo '<div style="display: none;">';
607 echo $reporturlwithdisplayoptions->hidden_params_out();
612 $table->print_html();
614 // Print "Select all" etc.
615 if (!empty($attempts) && $candelete) {
616 echo '<table id="commands">';
618 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.
619 get_string('selectall', 'quiz').'</a> / ';
620 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.
621 get_string('selectnone', 'quiz').'</a> ';
623 echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
624 echo '</td></tr></table>';
628 echo '</form></div>';
630 if (!empty($attempts)) {
631 echo '<table class="boxaligncenter"><tr>';
633 print_single_button($reporturl->out(true), $pageoptions +
$displayoptions +
array('download' => 'ODS'),
634 get_string('downloadods'));
637 print_single_button($reporturl->out(true), $pageoptions +
$displayoptions +
array('download' => 'Excel'),
638 get_string('downloadexcel'));
641 print_single_button($reporturl->out(true), $pageoptions +
$displayoptions +
array('download' => 'CSV'),
642 get_string('downloadtext'));
645 helpbutton('overviewdownload', get_string('overviewdownload', 'quiz_overview'), 'quiz');
647 echo '</tr></table>';
652 $table->print_html();
655 if ($download == 'Excel' or $download == 'ODS') {
658 } else if ($download == 'CSV') {
663 // Print display options
664 $mform->set_data($displayoptions +
compact('detailedmarks', 'pagesize'));
666 //should be quicker than a COUNT to test if there is at least one record :
667 if ($showgrades && get_records('quiz_grades', 'quiz', $quiz->id
, '', '*', 0, 1)){
668 $imageurl = $CFG->wwwroot
.'/mod/quiz/report/overview/overviewgraph.php?id='.$quiz->id
;
669 print_heading(get_string('overviewreportgraph', 'quiz_overview'));
670 echo '<div class="mdl-align"><img src="'.$imageurl.'" alt="'.get_string('overviewreportgraph', 'quiz_overview').'" /></div>';