Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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   * CLI interface for creating a test course.
  19   *
  20   * @package tool_generator
  21   * @copyright 2013 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('CLI_SCRIPT', true);
  26  define('NO_OUTPUT_BUFFERING', true);
  27  
  28  require(__DIR__ . '/../../../../config.php');
  29  require_once($CFG->libdir. '/clilib.php');
  30  
  31  // CLI options.
  32  list($options, $unrecognized) = cli_get_params(
  33      array(
  34          'help' => false,
  35          'shortname' => false,
  36          'fullname' => false,
  37          'summary' => false,
  38          'size' => false,
  39          'fixeddataset' => false,
  40          'filesizelimit' => false,
  41          'bypasscheck' => false,
  42          'additionalmodules' => "",
  43          'quiet' => false
  44      ),
  45      array(
  46          'h' => 'help'
  47      )
  48  );
  49  
  50  // Display help.
  51  if (!empty($options['help']) || empty($options['shortname']) || empty($options['size'])) {
  52      echo "
  53  Utility to create standard test course. (Also available in GUI interface.)
  54  
  55  Not for use on live sites; only normally works if debugging is set to DEVELOPER
  56  level.
  57  
  58  Options:
  59  --shortname      Shortname of course to create (required)
  60  --fullname       Fullname of course to create (optional)
  61  --summary        Course summary, in double quotes (optional)
  62  --size           Size of course to create XS, S, M, L, XL, or XXL (required)
  63  --fixeddataset   Use a fixed data set instead of randomly generated data
  64  --filesizelimit  Limits the size of the generated files to the specified bytes
  65  --bypasscheck    Bypasses the developer-mode check (be careful!)
  66  --additionalmodules Additional modules to be created when creating a course: a comma separated modules names without the mod_prefix
  67                      (like quiz, forum...)
  68  --quiet          Do not show any output
  69  
  70  -h, --help     Print out this help
  71  
  72  Example from Moodle root directory:
  73  \$ php admin/tool/generator/cli/maketestcourse.php --shortname=SIZE_S --size=S
  74  ";
  75      // Exit with error unless we're showing this because they asked for it.
  76      exit(empty($options['help']) ? 1 : 0);
  77  }
  78  
  79  // Check debugging is set to developer level.
  80  if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
  81      cli_error(get_string('error_notdebugging', 'tool_generator'));
  82  }
  83  
  84  // Get options.
  85  $shortname = $options['shortname'];
  86  $fullname = $options['fullname'];
  87  $summary = $options['summary'];
  88  $sizename = $options['size'];
  89  $fixeddataset = $options['fixeddataset'];
  90  $filesizelimit = $options['filesizelimit'];
  91  
  92  // Check size.
  93  try {
  94      $size = tool_generator_course_backend::size_for_name($sizename);
  95  } catch (coding_exception $e) {
  96      cli_error("Invalid size ($sizename). Use --help for help.");
  97  }
  98  
  99  // Check shortname.
 100  if ($error = tool_generator_course_backend::check_shortname_available($shortname)) {
 101      cli_error($error);
 102  }
 103  
 104  // Switch to admin user account.
 105  \core\session\manager::set_user(get_admin());
 106  $additionalmodulesarray = [];
 107  if (!empty($options['additionalmodules'])) {
 108      $additionalmodulesarray = explode(',', trim($options['additionalmodules']));
 109  }
 110  // Do backend code to generate course.
 111  $backend = new tool_generator_course_backend(
 112      $shortname,
 113      $size,
 114      $fixeddataset,
 115      $filesizelimit,
 116      empty($options['quiet']),
 117      $fullname,
 118      $summary,
 119      FORMAT_HTML,
 120      $additionalmodulesarray
 121  );
 122  $id = $backend->make();
 123  
 124  if (empty($options['quiet'])) {
 125      echo PHP_EOL.'Generated course: '.course_get_url($id).PHP_EOL;
 126  }