CourseRecycler.class.php 30 KB

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