CourseRecycler.class.php 27 KB

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