MDL-41402 tool_generator: fix whitespace
[moodle.git] / admin / tool / generator / classes / site_backend.php
CommitLineData
338bf2e0
DM
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 * tool_generator site backend.
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
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Backend code for the site generator.
29 *
30 * @package tool_generator
31 * @copyright 2013 David MonllaĆ³
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_generator_site_backend extends tool_generator_backend {
35
36 /**
37 * @var string The course's shortname prefix.
38 */
39 const SHORTNAMEPREFIX = 'testcourse_';
40
41 /**
42 * @var bool If the debugging level checking was skipped.
43 */
44 protected $bypasscheck;
45
46 /**
47 * @var array Multidimensional array where the first level is the course size and the second the site size.
48 */
49 protected static $sitecourses = array(
50 array(2, 8, 64, 256, 1024, 4096),
51 array(1, 4, 8, 16, 32, 64),
52 array(0, 0, 1, 4, 8, 16),
53 array(0, 0, 0, 1, 0, 0),
54 array(0, 0, 0, 0, 1, 0),
55 array(0, 0, 0, 0, 0, 1)
56 );
57
58 /**
59 * Constructs object ready to make the site.
60 *
61 * @param int $size Size as numeric index
62 * @param bool $bypasscheck If debugging level checking was skipped.
63 * @param bool $fixeddataset To use fixed or random data
64 * @param bool $progress True if progress information should be displayed
65 * @return int Course id
66 */
67 public function __construct($size, $bypasscheck, $fixeddataset = false, $progress = true) {
68
69 // Set parameters.
70 $this->bypasscheck = $bypasscheck;
71
72 parent::__construct($size, $fixeddataset, $progress);
73 }
74
75 /**
76 * Gets a list of size choices supported by this backend.
77 *
78 * @return array List of size (int) => text description for display
79 */
80 public static function get_size_choices() {
81 $options = array();
82 for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
83 $options[$size] = get_string('sitesize_' . $size, 'tool_generator');
84 }
85 return $options;
86 }
87
88 /**
89 * Runs the entire 'make' process.
90 *
91 * @return int Course id
92 */
93 public function make() {
94 global $DB, $CFG;
95
96 raise_memory_limit(MEMORY_EXTRA);
97
98 if ($this->progress && !CLI_SCRIPT) {
99 echo html_writer::start_tag('ul');
100 }
101
102 $entirestart = microtime(true);
103
104 // Create courses.
105 $prevchdir = getcwd();
106 chdir($CFG->dirroot);
107 $ncourse = $this->get_last_testcourse_id();
108 foreach (self::$sitecourses as $coursesize => $ncourses) {
109 for ($i = 1; $i <= $ncourses[$this->size]; $i++) {
110 // Non language-dependant shortname.
111 $ncourse++;
112 $this->run_create_course(self::SHORTNAMEPREFIX . $ncourse, $coursesize);
113 }
114 }
115 chdir($prevchdir);
116
117 // Store last course id to return it (will be the bigger one).
118 $lastcourseid = $DB->get_field('course', 'id', array('shortname' => self::SHORTNAMEPREFIX . $ncourse));
119
120 // Log total time.
121 $this->log('sitecompleted', round(microtime(true) - $entirestart, 1));
122
123 if ($this->progress && !CLI_SCRIPT) {
124 echo html_writer::end_tag('ul');
125 }
126
127 return $lastcourseid;
128 }
129
130 /**
131 * Creates a course with the specified shortname, coursesize and the provided maketestsite options.
132 *
133 * @param string $shortname The course shortname
134 * @param int $coursesize One of the possible course sizes.
135 * @return void
136 */
137 protected function run_create_course($shortname, $coursesize) {
138
139 // We are in $CFG->dirroot.
140 $command = 'php admin/tool/generator/cli/maketestcourse.php';
141
142 $options = array(
143 '--shortname="' . $shortname . '"',
144 '--size="' . get_string('shortsize_' . $coursesize, 'tool_generator') . '"'
145 );
146
147 if (!$this->progress) {
148 $options[] = '--quiet';
149 }
150
151 // Extend options.
152 $optionstoextend = array(
153 'fixeddataset' => 'fixeddataset',
154 'bypasscheck' => 'bypasscheck',
155 );
156
157 // Getting an options string.
158 foreach ($optionstoextend as $attribute => $option) {
159 if (!empty($this->{$attribute})) {
160 $options[] = '--' . $option;
161 }
162 }
163 $options = implode(' ', $options);
164 if ($this->progress) {
165 system($command . ' ' . $options, $exitcode);
166 } else {
167 passthru($command . ' ' . $options, $exitcode);
168 }
169
170 if ($exitcode != 0) {
171 exit($exitcode);
172 }
173 }
174
175 /**
176 * Obtains the last unique sufix (numeric) using the test course prefix.
177 *
178 * @return int The last generated numeric value.
179 */
180 protected function get_last_testcourse_id() {
181 global $DB;
182
183 $params = array();
184 $params['shortnameprefix'] = $DB->sql_like_escape(self::SHORTNAMEPREFIX) . '%';
185 $like = $DB->sql_like('shortname', ':shortnameprefix');
186
187 if (!$testcourses = $DB->get_records_select('course', $like, $params, 'shortname DESC')) {
188 return 0;
189 }
190
191 // They come ordered by shortname DESC, so non-numeric values will be the first ones.
192 foreach ($testcourses as $testcourse) {
193 $sufix = substr($testcourse->shortname, strlen(self::SHORTNAMEPREFIX));
194 if (is_numeric($sufix)) {
195 return $sufix;
196 }
197 }
198
199 // If all sufixes are not numeric this is the fist make test site run.
200 return 0;
201 }
202
203}