PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/scorm/report/objectives/classes/report.php

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