MDL-46015 lesson: Optimise report - use recordsets
[moodle.git] / mod / lesson / report.php
CommitLineData
86342d63 1<?php
0a4abb73
SH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
8d1b4ee7 18/**
19 * Displays the lesson statistics.
20 *
9b24f68b 21 * @package mod_lesson
cc3dbaaa
PS
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
8d1b4ee7 24 **/
37eeba77 25
0a4abb73
SH
26require_once('../../config.php');
27require_once($CFG->dirroot.'/mod/lesson/locallib.php');
28
29$id = required_param('id', PARAM_INT); // Course Module ID
1b586516 30$pageid = optional_param('pageid', null, PARAM_INT); // Lesson Page ID
0a4abb73
SH
31$action = optional_param('action', 'reportoverview', PARAM_ALPHA); // action to take
32$nothingtodisplay = false;
33
d85c8399 34$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
74df2951 35$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
b3daa926
PS
36$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
37
0a4abb73 38require_login($course, false, $cm);
37eeba77 39
5918e371 40$context = context_module::instance($cm->id);
0a4abb73 41require_capability('mod/lesson:manage', $context);
37eeba77 42
e07c51c4 43$ufields = user_picture::fields('u'); // These fields are enough
0a4abb73 44$params = array("lessonid" => $lesson->id);
9695ff81
TH
45list($sort, $sortparams) = users_order_by_sql('u');
46$params = array_merge($params, $sortparams);
e07c51c4 47// TODO: Improve this. Fetching all students always is crazy!
98da6021 48if (!empty($cm->groupingid)) {
334c6d80 49 $params["groupingid"] = $cm->groupingid;
e07c51c4 50 $sql = "SELECT DISTINCT $ufields
86342d63 51 FROM {lesson_attempts} a
646fc290 52 INNER JOIN {user} u ON u.id = a.userid
53 INNER JOIN {groups_members} gm ON gm.userid = u.id
334c6d80
RL
54 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
55 WHERE a.lessonid = :lessonid AND
56 gg.groupingid = :groupingid
9695ff81 57 ORDER BY $sort";
0a4abb73 58} else {
e07c51c4 59 $sql = "SELECT DISTINCT $ufields
0a4abb73
SH
60 FROM {user} u,
61 {lesson_attempts} a
62 WHERE a.lessonid = :lessonid and
63 u.id = a.userid
9695ff81 64 ORDER BY $sort";
0a4abb73 65}
c6949936 66
53339437
EV
67$students = $DB->get_recordset_sql($sql, $params);
68if (!$students->valid()) {
0a4abb73
SH
69 $nothingtodisplay = true;
70}
c6949936 71
a6855934 72$url = new moodle_url('/mod/lesson/report.php', array('id'=>$id));
0a4abb73
SH
73if ($action !== 'reportoverview') {
74 $url->param('action', $action);
75}
1b586516 76if ($pageid !== null) {
0a4abb73
SH
77 $url->param('pageid', $pageid);
78}
79$PAGE->set_url($url);
c165ce9f
RW
80if ($action == 'reportoverview') {
81 $PAGE->navbar->add(get_string('reports', 'lesson'));
82 $PAGE->navbar->add(get_string('overview', 'lesson'));
83}
84
649cf95d 85$lessonoutput = $PAGE->get_renderer('mod_lesson');
37eeba77 86
53339437
EV
87$attempts = $DB->get_recordset('lesson_attempts', array('lessonid' => $lesson->id), 'timeseen');
88if (!$attempts->valid()) {
0a4abb73
SH
89 $nothingtodisplay = true;
90}
ec81373f 91
0a4abb73
SH
92if (! $grades = $DB->get_records('lesson_grades', array('lessonid' => $lesson->id), 'completed')) {
93 $grades = array();
94}
86342d63 95
0a4abb73
SH
96if (! $times = $DB->get_records('lesson_timer', array('lessonid' => $lesson->id), 'starttime')) {
97 $times = array();
98}
37eeba77 99
0a4abb73 100if ($nothingtodisplay) {
d42bc7dc 101 echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('nolessonattempts', 'lesson'));
0a4abb73
SH
102 echo $OUTPUT->notification(get_string('nolessonattempts', 'lesson'));
103 echo $OUTPUT->footer();
104 exit();
105}
ec81373f 106
19735816
PS
107if ($action === 'delete') {
108 /// Process any form data before fetching attempts, grades and times
109 if (has_capability('mod/lesson:edit', $context) and $form = data_submitted() and confirm_sesskey()) {
110 /// Cycle through array of userids with nested arrays of tries
111 if (!empty($form->attempts)) {
112 foreach ($form->attempts as $userid => $tries) {
113 // Modifier IS VERY IMPORTANT! What does it do?
114 // Well, it is for when you delete multiple attempts for the same user.
115 // If you delete try 1 and 3 for a user, then after deleting try 1, try 3 then
116 // becomes try 2 (because try 1 is gone and all tries after try 1 get decremented).
117 // So, the modifier makes sure that the submitted try refers to the current try in the
118 // database - hope this all makes sense :)
119 $modifier = 0;
120
121 foreach ($tries as $try => $junk) {
122 $try -= $modifier;
123
124 /// Clean up the timer table by removing using the order - this is silly, it should be linked to specific attempt (skodak)
125 $params = array ("userid" => $userid, "lessonid" => $lesson->id);
126 $timers = $DB->get_records_sql("SELECT id FROM {lesson_timer}
127 WHERE userid = :userid AND lessonid = :lessonid
128 ORDER BY starttime", $params, $try, 1);
129 if ($timers) {
130 $timer = reset($timers);
131 $DB->delete_records('lesson_timer', array('id' => $timer->id));
132 }
133
134 /// Remove the grade from the grades and high_scores tables - this is silly, it should be linked to specific attempt (skodak)
135 $grades = $DB->get_records_sql("SELECT id FROM {lesson_grades}
136 WHERE userid = :userid AND lessonid = :lessonid
137 ORDER BY completed", $params, $try, 1);
138
139 if ($grades) {
140 $grade = reset($grades);
141 $DB->delete_records('lesson_grades', array('id' => $grade->id));
142 $DB->delete_records('lesson_high_scores', array('gradeid' => $grade->id, 'lessonid' => $lesson->id, 'userid' => $userid));
143 }
144
145 /// Remove attempts and update the retry number
146 $DB->delete_records('lesson_attempts', array('userid' => $userid, 'lessonid' => $lesson->id, 'retry' => $try));
147 $DB->execute("UPDATE {lesson_attempts} SET retry = retry - 1 WHERE userid = ? AND lessonid = ? AND retry > ?", array($userid, $lesson->id, $try));
148
149 /// Remove seen branches and update the retry number
150 $DB->delete_records('lesson_branch', array('userid' => $userid, 'lessonid' => $lesson->id, 'retry' => $try));
151 $DB->execute("UPDATE {lesson_branch} SET retry = retry - 1 WHERE userid = ? AND lessonid = ? AND retry > ?", array($userid, $lesson->id, $try));
152
153 /// update central gradebook
154 lesson_update_grades($lesson, $userid);
155
156 $modifier++;
157 }
158 }
159 }
160 }
161 redirect(new moodle_url($PAGE->url, array('action'=>'reportoverview')));
162
163} else if ($action === 'reportoverview') {
164 /**************************************************************************
165 this action is for default view and overview view
166 **************************************************************************/
d42bc7dc 167 echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('overview', 'lesson'));
19735816 168
5918e371 169 $course_context = context_course::instance($course->id);
19735816
PS
170 if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
171 $seeallgradeslink = new moodle_url('/grade/report/grader/index.php', array('id'=>$course->id));
172 $seeallgradeslink = html_writer::link($seeallgradeslink, get_string('seeallcoursegrades', 'grades'));
173 echo $OUTPUT->box($seeallgradeslink, 'allcoursegrades');
174 }
175
0a4abb73
SH
176 $studentdata = array();
177
178 // build an array for output
179 foreach ($attempts as $attempt) {
180 // if the user is not in the array or if the retry number is not in the sub array, add the data for that try.
181 if (!array_key_exists($attempt->userid, $studentdata) || !array_key_exists($attempt->retry, $studentdata[$attempt->userid])) {
182 // restore/setup defaults
183 $n = 0;
184 $timestart = 0;
185 $timeend = 0;
1b586516 186 $usergrade = null;
0a4abb73
SH
187
188 // search for the grade record for this try. if not there, the nulls defined above will be used.
189 foreach($grades as $grade) {
190 // check to see if the grade matches the correct user
191 if ($grade->userid == $attempt->userid) {
192 // see if n is = to the retry
193 if ($n == $attempt->retry) {
194 // get grade info
195 $usergrade = round($grade->grade, 2); // round it here so we only have to do it once
196 break;
ec81373f 197 }
0a4abb73 198 $n++; // if not equal, then increment n
ec81373f 199 }
0a4abb73
SH
200 }
201 $n = 0;
202 // search for the time record for this try. if not there, the nulls defined above will be used.
203 foreach($times as $time) {
204 // check to see if the grade matches the correct user
205 if ($time->userid == $attempt->userid) {
206 // see if n is = to the retry
207 if ($n == $attempt->retry) {
208 // get grade info
209 $timeend = $time->lessontime;
210 $timestart = $time->starttime;
211 break;
5f3b3a08 212 }
0a4abb73 213 $n++; // if not equal, then increment n
5f3b3a08 214 }
ec81373f 215 }
0a4abb73
SH
216
217 // build up the array.
218 // this array represents each student and all of their tries at the lesson
219 $studentdata[$attempt->userid][$attempt->retry] = array( "timestart" => $timestart,
220 "timeend" => $timeend,
221 "grade" => $usergrade,
222 "try" => $attempt->retry,
223 "userid" => $attempt->userid);
ec81373f 224 }
0a4abb73 225 }
53339437 226 $attempts->close();
0a4abb73
SH
227 // set all the stats variables
228 $numofattempts = 0;
229 $avescore = 0;
230 $avetime = 0;
1b586516
RT
231 $highscore = null;
232 $lowscore = null;
233 $hightime = null;
234 $lowtime = null;
0a4abb73
SH
235
236 $table = new html_table();
237
238 // set up the table object
239 $table->head = array(get_string('name'), get_string('attempts', 'lesson'), get_string('highscore', 'lesson'));
16be8974
DM
240 $table->align = array('center', 'left', 'left');
241 $table->wrap = array('nowrap', 'nowrap', 'nowrap');
242 $table->attributes['class'] = 'standardtable generaltable';
243 $table->size = array(null, '70%', null);
0a4abb73
SH
244
245 // print out the $studentdata array
246 // going through each student that has attempted the lesson, so, each student should have something to be displayed
247 foreach ($students as $student) {
248 // check to see if the student has attempts to print out
249 if (array_key_exists($student->id, $studentdata)) {
250 // set/reset some variables
251 $attempts = array();
252 // gather the data for each user attempt
253 $bestgrade = 0;
254 $bestgradefound = false;
255 // $tries holds all the tries/retries a student has done
256 $tries = $studentdata[$student->id];
257 $studentname = "{$student->lastname},&nbsp;$student->firstname";
258 foreach ($tries as $try) {
259 // start to build up the checkbox and link
260 if (has_capability('mod/lesson:edit', $context)) {
261 $temp = '<input type="checkbox" id="attempts" name="attempts['.$try['userid'].']['.$try['try'].']" /> ';
262 } else {
263 $temp = '';
264 }
86342d63 265
0a4abb73 266 $temp .= "<a href=\"report.php?id=$cm->id&amp;action=reportdetail&amp;userid=".$try['userid'].'&amp;try='.$try['try'].'">';
1b586516 267 if ($try["grade"] !== null) { // if null then not done yet
0a4abb73
SH
268 // this is what the link does when the user has completed the try
269 $timetotake = $try["timeend"] - $try["timestart"];
270
271 $temp .= $try["grade"]."%";
272 $bestgradefound = true;
273 if ($try["grade"] > $bestgrade) {
274 $bestgrade = $try["grade"];
ec81373f 275 }
0a4abb73
SH
276 $temp .= "&nbsp;".userdate($try["timestart"]);
277 $temp .= ",&nbsp;(".format_time($timetotake).")</a>";
278 } else {
279 // this is what the link does/looks like when the user has not completed the try
280 $temp .= get_string("notcompleted", "lesson");
281 $temp .= "&nbsp;".userdate($try["timestart"])."</a>";
1b586516 282 $timetotake = null;
0a4abb73
SH
283 }
284 // build up the attempts array
285 $attempts[] = $temp;
286
287 // run these lines for the stats only if the user finnished the lesson
1b586516 288 if ($try["grade"] !== null) {
0a4abb73
SH
289 $numofattempts++;
290 $avescore += $try["grade"];
291 $avetime += $timetotake;
1b586516 292 if ($try["grade"] > $highscore || $highscore === null) {
0a4abb73
SH
293 $highscore = $try["grade"];
294 }
1b586516 295 if ($try["grade"] < $lowscore || $lowscore === null) {
0a4abb73
SH
296 $lowscore = $try["grade"];
297 }
1b586516 298 if ($timetotake > $hightime || $hightime == null) {
0a4abb73
SH
299 $hightime = $timetotake;
300 }
1b586516 301 if ($timetotake < $lowtime || $lowtime == null) {
0a4abb73 302 $lowtime = $timetotake;
ec81373f 303 }
304 }
ec81373f 305 }
0a4abb73
SH
306 // get line breaks in after each attempt
307 $attempts = implode("<br />\n", $attempts);
308 // add it to the table data[] object
309 $table->data[] = array($studentname, $attempts, $bestgrade."%");
ec81373f 310 }
0a4abb73 311 }
53339437 312 $students->close();
0a4abb73
SH
313 // print it all out !
314 if (has_capability('mod/lesson:edit', $context)) {
315 echo "<form id=\"theform\" method=\"post\" action=\"report.php\">\n
316 <input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n
317 <input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
318 }
16be8974 319 echo html_writer::table($table);
0a4abb73
SH
320 if (has_capability('mod/lesson:edit', $context)) {
321 $checklinks = '<a href="javascript: checkall();">'.get_string('selectall').'</a> / ';
edc28287 322 $checklinks .= '<a href="javascript: checknone();">'.get_string('deselectall').'</a>';
68ca508f 323 $checklinks .= html_writer::label('action', 'menuaction', false, array('class' => 'accesshide'));
7266bd3e
ARN
324 $checklinks .= html_writer::select(array('delete' => get_string('deleteselected')), 'action', 0, array(''=>'choosedots'), array('id'=>'actionid', 'class' => 'autosubmit'));
325 $PAGE->requires->yui_module('moodle-core-formautosubmit',
326 'M.core.init_formautosubmit',
327 array(array('selectid' => 'actionid', 'nothing' => false))
328 );
edc28287 329 echo $OUTPUT->box($checklinks, 'center');
0a4abb73
SH
330 echo '</form>';
331 }
86342d63 332
0a4abb73
SH
333 // some stat calculations
334 if ($numofattempts == 0) {
335 $avescore = get_string("notcompleted", "lesson");
336 } else {
337 $avescore = format_float($avescore/$numofattempts, 2);
338 }
1b586516 339 if ($avetime == null) {
0a4abb73
SH
340 $avetime = get_string("notcompleted", "lesson");
341 } else {
342 $avetime = format_float($avetime/$numofattempts, 0);
343 $avetime = format_time($avetime);
344 }
1b586516 345 if ($hightime == null) {
0a4abb73
SH
346 $hightime = get_string("notcompleted", "lesson");
347 } else {
348 $hightime = format_time($hightime);
349 }
1b586516 350 if ($lowtime == null) {
0a4abb73
SH
351 $lowtime = get_string("notcompleted", "lesson");
352 } else {
353 $lowtime = format_time($lowtime);
354 }
1b586516 355 if ($highscore === null) {
0a4abb73
SH
356 $highscore = get_string("notcompleted", "lesson");
357 }
1b586516 358 if ($lowscore === null) {
0a4abb73
SH
359 $lowscore = get_string("notcompleted", "lesson");
360 }
ec81373f 361
0a4abb73 362 // output the stats
974beea1 363 echo $OUTPUT->heading(get_string('lessonstats', 'lesson'), 3);
0a4abb73
SH
364 $stattable = new html_table();
365 $stattable->head = array(get_string('averagescore', 'lesson'), get_string('averagetime', 'lesson'),
366 get_string('highscore', 'lesson'), get_string('lowscore', 'lesson'),
367 get_string('hightime', 'lesson'), get_string('lowtime', 'lesson'));
16be8974
DM
368 $stattable->align = array('center', 'center', 'center', 'center', 'center', 'center');
369 $stattable->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
370 $stattable->attributes['class'] = 'standardtable generaltable';
8b8d7fcd
RW
371
372 if (is_numeric($highscore)) {
373 $highscore .= '%';
374 }
375 if (is_numeric($lowscore)) {
376 $lowscore .= '%';
377 }
378 $stattable->data[] = array($avescore.'%', $avetime, $highscore, $lowscore, $hightime, $lowtime);
0a4abb73 379
16be8974 380 echo html_writer::table($stattable);
19735816 381} else if ($action === 'reportdetail') {
ec81373f 382 /**************************************************************************
383 this action is for a student detailed view and for the general detailed view
384
0a4abb73
SH
385 General flow of this section of the code
386 1. Generate a object which holds values for the statistics for each question/answer
387 2. Cycle through all the pages to create a object. Foreach page, see if the student actually answered
388 the page. Then process the page appropriatly. Display all info about the question,
389 Highlight correct answers, show how the user answered the question, and display statistics
390 about each page
391 3. Print out info about the try (if needed)
392 4. Print out the object which contains all the try info
393
394**************************************************************************/
d43d3418 395 echo $lessonoutput->header($lesson, $cm, $action, false, null, get_string('detailedstats', 'lesson'));
19735816 396
5918e371 397 $course_context = context_course::instance($course->id);
19735816
PS
398 if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
399 $seeallgradeslink = new moodle_url('/grade/report/grader/index.php', array('id'=>$course->id));
400 $seeallgradeslink = html_writer::link($seeallgradeslink, get_string('seeallcoursegrades', 'grades'));
401 echo $OUTPUT->box($seeallgradeslink, 'allcoursegrades');
402 }
403
0a4abb73
SH
404 $formattextdefoptions = new stdClass;
405 $formattextdefoptions->para = false; //I'll use it widely in this page
367a75fa 406 $formattextdefoptions->overflowdiv = true;
0a4abb73 407
1b586516
RT
408 $userid = optional_param('userid', null, PARAM_INT); // if empty, then will display the general detailed view
409 $try = optional_param('try', null, PARAM_INT);
0a4abb73
SH
410
411 $lessonpages = $lesson->load_all_pages();
412 foreach ($lessonpages as $lessonpage) {
413 if ($lessonpage->prevpageid == 0) {
414 $pageid = $lessonpage->id;
ec81373f 415 }
0a4abb73 416 }
f6e200cb 417
0a4abb73
SH
418 // now gather the stats into an object
419 $firstpageid = $pageid;
420 $pagestats = array();
421 while ($pageid != 0) { // EOL
422 $page = $lessonpages[$pageid];
423 $params = array ("lessonid" => $lesson->id, "pageid" => $page->id);
424 if ($allanswers = $DB->get_records_select("lesson_attempts", "lessonid = :lessonid AND pageid = :pageid", $params, "timeseen")) {
425 // get them ready for processing
426 $orderedanswers = array();
427 foreach ($allanswers as $singleanswer) {
428 // ordering them like this, will help to find the single attempt record that we want to keep.
429 $orderedanswers[$singleanswer->userid][$singleanswer->retry][] = $singleanswer;
430 }
431 // this is foreach user and for each try for that user, keep one attempt record
432 foreach ($orderedanswers as $orderedanswer) {
433 foreach($orderedanswer as $tries) {
434 $page->stats($pagestats, $tries);
ec81373f 435 }
ec81373f 436 }
0a4abb73
SH
437 } else {
438 // no one answered yet...
ec81373f 439 }
0a4abb73
SH
440 //unset($orderedanswers); initialized above now
441 $pageid = $page->nextpageid;
442 }
ec81373f 443
0a4abb73
SH
444 $manager = lesson_page_type_manager::get($lesson);
445 $qtypes = $manager->get_page_type_strings();
446
447 $answerpages = array();
448 $answerpage = "";
449 $pageid = $firstpageid;
450 // cycle through all the pages
451 // foreach page, add to the $answerpages[] array all the data that is needed
452 // from the question, the users attempt, and the statistics
453 // grayout pages that the user did not answer and Branch, end of branch, cluster
454 // and end of cluster pages
455 while ($pageid != 0) { // EOL
456 $page = $lessonpages[$pageid];
457 $answerpage = new stdClass;
458 $data ='';
d42bc7dc 459
0a4abb73 460 $answerdata = new stdClass;
2a03a824 461 // Set some defaults for the answer data.
1b586516
RT
462 $answerdata->score = null;
463 $answerdata->response = null;
2a03a824 464 $answerdata->responseformat = FORMAT_PLAIN;
0a4abb73
SH
465
466 $answerpage->title = format_string($page->title);
467
468 $options = new stdClass;
469 $options->noclean = true;
367a75fa 470 $options->overflowdiv = true;
01c37ef1 471 $answerpage->contents = format_text($page->contents, $page->contentsformat, $options);
0a4abb73
SH
472
473 $answerpage->qtype = $qtypes[$page->qtype].$page->option_description_string();
474 $answerpage->grayout = $page->grayout;
d85c8399 475 $answerpage->context = $context;
0a4abb73
SH
476
477 if (empty($userid)) {
478 // there is no userid, so set these vars and display stats.
479 $answerpage->grayout = 0;
1b586516 480 $useranswer = null;
0a4abb73
SH
481 } elseif ($useranswers = $DB->get_records("lesson_attempts",array("lessonid"=>$lesson->id, "userid"=>$userid, "retry"=>$try,"pageid"=>$page->id), "timeseen")) {
482 // get the user's answer for this page
483 // need to find the right one
484 $i = 0;
485 foreach ($useranswers as $userattempt) {
486 $useranswer = $userattempt;
487 $i++;
488 if ($lesson->maxattempts == $i) {
489 break; // reached maxattempts, break out
490 }
ec81373f 491 }
0a4abb73
SH
492 } else {
493 // user did not answer this page, gray it out and set some nulls
494 $answerpage->grayout = 1;
1b586516 495 $useranswer = null;
0a4abb73
SH
496 }
497 $i = 0;
498 $n = 0;
499 $answerpages[] = $page->report_answers(clone($answerpage), clone($answerdata), $useranswer, $pagestats, $i, $n);
500 $pageid = $page->nextpageid;
501 }
ec81373f 502
0a4abb73
SH
503 /// actually start printing something
504 $table = new html_table();
505 $table->wrap = array();
506 $table->width = "60%";
507 if (!empty($userid)) {
508 // if looking at a students try, print out some basic stats at the top
509
510 // print out users name
511 //$headingobject->lastname = $students[$userid]->lastname;
512 //$headingobject->firstname = $students[$userid]->firstname;
513 //$headingobject->attempt = $try + 1;
514 //print_heading(get_string("studentattemptlesson", "lesson", $headingobject));
974beea1 515 echo $OUTPUT->heading(get_string('attempt', 'lesson', $try+1), 3);
0a4abb73
SH
516
517 $table->head = array();
16be8974
DM
518 $table->align = array('right', 'left');
519 $table->attributes['class'] = 'compacttable generaltable';
0a4abb73
SH
520
521 $params = array("lessonid"=>$lesson->id, "userid"=>$userid);
522 if (!$grades = $DB->get_records_select("lesson_grades", "lessonid = :lessonid and userid = :userid", $params, "completed", "*", $try, 1)) {
523 $grade = -1;
524 $completed = -1;
525 } else {
526 $grade = current($grades);
527 $completed = $grade->completed;
528 $grade = round($grade->grade, 2);
529 }
530 if (!$times = $DB->get_records_select("lesson_timer", "lessonid = :lessonid and userid = :userid", $params, "starttime", "*", $try, 1)) {
531 $timetotake = -1;
532 } else {
533 $timetotake = current($times);
534 $timetotake = $timetotake->lessontime - $timetotake->starttime;
ec81373f 535 }
536
0a4abb73
SH
537 if ($timetotake == -1 || $completed == -1 || $grade == -1) {
538 $table->align = array("center");
ec81373f 539
0a4abb73
SH
540 $table->data[] = array(get_string("notcompleted", "lesson"));
541 } else {
53339437 542 $user = $DB->get_record('user', array('id' => $userid));
ec81373f 543
0a4abb73 544 $gradeinfo = lesson_grade($lesson, $try, $user->id);
86342d63 545
812dbaf7 546 $table->data[] = array(get_string('name').':', $OUTPUT->user_picture($user, array('courseid'=>$course->id)).fullname($user, true));
0a4abb73
SH
547 $table->data[] = array(get_string("timetaken", "lesson").":", format_time($timetotake));
548 $table->data[] = array(get_string("completed", "lesson").":", userdate($completed));
549 $table->data[] = array(get_string('rawgrade', 'lesson').':', $gradeinfo->earned.'/'.$gradeinfo->total);
550 $table->data[] = array(get_string("grade", "lesson").":", $grade."%");
551 }
16be8974 552 echo html_writer::table($table);
86342d63 553
0a4abb73 554 // Don't want this class for later tables
16be8974 555 $table->attributes['class'] = '';
0a4abb73 556 }
86342d63 557
ec81373f 558
16be8974
DM
559 $table->align = array('left', 'left');
560 $table->size = array('70%', null);
561 $table->attributes['class'] = 'compacttable generaltable';
ec81373f 562
0a4abb73
SH
563 foreach ($answerpages as $page) {
564 unset($table->data);
565 if ($page->grayout) { // set the color of text
566 $fontstart = "<span class=\"dimmed\">";
567 $fontend = "</font>";
568 $fontstart2 = $fontstart;
569 $fontend2 = $fontend;
570 } else {
571 $fontstart = "";
572 $fontend = "";
573 $fontstart2 = "";
574 $fontend2 = "";
575 }
ec81373f 576
0a4abb73
SH
577 $table->head = array($fontstart2.$page->qtype.": ".format_string($page->title).$fontend2, $fontstart2.get_string("classstats", "lesson").$fontend2);
578 $table->data[] = array($fontstart.get_string("question", "lesson").": <br />".$fontend.$fontstart2.$page->contents.$fontend2, " ");
579 $table->data[] = array($fontstart.get_string("answer", "lesson").":".$fontend, ' ');
580 // apply the font to each answer
5951d333 581 if (!empty($page->answerdata)) {
ec81373f 582 foreach ($page->answerdata->answers as $answer){
583 $modified = array();
584 foreach ($answer as $single) {
585 // need to apply a font to each one
586 $modified[] = $fontstart2.$single.$fontend2;
587 }
588 $table->data[] = $modified;
589 }
5951d333 590 if (isset($page->answerdata->response)) {
0abc18cf
JMV
591 $table->data[] = array($fontstart.get_string("response", "lesson").": <br />".$fontend
592 .$fontstart2.$page->answerdata->response.$fontend2, " ");
ec81373f 593 }
594 $table->data[] = array($page->answerdata->score, " ");
0a4abb73 595 } else {
5951d333 596 $table->data[] = array(get_string('didnotanswerquestion', 'lesson'), " ");
ec81373f 597 }
3266d14b 598 echo html_writer::start_tag('div', array('class' => 'no-overflow'));
16be8974 599 echo html_writer::table($table);
3266d14b 600 echo html_writer::end_tag('div');
ec81373f 601 }
0a4abb73
SH
602} else {
603 print_error('unknowaction');
604}
37eeba77 605
606/// Finish the page
0a4abb73 607echo $OUTPUT->footer();