CourseRecycler.class.php 29 KB

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