CourseRecycler.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. 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. if ($this->course->has_resources(RESOURCE_QUIZ)) {
  270. $table_qui_que = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  271. $table_qui_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  272. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  273. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  274. $table_qui_que_opt = Database :: get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  275. $table_qui_que_cat = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  276. $table_qui_que_rel_cat = Database :: get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  277. $ids = array_keys($this->course->resources[RESOURCE_QUIZ]);
  278. $ids = array_filter($ids);
  279. // If the value "-1" is in the ids of elements (questions) to
  280. // be deleted, then consider all orphan questions should be deleted
  281. // This value is set in CourseBuilder::quiz_build_questions()
  282. $delete_orphan_questions = in_array(-1, $ids);
  283. $ids = implode(',', $ids);
  284. if (!empty($ids)) {
  285. // Deletion of the tests first. Questions in these tests are
  286. // not deleted and become orphan at this point
  287. $sql = "DELETE FROM ".$table_qui." WHERE c_id = ".$this->course_id." AND iid IN(".$ids.")";
  288. Database::query($sql);
  289. $sql = "DELETE FROM ".$table_rel." WHERE c_id = ".$this->course_id." AND exercice_id IN(".$ids.")";
  290. Database::query($sql);
  291. }
  292. // Identifying again and deletion of the orphan questions, if it was desired.
  293. if ($delete_orphan_questions) {
  294. /*$sql = 'SELECT questions.id '.
  295. ' FROM '.$table_qui_que.' as questions '.
  296. ' LEFT JOIN '.$table_rel.' as quizz_questions '.
  297. ' ON questions.id=quizz_questions.question_id '.
  298. ' LEFT JOIN '.$table_qui.' as exercices '.
  299. ' ON exercice_id=exercices.id '.
  300. ' WHERE '.
  301. ' questions.c_id = '.$this->course_id.' AND '.
  302. ' quizz_questions.c_id = '.$this->course_id.' AND '.
  303. ' exercices.c_id = '.$this->course_id.' AND '.
  304. ' quizz_questions.exercice_id IS NULL OR '.
  305. ' exercices.active = -1';
  306. // active = -1 means "deleted" test.
  307. */
  308. //@todo fix query in order to use iid
  309. $sql = " (
  310. SELECT q.id FROM $table_qui_que q
  311. INNER JOIN $table_rel r
  312. ON (q.c_id = r.c_id AND q.id = r.question_id)
  313. INNER JOIN $table_qui ex
  314. ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
  315. WHERE ex.c_id = ".$this->course_id." AND (ex.active = '-1' OR ex.id = '-1')
  316. )
  317. UNION
  318. (
  319. SELECT q.id FROM $table_qui_que q
  320. LEFT OUTER JOIN $table_rel r
  321. ON (q.c_id = r.c_id AND q.id = r.question_id)
  322. WHERE q.c_id = ".$this->course_id." AND r.question_id is null
  323. )
  324. UNION
  325. (
  326. SELECT q.id FROM $table_qui_que q
  327. INNER JOIN $table_rel r
  328. ON (q.c_id = r.c_id AND q.id = r.question_id)
  329. WHERE r.c_id = ".$this->course_id." AND r.exercice_id = '-1' OR r.exercice_id = '0'
  330. )";
  331. $db_result = Database::query($sql);
  332. if (Database::num_rows($db_result) > 0) {
  333. $orphan_ids = array();
  334. while ($obj = Database::fetch_object($db_result)) {
  335. $orphan_ids[] = $obj->iid;
  336. }
  337. $orphan_ids = implode(',', $orphan_ids);
  338. $sql = "DELETE FROM ".$table_rel." WHERE c_id = ".$this->course_id." AND question_id IN(".$orphan_ids.")";
  339. Database::query($sql);
  340. $sql = "DELETE FROM ".$table_qui_ans." WHERE c_id = ".$this->course_id." AND question_id IN(".$orphan_ids.")";
  341. Database::query($sql);
  342. $sql = "DELETE FROM ".$table_qui_que." WHERE c_id = ".$this->course_id." AND iid IN(".$orphan_ids.")";
  343. Database::query($sql);
  344. }
  345. // Also delete questions categories and options
  346. $sql = "DELETE FROM $table_qui_que_rel_cat WHERE c_id = ".$this->course_id;
  347. Database::query($sql);
  348. $sql = "DELETE FROM $table_qui_que_cat WHERE c_id = ".$this->course_id;
  349. Database::query($sql);
  350. $sql = "DELETE FROM $table_qui_que_opt WHERE c_id = ".$this->course_id;
  351. Database::query($sql);
  352. }
  353. // Quizzes previously deleted are, in fact, kept with a status
  354. // (active field) of "-1". Delete those, now.
  355. $sql = "DELETE FROM ".$table_qui." WHERE c_id = ".$this->course_id." AND active = -1";
  356. Database::query($sql);
  357. }
  358. }
  359. /**
  360. * Recycle surveys - removes everything
  361. */
  362. function recycle_surveys() {
  363. if ($this->course->has_resources(RESOURCE_SURVEY)) {
  364. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  365. $table_survey_q = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  366. $table_survey_q_o = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  367. $table_survey_a = Database :: get_course_Table(TABLE_SURVEY_ANSWER);
  368. $table_survey_i = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  369. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_SURVEY])));
  370. $sql = "DELETE FROM ".$table_survey_i." WHERE c_id = ".$this->course_id." ";
  371. Database::query($sql);
  372. $sql = "DELETE FROM ".$table_survey_a." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  373. Database::query($sql);
  374. $sql = "DELETE FROM ".$table_survey_q_o." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  375. Database::query($sql);
  376. $sql = "DELETE FROM ".$table_survey_q." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  377. Database::query($sql);
  378. $sql = "DELETE FROM ".$table_survey." WHERE c_id = ".$this->course_id." AND survey_id IN(".$ids.")";
  379. Database::query($sql);
  380. }
  381. }
  382. /**
  383. * Recycle learnpaths
  384. */
  385. function recycle_learnpaths() {
  386. if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
  387. $table_main = Database :: get_course_table(TABLE_LP_MAIN);
  388. $table_item = Database :: get_course_table(TABLE_LP_ITEM);
  389. $table_view = Database :: get_course_table(TABLE_LP_VIEW);
  390. $table_iv = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  391. $table_iv_int = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  392. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  393. foreach($this->course->resources[RESOURCE_LEARNPATH] as $id => $learnpath) {
  394. // See task #875.
  395. if ($learnpath->lp_type == 2) {
  396. // This is a learning path of SCORM type.
  397. if (trim($learnpath->path) != '') // A sanity check for avoiding removal of the parent folder scorm/
  398. // when $learnpath->path value is incorrect for some reason.
  399. {
  400. // The directory trat contains files of the SCORM package is to be deleted.
  401. $scorm_package_dir = realpath($this->course->path . 'scorm/' . $learnpath->path);
  402. rmdirr($scorm_package_dir);
  403. }
  404. }
  405. //remove links from course homepage
  406. $sql = "DELETE FROM $table_tool WHERE c_id = ".$this->course_id." AND link LIKE '%lp_controller.php%lp_id=$id%' AND image='scormbuilder.gif'";
  407. Database::query($sql);
  408. //remove elements from lp_* tables (from bottom-up) by removing interactions, then item_view, then views and items, then paths
  409. $sql_items = "SELECT id FROM $table_item WHERE c_id = ".$this->course_id." AND lp_id=$id";
  410. $res_items = Database::query($sql_items);
  411. while ($row_item = Database::fetch_array($res_items)) {
  412. //get item views
  413. $sql_iv = "SELECT id FROM $table_iv WHERE c_id = ".$this->course_id." AND lp_item_id=".$row_item['id'];
  414. $res_iv = Database::query($sql_iv);
  415. while ($row_iv = Database::fetch_array($res_iv)) {
  416. //delete interactions
  417. $sql_iv_int_del = "DELETE FROM $table_iv_int WHERE c_id = ".$this->course_id." AND lp_iv_id = ".$row_iv['id'];
  418. $res_iv_int_del = Database::query($sql_iv_int_del);
  419. }
  420. //delete item views
  421. $sql_iv_del = "DELETE FROM $table_iv WHERE c_id = ".$this->course_id." AND lp_item_id=".$row_item['id'];
  422. $res_iv_del = Database::query($sql_iv_del);
  423. }
  424. //delete items
  425. $sql_items_del = "DELETE FROM $table_item WHERE c_id = ".$this->course_id." AND lp_id=$id";
  426. $res_items_del = Database::query($sql_items_del);
  427. //delete views
  428. $sql_views_del = "DELETE FROM $table_view WHERE c_id = ".$this->course_id." AND lp_id=$id";
  429. $res_views_del = Database::query($sql_views_del);
  430. //delete lps
  431. $sql_del = "DELETE FROM $table_main WHERE c_id = ".$this->course_id." AND id = $id";
  432. $res_del = Database::query($sql_del);
  433. }
  434. }
  435. }
  436. /**
  437. * Delete course description
  438. */
  439. function recycle_cours_description() {
  440. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION)) {
  441. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  442. $ids = implode(',', (array_keys($this->course->resources[RESOURCE_COURSEDESCRIPTION])));
  443. $sql = "DELETE FROM ".$table." WHERE c_id = ".$this->course_id." AND id IN(".$ids.")";
  444. Database::query($sql);
  445. }
  446. }
  447. /**
  448. * Recycle Thematics
  449. */
  450. function recycle_thematic($session_id = 0) {
  451. if ($this->course->has_resources(RESOURCE_THEMATIC)) {
  452. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  453. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  454. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  455. $resources = $this->course->resources;
  456. foreach ($resources[RESOURCE_THEMATIC] as $last_id => $thematic) {
  457. if (is_numeric($last_id)) {
  458. foreach($thematic->thematic_advance_list as $thematic_advance) {
  459. $cond = array('id = ? AND c_id = ?'=>array($thematic_advance['id'], $this->course_id));
  460. api_item_property_update($this->course_info, 'thematic_advance', $thematic_advance['id'],'ThematicAdvanceDeleted', api_get_user_id());
  461. Database::delete($table_thematic_advance, $cond);
  462. }
  463. foreach($thematic->thematic_plan_list as $thematic_plan) {
  464. $cond = array('id = ? AND c_id = ?'=>array($thematic_plan['id'], $this->course_id));
  465. api_item_property_update($this->course_info, 'thematic_plan', $thematic_advance['id'], 'ThematicPlanDeleted', api_get_user_id());
  466. Database::delete($table_thematic_plan, $cond);
  467. }
  468. $cond = array('id = ? AND c_id = ?'=>array($last_id, $this->course_id));
  469. api_item_property_update($this->course_info, 'thematic', $last_id,'ThematicDeleted', api_get_user_id());
  470. Database::delete($table_thematic,$cond);
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * Recycle Attendances
  477. */
  478. function recycle_attendance($session_id = 0) {
  479. if ($this->course->has_resources(RESOURCE_ATTENDANCE)) {
  480. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  481. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  482. $resources = $this->course->resources;
  483. foreach ($resources[RESOURCE_ATTENDANCE] as $last_id => $obj) {
  484. if (is_numeric($last_id)) {
  485. foreach($obj->attendance_calendar as $attendance_calendar) {
  486. $cond = array('id = ? AND c_id = ? '=>array($attendance_calendar['id'], $this->course_id));
  487. Database::delete($table_attendance_calendar, $cond);
  488. }
  489. $cond = array('id = ? AND c_id = ?'=>array($last_id, $this->course_id));
  490. Database::delete($table_attendance, $cond);
  491. api_item_property_update($this->course_info, TOOL_ATTENDANCE, $last_id,'AttendanceDeleted', api_get_user_id());
  492. }
  493. }
  494. }
  495. }
  496. }