MDL-51632 tool_generator: Print URL of the generated course
[moodle.git] / admin / tool / generator / cli / maketestcourse.php
CommitLineData
bc4054f9 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
25define('CLI_SCRIPT', true);
26define('NO_OUTPUT_BUFFERING', true);
27
28require(dirname(__FILE__) . '/../../../../config.php');
29require_once($CFG->libdir. '/clilib.php');
30
31// CLI options.
32list($options, $unrecognized) = cli_get_params(
33 array(
34 'help' => false,
35 'shortname' => false,
343b11a3
JC
36 'fullname' => false,
37 'summary' => false,
bc4054f9 38 'size' => false,
219cae03 39 'fixeddataset' => false,
53e6d79c 40 'filesizelimit' => false,
bc4054f9 41 'bypasscheck' => false,
42 'quiet' => false
43 ),
44 array(
45 'h' => 'help'
46 )
47);
48
49// Display help.
50if (!empty($options['help']) || empty($options['shortname']) || empty($options['size'])) {
51 echo "
52Utility to create standard test course. (Also available in GUI interface.)
53
54Not for use on live sites; only normally works if debugging is set to DEVELOPER
55level.
56
57Options:
53e6d79c 58--shortname Shortname of course to create (required)
343b11a3
JC
59--fullname Fullname of course to create (optional)
60--summary Course summary, in double quotes (optional)
53e6d79c
DM
61--size Size of course to create XS, S, M, L, XL, or XXL (required)
62--fixeddataset Use a fixed data set instead of randomly generated data
63--filesizelimit Limits the size of the generated files to the specified bytes
64--bypasscheck Bypasses the developer-mode check (be careful!)
65--quiet Do not show any output
bc4054f9 66
67-h, --help Print out this help
68
69Example from Moodle root directory:
70\$ php admin/tool/generator/cli/maketestcourse.php --shortname=SIZE_S --size=S
71";
72 // Exit with error unless we're showing this because they asked for it.
73 exit(empty($options['help']) ? 1 : 0);
74}
75
76// Check debugging is set to developer level.
77if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
78 cli_error(get_string('error_notdebugging', 'tool_generator'));
79}
80
81// Get options.
82$shortname = $options['shortname'];
343b11a3
JC
83$fullname = $options['fullname'];
84$summary = $options['summary'];
bc4054f9 85$sizename = $options['size'];
219cae03 86$fixeddataset = $options['fixeddataset'];
53e6d79c 87$filesizelimit = $options['filesizelimit'];
bc4054f9 88
89// Check size.
90try {
8cac8d3e 91 $size = tool_generator_course_backend::size_for_name($sizename);
bc4054f9 92} catch (coding_exception $e) {
93 cli_error("Invalid size ($sizename). Use --help for help.");
94}
95
96// Check shortname.
8cac8d3e 97if ($error = tool_generator_course_backend::check_shortname_available($shortname)) {
bc4054f9 98 cli_error($error);
99}
100
101// Switch to admin user account.
d79d5ac2 102\core\session\manager::set_user(get_admin());
bc4054f9 103
104// Do backend code to generate course.
343b11a3
JC
105$backend = new tool_generator_course_backend(
106 $shortname,
107 $size,
108 $fixeddataset,
109 $filesizelimit,
110 empty($options['quiet']),
111 $fullname,
112 $summary,
113 FORMAT_HTML
114);
bc4054f9 115$id = $backend->make();
aff1db05
MN
116
117if (empty($options['quiet'])) {
118 echo PHP_EOL.'Generated course: '.course_get_url($id).PHP_EOL;
119}