DummyCourseCreator.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Dummy course creator
  5. * @package chamilo.backup
  6. */
  7. /**
  8. * Code
  9. */
  10. require_once 'Course.class.php';
  11. require_once 'Document.class.php';
  12. require_once 'Event.class.php';
  13. require_once 'Link.class.php';
  14. require_once 'LinkCategory.class.php';
  15. require_once 'ForumCategory.class.php';
  16. require_once 'Forum.class.php';
  17. require_once 'ForumTopic.class.php';
  18. require_once 'ForumPost.class.php';
  19. require_once 'CourseDescription.class.php';
  20. require_once 'CourseCopyLearnpath.class.php';
  21. require_once 'CourseRestorer.class.php';
  22. /**
  23. * Class
  24. * @package chamilo.backup
  25. */
  26. class DummyCourseCreator
  27. {
  28. /**
  29. * The dummy course
  30. */
  31. var $course;
  32. /**
  33. *
  34. */
  35. var $default_property = array();
  36. /**
  37. * Create the dummy course
  38. */
  39. function create_dummy_course($course_code)
  40. {
  41. $this->default_property['insert_user_id'] = '1';
  42. $this->default_property['insert_date'] = date('Y-m-d H:i:s');
  43. $this->default_property['lastedit_date'] = date('Y-m-d H:i:s');
  44. $this->default_property['lastedit_user_id'] = '1';
  45. $this->default_property['to_group_id'] = '0';
  46. $this->default_property['to_user_id'] = null;
  47. $this->default_property['visibility'] = '1';
  48. $this->default_property['start_visible'] = '0000-00-00 00:00:00';
  49. $this->default_property['end_visible'] = '0000-00-00 00:00:00';
  50. $course = api_get_course_info($course_code);
  51. $this->course = new Course();
  52. $tmp_path = api_get_path(SYS_COURSE_PATH).$course['directory'].'/document/tmp_'.uniqid('');
  53. @mkdir($tmp_path, api_get_permissions_for_new_directories(), true);
  54. $this->course->backup_path = $tmp_path;
  55. $this->create_dummy_links();
  56. $this->create_dummy_events();
  57. $this->create_dummy_forums();
  58. $this->create_dummy_announcements();
  59. $this->create_dummy_documents();
  60. $this->create_dummy_learnpaths();
  61. $cr = new CourseRestorer($this->course);
  62. $cr->set_file_option(FILE_OVERWRITE);
  63. $cr->restore($course_code);
  64. api_rmdirr($tmp_path);
  65. }
  66. /**
  67. * Create dummy documents
  68. */
  69. function create_dummy_documents()
  70. {
  71. $course = api_get_course_info();
  72. $course_doc_path = $this->course->backup_path.'/document/';
  73. $number_of_documents = rand(10, 30);
  74. $extensions = array ('html', 'doc');
  75. $directories = array();
  76. $property = $this->default_property;
  77. $property['lastedit_type'] = 'DocumentAdded';
  78. $property['tool'] = TOOL_DOCUMENT;
  79. $doc_id = 0;
  80. for ($doc_id = 1; $doc_id < $number_of_documents; $doc_id ++)
  81. {
  82. $path = '';
  83. $doc_type = rand(0, count($extensions) - 1);
  84. $extension = $extensions[$doc_type];
  85. $filename = $this->get_dummy_content('title').'_'.$doc_id.'.'.$extension;
  86. $content = $this->get_dummy_content('text');
  87. $dirs = rand(0, 3);
  88. for ($i = 0; $i < $dirs; $i ++)
  89. {
  90. $path .= 'directory/';
  91. $directories[$path] = 1;
  92. }
  93. $dir_to_make = $course_doc_path.$path;
  94. if (!is_dir($dir_to_make))
  95. {
  96. @mkdir($dir_to_make, api_get_permissions_for_new_directories(), true);
  97. }
  98. $file = $course_doc_path.$path.$filename;
  99. $fp = fopen($file, 'w');
  100. fwrite($fp, $content);
  101. fclose($fp);
  102. $size = filesize($file);
  103. $document = new Document($doc_id, '/'.$path.$filename,$this->get_dummy_content('description'),$this->get_dummy_content('title'), 'file', $size);
  104. $document->item_properties[] = $property;
  105. $this->course->add_resource($document);
  106. }
  107. foreach($directories as $path => $flag)
  108. {
  109. $path = substr($path,0,strlen($path)-1);
  110. $document = new Document($doc_id++,'/'.$path, $this->get_dummy_content('description'),$this->get_dummy_content('title'),'folder',0);
  111. $property['lastedit_type'] = 'FolderCreated';
  112. $document->item_properties[] = $property;
  113. $this->course->add_resource($document);
  114. }
  115. }
  116. /**
  117. * Create dummy announcements
  118. */
  119. function create_dummy_announcements()
  120. {
  121. $property = $this->default_property;
  122. $property['lastedit_type'] = 'AnnouncementAdded';
  123. $property['tool'] = TOOL_ANNOUNCEMENT;
  124. $number_of_announcements = rand(10, 30);
  125. for ($i = 0; $i < $number_of_announcements; $i ++)
  126. {
  127. $time = mktime(rand(1, 24), rand(1, 60), 0, rand(1, 12), rand(1, 28), intval(date('Y')));
  128. $date = date('Y-m-d', $time);
  129. $announcement = new Announcement($i,$this->get_dummy_content('title'),$this->get_dummy_content('text'), $date,0);
  130. $announcement->item_properties[] = $property;
  131. $this->course->add_resource($announcement);
  132. }
  133. }
  134. /**
  135. * Create dummy events
  136. */
  137. function create_dummy_events()
  138. {
  139. $number_of_events = rand(10, 30);
  140. $property = $this->default_property;
  141. $property['lastedit_type'] = 'AgendaAdded';
  142. $property['tool'] = TOOL_CALENDAR_EVENT;
  143. for ($i = 0; $i < $number_of_events; $i ++)
  144. {
  145. $hour = rand(1,24);
  146. $minute = rand(1,60);
  147. $second = rand(1,60);
  148. $day = rand(1,28);
  149. $month = rand(1,12);
  150. $year = intval(date('Y'));
  151. $time = mktime($hour,$minute,$second,$month,$day,$year);
  152. $start_date = date('Y-m-d H:m:s', $time);
  153. $hour = rand($hour,24);
  154. $minute = rand($minute,60);
  155. $second = rand($second,60);
  156. $day = rand($day,28);
  157. $month = rand($month,12);
  158. $year = intval(date('Y'));
  159. $time = mktime($hour,$minute,$second,$month,$day,$year);
  160. $end_date = date('Y-m-d H:m:s', $time);
  161. $event = new Event($i, $this->get_dummy_content('title'), $this->get_dummy_content('text'), $start_date, $end_date);
  162. $event->item_properties[] = $property;
  163. $this->course->add_resource($event);
  164. }
  165. }
  166. /**
  167. * Create dummy links
  168. */
  169. function create_dummy_links()
  170. {
  171. // create categorys
  172. $number_of_categories = rand(5, 10);
  173. for ($i = 0; $i < $number_of_categories; $i ++)
  174. {
  175. $linkcat = new LinkCategory($i, $this->get_dummy_content('title'), $this->get_dummy_content('description'),$i);
  176. $this->course->add_resource($linkcat);
  177. }
  178. // create links
  179. $number_of_links = rand(5, 50);
  180. $on_homepage = rand(0,20) == 0 ? 1 : 0;
  181. $property = $this->default_property;
  182. $property['lastedit_type'] = 'LinkAdded';
  183. $property['tool'] = TOOL_LINK;
  184. for ($i = 0; $i < $number_of_links; $i ++)
  185. {
  186. $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);
  187. $link->item_properties[] = $property;
  188. $this->course->add_resource($link);
  189. }
  190. }
  191. /**
  192. * Create dummy forums
  193. */
  194. function create_dummy_forums()
  195. {
  196. $number_of_categories = rand(2, 6);
  197. $number_of_forums = rand(5, 50);
  198. $number_of_topics = rand(30, 100);
  199. $number_of_posts = rand(100, 1000);
  200. $last_forum_post = array ();
  201. $last_topic_post = array ();
  202. // create categorys
  203. $order = 1;
  204. for ($i = 1; $i <= $number_of_categories; $i ++)
  205. {
  206. $forumcat = new ForumCategory($i, $this->get_dummy_content('title'), $this->get_dummy_content('description'), $order, 0, 0);
  207. $this->course->add_resource($forumcat);
  208. $order++;
  209. }
  210. // create posts
  211. for ($post_id = 1; $post_id <= $number_of_posts; $post_id ++)
  212. {
  213. $topic_id = rand(1, $number_of_topics);
  214. $last_topic_post[$topic_id] = $post_id;
  215. $post = new ForumPost($post_id, $this->get_dummy_content('title'), $this->get_dummy_content('text'), date('Y-m-d H:i:s'), 1, 'Portal Administrator', 0, 0, $topic_id, 0, 1);
  216. $this->course->add_resource($post);
  217. }
  218. // create topics
  219. for ($topic_id = 1; $topic_id <= $number_of_topics; $topic_id ++)
  220. {
  221. $forum_id = rand(1, $number_of_forums);
  222. $last_forum_post[$forum_id] = $last_topic_post[$topic_id];
  223. $topic = new ForumTopic($topic_id, $this->get_dummy_content('title'), '2011-03-31 12:10:00', 'Chamilo', 'Administrator', 0, $forum_id, $last_topic_post[$topic_id]);
  224. $this->course->add_resource($topic);
  225. }
  226. // create forums
  227. for ($forum_id = 1; $forum_id <= $number_of_forums; $forum_id ++)
  228. {
  229. $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]);
  230. $this->course->add_resource($forum);
  231. }
  232. }
  233. /**
  234. * Create dummy learnpaths
  235. */
  236. function create_dummy_learnpaths()
  237. {
  238. $number_of_learnpaths = rand(3,5);
  239. $global_item_id = 1;
  240. for($i=1; $i<=$number_of_learnpaths;$i++)
  241. {
  242. $chapters = array();
  243. $number_of_chapters = rand(1,6);
  244. for($chapter_id = 1; $chapter_id <= $number_of_chapters; $chapter_id++)
  245. {
  246. $chapter['name'] = $this->get_dummy_content('title');
  247. $chapter['description'] = $this->get_dummy_content('description');
  248. $chapter['display_order'] = $chapter_id;
  249. $chapter['items'] = array();
  250. $number_of_items = rand(5,20);
  251. for( $item_id = 1; $item_id<$number_of_items; $item_id++)
  252. {
  253. $types = array(RESOURCE_ANNOUNCEMENT, RESOURCE_EVENT, RESOURCE_DOCUMENT,RESOURCE_LINK,RESOURCE_FORUM,RESOURCE_FORUMPOST,RESOURCE_FORUMTOPIC);
  254. $type = $types[rand(0,count($types)-1)];
  255. $resources = $this->course->resources[$type];
  256. $resource = $resources[rand(0,count($resources)-1)];
  257. $item = array();
  258. $item['type'] = $resource->type;
  259. $item['id'] = $resource->source_id;
  260. $item['display_order'] = $item_id;
  261. $item['title'] = $this->get_dummy_content('title');
  262. $item['description'] = $this->get_dummy_content('description');
  263. $item['ref_id'] = $global_item_id;
  264. if( rand(0,5) == 1 && $item_id > 1)
  265. {
  266. $item['prereq_type'] = 'i';
  267. $item['prereq'] = rand($global_item_id - $item_id,$global_item_id-1);
  268. }
  269. $chapter['items'][] = $item;
  270. $global_item_id++;
  271. }
  272. $chapters[] = $chapter;
  273. }
  274. $lp = new CourseCopyLearnpath($i,$this->get_dummy_content('title'),$this->get_dummy_content('description'),1,$chapters);
  275. $this->course->add_resource($lp);
  276. }
  277. }
  278. /**
  279. * Get dummy titles, descriptions and texts
  280. */
  281. function get_dummy_content($type)
  282. {
  283. $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.
  284. 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.
  285. 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.
  286. 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.
  287. 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.';
  288. switch($type)
  289. {
  290. case 'description':
  291. $descriptions = explode(".",$dummy_text);
  292. return $descriptions[rand(0,count($descriptions)-1)];
  293. break;
  294. case 'title':
  295. $dummy_text = str_replace(array("\n",'.',',',"\t"),array(' ','','',' '),$dummy_text);
  296. $titles = explode(" ",$dummy_text);
  297. return trim($titles[rand(0,count($titles)-1)]);
  298. break;
  299. case 'text':
  300. $texts = explode("\n",$dummy_text);
  301. return $texts[rand(0,count($texts)-1)];
  302. break;
  303. }
  304. }
  305. }