<?php
        if(!isset($CFG)) {
        	require('config.php');
        }
        require_once($CFG->dirroot.'/course/lib.php');
        require_once($CFG->libdir.'/dmllib.php');
        require_once($CFG->libdir.'/accesslib.php');
        require_once($CFG->libdir.'/adminlib.php');
        require_once($CFG->libdir.'/datalib.php');
	                                                                        
	$courses = get_records('course');
	$mods = get_records('modules');
	foreach($courses as $course) {
		$mods_to_add = rand(3,6);
		for($k=1;$k<=$course->numsections;$k++) {
		for($i=1;$i<=$mods_to_add;$i++) {
			$mod = $mods[rand(1,count($mods))];
			// Bring in the module library
			require_once("$CFG->dirroot/mod/".$mod->name."/lib.php");
			$data = new stdClass();
			$data->course = $course->id;
			$data->section = $k;
			$data->module = $mod->id;
			$data->modulename = $mod->name;
			
			switch($data->modulename) {
				case 'assignment':
					// Not implemented
					$types = array('uploadsingle','online');
					$data->assignmenttype = $types[rand(1,count($types))];
					$data->name = "Lorem ipsum assignment";
					$data->description = $data->name . ': ' . $data->type;
					$data->grade = rand(1,100);
					if($data->instance = assignment_base::add_instance($data)) {
						local_process_assign($data);
					}
					break;
				case 'label':
					$data->content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin gravida aliquet tortor sit 
					amet malesuada. Nunc pellentesque ante in erat vehicula non bibendum orci sagittis. Duis rhoncus accumsan 
					enim, eu imperdiet felis feugiat nec. Morbi varius molestie mauris vel fermentum. Quisque eget viverra diam. 
					Nunc tempus, leo vitae sodales faucibus, sapien sapien ultricies turpis, et hendrerit augue ipsum a dolor. In 
					nulla libero, gravida vitae tincidunt ut, elementum et justo. Aliquam rhoncus tincidunt tortor, vitae 
					faucibus urna pulvinar eget. In mollis nibh blandit mi porttitor pellentesque. Nullam eu lorem et diam 
					hendrerit hendrerit nec ut nisl. Vivamus varius rhoncus nunc at tristique. Praesent dignissim hendrerit 
					rhoncus.";
					if($data->instance = label_add_instance($data)) {
						local_process_assign($data);
					}
					break;
				case 'wiki':
					$data->name = "Lorem ipsum wiki";
					$data->summary = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
					if($data->instance = wiki_add_instance($data)) {
						local_process_assign($data);
					}
					break;
				case 'forum':
					$forum_types = array('single','eachuser','qanda','general');
					$data->name = "Lorem ipsum forum";
					$data->type = $forum_types[rand(0,count($forum_types)-1)];
					$data->intro = "A forum";
					if($data->instance = forum_add_instance($data)) {
						local_process_assign($data);
					}
					break;	
			}
		}
		}
	}
	
	function local_process_assign($data) {
		$data->coursemodule = add_course_module($data);
		add_mod_to_section($data);
		set_field("course_modules","section",$data->section,"id",$data->coursemodule);
		set_coursemodule_visible($data->coursemodule,1);
	}
?>
