Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 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. 8 // 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. 13 // 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/>. 16 /** 17 * Core Report class of basic reporting plugin 18 * @package scormreport 19 * @subpackage basic 20 * @author Dan Marsden and Ankit Kumar Agarwal 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 */ 23 24 namespace scormreport_basic; 25 26 defined('MOODLE_INTERNAL') || die(); 27 require_once($CFG->libdir . '/csvlib.class.php'); 28 29 class report extends \mod_scorm\report { 30 /** 31 * displays the full report 32 * @param \stdClass $scorm full SCORM object 33 * @param \stdClass $cm - full course_module object 34 * @param \stdClass $course - full course object 35 * @param string $download - type of download being requested 36 */ 37 public function display($scorm, $cm, $course, $download) { 38 global $CFG, $DB, $OUTPUT, $PAGE; 39 40 $contextmodule = \context_module::instance($cm->id); 41 $action = optional_param('action', '', PARAM_ALPHA); 42 $attemptids = optional_param_array('attemptid', array(), PARAM_RAW); 43 $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT); 44 $PAGE->set_url(new \moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode))); 45 46 if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) { 47 if (scorm_delete_responses($attemptids, $scorm)) { // Delete responses. 48 echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess'); 49 } 50 } 51 // Find out current groups mode. 52 $currentgroup = groups_get_activity_group($cm, true); 53 54 // Detailed report. 55 $mform = new \mod_scorm_report_settings($PAGE->url, compact('currentgroup')); 56 if ($fromform = $mform->get_data()) { 57 $detailedrep = $fromform->detailedrep; 58 $pagesize = $fromform->pagesize; 59 set_user_preference('scorm_report_detailed', $detailedrep); 60 set_user_preference('scorm_report_pagesize', $pagesize); 61 } else { 62 $detailedrep = get_user_preferences('scorm_report_detailed', false); 63 $pagesize = get_user_preferences('scorm_report_pagesize', 0); 64 } 65 if ($pagesize < 1) { 66 $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE; 67 } 68 69 // Select group menu. 70 $displayoptions = array(); 71 $displayoptions['attemptsmode'] = $attemptsmode; 72 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used. 73 if (!$download) { 74 groups_print_activity_menu($cm, new \moodle_url($PAGE->url, $displayoptions)); 75 } 76 } 77 78 // We only want to show the checkbox to delete attempts 79 // if the user has permissions and if the report mode is showing attempts. 80 $candelete = has_capability('mod/scorm:deleteresponses', $contextmodule) 81 && ($attemptsmode != SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO); 82 // Select the students. 83 $nostudents = false; 84 list($allowedlistsql, $params) = get_enrolled_sql($contextmodule, 'mod/scorm:savetrack', (int) $currentgroup); 85 if (empty($currentgroup)) { 86 // All users who can attempt scoes. 87 if (!$DB->record_exists_sql($allowedlistsql, $params)) { 88 echo $OUTPUT->notification(get_string('nostudentsyet')); 89 $nostudents = true; 90 } 91 } else { 92 // All users who can attempt scoes and who are in the currently selected group. 93 if (!$DB->record_exists_sql($allowedlistsql, $params)) { 94 echo $OUTPUT->notification(get_string('nostudentsingroup')); 95 $nostudents = true; 96 } 97 } 98 99 if ( !$nostudents ) { 100 // Now check if asked download of data. 101 $coursecontext = \context_course::instance($course->id); 102 if ($download) { 103 $shortname = format_string($course->shortname, true, array('context' => $coursecontext)); 104 $filename = clean_filename("$shortname ".format_string($scorm->name, true)); 105 } 106 107 // Define table columns. 108 $columns = array(); 109 $headers = array(); 110 if (!$download && $candelete) { 111 $columns[] = 'checkbox'; 112 $headers[] = $this->generate_master_checkbox(); 113 } 114 if (!$download && $CFG->grade_report_showuserimage) { 115 $columns[] = 'picture'; 116 $headers[] = ''; 117 } 118 $columns[] = 'fullname'; 119 $headers[] = get_string('name'); 120 // TODO Does not support custom user profile fields (MDL-70456). 121 $extrafields = \core_user\fields::get_identity_fields($coursecontext, false); 122 foreach ($extrafields as $field) { 123 $columns[] = $field; 124 $headers[] = \core_user\fields::get_display_name($field); 125 } 126 127 $columns[] = 'attempt'; 128 $headers[] = get_string('attempt', 'scorm'); 129 $columns[] = 'start'; 130 $headers[] = get_string('started', 'scorm'); 131 $columns[] = 'finish'; 132 $headers[] = get_string('last', 'scorm'); 133 $columns[] = 'score'; 134 $headers[] = get_string('score', 'scorm'); 135 if ($detailedrep && $scoes = $DB->get_records('scorm_scoes', array("scorm" => $scorm->id), 'sortorder, id')) { 136 foreach ($scoes as $sco) { 137 if ($sco->launch != '') { 138 $columns[] = 'scograde'.$sco->id; 139 $headers[] = format_string($sco->title); 140 } 141 } 142 } else { 143 $scoes = null; 144 } 145 146 if (!$download) { 147 $table = new \flexible_table('mod-scorm-report'); 148 149 $table->define_columns($columns); 150 $table->define_headers($headers); 151 $table->define_baseurl($PAGE->url); 152 153 $table->sortable(true); 154 $table->collapsible(true); 155 156 // This is done to prevent redundant data, when a user has multiple attempts. 157 $table->column_suppress('picture'); 158 $table->column_suppress('fullname'); 159 foreach ($extrafields as $field) { 160 $table->column_suppress($field); 161 } 162 163 $table->no_sorting('start'); 164 $table->no_sorting('finish'); 165 $table->no_sorting('score'); 166 $table->no_sorting('checkbox'); 167 $table->no_sorting('picture'); 168 169 if ( $scoes ) { 170 foreach ($scoes as $sco) { 171 if ($sco->launch != '') { 172 $table->no_sorting('scograde'.$sco->id); 173 } 174 } 175 } 176 177 $table->column_class('picture', 'picture'); 178 $table->column_class('fullname', 'bold'); 179 $table->column_class('score', 'bold'); 180 181 $table->set_attribute('cellspacing', '0'); 182 $table->set_attribute('id', 'attempts'); 183 $table->set_attribute('class', 'generaltable generalbox'); 184 185 // Start working -- this is necessary as soon as the niceties are over. 186 $table->setup(); 187 } else if ($download == 'ODS') { 188 require_once("$CFG->libdir/odslib.class.php"); 189 190 $filename .= ".ods"; 191 // Creating a workbook. 192 $workbook = new \MoodleODSWorkbook("-"); 193 // Sending HTTP headers. 194 $workbook->send($filename); 195 // Creating the first worksheet. 196 $sheettitle = get_string('report', 'scorm'); 197 $myxls = $workbook->add_worksheet($sheettitle); 198 // Format types. 199 $format = $workbook->add_format(); 200 $format->set_bold(0); 201 $formatbc = $workbook->add_format(); 202 $formatbc->set_bold(1); 203 $formatbc->set_align('center'); 204 $formatb = $workbook->add_format(); 205 $formatb->set_bold(1); 206 $formaty = $workbook->add_format(); 207 $formaty->set_bg_color('yellow'); 208 $formatc = $workbook->add_format(); 209 $formatc->set_align('center'); 210 $formatr = $workbook->add_format(); 211 $formatr->set_bold(1); 212 $formatr->set_color('red'); 213 $formatr->set_align('center'); 214 $formatg = $workbook->add_format(); 215 $formatg->set_bold(1); 216 $formatg->set_color('green'); 217 $formatg->set_align('center'); 218 // Here starts workshhet headers. 219 220 $colnum = 0; 221 foreach ($headers as $item) { 222 $myxls->write(0, $colnum, $item, $formatbc); 223 $colnum++; 224 } 225 $rownum = 1; 226 } else if ($download == 'Excel') { 227 require_once("$CFG->libdir/excellib.class.php"); 228 229 $filename .= ".xls"; 230 // Creating a workbook. 231 $workbook = new \MoodleExcelWorkbook("-"); 232 // Sending HTTP headers. 233 $workbook->send($filename); 234 // Creating the first worksheet. 235 $sheettitle = get_string('report', 'scorm'); 236 $myxls = $workbook->add_worksheet($sheettitle); 237 // Format types. 238 $format = $workbook->add_format(); 239 $format->set_bold(0); 240 $formatbc = $workbook->add_format(); 241 $formatbc->set_bold(1); 242 $formatbc->set_align('center'); 243 $formatb = $workbook->add_format(); 244 $formatb->set_bold(1); 245 $formaty = $workbook->add_format(); 246 $formaty->set_bg_color('yellow'); 247 $formatc = $workbook->add_format(); 248 $formatc->set_align('center'); 249 $formatr = $workbook->add_format(); 250 $formatr->set_bold(1); 251 $formatr->set_color('red'); 252 $formatr->set_align('center'); 253 $formatg = $workbook->add_format(); 254 $formatg->set_bold(1); 255 $formatg->set_color('green'); 256 $formatg->set_align('center'); 257 258 $colnum = 0; 259 foreach ($headers as $item) { 260 $myxls->write(0, $colnum, $item, $formatbc); 261 $colnum++; 262 } 263 $rownum = 1; 264 } else if ($download == 'CSV') { 265 $csvexport = new \csv_export_writer("tab"); 266 $csvexport->set_filename($filename, ".txt"); 267 $csvexport->add_data($headers); 268 } 269 // Construct the SQL. 270 $select = 'SELECT DISTINCT '.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, '; 271 // TODO Does not support custom user profile fields (MDL-70456). 272 $userfields = \core_user\fields::for_identity($coursecontext, false)->with_userpic()->including('idnumber'); 273 $selectfields = $userfields->get_sql('u', false, '', 'userid')->selects; 274 $select .= 'st.scormid AS scormid, st.attempt AS attempt ' . $selectfields . ' '; 275 276 // This part is the same for all cases - join users and scorm_scoes_track tables. 277 $from = 'FROM {user} u '; 278 $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = '.$scorm->id; 279 switch ($attemptsmode) { 280 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH: 281 // Show only students with attempts. 282 $where = " WHERE u.id IN ({$allowedlistsql}) AND st.userid IS NOT NULL"; 283 break; 284 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO: 285 // Show only students without attempts. 286 $where = " WHERE u.id IN ({$allowedlistsql}) AND st.userid IS NULL"; 287 break; 288 case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS: 289 // Show all students with or without attempts. 290 $where = " WHERE u.id IN ({$allowedlistsql}) AND (st.userid IS NOT NULL OR st.userid IS NULL)"; 291 break; 292 } 293 294 $countsql = 'SELECT COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').')) AS nbresults, '; 295 $countsql .= 'COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'st.attempt').')) AS nbattempts, '; 296 $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers '; 297 $countsql .= $from.$where; 298 299 if (!$download) { 300 $sort = $table->get_sql_sort(); 301 } else { 302 $sort = ''; 303 } 304 // Fix some wired sorting. 305 if (empty($sort)) { 306 $sort = ' ORDER BY uniqueid'; 307 } else { 308 $sort = ' ORDER BY '.$sort; 309 } 310 311 if (!$download) { 312 // Add extra limits due to initials bar. 313 list($twhere, $tparams) = $table->get_sql_where(); 314 if ($twhere) { 315 $where .= ' AND '.$twhere; // Initial bar. 316 $params = array_merge($params, $tparams); 317 } 318 319 if (!empty($countsql)) { 320 $count = $DB->get_record_sql($countsql, $params); 321 $totalinitials = $count->nbresults; 322 if ($twhere) { 323 $countsql .= ' AND '.$twhere; 324 } 325 $count = $DB->get_record_sql($countsql, $params); 326 $total = $count->nbresults; 327 } 328 329 $table->pagesize($pagesize, $total); 330 331 echo \html_writer::start_div('scormattemptcounts'); 332 if ( $count->nbresults == $count->nbattempts ) { 333 echo get_string('reportcountattempts', 'scorm', $count); 334 } else if ( $count->nbattempts > 0 ) { 335 echo get_string('reportcountallattempts', 'scorm', $count); 336 } else { 337 echo $count->nbusers.' '.get_string('users'); 338 } 339 echo \html_writer::end_div(); 340 } 341 342 // Fetch the attempts. 343 if (!$download) { 344 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params, 345 $table->get_page_start(), $table->get_page_size()); 346 echo \html_writer::start_div('', array('id' => 'scormtablecontainer')); 347 if ($candelete) { 348 // Start form. 349 $strreallydel = addslashes_js(get_string('deleteattemptcheck', 'scorm')); 350 echo \html_writer::start_tag('form', array('id' => 'attemptsform', 'method' => 'post', 351 'action' => $PAGE->url->out(false), 352 'onsubmit' => 'return confirm("'.$strreallydel.'");')); 353 echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'delete')); 354 echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); 355 echo \html_writer::start_div('', array('style' => 'display: none;')); 356 echo \html_writer::input_hidden_params($PAGE->url); 357 echo \html_writer::end_div(); 358 echo \html_writer::start_div(); 359 } 360 $table->initialbars($totalinitials > 20); // Build table rows. 361 } else { 362 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params); 363 } 364 365 if ($attempts) { 366 foreach ($attempts as $scouser) { 367 $row = array(); 368 if (!empty($scouser->attempt)) { 369 $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt); 370 } else { 371 $timetracks = ''; 372 } 373 if (in_array('checkbox', $columns)) { 374 if ($candelete && !empty($timetracks->start)) { 375 $row[] = $this->generate_row_checkbox('attemptid[]', "{$scouser->userid}:{$scouser->attempt}"); 376 } else if ($candelete) { 377 $row[] = ''; 378 } 379 } 380 if (in_array('picture', $columns)) { 381 $user = new \stdClass(); 382 $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields())); 383 $user = username_load_fields_from_object($user, $scouser, null, $additionalfields); 384 $user->id = $scouser->userid; 385 $row[] = $OUTPUT->user_picture($user, array('courseid' => $course->id)); 386 } 387 if (!$download) { 388 $url = new \moodle_url('/user/view.php', array('id' => $scouser->userid, 'course' => $course->id)); 389 $row[] = \html_writer::link($url, fullname($scouser)); 390 } else { 391 $row[] = fullname($scouser); 392 } 393 foreach ($extrafields as $field) { 394 $row[] = s($scouser->{$field}); 395 } 396 if (empty($timetracks->start)) { 397 $row[] = '-'; 398 $row[] = '-'; 399 $row[] = '-'; 400 $row[] = '-'; 401 } else { 402 if (!$download) { 403 $url = new \moodle_url('/mod/scorm/report/userreport.php', array('id' => $cm->id, 404 'user' => $scouser->userid, 'attempt' => $scouser->attempt)); 405 $row[] = \html_writer::link($url, $scouser->attempt); 406 } else { 407 $row[] = $scouser->attempt; 408 } 409 if ($download == 'ODS' || $download == 'Excel' ) { 410 $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig")); 411 } else { 412 $row[] = userdate($timetracks->start); 413 } 414 if ($download == 'ODS' || $download == 'Excel' ) { 415 $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig')); 416 } else { 417 $row[] = userdate($timetracks->finish); 418 } 419 $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt); 420 } 421 // Print out all scores of attempt. 422 if ($scoes) { 423 foreach ($scoes as $sco) { 424 if ($sco->launch != '') { 425 if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) { 426 if ($trackdata->status == '') { 427 $trackdata->status = 'notattempted'; 428 } 429 $strstatus = get_string($trackdata->status, 'scorm'); 430 // If raw score exists, print it. 431 if ($trackdata->score_raw != '') { 432 $score = $trackdata->score_raw; 433 // Add max score if it exists. 434 if (scorm_version_check($scorm->version, SCORM_13)) { 435 $maxkey = 'cmi.score.max'; 436 } else { 437 $maxkey = 'cmi.core.score.max'; 438 } 439 if (isset($trackdata->$maxkey)) { 440 $score .= '/'.$trackdata->$maxkey; 441 } 442 // Else print out status. 443 } else { 444 $score = $strstatus; 445 } 446 if (!$download) { 447 $url = new \moodle_url('/mod/scorm/report/userreporttracks.php', array('id' => $cm->id, 448 'scoid' => $sco->id, 'user' => $scouser->userid, 'attempt' => $scouser->attempt)); 449 $row[] = $OUTPUT->pix_icon($trackdata->status, $strstatus, 'scorm') . '<br>' . 450 \html_writer::link($url, $score, array('title' => get_string('details', 'scorm'))); 451 } else { 452 $row[] = $score; 453 } 454 } else { 455 // If we don't have track data, we haven't attempted yet. 456 $strstatus = get_string('notattempted', 'scorm'); 457 if (!$download) { 458 $row[] = $OUTPUT->pix_icon('notattempted', $strstatus, 'scorm') . '<br>' . $strstatus; 459 } else { 460 $row[] = $strstatus; 461 } 462 } 463 } 464 } 465 } 466 467 if (!$download) { 468 $table->add_data($row); 469 } else if ($download == 'Excel' or $download == 'ODS') { 470 $colnum = 0; 471 foreach ($row as $item) { 472 $myxls->write($rownum, $colnum, $item, $format); 473 $colnum++; 474 } 475 $rownum++; 476 } else if ($download == 'CSV') { 477 $csvexport->add_data($row); 478 } 479 } 480 if (!$download) { 481 $table->finish_output(); 482 if ($candelete) { 483 echo \html_writer::start_tag('table', array('id' => 'commands')); 484 echo \html_writer::start_tag('tr').\html_writer::start_tag('td'); 485 echo $this->generate_delete_selected_button(); 486 echo \html_writer::end_tag('td').\html_writer::end_tag('tr').\html_writer::end_tag('table'); 487 // Close form. 488 echo \html_writer::end_tag('div'); 489 echo \html_writer::end_tag('form'); 490 } 491 echo \html_writer::end_div(); 492 if (!empty($attempts)) { 493 echo \html_writer::start_tag('table', array('class' => 'boxaligncenter')).\html_writer::start_tag('tr'); 494 echo \html_writer::start_tag('td'); 495 echo $OUTPUT->single_button(new \moodle_url($PAGE->url, 496 array('download' => 'ODS') + $displayoptions), 497 get_string('downloadods')); 498 echo \html_writer::end_tag('td'); 499 echo \html_writer::start_tag('td'); 500 echo $OUTPUT->single_button(new \moodle_url($PAGE->url, 501 array('download' => 'Excel') + $displayoptions), 502 get_string('downloadexcel')); 503 echo \html_writer::end_tag('td'); 504 echo \html_writer::start_tag('td'); 505 echo $OUTPUT->single_button(new \moodle_url($PAGE->url, 506 array('download' => 'CSV') + $displayoptions), 507 get_string('downloadtext')); 508 echo \html_writer::end_tag('td'); 509 echo \html_writer::start_tag('td'); 510 echo \html_writer::end_tag('td'); 511 echo \html_writer::end_tag('tr').\html_writer::end_tag('table'); 512 } 513 } 514 } else { 515 if ($candelete && !$download) { 516 echo \html_writer::end_div(); 517 echo \html_writer::end_tag('form'); 518 $table->finish_output(); 519 } 520 echo \html_writer::end_div(); 521 } 522 // Show preferences form irrespective of attempts are there to report or not. 523 if (!$download) { 524 $mform->set_data(compact('detailedrep', 'pagesize', 'attemptsmode')); 525 $mform->display(); 526 } 527 if ($download == 'Excel' or $download == 'ODS') { 528 $workbook->close(); 529 exit; 530 } else if ($download == 'CSV') { 531 $csvexport->download_file(); 532 exit; 533 } 534 } else { 535 echo $OUTPUT->notification(get_string('noactivity', 'scorm')); 536 } 537 }// Function ends. 538 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body