Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
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 course backend code. 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 defined('MOODLE_INTERNAL') || die(); 26 27 /** 28 * Backend code for the 'make large course' tool. 29 * 30 * @package tool_generator 31 * @copyright 2013 The Open University 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class tool_generator_course_backend extends tool_generator_backend { 35 /** 36 * @var array Number of sections in course 37 */ 38 private static $paramsections = array(1, 10, 100, 500, 1000, 2000); 39 /** 40 * @var array Number of assignments in course 41 */ 42 private static $paramassignments = array(1, 10, 100, 500, 1000, 2000); 43 /** 44 * @var array Number of Page activities in course 45 */ 46 private static $parampages = array(1, 50, 200, 1000, 5000, 10000); 47 /** 48 * @var array Number of students enrolled in course 49 */ 50 private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000); 51 /** 52 * Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB. 53 * 54 * @var array Number of small files created in a single file activity 55 */ 56 private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768); 57 /** 58 * @var array Size of small files (to make the totals into nice numbers) 59 */ 60 private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536); 61 /** 62 * Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB. 63 * 64 * @var array Number of big files created as individual file activities 65 */ 66 private static $parambigfilecount = array(1, 2, 5, 10, 10, 10); 67 /** 68 * @var array Size of each large file 69 */ 70 private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080, 71 858993459, 1717986918); 72 /** 73 * @var array Number of forum discussions 74 */ 75 private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000); 76 /** 77 * @var array Number of forum posts per discussion 78 */ 79 private static $paramforumposts = array(2, 2, 5, 10, 10, 10); 80 81 /** 82 * @var string Course shortname 83 */ 84 private $shortname; 85 86 /** 87 * @var string Course fullname. 88 */ 89 private $fullname = ""; 90 91 /** 92 * @var string Course summary. 93 */ 94 private $summary = ""; 95 96 /** 97 * @var string Course summary format, defaults to FORMAT_HTML. 98 */ 99 private $summaryformat = FORMAT_HTML; 100 101 /** 102 * @var testing_data_generator Data generator 103 */ 104 protected $generator; 105 106 /** 107 * @var stdClass Course object 108 */ 109 private $course; 110 111 /** 112 * @var array Array from test user number (1...N) to userid in database 113 */ 114 private $userids; 115 116 /** 117 * Constructs object ready to create course. 118 * 119 * @param string $shortname Course shortname 120 * @param int $size Size as numeric index 121 * @param bool $fixeddataset To use fixed or random data 122 * @param int|bool $filesizelimit The max number of bytes for a generated file 123 * @param bool $progress True if progress information should be displayed 124 */ 125 public function __construct( 126 $shortname, 127 $size, 128 $fixeddataset = false, 129 $filesizelimit = false, 130 $progress = true, 131 $fullname = null, 132 $summary = null, 133 $summaryformat = FORMAT_HTML) { 134 135 // Set parameters. 136 $this->shortname = $shortname; 137 138 // We can't allow fullname to be set to an empty string. 139 if (empty($fullname)) { 140 $this->fullname = get_string( 141 'fullname', 142 'tool_generator', 143 array( 144 'size' => get_string('shortsize_' . $size, 'tool_generator') 145 ) 146 ); 147 } else { 148 $this->fullname = $fullname; 149 } 150 151 // Summary, on the other hand, should be empty-able. 152 if (!is_null($summary)) { 153 $this->summary = $summary; 154 $this->summaryformat = $summaryformat; 155 } 156 157 parent::__construct($size, $fixeddataset, $filesizelimit, $progress); 158 } 159 160 /** 161 * Returns the relation between users and course sizes. 162 * 163 * @return array 164 */ 165 public static function get_users_per_size() { 166 return self::$paramusers; 167 } 168 169 /** 170 * Gets a list of size choices supported by this backend. 171 * 172 * @return array List of size (int) => text description for display 173 */ 174 public static function get_size_choices() { 175 $options = array(); 176 for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) { 177 $options[$size] = get_string('coursesize_' . $size, 'tool_generator'); 178 } 179 return $options; 180 } 181 182 /** 183 * Checks that a shortname is available (unused). 184 * 185 * @param string $shortname Proposed course shortname 186 * @return string An error message if the name is unavailable or '' if OK 187 */ 188 public static function check_shortname_available($shortname) { 189 global $DB; 190 $fullname = $DB->get_field('course', 'fullname', 191 array('shortname' => $shortname), IGNORE_MISSING); 192 if ($fullname !== false) { 193 // I wanted to throw an exception here but it is not possible to 194 // use strings from moodle.php in exceptions, and I didn't want 195 // to duplicate the string in tool_generator, so I changed this to 196 // not use exceptions. 197 return get_string('shortnametaken', 'moodle', $fullname); 198 } 199 return ''; 200 } 201 202 /** 203 * Runs the entire 'make' process. 204 * 205 * @return int Course id 206 */ 207 public function make() { 208 global $DB, $CFG; 209 require_once($CFG->dirroot . '/lib/phpunit/classes/util.php'); 210 211 raise_memory_limit(MEMORY_EXTRA); 212 213 if ($this->progress && !CLI_SCRIPT) { 214 echo html_writer::start_tag('ul'); 215 } 216 217 $entirestart = microtime(true); 218 219 // Get generator. 220 $this->generator = phpunit_util::get_data_generator(); 221 222 // Make course. 223 $this->course = $this->create_course(); 224 225 $this->create_assignments(); 226 $this->create_pages(); 227 $this->create_small_files(); 228 $this->create_big_files(); 229 230 // Create users as late as possible to reduce regarding in the gradebook. 231 $this->create_users(); 232 $this->create_forum(); 233 234 // Log total time. 235 $this->log('coursecompleted', round(microtime(true) - $entirestart, 1)); 236 237 if ($this->progress && !CLI_SCRIPT) { 238 echo html_writer::end_tag('ul'); 239 } 240 241 return $this->course->id; 242 } 243 244 /** 245 * Creates the actual course. 246 * 247 * @return stdClass Course record 248 */ 249 private function create_course() { 250 $this->log('createcourse', $this->shortname); 251 $courserecord = array( 252 'shortname' => $this->shortname, 253 'fullname' => $this->fullname, 254 'numsections' => self::$paramsections[$this->size], 255 'startdate' => usergetmidnight(time()) 256 ); 257 if (strlen($this->summary) > 0) { 258 $courserecord['summary'] = $this->summary; 259 $courserecord['summary_format'] = $this->summaryformat; 260 } 261 262 return $this->generator->create_course($courserecord, array('createsections' => true)); 263 } 264 265 /** 266 * Creates a number of user accounts and enrols them on the course. 267 * Note: Existing user accounts that were created by this system are 268 * reused if available. 269 */ 270 private function create_users() { 271 global $DB; 272 273 // Work out total number of users. 274 $count = self::$paramusers[$this->size]; 275 276 // Get existing users in order. We will 'fill up holes' in this up to 277 // the required number. 278 $this->log('checkaccounts', $count); 279 $nextnumber = 1; 280 $rs = $DB->get_recordset_select('user', $DB->sql_like('username', '?'), 281 array('tool_generator_%'), 'username', 'id, username'); 282 foreach ($rs as $rec) { 283 // Extract number from username. 284 $matches = array(); 285 if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) { 286 continue; 287 } 288 $number = (int)$matches[1]; 289 290 // Create missing users in range up to this. 291 if ($number != $nextnumber) { 292 $this->create_user_accounts($nextnumber, min($number - 1, $count)); 293 } else { 294 $this->userids[$number] = (int)$rec->id; 295 } 296 297 // Stop if we've got enough users. 298 $nextnumber = $number + 1; 299 if ($number >= $count) { 300 break; 301 } 302 } 303 $rs->close(); 304 305 // Create users from end of existing range. 306 if ($nextnumber <= $count) { 307 $this->create_user_accounts($nextnumber, $count); 308 } 309 310 // Assign all users to course. 311 $this->log('enrol', $count, true); 312 313 $enrolplugin = enrol_get_plugin('manual'); 314 $instances = enrol_get_instances($this->course->id, true); 315 foreach ($instances as $instance) { 316 if ($instance->enrol === 'manual') { 317 break; 318 } 319 } 320 if ($instance->enrol !== 'manual') { 321 throw new coding_exception('No manual enrol plugin in course'); 322 } 323 $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); 324 325 for ($number = 1; $number <= $count; $number++) { 326 // Enrol user. 327 $enrolplugin->enrol_user($instance, $this->userids[$number], $role->id); 328 $this->dot($number, $count); 329 } 330 331 // Sets the pointer at the beginning to be aware of the users we use. 332 reset($this->userids); 333 334 $this->end_log(); 335 } 336 337 /** 338 * Creates user accounts with a numeric range. 339 * 340 * @param int $first Number of first user 341 * @param int $last Number of last user 342 */ 343 private function create_user_accounts($first, $last) { 344 global $CFG; 345 346 $this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true); 347 $count = $last - $first + 1; 348 $done = 0; 349 for ($number = $first; $number <= $last; $number++, $done++) { 350 // Work out username with 6-digit number. 351 $textnumber = (string)$number; 352 while (strlen($textnumber) < 6) { 353 $textnumber = '0' . $textnumber; 354 } 355 $username = 'tool_generator_' . $textnumber; 356 357 // Create user account. 358 $record = array('username' => $username, 'idnumber' => $number); 359 360 // We add a user password if it has been specified. 361 if (!empty($CFG->tool_generator_users_password)) { 362 $record['password'] = $CFG->tool_generator_users_password; 363 } 364 365 $user = $this->generator->create_user($record); 366 $this->userids[$number] = (int)$user->id; 367 $this->dot($done, $count); 368 } 369 $this->end_log(); 370 } 371 372 /** 373 * Creates a number of Assignment activities. 374 */ 375 private function create_assignments() { 376 // Set up generator. 377 $assigngenerator = $this->generator->get_plugin_generator('mod_assign'); 378 379 // Create assignments. 380 $number = self::$paramassignments[$this->size]; 381 $this->log('createassignments', $number, true); 382 for ($i = 0; $i < $number; $i++) { 383 $record = array('course' => $this->course); 384 $options = array('section' => $this->get_target_section()); 385 $assigngenerator->create_instance($record, $options); 386 $this->dot($i, $number); 387 } 388 389 $this->end_log(); 390 } 391 392 /** 393 * Creates a number of Page activities. 394 */ 395 private function create_pages() { 396 // Set up generator. 397 $pagegenerator = $this->generator->get_plugin_generator('mod_page'); 398 399 // Create pages. 400 $number = self::$parampages[$this->size]; 401 $this->log('createpages', $number, true); 402 for ($i = 0; $i < $number; $i++) { 403 $record = array('course' => $this->course); 404 $options = array('section' => $this->get_target_section()); 405 $pagegenerator->create_instance($record, $options); 406 $this->dot($i, $number); 407 } 408 409 $this->end_log(); 410 } 411 412 /** 413 * Creates one resource activity with a lot of small files. 414 */ 415 private function create_small_files() { 416 $count = self::$paramsmallfilecount[$this->size]; 417 $this->log('createsmallfiles', $count, true); 418 419 // Create resource with default textfile only. 420 $resourcegenerator = $this->generator->get_plugin_generator('mod_resource'); 421 $record = array('course' => $this->course, 422 'name' => get_string('smallfiles', 'tool_generator')); 423 $options = array('section' => 0); 424 $resource = $resourcegenerator->create_instance($record, $options); 425 426 // Add files. 427 $fs = get_file_storage(); 428 $context = context_module::instance($resource->cmid); 429 $filerecord = array('component' => 'mod_resource', 'filearea' => 'content', 430 'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/'); 431 for ($i = 0; $i < $count; $i++) { 432 $filerecord['filename'] = 'smallfile' . $i . '.dat'; 433 434 // Generate random binary data (different for each file so it 435 // doesn't compress unrealistically). 436 $data = random_bytes_emulate($this->limit_filesize(self::$paramsmallfilesize[$this->size])); 437 438 $fs->create_file_from_string($filerecord, $data); 439 $this->dot($i, $count); 440 } 441 442 $this->end_log(); 443 } 444 445 /** 446 * Creates a number of resource activities with one big file each. 447 */ 448 private function create_big_files() { 449 // Work out how many files and how many blocks to use (up to 64KB). 450 $count = self::$parambigfilecount[$this->size]; 451 $filesize = $this->limit_filesize(self::$parambigfilesize[$this->size]); 452 $blocks = ceil($filesize / 65536); 453 $blocksize = floor($filesize / $blocks); 454 455 $this->log('createbigfiles', $count, true); 456 457 // Prepare temp area. 458 $tempfolder = make_temp_directory('tool_generator'); 459 $tempfile = $tempfolder . '/' . rand(); 460 461 // Create resources and files. 462 $fs = get_file_storage(); 463 $resourcegenerator = $this->generator->get_plugin_generator('mod_resource'); 464 for ($i = 0; $i < $count; $i++) { 465 // Create resource. 466 $record = array('course' => $this->course, 467 'name' => get_string('bigfile', 'tool_generator', $i)); 468 $options = array('section' => $this->get_target_section()); 469 $resource = $resourcegenerator->create_instance($record, $options); 470 471 // Write file. 472 $handle = fopen($tempfile, 'w'); 473 if (!$handle) { 474 throw new coding_exception('Failed to open temporary file'); 475 } 476 for ($j = 0; $j < $blocks; $j++) { 477 $data = random_bytes_emulate($blocksize); 478 fwrite($handle, $data); 479 $this->dot($i * $blocks + $j, $count * $blocks); 480 } 481 fclose($handle); 482 483 // Add file. 484 $context = context_module::instance($resource->cmid); 485 $filerecord = array('component' => 'mod_resource', 'filearea' => 'content', 486 'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/', 487 'filename' => 'bigfile' . $i . '.dat'); 488 $fs->create_file_from_pathname($filerecord, $tempfile); 489 } 490 491 unlink($tempfile); 492 $this->end_log(); 493 } 494 495 /** 496 * Creates one forum activity with a bunch of posts. 497 */ 498 private function create_forum() { 499 global $DB; 500 501 $discussions = self::$paramforumdiscussions[$this->size]; 502 $posts = self::$paramforumposts[$this->size]; 503 $totalposts = $discussions * $posts; 504 505 $this->log('createforum', $totalposts, true); 506 507 // Create empty forum. 508 $forumgenerator = $this->generator->get_plugin_generator('mod_forum'); 509 $record = array('course' => $this->course, 510 'name' => get_string('pluginname', 'forum')); 511 $options = array('section' => 0); 512 $forum = $forumgenerator->create_instance($record, $options); 513 514 // Add discussions and posts. 515 $sofar = 0; 516 for ($i = 0; $i < $discussions; $i++) { 517 $record = array('forum' => $forum->id, 'course' => $this->course->id, 518 'userid' => $this->get_target_user()); 519 $discussion = $forumgenerator->create_discussion($record); 520 $parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST); 521 $sofar++; 522 for ($j = 0; $j < $posts - 1; $j++, $sofar++) { 523 $record = array('discussion' => $discussion->id, 524 'userid' => $this->get_target_user(), 'parent' => $parentid); 525 $forumgenerator->create_post($record); 526 $this->dot($sofar, $totalposts); 527 } 528 } 529 530 $this->end_log(); 531 } 532 533 /** 534 * Gets a section number. 535 * 536 * Depends on $this->fixeddataset. 537 * 538 * @return int A section number from 1 to the number of sections 539 */ 540 private function get_target_section() { 541 542 if (!$this->fixeddataset) { 543 $key = rand(1, self::$paramsections[$this->size]); 544 } else { 545 // Using section 1. 546 $key = 1; 547 } 548 549 return $key; 550 } 551 552 /** 553 * Gets a user id. 554 * 555 * Depends on $this->fixeddataset. 556 * 557 * @return int A user id for a random created user 558 */ 559 private function get_target_user() { 560 561 if (!$this->fixeddataset) { 562 $userid = $this->userids[rand(1, self::$paramusers[$this->size])]; 563 } else if ($userid = current($this->userids)) { 564 // Moving pointer to the next user. 565 next($this->userids); 566 } else { 567 // Returning to the beginning if we reached the end. 568 $userid = reset($this->userids); 569 } 570 571 return $userid; 572 } 573 574 /** 575 * Restricts the binary file size if necessary 576 * 577 * @param int $length The total length 578 * @return int The limited length if a limit was specified. 579 */ 580 private function limit_filesize($length) { 581 582 // Limit to $this->filesizelimit. 583 if (is_numeric($this->filesizelimit) && $length > $this->filesizelimit) { 584 $length = floor($this->filesizelimit); 585 } 586 587 return $length; 588 } 589 590 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body