CourseRecycler.class.php 32 KB

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