CourseRecycler.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'Course.class.php';
  4. /**
  5. * Class to delete items from a Chamilo-course
  6. * @author Bart Mollet <bart.mollet@hogent.be>
  7. * @package chamilo.backup
  8. */
  9. class CourseRecycler
  10. {
  11. /**
  12. * A course-object with the items to delete
  13. */
  14. public $course;
  15. public $type;
  16. /**
  17. * Create a new CourseRecycler
  18. * @param course $course The course-object which contains the items to
  19. * delete
  20. */
  21. function CourseRecycler($course) {
  22. $this->course = $course;
  23. $this->course_info = api_get_course_info($this->course->code);
  24. $this->course_id = $this->course_info['real_id'];
  25. }
  26. /**
  27. * Delete all items from the course.
  28. * This deletes all items in the course-object from the current Chamilo-
  29. * course
  30. * @param string The type of recycling we want (full_backup or select_items)
  31. * @assert (null) === false
  32. */
  33. function recycle($type) {
  34. if (empty($type)) { return false; }
  35. $this->type = $type;
  36. $table_tool_intro = Database::get_course_table(TABLE_TOOL_INTRO);
  37. $table_linked_resources = Database::get_course_table(TABLE_LINKED_RESOURCES);
  38. $table_item_properties = Database::get_course_table(TABLE_ITEM_PROPERTY);
  39. $this->recycle_links();
  40. $this->recycle_link_categories();
  41. $this->recycle_events();
  42. $this->recycle_announcements();
  43. $this->recycle_documents();
  44. $this->recycle_forums();
  45. $this->recycle_forum_categories();
  46. $this->recycle_quizzes();
  47. $this->recycle_surveys();
  48. $this->recycle_learnpaths();
  49. $this->recycle_cours_description();
  50. $this->recycle_wiki();
  51. $this->recycle_glossary();
  52. $this->recycle_thematic();
  53. $this->recycle_attendance();
  54. foreach ($this->course->resources as $type => $resources) {
  55. foreach ($resources as $id => $resource) {
  56. $sql = "DELETE FROM ".$table_linked_resources." WHERE c_id = ".$this->course_id." AND (source_type = '".$type."' AND source_id = '".$id."') OR (resource_type = '".$type."' AND resource_id = '".$id."') ";
  57. Database::query($sql);
  58. if (is_numeric($id)) {
  59. $sql = "DELETE FROM ".$table_item_properties." WHERE c_id = ".$this->course_id." AND tool ='".$resource->get_tool()."' AND ref=".$id;
  60. Database::query($sql);
  61. } elseif ($type == RESOURCE_TOOL_INTRO) {
  62. $sql = "DELETE FROM $table_tool_intro WHERE c_id = ".$this->course_id." AND id='$id'";
  63. Database::query($sql);
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * Delete documents
  70. */
  71. function recycle_documents() {
  72. if ($this->course->has_resources(RESOURCE_DOCUMENT)) {
  73. $table = Database :: get_course_table(TABLE_DOCUMENT);
  74. foreach ($this->course->resources[RESOURCE_DOCUMENT] as $id => $document) {
  75. rmdirr($this->course->backup_path.'/'.$document->path);
  76. }
  77. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_DOCUMENT])));
  78. $sql = "DELETE FROM ".$table." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  79. Database::query($sql);
  80. }
  81. }
  82. /**
  83. * Delete wiki
  84. */
  85. function recycle_wiki() {
  86. if ($this->course->has_resources(RESOURCE_WIKI)) {
  87. $table_wiki = Database::get_course_table(TABLE_WIKI);
  88. $table_wiki_conf = Database::get_course_table(TABLE_WIKI_CONF);
  89. //$table_wiki_discuss = Database::get_course_table(TABLE_WIKI_DISCUSS);
  90. //$table_wiki_mailcue = Database::get_course_table(TABLE_WIKI_MAILCUE);
  91. $pages = array();
  92. foreach ($this->course->resources[RESOURCE_WIKI] as $resource) {
  93. $pages[] = $resource->page_id;
  94. }
  95. $wiki_ids = implode(',', (array_keys($this->course->resources[RESOURCE_WIKI])));
  96. $page_ids = implode(',', $pages);
  97. $sql = "DELETE FROM ".$table_wiki." WHERE c_id = ".$this->course_id." AND id IN(".$wiki_ids.")";
  98. Database::query($sql);
  99. $sql = "DELETE FROM ".$table_wiki_conf." WHERE c_id = ".$this->course_id." AND page_id IN(".$page_ids.")";
  100. Database::query($sql);
  101. }
  102. }
  103. /**
  104. * Delete glossary
  105. */
  106. function recycle_glossary() {
  107. if ($this->course->has_resources(RESOURCE_GLOSSARY)) {
  108. $table_glossary = Database::get_course_table(TABLE_GLOSSARY);
  109. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_GLOSSARY])));
  110. $sql = "DELETE FROM ".$table_glossary." WHERE c_id = ".$this->course_id." AND glossary_id IN(".$ids.")";
  111. Database::query($sql);
  112. }
  113. }
  114. /**
  115. * Delete links
  116. */
  117. function recycle_links() {
  118. if ($this->course->has_resources(RESOURCE_LINK)) {
  119. $table = Database :: get_course_table(TABLE_LINK);
  120. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_LINK])));
  121. $sql = "DELETE FROM ".$table." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  122. Database::query($sql);
  123. }
  124. }
  125. /**
  126. * Delete forums
  127. */
  128. function recycle_forums() {
  129. $table_category = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  130. $table_forum = Database :: get_course_table(TABLE_FORUM);
  131. $table_thread = Database :: get_course_table(TABLE_FORUM_THREAD);
  132. $table_post = Database :: get_course_table(TABLE_FORUM_POST);
  133. $table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  134. $table_notification = Database::get_course_table(TABLE_FORUM_NOTIFICATION);
  135. $table_mail_queue = Database::get_course_table(TABLE_FORUM_MAIL_QUEUE);
  136. $table_thread_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY);
  137. $table_thread_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG);
  138. if ($this->type == 'full_backup') {
  139. $sql = "DELETE FROM ".$table_category." WHERE c_id = ".$this->course_id;
  140. Database::query($sql);
  141. $sql = "DELETE FROM ".$table_forum." WHERE c_id = ".$this->course_id;
  142. Database::query($sql);
  143. $sql = "DELETE FROM ".$table_thread." WHERE c_id = ".$this->course_id;
  144. Database::query($sql);
  145. $sql = "DELETE FROM ".$table_post." WHERE c_id = ".$this->course_id;
  146. Database::query($sql);
  147. $sql = "DELETE FROM ".$table_attachment." WHERE c_id = ".$this->course_id;
  148. Database::query($sql);
  149. $sql = "DELETE FROM ".$table_notification." WHERE c_id = ".$this->course_id;
  150. Database::query($sql);
  151. $sql = "DELETE FROM ".$table_mail_queue." WHERE c_id = ".$this->course_id;
  152. Database::query($sql);
  153. $sql = "DELETE FROM ".$table_thread_qualify." WHERE c_id = ".$this->course_id;
  154. Database::query($sql);
  155. $sql = "DELETE FROM ".$table_thread_qualify_log." WHERE c_id = ".$this->course_id;
  156. Database::query($sql);
  157. $sql = "DELETE FROM ".$table_thread_qualify_log." WHERE c_id = ".$this->course_id;
  158. Database::query($sql);
  159. }
  160. if ($this->course->has_resources(RESOURCE_FORUMCATEGORY)) {
  161. $forum_ids = implode(',', (array_keys($this->course->resources[RESOURCE_FORUMCATEGORY])));
  162. $sql = "DELETE FROM ".$table_category." WHERE c_id = ".$this->course_id." AND cat_id IN(".$forum_ids.");";
  163. Database::query($sql);
  164. }
  165. if ($this->course->has_resources(RESOURCE_FORUM)) {
  166. $forum_ids = implode(',', (array_keys($this->course->resources[RESOURCE_FORUM])));
  167. $sql = "DELETE FROM $table_attachment USING $table_attachment
  168. INNER JOIN $table_post
  169. WHERE ".$table_post.".c_id = ".$this->course_id." AND
  170. ".$table_attachment.".c_id = ".$this->course_id." AND
  171. ".$table_attachment.".post_id = ".$table_post.".post_id".
  172. " AND ".$table_post.".forum_id IN(".$forum_ids.");";
  173. Database::query($sql);
  174. $sql = "DELETE FROM ".$table_mail_queue." USING ".$table_mail_queue." INNER JOIN ".$table_post.
  175. " WHERE ".$table_post.".c_id = ".$this->course_id." AND ".$table_mail_queue.".c_id = ".$this->course_id." AND ".$table_mail_queue.".post_id = ".$table_post.".post_id".
  176. " AND ".$table_post.".forum_id IN(".$forum_ids.");";
  177. Database::query($sql);
  178. // Just in case, deleting in the same table using thread_id as record-linker.
  179. $sql = "DELETE FROM ".$table_mail_queue.
  180. " USING ".$table_mail_queue." INNER JOIN ".$table_thread.
  181. " WHERE $table_mail_queue.c_id = ".$this->course_id." AND $table_thread.c_id = ".$this->course_id." AND ".$table_mail_queue.".thread_id = ".$table_thread.".thread_id".
  182. " AND ".$table_thread.".forum_id IN(".$forum_ids.");";
  183. Database::query($sql);
  184. $sql = "DELETE FROM ".$table_thread_qualify.
  185. " USING ".$table_thread_qualify." INNER JOIN ".$table_thread.
  186. " WHERE $table_thread_qualify.c_id = ".$this->course_id." AND $table_thread.c_id = ".$this->course_id." AND ".$table_thread_qualify.".thread_id = ".$table_thread.".thread_id".
  187. " AND ".$table_thread.".forum_id IN(".$forum_ids.");";
  188. Database::query($sql);
  189. $sql = "DELETE FROM ".$table_thread_qualify_log.
  190. " USING ".$table_thread_qualify_log." INNER JOIN ".$table_thread.
  191. " WHERE $table_thread_qualify_log.c_id = ".$this->course_id." AND $table_thread.c_id = ".$this->course_id." AND ".$table_thread_qualify_log.".thread_id = ".$table_thread.".thread_id".
  192. " AND ".$table_thread.".forum_id IN(".$forum_ids.");";
  193. Database::query($sql);
  194. $sql = "DELETE FROM ".$table_notification." WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
  195. Database::query($sql);
  196. $sql = "DELETE FROM ".$table_post." WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
  197. Database::query($sql);
  198. $sql = "DELETE FROM ".$table_thread." WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
  199. Database::query($sql);
  200. $sql = "DELETE FROM ".$table_forum." WHERE c_id = ".$this->course_id." AND forum_id IN(".$forum_ids.")";
  201. Database::query($sql);
  202. }
  203. }
  204. /**
  205. * Delete forum-categories
  206. * Deletes all forum-categories from current course without forums
  207. */
  208. function recycle_forum_categories() {
  209. $table_forum = Database :: get_course_table(TABLE_FORUM);
  210. $table_forumcat = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  211. $sql = "SELECT fc.cat_id FROM ".$table_forumcat." fc
  212. LEFT JOIN ".$table_forum." f ON fc.cat_id=f.forum_category AND fc.c_id = ".$this->course_id." AND f.c_id = ".$this->course_id."
  213. WHERE f.forum_id IS NULL";
  214. $res = Database::query($sql);
  215. while ($obj = Database::fetch_object($res)) {
  216. $sql = "DELETE FROM ".$table_forumcat." WHERE c_id = ".$this->course_id." AND cat_id = ".$obj->cat_id;
  217. Database::query($sql);
  218. }
  219. }
  220. /**
  221. * Delete link-categories
  222. * Deletes all empty link-categories (=without links) from current course
  223. */
  224. function recycle_link_categories() {
  225. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  226. $link_table = Database :: get_course_table(TABLE_LINK);
  227. $sql = "SELECT lc.id FROM ".$link_cat_table." lc
  228. LEFT JOIN ".$link_table." l
  229. ON lc.id=l.category_id AND lc.c_id = ".$this->course_id." AND l.c_id = ".$this->course_id."
  230. WHERE l.id IS NULL";
  231. $res = Database::query($sql);
  232. while ($obj = Database::fetch_object($res)) {
  233. $sql = "DELETE FROM ".$link_cat_table." WHERE c_id = ".$this->course_id." AND id = ".$obj->id;
  234. Database::query($sql);
  235. }
  236. }
  237. /**
  238. * Delete events
  239. */
  240. function recycle_events() {
  241. if ($this->course->has_resources(RESOURCE_EVENT)) {
  242. $table = Database :: get_course_table(TABLE_AGENDA);
  243. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  244. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_EVENT])));
  245. $sql = "DELETE FROM ".$table." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  246. Database::query($sql);
  247. $sql = "DELETE FROM ".$table_attachment." WHERE c_id = ".$this->course_id." AND agenda_id IN(".$ids.")";
  248. Database::query($sql);
  249. }
  250. }
  251. /**
  252. * Delete announcements
  253. */
  254. function recycle_announcements() {
  255. if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT)) {
  256. $table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
  257. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  258. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_ANNOUNCEMENT])));
  259. $sql = "DELETE FROM ".$table." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  260. Database::query($sql);
  261. $sql = "DELETE FROM ".$table_attachment." WHERE c_id = ".$this->course_id." AND announcement_id IN(".$ids.")";
  262. Database::query($sql);
  263. }
  264. }
  265. /**
  266. * Recycle quizzes - doesn't remove the questions and their answers, as they might still be used later
  267. */
  268. function recycle_quizzes() {
  269. if ($this->course->has_resources(RESOURCE_QUIZ)) {
  270. $table_qui_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  271. $table_qui_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  272. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  273. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  274. $table_qui_que_opt = Database :: get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  275. $table_qui_que_cat = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  276. $table_qui_que_rel_cat = Database :: get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  277. $ids = array_keys($this->course->resources[RESOURCE_QUIZ]);
  278. // If the value "-1" is in the ids of elements (questions) to
  279. // be deleted, then consider all orphan questions should be deleted
  280. // This value is set in CourseBuilder::quiz_build_questions()
  281. $delete_orphan_questions = in_array(-1, $ids);
  282. $ids = implode(',', $ids);
  283. // Deletion of the tests first. Questions in these tests are
  284. // not deleted and become orphan at this point
  285. $sql = "DELETE FROM ".$table_qui." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  286. Database::query($sql);
  287. $sql = "DELETE FROM ".$table_rel." WHERE c_id = ".$this->course_id." AND exercice_id IN(".$ids.")";
  288. Database::query($sql);
  289. // Identifying again and deletion of the orphan questions, if it was desired.
  290. if ($delete_orphan_questions) {
  291. $sql = 'SELECT questions.id '.
  292. ' FROM '.$table_qui_que.' as questions '.
  293. ' LEFT JOIN '.$table_rel.' as quizz_questions '.
  294. ' ON questions.id=quizz_questions.question_id '.
  295. ' LEFT JOIN '.$table_qui.' as exercices '.
  296. ' ON exercice_id=exercices.id '.
  297. ' WHERE '.
  298. ' questions.c_id = '.$this->course_id.' AND '.
  299. ' quizz_questions.c_id = '.$this->course_id.' AND '.
  300. ' exercices.c_id = '.$this->course_id.' AND '.
  301. ' quizz_questions.exercice_id IS NULL OR '.
  302. ' exercices.active = -1';
  303. // active = -1 means "deleted" test.
  304. $db_result = Database::query($sql);
  305. if (Database::num_rows($db_result) > 0) {
  306. $orphan_ids = array();
  307. while ($obj = Database::fetch_object($db_result)) {
  308. $orphan_ids[] = $obj->id;
  309. }
  310. $orphan_ids = implode(',', $orphan_ids);
  311. $sql = "DELETE FROM ".$table_rel." WHERE c_id = ".$this->course_id." AND question_id IN(".$orphan_ids.")";
  312. Database::query($sql);
  313. $sql = "DELETE FROM ".$table_qui_ans." WHERE c_id = ".$this->course_id." AND question_id IN(".$orphan_ids.")";
  314. Database::query($sql);
  315. $sql = "DELETE FROM ".$table_qui_que." WHERE c_id = ".$this->course_id." AND id IN(".$orphan_ids.")";
  316. Database::query($sql);
  317. }
  318. // Also delete questions categories and options
  319. $sql = "DELETE FROM $table_qui_que_rel_cat WHERE c_id = ".$this->course_id;
  320. Database::query($sql);
  321. $sql = "DELETE FROM $table_qui_que_cat WHERE c_id = ".$this->course_id;
  322. Database::query($sql);
  323. $sql = "DELETE FROM $table_qui_que_opt WHERE c_id = ".$this->course_id;
  324. Database::query($sql);
  325. }
  326. // Quizzes previously deleted are, in fact, kept with a status
  327. // (active field) of "-1". Delete those, now.
  328. $sql = "DELETE FROM ".$table_qui." WHERE c_id = ".$this->course_id." AND active = -1";
  329. Database::query($sql);
  330. }
  331. }
  332. /**
  333. * Recycle surveys - removes everything
  334. */
  335. function recycle_surveys() {
  336. if ($this->course->has_resources(RESOURCE_SURVEY)) {
  337. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  338. $table_survey_q = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  339. $table_survey_q_o = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  340. $table_survey_a = Database :: get_course_Table(TABLE_SURVEY_ANSWER);
  341. $table_survey_i = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  342. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_SURVEY])));
  343. $sql = "DELETE FROM ".$table_survey_i." WHERE c_id = ".$this->course_id." ";
  344. Database::query($sql);
  345. $sql = "DELETE FROM ".$table_survey_a." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  346. Database::query($sql);
  347. $sql = "DELETE FROM ".$table_survey_q_o." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  348. Database::query($sql);
  349. $sql = "DELETE FROM ".$table_survey_q." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  350. Database::query($sql);
  351. $sql = "DELETE FROM ".$table_survey." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  352. Database::query($sql);
  353. }
  354. }
  355. /**
  356. * Recycle learnpaths
  357. */
  358. function recycle_learnpaths() {
  359. if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
  360. $table_main = Database :: get_course_table(TABLE_LP_MAIN);
  361. $table_item = Database :: get_course_table(TABLE_LP_ITEM);
  362. $table_view = Database :: get_course_table(TABLE_LP_VIEW);
  363. $table_iv = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  364. $table_iv_int = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  365. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  366. foreach($this->course->resources[RESOURCE_LEARNPATH] as $id => $learnpath) {
  367. // See task #875.
  368. if ($learnpath->lp_type == 2) {
  369. // This is a learning path of SCORM type.
  370. if (trim($learnpath->path) != '') // A sanity check for avoiding removal of the parent folder scorm/
  371. // when $learnpath->path value is incorrect for some reason.
  372. {
  373. // The directory trat contains files of the SCORM package is to be deleted.
  374. $scorm_package_dir = realpath($this->course->path . 'scorm/' . $learnpath->path);
  375. rmdirr($scorm_package_dir);
  376. }
  377. }
  378. //remove links from course homepage
  379. $sql = "DELETE FROM $table_tool WHERE c_id = ".$this->course_id." AND link LIKE '%lp_controller.php%lp_id=$id%' AND image='scormbuilder.gif'";
  380. Database::query($sql);
  381. //remove elements from lp_* tables (from bottom-up) by removing interactions, then item_view, then views and items, then paths
  382. $sql_items = "SELECT id FROM $table_item WHERE c_id = ".$this->course_id." AND lp_id=$id";
  383. $res_items = Database::query($sql_items);
  384. while ($row_item = Database::fetch_array($res_items)) {
  385. //get item views
  386. $sql_iv = "SELECT id FROM $table_iv WHERE c_id = ".$this->course_id." AND lp_item_id=".$row_item['id'];
  387. $res_iv = Database::query($sql_iv);
  388. while ($row_iv = Database::fetch_array($res_iv)) {
  389. //delete interactions
  390. $sql_iv_int_del = "DELETE FROM $table_iv_int WHERE c_id = ".$this->course_id." AND lp_iv_id = ".$row_iv['id'];
  391. $res_iv_int_del = Database::query($sql_iv_int_del);
  392. }
  393. //delete item views
  394. $sql_iv_del = "DELETE FROM $table_iv WHERE c_id = ".$this->course_id." AND lp_item_id=".$row_item['id'];
  395. $res_iv_del = Database::query($sql_iv_del);
  396. }
  397. //delete items
  398. $sql_items_del = "DELETE FROM $table_item WHERE c_id = ".$this->course_id." AND lp_id=$id";
  399. $res_items_del = Database::query($sql_items_del);
  400. //delete views
  401. $sql_views_del = "DELETE FROM $table_view WHERE c_id = ".$this->course_id." AND lp_id=$id";
  402. $res_views_del = Database::query($sql_views_del);
  403. //delete lps
  404. $sql_del = "DELETE FROM $table_main WHERE c_id = ".$this->course_id." AND id = $id";
  405. $res_del = Database::query($sql_del);
  406. }
  407. }
  408. }
  409. /**
  410. * Delete course description
  411. */
  412. function recycle_cours_description() {
  413. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION)) {
  414. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  415. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_COURSEDESCRIPTION])));
  416. $sql = "DELETE FROM ".$table." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  417. Database::query($sql);
  418. }
  419. }
  420. /**
  421. * Recycle Thematics
  422. */
  423. function recycle_thematic($session_id = 0) {
  424. if ($this->course->has_resources(RESOURCE_THEMATIC)) {
  425. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  426. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  427. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  428. $resources = $this->course->resources;
  429. foreach ($resources[RESOURCE_THEMATIC] as $last_id => $thematic) {
  430. if (is_numeric($last_id)) {
  431. foreach($thematic->thematic_advance_list as $thematic_advance) {
  432. $cond = array('id = ? AND c_id = ?'=>array($thematic_advance['id'], $this->course_id));
  433. api_item_property_update($this->course_info, 'thematic_advance', $thematic_advance['id'],'ThematicAdvanceDeleted', api_get_user_id());
  434. Database::delete($table_thematic_advance, $cond);
  435. }
  436. foreach($thematic->thematic_plan_list as $thematic_plan) {
  437. $cond = array('id = ? AND c_id = ?'=>array($thematic_plan['id'], $this->course_id));
  438. api_item_property_update($this->course_info, 'thematic_plan', $thematic_advance['id'], 'ThematicPlanDeleted', api_get_user_id());
  439. Database::delete($table_thematic_plan, $cond);
  440. }
  441. $cond = array('id = ? AND c_id = ?'=>array($last_id, $this->course_id));
  442. api_item_property_update($this->course_info, 'thematic', $last_id,'ThematicDeleted', api_get_user_id());
  443. Database::delete($table_thematic,$cond);
  444. }
  445. }
  446. }
  447. }
  448. /**
  449. * Recycle Attendances
  450. */
  451. function recycle_attendance($session_id = 0) {
  452. if ($this->course->has_resources(RESOURCE_ATTENDANCE)) {
  453. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  454. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  455. $resources = $this->course->resources;
  456. foreach ($resources[RESOURCE_ATTENDANCE] as $last_id => $obj) {
  457. if (is_numeric($last_id)) {
  458. foreach($obj->attendance_calendar as $attendance_calendar) {
  459. $cond = array('id = ? AND c_id = ? '=>array($attendance_calendar['id'], $this->course_id));
  460. Database::delete($table_attendance_calendar, $cond);
  461. }
  462. $cond = array('id = ? AND c_id = ?'=>array($last_id, $this->course_id));
  463. Database::delete($table_attendance, $cond);
  464. api_item_property_update($this->course_info, TOOL_ATTENDANCE, $last_id,'AttendanceDeleted', api_get_user_id());
  465. }
  466. }
  467. }
  468. }
  469. }