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

/mod/scorm/report/interactions/classes/report.php

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