PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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