2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Core Report class of basic reporting plugin
18 * @package scormreport
20 * @author Dan Marsden and Ankit Kumar Agarwal
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 class scorm_basic_report extends scorm_default_report {
28 * displays the full report
29 * @param stdClass $scorm full SCORM object
30 * @param stdClass $cm - full course_module object
31 * @param stdClass $course - full course object
32 * @param string $download - type of download being requested
34 function display($scorm, $cm, $course, $download) {
35 global $CFG, $DB, $OUTPUT, $PAGE;
36 $contextmodule= get_context_instance(CONTEXT_MODULE, $cm->id);
37 $action = optional_param('action', '', PARAM_ALPHA);
38 $attemptids = optional_param_array('attemptid', array(), PARAM_RAW);
40 if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) {
41 if (scorm_delete_responses($attemptids, $scorm)) { //delete responses.
42 add_to_log($course->id, 'scorm', 'delete attempts', 'report.php?id=' . $cm->id, implode(",", $attemptids), $cm->id);
43 echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
46 // find out current groups mode
47 $currentgroup = groups_get_activity_group($cm, true);
50 $mform = new mod_scorm_report_settings($PAGE->url, compact('currentgroup'));
51 if ($fromform = $mform->get_data()) {
52 $detailedrep = $fromform->detailedrep;
53 $pagesize = $fromform->pagesize;
54 $attemptsmode = !empty($fromform->attemptsmode) ? $fromform->attemptsmode : SCORM_REPORT_ATTEMPTS_ALL_STUDENTS;
55 set_user_preference('scorm_report_detailed', $detailedrep);
56 set_user_preference('scorm_report_pagesize', $pagesize);
58 $detailedrep = get_user_preferences('scorm_report_detailed', false);
59 $pagesize = get_user_preferences('scorm_report_pagesize', 0);
60 $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_STUDENTS_WITH, PARAM_INT);
63 $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
67 $displayoptions = array();
68 $displayoptions['attemptsmode'] = $attemptsmode;
69 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
71 groups_print_activity_menu($cm, new moodle_url($PAGE->url, $displayoptions));
75 // We only want to show the checkbox to delete attempts
76 // if the user has permissions and if the report mode is showing attempts.
77 $candelete = has_capability('mod/scorm:deleteresponses', $contextmodule)
78 && ($attemptsmode!= SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO);
79 // select the students
82 if (empty($currentgroup)) {
83 // all users who can attempt scoes
84 if (!$students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', '', '', '', '', '', '', false)) {
85 echo $OUTPUT->notification(get_string('nostudentsyet'));
89 $allowedlist = array_keys($students);
92 // all users who can attempt scoes and who are in the currently selected group
93 if (!$groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', '', '', '', '', $currentgroup, '', false)) {
94 echo $OUTPUT->notification(get_string('nostudentsingroup'));
96 $groupstudents = array();
98 $allowedlist = array_keys($groupstudents);
101 if ( !$nostudents ) {
102 // Now check if asked download of data
103 $coursecontext = context_course::instance($course->id);
105 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
106 $filename = clean_filename("$shortname ".format_string($scorm->name, true));
109 // Define table columns
112 if (!$download && $candelete) {
113 $columns[]= 'checkbox';
116 if (!$download && $CFG->grade_report_showuserimage) {
117 $columns[]= 'picture';
120 $columns[]= 'fullname';
121 $headers[]= get_string('name');
122 $extrafields = get_extra_user_fields($coursecontext);
123 foreach ($extrafields as $field) {
125 $headers[] = get_user_field_name($field);
128 $columns[]= 'attempt';
129 $headers[]= get_string('attempt', 'scorm');
131 $headers[]= get_string('started', 'scorm');
132 $columns[]= 'finish';
133 $headers[]= get_string('last', 'scorm');
135 $headers[]= get_string('score', 'scorm');
136 if ($detailedrep && $scoes = $DB->get_records('scorm_scoes', array("scorm"=>$scorm->id), 'id')) {
137 foreach ($scoes as $sco) {
138 if ($sco->launch!='') {
139 $columns[]= 'scograde'.$sco->id;
140 $headers[]= format_string($sco->title);
141 $table->head[]= format_string($sco->title);
149 $table = new flexible_table('mod-scorm-report');
151 $table->define_columns($columns);
152 $table->define_headers($headers);
153 $table->define_baseurl($PAGE->url);
155 $table->sortable(true);
156 $table->collapsible(true);
158 $table->column_suppress('picture');
159 $table->column_suppress('fullname');
160 // I wonder why it is doing all this suppress malarkey?
161 // However, if it was suppressing idnumber field before, I guess
162 // it needs to suppress all the user identity fields now
163 foreach ($extrafields as $field) {
164 $table->column_suppress($field);
167 $table->no_sorting('start');
168 $table->no_sorting('finish');
169 $table->no_sorting('score');
171 foreach ($scoes as $sco) {
172 if ($sco->launch!='') {
173 $table->no_sorting('scograde'.$sco->id);
178 $table->column_class('picture', 'picture');
179 $table->column_class('fullname', 'bold');
180 $table->column_class('score', 'bold');
182 $table->set_attribute('cellspacing', '0');
183 $table->set_attribute('id', 'attempts');
184 $table->set_attribute('class', 'generaltable generalbox');
186 // Start working -- this is necessary as soon as the niceties are over
188 } else if ($download =='ODS') {
189 require_once("$CFG->libdir/odslib.class.php");
192 // Creating a workbook
193 $workbook = new MoodleODSWorkbook("-");
194 // Sending HTTP headers
195 $workbook->send($filename);
196 // Creating the first worksheet
197 $sheettitle = get_string('report', 'scorm');
198 $myxls =& $workbook->add_worksheet($sheettitle);
200 $format =& $workbook->add_format();
201 $format->set_bold(0);
202 $formatbc =& $workbook->add_format();
203 $formatbc->set_bold(1);
204 $formatbc->set_align('center');
205 $formatb =& $workbook->add_format();
206 $formatb->set_bold(1);
207 $formaty =& $workbook->add_format();
208 $formaty->set_bg_color('yellow');
209 $formatc =& $workbook->add_format();
210 $formatc->set_align('center');
211 $formatr =& $workbook->add_format();
212 $formatr->set_bold(1);
213 $formatr->set_color('red');
214 $formatr->set_align('center');
215 $formatg =& $workbook->add_format();
216 $formatg->set_bold(1);
217 $formatg->set_color('green');
218 $formatg->set_align('center');
219 // Here starts workshhet headers
222 foreach ($headers as $item) {
223 $myxls->write(0, $colnum, $item, $formatbc);
227 } else if ($download =='Excel') {
228 require_once("$CFG->libdir/excellib.class.php");
231 // Creating a workbook
232 $workbook = new MoodleExcelWorkbook("-");
233 // Sending HTTP headers
234 $workbook->send($filename);
235 // Creating the first worksheet
236 $sheettitle = get_string('report', 'scorm');
237 $myxls =& $workbook->add_worksheet($sheettitle);
239 $format =& $workbook->add_format();
240 $format->set_bold(0);
241 $formatbc =& $workbook->add_format();
242 $formatbc->set_bold(1);
243 $formatbc->set_align('center');
244 $formatb =& $workbook->add_format();
245 $formatb->set_bold(1);
246 $formaty =& $workbook->add_format();
247 $formaty->set_bg_color('yellow');
248 $formatc =& $workbook->add_format();
249 $formatc->set_align('center');
250 $formatr =& $workbook->add_format();
251 $formatr->set_bold(1);
252 $formatr->set_color('red');
253 $formatr->set_align('center');
254 $formatg =& $workbook->add_format();
255 $formatg->set_bold(1);
256 $formatg->set_color('green');
257 $formatg->set_align('center');
260 foreach ($headers as $item) {
261 $myxls->write(0, $colnum, $item, $formatbc);
265 } else if ($download=='CSV') {
267 header("Content-Type: application/download\n");
268 header("Content-Disposition: attachment; filename=\"$filename\"");
269 header("Expires: 0");
270 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
271 header("Pragma: public");
272 echo implode("\t", $headers)." \n";
275 list($usql, $params) = $DB->get_in_or_equal($allowedlist, SQL_PARAMS_NAMED);
277 $select = 'SELECT DISTINCT '.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, ';
278 $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' .
279 'u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, u.imagealt, u.email' .
280 get_extra_user_fields_sql($coursecontext, 'u', '', array('idnumber')) . ' ';
282 // This part is the same for all cases - join users and scorm_scoes_track tables
283 $from = 'FROM {user} u ';
284 $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = '.$scorm->id;
285 switch ($attemptsmode) {
286 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
287 // Show only students with attempts
288 $where = ' WHERE u.id ' .$usql. ' AND st.userid IS NOT NULL';
290 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
291 // Show only students without attempts
292 $where = ' WHERE u.id ' .$usql. ' AND st.userid IS NULL';
294 case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
295 // Show all students with or without attempts
296 $where = ' WHERE u.id ' .$usql. ' AND (st.userid IS NOT NULL OR st.userid IS NULL)';
300 $countsql = 'SELECT COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').')) AS nbresults, ';
301 $countsql .= 'COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'st.attempt').')) AS nbattempts, ';
302 $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
303 $countsql .= $from.$where;
306 $sort = $table->get_sql_sort();
310 // Fix some wired sorting
312 $sort = ' ORDER BY uniqueid';
314 $sort = ' ORDER BY '.$sort;
318 // Add extra limits due to initials bar
319 list($twhere, $tparams) = $table->get_sql_where();
321 $where .= ' AND '.$twhere; //initial bar
322 $params = array_merge($params, $tparams);
325 if (!empty($countsql)) {
326 $count = $DB->get_record_sql($countsql, $params);
327 $totalinitials = $count->nbresults;
329 $countsql .= ' AND '.$twhere;
331 $count = $DB->get_record_sql($countsql, $params);
332 $total = $count->nbresults;
335 $table->pagesize($pagesize, $total);
337 echo '<div class="quizattemptcounts">';
338 if ( $count->nbresults == $count->nbattempts ) {
339 echo get_string('reportcountattempts', 'scorm', $count);
340 } else if ( $count->nbattempts>0 ) {
341 echo get_string('reportcountallattempts', 'scorm', $count);
343 echo $count->nbusers.' '.get_string('users');
348 // Fetch the attempts
350 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params,
351 $table->get_page_start(), $table->get_page_size());
352 echo '<div id="scormtablecontainer">';
355 $strreallydel = addslashes_js(get_string('deleteattemptcheck', 'scorm'));
356 echo '<form id="attemptsform" method="post" action="' . $PAGE->url->out(false) .
357 '" onsubmit="return confirm(\''.$strreallydel.'\');">';
358 echo '<input type="hidden" name="action" value="delete"/>';
359 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
360 echo '<div style="display: none;">';
361 echo html_writer::input_hidden_params($PAGE->url);
365 $table->initialbars($totalinitials>20); // Build table rows
367 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params);
371 foreach ($attempts as $scouser) {
373 if (!empty($scouser->attempt)) {
374 $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
378 if (in_array('checkbox', $columns)) {
379 if ($candelete && !empty($timetracks->start)) {
380 $row[] = '<input type="checkbox" name="attemptid[]" value="'. $scouser->userid . ':' . $scouser->attempt . '" />';
381 } else if ($candelete) {
385 if (in_array('picture', $columns)) {
386 $user = (object)array(
387 'id'=>$scouser->userid,
388 'picture'=>$scouser->picture,
389 'imagealt'=>$scouser->imagealt,
390 'email'=>$scouser->email,
391 'firstname'=>$scouser->firstname,
392 'lastname'=>$scouser->lastname);
393 $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
396 $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&course='.$course->id.'">'.fullname($scouser).'</a>';
398 $row[] = fullname($scouser);
400 foreach ($extrafields as $field) {
401 $row[] = s($scouser->{$field});
403 if (empty($timetracks->start)) {
410 $row[] = '<a href="userreport.php?a='.$scorm->id.'&user='.$scouser->userid.'&attempt='.$scouser->attempt.'">'.$scouser->attempt.'</a>';
412 $row[] = $scouser->attempt;
414 if ($download =='ODS' || $download =='Excel' ) {
415 $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
417 $row[] = userdate($timetracks->start);
419 if ($download =='ODS' || $download =='Excel' ) {
420 $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
422 $row[] = userdate($timetracks->finish);
424 $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
426 // print out all scores of attempt
428 foreach ($scoes as $sco) {
429 if ($sco->launch!='') {
430 if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) {
431 if ($trackdata->status == '') {
432 $trackdata->status = 'notattempted';
434 $strstatus = get_string($trackdata->status, 'scorm');
435 // if raw score exists, print it
436 if ($trackdata->score_raw != '') {
437 $score = $trackdata->score_raw;
438 // add max score if it exists
439 if (scorm_version_check($scorm->version, SCORM_13)) {
440 $maxkey = 'cmi.score.max';
442 $maxkey = 'cmi.core.score.max';
444 if (isset($trackdata->$maxkey)) {
445 $score .= '/'.$trackdata->$maxkey;
447 // else print out status
452 $row[] = '<img src="'.$OUTPUT->pix_url($trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>
453 <a href="userreport.php?b='.$sco->id.'&user='.$scouser->userid.'&attempt='.$scouser->attempt.
454 '" title="'.get_string('details', 'scorm').'">'.$score.'</a>';
459 // if we don't have track data, we haven't attempted yet
460 $strstatus = get_string('notattempted', 'scorm');
462 $row[] = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>'.$strstatus;
472 $table->add_data($row);
473 } else if ($download == 'Excel' or $download == 'ODS') {
475 foreach ($row as $item) {
476 $myxls->write($rownum, $colnum, $item, $format);
480 } else if ($download=='CSV') {
481 $text = implode("\t", $row);
486 $table->finish_output();
488 echo '<table id="commands">';
490 echo '<a href="javascript:select_all_in(\'DIV\', null, \'scormtablecontainer\');">'.
491 get_string('selectall', 'scorm').'</a> / ';
492 echo '<a href="javascript:deselect_all_in(\'DIV\', null, \'scormtablecontainer\');">'.
493 get_string('selectnone', 'scorm').'</a> ';
495 echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
496 echo '</td></tr></table>';
502 if (!empty($attempts)) {
503 echo '<table class="boxaligncenter"><tr>';
505 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
506 array('download'=>'ODS') + $displayoptions),
507 get_string('downloadods'));
510 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
511 array('download'=>'Excel') + $displayoptions),
512 get_string('downloadexcel'));
515 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
516 array('download'=>'CSV') + $displayoptions),
517 get_string('downloadtext'));
521 echo '</tr></table>';
525 $mform->set_data(compact('detailedrep', 'pagesize'));
529 if ($candelete && !$download) {
534 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
536 if ($download == 'Excel' or $download == 'ODS') {
539 } else if ($download == 'CSV') {
543 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));