Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

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