/** * Form validation. * * @param array $data * @param array $files * @return void */ public function validation($data, $files) { global $DB; $errors = array(); // Check course doesn't already exist. if (!empty($data['shortname'])) { // Check shortname. $error = tool_generator_course_backend::check_shortname_available($data['shortname']); if ($error) { $errors['shortname'] = $error; } } return $errors; }
/** * Creates a small test course specifying a maximum size and checks the generated files size is limited. */ public function test_filesize_limit() { $this->resetAfterTest(); $this->setAdminUser(); // Limit. $filesizelimit = 100; // Create a limited XS course. $backend = new tool_generator_course_backend('TOOL_XS_LIMITED', 0, false, $filesizelimit, false); $courseid = $backend->make(); $course = get_course($courseid); $modinfo = get_fast_modinfo($course); // Check there are small files. $fs = get_file_storage(); $resources = $modinfo->get_instances_of('resource'); foreach ($resources as $resource) { $resourcecontext = context_module::instance($resource->id); $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false); foreach ($files as $file) { if ($file->get_mimetype() == 'application/octet-stream') { $this->assertLessThanOrEqual($filesizelimit, $file->get_filesize()); } } } // Create a non-limited XS course. $backend = new tool_generator_course_backend('TOOL_XS_NOLIMITS', 0, false, false, false); $courseid = $backend->make(); $course = get_course($courseid); $modinfo = get_fast_modinfo($course); // Check there are small files. $fs = get_file_storage(); $resources = $modinfo->get_instances_of('resource'); foreach ($resources as $resource) { $resourcecontext = context_module::instance($resource->id); $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false); foreach ($files as $file) { if ($file->get_mimetype() == 'application/octet-stream') { $this->assertGreaterThan($filesizelimit, (int) $file->get_filesize()); } } } }
exit(empty($options['help']) ? 1 : 0); } // Check debugging is set to developer level. if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) { cli_error(get_string('error_notdebugging', 'tool_generator')); } // Get options. $shortname = $options['shortname']; $fullname = $options['fullname']; $summary = $options['summary']; $sizename = $options['size']; $fixeddataset = $options['fixeddataset']; $filesizelimit = $options['filesizelimit']; // Check size. try { $size = tool_generator_course_backend::size_for_name($sizename); } catch (coding_exception $e) { cli_error("Invalid size ({$sizename}). Use --help for help."); } // Check shortname. if ($error = tool_generator_course_backend::check_shortname_available($shortname)) { cli_error($error); } // Switch to admin user account. \core\session\manager::set_user(get_admin()); // Do backend code to generate course. $backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, $filesizelimit, empty($options['quiet']), $fullname, $summary, FORMAT_HTML); $id = $backend->make(); if (empty($options['quiet'])) { echo PHP_EOL . 'Generated course: ' . course_get_url($id) . PHP_EOL; }
/** * Checks if the selected target course is ok. * * @param int|string $course * @param int $size * @return array Errors array or false if everything is ok */ public static function has_selected_course_any_problem($course, $size) { global $DB; $errors = array(); if (!is_numeric($course)) { if (!($course = $DB->get_field('course', 'id', array('shortname' => $course)))) { $errors['courseid'] = get_string('error_nonexistingcourse', 'tool_generator'); return $errors; } } $coursecontext = context_course::instance($course, IGNORE_MISSING); if (!$coursecontext) { $errors['courseid'] = get_string('error_nonexistingcourse', 'tool_generator'); return $errors; } if (!($users = get_enrolled_users($coursecontext, '', 0, 'u.id'))) { $errors['courseid'] = get_string('coursewithoutusers', 'tool_generator'); } // Checks that the selected course has enough users. $coursesizes = tool_generator_course_backend::get_users_per_size(); if (count($users) < self::$users[$size]) { $errors['size'] = get_string('notenoughusers', 'tool_generator'); } if (empty($errors)) { return false; } return $errors; }