learnpath_functions.inc.php 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a function library for the learning path.
  5. *
  6. * Due to the face that the learning path has been built upon the resoucelinker,
  7. * naming conventions have changed at least 2 times. You can see here in order the :
  8. * 1. name used in the first version of the resourcelinker
  9. * 2. name used in the first version of the LP
  10. * 3. name used in the second (current) version of the LP
  11. *
  12. * 1. 2. 3.
  13. * Category = Chapter = Module
  14. * Item (?) = Item = Step
  15. *
  16. * @author Denes Nagy <darkden@evk.bke.hu>, main author
  17. * @author Roan Embrechts, some code cleaning
  18. * @author Yannick Warnier <yannick.warnier@beeznest.com>, multi-level learnpath behaviour + new SCORM tool
  19. * @access public
  20. * @package chamilo.learnpath
  21. * @todo rename functions to coding conventions: not deleteitem but delete_item, etc
  22. * @todo rewrite functions to comply with phpDocumentor
  23. * @todo remove code duplication
  24. */
  25. use \ChamiloSession as Session;
  26. /**
  27. * This function deletes an item
  28. * @param integer $id: the item we want to delete
  29. * @return boolean True if item was deleted, false if not found or error
  30. */
  31. function deleteitem($id) {
  32. $course_id = api_get_course_int_id();
  33. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  34. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  35. // Get the display order for this item before it is deleted.
  36. $sql = "SELECT display_order, parent_item_id FROM $tbl_lp_item WHERE c_id = $course_id AND id=$id";
  37. $result = Database::query($sql);
  38. if (Database::num_rows($result) == 0) {
  39. return false;
  40. }
  41. $row = Database::fetch_row($result);
  42. $display_order = $row[0];
  43. $parent_item_id = $row[1];
  44. // Delete the item.
  45. $sql = "DELETE FROM $tbl_learnpath_item WHERE c_id = $course_id AND id='$id'";
  46. $result = Database::query($sql);
  47. if ($result === false) {
  48. return false;
  49. }
  50. // Update the other items and chapters.
  51. $sql = "UPDATE $tbl_learnpath_item SET display_order = display_order-1
  52. WHERE c_id = $course_id AND display_order > $display_order AND parent_item_id = $parent_item_id";
  53. Database::query($sql);
  54. $sql = "UPDATE $tbl_learnpath_chapter SET display_order = display_order-1
  55. WHERE c_id = $course_id AND display_order > $display_order AND parent_item_id = $parent_item_id";
  56. Database::query($sql);
  57. return true;
  58. }
  59. /**
  60. * This function deletes a module(chapter) and all its steps(items).
  61. *
  62. * @param integer id of the chapter we want to delete
  63. * @return boolean True on success and false if not found or error
  64. */
  65. function deletemodule($parent_item_id) {
  66. global $learnpath_id;
  67. $course_id = api_get_course_int_id();
  68. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  69. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  70. // Added for multi-level behaviour - slightly recursive.
  71. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND lp_id=$learnpath_id";
  72. $result = Database::query($sql);
  73. while ($row = Database::fetch_array($result)) {
  74. if ($row['parent_item_id'] == $parent_item_id) {
  75. // Delete every subchapter.
  76. if (deletemodule($row['id']) === false) {
  77. return false;
  78. }
  79. }
  80. }
  81. // Get this chapter's display order.
  82. $sql = "SELECT display_order, parent_item_id FROM $tbl_learnpath_chapter
  83. WHERE c_id = $course_id AND id=$parent_item_id and lp_id=$learnpath_id";
  84. $result = Database::query($sql);
  85. if (Database::num_rows($result) == 0) {
  86. return false;
  87. }
  88. $row = Database::fetch_row($result);
  89. $display_order = $row[0];
  90. $parent_id = $row[1];
  91. // Delete the chapter itself.
  92. $sql = "DELETE FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (id=$parent_item_id and lp_id=$learnpath_id)";
  93. $result = Database::query($sql);
  94. // Delete items from that chapter.
  95. $sql2 = "DELETE FROM $tbl_learnpath_item WHERE c_id = $course_id AND parent_item_id=$parent_item_id";
  96. $result = Database::query($sql2);
  97. // Update all other chapters accordingly.
  98. $sql = "UPDATE $tbl_learnpath_item SET display_order = display_order-1 WHERE c_id = $course_id AND display_order > $display_order AND parent_item_id = $parent_id";
  99. $result = Database::query($sql);
  100. $sql = "UPDATE $tbl_learnpath_chapter SET display_order = display_order-1 WHERE c_id = $course_id AND display_order > $display_order AND parent_item_id = $parent_id";
  101. $result = Database::query($sql);
  102. return true;
  103. }
  104. /**
  105. * This function deletes an entire path.
  106. *
  107. * @param integer $id: the path we want to delete
  108. * @return void
  109. */
  110. function deletepath($path_id) {
  111. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  112. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  113. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  114. $course_id = api_get_course_int_id();
  115. $sql = "DELETE FROM $tbl_learnpath_main WHERE c_id = $course_id AND lp_id='$path_id'";
  116. $result = Database::query($sql);
  117. //@TODO check how this function is used before uncommenting the following
  118. //also delete all elements inside that path
  119. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND lp_id = $path_id";
  120. $result = Database::query($sql);
  121. while ($row = Database::fetch_array($result)) {
  122. deletemodule($row['id']);
  123. }
  124. }
  125. /**
  126. * This function moves an item.
  127. *
  128. * @param string $direction: move the given chapter up or down
  129. * @param integer Item ID
  130. * @param integer $moduleid: the id of the chapter the element resides in
  131. * @return boolean Returns false on error
  132. * @note With this new version, the moveitem deals with items AND directories (not the base-level modules). This is a lot more complicated but is a temporary step towards new database structure as 'everything is an item'
  133. */
  134. function moveitem($direction, $id, $moduleid, $type = 'item') {
  135. global $learnpath_id;
  136. $course_id = api_get_course_int_id();
  137. $tbl_learnpath_item = Database::get_course_table(TABLE_LEARNPATH_ITEM);
  138. $tbl_learnpath_chapter = Database::get_course_table(TABLE_LEARNPATH_CHAPTER);
  139. $tree = get_learnpath_tree($learnpath_id);
  140. $orig_order = 0;
  141. $orig_type = '';
  142. $orig_id = $id;
  143. foreach ($tree[$moduleid] as $row) {
  144. // If this is the element we want (be it a chapter or an item), get its data.
  145. if (($row['id'] == $id) && ($row['type'] == $type)) {
  146. $orig_order = $row['display_order'];
  147. $orig_type = $row['type'];
  148. break;
  149. }
  150. }
  151. $dest_order = 0;
  152. $dest_type = '';
  153. $dest_id = 0;
  154. if ($direction == 'up') {
  155. if (!empty ($tree[$moduleid][$orig_order - 1])) {
  156. $dest_order = $orig_order - 1;
  157. $dest_type = $tree[$moduleid][$orig_order - 1]['type'];
  158. $dest_id = $tree[$moduleid][$orig_order - 1]['id'];
  159. } else {
  160. return false;
  161. }
  162. } else {
  163. // Move down.
  164. if (!empty ($tree[$moduleid][$orig_order + 1])) {
  165. $dest_order = $orig_order + 1;
  166. $dest_type = $tree[$moduleid][$orig_order + 1]['type'];
  167. $dest_id = $tree[$moduleid][$orig_order + 1]['id'];
  168. } else {
  169. return false;
  170. }
  171. }
  172. $sql1 = '';
  173. $sql2 = '';
  174. if ($orig_type == 'chapter') {
  175. $sql1 = "UPDATE $tbl_learnpath_chapter SET display_order = ".$dest_order." WHERE c_id = $course_id AND (id=$orig_id and parent_item_id=$moduleid)";
  176. } elseif ($orig_type == 'item') {
  177. $sql1 = "UPDATE $tbl_learnpath_item SET display_order = ".$dest_order." WHERE c_id = $course_id AND (id=$orig_id and parent_item_id=$moduleid)";
  178. } else {
  179. return false;
  180. }
  181. if ($dest_type == 'chapter') {
  182. $sql2 = "UPDATE $tbl_learnpath_chapter SET display_order = ".$orig_order." WHERE c_id = $course_id AND (id='$dest_id' and parent_item_id=$moduleid)";
  183. } elseif ($dest_type == 'item') {
  184. $sql2 = "UPDATE $tbl_learnpath_item SET display_order = ".$orig_order." WHERE c_id = $course_id AND (id='$dest_id' and parent_item_id=$moduleid)";
  185. } else {
  186. return false;
  187. }
  188. Database::query($sql1);
  189. Database::query($sql2);
  190. }
  191. /**
  192. * This function moves a module (also called chapter or category).
  193. *
  194. * @param string $direction: move the given chapter up or down
  195. * @param integer $id: the id of the chapter we want to move
  196. * @return void
  197. */
  198. function movemodule($direction, $id) {
  199. global $learnpath_id;
  200. $course_id = api_get_course_int_id();
  201. $tbl_learnpath_chapter = Database::get_course_table(TABLE_LEARNPATH_CHAPTER);
  202. if ($direction == 'up') {
  203. $sortDirection = 'DESC';
  204. } else {
  205. $sortDirection = 'ASC';
  206. }
  207. // Select all chapters of first level (parent_item_id = 0).
  208. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (lp_id=$learnpath_id AND parent_item_id = 0) ORDER BY display_order $sortDirection";
  209. $result = Database::query($sql);
  210. $previousrow = '';
  211. // See similar comment in moveitem() function.
  212. // @TODO: this only works for chapters in multi-level mode. Why not gather
  213. // this function and moveitem to keep only one multi-uses function?
  214. while ($row = Database::fetch_array($result)) {
  215. // Step 2: Performing the move (only happens when passed trhough step 1 at least once).
  216. if (!empty ($this_cat_order)) {
  217. $next_cat_order = $row['display_order'];
  218. $next_cat_id = $row['id'];
  219. $sql1 = "UPDATE $tbl_learnpath_chapter SET display_order = '$next_cat_order' WHERE c_id = $course_id AND (id='$this_cat_id' and lp_id=$learnpath_id)";
  220. $sql2 = "UPDATE $tbl_learnpath_chapter SET display_order = '$this_cat_order' WHERE c_id = $course_id AND (id='$next_cat_id' and lp_id=$learnpath_id)";
  221. Database::query($sql1);
  222. Database::query($sql2);
  223. unset ($this_cat_order);
  224. unset ($next_cat_order);
  225. unset ($next_cat_id);
  226. break;
  227. }
  228. // Step 1: Looking for the order of the row we want to move.
  229. if ($row['id'] == $id) {
  230. $this_cat_order = $row['display_order'];
  231. $this_cat_id = $id;
  232. }
  233. }
  234. }
  235. /**
  236. * Inserts a new element in a learnpath table (item or chapter)
  237. * @param string Element type ('chapter' or 'item')
  238. * @param string Chapter name
  239. * @param string Chapter description (optional)
  240. * @param integer Parent chapter ID (default: 0)
  241. * @param integer Learnpath ID
  242. * @param mixed If type 'item', then array(prereq_id=>value, prereq_..)
  243. * @return integer The new chapter ID, or false on failure
  244. * @TODO Finish this function before it is used. Currently only chapters can be added using it.
  245. * @note This function is currently never used!
  246. */
  247. function insert_item($type = 'item', $name, $chapter_description = '', $parent_id = 0, $learnpath_id = 0, $params = null) {
  248. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  249. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  250. $course_id = api_get_course_int_id();
  251. // Getting the last order number from the chapters table, in this learnpath, for the parent chapter given.
  252. $sql = "SELECT * FROM $tbl_learnpath_chapter
  253. WHERE c_id = $course_id AND lp_id=$learnpath_id AND parent_item_id = $parent_id
  254. ORDER BY display_order DESC";
  255. $result = Database::query($sql);
  256. $row = Database::fetch_array($result);
  257. $last_chapter_order = $row['display_order'];
  258. // Getting the last order number of the items.
  259. $sql = "SELECT * FROM $tbl_learnpath_item
  260. WHERE c_id = $course_id AND parent_item_id = $parent_id
  261. ORDER BY display_order DESC";
  262. $result = Database::query($sql);
  263. $row = Database::fetch_array($result);
  264. $last_item_order = $row['display_order'];
  265. $new_order = max($last_chapter_order, $last_item_order) + 1;
  266. if ($type === 'chapter') {
  267. $sql = "INSERT INTO $tbl_learnpath_chapter (c_id, lp_id, chapter_name, chapter_description, display_order)
  268. VALUES ( $course_id,
  269. '".domesticate($learnpath_id)."',
  270. '".domesticate(htmlspecialchars($name))."',
  271. '".domesticate(htmlspecialchars($chapter_description))."',
  272. $new_order )";
  273. $result = Database::query($sql);
  274. if ($result === false) {
  275. return false;
  276. }
  277. $id = Database :: insert_id();
  278. } elseif ($type === 'item') {
  279. $sql = "INSERT INTO $tbl_learnpath_item (c_id, parent_item_id, item_type, display_order) VALUES
  280. ($course_id, '".domesticate($parent_id)."','".domesticate(htmlspecialchars($type))."', $new_order )";
  281. $result = Database::query($sql);
  282. if ($result === false) {
  283. return false;
  284. }
  285. $id = Database :: insert_id();
  286. }
  287. return $id;
  288. }
  289. /**
  290. * This function returns an array with all the learnpath categories/chapters
  291. * @return array List of learnpath chapter titles
  292. */
  293. function array_learnpath_categories() {
  294. $course_id = api_get_course_int_id();
  295. global $learnpath_id;
  296. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  297. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (lp_id=$learnpath_id) ORDER BY display_order ASC";
  298. $result = Database::query($sql);
  299. while ($row = Database::fetch_array($result)) {
  300. $array_learnpath_categories[] = array($row['id'], $row['chapter_name']);
  301. }
  302. //$array_learnpath_categories = array($array_learnpath_categories_name, $array_learnpath_categories_id);
  303. return $array_learnpath_categories;
  304. }
  305. /**
  306. * Displays the learnpath chapters(=modules,categories) and their contents.
  307. * @param integer Chapter ID to display now (enables recursive behaviour)
  308. * @param array The array as returned by get_learnpath_tree, with all the elements of a learnpath compiled and structured into the array, by chapter id
  309. * @param integer Level (the depth of the call - helps in display)
  310. * @todo eliminate all global $lang declarations, use get_lang, improve structure.
  311. * @author Denes Nagy
  312. * @author Roan Embrechts
  313. * @author Yannick Warnier <yannick.warnier@beeznest.com> - complete redesign for multi-level learnpath chapters
  314. */
  315. function display_learnpath_chapters($parent_item_id = 0, $tree = array (), $level = 0) {
  316. //error_log('New LP - In learnpath_functions::display_learnpath_chapters', 0);
  317. global $color2;
  318. global $xml_output;
  319. global $learnpath_id;
  320. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  321. // @todo: coding standards: Language variables are CaMMelCaSe, all other variables should use the underscoring method.
  322. $lg_move_down = get_lang('LearnpathMoveDown');
  323. $lg_move_up = get_lang('LearnpathMoveUp');
  324. $lg_edit_learnpath_module = get_lang('LearnpathEditModule');
  325. $lg_delete_learnpath_module = get_lang('LearnpathDeleteModule');
  326. $lg_nochapters = get_lang('LearnpathNoChapters');
  327. $lg_prerequisites = get_lang('LearnpathPrerequisites');
  328. $lg_prerequisites_limit = get_lang('LearnpathPrerequisitesLimit');
  329. $lg_add_learnpath_item = get_lang('LearnpathAddItem');
  330. $lg_title_and_desc = get_lang('LearnpathTitleAndDesc');
  331. $lg_change_order = get_lang('LearnpathChangeOrder');
  332. $lg_add_prereqi = get_lang('LearnpathAddPrereqi');
  333. $lg_add_title_and_desc = get_lang('LearnpathAddTitleAndDesc');
  334. $lg_delete = get_lang('Delete');
  335. if ($parent_item_id === 0) {
  336. // This is the first time we use the function, define the tree and display learnpath name.
  337. $tree = get_learnpath_tree($learnpath_id);
  338. $num_modules = count($tree);
  339. //$num_modules = Database::num_rows($result);
  340. if ($num_modules == 0) {
  341. // do not diplay useless information
  342. //echo "<tr><td>&nbsp;$lg_nochapters</td></tr>";
  343. } else {
  344. echo " <tr align='center' valign='top'><td><b>&nbsp;$lg_title_and_desc </b></td>\n"." <td><b>&nbsp;$lg_add_learnpath_item </b></td>\n";
  345. if (is_prereq($learnpath_id)) {
  346. echo " <td bgcolor='#ddddee'><b>&nbsp;$lg_prerequisites_limit </b></td>\n";
  347. } else {
  348. echo " <td><b>&nbsp;$lg_prerequisites </b></td>\n";
  349. }
  350. echo " <td colspan='2'><b>&nbsp;$lg_change_order </b></td><td><b>&nbsp;$lg_add_prereqi </b></td>\n"." <td><b>&nbsp;$lg_add_title_and_desc </b></td><td><b>&nbsp;$lg_delete </b></td>\n"." </tr>\n";
  351. }
  352. }
  353. $i = 1;
  354. $counter = 0;
  355. $num_modules = count($tree[$parent_item_id]);
  356. //while ($row = Database::fetch_array($result))
  357. if (isset ($tree[$parent_item_id])) {
  358. foreach ($tree[$parent_item_id] as $row) {
  359. if ($row['item_type'] === 'dokeos_chapter') {
  360. $xml_output .= "<chapter>";
  361. $xml_output .= "<chaptertitle>".$row['title']."</chaptertitle>";
  362. $xml_output .= "<chapterdescription>".$row['description']."</chapterdescription>";
  363. $counter ++;
  364. if (($counter % 2) == 0) {
  365. $oddclass = 'row_odd';
  366. } else {
  367. $oddclass = 'row_even';
  368. }
  369. //echo '<tr class="'.$oddclass.'">'."\n".' <td>'.str_repeat("&nbsp;&gt;", $level)."<img src='../img/documents.gif' alt='folder'/><a href='".api_get_self()."?lp_id=$learnpath_id&item_id={$row['id']}&action=add&type=learnpathitem&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9'><b>&nbsp;".$row['title']."</b></a>"."<br /><i><div align='justify'>&nbsp;".str_repeat("&nbsp;&nbsp;&nbsp;", $level)."</i></td>\n".' <td align="center"><a href="'.api_get_self()."?lp_id=$learnpath_id&item_id={$row['id']}&action=add&type=learnpathitem&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9"><img src='../img/0.gif' width='13' height='13' border='0' title='$lg_add_learnpath_item'></a></td>\n"." <td";
  370. echo '<tr class="'.$oddclass.'">'."\n".' <td>'.str_repeat("&nbsp;&gt;", $level)."<img src='../img/documents.gif' alt='folder'/><a href='".api_get_self()."?lp_id=$learnpath_id&parent_item_id=".$row['id']."&action=add_sub_item'><b>&nbsp;".$row['title']."</b></a>"."<br /><i><div align='justify'>&nbsp;".str_repeat("&nbsp;&nbsp;&nbsp;", $level)."</i></td>\n".' <td align="center"><a href="'.api_get_self()."?lp_id=$learnpath_id&parent_item_id=".$row['id']."&action=add_sub_item\"><img src='../img/0.gif' width='13' height='13' border='0' title='$lg_add_learnpath_item'></a></td>\n"." <td";
  371. if (is_prereq($learnpath_id)) {
  372. echo " bgcolor='#ddddee'";
  373. }
  374. echo ">".$row['prerequisite']."</td>\n";
  375. // Showing the edit, delete and move icons.
  376. if (api_is_allowed_to_edit()) {
  377. $myaction = 'move_item';
  378. if ($i < $num_modules) {
  379. // If we are still under the number of chapters in this section, show "move down".
  380. //echo " <td align=center>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=".$myaction."&amp;direction=down&amp;moduleid=".$parent_item_id."&amp;id=".$row['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/down.gif\" border=\"0\" title=\"$lg_move_down\">"."</a></td>\n";
  381. echo " <td align=center>"."<a href='".api_get_self()."?lp_id=$learnpath_id&action=".$myaction."&direction=down&moduleid=".$parent_item_id."&id=".$row['id']."'>"."<img src=\"../img/down.gif\" border=\"0\" title=\"$lg_move_down\">"."</a></td>\n";
  382. } else {
  383. echo ' <td align="center">&nbsp;</td>'."\n";
  384. }
  385. if ($i > 1) {
  386. //echo ' <td align="center">'."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=".$myaction."&amp;direction=up&amp;moduleid=".$parent_item_id."&amp;id=".$row['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/up.gif\" border=\"0\" title=\"$lg_move_up\">"."</a>"."</td>\n";
  387. echo ' <td align="center">'."<a href='".api_get_self()."?lp_id=$learnpath_id&action=".$myaction."&direction=up&moduleid=".$parent_item_id."&id=".$row['id']."'>"."<img src=\"../img/up.gif\" border=\"0\" title=\"$lg_move_up\">"."</a>"."</td>\n";
  388. } else {
  389. echo ' <td align="center">&nbsp;</td>'."\n";
  390. }
  391. echo " <td align='center'>&nbsp;</td>\n";
  392. echo " <td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&action=edititem&id=".$row['id']."'>"."<img src=\"../img/edit.gif\" border=\"0\" title=\"$lg_edit_learnpath_module\">"."</a>"."</td>\n";
  393. echo " <td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&action=delete_item&id=".$row['id']."' onclick=\"javascript: return confirmation('".$row['title']."');\">"."<img src=\"../img/delete.gif\" border=\"0\" title=\"$lg_delete_learnpath_module\">"."</a>"."</td>\n";
  394. }
  395. echo "</tr>\n";
  396. $i ++;
  397. $xml_output .= "<items>";
  398. //display_learnpath_items($row['id']);
  399. display_learnpath_chapters($row['id'], $tree, $level + 1);
  400. $xml_output .= "</items>";
  401. $xml_output .= "</chapter>";
  402. } else //if //($row['item_type'] === 'item')
  403. {
  404. $row_items = $row;
  405. echo "<tr>\n <td colspan='2' valign='top'>";
  406. //require 'resourcelinker.inc.php';
  407. display_addedresource_link_in_learnpath($row_items['item_type'], $row_items['ref'], '', $row_items['id'], 'builder', 'icon', $level);
  408. if ($row_items['description']) {
  409. echo "<div align='justify'>&nbsp;&nbsp;&nbsp;{$row_items['description']}";
  410. }
  411. echo "</td>";
  412. if (is_prereq($learnpath_id)) {
  413. echo '<td bgcolor="#EEEEFF">';
  414. } else {
  415. echo "<td>";
  416. }
  417. if (api_is_allowed_to_edit()) {
  418. if ($row_items['prerequisite'] != '') {
  419. $prereq = $row_items['prerequisite'];
  420. //if ($row_items['prereq_type'] == 'i') {
  421. // item
  422. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE id='$prereq'"; // Check if prereq has been deleted.
  423. $result_items2 = Database::query($sql_items2);
  424. $number_items2 = Database::num_rows($result_items2);
  425. if ($number_items2 == 0) {
  426. echo get_lang('PrerequisiteDeletedError');
  427. }
  428. $row_items2 = Database::fetch_array($result_items2);
  429. display_addedresource_link_in_learnpath($row_items2['item_type'], $row_items2['ref'], '', $row_items2['id'], 'builder', '', 0);
  430. if ((($row_items2['item_type'] == TOOL_QUIZ) or ($row_items2['item_type'] == 'HotPotatoes')) and ($row_items['prerequisite'])) {
  431. //echo "&nbsp;({$row_items2['title']})";
  432. }
  433. //}
  434. /*
  435. if ($row_items['prereq_type'] == 'c') {
  436. // chapter
  437. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE id='$prereq' AND item_type='dokeos_chapter'"; // Check if prereq has been deleted.
  438. $result_items2 = Database::query($sql_items2);
  439. $number_items2 = Database::num_rows($result_items2);
  440. if ($number_items2 == 0) {
  441. echo "<font color='red'>$lg_prereq_deleted_error</font>";
  442. }
  443. $row_items2 = Database::fetch_array($result_items2);
  444. echo " {$row_items2['title']}";
  445. }*/
  446. }
  447. echo "</font></td>";
  448. $xml_output .= "<element_type>".$row_items['item_type']."</element_type>";
  449. $xml_output .= "<element_id>".$row_items['item_id']."</element_id>";
  450. // Move
  451. if ($i < $num_modules) {
  452. echo "<td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=moveitem&amp;type=item&amp;direction=down&amp;moduleid=".$parent_item_id."&amp;id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/down.gif\" border=\"0\" title=\"$lg_move_down\">"."</a>"."</td>";
  453. } else {
  454. echo "<td width='30' align='center'>&nbsp;</td>";
  455. }
  456. if ($i > 1) {
  457. echo "<td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=moveitem&amp;type=item&amp;direction=up&amp;moduleid=".$parent_item_id."&amp;id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/up.gif\" border=\"0\" title=\"$lg_move_up\">"."</a>";
  458. } else {
  459. echo "<td width='30' align='center'>&nbsp;</td>";
  460. }
  461. echo "</td>"."<td align='center'>";
  462. // Edit prereq
  463. echo "<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=edititemprereq&amp;id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/scormpre.gif\" border=\"0\" title=\"$lg_add_prereq\">"."</a>"."</td>";
  464. // Edit
  465. echo "<td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=edititem&amp;id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/edit.gif\" border=\"0\" title=\"$lg_edit_learnpath_item\">"."</a>"."</td>";
  466. // Delete
  467. echo "<td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&action=deleteitem&id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/delete.gif\" border=\"0\" title=\"$lg_delete_learnpath_item\" onclick=\"javascript: return confirmation('".$row_items['item_type']."');\">"."</a>";
  468. }
  469. $i ++;
  470. echo "</td></tr>";
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * Displays the learning path items/steps.
  477. * @param integer Category ID
  478. * @return void
  479. * @todo eliminate all global $lang declarations, use get_lang, improve structure.
  480. */
  481. function display_learnpath_items($categoryid) {
  482. global $xml_output;
  483. global $lg_prerequisites, $lg_move_down, $lg_move_up, $lg_edit_learnpath_item, $lg_delete_learnpath_item, $learnpath_id, $lg_add_prereq, $lg_prereq_deleted_error, $lg_pre_short, $langThisItem;
  484. $course_id = api_get_course_int_id();
  485. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  486. $sql_items = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND parent_item_id='$categoryid' ORDER BY display_order ASC";
  487. $result_items = Database::query($sql_items);
  488. $number_items = Database::num_rows($result_items);
  489. $i = 1;
  490. while ($row_items = Database::fetch_array($result_items)) {
  491. echo "<tr><td colspan='2' valign='top'>";
  492. display_addedresource_link_in_learnpath($row_items['item_type'], $row_items['ref'], '', $row_items['id'], 'builder', 'icon');
  493. if ($row_items['description']) {
  494. echo "<div align='justify'><font color='#999999'>&nbsp;&nbsp;&nbsp;{$row_items['description']}</font>";
  495. }
  496. echo "</td>";
  497. if (is_prereq($learnpath_id)) {
  498. echo '<td bgcolor="#EEEEFF">';
  499. } else {
  500. echo "<td>";
  501. }
  502. if (api_is_allowed_to_edit()) {
  503. //error_log('Is allowed to edit item'.$row_items['id'], 0);
  504. // TODO: Fix by adding true prerequisites parsing (and cycle through).
  505. // Over simplification here, we transform prereq_id field into prerequisite field.
  506. if ($row_items['prerequisite'] != '') {
  507. $prereq = $row_items['prerequisite'];
  508. //if ($row_items['prereq_type'] == 'i') {
  509. // item
  510. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND id='$prereq'"; // Check if prereq has been deleted.
  511. $result_items2 = Database::query($sql_items2);
  512. $number_items2 = Database::num_rows($result_items2);
  513. if ($number_items2 == 0) {
  514. echo "<font color=red>$lg_prereq_deleted_error</font>";
  515. }
  516. $row_items2 = Database::fetch_array($result_items2);
  517. display_addedresource_link_in_learnpath($row_items2['item_type'], $row_items2['ref'], '', $row_items2['id'], 'builder', '');
  518. if ((($row_items2['item_type'] == 'Exercise') or ($row_items2['item_type'] == 'HotPotatoes')) and ($row_items['prerequisites'])) {
  519. echo "&nbsp;({$row_items2['title']})";
  520. }
  521. //}
  522. /*if ($row_items['prereq_type'] == 'c') {
  523. // chapter
  524. $sql_items2 = "SELECT * FROM $tbl_learnpath_chapter WHERE id='$prereq'"; //check if prereq has been deleted
  525. $result_items2 = Database::query($sql_items2);
  526. $number_items2 = Database::num_rows($result_items2);
  527. if ($number_items2 == 0) {
  528. echo "<font color=red>$lg_prereq_deleted_error</font>";
  529. }
  530. $row_items2 = Database::fetch_array($result_items2);
  531. echo " {$row_items2['chapter_name']}";
  532. }*/
  533. }
  534. echo "</font></td>";
  535. $xml_output .= "<element_type>".$row_items['item_type']."</element_type>";
  536. $xml_output .= "<element_id>".$row_items['id']."</element_id>";
  537. // Move
  538. if ($i < $number_items) {
  539. echo "<td align='center'><a href='".api_get_self()."?lp_id=$learnpath_id&action=moveitem&direction=down&moduleid=".$categoryid."&id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/down.gif\" border=\"0\" title=\"$lg_move_down\"></a></td>";
  540. } else {
  541. echo "<td width='30' align='center'>&nbsp;</td>";
  542. }
  543. if ($i > 1) {
  544. echo "<td align='center'><a href='".api_get_self()."?lp_id=$learnpath_id&action=moveitem&direction=up&moduleid=".$categoryid."&id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/up.gif\" border=\"0\" title=\"$lg_move_up\"></a>";
  545. } else {
  546. echo "<td width='30' align='center'>&nbsp;</td>";
  547. }
  548. echo "</td><td align='center'>";
  549. // Edit prereq
  550. echo "<a href='".api_get_self()."?lp_id=$learnpath_id&action=edititemprereq&id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/scormpre.gif\" border=\"0\" title=\"$lg_add_prereq\"></a></td>";
  551. // Edit
  552. echo "<td align='center'><a href='".api_get_self()."?lp_id=$learnpath_id&action=edititem&id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/edit.gif\" border=\"0\" title=\"$lg_edit_learnpath_item\"></a></td>";
  553. // Delete
  554. echo "<td align='center'>";
  555. echo "<a href='".api_get_self()."?lp_id=$learnpath_id&action=deleteitem&id=".$row_items['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/delete.gif\" border=\"0\" title=\"$lg_delete_learnpath_item\" onclick=\"javascript: return confirmation('".$langThisItem."');\"></a>";
  556. }
  557. $i ++;
  558. echo "</td></tr>";
  559. }
  560. }
  561. /**
  562. * This function returns the items belonging to the chapter that contains the given item (brother items)
  563. * @param integer Item id
  564. * @return array Table containing the items
  565. */
  566. function learnpath_items($itemid) {
  567. global $xml_output;
  568. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  569. $course_id = api_get_course_int_id();
  570. $sql_items = "SELECT parent_item_id FROM $tbl_lp_item WHERE c_id = $course_id AND id='$itemid'";
  571. $moduleid_sql = Database::query($sql_items);
  572. $moduleid_array = Database::fetch_array($moduleid_sql); // First row of the results.
  573. $moduleid = $moduleid_array['parent_item_id'];
  574. $sql_items = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND parent_item_id='$moduleid' ORDER BY display_order ASC";
  575. $result_items = Database::query($sql_items);
  576. $ar = Database::fetch_array($result_items);
  577. while ($ar != '') {
  578. $result[] = $ar;
  579. $ar = Database::fetch_array($result_items);
  580. }
  581. return $result;
  582. }
  583. /**
  584. * This function returns the chapters belonging to the path that contais the given chapter (brother chapters)
  585. * @param integer Learnpath id
  586. * @return array Table containing the chapters
  587. */
  588. function learnpath_chapters($learnpath_id) {
  589. global $xml_output, $learnpath_id;
  590. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  591. $course_id = api_get_course_int_id();
  592. $sql_items = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id='$learnpath_id' AND item_type='dokeos_chapter' ORDER BY display_order ASC";
  593. //$sql_items = "SELECT * FROM $tbl_learnpath_chapter WHERE lp_id='$learnpath_id' ORDER BY display_order ASC";
  594. $result_items = Database::query($sql_items);
  595. $ar = Database::fetch_array($result_items);
  596. while ($ar != '') {
  597. $result[] = $ar;
  598. $ar = Database::fetch_array($result_items);
  599. }
  600. return $result;
  601. }
  602. /**
  603. * This function tells if a learnpath contains items which are prerequisite to other items
  604. * @param integer Learnpath id
  605. * @return boolean True if this learnpath contains an item which is a prerequisite to something
  606. */
  607. function is_prereq($learnpath_id) {
  608. global $xml_output;
  609. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  610. $course_id = api_get_course_int_id();
  611. $prereq = false;
  612. $sql_items = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id='$learnpath_id' AND parent_item_id=0 ORDER BY display_order ASC";
  613. $result_items = Database::query($sql_items);
  614. while ($ar = Database::fetch_array($result_items)) {
  615. $c = $ar['id'];
  616. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $learnpath_id AND parent_item_id='$c' ORDER BY display_order ASC";
  617. $result_items2 = Database::query($sql_items2);
  618. while ($ar2 = Database::fetch_array($result_items2)) {
  619. if ($ar2['prerequisite'] != '') {
  620. $prereq = true;
  621. }
  622. }
  623. }
  624. return ($prereq);
  625. }
  626. /**
  627. * This function returns the prerequisite sentence
  628. * @param integer Item ID
  629. * @return string Prerequisite warning text
  630. */
  631. function prereqcheck($id_in_path) {
  632. // 1. Initialise and import working vars.
  633. global $learnpath_id, $_user;
  634. global $langPrereqToEnter, $langPrereqTestLimit1, $langPrereqTestLimit2, $langPrereqTestLimitNow, $langPrereqFirstNeedTo, $langPrereqModuleMinimum1, $langPrereqModuleMinimum2;
  635. $tbl_learnpath_user = Database :: get_course_table(TABLE_LEARNPATH_USER);
  636. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  637. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  638. $course_id = api_get_course_int_id();
  639. // 2. Initialise return value.
  640. $prereq = false;
  641. // 3. Get item data from the database.
  642. $sql_items = "SELECT * FROM $tbl_learnpath_item WHERE c_id = $course_id AND id='$id_in_path'";
  643. $result_items = Database::query($sql_items);
  644. $row = Database::fetch_array($result_items);
  645. // 4. Check prerequisite's type.
  646. if ($row['prereq_type'] == 'i') {
  647. // 4.a If prerequisite is of type 'i' (item):
  648. // 4.a.1 Get data ready for use.
  649. $id_in_path3 = $row['prereq_id'];
  650. $prereq_limit = $row['prereq_completion_limit'];
  651. // 4.a.2 Get data from the user-item relation.
  652. if ($_user['user_id'] == '') {
  653. $user_id = '0';
  654. } else {
  655. $user_id = $_user['user_id'];
  656. }
  657. $sql_items3 = "SELECT * FROM $tbl_learnpath_user WHERE c_id = $course_id AND (learnpath_item_id='$id_in_path3' and user_id=$user_id)";
  658. $result_items3 = Database::query($sql_items3);
  659. $row3 = Database::fetch_array($result_items3);
  660. // 4.a.3 Get the link that needs to be shown for the current item (not the prereq)
  661. $stepname = display_addedresource_link_in_learnpath($row['item_type'], $row['ref'], '', $id_in_path, 'builder', 'nolink');
  662. // This is the step we want to open.
  663. $stepname = trim($stepname); // Removing occasional line breaks and white spaces
  664. // 4.a.4 Get the prerequisite item.
  665. $sql6 = "SELECT * FROM $tbl_learnpath_item WHERE (id='$id_in_path3')";
  666. $result6 = Database::query($sql6);
  667. $row6 = Database::fetch_array($result6);
  668. // 4.a.5 Get a link to the prerequisite item.
  669. $prereqname = display_addedresource_link_in_learnpath($row6['item_type'], $row6['ref'], '', $id_in_path3, 'builder', 'nolink'); //this is the prereq of the step we want to open
  670. // 4.a.5 Initialise limit value.
  671. $limitok = true;
  672. // 4.a.6 Get prerequisite limit.
  673. if ($prereq_limit) {
  674. // 4.a.6.a If the completion limit exists.
  675. if ($row3['score'] < $prereq_limit) {
  676. // 4.a.6.a.a If the completion limit hasn't been reached, then display the corresponding message.
  677. $prereq = $langPrereqToEnter.$stepname.$langPrereqTestLimit1."$prereq_limit".$langPrereqTestLimit2.$prereqname.". (".$langPrereqTestLimitNow.$row3['score'].")";
  678. } else {
  679. // 4.a.6.a.b The completion limit has been reached. Prepare to return false (no prereq hanging).
  680. $prereq = false;
  681. }
  682. } else {
  683. // 4.a.6.b If the completion limit doesn't exist.
  684. if ($row3['status'] == 'completed' or $row3['status'] == 'passed') {
  685. // 4.a.6.b.a If the prerequisite status is 'completed'.
  686. $prereq = false;
  687. } else {
  688. // 4.a.6.b.b The prerequisite status is not 'completed', return corresponding message.
  689. $prereq = $langPrereqToEnter.$stepname.$langPrereqFirstNeedTo.$prereqname.'.';
  690. }
  691. }
  692. } elseif ($row['prereq_type'] == 'c') {
  693. // 4.b If prerequisite is of type 'c' (chapter).
  694. // 4.b.1 Get data ready to use.
  695. $id_in_path2 = $row['prereq_id'];
  696. // 4.b.2 Get all items in the prerequisite chapter.
  697. $sql_items3 = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND parent_item_id='$id_in_path2'";
  698. $result_items3 = Database::query($sql_items3);
  699. $allcompleted = true;
  700. while ($row3 = Database::fetch_array($result_items3)) {
  701. // 4.b.3 Cycle through items in the prerequisite chapter.
  702. // 4.b.3.1 Get data ready to use.
  703. $id_in_path4 = $row3['id'];
  704. if ($_user['user_id'] == '') {
  705. $user_id = '0';
  706. } else {
  707. $user_id = $_user['user_id'];
  708. }
  709. // 4.b.3.2 Get user-item relation.
  710. $sql_items4 = "SELECT * FROM $tbl_learnpath_user WHERE c_id = $course_id AND (learnpath_item_id='$id_in_path4' and user_id=$user_id)";
  711. $result_items4 = Database::query($sql_items4);
  712. $row4 = Database::fetch_array($result_items4);
  713. // 4.b.3.3 If any of these elements is not 'completed', the overall completion status is false.
  714. if ($row4['status'] != 'completed' and $row4['status'] != 'passed') {
  715. $allcompleted = false;
  716. }
  717. }
  718. if ($allcompleted) {
  719. // 4.b.4.a All items were completed, prepare to return that there is no prerequisite blocking the way.
  720. $prereq = false;
  721. } else {
  722. // 4.b.4.b Something was not completed. Return corresponding message.
  723. $sql5 = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (lp_id='$learnpath_id' and id='$id_in_path2')";
  724. $result5 = Database::query($sql5);
  725. $row5 = Database::fetch_array($result5);
  726. $prereqmodulename = trim($row5['chapter_name']);
  727. $prereq = $langPrereqModuleMinimum1.$prereqmodulename.$langPrereqModuleMinimum2;
  728. }
  729. } else {
  730. // 5. If prerequisite type undefined, no prereq.
  731. $prereq = false;
  732. }
  733. // 6. Return the message (or false if no prerequisite waiting).
  734. return ($prereq);
  735. }
  736. /**
  737. * Constructs the tree that will be used to build the learnpath structure
  738. * @params integer Learnpath_id
  739. * @return array Tree of the learnpath structure
  740. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  741. * @comment This is a temporary function, which exists while the chapters and items
  742. * are still in separate tables in the database. This function gathers the data in a unique tree.
  743. */
  744. function get_learnpath_tree($learnpath_id) {
  745. //error_log('New LP - In learnpath_functions::get_learnpath_tree', 0);
  746. // Init elems
  747. //global $tbl_learnpath_item, $tbl_learnpath_chapter;
  748. /*
  749. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  750. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  751. */
  752. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  753. $tree = array();
  754. $chapters = array();
  755. $all_items_by_chapter = array();
  756. $course_id = api_get_course_int_id();
  757. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = ".$learnpath_id." AND item_type='dokeos_chapter' ORDER BY display_order";
  758. //error_log('New LP - learnpath_functions - get_learnpath_tree: '.$sql,0);
  759. $res = Database::query($sql);
  760. // Format the $chapters_by_parent array so we have a suitable structure to work with.
  761. while ($row = Database::fetch_array($res)) {
  762. $chapters[] = $row;
  763. // Shouldn't be necessary (check no null value).
  764. if (empty ($row['parent_item_id'])) {
  765. $row['parent_item_id'] = 0;
  766. }
  767. //$chapters_by_parent[$row['parent_item_id']][$row['previous_item_id']] = $row;
  768. $all_items_by_chapter[$row['parent_item_id']][$row['display_order']] = $row;
  769. $all_items_by_chapter[$row['parent_item_id']][$row['display_order']]['type'] = 'dokeos_chapter';
  770. }
  771. // Now for every item in each chapter, get a suitable structure too.
  772. foreach ($chapters as $row) {
  773. // Select items from this chapter.
  774. $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $learnpath_id AND parent_item_id = ".$row['id']." ORDER BY display_order";
  775. //error_log('New LP - learnpath_functions - get_learnpath_tree: '.$sql, 0);
  776. $res = Database::query($sql);
  777. //error_log('New LP - learnpath_functions - get_learnpath_tree: Found '.Database::num_rows($res).' results', 0);
  778. while ($myrow = Database::fetch_array($res, 'ASSOC')) {
  779. //$items[] = $myrow;
  780. //$items_by_chapter[$myrow['parent_item_id']][$myrow['display_order']] = $myrow;
  781. $all_items_by_chapter[$row['id']][$myrow['display_order']] = $myrow;
  782. $all_items_by_chapter[$row['id']][$myrow['display_order']]['type'] = 'item';
  783. }
  784. }
  785. //array_multisort($all_items_by_chapter[0], SORT_ASC, SORT_NUMERIC);
  786. foreach ($all_items_by_chapter as $key => $subrow) {
  787. ksort($all_items_by_chapter[$key]);
  788. }
  789. //all items should now be well-ordered
  790. //error_log('New LP - In get_learnpath_tree, returning '.print_r($all_items_by_chapter,true), 0);
  791. return $all_items_by_chapter;
  792. }
  793. /**
  794. * Gives a list of sequencial elements IDs for next/previous actions
  795. * @param array The elements tree as returned by get_learnpath_tree()
  796. * @param integer The chapter id to start from
  797. * @param boolean Whether to include chapters or not
  798. * @return array List of elements in the first to last order
  799. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  800. */
  801. function get_ordered_items_list($tree, $chapter = 0, $include_chapters = false) {
  802. $list = array ();
  803. foreach ($tree[$chapter] as $order => $elem) {
  804. if ($elem['type'] == 'chapter') {
  805. if ($include_chapters === true) {
  806. $list[] = array ('id' => $elem['id'], 'type' => $elem['type']);
  807. }
  808. $res = get_ordered_items_list($tree, $elem['id'], $include_chapters);
  809. foreach ($res as $elem) {
  810. $list[] = $elem;
  811. }
  812. } elseif ($elem['type'] == 'item') {
  813. $list[] = array ('id' => $elem['id'], 'type' => $elem['type'], 'item_type' => $elem['item_type'], 'parent_item_id' => $elem['parent_item_id'], 'item_id' => $elem['item_id']);
  814. }
  815. }
  816. return $list;
  817. }
  818. /**
  819. * Displays the structure of a chapter recursively. Takes the result of get_learnpath_tree as argument
  820. * @param array Chapter structure
  821. * @param integer Chapter ID (start point in the tree)
  822. * @param integer Learnpath ID
  823. * @param integer User ID
  824. * @param boolean Indicates if the style is wrapped (true) or extended (false)
  825. * @param integer Level reached so far in the tree depth (enables recursive behaviour)
  826. * @return array Number of items, Number of items completed
  827. * @author Many changes by Yannick Warnier <yannick.warnier@beeznest.com>
  828. */
  829. function display_toc_chapter_contents($tree, $parent_item_id = 0, $learnpath_id, $uid, $wrap, $level = 0) {
  830. //global $tbl_learnpath_user;
  831. $tbl_learnpath_user = Database :: get_course_table(TABLE_LEARNPATH_USER);
  832. $num = 0;
  833. $num_completed = 0;
  834. foreach ($tree[$parent_item_id] as $order => $elem) {
  835. $bold = false;
  836. if (!empty ($_SESSION['cur_open']) && ($elem['id'] == $_SESSION['cur_open'])) {
  837. $bold = true;
  838. }
  839. if ($elem['type'] === 'chapter') {
  840. if ($wrap) {
  841. echo str_repeat("&nbsp;&nbsp;", $level).shorten(strip_tags($elem['chapter_name']), (35 - 3 * $level))."<br />\n";
  842. } else {
  843. echo "<tr><td colspan='3'>".str_repeat("&nbsp;&nbsp;", $level).shorten($elem['chapter_name'], (35 - 3 * $level))."</td></tr>\n";
  844. }
  845. if ($wrap) {
  846. if ($elem['chapter_description'] != '') {
  847. echo "<div class='description'>".str_repeat("&nbsp;&nbsp;", $level)."&nbsp;".shorten($elem['chapter_description'], (35 - 3 * $level))."</div>\n";
  848. }
  849. } else {
  850. if ($elem['chapter_description'] != '') {
  851. echo "<tr><td colspan='3'><div class='description'>".str_repeat("&nbsp;&nbsp;", $level)."&nbsp;".shorten($elem['chapter_description'], (35 - 3 * $level))."</div></td></tr>\n";
  852. }
  853. }
  854. list ($a, $b) = display_toc_chapter_contents($tree, $elem['id'], $learnpath_id, $uid, $wrap, $level + 1);
  855. $num += $a;
  856. $num_completed += $b;
  857. } elseif ($elem['type'] === 'item') {
  858. // If this element is an item (understand: not a directory/module).
  859. $sql0 = "SELECT * FROM $tbl_learnpath_user WHERE (user_id='".$uid."' and learnpath_item_id='".$elem['id']."' and lp_id='".$learnpath_id."')";
  860. $result0 = Database::query($sql0);
  861. $row0 = Database::fetch_array($result0);
  862. $completed = '';
  863. if (($row0['status'] == 'completed') or ($row0['status'] == 'passed')) {
  864. $completed = 'completed';
  865. $num_completed ++;
  866. }
  867. if ($wrap) {
  868. echo str_repeat("&nbsp;&nbsp;", $level)."<a name='{$elem['id']}' />\n";
  869. } else {
  870. echo "<tr><td>".str_repeat("&nbsp;&nbsp;", $level-1)."<a name='{$elem['id']}' />\n";
  871. }
  872. if ($wrap) {
  873. $icon = 'wrap';
  874. }
  875. if ($bold) {
  876. echo "<b>";
  877. }
  878. display_addedresource_link_in_learnpath($elem['item_type'], $elem['ref'], $completed, $elem['id'], 'player', $icon);
  879. if ($bold) {
  880. echo "</b>";
  881. }
  882. if ($wrap) {
  883. echo "<br />\n";
  884. } else {
  885. echo "</td></tr>\n";
  886. }
  887. $num ++;
  888. }
  889. }
  890. return array ($num, $num_completed);
  891. }
  892. /**
  893. * Returns a string to display in the tracking frame within the contents.php page (for example)
  894. * @param integer Learnpath id
  895. * @param integer Current user id
  896. * @param integer Starting chapter id
  897. * @param array Tree of elements as returned by get_learnpath_tree()
  898. * @param integer Level of recursivity we have reached
  899. * @param integer Counter of elements already displayed
  900. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  901. * @deprecated this function seems to be unused
  902. * @note : forced display because of display_addedresource_link_in_learnpath behaviour (outputing a string would be better)
  903. */
  904. function get_tracking_table($learnpath_id, $user_id, $parent_item_id = 0, $tree = false, $level = 0, $counter = 0) {
  905. $tbl_learnpath_chapter = Database :: get_course_learnpath_chapter_table();
  906. $tbl_learnpath_item = Database :: get_course_learnpath_item_table();
  907. $tbl_learnpath_user = Database :: get_course_learnpath_user_table();
  908. //$mytable = '';
  909. $include_chapters = true;
  910. if (!is_array($tree)) {
  911. // Get a tree of the current learnpath elements.
  912. $tree = get_learnpath_tree($learnpath_id);
  913. }
  914. foreach ($tree[$parent_item_id] as $order => $elem) {
  915. if (($counter % 2) == 0) {
  916. $oddclass = 'row_odd';
  917. } else {
  918. $oddclass = 'row_even';
  919. }
  920. if ($elem['type'] == 'chapter') {
  921. if ($include_chapters === true) {
  922. //$mytable .= "<tr class='$oddclass'><td colspan = '3'>".str_repeat('&nbsp;',$level*2+2).$elem['chapter_name']."</td></tr>\n";
  923. echo "<tr class='$oddclass'><td colspan = '3'>".str_repeat('&nbsp;', $level * 2 + 2).$elem['chapter_name']."</td></tr>\n";
  924. }
  925. $counter ++;
  926. //$mytable .= get_tracking_table($learnpath_id, $user_id, $elem['id'], $tree, $level + 1, $counter );
  927. get_tracking_table($learnpath_id, $user_id, $elem['id'], $tree, $level + 1, $counter);
  928. } elseif ($elem['type'] == 'item') {
  929. $sql = "SELECT * FROM $tbl_learnpath_user "."WHERE user_id = $user_id "."AND lp_id = $learnpath_id "."AND learnpath_item_id = ".$elem['id'];
  930. $res = Database::query($sql);
  931. $myrow = Database::fetch_array($res);
  932. if (($myrow['status'] == 'completed') || ($myrow['status'] == 'passed')) {
  933. $color = 'blue';
  934. $statusmessage = get_lang('Complete');
  935. } else {
  936. $color = 'black';
  937. $statusmessage = get_lang('Incomplete');
  938. }
  939. $link = get_addedresource_link_in_learnpath($elem['item_type'], $elem['id'], $elem['item_id']);
  940. //$link = display_addedresource_link_in_learnpath($elem['item_type'], $elem['id'], $row['status'], $elem['item_id'], 'player', 'none');
  941. //$mytable .= "<tr class='$oddclass'>"
  942. echo "<tr class='$oddclass'>"."<td class='mystatus'>".str_repeat("&nbsp;", $level * 2 + 2);
  943. //."<a href='$link?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='toc'>hop</a>"
  944. display_addedresource_link_in_learnpath($elem['item_type'], $elem['ref'], $myrow['status'], $elem['id'], 'player', 'wrap');
  945. //we should also add the total score here
  946. echo "<td>"."<font color='$color'><div class='mystatus'>".$statusmessage."</div></font>"."</td>"."<td>"."<div class='mystatus' align='center'>". ($myrow['score'] == 0 ? '-' : $myrow['score'])."</div>"."</td>"."</tr>\n";
  947. $counter ++;
  948. }
  949. }
  950. //return $mytable;
  951. return true;
  952. }
  953. /**
  954. * This function returns false if there is at least one item in the path
  955. * @param Learnpath ID
  956. * @return boolean True if nothing was found, false otherwise
  957. */
  958. function is_empty($id) {
  959. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  960. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  961. $course_id = api_get_course_int_id();
  962. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND lp_id=$id ORDER BY display_order ASC";
  963. $result = Database::query($sql);
  964. $num_modules = Database::num_rows($result);
  965. $empty = true;
  966. if ($num_modules != 0) {
  967. while ($row = Database::fetch_array($result)) {
  968. $num_items = 0;
  969. $parent_item_id = $row['id'];
  970. $sql2 = "SELECT * FROM $tbl_learnpath_item WHERE c_id = $course_id AND (parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  971. $result2 = Database::query($sql2);
  972. $num_items = Database::num_rows($result2);
  973. if ($num_items > 0) {
  974. $empty = false;
  975. }
  976. }
  977. }
  978. return ($empty);
  979. }
  980. /**
  981. * This function writes $content to $filename
  982. * @param string Destination filename
  983. * @param string Learnpath name
  984. * @param integer Learnpath ID
  985. * @param string Content to write
  986. * @return void
  987. */
  988. function exporttofile($filename, $LPname, $LPid, $content) {
  989. global $circle1_files; // This keeps all the files which are exported [0]:filename [1]:LP name.
  990. // The $circle1_files variable is going to be used to a deep extent in the imsmanifest.xml.
  991. global $expdir;
  992. if (!$handle = fopen($expdir.'/'.$filename, 'w')) {
  993. echo "Cannot open file ($filename)";
  994. }
  995. if (fwrite($handle, $content) === false) {
  996. echo "Cannot write to file ($filename)";
  997. exit;
  998. }
  999. fclose($handle);
  1000. $circle1_files[0][] = $filename;
  1001. $circle1_files[1][] = $LPname;
  1002. $circle1_files[2][] = $LPid;
  1003. }
  1004. /**
  1005. * This function exports the given Chamilo test
  1006. * @param integer Test ID
  1007. * @return string The test itself as an HTML string
  1008. */
  1009. function export_exercise($item_id) {
  1010. global $expdir, $_course, $_configuration, $_SESSION, $_SERVER, $language_interface, $langExerciseNotFound, $langQuestion, $langOk, $origin, $questionNum;
  1011. $exerciseId = $item_id;
  1012. $TBL_EXERCISES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1013. /* Clears the exercise session */
  1014. if (isset ($_SESSION['objExercise'])) {
  1015. Session::erase('objExercise');
  1016. }
  1017. if (isset ($_SESSION['objQuestion'])) {
  1018. Session::erase('objQuestion');
  1019. }
  1020. if (isset ($_SESSION['objAnswer'])) {
  1021. Session::erase('objAnswer');
  1022. }
  1023. if (isset ($_SESSION['questionList'])) {
  1024. Session::erase('questionList');
  1025. }
  1026. if (isset ($_SESSION['exerciseResult'])) {
  1027. Session::erase('exerciseResult');
  1028. }
  1029. // If the object is not in the session:
  1030. if (!isset ($_SESSION['objExercise'])) {
  1031. // Construction of Exercise.
  1032. $objExercise = new Exercise();
  1033. $sql = "SELECT title,description,sound,type,random,active FROM $TBL_EXERCISES WHERE id='$exerciseId'";
  1034. // If the specified exercise doesn't exist or is disabled:
  1035. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !api_is_allowed_to_edit() && ($origin != 'learnpath'))) {
  1036. die($langExerciseNotFound);
  1037. }
  1038. // Saves the object into the session.
  1039. Session::write('objExercise',$objExercise);
  1040. }
  1041. $exerciseTitle = $objExercise->selectTitle();
  1042. $exerciseDescription = $objExercise->selectDescription();
  1043. $exerciseSound = $objExercise->selectSound();
  1044. $randomQuestions = $objExercise->isRandom();
  1045. $exerciseType = $objExercise->selectType();
  1046. if (!isset ($_SESSION['questionList'])) {
  1047. // Selects the list of question ID.
  1048. $questionList = $randomQuestions ? $objExercise->selectRandomList() : $objExercise->selectQuestionList();
  1049. // Saves the question list into the session.
  1050. Session::write('questionList',$questionList);
  1051. }
  1052. $nbrQuestions = sizeof($questionList);
  1053. // If questionNum comes from POST and not from GET:
  1054. if (!$questionNum || $_POST['questionNum']) {
  1055. // Only used for sequential exercises (see $exerciseType).
  1056. if (!$questionNum) {
  1057. $questionNum = 1;
  1058. } else {
  1059. $questionNum ++;
  1060. }
  1061. }
  1062. $exerciseTitle = text_filter($exerciseTitle);
  1063. $test .= "<h3>".$exerciseTitle."</h3>";
  1064. if (!empty ($exerciseSound)) {
  1065. $test .= "<a href=\"../document/download.php?doc_url=%2Faudio%2F".$exerciseSound."\"&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9 target=\"_blank\"><img src=\"../img/sound.gif\" border=\"0\" align=\"absmiddle\" alt=".get_lang("Sound")."\" /></a>";
  1066. }
  1067. $exerciseDescription = text_filter($exerciseDescription);
  1068. // Writing the .js file with to check the correct answers begin.
  1069. $scriptfilename = "Exercice".$item_id.".js";
  1070. $s = "<script type=\"text/javascript\" src='../js/".$scriptfilename."'></script>";
  1071. $test .= $s;
  1072. $content = "function evaluate() {
  1073. alert('Test evaluated.');
  1074. }
  1075. ";
  1076. if (!$handle = fopen($expdir.'/js/'.$scriptfilename, 'w')) {
  1077. echo "Cannot open file ($scriptfilename)";
  1078. }
  1079. if (fwrite($handle, $content) === false) {
  1080. echo "Cannot write to file ($filename)";
  1081. exit;
  1082. }
  1083. fclose($handle);
  1084. // Writing the .js file with to check the correct answers end.
  1085. $s = "
  1086. <p>$exerciseDescription</p>
  1087. <table width='100%' border='0' cellpadding='1' cellspacing='0'>
  1088. <form method='post' action=''><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1089. <input type='hidden' name='formSent' value='1' />
  1090. <input type='hidden' name='exerciseType' value='".$exerciseType."' />
  1091. <input type='hidden' name='questionNum' value='".$questionNum."' />
  1092. <input type='hidden' name='nbrQuestions' value='".$nbrQuestions."' />
  1093. <tr>
  1094. <td>
  1095. <table width='100%' cellpadding='4' cellspacing='2' border='0'>";
  1096. $test .= $s;
  1097. $i = 0;
  1098. foreach ($questionList as $questionId) {
  1099. $i ++;
  1100. echo $s = "<tr bgcolor='#e6e6e6'><td valign='top' colspan='2'>".get_lang('Question')." ";
  1101. $test .= ExerciseLib::showQuestion($questionId, false, 'export', $i);
  1102. } // end foreach()
  1103. $s = "</table></td></tr><tr><td><br/><input type='button' value='".$langOk."' onclick=\"javascript: evaluate(); alert('Evaluated.');\">";
  1104. $s .= "</td></tr></form></table>";
  1105. $s .= "<script type='text/javascript'> loadPage(); </script>";
  1106. $b = 2;
  1107. $test .= $s;
  1108. return ($test);
  1109. }
  1110. /**
  1111. * This function exports the given item
  1112. * @param integer Id from learnpath_items table
  1113. * @param integer Item id
  1114. * @param string Itm type
  1115. * @param boolean Shall the SCORM communications features be added? (true). Default: false.
  1116. * @return void (outputs a zip file)
  1117. * @todo Try using the SCORM communications addition (adding a button and several javascript calls to the SCORM API) elsewhere than just in the export feature, so it doesn't look like an incoherent feature
  1118. */
  1119. function exportitem($id, $item_id, $item_type, $add_scorm_communications = false) {
  1120. $course_id = api_get_course_int_id();
  1121. global $circle1_files, $expdir, $_course, $_SESSION, $GLOBALS;
  1122. global $timeNoSecFormat, $dateFormatLong, $language_interface, $langPubl, $langDone, $langThisCourseDescriptionIsEmpty, $lg_course_description, $lg_introduction_text, $_cid, $langHotPotatoesFinished, $lg_author, $lg_date, $lg_groups, $lg_users, $lg_ass, $lg_dropbox, $test, $langQuestion;
  1123. $libp = api_get_path(SYS_CODE_PAH);
  1124. $langLasting = '';//avoid code parser warning
  1125. include_once $libp.'lang/english/announcements.inc.php'; //this line is here only for $langPubl in announcements
  1126. include_once $libp.'lang/'.$language_interface.'/announcements.inc.php'; //this line is here only for $langPubl in announcements
  1127. include_once $libp.'lang/english/agenda.inc.php'; //this line is here only for $langLasting
  1128. include_once $libp.'lang/'.$language_interface.'/agenda.inc.php'; //this line is here only for $langLasting
  1129. include_once $libp.'lang/english/course_description.inc.php'; //this line is here only for $langThisCourseDescriptionIsEmpty
  1130. include_once $libp.'lang/'.$language_interface.'/course_description.inc.php'; // -||-
  1131. include_once $libp.'lang/english/resourcelinker.inc.php';
  1132. include_once $libp.'lang/'.$language_interface.'/resourcelinker.inc.php';
  1133. include_once $libp.'lang/english/learnpath.inc.php';
  1134. include_once $libp.'lang/'.$language_interface.'/learnpath.inc.php';
  1135. include_once $libp.'lang/english/exercice.inc.php';
  1136. include_once $libp.'lang/'.$language_interface.'/exercice.inc.php';
  1137. include_once '../resourcelinker/resourcelinker.inc.php';
  1138. $LPname = display_addedresource_link_in_learnpath($item_type, $item_id, '', $id, 'builder', 'nolink');
  1139. $expcontent = "<!--
  1140. This is an exported file from Chamilo Learning Path belonging to a Scorm compliant content package.
  1141. Do not modify or replace individually.
  1142. Export module author : Denes Nagy <darkden@evk.bke.hu>
  1143. -->
  1144. ";
  1145. // Files needed for communicating with the scos.
  1146. $scocomfiles = "<script type='text/javascript' src='../js/APIWrapper.js'></script>"."<script type='text/javascript' src='../js/SCOFunctions.js'></script>";
  1147. $expcontent .= '<html><head><link rel="stylesheet" href="../css/default.css" type="text/css" media="screen,projection" />'.$scocomfiles.'</head><body>';
  1148. $donebutton .= "<script type='text/javascript'>
  1149. /* <![CDATA[ */
  1150. loadPage();
  1151. var studentName = '!';
  1152. var lmsStudentName = doLMSGetValue( 'cmi.core.student_name' );
  1153. if ( lmsStudentName != '' )
  1154. {
  1155. studentName = ' ' + lmsStudentName + '!';
  1156. }
  1157. /* ]]> */
  1158. </script>
  1159. <br /><br />
  1160. <form><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1161. <table cols='3' width='100%' align='center'>
  1162. <tr>
  1163. <td align='middle'><input type = 'button' value = ' ".$langDone." ' onclick = \"javascript: doQuit('completed');\" id='button2' name='button2'></td>
  1164. </tr>
  1165. </table>
  1166. </form>";
  1167. /**
  1168. * Switch between the different element types, namely:
  1169. * - Agenda
  1170. * - Ad_Valvas
  1171. * - Course_description
  1172. * - Document
  1173. * - Introduction_text
  1174. * - HotPotatoes
  1175. * - Exercise
  1176. * - Post
  1177. * - Forum ]
  1178. * - Thread ]
  1179. * - Dropbox ]
  1180. * - Assignments ] These elements are all replaced by a simple message in the exported document.
  1181. * - Groups ]
  1182. * - Users ]
  1183. * - Link _self
  1184. * - Link _blank
  1185. */
  1186. switch ($item_type) {
  1187. // AGENDA BEGIN
  1188. case 'Agenda':
  1189. // 1. Get agenda event data from the database table.
  1190. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  1191. $sql = "SELECT * FROM ".$TABLEAGENDA." where c_id = $course_id AND (id=$item_id)";
  1192. $result = Database::query($sql);
  1193. // 2. Prepare table output.
  1194. $expcontent .= "<table class=\"data_table\" >";
  1195. $barreMois = '';
  1196. // 3. For each event corresponding to this agenda, do the following:
  1197. while ($myrow = Database::fetch_array($result)) {
  1198. $start_date_local = api_get_local_time($myrow['start_date'], null, date_default_timezone_get());
  1199. //3.1 Make the blue month bar appear only once.
  1200. if ($barreMois != api_format_date($start_date_local, "%m")) {
  1201. // 3.1.1 Update the check value for the month bar.
  1202. $barreMois = api_format_date($start_date_local, "%m");
  1203. // 3.1.2 Display the month bar.
  1204. $expcontent .= "<tr><td id=\"title\" colspan=\"2\" class=\"month\" valign=\"top\">".api_format_date($start_date_local, "%B %Y")."</td></tr>";
  1205. }
  1206. // 3.2 Display the agenda items (of this month): the date, hour and title.
  1207. $db_date = (int) api_format_date($start_date_local, "%d");
  1208. if ($_GET['day'] != $db_date) {
  1209. // 3.2.1.a If the day given in the URL (might not be set) is different from this element's day, use style 'data'.
  1210. $expcontent .= "<tr><td class=\"data\" colspan='2'>";
  1211. } else {
  1212. // 3.2.1.b Else (same day) use style 'datanow'.
  1213. $expcontent .= "<tr><td class=\"datanow\" colspan='2'>";
  1214. }
  1215. // 3.2.2 Mark an anchor for this date.
  1216. $expcontent .= "<a name=\"".$db_date."\"></a>"; // anchoring
  1217. // 3.2.3 Write the date and time of this event to the export string.
  1218. $expcontent .= api_format_date($start_date_local);
  1219. // 3.2.4 If a duration is set, write it, otherwise ignore.
  1220. if ($myrow['duration'] == '') {
  1221. $expcontent .= "<br />";
  1222. } else {
  1223. $expcontent .= " / ".$langLasting." ".$myrow['duration']."<br />"; //langLasting comes from lang/x/agenda.inc.php
  1224. }
  1225. // 3.2.5 Write the title.
  1226. $expcontent .= $myrow['title'];
  1227. $expcontent .= "</td></tr>";
  1228. // 3.2.6 Prepare the content of the agenda item.
  1229. $content = $myrow['content'];
  1230. // 3.2.7 Make clickable???
  1231. $content = make_clickable($content);
  1232. $content = text_filter($content);
  1233. // 3.2.8 Write the prepared content to the export string.
  1234. $expcontent .= "<tr><td class=\"text\" colspan='2'>";
  1235. $expcontent .= $content;
  1236. $expcontent .= "</td></tr>";
  1237. // Displaying the agenda item of this month: the added resources.
  1238. // This part is not included into LP export.
  1239. /*if (check_added_resources("Agenda", $myrow['id'])) {
  1240. $content.= "<tr><td colspan='2'>";
  1241. $content.= "<i>".get_lang('AddedResources')."</i><br />";
  1242. display_added_resources("Agenda", $myrow['id']);
  1243. $content.= "</td></tr>";
  1244. }*/
  1245. }
  1246. // 4. Finish the export string.
  1247. $expcontent .= "<tr></table>";
  1248. break;
  1249. // ANNOUNCEMENT BEGIN
  1250. case 'Ad_Valvas':
  1251. // 1. Get the announcement data from the database
  1252. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1253. $sql = "SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id='$item_id'";
  1254. $result = Database::query($sql);
  1255. // 2. Initialise export string
  1256. $expcontent .= "<table class=\"data_table\">";
  1257. // 3. For each announcement matching the query
  1258. while ($myrow = Database::fetch_array($result)) {
  1259. // 3.1 Get the __ field data.
  1260. $content = $myrow[1];
  1261. //$content = nl2br($content);
  1262. // 3.2 Prepare the data for export.
  1263. $content = make_clickable($content);
  1264. $content = text_filter($content);
  1265. // 3.3 Get a UNIX(?<-mktime) Timestamp of the end_date for this announcement.
  1266. $last_post_datetime = $myrow['end_date']; // post time format datetime of database layer (MySQL is assumed)
  1267. list ($last_post_date, $last_post_time) = split(' ', $last_post_datetime);
  1268. list ($year, $month, $day) = explode('-', $last_post_date);
  1269. list ($hour, $min) = explode(':', $last_post_time);
  1270. $announceDate = mktime($hour, $min, 0, $month, $day, $year);
  1271. // 3.4 Compare the end date to the last login date of the user (mark it in red if he has not already read it).
  1272. if ($announceDate > $_SESSION['user_last_login_datetime']) {
  1273. $colorBecauseNew = " color=\"red\" ";
  1274. } else {
  1275. $colorBecauseNew = ' ';
  1276. }
  1277. // 3.5 Write this content to the export string (formatted HTML array).
  1278. $expcontent .= "<tr>\n"."<td class=\"cell_header\">\n"."<font ".$colorBecauseNew.">".$langPubl." : ".api_convert_and_format_date($last_post_datetime, null, date_default_timezone_get())."</font>\n"."</td>\n"."</tr>\n"."<tr>\n"."<td>\n".$content."</td>\n"."</tr>\n";
  1279. } // while loop
  1280. // 4 Finish the export string.
  1281. $expcontent .= "</table>";
  1282. break;
  1283. // Course_description BEGIN
  1284. case 'Course_description':
  1285. // 1. Get course description data from database.
  1286. $tbl_course_description = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  1287. $result = Database::query("SELECT id, title, content FROM ".$tbl_course_description." WHERE c_id = $course_id ORDER BY id");
  1288. // 2. Check this element.
  1289. if (Database::num_rows($result)) {
  1290. // 2.a This course has one (or more) description in the database.
  1291. $expcontent .= "<hr noshade=\"noshade\" size=\"1\" />";
  1292. // 2.a.1 For each description available for this course.
  1293. while ($row = Database::fetch_array($result)) {
  1294. // 2.a.1.1 Write title to export string.
  1295. $expcontent .= "<h4>".$row['title']."</h4>";
  1296. // 2.a.1.2 Prepare content.
  1297. $content = make_clickable(nl2br($row['content']));
  1298. $content = text_filter($content);
  1299. // 2.a.1.3 Write content to the export string.
  1300. $expcontent .= $content;
  1301. }
  1302. } else {
  1303. // 2.b This course has no description available.
  1304. $expcontent .= "<br /><h4>$langThisCourseDescriptionIsEmpty</h4>";
  1305. }
  1306. break;
  1307. // DOCUMENT BEGIN
  1308. case 'Document':
  1309. // 1. Get the document data from the database.
  1310. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1311. $sql_query = "SELECT * FROM $tbl_document WHERE c_id = $course_id AND id=$item_id";
  1312. $sql_result = Database::query($sql_query);
  1313. $myrow = Database::fetch_array($sql_result);
  1314. // 2. Get the origin path of the document to treat it internally.
  1315. $orig = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$myrow['path'];
  1316. // 3. Make some kind of strange transformation to get the destination filepath ???
  1317. $pathname = explode('/', $myrow['path']);
  1318. $last = count($pathname) - 1;
  1319. $filename = 'data/'.$filename.$pathname[$last];
  1320. $copyneeded = true;
  1321. // Html files do not need to be copied as the ok button is inserted into them,
  1322. // so don't copy directly.
  1323. $extension = explode('.', $pathname[$last]);
  1324. // This old condition was WRONG for names like design.html.old. Instead, we now get the extension
  1325. // by using preg_match to match case-insensitive (probably faster than 4 conditions).
  1326. //if (($extension[1]=='htm') or ($extension[1]=='html') or ($extension[1]=='HTM') or ($extension[1]=='HTML')) {
  1327. // 4. Check the file extension.
  1328. if (preg_match('/.*(\.htm(l)?)$/i', $pathname[$last])) {
  1329. // 4.a If this file ends with ".htm(l)", we consider it's an HTML file.
  1330. // src tag check begin
  1331. // We now check if there is any src attribute in htm(l) files, if yes, we have to also export
  1332. // the target file (swf, mp3, video,...) of that src tag.
  1333. // In case of absolute links (http://) this is not neccessary, but makes no error.
  1334. // However still missing : relative links case with subdirs -> the necessary dirs are not created in the exported package.
  1335. // 4.a.1 Get the file contents into $file.
  1336. $file = file_get_contents($orig);
  1337. // 4.a.2 Get all the src links in this file.
  1338. //preg_match_all("|((?i)src=\".*\" )|U",$file,$match);
  1339. $match = GetSRCTags($orig);
  1340. // 4.a.3 For each src tag found, do the following:
  1341. for ($i = 0; $i < count($match); $i ++) {
  1342. // 4.a.3.1 Get the tag (split from the key).
  1343. list ($key, $srctag) = each($match);
  1344. $src = $srctag;
  1345. // 4.a.3.2 Check the link kind (web or absolute/relative).
  1346. if (stristr($src, 'http') === false) {
  1347. // 4.a.3.2.a Do something only if relative (otherwise the user will be able to see it too anyway).
  1348. // 4.a.3.2.a.1 Get a proper URL and remove all './'
  1349. $src = urldecode($src); //mp3
  1350. //$src=str_replace('./','',$src);
  1351. $src = preg_replace('/^\.\//', '', $src);
  1352. // 4.a.3.2.a.2 Remove the player link from the URL (only use the mp3 file).
  1353. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1354. // 4.a.3.2.a.3 Remove funny link parts.
  1355. $src = str_replace('?0', '', $src); //mp3
  1356. // The previous lines are used when creating docs with Chamilo Document tool's htmlarea
  1357. // Rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1358. // and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1359. // created by any other software.
  1360. // 4.a.3.2.a.4 Prepare source and destination paths.
  1361. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1362. $dest = $expdir.'/data/'.$src;
  1363. //CopyNCreate($source,$dest);
  1364. rcopy($source, $dest);
  1365. } //else...?
  1366. }
  1367. // src tag check end
  1368. // sco communication insertion begin
  1369. // 4.a.4 If we want to add SCORM actions and a "Done" button, do the following:
  1370. if ($add_scorm_communications === true) {
  1371. if ($bodyclose = strpos($file, '</body>')) {
  1372. $file = substr_replace($file, $scocomfiles.$donebutton, $bodyclose, 7);
  1373. } elseif ($htmlclose = strpos($file, '</html>')) {
  1374. $file = substr_replace($file, $scocomfiles.$donebutton, $htmlclose, 7);
  1375. $file .= '</html>';
  1376. } else {
  1377. $file .= $scocomfiles.$donebutton;
  1378. }
  1379. } //sco communication insertion end
  1380. // 4.a.5 Replace the file's name by adding the element's ID before htm.
  1381. // This will not work with uppercase HTML though. Maybe use the preg_replace syntax proposed...
  1382. $filename = str_replace('.htm', $id.'.htm', $filename);
  1383. //$filename=preg_replace('/.*(\.htm(l)?)$/i',$id.$1,$filename);
  1384. // 4.a.6 Export these contents to a file and set the circle1_files array for later reuse.
  1385. exporttofile($filename, $LPname, $id, $file);
  1386. // The file has been copied, so ask not to copy it again.
  1387. $copyneeded = false;
  1388. } //if (htm(l) files) end
  1389. // 5. If we still need to copy the file (e.g. it was not an HTML file), then copy and set circle1_files for later reuse.
  1390. if ($copyneeded) {
  1391. copy($orig, $expdir.'/'.$filename);
  1392. $circle1_files[0][] = $filename;
  1393. $circle1_files[1][] = $LPname;
  1394. $circle1_files[2][] = $id;
  1395. }
  1396. //echo $orig;
  1397. return;
  1398. // Introduction_text BEGIN
  1399. case 'Introduction_text':
  1400. // 1 Get the introduction text data from the database.
  1401. $TBL_INTRO = Database :: get_course_tool_intro_table();
  1402. // Modified by Ivan Tcholakov, 15-SEP-2008.
  1403. //$result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE id=1");
  1404. $result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE c_id = $course_id AND id='course_homepage'");
  1405. //
  1406. $myrow = Database::fetch_array($result);
  1407. $intro = $myrow['intro_text'];
  1408. // 2 Write introduction text to the export string.
  1409. $expcontent .= '<br />'.$intro;
  1410. break;
  1411. // HotPotatoes BEGIN
  1412. case 'HotPotatoes':
  1413. // 1. Get HotPotatoes data from the document table.
  1414. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1415. $result = Database::query("SELECT * FROM $tbl_document WHERE c_id = $course_id AND id=$item_id");
  1416. $myrow = Database::fetch_array($result);
  1417. // 2. Get the document path.
  1418. $testfile = api_get_path(SYS_COURSE_PATH).$_course['path']."/document".urldecode($myrow['path']);
  1419. // 3. Get the document contents into a string.
  1420. $content = file_get_contents($testfile);
  1421. // 4. Get the document filename (just the file, no path) - would probably be better to use PHP native function.
  1422. $pathname = explode('/', $myrow['path']);
  1423. $last = count($pathname) - 1;
  1424. $filename = 'data/'.$filename.$pathname[$last];
  1425. // 4beta - get all linked files and copy them (procedure copied from documents type).
  1426. // Get all the src links in this file.
  1427. $match = GetSRCTags($testfile);
  1428. // For each src tag found, do the following:
  1429. foreach ($match as $src) {
  1430. //Check the link kind (web or absolute/relative)
  1431. if (stristr($src, 'http') === false) {
  1432. // Do something only if relative (otherwise the user will be able to see it too anyway).
  1433. // Get a proper URL and remove all './'
  1434. $src = urldecode($src); //mp3
  1435. $src = str_replace('./', '', $src);
  1436. // Remove the player link from the URL (only use the mp3 file).
  1437. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1438. // Remove funny link parts.
  1439. $src = str_replace('?0', '', $src); //mp3
  1440. // The previous lines are used when creating docs with Chamilo Document tool's htmlarea.
  1441. // Rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1442. // and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1443. // created by any other software.
  1444. // Prepare source and destination paths.
  1445. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1446. $dest = $expdir.'/data/'.$src;
  1447. //CopyNCreate($source,$dest);
  1448. rcopy($source, $dest);
  1449. } //else...?
  1450. }
  1451. // 5. Prepare the special "close window" for this test.
  1452. $closewindow = "<html><head><link rel='stylesheet' type='text/css' href='../css/default.css'></head><body>"."<br /><div class='message'>$langHotPotatoesFinished</div></body></html>";
  1453. // Finish is the function of HP to save scores, we insert our scorm function calls to its beginning
  1454. // 'Score' is the variable that tracks the score in HP tests.
  1455. // 6.
  1456. $mit = "function Finish(){";
  1457. $js_content = "var SaveScoreVariable = 0; // This variable is included by Chamilo LP export\n"."function mySaveScore() // This function is included by Chamilo LP export\n"."{\n"." if (SaveScoreVariable==0)\n"." {\n"." SaveScoreVariable = 1;\n".
  1458. //the following function are implemented in SCOFunctions.js
  1459. " exitPageStatus = true;\n"." computeTime();\n"." doLMSSetValue( 'cmi.core.score.raw', Score );\n"." doLMSSetValue( 'cmi.core.lesson_status', 'completed' );\n"." doLMSCommit();\n"." doLMSFinish();\n".
  1460. // " document.write('".$closewindow."');\n".
  1461. //if you insert the previous row, the test does not appear correctly !!!!
  1462. " }\n"."}\n"."function Finish(){\n"." mySaveScore();";
  1463. $start = "<script type='text/javascript'> loadPage(); </script>";
  1464. // 7. Replace the current MIT function call by our set of functions. In clear, transform HP to SCORM.
  1465. $content = str_replace($mit, $js_content, $content);
  1466. // 8. Finally, add the API loading calls (although that might have been done first).
  1467. $content = str_replace("</script>", "</script>".$scocomfiles.$start, $content);
  1468. // 9. Change the filename to add the database ID and export to a new file,
  1469. // setting the circle1_files array for later reuse.
  1470. $filename = str_replace('.htm', $id.'.htm', $filename);
  1471. exporttofile($filename, $LPname, $id, $content);
  1472. return;
  1473. // Chamilo test BEGIN
  1474. case 'Exercise':
  1475. //1 Use the export_exercise() function to do the job of constructing the question's HTML table
  1476. $expcontent .= export_exercise($item_id);
  1477. break;
  1478. // POST BEGIN
  1479. case 'Post':
  1480. // 1. Get the forum post data from the database.
  1481. $tbl_posts =Database::get_course_table(TABLE_FORUM_POST);
  1482. $tbl_posts_text =Database::get_course_table(TOOL_FORUM_POST_TEXT_TABLE);
  1483. $result = Database::query("SELECT * FROM $tbl_posts where c_id = $course_id AND post_id=$item_id");
  1484. $myrow = Database::fetch_array($result);
  1485. // Grabbing the title of the post.
  1486. $sql_titel = "SELECT * FROM $tbl_posts_text WHERE c_id = $course_id AND post_id=".$myrow['post_id'];
  1487. $result_titel = Database::query($sql_titel);
  1488. $myrow_titel = Database::fetch_array($result_titel);
  1489. $posternom = $myrow['nom'];
  1490. $posterprenom = $myrow['prenom'];
  1491. $posttime = $myrow['post_time'];
  1492. $posttext = $myrow_titel['post_text'];
  1493. $posttitle = $myrow_titel['post_title'];
  1494. $posttext = str_replace('"', "'", $posttext);
  1495. //2 Export contents as an HTML table
  1496. $expcontent .= "<table border='0' cellpadding='3' cellspacing='1' width='100%'>
  1497. <tr>
  1498. <td colspan='2' bgcolor='#e6e6e6'><b>$posttitle</b><br />$posttext</td>
  1499. </tr>
  1500. <tr>
  1501. <td colspan='2'></td>
  1502. </tr>
  1503. <tr>
  1504. <td bgcolor='#cccccc' align='left'>$lg_author : $posterprenom $posternom</td>
  1505. <td align='right' bgcolor='#cccccc'>$lg_date : $posttime</td>
  1506. </tr>
  1507. <tr><td colspan='2' height='10'></td></tr>
  1508. </table>";
  1509. break;
  1510. // NOT IMPLEMENTED ITEMS BEGIN
  1511. case 'Forum':
  1512. case 'Thread':
  1513. case 'Dropbox':
  1514. case 'Assignments':
  1515. case 'Groups':
  1516. case 'Users':
  1517. // 1. Instead of building something, put an info message.
  1518. $langItemMissing1 = "There was a ";
  1519. $langItemMissing2 = "page (step) here in the original Chamilo Learning Path.";
  1520. $expcontent .= "<div class='message'>$langItemMissing1 $item_type $langItemMissing2</div>";
  1521. break;
  1522. // Link BEGIN
  1523. case 'Link _self':
  1524. case 'Link _blank':
  1525. // 1. Get the link data from the database.
  1526. $TABLETOOLLINK = Database :: get_course_link_table();
  1527. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$item_id");
  1528. $myrow = Database::fetch_array($result);
  1529. $thelink = $myrow['url'];
  1530. // 2. Check the link type (open in blank page or in current page).
  1531. if ($item_type == 'Link _blank')
  1532. {
  1533. $target = '_blank';
  1534. }
  1535. // 3. Write the link to the export string.
  1536. $expcontent .= "<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='".$target."'>$LPname</a>";
  1537. // 4. Change the element type for later changes (this is lost, however, so useless here).
  1538. $item_type = 'Link'; // To put this to the filename.
  1539. //$LPname="<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target=".$target.">$LPname</a>";
  1540. // I am still not sure about Link export : to export them as files or they can appear in the TOC at once ?
  1541. // To enable the second possibility, unrem the row $LPname=...
  1542. break;
  1543. }
  1544. // Now we add the Done button and the initialize function : loadpage()
  1545. // not in the case of Documents, HotP
  1546. if ($item_type != 'Exercise' and ($add_scorm_communications === true)) {
  1547. $expcontent .= $donebutton;
  1548. }
  1549. // End the export string with valid HTML tags.
  1550. $expcontent .= "</body></html>";
  1551. // Prepare new file name.
  1552. $filename = $item_type.$id.".htm";
  1553. // Write the export content to the new file.
  1554. exporttofile('data/'.$filename, $LPname, $id, $expcontent);
  1555. }
  1556. /**
  1557. * This function exports the given item's description into a separate file
  1558. * @param integer Item id
  1559. * @param string Item type
  1560. * @param string Description
  1561. * @return void
  1562. */
  1563. function exportdescription($id, $item_type, $description) {
  1564. global $expdir;
  1565. $filename = $item_type.$id.'.desc';
  1566. $expcontent = $description;
  1567. exporttofile($expdir.$filename, 'description_of_'.$item_type.$id, 'description_of_item_'.$id, $expcontent);
  1568. }
  1569. /**
  1570. * This function deletes an entire directory
  1571. * @param string The directory path
  1572. * @return boolean True on success, false on failure
  1573. */
  1574. function deldir($dir) {
  1575. $dh = opendir($dir);
  1576. while ($file = readdir($dh)) {
  1577. if ($file != '.' && $file != '..') {
  1578. $fullpath = $dir.'/'.$file;
  1579. if (!is_dir($fullpath)) {
  1580. unlink($fullpath);
  1581. } else {
  1582. deldir($fullpath);
  1583. }
  1584. }
  1585. }
  1586. closedir($dh);
  1587. if (rmdir($dir)) {
  1588. return true;
  1589. }
  1590. return false;
  1591. }
  1592. /**
  1593. * Export SCORM content into a zip file
  1594. *
  1595. * Basically, all this function does is put the scorm directory back into a zip file (like the one
  1596. * that was most probably used to import the course at first)
  1597. * @deprecated this function is only called in the newscorm/scorm_admin.php which is deprecated
  1598. *
  1599. * @param string Name of the SCORM path (or the directory under which it resides)
  1600. * @param array Not used right now. Should replace the use of global $_course
  1601. * @return void
  1602. * @author imandak80
  1603. */
  1604. function exportSCORM($scormname, $course) {
  1605. global $_course;
  1606. // Initialize.
  1607. $tmpname = api_get_path(SYS_COURSE_PATH).$_course['path'].'/scorm';
  1608. $zipfoldername = $tmpname.$scormname;
  1609. $zipfilename = $zipfoldername.'.zip';
  1610. // Create zipfile of given directory.
  1611. include_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  1612. $zip_folder = new PclZip($zipfilename);
  1613. $list = 1;
  1614. //$list = $zip_folder->create($zipfoldername.'/',PCLZIP_OPT_REMOVE_PATH,$tmpname.$scormname."/"); // whitout folder
  1615. $list = $zip_folder->create($zipfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $tmpname);
  1616. if ($list == 0) {
  1617. //echo "Error : ".$zip_folder->errorInfo(true);
  1618. }
  1619. // Send to client.
  1620. DocumentManager :: file_send_for_download($zipfilename, false, basename($scormname.'.zip'));
  1621. // Clear.
  1622. my_delete($zipfilename);
  1623. }
  1624. /**
  1625. * This function returns an xml tag
  1626. * $data behaves as the content in case of full tags
  1627. * $data is an array of attributes in case of returning an opening tag
  1628. * @param string
  1629. * @param string
  1630. * @param array
  1631. * @param string
  1632. * @return string
  1633. */
  1634. function xmltagwrite($tagname, $which, $data, $linebreak = 'yes') {
  1635. switch ($which) {
  1636. case 'open':
  1637. $tag = '<'.$tagname;
  1638. $i = 0;
  1639. while ($data[0][$i]) {
  1640. $tag .= ' '.$data[0][$i]."=\"".$data[1][$i]."\"";
  1641. $i ++;
  1642. }
  1643. if ($tagname == 'file') {
  1644. $closing = '/';
  1645. }
  1646. $tag .= $closing.'>';
  1647. if ($linebreak != 'no_linebreak') {
  1648. $tag .= "\n";
  1649. }
  1650. break;
  1651. case 'close':
  1652. $tag = '</'.$tagname.'>';
  1653. if ($linebreak != 'no_linebreak') {
  1654. $tag .= "\n";
  1655. }
  1656. break;
  1657. case 'full':
  1658. $tag = '<'.$tagname;
  1659. $tag .= '>'.$data.'</'.$tagname.'>';
  1660. if ($linebreak != 'no_linebreak') {
  1661. $tag .= "\n";
  1662. }
  1663. break;
  1664. }
  1665. return $tag;
  1666. }
  1667. /**
  1668. * Gets the tags of the file given as parameter
  1669. *
  1670. * if $filename is not found, GetSRCTags(filename) will return FALSE
  1671. * @param string File path
  1672. * @return mixed Array of strings on success, false on failure
  1673. * @author unknown
  1674. * @author Included by imandak80
  1675. */
  1676. function GetSRCTags($fileName) {
  1677. if (!($fp = fopen($fileName, 'r'))) {
  1678. // Iif file can't be opened, return false.
  1679. return false;
  1680. }
  1681. // Read file contents.
  1682. $contents = fread($fp, filesize($fileName));
  1683. fclose($fp);
  1684. $matches = array();
  1685. $srcList = array();
  1686. // Get all src tags contents in this file. Use multi-line search.
  1687. preg_match_all('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/mi', $contents, $matches); // Get the img src as contained between " or '
  1688. foreach ($matches[3] as $match) {
  1689. if (!in_array($match, $srcList)) {
  1690. $srcList[] = $match;
  1691. }
  1692. }
  1693. if (count($srcList) == 0) {
  1694. return false;
  1695. }
  1696. return $srcList;
  1697. }
  1698. /**
  1699. * Copy file and create directories in the path if needed.
  1700. *
  1701. * @param string $source Source path
  1702. * @param string $dest Destination path
  1703. * @return boolean true on success, false on failure
  1704. */
  1705. function CopyNCreate($source, $dest) {
  1706. if (strcmp($source, $dest) == 0)
  1707. return false;
  1708. $dir = '';
  1709. $tdest = explode('/', $dest);
  1710. for ($i = 0; $i < sizeof($tdest) - 1; $i ++) {
  1711. $dir = $dir.$tdest[$i].'/';
  1712. if (!is_dir($dir))
  1713. if (!mkdir($dir, api_get_permissions_for_new_directories()))
  1714. return false;
  1715. }
  1716. if (!copy($source, $dest))
  1717. return false;
  1718. return true;
  1719. }
  1720. function rcopy($source, $dest) {
  1721. //error_log($source." -> ".$dest, 0);
  1722. if (!file_exists($source)) {
  1723. //error_log($source." does not exist", 0);
  1724. return false;
  1725. }
  1726. if (is_dir($source)) {
  1727. //error_log($source." is a dir", 0);
  1728. // This is a directory.
  1729. // Remove trailing '/'
  1730. if (strrpos($source, '/') == sizeof($source) - 1) {
  1731. $source = substr($source, 0, size_of($source) - 1);
  1732. }
  1733. if (strrpos($dest, '/') == sizeof($dest) - 1) {
  1734. $dest = substr($dest, 0, size_of($dest) - 1);
  1735. }
  1736. if (!is_dir($dest)) {
  1737. $res = @mkdir($dest, api_get_permissions_for_new_directories());
  1738. if ($res !== false) {
  1739. return true;
  1740. } else {
  1741. // Remove latest part of path and try creating that.
  1742. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/')))) {
  1743. return @mkdir($dest, api_get_permissions_for_new_directories());
  1744. } else {
  1745. return false;
  1746. }
  1747. }
  1748. }
  1749. return true;
  1750. } else {
  1751. // This is presumably a file.
  1752. //error_log($source." is a file", 0);
  1753. if (!@ copy($source, $dest)) {
  1754. //error_log("Could not simple-copy $source", 0);
  1755. $res = rcopy(dirname($source), dirname($dest));
  1756. if ($res === true) {
  1757. //error_log("Welcome dir created", 0);
  1758. return @ copy($source, $dest);
  1759. } else {
  1760. return false;
  1761. //error_log("Error creating path", 0);
  1762. }
  1763. } else {
  1764. //error_log("Could well simple-copy $source", 0);
  1765. return true;
  1766. }
  1767. }
  1768. }