|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 defined('MOODLE_INTERNAL') || die(); 00027 00028 require_once($CFG->libdir . '/formslib.php'); 00029 require_once($CFG->dirroot .'/course/lib.php'); 00030 require_once($CFG->libdir .'/filelib.php'); 00031 00032 define('GENERATOR_RANDOM', 0); 00033 define('GENERATOR_SEQUENCE', 1); 00034 00038 class generator { 00039 public $modules_to_ignore = array('hotpot', 'lams', 'journal', 'scorm', 'exercise', 'dialogue'); 00040 public $modules_list = array('forum' => 'forum', 00041 'assignment' => 'assignment', 00042 'chat' => 'chat', 00043 'data' => 'data', 00044 'glossary' => 'glossary', 00045 'quiz' => 'quiz', 00046 'comments' => 'comments', 00047 'feedback' => 'feedback', 00048 'label' => 'label', 00049 'lesson' => 'lesson', 00050 'chat' => 'chat', 00051 'choice' => 'choice', 00052 'resource' => 'resource', 00053 'survey' => 'survey', 00054 'wiki' => 'wiki', 00055 'workshop' => 'workshop'); 00056 00057 public $resource_types = array('text', 'file', 'html', 'repository', 'directory', 'ims'); 00058 public $glossary_formats = array('continuous', 'encyclopedia', 'entrylist', 'faq', 'fullwithauthor', 'fullwithoutauthor', 'dictionary'); 00059 public $assignment_types = array('upload', 'uploadsingle', 'online', 'offline'); 00060 public $forum_types = array('general'); // others include 'single', 'eachuser', 'qanda' 00061 00062 public $resource_type_counter = 0; 00063 public $assignment_type_counter = 0; 00064 public $forum_type_counter = 0; 00065 00066 public $settings = array(); 00067 public $eolchar = '<br />'; 00068 public $do_generation = false; 00069 public $starttime; 00070 public $original_db; 00071 00072 public function __construct($settings = array(), $generate=false) { 00073 global $CFG; 00074 00075 $this->starttime = time()+microtime(); 00076 00077 $arguments = array( 00078 array('short'=>'u', 'long'=>'username', 00079 'help' => 'Your moodle username', 'type'=>'STRING', 'default' => ''), 00080 array('short'=>'pw', 'long'=>'password', 00081 'help' => 'Your moodle password', 'type'=>'STRING', 'default' => ''), 00082 array('short'=>'P', 'long' => 'database_prefix', 00083 'help' => 'Database prefix to use: tables must already exist or the script will abort!', 00084 'type'=>'STRING', 'default' => $CFG->prefix), 00085 array('short'=>'c', 'long' => 'pre_cleanup', 'help' => 'Delete previously generated data'), 00086 array('short'=>'C', 'long' => 'post_cleanup', 00087 'help' => 'Deletes all generated data at the end of the script (for benchmarking of generation only)'), 00088 array('short'=>'t', 'long' => 'time_limit', 00089 'help' => 'Optional time limit after which to abort the generation, 0 = no limit. Default=0', 00090 'type'=>'SECONDS', 'default' => 0), 00091 array('short'=>'v', 'long' => 'verbose', 'help' => 'Display extra information about the data generation'), 00092 array('short'=>'q', 'long' => 'quiet', 'help' => 'Inhibits all outputs'), 00093 array('short'=>'i', 'long' => 'ignore_errors', 'help' => 'Continue script execution when errors occur'), 00094 array('short'=>'N', 'long' => 'no_data', 'help' => 'Generate nothing (used for cleaning up only)'), 00095 array('short'=>'T', 'long' => 'tiny', 00096 'help' => 'Generates a tiny data set (1 of each course, module, user and section)', 00097 'default' => 0), 00098 array('short'=>'nc', 'long' => 'number_of_courses', 00099 'help' => 'The number of courses to generate. Default=1', 00100 'type'=>'NUMBER', 'default' => 1), 00101 array('short'=>'ns', 'long' => 'number_of_students', 00102 'help' => 'The number of students to generate. Default=250', 00103 'type'=>'NUMBER', 'default' => 250), 00104 array('short'=>'sc', 'long' => 'students_per_course', 00105 'help' => 'The number of students to enrol in each course. Default=20', 00106 'type'=>'NUMBER', 'default' => 20), 00107 array('short'=>'nsec', 'long' => 'number_of_sections', 00108 'help' => 'The number of sections to generate in each course. Default=10', 00109 'type'=>'NUMBER', 'default' => 10), 00110 array('short'=>'nmod', 'long' => 'number_of_modules', 00111 'help' => 'The number of modules to generate in each section. Default=10', 00112 'type'=>'NUMBER', 'default' => 10), 00113 array('short'=>'mods', 'long' => 'modules_list', 00114 'help' => 'The list of modules you want to generate', 'default' => $this->modules_list, 00115 'type' => 'mod1,mod2...'), 00116 array('short'=>'rt', 'long' => 'resource_type', 00117 'help' => 'The specific type of resource you want to generate. Defaults to all', 00118 'default' => $this->resource_types, 00119 'type' => 'SELECT'), 00120 array('short'=>'at', 'long' => 'assignment_type', 00121 'help' => 'The specific type of assignment you want to generate. Defaults to all', 00122 'default' => $this->assignment_types, 00123 'type' => 'SELECT'), 00124 array('short'=>'ft', 'long' => 'forum_type', 00125 'help' => 'The specific type of forum you want to generate. Defaults to all', 00126 'default' => $this->forum_types, 00127 'type' => 'SELECT'), 00128 array('short'=>'gf', 'long' => 'glossary_format', 00129 'help' => 'The specific format of glossary you want to generate. Defaults to all', 00130 'default' => $this->glossary_formats, 00131 'type' => 'SELECT'), 00132 array('short'=>'ag', 'long' => 'assignment_grades', 00133 'help' => 'Generate random grades for each student/assignment tuple', 'default' => true), 00134 array('short'=>'qg', 'long' => 'quiz_grades', 00135 'help' => 'Generate random grades for each student/quiz tuple', 'default' => true), 00136 array('short'=>'eg', 'long' => 'entries_per_glossary', 00137 'help' => 'The number of definitions to generate per glossary. Default=0', 00138 'type'=>'NUMBER', 'default' => 1), 00139 array('short'=>'nq', 'long' => 'questions_per_course', 00140 'help' => 'The number of questions to generate per course. Default=20', 00141 'type'=>'NUMBER', 'default' => 20), 00142 array('short'=>'qq', 'long' => 'questions_per_quiz', 00143 'help' => 'The number of questions to assign to each quiz. Default=5', 00144 'type'=>'NUMBER', 'default' => 5), 00145 array('short'=>'df', 'long' => 'discussions_per_forum', 00146 'help' => 'The number of discussions to generate for each forum. Default=5', 00147 'type'=>'NUMBER', 'default' => 5), 00148 array('short'=>'pd', 'long' => 'posts_per_discussion', 00149 'help' => 'The number of posts to generate for each forum discussion. Default=15', 00150 'type'=>'NUMBER', 'default' => 15), 00151 array('short'=>'fd', 'long' => 'fields_per_database', 00152 'help' => 'The number of fields to generate for each database. Default=4', 00153 'type'=>'NUMBER', 'default' => 4), 00154 array('short'=>'drs', 'long' => 'database_records_per_student', 00155 'help' => 'The number of records to generate for each student/database tuple. Default=1', 00156 'type'=>'NUMBER', 'default' => 1), 00157 array('short'=>'mc', 'long' => 'messages_per_chat', 00158 'help' => 'The number of messages to generate for each chat module. Default=10', 00159 'type'=>'NUMBER', 'default' => 10), 00160 ); 00161 00162 foreach ($arguments as $args_array) { 00163 $this->settings[$args_array['long']] = new generator_argument($args_array); 00164 } 00165 00166 foreach ($settings as $setting => $value) { 00167 $this->settings[$setting]->value = $value; 00168 } 00169 00170 if ($generate) { 00171 $this->generate_data(); 00172 } 00173 } 00174 00175 public function connect() { 00176 global $DB, $CFG; 00177 $this->original_db = $DB; 00178 00179 $class = get_class($DB); 00180 $DB = new $class(); 00181 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $this->get('database_prefix')); 00182 } 00183 00184 public function dispose() { 00185 global $DB; 00186 $DB->dispose(); 00187 $DB = $this->original_db; 00188 } 00189 00190 public function generate_users() { 00191 global $DB, $CFG; 00192 00196 $this->verbose("Generating ".$this->get('number_of_students')." students..."); 00197 $lastnames = array('SMITH','JOHNSON','WILLIAMS','JONES','BROWN','DAVIS','MILLER','WILSON', 00198 'MOORE','TAYLOR','ANDERSON','THOMAS','JACKSON','WHITE','HARRIS','MARTIN','THOMPSON', 00199 'GARCIA','MARTINEZ','ROBINSON','CLARK','RODRIGUEZ','LEWIS','LEE','WALKER','HALL', 00200 'ALLEN','YOUNG','HERNANDEZ','KING','WRIGHT','LOPEZ','HILL','SCOTT','GREEN','ADAMS', 00201 'BAKER','GONZALEZ','NELSON','CARTER','MITCHELL','PEREZ','ROBERTS','TURNER','PHILLIPS', 00202 'CAMPBELL','PARKER','EVANS','EDWARDS','COLLINS','STEWART','SANCHEZ','MORRIS','ROGERS', 00203 'REED','COOK','MORGAN','BELL','MURPHY','BAILEY','RIVERA','COOPER','RICHARDSON','COX', 00204 'HOWARD','WARD','TORRES','PETERSON','GRAY','RAMIREZ','JAMES','WATSON','BROOKS','KELLY', 00205 'SANDERS','PRICE','BENNETT','WOOD','BARNES','ROSS','HENDERSON','COLEMAN','JENKINS','PERRY', 00206 'POWELL','LONG','PATTERSON','HUGHES','FLORES','WASHINGTON','BUTLER','SIMMONS','FOSTER', 00207 'GONZALES','BRYANT','ALEXANDER','RUSSELL','GRIFFIN','DIAZ','HAYES','MYERS','FORD','HAMILTON', 00208 'GRAHAM','SULLIVAN','WALLACE','WOODS','COLE','WEST','JORDAN','OWENS','REYNOLDS','FISHER', 00209 'ELLIS','HARRISON','GIBSON','MCDONALD','CRUZ','MARSHALL','ORTIZ','GOMEZ','MURRAY','FREEMAN', 00210 'WELLS','WEBB','SIMPSON','STEVENS','TUCKER','PORTER','HUNTER','HICKS','CRAWFORD','HENRY', 00211 'BOYD','MASON','MORALES','KENNEDY','WARREN','DIXON','RAMOS','REYES','BURNS','GORDON','SHAW', 00212 'HOLMES','RICE','ROBERTSON','HUNT','BLACK','DANIELS','PALMER','MILLS','NICHOLS','GRANT', 00213 'KNIGHT','FERGUSON','ROSE','STONE','HAWKINS','DUNN','PERKINS','HUDSON','SPENCER','GARDNER', 00214 'STEPHENS','PAYNE','PIERCE','BERRY','MATTHEWS','ARNOLD','WAGNER','WILLIS','RAY','WATKINS', 00215 'OLSON','CARROLL','DUNCAN','SNYDER','HART','CUNNINGHAM','BRADLEY','LANE','ANDREWS','RUIZ', 00216 'HARPER','FOX','RILEY','ARMSTRONG','CARPENTER','WEAVER','GREENE','LAWRENCE','ELLIOTT','CHAVEZ', 00217 'SIMS','AUSTIN','PETERS','KELLEY','FRANKLIN','LAWSON','FIELDS','GUTIERREZ','RYAN','SCHMIDT', 00218 'CARR','VASQUEZ','CASTILLO','WHEELER','CHAPMAN','OLIVER','MONTGOMERY','RICHARDS','WILLIAMSON', 00219 'JOHNSTON','BANKS','MEYER','BISHOP','MCCOY','HOWELL','ALVAREZ','MORRISON','HANSEN','FERNANDEZ', 00220 'GARZA','HARVEY','LITTLE','BURTON','STANLEY','NGUYEN','GEORGE','JACOBS','REID','KIM','FULLER', 00221 'LYNCH','DEAN','GILBERT','GARRETT','ROMERO','WELCH','LARSON','FRAZIER','BURKE','HANSON','DAY', 00222 'MENDOZA','MORENO','BOWMAN','MEDINA','FOWLER'); 00223 $firstnames = array( 'JAMES','JOHN','ROBERT','MARY','MICHAEL','WILLIAM','DAVID','RICHARD', 00224 'CHARLES','JOSEPH','THOMAS','PATRICIA','LINDA','CHRISTOPHER','BARBARA','DANIEL','PAUL', 00225 'MARK','ELIZABETH','JENNIFER','DONALD','GEORGE','MARIA','KENNETH','SUSAN','STEVEN','EDWARD', 00226 'MARGARET','BRIAN','DOROTHY','RONALD','ANTHONY','LISA','KEVIN','NANCY','KAREN','BETTY', 00227 'HELEN','JASON','MATTHEW','GARY','TIMOTHY','SANDRA','JOSE','LARRY','JEFFREY','DONNA', 00228 'FRANK','CAROL','RUTH','SCOTT','ERIC','STEPHEN','ANDREW','SHARON','MICHELLE','LAURA', 00229 'SARAH','KIMBERLY','DEBORAH','JESSICA','RAYMOND','SHIRLEY','CYNTHIA','ANGELA','MELISSA', 00230 'BRENDA','AMY','GREGORY','ANNA','JOSHUA','JERRY','REBECCA','VIRGINIA','KATHLEEN','PAMELA', 00231 'DENNIS','MARTHA','DEBRA','AMANDA','STEPHANIE','WALTER','PATRICK','CAROLYN','CHRISTINE', 00232 'PETER','MARIE','JANET','CATHERINE','HAROLD','FRANCES','DOUGLAS','HENRY','ANN','JOYCE', 00233 'DIANE','ALICE','JULIE','CARL','HEATHER'); 00234 $users_count = 0; 00235 $users = array(); 00236 00237 shuffle($lastnames); 00238 shuffle($firstnames); 00239 00240 $next_user_id = $DB->get_field_sql("SELECT MAX(id) FROM {user}") + 1; 00241 00242 for ($i = 0; $i < $this->get('number_of_students'); $i++) { 00243 00244 $lastname = trim(ucfirst(strtolower($lastnames[rand(0, count($lastnames) - 1)]))); 00245 $firstname = $firstnames[rand(0, count($firstnames) - 1)]; 00246 00247 $user = new stdClass(); 00248 $user->firstname = trim(ucfirst(strtolower($firstname))); 00249 $user->username = strtolower(substr($firstname, 0, 7) . substr($lastname, 0, 7)) . $next_user_id++; 00250 $user->lastname = $lastname; 00251 $user->email = $user->username . '@example.com'; 00252 $user->mnethostid = 1; 00253 $user->city = 'Test City'; 00254 $user->country = 'AU'; 00255 $user->password = md5('password'); 00256 $user->auth = 'manual'; 00257 $user->confirmed = 1; 00258 $user->lang = $CFG->lang; 00259 $user->timemodified= time(); 00260 00261 $user->id = $DB->insert_record("user", $user); 00262 $users_count++; 00263 $users[] = $user->id; 00264 $next_user_id = $user->id + 1; 00265 $this->verbose("Inserted $user->firstname $user->lastname into DB " 00266 ."(username=$user->username, password=password)."); 00267 } 00268 00269 if (!$this->get('quiet')) { 00270 echo "$users_count users correctly inserted in the database.{$this->eolchar}"; 00271 } 00272 return $users; 00273 } 00274 00275 public function generate_data() { 00276 if (!$this->do_generation) { 00277 return false; 00278 } 00279 00280 set_time_limit($this->get('time_limit')); 00281 00282 // Process tiny data set 00283 $tiny = $this->get('tiny'); 00284 if (!empty($tiny)) { 00285 $this->verbose("Generating a tiny data set: 1 student in 1 course with 1 module in 1 section..."); 00286 $this->set('number_of_courses',1); 00287 $this->set('number_of_students',1); 00288 $this->set('number_of_modules',1); 00289 $this->set('number_of_sections',1); 00290 $this->set('assignment_grades',false); 00291 $this->set('quiz_grades',false); 00292 $this->set('students_per_course',1); 00293 $this->set('questions_per_course',1); 00294 $this->set('questions_per_quiz',1); 00295 } 00296 00297 if ($this->get('pre_cleanup')) { 00298 $this->verbose("Deleting previous test data..."); 00299 $this->data_cleanup(); 00300 00301 if (!$this->get('quiet')) { 00302 echo "Previous test data has been deleted.{$this->eolchar}"; 00303 } 00304 } 00305 00306 00307 if (!$this->get('no_data')) { 00308 $users = $this->generate_users(); 00309 $courses = $this->generate_courses(); 00310 $modules = $this->generate_modules($courses); 00311 $questions = $this->generate_questions($courses, $modules); 00312 $course_users = $this->generate_role_assignments($users, $courses); 00313 $this->generate_forum_posts($course_users, $modules); 00314 $this->generate_grades($course_users, $courses, $modules); 00315 $this->generate_module_content($course_users, $courses, $modules); 00316 } 00317 00318 if ($this->get('post_cleanup')) { 00319 if (!$this->get('quiet')) { 00320 echo "Removing generated data..." . $this->eolchar; 00321 } 00322 $this->data_cleanup(); 00323 if (!$this->get('quiet')) { 00324 echo "Generated data has been deleted." . $this->eolchar; 00325 } 00326 } 00327 00331 $stoptimer = time()+microtime(); 00332 $timer = round($stoptimer-$this->starttime,4); 00333 if (!$this->get('quiet')) { 00334 echo "End of script! ($timer seconds taken){$this->eolchar}"; 00335 } 00336 00337 } 00338 00339 public function generate_courses() { 00340 global $DB; 00341 00342 $this->verbose("Generating " . $this->get('number_of_courses')." courses..."); 00343 $base_course = new stdClass(); 00344 $next_course_id = $DB->get_field_sql("SELECT MAX(id) FROM {course}") + 1; 00345 00346 $base_course->MAX_FILE_SIZE = '2097152'; 00347 $base_course->category = '1'; 00348 $base_course->summary = 'Blah Blah'; 00349 $base_course->format = 'weeks'; 00350 $base_course->numsections = '10'; 00351 $base_course->startdate = mktime(); 00352 $base_course->id = '0'; 00353 00354 $courses_count = 0; 00355 $courses = array(); 00356 for ($i = 1; $i <= $this->get('number_of_courses'); $i++) { 00357 $newcourse = fullclone($base_course); 00358 $newcourse->fullname = "Test course $next_course_id"; 00359 $newcourse->shortname = "Test $next_course_id"; 00360 $newcourse->idnumber = $next_course_id; 00361 if (!$course = create_course($newcourse)) { 00362 $this->verbose("Error inserting a new course in the database!"); 00363 if (!$this->get('ignore_errors')) { 00364 die(); 00365 } 00366 } else { 00367 $courses_count++; 00368 $next_course_id++; 00369 $courses[] = $course->id; 00370 $next_course_id = $course->id + 1; 00371 $this->verbose("Inserted $course->fullname into DB (idnumber=$course->idnumber)."); 00372 } 00373 } 00374 00375 if (!$this->get('quiet')) { 00376 echo "$courses_count test courses correctly inserted into the database.{$this->eolchar}"; 00377 } 00378 return $courses; 00379 } 00380 00381 public function generate_modules($courses) { 00382 global $DB, $CFG; 00383 // Parse the modules-list variable 00384 00385 $this->verbose("Generating " . $this->get('number_of_sections')." sections with " 00386 .$this->get('number_of_modules')." modules in each section, for each course..."); 00387 00388 list($modules_list_sql, $modules_params) = 00389 $DB->get_in_or_equal($this->get('modules_list'), SQL_PARAMS_NAMED, 'mod', true); 00390 00391 list($modules_ignored_sql, $ignore_params) = 00392 $DB->get_in_or_equal($this->modules_to_ignore, SQL_PARAMS_NAMED, 'ignore', false); 00393 00394 $wheresql = "name $modules_list_sql AND name $modules_ignored_sql"; 00395 $modules = $DB->get_records_select('modules', $wheresql, array_merge($modules_params, $ignore_params)); 00396 00397 foreach ($modules as $key => $module) { 00398 $module->count = 0; 00399 00400 // Scorm, lams and hotpot are too complex to set up, remove them 00401 if (in_array($module->name, $this->modules_to_ignore) || 00402 !in_array($module->name, $this->modules_list)) { 00403 unset($modules[$key]); 00404 } 00405 } 00406 00407 // Dirty hack for renumbering the modules array's keys 00408 $first_module = reset($modules); 00409 array_shift($modules); 00410 array_unshift($modules, $first_module); 00411 00412 $modules_array = array(); 00413 00414 if (count($courses) > 0) { 00415 $libraries = array(); 00416 foreach ($courses as $courseid) { 00417 00418 // Text resources 00419 for ($i = 1; $i <= $this->get('number_of_sections'); $i++) { 00420 for ($j = 0; $j < $this->get('number_of_modules'); $j++) { 00421 00422 $module = new stdClass(); 00423 00424 // If only one module is created, and we also need to add a question to a quiz, create only a quiz 00425 if ($this->get('number_of_modules') == 1 00426 && $this->get('questions_per_quiz') > 0 00427 && !empty($modules[8])) { 00428 $moduledata = $modules[8]; 00429 } else { 00430 $moduledata = $modules[array_rand($modules)]; 00431 } 00432 00433 $libfile = "$CFG->dirroot/mod/$moduledata->name/lib.php"; 00434 if (file_exists($libfile)) { 00435 if (!in_array($libfile, $libraries)) { 00436 $this->verbose("Including library for $moduledata->name..."); 00437 $libraries[] = $libfile; 00438 require_once($libfile); 00439 } 00440 } else { 00441 $this->verbose("Could not load lib file for module $moduledata->name!"); 00442 if (!$this->get('ignore_errors')) { 00443 die(); 00444 } 00445 } 00446 00447 // Basically 2 types of text fields: description and content 00448 $description = "This $moduledata->name has been randomly generated by a very useful script, " 00449 . "for the purpose of testing " 00450 . "the boundaries of Moodle in various contexts. Moodle should be able to scale to " 00451 . "any size without " 00452 . "its speed and ease of use being affected dramatically."; 00453 $content = 'Very useful content, I am sure you would agree'; 00454 00455 $module_type_index = 0; 00456 $module->introformat = FORMAT_MOODLE; 00457 $module->messageformat = FORMAT_MOODLE; 00458 00459 // Special module-specific config 00460 switch ($moduledata->name) { 00461 case 'assignment': 00462 $module->intro = $description; 00463 $module->assignmenttype = $this->get_module_type('assignment'); 00464 $module->timedue = mktime() + 89487321; 00465 $module->grade = rand(50,100); 00466 break; 00467 case 'chat': 00468 $module->intro = $description; 00469 $module->schedule = 1; 00470 $module->chattime = 60 * 60 * 4; 00471 break; 00472 case 'data': 00473 $module->intro = $description; 00474 $module->name = 'test'; 00475 break; 00476 case 'choice': 00477 $module->intro = $description; 00478 $module->text = $content; 00479 $module->option = array('Good choice', 'Bad choice', 'No choice'); 00480 $module->limit = array(1, 5, 0); 00481 break; 00482 case 'comments': 00483 $module->intro = $description; 00484 $module->comments = $content; 00485 break; 00486 case 'feedback': 00487 $module->intro = $description; 00488 $module->page_after_submit = $description; 00489 $module->comments = $content; 00490 break; 00491 case 'forum': 00492 $module->intro = $description; 00493 $module->type = $this->get_module_type('forum'); 00494 $module->forcesubscribe = rand(0, 1); 00495 $module->format = 1; 00496 break; 00497 case 'glossary': 00498 $module->intro = $description; 00499 $module->displayformat = $this->glossary_formats[rand(0, count($this->glossary_formats) - 1)]; 00500 $module->cmidnumber = rand(0,999999); 00501 break; 00502 case 'label': 00503 $module->content = $content; 00504 $module->intro = $description; 00505 break; 00506 case 'lesson': 00507 $module->lessondefault = 1; 00508 $module->available = mktime(); 00509 $module->deadline = mktime() + 719891987; 00510 $module->grade = 100; 00511 break; 00512 case 'quiz': 00513 $module->intro = $description; 00514 $module->feedbacktext = 'blah'; 00515 $module->feedback = 1; 00516 $module->feedbackboundaries = array(2, 1); 00517 $module->grade = 10; 00518 $module->timeopen = time(); 00519 $module->timeclose = time() + 68854; 00520 $module->shufflequestions = true; 00521 $module->shuffleanswers = true; 00522 $module->quizpassword = ''; 00523 break; 00524 case 'resource': 00525 $module->type = $this->get_module_type('resource'); 00526 $module->alltext = $content; 00527 $module->summary = $description; 00528 $module->windowpopup = rand(0,1); 00529 $module->display = rand(0,1); 00530 $module->resizable = rand(0,1); 00531 $module->scrollbars = rand(0,1); 00532 $module->directories = rand(0,1); 00533 $module->location = 'file.txt'; 00534 $module->menubar = rand(0,1); 00535 $module->toolbar = rand(0,1); 00536 $module->status = rand(0,1); 00537 $module->width = rand(200,600); 00538 $module->height = rand(200,600); 00539 $module->directories = rand(0,1); 00540 $module->files = false; 00541 $module->param_navigationmenu = rand(0,1); 00542 $module->param_navigationbuttons = rand(0,1); 00543 $module->reference = 1; 00544 $module->forcedownload = 1; 00545 break; 00546 case 'survey': 00547 $module->template = rand(1,5); 00548 $module->intro = $description; 00549 break; 00550 case 'wiki': 00551 $module->intro = $description; 00552 $module->summary = $description; 00553 break; 00554 } 00555 00556 $module->name = ucfirst($moduledata->name) . ' ' . $moduledata->count++; 00557 00558 $module->course = $courseid; 00559 $module->section = $i; 00560 $module->module = $moduledata->id; 00561 $module->modulename = $moduledata->name; 00562 $module->add = $moduledata->name; 00563 $module->cmidnumber = ''; 00564 $module->coursemodule = ''; 00565 $add_instance_function = $moduledata->name . '_add_instance'; 00566 00567 $section = get_course_section($i, $courseid); 00568 $module->section = $section->id; 00569 $module->coursemodule = add_course_module($module); 00570 $module->section = $i; 00571 00572 if (function_exists($add_instance_function)) { 00573 $this->verbose("Calling module function $add_instance_function"); 00574 $module->instance = $add_instance_function($module, ''); 00575 $DB->set_field('course_modules', 'instance', $module->instance, array('id'=>$module->coursemodule)); 00576 } else { 00577 $this->verbose("Function $add_instance_function does not exist!"); 00578 if (!$this->get('ignore_errors')) { 00579 die(); 00580 } 00581 } 00582 00583 add_mod_to_section($module); 00584 00585 $module->cmidnumber = set_coursemodule_idnumber($module->coursemodule, ''); 00586 00587 $this->verbose("A $moduledata->name module was added to section $i (id $module->section) " 00588 ."of course $courseid."); 00589 rebuild_course_cache($courseid); 00590 00591 $module_instance = $DB->get_field('course_modules', 'instance', array('id' => $module->coursemodule)); 00592 $module_record = $DB->get_record($moduledata->name, array('id' => $module_instance)); 00593 $module_record->instance = $module_instance; 00594 00595 if (empty($modules_array[$moduledata->name])) { 00596 $modules_array[$moduledata->name] = array(); 00597 } 00598 00599 // TODO Find out why some $module_record end up empty here... (particularly quizzes) 00600 if (!empty($module_record->instance)) { 00601 $modules_array[$moduledata->name][] = $module_record; 00602 } 00603 } 00604 } 00605 } 00606 00607 if (!$this->get('quiet')) { 00608 echo "Successfully generated " . $this->get('number_of_modules') * $this->get('number_of_sections') 00609 . " modules in each course!{$this->eolchar}"; 00610 } 00611 00612 return $modules_array; 00613 } 00614 return null; 00615 } 00616 00617 public function generate_questions($courses, $modules) { 00618 global $DB, $CFG; 00619 00620 if (!is_null($this->get('questions_per_course')) && count($courses) > 0 && is_array($courses)) { 00621 require_once($CFG->libdir .'/questionlib.php'); 00622 require_once($CFG->dirroot .'/mod/quiz/editlib.php'); 00623 $questions = array(); 00624 $questionsmenu = question_bank::get_creatable_qtypes(); 00625 $questiontypes = array(); 00626 foreach ($questionsmenu as $qtype => $qname) { 00627 $questiontypes[] = $qtype; 00628 } 00629 00630 // Add the questions 00631 foreach ($courses as $courseid) { 00632 $questions[$courseid] = array(); 00633 for ($i = 0; $i < $this->get('questions_per_course'); $i++) { 00634 $qtype = $questiontypes[array_rand($questiontypes)]; 00635 00636 // Only the following types are supported right now. Hang around for more! 00637 $supported_types = array('match', 'essay', 'multianswer', 'multichoice', 'shortanswer', 00638 'numerical', 'truefalse', 'calculated'); 00639 $qtype = $supported_types[array_rand($supported_types)]; 00640 00641 if ($qtype == 'calculated') { 00642 continue; 00643 } 00644 $classname = "question_{$qtype}_qtype"; 00645 if ($qtype == 'multianswer') { 00646 $classname = "embedded_cloze_qtype"; 00647 } 00648 00649 $question = new $classname(); 00650 $question->qtype = $qtype; 00651 $questions[$courseid][] = $question->generate_test("question$qtype-$i", $courseid); 00652 $this->verbose("Generated a question of type $qtype for course id $courseid."); 00653 } 00654 } 00655 00656 // Assign questions to quizzes, if such exist 00657 if (!empty($modules['quiz']) && !empty($questions) && !is_null($this->get('questions_per_quiz'))) { 00658 $quizzes = $modules['quiz']; 00659 00660 // Cannot assign more questions per quiz than are available, so determine which is the largest 00661 $questions_per_quiz = max(count($questions), $this->get('questions_per_quiz')); 00662 00663 foreach ($quizzes as $quiz) { 00664 $questions_added = array(); 00665 for ($i = 0; $i < $questions_per_quiz; $i++) { 00666 00667 // Add a random question to the quiz 00668 do { 00669 if (empty($quiz->course)) { 00670 print_object($quizzes);die(); 00671 } 00672 $random = rand(0, count($questions[$quiz->course])); 00673 } while (in_array($random, $questions_added) || !array_key_exists($random, $questions[$quiz->course])); 00674 00675 if (!quiz_add_quiz_question($questions[$quiz->course][$random]->id, $quiz)) { 00676 00677 // Could not add question to quiz!! report error 00678 if (!$this->get('quiet')) { 00679 echo "WARNING: Could not add question id $random to quiz id $quiz->id{$this->eolchar}"; 00680 } 00681 } else { 00682 $this->verbose("Adding question id $random to quiz id $quiz->id."); 00683 $questions_added[] = $random; 00684 } 00685 } 00686 } 00687 } 00688 return $questions; 00689 } 00690 return null; 00691 } 00692 00693 public function generate_role_assignments($users, $courses) { 00694 global $CFG, $DB; 00695 $course_users = array(); 00696 00697 if (count($courses) > 0) { 00698 $this->verbose("Inserting student->course role assignments..."); 00699 $assigned_count = 0; 00700 $assigned_users = array(); 00701 00702 foreach ($courses as $courseid) { 00703 $course_users[$courseid] = array(); 00704 00705 // Select $students_per_course for assignment to course 00706 shuffle($users); 00707 $users_to_assign = array_slice($users, 0, $this->get('students_per_course')); 00708 00709 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00710 foreach ($users_to_assign as $random_user) { 00711 role_assign(5, $random_user, $context->id); 00712 00713 $assigned_count++; 00714 $course_users[$courseid][] = $random_user; 00715 if (!isset($assigned_users[$random_user])) { 00716 $assigned_users[$random_user] = 1; 00717 } else { 00718 $assigned_users[$random_user]++; 00719 } 00720 $this->verbose("Student $random_user was assigned to course $courseid."); 00721 } 00722 } 00723 00724 if (!$this->get('quiet')) { 00725 echo "$assigned_count user => course role assignments have been correctly performed.{$this->eolchar}"; 00726 } 00727 return $course_users; 00728 } 00729 return null; 00730 } 00731 00732 public function generate_forum_posts($course_users, $modules) { 00733 global $CFG, $DB, $USER; 00734 00735 if (in_array('forum', $this->modules_list) && 00736 $this->get('discussions_per_forum') && 00737 $this->get('posts_per_discussion') && 00738 isset($modules['forum'])) { 00739 00740 $discussions_count = 0; 00741 $posts_count = 0; 00742 00743 foreach ($modules['forum'] as $forum) { 00744 $forum_users = $course_users[$forum->course]; 00745 00746 for ($i = 0; $i < $this->get('discussions_per_forum'); $i++) { 00747 $mform = new fake_form(); 00748 00749 require_once($CFG->dirroot.'/mod/forum/lib.php'); 00750 00751 $discussion = new stdClass(); 00752 $discussion->course = $forum->course; 00753 $discussion->forum = $forum->id; 00754 $discussion->name = 'Test discussion'; 00755 $discussion->intro = 'This is just a test forum discussion'; 00756 $discussion->assessed = 0; 00757 $discussion->messageformat = 1; 00758 $discussion->messagetrust = 0; 00759 $discussion->mailnow = false; 00760 $discussion->groupid = -1; 00761 $discussion->attachments = null; 00762 $discussion->itemid = 752157083; 00763 00764 $message = ''; 00765 $super_global_user = clone($USER); 00766 $user_id = $forum_users[array_rand($forum_users)]; 00767 $USER = $DB->get_record('user', array('id' => $user_id)); 00768 00769 if ($discussion_id = forum_add_discussion($discussion, $mform, $message)) { 00770 $discussion = $DB->get_record('forum_discussions', array('id' => $discussion_id)); 00771 $discussions_count++; 00772 00773 // Add posts to this discussion 00774 $post_ids = array($discussion->firstpost); 00775 00776 for ($j = 0; $j < $this->get('posts_per_discussion'); $j++) { 00777 $global_user = clone($USER); 00778 $user_id = $forum_users[array_rand($forum_users)]; 00779 $USER = $DB->get_record('user', array('id' => $user_id)); 00780 $post = new stdClass(); 00781 $post->discussion = $discussion_id; 00782 $post->subject = 'Re: test discussion'; 00783 $post->message = '<p>Nothing much to say, since this is just a test...</p>'; 00784 $post->format = 1; 00785 $post->attachments = null; 00786 $post->itemid = 752157083; 00787 $post->parent = $post_ids[array_rand($post_ids)]; 00788 00789 if ($post_ids[] = forum_add_new_post($post, $mform, $message)) { 00790 $posts_count++; 00791 } 00792 $USER = $global_user; 00793 } 00794 } 00795 00796 $USER = $super_global_user; 00797 00798 if ($forum->type == 'single') { 00799 break; 00800 } 00801 } 00802 } 00803 if ($discussions_count > 0 && !$this->get('quiet')) { 00804 echo "$discussions_count forum discussions have been generated.{$this->eolchar}"; 00805 } 00806 if ($posts_count > 0 && !$this->get('quiet')) { 00807 echo "$posts_count forum posts have been generated.{$this->eolchar}"; 00808 } 00809 00810 return true; 00811 } 00812 return null; 00813 00814 } 00815 00816 public function generate_grades($course_users, $courses, $modules) { 00817 global $CFG, $DB, $USER; 00818 00822 if ($this->get('assignment_grades') && isset($modules['assignment'])) { 00823 $grades_count = 0; 00824 foreach ($course_users as $courseid => $userid_array) { 00825 foreach ($userid_array as $userid) { 00826 foreach ($modules['assignment'] as $assignment) { 00827 if (in_array($assignment->course, $courses)) { 00828 $maxgrade = $assignment->grade; 00829 $random_grade = rand(0, $maxgrade); 00830 $grade = new stdClass(); 00831 $grade->assignment = $assignment->id; 00832 $grade->userid = $userid; 00833 $grade->grade = $random_grade; 00834 $grade->rawgrade = $random_grade; 00835 $grade->teacher = $USER->id; 00836 $grade->submissioncomment = 'comment'; 00837 $DB->insert_record('assignment_submissions', $grade); 00838 grade_update('mod/assignment', $assignment->course, 'mod', 'assignment', $assignment->id, 0, $grade); 00839 $this->verbose("A grade ($random_grade) has been given to user $userid " 00840 . "for assignment $assignment->id"); 00841 $grades_count++; 00842 } 00843 } 00844 } 00845 } 00846 if ($grades_count > 0) { 00847 $this->verbose("$grades_count assignment grades have been generated.{$this->eolchar}"); 00848 } 00849 } 00850 00854 if ($this->get('quiz_grades') && isset($modules['quiz'])) { 00855 $grades_count = 0; 00856 foreach ($course_users as $userid => $courses) { 00857 foreach ($modules['quiz'] as $quiz) { 00858 if (in_array($quiz->course, $courses)) { 00859 $maxgrade = $quiz->grade; 00860 $random_grade = rand(0, $maxgrade); 00861 $grade = new stdClass(); 00862 $grade->quiz = $quiz->id; 00863 $grade->userid = $userid; 00864 $grade->grade = $random_grade; 00865 $grade->rawgrade = $random_grade; 00866 $DB->insert_record('quiz_grades', $grade); 00867 grade_update('mod/quiz', $courseid, 'mod', 'quiz', $quiz->id, 0, $grade); 00868 $this->verbose("A grade ($random_grade) has been given to user $userid for quiz $quiz->id"); 00869 $grades_count++; 00870 } 00871 } 00872 } 00873 if ($grades_count > 0 && !$this->get('quiet')) { 00874 echo "$grades_count quiz grades have been generated.{$this->eolchar}"; 00875 } 00876 } 00877 return null; 00878 } 00879 00880 public function generate_module_content($course_users, $courses, $modules) { 00881 global $USER, $DB, $CFG; 00882 $result = null; 00883 00884 $entries_count = 0; 00885 if ($this->get('entries_per_glossary') && !empty($modules['glossary'])) { 00886 foreach ($modules['glossary'] as $glossary) { 00887 for ($i = 0; $i < $this->get('entries_per_glossary'); $i++) { 00888 $entry = new stdClass(); 00889 $entry->glossaryid = $glossary->id; 00890 $entry->userid = $USER->id; 00891 $entry->concept = "Test concept"; 00892 $entry->definition = "A test concept is nothing to write home about: just a test concept."; 00893 $entry->format = 1; 00894 $entry->timecreated = time(); 00895 $entry->timemodified = time(); 00896 $entry->teacherentry = 0; 00897 $entry->approved = 1; 00898 $DB->insert_record('glossary_entries', $entry); 00899 $entries_count++; 00900 } 00901 } 00902 if ($entries_count > 0 && !$this->get('quiet')) { 00903 echo "$entries_count glossary definitions have been generated.{$this->eolchar}"; 00904 } 00905 $result = true; 00906 } 00907 00908 $fields_count = 0; 00909 if (!empty($modules['data']) && $this->get('fields_per_database') && $this->get('database_records_per_student')) { 00910 $database_field_types = array('checkbox', 00911 'date', 00912 'file', 00913 'latlong', 00914 'menu', 00915 'multimenu', 00916 'number', 00917 'picture', 00918 'radiobutton', 00919 'text', 00920 'textarea', 00921 'url'); 00922 00923 00924 $fields = array(); 00925 00926 foreach ($modules['data'] as $data) { 00927 00928 for ($i = 0; $i < $this->get('fields_per_database'); $i++) { 00929 $type = $database_field_types[array_rand($database_field_types)]; 00930 require_once($CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php'); 00931 $newfield = 'data_field_'.$type; 00932 $cm = get_coursemodule_from_instance('data', $data->id); 00933 $newfield = new $newfield(0, $data, $cm); 00934 $fields[$data->id][] = $newfield; 00935 $newfield->insert_field(); 00936 } 00937 00938 // Generate fields for each database (same fields for all, no arguing) 00939 for ($i = 0; $i < $this->get('fields_per_database'); $i++) { 00940 00941 } 00942 00943 // Generate database records for each student, if needed 00944 for ($i = 0; $i < $this->get('database_records_per_student'); $i++) { 00945 00946 } 00947 } 00948 if ($fields_count > 0 && !$this->get('quiet')) { 00949 $datacount = count($modules['data']); 00950 echo "$fields_count database fields have been generated for each of the " 00951 . "$datacount generated databases.{$this->eolchar}"; 00952 } 00953 $result = true; 00954 } 00955 00956 $messages_count = 0; 00957 if (!empty($modules['chat']) && $this->get('messages_per_chat')) { 00958 00959 // Insert all users into chat_users table, then a message from each user 00960 foreach ($modules['chat'] as $chat) { 00961 00962 foreach ($course_users as $courseid => $users_array) { 00963 00964 foreach ($users_array as $userid) { 00965 if ($messages_count < $this->get('messages_per_chat')) { 00966 $chat_user = new stdClass(); 00967 $chat_user->chatid = $chat->id; 00968 $chat_user->userid = $userid; 00969 $chat_user->course = $courseid; 00970 $DB->insert_record('chat_users', $chat_user); 00971 00972 $chat_message = new stdClass(); 00973 $chat_message->chatid = $chat->id; 00974 $chat_message->userid = $userid; 00975 $chat_message->message = "Hi, everyone!"; 00976 $DB->insert_record('chat_messages', $chat_message); 00977 00978 $messages_count++; 00979 } 00980 } 00981 } 00982 } 00983 00984 if ($messages_count > 0 && !$this->get('quiet')) { 00985 $datacount = count($modules['chat']); 00986 echo "$messages_count messages have been generated for each of the " 00987 . "$datacount generated chats.{$this->eolchar}"; 00988 } 00989 $result = true; 00990 } 00991 00992 return $result; 00993 } 00994 00995 01000 public function verbose($string) { 01001 if ($this->get('verbose') && !$this->get('quiet')) { 01002 echo $string . $this->eolchar; 01003 } 01004 } 01005 01006 01011 function data_cleanup() { 01012 global $DB; 01013 01014 if ($this->get('quiet')) { 01015 ob_start(); 01016 } 01017 01018 // TODO Cleanup code 01019 01020 if ($this->get('quiet')) { 01021 ob_end_clean(); 01022 } 01023 } 01024 01025 public function get($setting) { 01026 if (isset($this->settings[$setting])) { 01027 return $this->settings[$setting]->value; 01028 } else { 01029 return null; 01030 } 01031 } 01032 01033 public function set($setting, $value) { 01034 if (isset($this->settings[$setting])) { 01035 $this->settings[$setting]->value = $value; 01036 } else { 01037 return false; 01038 } 01039 } 01040 01041 public function get_module_type($modulename) { 01042 $return_val = false; 01043 01044 $type = $this->get($modulename.'_type'); 01045 01046 if (is_object($type) && isset($type->type) && isset($type->options)) { 01047 01048 if ($type->type == GENERATOR_RANDOM) { 01049 $return_val = $type->options[array_rand($type->options)]; 01050 01051 } elseif ($type->type == GENERATOR_SEQUENCE) { 01052 $return_val = $type->options[$this->{$modulename.'_type_counter'}]; 01053 $this->{$modulename.'_type_counter'}++; 01054 01055 if ($this->{$modulename.'_type_counter'} == count($type->options)) { 01056 $this->{$modulename.'_type_counter'} = 0; 01057 } 01058 } 01059 01060 } elseif (is_array($type)) { 01061 $return_val = $type[array_rand($type)]; 01062 01063 } elseif (is_string($type)) { 01064 $return_val = $type; 01065 } 01066 01067 return $return_val; 01068 } 01069 } 01070 01071 class generator_argument { 01072 public $short; 01073 public $long; 01074 public $help; 01075 public $type; 01076 public $default = null; 01077 public $value; 01078 01079 public function __construct($params) { 01080 foreach ($params as $key => $val) { 01081 $this->$key = $val; 01082 } 01083 $this->value = $this->default; 01084 } 01085 } 01086 01087 class generator_cli extends generator { 01088 public $eolchar = "\n"; 01089 01090 public function __construct($settings, $argc) { 01091 parent::__construct(); 01092 01093 // Building the USAGE output of the command line version 01094 $help = "Moodle Data Generator. Generates Data for Moodle sites. Good for benchmarking and other tests.\n\n" 01095 . "FOR DEVELOPMENT PURPOSES ONLY! DO NOT USE ON A PRODUCTION SITE!\n\n" 01096 . "Note: By default the script attempts to fill DB tables prefixed with tst_\n" 01097 . "To override the prefix, use the -P (--database_prefix) setting.\n\n" 01098 . "Usage: {$settings[0]}; [OPTION] ...\n" 01099 . "Options:\n" 01100 . " -h, -?, -help, --help This output\n"; 01101 01102 foreach ($this->settings as $argument) { 01103 $equal = ''; 01104 if (!empty($argument->type)) { 01105 $equal = "={$argument->type}"; 01106 } 01107 01108 $padding1 = 5 - strlen($argument->short); 01109 $padding2 = 30 - (strlen($argument->long) + strlen($equal)); 01110 $paddingstr1 = ''; 01111 for ($i = 0; $i < $padding1; $i++) { 01112 $paddingstr1 .= ' '; 01113 } 01114 $paddingstr2 = ''; 01115 for ($i = 0; $i < $padding2; $i++) { 01116 $paddingstr2 .= ' '; 01117 } 01118 01119 $help .= " -{$argument->short},$paddingstr1--{$argument->long}$equal$paddingstr2{$argument->help}\n"; 01120 } 01121 01122 $help .= "\nUse http://tracker.moodle.org for any suggestions or bug reports.\n"; 01123 01124 if ($argc == 1 || in_array($settings[1], array('--help', '-help', '-h', '-?'))) { 01125 echo $help; 01126 die(); 01127 01128 } else { 01129 $this->do_generation = true; 01130 $settings = $this->_arguments($settings); 01131 $argscount = 0; 01132 01133 foreach ($this->settings as $argument) { 01134 $value = null; 01135 01136 if (in_array($argument->short, array_keys($settings))) { 01137 $value = $settings[$argument->short]; 01138 unset($settings[$argument->short]); 01139 01140 } elseif (in_array($argument->long, array_keys($settings))) { 01141 $value = $settings[$argument->long]; 01142 unset($settings[$argument->long]); 01143 } 01144 01145 if (!is_null($value)) { 01146 01147 if (!empty($argument->type) && ($argument->type == 'mod1,mod2...' || $argument->type == 'SELECT')) { 01148 $value = explode(',', $value); 01149 } 01150 01151 $this->set($argument->long, $value); 01152 $argscount++; 01153 } 01154 } 01155 01156 // If some params are left in argv, it means they are not supported 01157 if ($argscount == 0 || count($settings) > 0) { 01158 echo $help; 01159 die(); 01160 } 01161 } 01162 01163 $this->connect(); 01164 } 01165 01166 public function generate_data() { 01167 if (is_null($this->get('username')) || $this->get('username') == '') { 01168 echo "You must enter a valid username for a moodle administrator account on this site.{$this->eolchar}"; 01169 die(); 01170 } elseif (is_null($this->get('password')) || $this->get('password') == '') { 01171 echo "You must enter a valid password for a moodle administrator account on this site.{$this->eolchar}"; 01172 die(); 01173 } else { 01174 if (!$user = authenticate_user_login($this->get('username'), $this->get('password'))) { 01175 echo "Invalid username or password!{$this->eolchar}"; 01176 die(); 01177 } 01178 if (!is_siteadmin($user)) {//TODO: add some proper access control check here!! 01179 echo "You do not have administration privileges on this Moodle site. " 01180 ."These are required for running the generation script.{$this->eolchar}"; 01181 die(); 01182 } 01183 complete_user_login($user); 01184 } 01185 01186 parent::generate_data(); 01187 } 01188 01194 private function _arguments($argv) { 01195 $_ARG = array(); 01196 foreach ($argv as $arg) { 01197 if (preg_match('/--?([^=]+)=(.*)/',$arg,$reg)) { 01198 $_ARG[$reg[1]] = $reg[2]; 01199 } elseif(preg_match('/-([a-zA-Z0-9]+)/',$arg,$reg)) { 01200 $_ARG[$reg[1]] = 'true'; 01201 } 01202 } 01203 return $_ARG; 01204 } 01205 } 01206 01207 class generator_web extends generator { 01208 public $eolchar = '<br />'; 01209 public $mform; 01210 01211 public function setup() { 01212 global $CFG; 01213 $this->mform = new generator_form(); 01214 01215 if ($data = $this->mform->get_data(false)) { 01216 $this->do_generation = optional_param('do_generation', false, PARAM_BOOL); 01217 foreach ($this->settings as $setting) { 01218 if (isset($data->{$setting->long})) { 01219 $this->set($setting->long, $data->{$setting->long}); 01220 } 01221 } 01222 } else { 01223 $this->do_generation = false; 01224 } 01225 } 01226 01227 public function display() { 01228 global $OUTPUT, $PAGE; 01229 $PAGE->set_title("Data generator"); 01230 echo $OUTPUT->header(); 01231 echo $OUTPUT->heading("Data generator: web interface"); 01232 echo $OUTPUT->heading("FOR DEVELOPMENT PURPOSES ONLY. DO NOT USE ON A PRODUCTION SITE!", 3); 01233 echo $OUTPUT->heading("Your database contents will probably be massacred. You have been warned", 5); 01234 01235 $this->mform->display(); 01236 $this->connect(); 01237 } 01238 01239 public function complete() { 01240 global $OUTPUT; 01241 $this->dispose(); 01242 echo $OUTPUT->footer(); 01243 } 01244 } 01245 01246 class generator_silent extends generator { 01247 01248 } 01249 01250 function generator_generate_data($settings) { 01251 $generator = new generator($settings); 01252 $generator->do_generation = true; 01253 $generator->generate_data(); 01254 } 01255 01256 class fake_form { 01257 function get_new_filename($string) { 01258 return false; 01259 } 01260 01261 function save_stored_file() { 01262 return true; 01263 } 01264 01265 function get_data() { 01266 return array(); 01267 } 01268 } 01269 01270 class generator_form extends moodleform { 01271 function definition() { 01272 global $generator, $CFG; //TODO: sloppy coding style!! 01273 01274 $mform =& $this->_form; 01275 $mform->addElement('hidden', 'do_generation', 1); 01276 $mform->setType('do_generation', PARAM_INT); 01277 01278 foreach ($generator->settings as $setting) { 01279 $type = 'advcheckbox'; 01280 $options = null; 01281 $htmloptions = null; 01282 01283 $label = ucfirst(str_replace('_', ' ', $setting->long)); 01284 if (!empty($setting->type) && $setting->type == 'mod1,mod2...') { 01285 $type = 'select'; 01286 $options = $generator->modules_list; 01287 $htmloptions = array('multiple' => 'multiple'); 01288 } elseif (!empty($setting->type) && $setting->type == 'SELECT') { 01289 $type = 'select'; 01290 $options = array(); 01291 foreach ($setting->default as $option) { 01292 $options[$option] = $option; 01293 } 01294 $htmloptions = array('multiple' => 'multiple'); 01295 } elseif (!empty($setting->type)) { 01296 $type = 'text'; 01297 } 01298 01299 if ($setting->long == 'password' || $setting->long == 'username') { 01300 continue; 01301 } 01302 01303 $mform->addElement($type, $setting->long, $label, $options, $htmloptions); 01304 01305 if (isset($setting->default)) { 01306 $mform->setDefault($setting->long, $setting->default); 01307 } 01308 } 01309 $this->add_action_buttons(false, 'Generate data!'); 01310 } 01311 01312 function definition_after_data() { 01313 01314 } 01315 }