Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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  /**
  18   * mod_scorm data generator.
  19   *
  20   * @package    mod_scorm
  21   * @category   test
  22   * @copyright  2013 Marina Glancy
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * mod_scorm data generator class.
  30   *
  31   * @package    mod_scorm
  32   * @category   test
  33   * @copyright  2013 Marina Glancy
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mod_scorm_generator extends testing_module_generator {
  37  
  38      public function create_instance($record = null, array $options = null) {
  39          global $CFG, $USER;
  40          require_once($CFG->dirroot.'/mod/scorm/lib.php');
  41          require_once($CFG->dirroot.'/mod/scorm/locallib.php');
  42          $cfgscorm = get_config('scorm');
  43  
  44          // Add default values for scorm.
  45          $record = (array)$record + array(
  46              'scormtype' => SCORM_TYPE_LOCAL,
  47              'packagefile' => '',
  48              'packageurl' => '',
  49              'updatefreq' => SCORM_UPDATE_NEVER,
  50              'popup' => 0,
  51              'width' => $cfgscorm->framewidth,
  52              'height' => $cfgscorm->frameheight,
  53              'skipview' => $cfgscorm->skipview,
  54              'hidebrowse' => $cfgscorm->hidebrowse,
  55              'displaycoursestructure' => $cfgscorm->displaycoursestructure,
  56              'hidetoc' => $cfgscorm->hidetoc,
  57              'nav' => $cfgscorm->nav,
  58              'navpositionleft' => $cfgscorm->navpositionleft,
  59              'navpositiontop' => $cfgscorm->navpositiontop,
  60              'displayattemptstatus' => $cfgscorm->displayattemptstatus,
  61              'timeopen' => 0,
  62              'timeclose' => 0,
  63              'grademethod' => GRADESCOES,
  64              'maxgrade' => $cfgscorm->maxgrade,
  65              'maxattempt' => $cfgscorm->maxattempt,
  66              'whatgrade' => $cfgscorm->whatgrade,
  67              'forcenewattempt' => $cfgscorm->forcenewattempt,
  68              'lastattemptlock' => $cfgscorm->lastattemptlock,
  69              'forcecompleted' => $cfgscorm->forcecompleted,
  70              'masteryoverride' => $cfgscorm->masteryoverride,
  71              'auto' => $cfgscorm->auto
  72          );
  73          if (empty($record['packagefilepath'])) {
  74              $record['packagefilepath'] = $CFG->dirroot.'/mod/scorm/tests/packages/singlescobasic.zip';
  75          }
  76          if (strpos($record['packagefilepath'], $CFG->dirroot) !== 0) {
  77              $record['packagefilepath'] = "{$CFG->dirroot}/{$record['packagefilepath']}";
  78          }
  79  
  80          // The 'packagefile' value corresponds to the draft file area ID. If not specified, create from packagefilepath.
  81          if (empty($record['packagefile']) && $record['scormtype'] === SCORM_TYPE_LOCAL) {
  82              if (!isloggedin() || isguestuser()) {
  83                  throw new coding_exception('Scorm generator requires a current user');
  84              }
  85              if (!file_exists($record['packagefilepath'])) {
  86                  throw new coding_exception("File {$record['packagefilepath']} does not exist");
  87              }
  88              $usercontext = context_user::instance($USER->id);
  89  
  90              // Pick a random context id for specified user.
  91              $record['packagefile'] = file_get_unused_draft_itemid();
  92  
  93              // Add actual file there.
  94              $filerecord = array('component' => 'user', 'filearea' => 'draft',
  95                      'contextid' => $usercontext->id, 'itemid' => $record['packagefile'],
  96                      'filename' => basename($record['packagefilepath']), 'filepath' => '/');
  97              $fs = get_file_storage();
  98              $fs->create_file_from_pathname($filerecord, $record['packagefilepath']);
  99          }
 100  
 101          return parent::create_instance($record, (array)$options);
 102      }
 103  }