CourseRecycler.class.php 27 KB

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