CourseRecycler.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. // $Id: CourseRecycler.class.php 10190 2006-11-24 00:23:20Z pcool $
  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 ('rmdirr.php');
  23. /**
  24. * Class to delete items from a Dokeos-course
  25. * @author Bart Mollet <bart.mollet@hogent.be>
  26. * @package dokeos.backup
  27. */
  28. class CourseRecycler
  29. {
  30. /**
  31. * A course-object with the items to delete
  32. */
  33. var $course;
  34. /**
  35. * Create a new CourseRecycler
  36. * @param course $course The course-object which contains the items to
  37. * delete
  38. */
  39. function CourseRecycler($course)
  40. {
  41. $this->course = $course;
  42. }
  43. /**
  44. * Delete all items from the course.
  45. * This deletes all items in the course-object from the current Dokeos-
  46. * course
  47. */
  48. function recycle()
  49. {
  50. $table_linked_resources = Database :: get_course_table(LINKED_RESOURCES_TABLE, $this->course->destination_db);
  51. $table_item_properties = Database::get_course_table(ITEM_PROPERTY_TABLE);
  52. foreach ($this->course->resources as $type => $resources)
  53. {
  54. foreach ($resources as $id => $resource)
  55. {
  56. $sql = "DELETE FROM ".$table_linked_resources." WHERE (source_type = '".$type."' AND source_id = '".$id."') OR (resource_type = '".$type."' AND resource_id = '".$id."') ";
  57. api_sql_query($sql,__FILE__,__LINE__);
  58. $sql = "DELETE FROM ".$table_item_properties." WHERE tool ='".$resource->get_tool()."' AND ref=".$id;
  59. api_sql_query($sql);
  60. }
  61. }
  62. $this->recycle_links();
  63. $this->recycle_link_categories();
  64. $this->recycle_events();
  65. $this->recycle_announcements();
  66. $this->recycle_documents();
  67. //$this->recycle_forums();
  68. //$this->recycle_forum_categories();
  69. $this->recycle_quizzes();
  70. $this->recycle_learnpaths();
  71. $this->recycle_cours_description();
  72. }
  73. /**
  74. * Delete documents
  75. */
  76. function recycle_documents()
  77. {
  78. if ($this->course->has_resources(RESOURCE_DOCUMENT))
  79. {
  80. $table = Database :: get_course_table(DOCUMENT_TABLE);
  81. foreach ($this->course->resources[RESOURCE_DOCUMENT] as $id => $document)
  82. {
  83. rmdirr($this->course->backup_path.'/'.$document->path);
  84. }
  85. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_DOCUMENT])));
  86. $sql = "DELETE FROM ".$table." WHERE id IN(".$ids.")";
  87. api_sql_query($sql,__FILE__,__LINE__);
  88. }
  89. }
  90. /**
  91. * Delete links
  92. */
  93. function recycle_links()
  94. {
  95. if ($this->course->has_resources(RESOURCE_LINK))
  96. {
  97. $table = Database :: get_course_table(LINK_TABLE);
  98. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_LINK])));
  99. $sql = "DELETE FROM ".$table." WHERE id IN(".$ids.")";
  100. api_sql_query($sql,__FILE__,__LINE__);
  101. }
  102. }
  103. /**
  104. * Delete forums
  105. */
  106. function recycle_forums()
  107. {
  108. if ($this->course->has_resources(RESOURCE_FORUM))
  109. {
  110. $table_forum = Database :: get_course_table(FORUM_TABLE);
  111. $table_topic = Database :: get_course_table(FORUM_TOPIC_TABLE);
  112. $table_post = Database :: get_course_table(TABLE_FORUM_POST);
  113. $table_posttext = Database :: get_course_table(TOOL_FORUM_POST_TEXT_TABLE);
  114. $forum_ids = implode(',', (array_keys($this->course->resources[RESOURCE_FORUM])));
  115. $sql = "SELECT post_id FROM ".$table_post." WHERE forum_id IN (".$forum_ids.")";
  116. $res = api_sql_query($sql,__FILE__,__LINE__);
  117. $post_ids = array ();
  118. while ($obj = mysql_fetch_object($res))
  119. {
  120. $post_ids[] = $obj->post_id;
  121. }
  122. $post_ids = implode(',', $post_ids);
  123. $sql = "DELETE FROM ".$table_posttext." WHERE post_id IN(".$post_ids.")";
  124. api_sql_query($sql,__FILE__,__LINE__);
  125. $sql = "DELETE FROM ".$table_post." WHERE forum_id IN(".$forum_ids.")";
  126. api_sql_query($sql,__FILE__,__LINE__);
  127. $sql = "DELETE FROM ".$table_topic." WHERE forum_id IN(".$forum_ids.")";
  128. api_sql_query($sql,__FILE__,__LINE__);
  129. $sql = "DELETE FROM ".$table_forum." WHERE forum_id IN(".$forum_ids.")";
  130. api_sql_query($sql,__FILE__,__LINE__);
  131. }
  132. }
  133. /**
  134. * Delete forum-categories
  135. * Deletes all forum-categories from current course without forums
  136. */
  137. function recycle_forum_categories()
  138. {
  139. $table_forum = Database :: get_course_table(TABLE_FORUM);
  140. $table_forumcat = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  141. $sql = "SELECT fc.cat_id FROM ".$table_forumcat." fc LEFT JOIN ".$table_forum." f ON fc.cat_id=f.cat_id WHERE f.forum_id IS NULL";
  142. $res = api_sql_query($sql,__FILE__,__LINE__);
  143. while ($obj = mysql_fetch_object($res))
  144. {
  145. $sql = "DELETE FROM ".$table_forumcat." WHERE cat_id = ".$obj->cat_id;
  146. api_sql_query($sql,__FILE__,__LINE__);
  147. }
  148. }
  149. /**
  150. * Delete link-categories
  151. * Deletes all link-categories from current course without links
  152. */
  153. function recycle_link_categories()
  154. {
  155. $link_cat_table = Database :: get_course_table(LINK_CATEGORY_TABLE);
  156. $link_table = Database :: get_course_table(LINK_TABLE);
  157. $sql = "SELECT lc.id FROM ".$link_cat_table." lc LEFT JOIN ".$link_table." l ON lc.id=l.category_id WHERE l.id IS NULL";
  158. $res = api_sql_query($sql,__FILE__,__LINE__);
  159. while ($obj = mysql_fetch_object($res))
  160. {
  161. $sql = "DELETE FROM ".$link_cat_table." WHERE id = ".$obj->id;
  162. api_sql_query($sql,__FILE__,__LINE__);
  163. }
  164. }
  165. /**
  166. * Delete events
  167. */
  168. function recycle_events()
  169. {
  170. if ($this->course->has_resources(RESOURCE_EVENT))
  171. {
  172. $table = Database :: get_course_table(AGENDA_TABLE);
  173. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_EVENT])));
  174. $sql = "DELETE FROM ".$table." WHERE id IN(".$ids.")";
  175. api_sql_query($sql,__FILE__,__LINE__);
  176. }
  177. }
  178. /**
  179. * Delete announcements
  180. */
  181. function recycle_announcements()
  182. {
  183. if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT))
  184. {
  185. $table = Database :: get_course_table(ANNOUNCEMENT_TABLE);
  186. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_ANNOUNCEMENT])));
  187. $sql = "DELETE FROM ".$table." WHERE id IN(".$ids.")";
  188. api_sql_query($sql,__FILE__,__LINE__);
  189. }
  190. }
  191. /**
  192. * Recycle quizzes
  193. */
  194. function recycle_quizzes()
  195. {
  196. if ($this->course->has_resources(RESOURCE_QUIZ))
  197. {
  198. $table_qui = Database :: get_course_table(QUIZ_TEST_TABLE);
  199. $table_rel = Database :: get_course_table(QUIZ_TEST_QUESTION_TABLE);
  200. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_QUIZ])));
  201. $sql = "DELETE FROM ".$table_qui." WHERE id IN(".$ids.")";
  202. api_sql_query($sql,__FILE__,__LINE__);
  203. $sql = "DELETE FROM ".$table_rel." WHERE exercice_id IN(".$ids.")";
  204. api_sql_query($sql,__FILE__,__LINE__);
  205. }
  206. }
  207. /**
  208. * Recycle learnpaths
  209. */
  210. function recycle_learnpaths()
  211. {
  212. if ($this->course->has_resources(RESOURCE_LEARNPATH))
  213. {
  214. $table_main = Database :: get_course_table(LEARNPATH_MAIN_TABLE);
  215. $table_chapter = Database :: get_course_table(LEARNPATH_CHAPTER_TABLE);
  216. $table_item = Database :: get_course_table(LEARNPATH_ITEM_TABLE);
  217. $table_tool = Database::get_course_table(TOOL_LIST_TABLE);
  218. foreach($this->course->resources[RESOURCE_LEARNPATH] as $id => $learnpath)
  219. {
  220. $sql = "DELETE FROM $table_tool WHERE link='".mysql_real_escape_string('learnpath/learnpath_handler.php?learnpath_id='.$id)."'";
  221. api_sql_query($sql,__FILE__,__LINE__);
  222. }
  223. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_LEARNPATH])));
  224. $sql = "SELECT id FROM ".$table_chapter." WHERE learnpath_id IN (".$ids.")";
  225. $db_result = api_sql_query($sql,__FILE__,__LINE__);
  226. $chapter_ids = array ();
  227. while ($chap = mysql_fetch_object($db_result))
  228. {
  229. $chapter_ids[] = $chap->id;
  230. }
  231. if( count($chapter_ids) > 0 )
  232. {
  233. $chap_ids = implode(',', $chapter_ids);
  234. $sql = "DELETE FROM ".$table_item." WHERE chapter_id IN (".$chap_ids.")";
  235. api_sql_query($sql,__FILE__,__LINE__);
  236. $sql = "DELETE FROM ".$table_chapter." WHERE id IN (".$chap_ids.")";
  237. api_sql_query($sql,__FILE__,__LINE__);
  238. }
  239. $sql = "DELETE FROM ".$table_main." WHERE learnpath_id IN(".$ids.")";
  240. api_sql_query($sql,__FILE__,__LINE__);
  241. }
  242. }
  243. /**
  244. * Delete course description
  245. */
  246. function recycle_cours_description()
  247. {
  248. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION))
  249. {
  250. $table = Database :: get_course_table(COURSE_DESCRIPTION_TABLE);
  251. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_COURSEDESCRIPTION])));
  252. $sql = "DELETE FROM ".$table." WHERE id IN(".$ids.")";
  253. api_sql_query($sql,__FILE__,__LINE__);
  254. }
  255. }
  256. }
  257. ?>