DummyCourseCreator.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. // $Id: DummyCourseCreator.class.php 15087 2008-04-25 04:37:14Z yannoo $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. require_once 'Course.class.php';
  22. require_once 'Document.class.php';
  23. require_once 'Event.class.php';
  24. require_once 'Link.class.php';
  25. require_once 'LinkCategory.class.php';
  26. require_once 'ForumCategory.class.php';
  27. require_once 'Forum.class.php';
  28. require_once 'ForumTopic.class.php';
  29. require_once 'ForumPost.class.php';
  30. require_once 'CourseDescription.class.php';
  31. require_once 'Learnpath.class.php';
  32. require_once 'CourseRestorer.class.php';
  33. class DummyCourseCreator
  34. {
  35. /**
  36. * The dummy course
  37. */
  38. var $course;
  39. /**
  40. *
  41. */
  42. var $default_property = array();
  43. /**
  44. * Create the dummy course
  45. */
  46. function create_dummy_course($course_code)
  47. {
  48. $this->default_property['insert_user_id'] = '1';
  49. $this->default_property['insert_date'] = date('Y-m-d H:i:s');
  50. $this->default_property['lastedit_date'] = date('Y-m-d H:i:s');
  51. $this->default_property['lastedit_user_id'] = '1';
  52. $this->default_property['to_group_id'] = '0';
  53. $this->default_property['to_user_id'] = null;
  54. $this->default_property['visibility'] = '1';
  55. $this->default_property['start_visible'] = '0000-00-00 00:00:00';
  56. $this->default_property['end_visible'] = '0000-00-00 00:00:00';
  57. $course = Database::get_course_info($course_code);
  58. $this->course = new Course();
  59. $tmp_path = api_get_path(SYS_COURSE_PATH).$course['directory'].'/document/tmp_'.uniqid('');
  60. @mkdir($tmp_path, 0755, true);
  61. $this->course->backup_path = $tmp_path;
  62. $this->create_dummy_links();
  63. $this->create_dummy_events();
  64. $this->create_dummy_forums();
  65. $this->create_dummy_announcements();
  66. $this->create_dummy_documents();
  67. $this->create_dummy_learnpaths();
  68. $cr = new CourseRestorer($this->course);
  69. $cr->set_file_option(FILE_OVERWRITE);
  70. $cr->restore($course_code);
  71. rmdirr($tmp_path);
  72. }
  73. /**
  74. * Create dummy documents
  75. */
  76. function create_dummy_documents()
  77. {
  78. $course = api_get_course_info();
  79. $course_doc_path = $this->course->backup_path.'/document/';
  80. $number_of_documents = rand(10, 30);
  81. $extensions = array ('html', 'doc');
  82. $directories = array();
  83. $property = $this->default_property;
  84. $property['lastedit_type'] = 'DocumentAdded';
  85. $property['tool'] = TOOL_DOCUMENT;
  86. $doc_id = 0;
  87. for ($doc_id = 1; $doc_id < $number_of_documents; $doc_id ++)
  88. {
  89. $path = '';
  90. $doc_type = rand(0, count($extensions) - 1);
  91. $extension = $extensions[$doc_type];
  92. $filename = $this->get_dummy_content('title').'_'.$doc_id.'.'.$extension;
  93. $content = $this->get_dummy_content('text');
  94. $dirs = rand(0, 3);
  95. for ($i = 0; $i < $dirs; $i ++)
  96. {
  97. $path .= 'directory/';
  98. $directories[$path] = 1;
  99. }
  100. $dir_to_make = $course_doc_path.$path;
  101. if (!is_dir($dir_to_make))
  102. {
  103. @mkdir($dir_to_make, 0755, true);
  104. }
  105. $file = $course_doc_path.$path.$filename;
  106. $fp = fopen($file, 'w');
  107. fwrite($fp, $content);
  108. fclose($fp);
  109. $size = filesize($file);
  110. $document = new Document($doc_id, '/'.$path.$filename,$this->get_dummy_content('description'),$this->get_dummy_content('title'), 'file', $size);
  111. $document->item_properties[] = $property;
  112. $this->course->add_resource($document);
  113. }
  114. foreach($directories as $path => $flag)
  115. {
  116. $path = substr($path,0,strlen($path)-1);
  117. $document = new Document($doc_id++,'/'.$path, $this->get_dummy_content('description'),$this->get_dummy_content('title'),'folder',0);
  118. $property['lastedit_type'] = 'FolderCreated';
  119. $document->item_properties[] = $property;
  120. $this->course->add_resource($document);
  121. }
  122. }
  123. /**
  124. * Create dummy announcements
  125. */
  126. function create_dummy_announcements()
  127. {
  128. $property = $this->default_property;
  129. $property['lastedit_type'] = 'AnnouncementAdded';
  130. $property['tool'] = TOOL_ANNOUNCEMENT;
  131. $number_of_announcements = rand(10, 30);
  132. for ($i = 0; $i < $number_of_announcements; $i ++)
  133. {
  134. $time = mktime(rand(1, 24), rand(1, 60), 0, rand(1, 12), rand(1, 28), intval(date('Y')));
  135. $date = date('Y-m-d', $time);
  136. $announcement = new Announcement($i,$this->get_dummy_content('title'),$this->get_dummy_content('text'), $date,0);
  137. $announcement->item_properties[] = $property;
  138. $this->course->add_resource($announcement);
  139. }
  140. }
  141. /**
  142. * Create dummy events
  143. */
  144. function create_dummy_events()
  145. {
  146. $number_of_events = rand(10, 30);
  147. $property = $this->default_property;
  148. $property['lastedit_type'] = 'AgendaAdded';
  149. $property['tool'] = TOOL_CALENDAR_EVENT;
  150. for ($i = 0; $i < $number_of_events; $i ++)
  151. {
  152. $hour = rand(1,24);
  153. $minute = rand(1,60);
  154. $second = rand(1,60);
  155. $day = rand(1,28);
  156. $month = rand(1,12);
  157. $year = intval(date('Y'));
  158. $time = mktime($hour,$minute,$second,$month,$day,$year);
  159. $start_date = date('Y-m-d H:m:s', $time);
  160. $hour = rand($hour,24);
  161. $minute = rand($minute,60);
  162. $second = rand($second,60);
  163. $day = rand($day,28);
  164. $month = rand($month,12);
  165. $year = intval(date('Y'));
  166. $time = mktime($hour,$minute,$second,$month,$day,$year);
  167. $end_date = date('Y-m-d H:m:s', $time);
  168. $event = new Event($i, $this->get_dummy_content('title'), $this->get_dummy_content('text'), $start_date, $end_date);
  169. $event->item_properties[] = $property;
  170. $this->course->add_resource($event);
  171. }
  172. }
  173. /**
  174. * Create dummy links
  175. */
  176. function create_dummy_links()
  177. {
  178. // create categorys
  179. $number_of_categories = rand(5, 10);
  180. for ($i = 0; $i < $number_of_categories; $i ++)
  181. {
  182. $linkcat = new LinkCategory($i, $this->get_dummy_content('title'), $this->get_dummy_content('description'),$i);
  183. $this->course->add_resource($linkcat);
  184. }
  185. // create links
  186. $number_of_links = rand(5, 50);
  187. $on_homepage = rand(0,20) == 0 ? 1 : 0;
  188. $property = $this->default_property;
  189. $property['lastedit_type'] = 'LinkAdded';
  190. $property['tool'] = TOOL_LINK;
  191. for ($i = 0; $i < $number_of_links; $i ++)
  192. {
  193. $link = new Link($i, $this->get_dummy_content('title'), 'http://www.google.com/search?q='.$this->get_dummy_content('title'), $this->get_dummy_content('description'), rand(0, $number_of_categories -1),$on_homepage);
  194. $link->item_properties[] = $property;
  195. $this->course->add_resource($link);
  196. }
  197. }
  198. /**
  199. * Create dummy forums
  200. */
  201. function create_dummy_forums()
  202. {
  203. $number_of_categories = rand(2, 6);
  204. $number_of_forums = rand(5, 50);
  205. $number_of_topics = rand(30, 100);
  206. $number_of_posts = rand(100, 1000);
  207. $last_forum_post = array ();
  208. $last_topic_post = array ();
  209. // create categorys
  210. $order = 1;
  211. for ($i = 1; $i <= $number_of_categories; $i ++)
  212. {
  213. $forumcat = new ForumCategory($i, $this->get_dummy_content('title'), $this->get_dummy_content('description'), $order, 0, 0);
  214. $this->course->add_resource($forumcat);
  215. $order++;
  216. }
  217. // create posts
  218. for ($post_id = 1; $post_id <= $number_of_posts; $post_id ++)
  219. {
  220. $topic_id = rand(1, $number_of_topics);
  221. $last_topic_post[$topic_id] = $post_id;
  222. $post = new ForumPost($post_id, $this->get_dummy_content('title'), $this->get_dummy_content('text'), date('Y-m-d H:i:s'), 1, 'Dokeos Administrator', 0, 0, $topic_id, 0, 1);
  223. $this->course->add_resource($post);
  224. }
  225. // create topics
  226. for ($topic_id = 1; $topic_id <= $number_of_topics; $topic_id ++)
  227. {
  228. $forum_id = rand(1, $number_of_forums);
  229. $last_forum_post[$forum_id] = $last_topic_post[$topic_id];
  230. $topic = new ForumTopic($topic_id, $this->get_dummy_content('title'), '2005-03-31 12:10:00', 'Dokeos', 'Administrator', 0, $forum_id, $last_topic_post[$topic_id]);
  231. $this->course->add_resource($topic);
  232. }
  233. // create forums
  234. for ($forum_id = 1; $forum_id <= $number_of_forums; $forum_id ++)
  235. {
  236. $forum = new Forum($forum_id, $this->get_dummy_content('title'),$this->get_dummy_content('description') , rand(1, $number_of_categories), $last_forum_post[$forum_id]);
  237. $this->course->add_resource($forum);
  238. }
  239. }
  240. /**
  241. * Create dummy learnpaths
  242. */
  243. function create_dummy_learnpaths()
  244. {
  245. $number_of_learnpaths = rand(3,5);
  246. $global_item_id = 1;
  247. for($i=1; $i<=$number_of_learnpaths;$i++)
  248. {
  249. $chapters = array();
  250. $number_of_chapters = rand(1,6);
  251. for($chapter_id = 1; $chapter_id <= $number_of_chapters; $chapter_id++)
  252. {
  253. $chapter['name'] = $this->get_dummy_content('title');
  254. $chapter['description'] = $this->get_dummy_content('description');
  255. $chapter['display_order'] = $chapter_id;
  256. $chapter['items'] = array();
  257. $number_of_items = rand(5,20);
  258. for( $item_id = 1; $item_id<$number_of_items; $item_id++)
  259. {
  260. $types = array(RESOURCE_ANNOUNCEMENT, RESOURCE_EVENT, RESOURCE_DOCUMENT,RESOURCE_LINK,RESOURCE_FORUM,RESOURCE_FORUMPOST,RESOURCE_FORUMTOPIC);
  261. $type = $types[rand(0,count($types)-1)];
  262. $resources = $this->course->resources[$type];
  263. $resource = $resources[rand(0,count($resources)-1)];
  264. $item = array();
  265. $item['type'] = $resource->type;
  266. $item['id'] = $resource->source_id;
  267. $item['display_order'] = $item_id;
  268. $item['title'] = $this->get_dummy_content('title');
  269. $item['description'] = $this->get_dummy_content('description');
  270. $item['ref_id'] = $global_item_id;
  271. if( rand(0,5) == 1 && $item_id > 1)
  272. {
  273. $item['prereq_type'] = 'i';
  274. $item['prereq'] = rand($global_item_id - $item_id,$global_item_id-1);
  275. }
  276. $chapter['items'][] = $item;
  277. $global_item_id++;
  278. }
  279. $chapters[] = $chapter;
  280. }
  281. $lp = new Learnpath($i,$this->get_dummy_content('title'),$this->get_dummy_content('description'),1,$chapters);
  282. $this->course->add_resource($lp);
  283. }
  284. }
  285. /**
  286. * Get dummy titles, descriptions and texts
  287. */
  288. function get_dummy_content($type)
  289. {
  290. $dummy_text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque lectus. Duis sodales. Vivamus et nunc. Phasellus interdum est a lorem. Fusce venenatis luctus lectus. Mauris quis turpis ac erat rhoncus suscipit. Phasellus elit dui, semper at, porta ut, egestas ac, enim. Quisque pellentesque, nisl nec consequat mollis, ipsum justo pellentesque nibh, non faucibus odio ante at lorem. Donec vitae pede ut felis ultricies semper. Suspendisse velit nibh, interdum quis, gravida nec, dapibus ac, leo. Cras id sem ut tellus tincidunt scelerisque. Aenean ac magna feugiat dolor accumsan dignissim. Integer eget nisl.
  291. Ut sit amet nulla. Vestibulum venenatis posuere mauris. Nullam magna leo, blandit luctus, consequat quis, gravida nec, justo. Nam pede. Etiam ut nisl. In at quam scelerisque sapien faucibus commodo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Proin mattis lorem quis nunc. Praesent placerat ligula id elit. Aenean blandit, purus sit amet pharetra auctor, libero orci rutrum felis, sit amet sodales mauris ipsum ultricies sapien. Vivamus wisi. Cras elit elit, ullamcorper ac, interdum nec, pulvinar nec, lacus. In lacus. Vivamus auctor, arcu vitae tincidunt porta, eros lacus tristique justo, vitae semper risus neque eget massa. Vivamus turpis.
  292. Aenean ac wisi non enim aliquam scelerisque. Praesent eget mi. Vestibulum volutpat pulvinar justo. Phasellus sapien ante, pharetra id, bibendum sed, porta non, purus. Maecenas leo velit, luctus quis, porta non, feugiat sit amet, sapien. Proin vitae augue ut massa adipiscing placerat. Morbi ac risus. Proin dapibus eros egestas quam. Fusce fermentum lobortis elit. Duis lectus tellus, convallis nec, lobortis vel, accumsan ut, nunc. Nunc est. Donec ullamcorper laoreet quam.
  293. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Suspendisse potenti. Mauris mi. Vivamus risus lacus, faucibus sit amet, sollicitudin a, blandit et, justo. In hendrerit. Sed imperdiet, eros at fringilla tempor, turpis augue semper enim, quis rhoncus nibh enim quis dui. Sed massa sapien, mattis et, laoreet sit amet, dignissim nec, urna. Integer laoreet quam quis lectus. Curabitur convallis gravida dui. Nam metus. Ut sit amet augue in nibh interdum scelerisque. Donec venenatis, lacus et pulvinar euismod, libero massa condimentum pede, commodo tristique nunc massa eu quam. Donec vulputate. Aenean in nibh. Phasellus porttitor. Donec molestie, sem ac porttitor vulputate, mauris dui egestas libero, ac lobortis dolor sem vel ligula. Nam vulputate pretium libero. Cras accumsan. Vivamus lacinia sapien sit amet elit.
  294. Duis bibendum elementum justo. Duis posuere. Fusce nulla odio, posuere eget, condimentum nec, venenatis eu, elit. In hac habitasse platea dictumst. Aenean ac sem in enim imperdiet feugiat. Integer tincidunt lectus at elit. Integer magna lacus, vehicula quis, eleifend eget, suscipit vitae, leo. Nunc porta augue nec enim. Curabitur vehicula volutpat enim. Aliquam consequat. Vestibulum rhoncus tellus vitae erat. Integer est. Quisque fermentum leo nec odio. Suspendisse lobortis sollicitudin augue. Nullam urna mi, suscipit eu, sagittis laoreet, ultrices ac, sem. Aliquam enim tortor, hendrerit non, cursus a, tristique sit amet, sapien. Suspendisse potenti. Aenean semper placerat neque.';
  295. switch($type)
  296. {
  297. case 'description':
  298. $descriptions = explode(".",$dummy_text);
  299. return $descriptions[rand(0,count($descriptions)-1)];
  300. break;
  301. case 'title':
  302. $dummy_text = str_replace(array("\n",'.',',',"\t"),array(' ','','',' '),$dummy_text);
  303. $titles = explode(" ",$dummy_text);
  304. return trim($titles[rand(0,count($titles)-1)]);
  305. break;
  306. case 'text':
  307. $texts = explode("\n",$dummy_text);
  308. return $texts[rand(0,count($texts)-1)];
  309. break;
  310. }
  311. }
  312. }
  313. ?>