Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   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 plan
  19   *
  20   * @package tool_generator
  21   * @copyright 2013 David MonllaĆ³
  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          'size' => false,
  37          'bypasscheck' => false,
  38          'updateuserspassword' => false
  39      ),
  40      array(
  41          'h' => 'help'
  42      )
  43  );
  44  
  45  $testplansizes = '* ' . implode(PHP_EOL . '* ', tool_generator_testplan_backend::get_size_choices());
  46  
  47  // Display help.
  48  if (!empty($options['help']) || empty($options['shortname']) || empty($options['size'])) {
  49  
  50      echo get_string('testplanexplanation', 'tool_generator', tool_generator_testplan_backend::get_repourl()) .
  51  "Options:
  52  -h, --help              Print out this help
  53  --shortname             Shortname of the test plan's target course (required)
  54  --size                  Size of the test plan to create XS, S, M, L, XL, or XXL (required)
  55  --bypasscheck           Bypasses the developer-mode check (be careful!)
  56  --updateuserspassword   Updates the target course users password according to \$CFG->tool_generator_users_password
  57  
  58  $testplansizes
  59  
  60  Consider that, the server resources you will need to run the test plan will be higher as the test plan size is higher.
  61  
  62  Example from Moodle root directory:
  63  \$ sudo -u www-data /usr/bin/php admin/tool/generator/cli/maketestplan.php --shortname=\"testcourse_12\" --size=S
  64  ";
  65      // Exit with error unless we're showing this because they asked for it.
  66      exit(empty($options['help']) ? 1 : 0);
  67  }
  68  
  69  // Check debugging is set to developer level.
  70  if (empty($options['bypasscheck']) && !$CFG->debugdeveloper) {
  71      cli_error(get_string('error_notdebugging', 'tool_generator'));
  72  }
  73  
  74  // Get options.
  75  $shortname = $options['shortname'];
  76  $sizename = $options['size'];
  77  
  78  // Check size.
  79  try {
  80      $size = tool_generator_testplan_backend::size_for_name($sizename);
  81  } catch (coding_exception $e) {
  82      cli_error("Error: Invalid size ($sizename). Use --help for help.");
  83  }
  84  
  85  // Check selected course.
  86  if ($errors = tool_generator_testplan_backend::has_selected_course_any_problem($shortname, $size)) {
  87      // Showing the first reported problem.
  88      cli_error("Error: " . reset($errors));
  89  }
  90  
  91  // Checking if test users password is set.
  92  if (empty($CFG->tool_generator_users_password) || is_bool($CFG->tool_generator_users_password)) {
  93      cli_error("Error: " . get_string('error_nouserspassword', 'tool_generator'));
  94  }
  95  
  96  // Switch to admin user account.
  97  \core\session\manager::set_user(get_admin());
  98  
  99  // Create files.
 100  $courseid = $DB->get_field('course', 'id', array('shortname' => $shortname));
 101  $usersfile = tool_generator_testplan_backend::create_users_file($courseid, !empty($options['updateuserspassword']), $size);
 102  $testplanfile = tool_generator_testplan_backend::create_testplan_file($courseid, $size);
 103  
 104  // One file path per line so other CLI scripts can easily parse the output.
 105  echo moodle_url::make_pluginfile_url(
 106          $testplanfile->get_contextid(),
 107          $testplanfile->get_component(),
 108          $testplanfile->get_filearea(),
 109          $testplanfile->get_itemid(),
 110          $testplanfile->get_filepath(),
 111          $testplanfile->get_filename()
 112      ) .
 113      PHP_EOL .
 114      moodle_url::make_pluginfile_url(
 115          $usersfile->get_contextid(),
 116          $usersfile->get_component(),
 117          $usersfile->get_filearea(),
 118          $usersfile->get_itemid(),
 119          $usersfile->get_filepath(),
 120          $usersfile->get_filename()
 121      ) .
 122      PHP_EOL;