Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400]

   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   * Data generator.
  19   *
  20   * @package    mod_resource
  21   * @copyright 2013 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * Resource module data generator class.
  30   *
  31   * @package    mod_resource
  32   * @copyright 2013 The Open University
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class mod_resource_generator extends testing_module_generator {
  36  
  37      /**
  38       * Creates new resource module instance. By default it contains a short
  39       * text file.
  40       *
  41       * @param array|stdClass $record data for module being generated. Requires 'course' key
  42       *     (an id or the full object). Also can have any fields from add module form, and a
  43       *     'defaultfilename' to set the name of the file created if no draft ID is supplied.
  44       * @param null|array $options general options for course module. Since 2.6 it is
  45       *     possible to omit this argument by merging options into $record
  46       * @return stdClass record from module-defined table with additional field
  47       *     cmid (corresponding id in course_modules table)
  48       */
  49      public function create_instance($record = null, array $options = null) {
  50          global $CFG, $USER;
  51          require_once($CFG->dirroot . '/lib/resourcelib.php');
  52          // Ensure the record can be modified without affecting calling code.
  53          $record = (object)(array)$record;
  54  
  55          // Fill in optional values if not specified.
  56          if (!isset($record->display)) {
  57              $record->display = RESOURCELIB_DISPLAY_AUTO;
  58          }
  59          if (!isset($record->printintro)) {
  60              $record->printintro = 0;
  61          }
  62          if (!isset($record->showsize)) {
  63              $record->showsize = 0;
  64          }
  65          if (!isset($record->showtype)) {
  66              $record->showtype = 0;
  67          }
  68          if (!isset($record->uploaded)) {
  69              $record->uploaded = 0;
  70          }
  71  
  72          // The 'files' value corresponds to the draft file area ID. If not
  73          // specified, create a default file.
  74          if (!isset($record->files)) {
  75              if (empty($USER->username) || $USER->username === 'guest') {
  76                  throw new coding_exception('resource generator requires a current user');
  77              }
  78              $usercontext = context_user::instance($USER->id);
  79              $filename = $record->defaultfilename ?? 'resource' . ($this->instancecount + 1) . '.txt';
  80  
  81              // Pick a random context id for specified user.
  82              $record->files = file_get_unused_draft_itemid();
  83  
  84              // Add actual file there.
  85              $filerecord = ['component' => 'user', 'filearea' => 'draft',
  86                      'contextid' => $usercontext->id, 'itemid' => $record->files,
  87                      'filename' => basename($filename), 'filepath' => '/'];
  88              $fs = get_file_storage();
  89              if ($record->uploaded == 1) {
  90                  // For uploading a file, it's required to specify a file, how not!
  91                  if (!isset($record->defaultfilename)) {
  92                      throw new coding_exception(
  93                          'The $record->defaultfilename option is required in order to upload a file');
  94                  }
  95                  // We require the full file path to exist when uploading a real file (fixture or whatever).
  96                  $fullfilepath = $CFG->dirroot . '/' . $record->defaultfilename;
  97                  if (!is_readable($fullfilepath)) {
  98                      throw new coding_exception(
  99                          'The $record->defaultfilename option must point to an existing file within dirroot');
 100                  }
 101                  // Create file using pathname (defaultfilename) set.
 102                  $fs->create_file_from_pathname($filerecord, $fullfilepath);
 103              } else {
 104                  // If defaultfilename is not set, create file from string "resource 1.txt".
 105                  $fs->create_file_from_string($filerecord, 'Test resource ' . $filename . ' file');
 106              }
 107          }
 108  
 109          // Do work to actually add the instance.
 110          return parent::create_instance($record, $options);
 111      }
 112  }