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
19 * @subpackage interactions
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 require_once($CFG->dirroot.'/mod/scorm/report/interactions/responsessettings_form.php');
28 class scorm_interactions_report extends scorm_default_report {
30 * displays the full report
31 * @param stdClass $scorm full SCORM object
32 * @param stdClass $cm - full course_module object
33 * @param stdClass $course - full course object
34 * @param string $download - type of download being requested
36 function display($scorm, $cm, $course, $download) {
37 global $CFG, $DB, $OUTPUT, $PAGE;
38 $contextmodule = get_context_instance(CONTEXT_MODULE, $cm->id);
39 $action = optional_param('action', '', PARAM_ALPHA);
40 $attemptids = optional_param('attemptid', array(), PARAM_RAW);
42 if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) {
43 if (scorm_delete_responses($attemptids, $scorm)) { //delete responses.
44 add_to_log($course->id, 'scorm', 'delete attempts', 'report.php?id=' . $cm->id, implode(",", $attemptids), $cm->id);
45 echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
50 $mform = new mod_scorm_report_interactions_settings($PAGE->url, compact('currentgroup'));
51 if ($fromform = $mform->get_data()) {
52 $pagesize = $fromform->pagesize;
53 $includeqtext = $fromform->qtext;
54 $includeresp = $fromform->resp;
55 $includeright = $fromform->right;
56 $attemptsmode = !empty($fromform->attemptsmode) ? $fromform->attemptsmode : SCORM_REPORT_ATTEMPTS_ALL_STUDENTS;
57 set_user_preference('scorm_report_pagesize', $pagesize);
58 set_user_preference('scorm_report_interactions_qtext', $includeqtext);
59 set_user_preference('scorm_report_interactions_resp', $includeresp);
60 set_user_preference('scorm_report_interactions_right', $includeright);
62 $pagesize = get_user_preferences('scorm_report_pagesize', 0);
63 $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_STUDENTS_WITH, PARAM_INT);
64 $includeqtext = get_user_preferences('scorm_report_interactions_qtext', 0);
65 $includeresp = get_user_preferences('scorm_report_interactions_resp', 1);
66 $includeright = get_user_preferences('scorm_report_interactions_right', 0);
69 $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
73 $displayoptions = array();
74 $displayoptions['attemptsmode'] = $attemptsmode;
75 $displayoptions['qtext'] = $includeqtext;
76 $displayoptions['resp'] = $includeresp;
77 $displayoptions['right'] = $includeright;
79 $mform->set_data($displayoptions + array('pagesize' => $pagesize));
80 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
82 groups_print_activity_menu($cm, new moodle_url($PAGE->url, $displayoptions));
85 $formattextoptions = array('context' => get_context_instance(CONTEXT_COURSE, $course->id));
87 // We only want to show the checkbox to delete attempts
88 // if the user has permissions and if the report mode is showing attempts.
89 $candelete = has_capability('mod/scorm:deleteresponses', $contextmodule)
90 && ($attemptsmode != SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO);
91 // select the students
94 if (empty($currentgroup)) {
95 // all users who can attempt scoes
96 if (!$students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', '', '', '', '', '', '', false)) {
97 echo $OUTPUT->notification(get_string('nostudentsyet'));
101 $allowedlist = array_keys($students);
104 // all users who can attempt scoes and who are in the currently selected group
105 if (!$groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', '', '', '', '', $currentgroup, '', false)) {
106 echo $OUTPUT->notification(get_string('nostudentsingroup'));
108 $groupstudents = array();
110 $allowedlist = ($groupstudents);
112 if ( !$nostudents ) {
113 // Now check if asked download of data
115 $filename = clean_filename("$course->shortname ".format_string($scorm->name, true,$formattextoptions));
118 // Define table columns
121 if (!$download && $candelete) {
122 $columns[] = 'checkbox';
125 if (!$download && $CFG->grade_report_showuserimage) {
126 $columns[] = 'picture';
129 $columns[] = 'fullname';
130 $headers[] = get_string('name');
131 if ($CFG->grade_report_showuseridnumber) {
132 $columns[] = 'idnumber';
133 $headers[] = get_string('idnumber');
135 $columns[] = 'attempt';
136 $headers[] = get_string('attempt', 'scorm');
137 $columns[] = 'start';
138 $headers[] = get_string('started', 'scorm');
139 $columns[] = 'finish';
140 $headers[] = get_string('last', 'scorm');
141 $columns[] = 'score';
142 $headers[] = get_string('score', 'scorm');
143 $scoes = $DB->get_records('scorm_scoes', array("scorm"=>$scorm->id), 'id');
144 foreach ($scoes as $sco) {
145 if ($sco->launch != '') {
146 $columns[] = 'scograde'.$sco->id;
147 $headers[] = format_string($sco->title,'',$formattextoptions);
148 $table->head[]= format_string($sco->title,'',$formattextoptions);
153 list($usql, $params) = $DB->get_in_or_equal($allowedlist);
155 $select = 'SELECT DISTINCT '.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, ';
156 $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' .
157 'u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, u.imagealt, u.email ';
159 // This part is the same for all cases - join users and scorm_scoes_track tables
160 $from = 'FROM {user} u ';
161 $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = '.$scorm->id;
162 switch ($attemptsmode) {
163 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
164 // Show only students with attempts
165 $where = ' WHERE u.id ' .$usql. ' AND st.userid IS NOT NULL';
167 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
168 // Show only students without attempts
169 $where = ' WHERE u.id ' .$usql. ' AND st.userid IS NULL';
171 case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
172 // Show all students with or without attempts
173 $where = ' WHERE u.id ' .$usql. ' AND (st.userid IS NOT NULL OR st.userid IS NULL)';
177 $countsql = 'SELECT COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').')) AS nbresults, ';
178 $countsql .= 'COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'st.attempt').')) AS nbattempts, ';
179 $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
180 $countsql .= $from.$where;
181 $attempts = $DB->get_records_sql($select.$from.$where, $params);
182 $questioncount = get_scorm_question_count($scorm->id);
183 for($id = 0; $id < $questioncount; $id++) {
184 if ($displayoptions['qtext']) {
185 $columns[] = 'question' . $id;
186 $headers[] = get_string('questionx', 'scormreport_interactions', $id);
188 if ($displayoptions['resp']) {
189 $columns[] = 'response' . $id;
190 $headers[] = get_string('responsex', 'scormreport_interactions', $id);
192 if ($displayoptions['right']) {
193 $columns[] = 'right' . $id;
194 $headers[] = get_string('rightanswerx', 'scormreport_interactions', $id);
199 $table = new flexible_table('mod-scorm-report');
201 $table->define_columns($columns);
202 $table->define_headers($headers);
203 $table->define_baseurl($PAGE->url);
205 $table->sortable(true);
206 $table->collapsible(true);
208 $table->column_suppress('picture');
209 $table->column_suppress('fullname');
210 $table->column_suppress('idnumber');
212 $table->no_sorting('start');
213 $table->no_sorting('finish');
214 $table->no_sorting('score');
216 foreach ($scoes as $sco) {
217 if ($sco->launch != '') {
218 $table->no_sorting('scograde'.$sco->id);
222 $table->column_class('picture', 'picture');
223 $table->column_class('fullname', 'bold');
224 $table->column_class('score', 'bold');
226 $table->set_attribute('cellspacing', '0');
227 $table->set_attribute('id', 'attempts');
228 $table->set_attribute('class', 'generaltable generalbox');
230 // Start working -- this is necessary as soon as the niceties are over
232 } else if ($download == 'ODS') {
233 require_once("$CFG->libdir/odslib.class.php");
236 // Creating a workbook
237 $workbook = new MoodleODSWorkbook("-");
238 // Sending HTTP headers
239 $workbook->send($filename);
240 // Creating the first worksheet
241 $sheettitle = get_string('report', 'scorm');
242 $myxls =& $workbook->add_worksheet($sheettitle);
244 $format =& $workbook->add_format();
245 $format->set_bold(0);
246 $formatbc =& $workbook->add_format();
247 $formatbc->set_bold(1);
248 $formatbc->set_align('center');
249 $formatb =& $workbook->add_format();
250 $formatb->set_bold(1);
251 $formaty =& $workbook->add_format();
252 $formaty->set_bg_color('yellow');
253 $formatc =& $workbook->add_format();
254 $formatc->set_align('center');
255 $formatr =& $workbook->add_format();
256 $formatr->set_bold(1);
257 $formatr->set_color('red');
258 $formatr->set_align('center');
259 $formatg =& $workbook->add_format();
260 $formatg->set_bold(1);
261 $formatg->set_color('green');
262 $formatg->set_align('center');
263 // Here starts workshhet headers
266 foreach ($headers as $item) {
267 $myxls->write(0, $colnum, $item, $formatbc);
271 } else if ($download =='Excel') {
272 require_once("$CFG->libdir/excellib.class.php");
275 // Creating a workbook
276 $workbook = new MoodleExcelWorkbook("-");
277 // Sending HTTP headers
278 $workbook->send($filename);
279 // Creating the first worksheet
280 $sheettitle = get_string('report', 'scorm');
281 $myxls =& $workbook->add_worksheet($sheettitle);
283 $format =& $workbook->add_format();
284 $format->set_bold(0);
285 $formatbc =& $workbook->add_format();
286 $formatbc->set_bold(1);
287 $formatbc->set_align('center');
288 $formatb =& $workbook->add_format();
289 $formatb->set_bold(1);
290 $formaty =& $workbook->add_format();
291 $formaty->set_bg_color('yellow');
292 $formatc =& $workbook->add_format();
293 $formatc->set_align('center');
294 $formatr =& $workbook->add_format();
295 $formatr->set_bold(1);
296 $formatr->set_color('red');
297 $formatr->set_align('center');
298 $formatg =& $workbook->add_format();
299 $formatg->set_bold(1);
300 $formatg->set_color('green');
301 $formatg->set_align('center');
304 foreach ($headers as $item) {
305 $myxls->write(0, $colnum, $item, $formatbc);
309 } else if ($download == 'CSV') {
311 header("Content-Type: application/download\n");
312 header("Content-Disposition: attachment; filename=\"$filename\"");
313 header("Expires: 0");
314 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
315 header("Pragma: public");
316 echo implode("\t", $headers)." \n";
320 $sort = $table->get_sql_sort();
324 // Fix some wired sorting
326 $sort = ' ORDER BY uniqueid';
328 $sort = ' ORDER BY '.$sort;
332 // Add extra limits due to initials bar
333 list($twhere, $tparams) = $table->get_sql_where();
335 $where .= ' AND '.$twhere; //initial bar
336 $params = array_merge($params, $tparams);
339 if (!empty($countsql)) {
340 $count = $DB->get_record_sql($countsql,$params);
341 $totalinitials = $count->nbresults;
343 $countsql .= ' AND '.$twhere;
345 $count = $DB->get_record_sql($countsql, $params);
346 $total = $count->nbresults;
349 $table->pagesize($pagesize, $total);
351 echo '<div class="quizattemptcounts">';
352 if ( $count->nbresults == $count->nbattempts ) {
353 echo get_string('reportcountattempts', 'scorm', $count);
354 } else if ( $count->nbattempts>0 ) {
355 echo get_string('reportcountallattempts', 'scorm', $count);
357 echo $count->nbusers.' '.get_string('users');
362 // Fetch the attempts
364 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params,
365 $table->get_page_start(), $table->get_page_size());
366 echo '<div id="scormtablecontainer">';
369 $strreallydel = addslashes_js(get_string('deleteattemptcheck', 'scorm'));
370 echo '<form id="attemptsform" method="post" action="' . $PAGE->url->out(false) .
371 '" onsubmit="return confirm(\''.$strreallydel.'\');">';
372 echo '<input type="hidden" name="action" value="delete"/>';
373 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
374 echo '<div style="display: none;">';
375 echo html_writer::input_hidden_params($PAGE->url);
379 $table->initialbars($totalinitials>20); // Build table rows
381 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params);
384 foreach ($attempts as $scouser) {
386 if (!empty($scouser->attempt)) {
387 $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
391 if (in_array('checkbox', $columns)) {
392 if ($candelete && !empty($timetracks->start)) {
393 $row[] = '<input type="checkbox" name="attemptid[]" value="'. $scouser->userid . ':' . $scouser->attempt . '" />';
394 } else if ($candelete) {
398 if (in_array('picture', $columns)) {
399 $user = (object)array(
400 'id'=>$scouser->userid,
401 'picture'=>$scouser->picture,
402 'imagealt'=>$scouser->imagealt,
403 'email'=>$scouser->email,
404 'firstname'=>$scouser->firstname,
405 'lastname'=>$scouser->lastname);
406 $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
409 $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&course='.$course->id.'">'.fullname($scouser).'</a>';
411 $row[] = fullname($scouser);
413 if (in_array('idnumber', $columns)) {
414 $row[] = $scouser->idnumber;
416 if (empty($timetracks->start)) {
423 $row[] = '<a href="userreport.php?a='.$scorm->id.'&user='.$scouser->userid.'&attempt='.$scouser->attempt.'">'.$scouser->attempt.'</a>';
425 $row[] = $scouser->attempt;
427 if ($download =='ODS' || $download =='Excel' ) {
428 $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
430 $row[] = userdate($timetracks->start);
432 if ($download =='ODS' || $download =='Excel' ) {
433 $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
435 $row[] = userdate($timetracks->finish);
437 $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
439 // print out all scores of attempt
440 foreach ($scoes as $sco) {
441 if ($sco->launch != '') {
442 if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) {
443 if ($trackdata->status == '') {
444 $trackdata->status = 'notattempted';
446 $strstatus = get_string($trackdata->status, 'scorm');
447 // if raw score exists, print it
448 if ($trackdata->score_raw != '') {
449 $score = $trackdata->score_raw;
450 // add max score if it exists
451 if ($scorm->version == 'SCORM_1.3') {
452 $maxkey = 'cmi.score.max';
454 $maxkey = 'cmi.core.score.max';
456 if (isset($trackdata->$maxkey)) {
457 $score .= '/'.$trackdata->$maxkey;
459 // else print out status
464 $row[] = '<img src="'.$OUTPUT->pix_url($trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>
465 <a href="userreport.php?b='.$sco->id.'&user='.$scouser->userid.'&attempt='.$scouser->attempt.
466 '" title="'.get_string('details', 'scorm').'">'.$score.'</a>';
472 $element='cmi.interactions_'.$i.'.id';
473 while(isset($trackdata->$element)) {
474 if ($displayoptions['qtext']) {
475 $element='cmi.interactions_'.$i.'.id';
476 if (isset($trackdata->$element)) {
477 $row[] = s($trackdata->$element);
482 if ($displayoptions['resp']) {
483 $element='cmi.interactions_'.$i.'.student_response';
484 if (isset($trackdata->$element)) {
485 $row[] = s($trackdata->$element);
490 if ($displayoptions['right']) {
492 $element = 'cmi.interactions_'.$i.'.correct_responses_'.$j.'.pattern';
494 if (isset($trackdata->$element)) {
495 while(isset($trackdata->$element)) {
499 $rightans .= s($trackdata->$element);
501 $element = 'cmi.interactions_'.$i.'.correct_responses_'.$j.'.pattern';
509 $element = 'cmi.interactions_'.$i.'.id';
511 //---end of interaction data*/
513 // if we don't have track data, we haven't attempted yet
514 $strstatus = get_string('notattempted', 'scorm');
516 $row[] = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>'.$strstatus;
525 $table->add_data($row);
526 } else if ($download == 'Excel' or $download == 'ODS') {
528 foreach ($row as $item) {
529 $myxls->write($rownum, $colnum, $item, $format);
533 } else if ($download == 'CSV') {
534 $text = implode("\t", $row);
539 $table->finish_output();
541 echo '<table id="commands">';
543 echo '<a href="javascript:select_all_in(\'DIV\', null, \'scormtablecontainer\');">'.
544 get_string('selectall', 'scorm').'</a> / ';
545 echo '<a href="javascript:deselect_all_in(\'DIV\', null, \'scormtablecontainer\');">'.
546 get_string('selectnone', 'scorm').'</a> ';
548 echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
549 echo '</td></tr></table>';
555 if (!empty($attempts)) {
556 echo '<table class="boxaligncenter"><tr>';
558 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
559 array('download'=>'ODS') + $displayoptions),
560 get_string('downloadods'));
563 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
564 array('download'=>'Excel') + $displayoptions),
565 get_string('downloadexcel'));
568 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
569 array('download'=>'CSV') + $displayoptions),
570 get_string('downloadtext'));
574 echo '</tr></table>';
578 $mform->set_data(compact('detailedrep', 'pagesize'));
582 if ($candelete && !$download) {
587 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
589 if ($download == 'Excel' or $download == 'ODS') {
592 } else if ($download == 'CSV') {
596 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));