learnpath_functions.inc.php 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  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. /**
  26. * This function deletes an item
  27. * @param integer $id: the item we want to delete
  28. * @return boolean True if item was deleted, false if not found or error
  29. */
  30. function deleteitem($id) {
  31. $course_id = api_get_course_int_id();
  32. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  33. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  34. // Get the display order for this item before it is deleted.
  35. $sql = "SELECT display_order, parent_item_id FROM $tbl_lp_item WHERE c_id = $course_id AND id=$id";
  36. $result = Database::query($sql);
  37. if (Database::num_rows($result) == 0) {
  38. return false;
  39. }
  40. $row = Database::fetch_row($result);
  41. $display_order = $row[0];
  42. $parent_item_id = $row[1];
  43. // Delete the item.
  44. $sql = "DELETE FROM $tbl_learnpath_item WHERE c_id = $course_id AND id='$id'";
  45. $result = Database::query($sql);
  46. if ($result === false) {
  47. return false;
  48. }
  49. // Update the other items and chapters.
  50. $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_item_id";
  51. $result = Database::query($sql);
  52. $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_item_id";
  53. $result = Database::query($sql);
  54. return true;
  55. }
  56. /**
  57. * This function deletes a module(chapter) and all its steps(items).
  58. *
  59. * @param integer id of the chapter we want to delete
  60. * @return boolean True on success and false if not found or error
  61. */
  62. function deletemodule($parent_item_id) {
  63. global $learnpath_id;
  64. $course_id = api_get_course_int_id();
  65. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  66. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  67. // Added for multi-level behaviour - slightly recursive.
  68. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND lp_id=$learnpath_id";
  69. $result = Database::query($sql);
  70. while ($row = Database::fetch_array($result)) {
  71. if ($row['parent_item_id'] == $parent_item_id) {
  72. // Delete every subchapter.
  73. if (deletemodule($row['id']) === false) {
  74. return false;
  75. }
  76. }
  77. }
  78. // Get this chapter's display order.
  79. $sql = "SELECT display_order, parent_item_id FROM $tbl_learnpath_chapter
  80. WHERE c_id = $course_id AND id=$parent_item_id and lp_id=$learnpath_id";
  81. $result = Database::query($sql);
  82. if (Database::num_rows($result) == 0) {
  83. return false;
  84. }
  85. $row = Database::fetch_row($result);
  86. $display_order = $row[0];
  87. $parent_id = $row[1];
  88. // Delete the chapter itself.
  89. $sql = "DELETE FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (id=$parent_item_id and lp_id=$learnpath_id)";
  90. $result = Database::query($sql);
  91. // Delete items from that chapter.
  92. $sql2 = "DELETE FROM $tbl_learnpath_item WHERE c_id = $course_id AND parent_item_id=$parent_item_id";
  93. $result = Database::query($sql2);
  94. // Update all other chapters accordingly.
  95. $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";
  96. $result = Database::query($sql);
  97. $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";
  98. $result = Database::query($sql);
  99. return true;
  100. }
  101. /**
  102. * This function deletes an entire path.
  103. *
  104. * @param integer $id: the path we want to delete
  105. * @return void
  106. */
  107. function deletepath($path_id) {
  108. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  109. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  110. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  111. $course_id = api_get_course_int_id();
  112. $sql = "DELETE FROM $tbl_learnpath_main WHERE c_id = $course_id AND lp_id='$path_id'";
  113. $result = Database::query($sql);
  114. //@TODO check how this function is used before uncommenting the following
  115. //also delete all elements inside that path
  116. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND lp_id = $path_id";
  117. $result = Database::query($sql);
  118. while ($row = Database::fetch_array($result)) {
  119. deletemodule($row['id']);
  120. }
  121. }
  122. /**
  123. * This function moves an item.
  124. *
  125. * @param string $direction: move the given chapter up or down
  126. * @param integer Item ID
  127. * @param integer $moduleid: the id of the chapter the element resides in
  128. * @return boolean Returns false on error
  129. * @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'
  130. */
  131. function moveitem($direction, $id, $moduleid, $type = 'item') {
  132. global $learnpath_id;
  133. $course_id = api_get_course_int_id();
  134. $tbl_learnpath_item = Database::get_course_table(TABLE_LEARNPATH_ITEM);
  135. $tbl_learnpath_chapter = Database::get_course_table(TABLE_LEARNPATH_CHAPTER);
  136. $tree = get_learnpath_tree($learnpath_id);
  137. $orig_order = 0;
  138. $orig_type = '';
  139. $orig_id = $id;
  140. foreach ($tree[$moduleid] as $row) {
  141. // If this is the element we want (be it a chapter or an item), get its data.
  142. if (($row['id'] == $id) && ($row['type'] == $type)) {
  143. $orig_order = $row['display_order'];
  144. $orig_type = $row['type'];
  145. break;
  146. }
  147. }
  148. $dest_order = 0;
  149. $dest_type = '';
  150. $dest_id = 0;
  151. if ($direction == 'up') {
  152. if (!empty ($tree[$moduleid][$orig_order - 1])) {
  153. $dest_order = $orig_order - 1;
  154. $dest_type = $tree[$moduleid][$orig_order - 1]['type'];
  155. $dest_id = $tree[$moduleid][$orig_order - 1]['id'];
  156. } else {
  157. return false;
  158. }
  159. } else {
  160. // Move down.
  161. if (!empty ($tree[$moduleid][$orig_order + 1])) {
  162. $dest_order = $orig_order + 1;
  163. $dest_type = $tree[$moduleid][$orig_order + 1]['type'];
  164. $dest_id = $tree[$moduleid][$orig_order + 1]['id'];
  165. } else {
  166. return false;
  167. }
  168. }
  169. $sql1 = '';
  170. $sql2 = '';
  171. if ($orig_type == 'chapter') {
  172. $sql1 = "UPDATE $tbl_learnpath_chapter SET display_order = ".$dest_order." WHERE c_id = $course_id AND (id=$orig_id and parent_item_id=$moduleid)";
  173. } elseif ($orig_type == 'item') {
  174. $sql1 = "UPDATE $tbl_learnpath_item SET display_order = ".$dest_order." WHERE c_id = $course_id AND (id=$orig_id and parent_item_id=$moduleid)";
  175. } else {
  176. return false;
  177. }
  178. if ($dest_type == 'chapter') {
  179. $sql2 = "UPDATE $tbl_learnpath_chapter SET display_order = ".$orig_order." WHERE c_id = $course_id AND (id='$dest_id' and parent_item_id=$moduleid)";
  180. } elseif ($dest_type == 'item') {
  181. $sql2 = "UPDATE $tbl_learnpath_item SET display_order = ".$orig_order." WHERE c_id = $course_id AND (id='$dest_id' and parent_item_id=$moduleid)";
  182. } else {
  183. return false;
  184. }
  185. Database::query($sql1);
  186. Database::query($sql2);
  187. }
  188. /**
  189. * This function moves a module (also called chapter or category).
  190. *
  191. * @param string $direction: move the given chapter up or down
  192. * @param integer $id: the id of the chapter we want to move
  193. * @return void
  194. */
  195. function movemodule($direction, $id) {
  196. global $learnpath_id;
  197. $course_id = api_get_course_int_id();
  198. $tbl_learnpath_chapter = Database::get_course_table(TABLE_LEARNPATH_CHAPTER);
  199. if ($direction == 'up') {
  200. $sortDirection = 'DESC';
  201. } else {
  202. $sortDirection = 'ASC';
  203. }
  204. // Select all chapters of first level (parent_item_id = 0).
  205. $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";
  206. $result = Database::query($sql);
  207. $previousrow = '';
  208. // See similar comment in moveitem() function.
  209. // @TODO: this only works for chapters in multi-level mode. Why not gather
  210. // this function and moveitem to keep only one multi-uses function?
  211. while ($row = Database::fetch_array($result)) {
  212. // Step 2: Performing the move (only happens when passed trhough step 1 at least once).
  213. if (!empty ($this_cat_order)) {
  214. $next_cat_order = $row['display_order'];
  215. $next_cat_id = $row['id'];
  216. $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)";
  217. $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)";
  218. Database::query($sql1);
  219. Database::query($sql2);
  220. unset ($this_cat_order);
  221. unset ($next_cat_order);
  222. unset ($next_cat_id);
  223. break;
  224. }
  225. // Step 1: Looking for the order of the row we want to move.
  226. if ($row['id'] == $id) {
  227. $this_cat_order = $row['display_order'];
  228. $this_cat_id = $id;
  229. }
  230. }
  231. }
  232. /**
  233. * Inserts a new element in a learnpath table (item or chapter)
  234. * @param string Element type ('chapter' or 'item')
  235. * @param string Chapter name
  236. * @param string Chapter description (optional)
  237. * @param integer Parent chapter ID (default: 0)
  238. * @param integer Learnpath ID
  239. * @param mixed If type 'item', then array(prereq_id=>value, prereq_..)
  240. * @return integer The new chapter ID, or false on failure
  241. * @TODO Finish this function before it is used. Currently only chapters can be added using it.
  242. * @note This function is currently never used!
  243. */
  244. function insert_item($type = 'item', $name, $chapter_description = '', $parent_id = 0, $learnpath_id = 0, $params = null) {
  245. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  246. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  247. $course_id = api_get_course_int_id();
  248. // Getting the last order number from the chapters table, in this learnpath, for the parent chapter given.
  249. $sql = "SELECT * FROM $tbl_learnpath_chapter
  250. WHERE c_id = $course_id AND lp_id=$learnpath_id AND parent_item_id = $parent_id
  251. ORDER BY display_order DESC";
  252. $result = Database::query($sql);
  253. $row = Database::fetch_array($result);
  254. $last_chapter_order = $row['display_order'];
  255. // Getting the last order number of the items.
  256. $sql = "SELECT * FROM $tbl_learnpath_item
  257. WHERE c_id = $course_id AND parent_item_id = $parent_id
  258. ORDER BY display_order DESC";
  259. $result = Database::query($sql);
  260. $row = Database::fetch_array($result);
  261. $last_item_order = $row['display_order'];
  262. $new_order = max($last_chapter_order, $last_item_order) + 1;
  263. if ($type === 'chapter') {
  264. $sql = "INSERT INTO $tbl_learnpath_chapter (c_id, lp_id, chapter_name, chapter_description, display_order)
  265. VALUES ( $course_id,
  266. '".domesticate($learnpath_id)."',
  267. '".domesticate(htmlspecialchars($name))."',
  268. '".domesticate(htmlspecialchars($chapter_description))."',
  269. $new_order )";
  270. $result = Database::query($sql);
  271. if ($result === false) {
  272. return false;
  273. }
  274. $id = Database :: insert_id();
  275. } elseif ($type === 'item') {
  276. $sql = "INSERT INTO $tbl_learnpath_item (c_id, parent_item_id, item_type, display_order) VALUES
  277. ($course_id, '".domesticate($parent_id)."','".domesticate(htmlspecialchars($type))."', $new_order )";
  278. $result = Database::query($sql);
  279. if ($result === false) {
  280. return false;
  281. }
  282. $id = Database :: insert_id();
  283. }
  284. return $id;
  285. }
  286. /**
  287. * This function returns an array with all the learnpath categories/chapters
  288. * @return array List of learnpath chapter titles
  289. */
  290. function array_learnpath_categories() {
  291. $course_id = api_get_course_int_id();
  292. global $learnpath_id;
  293. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  294. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (lp_id=$learnpath_id) ORDER BY display_order ASC";
  295. $result = Database::query($sql);
  296. while ($row = Database::fetch_array($result)) {
  297. $array_learnpath_categories[] = array($row['id'], $row['chapter_name']);
  298. }
  299. //$array_learnpath_categories = array($array_learnpath_categories_name, $array_learnpath_categories_id);
  300. return $array_learnpath_categories;
  301. }
  302. /**
  303. * Displays the learnpath chapters(=modules,categories) and their contents.
  304. * @param integer Chapter ID to display now (enables recursive behaviour)
  305. * @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
  306. * @param integer Level (the depth of the call - helps in display)
  307. * @todo eliminate all global $lang declarations, use get_lang, improve structure.
  308. * @author Denes Nagy
  309. * @author Roan Embrechts
  310. * @author Yannick Warnier <yannick.warnier@beeznest.com> - complete redesign for multi-level learnpath chapters
  311. */
  312. function display_learnpath_chapters($parent_item_id = 0, $tree = array (), $level = 0) {
  313. //error_log('New LP - In learnpath_functions::display_learnpath_chapters', 0);
  314. global $color2;
  315. global $xml_output;
  316. global $learnpath_id;
  317. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  318. // @todo: coding standards: Language variables are CaMMelCaSe, all other variables should use the underscoring method.
  319. $lang_move_down = get_lang('_move_down');
  320. $lang_move_up = get_lang('lang_move_up');
  321. $lang_edit_learnpath_module = get_lang('lang_edit_learnpath_module');
  322. $lang_delete_learnpath_module = get_lang('lang_delete_learnpath_module');
  323. $lang_nochapters = get_lang('lang_nochapters');
  324. $lang_prerequisites = get_lang('lang_prerequisites');
  325. $lang_prerequisites_limit = get_lang('lang_prerequisites_limit');
  326. $lang_add_learnpath_item = get_lang('lang_add_learnpath_item');
  327. $lang_add_item = get_lang('lang_add_item');
  328. $lang_title_and_desc = get_lang('lang_title_and_desc');
  329. $lang_change_order = get_lang('lang_change_order');
  330. $lang_add_prereqi = get_lang('lang_add_prereqi');
  331. $lang_add_title_and_desc = get_lang('lang_add_title_and_desc');
  332. $lang_delete = get_lang('Delete');
  333. if ($parent_item_id === 0) {
  334. // This is the first time we use the function, define the tree and display learnpath name.
  335. $tree = get_learnpath_tree($learnpath_id);
  336. $num_modules = count($tree);
  337. //$num_modules = Database::num_rows($result);
  338. if ($num_modules == 0) {
  339. // do not diplay useless information
  340. //echo "<tr><td>&nbsp;$lang_nochapters</td></tr>";
  341. } else {
  342. echo " <tr align='center' valign='top'><td><b>&nbsp;$lang_title_and_desc </b></td>\n"." <td><b>&nbsp;$lang_add_item </b></td>\n";
  343. if (is_prereq($learnpath_id)) {
  344. echo " <td bgcolor='#ddddee'><b>&nbsp;$lang_prerequisites_limit </b></td>\n";
  345. } else {
  346. echo " <td><b>&nbsp;$lang_prerequisites </b></td>\n";
  347. }
  348. echo " <td colspan='2'><b>&nbsp;$lang_change_order </b></td><td><b>&nbsp;$lang_add_prereqi </b></td>\n"." <td><b>&nbsp;$lang_add_title_and_desc </b></td><td><b>&nbsp;$lang_delete </b></td>\n"." </tr>\n";
  349. }
  350. }
  351. $i = 1;
  352. $counter = 0;
  353. $num_modules = count($tree[$parent_item_id]);
  354. //while ($row = Database::fetch_array($result))
  355. if (isset ($tree[$parent_item_id])) {
  356. foreach ($tree[$parent_item_id] as $row) {
  357. if ($row['item_type'] === 'dokeos_chapter') {
  358. $xml_output .= "<chapter>";
  359. $xml_output .= "<chaptertitle>".$row['title']."</chaptertitle>";
  360. $xml_output .= "<chapterdescription>".$row['description']."</chapterdescription>";
  361. $counter ++;
  362. if (($counter % 2) == 0) {
  363. $oddclass = 'row_odd';
  364. } else {
  365. $oddclass = 'row_even';
  366. }
  367. //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='$lang_add_learnpath_item'></a></td>\n"." <td";
  368. 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='$lang_add_learnpath_item'></a></td>\n"." <td";
  369. if (is_prereq($learnpath_id)) {
  370. echo " bgcolor='#ddddee'";
  371. }
  372. echo ">".$row['prerequisite']."</td>\n";
  373. // Showing the edit, delete and move icons.
  374. if (api_is_allowed_to_edit()) {
  375. $myaction = 'move_item';
  376. if ($i < $num_modules) {
  377. // If we are still under the number of chapters in this section, show "move down".
  378. //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=\"$lang_move_down\">"."</a></td>\n";
  379. 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=\"$lang_move_down\">"."</a></td>\n";
  380. } else {
  381. echo ' <td align="center">&nbsp;</td>'."\n";
  382. }
  383. if ($i > 1) {
  384. //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=\"$lang_move_up\">"."</a>"."</td>\n";
  385. 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=\"$lang_move_up\">"."</a>"."</td>\n";
  386. } else {
  387. echo ' <td align="center">&nbsp;</td>'."\n";
  388. }
  389. echo " <td align='center'>&nbsp;</td>\n";
  390. //echo " <td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=editmodule&amp;id=".$row['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>"."<img src=\"../img/edit.gif\" border=\"0\" title=\"$lang_edit_learnpath_module\">"."</a>"."</td>\n";
  391. 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=\"$lang_edit_learnpath_module\">"."</a>"."</td>\n";
  392. //echo " <td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=deletemodule&amp;id=".$row['id']."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9 onclick=\"javascript: return confirmation('".$row['chapter_name']."');\">"."<img src=\"../img/delete.gif\" border=\"0\" title=\"$lang_delete_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=\"$lang_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'>$lang_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=\"$lang_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=\"$lang_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=\"$lang_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=\"$lang_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=\"$lang_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 $lang_prerequisites, $lang_move_down, $lang_move_up, $lang_edit_learnpath_item, $lang_delete_learnpath_item, $learnpath_id, $lang_add_prereq, $lang_prereq_deleted_error, $lang_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>$lang_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>$lang_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=\"$lang_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=\"$lang_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=\"$lang_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=\"$lang_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=\"$lang_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. require_once '../exercice/exercise.class.php';
  1013. require_once '../exercice/question.class.php';
  1014. require_once '../exercice/answer.class.php';
  1015. require_once '../exercice/exercise.lib.php';
  1016. // Answer types
  1017. define('UNIQUE_ANSWER', 1);
  1018. define('MULTIPLE_ANSWER', 2);
  1019. define('FILL_IN_BLANKS', 3);
  1020. define('MATCHING', 4);
  1021. define('FREE_ANSWER', 5);
  1022. $TBL_EXERCISES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1023. /* Clears the exercise session */
  1024. if (isset ($_SESSION['objExercise'])) {
  1025. api_session_unregister('objExercise');
  1026. }
  1027. if (isset ($_SESSION['objQuestion'])) {
  1028. api_session_unregister('objQuestion');
  1029. }
  1030. if (isset ($_SESSION['objAnswer'])) {
  1031. api_session_unregister('objAnswer');
  1032. }
  1033. if (isset ($_SESSION['questionList'])) {
  1034. api_session_unregister('questionList');
  1035. }
  1036. if (isset ($_SESSION['exerciseResult'])) {
  1037. api_session_unregister('exerciseResult');
  1038. }
  1039. // If the object is not in the session:
  1040. if (!isset ($_SESSION['objExercise'])) {
  1041. // Construction of Exercise.
  1042. $objExercise = new Exercise();
  1043. $sql = "SELECT title,description,sound,type,random,active FROM $TBL_EXERCISES WHERE id='$exerciseId'";
  1044. // If the specified exercise doesn't exist or is disabled:
  1045. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !api_is_allowed_to_edit() && ($origin != 'learnpath'))) {
  1046. die($langExerciseNotFound);
  1047. }
  1048. // Saves the object into the session.
  1049. api_session_register('objExercise');
  1050. }
  1051. $exerciseTitle = $objExercise->selectTitle();
  1052. $exerciseDescription = $objExercise->selectDescription();
  1053. $exerciseSound = $objExercise->selectSound();
  1054. $randomQuestions = $objExercise->isRandom();
  1055. $exerciseType = $objExercise->selectType();
  1056. if (!isset ($_SESSION['questionList'])) {
  1057. // Selects the list of question ID.
  1058. $questionList = $randomQuestions ? $objExercise->selectRandomList() : $objExercise->selectQuestionList();
  1059. // Saves the question list into the session.
  1060. api_session_register('questionList');
  1061. }
  1062. $nbrQuestions = sizeof($questionList);
  1063. // If questionNum comes from POST and not from GET:
  1064. if (!$questionNum || $_POST['questionNum']) {
  1065. // Only used for sequential exercises (see $exerciseType).
  1066. if (!$questionNum) {
  1067. $questionNum = 1;
  1068. } else {
  1069. $questionNum ++;
  1070. }
  1071. }
  1072. $exerciseTitle = text_filter($exerciseTitle);
  1073. $test .= "<h3>".$exerciseTitle."</h3>";
  1074. if (!empty ($exerciseSound)) {
  1075. $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>";
  1076. }
  1077. $exerciseDescription = text_filter($exerciseDescription);
  1078. // Writing the .js file with to check the correct answers begin.
  1079. $scriptfilename = "Exercice".$item_id.".js";
  1080. $s = "<script type=\"text/javascript\" src='../js/".$scriptfilename."'></script>";
  1081. $test .= $s;
  1082. $content = "function evaluate() {
  1083. alert('Test evaluated.');
  1084. }
  1085. ";
  1086. if (!$handle = fopen($expdir.'/js/'.$scriptfilename, 'w')) {
  1087. echo "Cannot open file ($scriptfilename)";
  1088. }
  1089. if (fwrite($handle, $content) === false) {
  1090. echo "Cannot write to file ($filename)";
  1091. exit;
  1092. }
  1093. fclose($handle);
  1094. // Writing the .js file with to check the correct answers end.
  1095. $s = "
  1096. <p>$exerciseDescription</p>
  1097. <table width='100%' border='0' cellpadding='1' cellspacing='0'>
  1098. <form method='post' action=''><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1099. <input type='hidden' name='formSent' value='1' />
  1100. <input type='hidden' name='exerciseType' value='".$exerciseType."' />
  1101. <input type='hidden' name='questionNum' value='".$questionNum."' />
  1102. <input type='hidden' name='nbrQuestions' value='".$nbrQuestions."' />
  1103. <tr>
  1104. <td>
  1105. <table width='100%' cellpadding='4' cellspacing='2' border='0'>";
  1106. $exerciseType = 1; // So to list all questions in one page.
  1107. $test .= $s;
  1108. $i = 0;
  1109. foreach ($questionList as $questionId) {
  1110. $i ++;
  1111. // For sequential exercises.
  1112. if ($exerciseType == 2) {
  1113. // If it is not the right question, goes to the next loop iteration.
  1114. if ($questionNum != $i) {
  1115. continue;
  1116. } else {
  1117. // if the user has already answered this question:
  1118. if (isset ($exerciseResult[$questionId])) {
  1119. // Construction of the Question object.
  1120. $objQuestionTmp = new Question();
  1121. // Reads question informations.
  1122. $objQuestionTmp->read($questionId);
  1123. $questionName = $objQuestionTmp->selectTitle();
  1124. // Destruction of the Question object.
  1125. unset ($objQuestionTmp);
  1126. $test .= '<tr><td>'.get_lang('AlreadyAnswered').' &quot;'.$questionName.'&quot;</td></tr>';
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. echo $s = "<tr bgcolor='#e6e6e6'><td valign='top' colspan='2'>".get_lang('Question')." ";
  1132. // Call the showQuestion() function from exercise.lib.php. This basically displays the question in a table.
  1133. $test .= showQuestion($questionId, false, 'export', $i);
  1134. } // end foreach()
  1135. $s = "</table></td></tr><tr><td><br/><input type='button' value='".$langOk."' onclick=\"javascript: evaluate(); alert('Evaluated.');\">";
  1136. $s .= "</td></tr></form></table>";
  1137. $s .= "<script type='text/javascript'> loadPage(); </script>";
  1138. $b = 2;
  1139. $test .= $s;
  1140. return ($test);
  1141. }
  1142. /**
  1143. * This function exports the given item
  1144. * @param integer Id from learnpath_items table
  1145. * @param integer Item id
  1146. * @param string Itm type
  1147. * @param boolean Shall the SCORM communications features be added? (true). Default: false.
  1148. * @return void (outputs a zip file)
  1149. * @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
  1150. */
  1151. function exportitem($id, $item_id, $item_type, $add_scorm_communications = false) {
  1152. $course_id = api_get_course_int_id();
  1153. global $circle1_files, $expdir, $_course, $_SESSION, $GLOBALS;
  1154. global $timeNoSecFormat, $dateFormatLong, $language_interface, $langPubl, $langDone, $langThisCourseDescriptionIsEmpty, $lang_course_description, $lang_introduction_text, $_cid, $langHotPotatoesFinished, $lang_author, $lang_date, $lang_groups, $lang_users, $lang_ass, $lang_dropbox, $test, $langQuestion;
  1155. $libp = api_get_path(SYS_CODE_PAH);
  1156. include_once $libp.'exercice/exercise.class.php';
  1157. include_once $libp.'question.class.php';
  1158. include_once $libp.'answer.class.php';
  1159. include_once $libp.'exercise.lib.php';
  1160. $langLasting = '';//avoid code parser warning
  1161. include_once $libp.'lang/english/announcements.inc.php'; //this line is here only for $langPubl in announcements
  1162. include_once $libp.'lang/'.$language_interface.'/announcements.inc.php'; //this line is here only for $langPubl in announcements
  1163. include_once $libp.'lang/english/agenda.inc.php'; //this line is here only for $langLasting
  1164. include_once $libp.'lang/'.$language_interface.'/agenda.inc.php'; //this line is here only for $langLasting
  1165. include_once $libp.'lang/english/course_description.inc.php'; //this line is here only for $langThisCourseDescriptionIsEmpty
  1166. include_once $libp.'lang/'.$language_interface.'/course_description.inc.php'; // -||-
  1167. include_once $libp.'lang/english/resourcelinker.inc.php';
  1168. include_once $libp.'lang/'.$language_interface.'/resourcelinker.inc.php';
  1169. include_once $libp.'lang/english/learnpath.inc.php';
  1170. include_once $libp.'lang/'.$language_interface.'/learnpath.inc.php';
  1171. include_once $libp.'lang/english/exercice.inc.php';
  1172. include_once $libp.'lang/'.$language_interface.'/exercice.inc.php';
  1173. include_once '../resourcelinker/resourcelinker.inc.php';
  1174. $LPname = display_addedresource_link_in_learnpath($item_type, $item_id, '', $id, 'builder', 'nolink');
  1175. $expcontent = "<!--
  1176. This is an exported file from Chamilo Learning Path belonging to a Scorm compliant content package.
  1177. Do not modify or replace individually.
  1178. Export module author : Denes Nagy <darkden@evk.bke.hu>
  1179. -->
  1180. ";
  1181. // Files needed for communicating with the scos.
  1182. $scocomfiles = "<script type='text/javascript' src='../js/APIWrapper.js'></script>"."<script type='text/javascript' src='../js/SCOFunctions.js'></script>";
  1183. $expcontent .= '<html><head><link rel="stylesheet" href="../css/default.css" type="text/css" media="screen,projection" />'.$scocomfiles.'</head><body>';
  1184. $donebutton .= "<script type='text/javascript'>
  1185. /* <![CDATA[ */
  1186. loadPage();
  1187. var studentName = '!';
  1188. var lmsStudentName = doLMSGetValue( 'cmi.core.student_name' );
  1189. if ( lmsStudentName != '' )
  1190. {
  1191. studentName = ' ' + lmsStudentName + '!';
  1192. }
  1193. /* ]]> */
  1194. </script>
  1195. <br /><br />
  1196. <form><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1197. <table cols='3' width='100%' align='center'>
  1198. <tr>
  1199. <td align='middle'><input type = 'button' value = ' ".$langDone." ' onclick = \"javascript: doQuit('completed');\" id='button2' name='button2'></td>
  1200. </tr>
  1201. </table>
  1202. </form>";
  1203. /**
  1204. * Switch between the different element types, namely:
  1205. * - Agenda
  1206. * - Ad_Valvas
  1207. * - Course_description
  1208. * - Document
  1209. * - Introduction_text
  1210. * - HotPotatoes
  1211. * - Exercise
  1212. * - Post
  1213. * - Forum ]
  1214. * - Thread ]
  1215. * - Dropbox ]
  1216. * - Assignments ] These elements are all replaced by a simple message in the exported document.
  1217. * - Groups ]
  1218. * - Users ]
  1219. * - Link _self
  1220. * - Link _blank
  1221. */
  1222. switch ($item_type) {
  1223. // AGENDA BEGIN
  1224. case 'Agenda':
  1225. // 1. Get agenda event data from the database table.
  1226. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  1227. $sql = "SELECT * FROM ".$TABLEAGENDA." where c_id = $course_id AND (id=$item_id)";
  1228. $result = Database::query($sql);
  1229. // 2. Prepare table output.
  1230. $expcontent .= "<table class=\"data_table\" >";
  1231. $barreMois = '';
  1232. // 3. For each event corresponding to this agenda, do the following:
  1233. while ($myrow = Database::fetch_array($result)) {
  1234. $start_date_local = api_get_local_time($myrow['start_date'], null, date_default_timezone_get());
  1235. //3.1 Make the blue month bar appear only once.
  1236. if ($barreMois != api_format_date($start_date_local, "%m")) {
  1237. // 3.1.1 Update the check value for the month bar.
  1238. $barreMois = api_format_date($start_date_local, "%m");
  1239. // 3.1.2 Display the month bar.
  1240. $expcontent .= "<tr><td id=\"title\" colspan=\"2\" class=\"month\" valign=\"top\">".api_format_date($start_date_local, "%B %Y")."</td></tr>";
  1241. }
  1242. // 3.2 Display the agenda items (of this month): the date, hour and title.
  1243. $db_date = (int) api_format_date($start_date_local, "%d");
  1244. if ($_GET['day'] != $db_date) {
  1245. // 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'.
  1246. $expcontent .= "<tr><td class=\"data\" colspan='2'>";
  1247. } else {
  1248. // 3.2.1.b Else (same day) use style 'datanow'.
  1249. $expcontent .= "<tr><td class=\"datanow\" colspan='2'>";
  1250. }
  1251. // 3.2.2 Mark an anchor for this date.
  1252. $expcontent .= "<a name=\"".$db_date."\"></a>"; // anchoring
  1253. // 3.2.3 Write the date and time of this event to the export string.
  1254. $expcontent .= api_format_date($start_date_local);
  1255. // 3.2.4 If a duration is set, write it, otherwise ignore.
  1256. if ($myrow['duration'] == '') {
  1257. $expcontent .= "<br />";
  1258. } else {
  1259. $expcontent .= " / ".$langLasting." ".$myrow['duration']."<br />"; //langLasting comes from lang/x/agenda.inc.php
  1260. }
  1261. // 3.2.5 Write the title.
  1262. $expcontent .= $myrow['title'];
  1263. $expcontent .= "</td></tr>";
  1264. // 3.2.6 Prepare the content of the agenda item.
  1265. $content = $myrow['content'];
  1266. // 3.2.7 Make clickable???
  1267. $content = make_clickable($content);
  1268. $content = text_filter($content);
  1269. // 3.2.8 Write the prepared content to the export string.
  1270. $expcontent .= "<tr><td class=\"text\" colspan='2'>";
  1271. $expcontent .= $content;
  1272. $expcontent .= "</td></tr>";
  1273. // Displaying the agenda item of this month: the added resources.
  1274. // This part is not included into LP export.
  1275. /*if (check_added_resources("Agenda", $myrow['id'])) {
  1276. $content.= "<tr><td colspan='2'>";
  1277. $content.= "<i>".get_lang('AddedResources')."</i><br />";
  1278. display_added_resources("Agenda", $myrow['id']);
  1279. $content.= "</td></tr>";
  1280. }*/
  1281. }
  1282. // 4. Finish the export string.
  1283. $expcontent .= "<tr></table>";
  1284. break;
  1285. // ANNOUNCEMENT BEGIN
  1286. case 'Ad_Valvas':
  1287. // 1. Get the announcement data from the database
  1288. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1289. $sql = "SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id='$item_id'";
  1290. $result = Database::query($sql);
  1291. // 2. Initialise export string
  1292. $expcontent .= "<table class=\"data_table\">";
  1293. // 3. For each announcement matching the query
  1294. while ($myrow = Database::fetch_array($result)) {
  1295. // 3.1 Get the __ field data.
  1296. $content = $myrow[1];
  1297. //$content = nl2br($content);
  1298. // 3.2 Prepare the data for export.
  1299. $content = make_clickable($content);
  1300. $content = text_filter($content);
  1301. // 3.3 Get a UNIX(?<-mktime) Timestamp of the end_date for this announcement.
  1302. $last_post_datetime = $myrow['end_date']; // post time format datetime of database layer (MySQL is assumed)
  1303. list ($last_post_date, $last_post_time) = split(' ', $last_post_datetime);
  1304. list ($year, $month, $day) = explode('-', $last_post_date);
  1305. list ($hour, $min) = explode(':', $last_post_time);
  1306. $announceDate = mktime($hour, $min, 0, $month, $day, $year);
  1307. // 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).
  1308. if ($announceDate > $_SESSION['user_last_login_datetime']) {
  1309. $colorBecauseNew = " color=\"red\" ";
  1310. } else {
  1311. $colorBecauseNew = ' ';
  1312. }
  1313. // 3.5 Write this content to the export string (formatted HTML array).
  1314. $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";
  1315. } // while loop
  1316. // 4 Finish the export string.
  1317. $expcontent .= "</table>";
  1318. break;
  1319. // Course_description BEGIN
  1320. case 'Course_description':
  1321. // 1. Get course description data from database.
  1322. $tbl_course_description = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  1323. $result = Database::query("SELECT id, title, content FROM ".$tbl_course_description." WHERE c_id = $course_id ORDER BY id");
  1324. // 2. Check this element.
  1325. if (Database::num_rows($result)) {
  1326. // 2.a This course has one (or more) description in the database.
  1327. $expcontent .= "<hr noshade=\"noshade\" size=\"1\" />";
  1328. // 2.a.1 For each description available for this course.
  1329. while ($row = Database::fetch_array($result)) {
  1330. // 2.a.1.1 Write title to export string.
  1331. $expcontent .= "<h4>".$row['title']."</h4>";
  1332. // 2.a.1.2 Prepare content.
  1333. $content = make_clickable(nl2br($row['content']));
  1334. $content = text_filter($content);
  1335. // 2.a.1.3 Write content to the export string.
  1336. $expcontent .= $content;
  1337. }
  1338. } else {
  1339. // 2.b This course has no description available.
  1340. $expcontent .= "<br /><h4>$langThisCourseDescriptionIsEmpty</h4>";
  1341. }
  1342. break;
  1343. // DOCUMENT BEGIN
  1344. case 'Document':
  1345. // 1. Get the document data from the database.
  1346. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1347. $sql_query = "SELECT * FROM $tbl_document WHERE c_id = $course_id AND id=$item_id";
  1348. $sql_result = Database::query($sql_query);
  1349. $myrow = Database::fetch_array($sql_result);
  1350. // 2. Get the origin path of the document to treat it internally.
  1351. $orig = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$myrow['path'];
  1352. // 3. Make some kind of strange transformation to get the destination filepath ???
  1353. $pathname = explode('/', $myrow['path']);
  1354. $last = count($pathname) - 1;
  1355. $filename = 'data/'.$filename.$pathname[$last];
  1356. $copyneeded = true;
  1357. // Html files do not need to be copied as the ok button is inserted into them,
  1358. // so don't copy directly.
  1359. $extension = explode('.', $pathname[$last]);
  1360. // This old condition was WRONG for names like design.html.old. Instead, we now get the extension
  1361. // by using preg_match to match case-insensitive (probably faster than 4 conditions).
  1362. //if (($extension[1]=='htm') or ($extension[1]=='html') or ($extension[1]=='HTM') or ($extension[1]=='HTML')) {
  1363. // 4. Check the file extension.
  1364. if (preg_match('/.*(\.htm(l)?)$/i', $pathname[$last])) {
  1365. // 4.a If this file ends with ".htm(l)", we consider it's an HTML file.
  1366. // src tag check begin
  1367. // We now check if there is any src attribute in htm(l) files, if yes, we have to also export
  1368. // the target file (swf, mp3, video,...) of that src tag.
  1369. // In case of absolute links (http://) this is not neccessary, but makes no error.
  1370. // However still missing : relative links case with subdirs -> the necessary dirs are not created in the exported package.
  1371. // 4.a.1 Get the file contents into $file.
  1372. $file = file_get_contents($orig);
  1373. // 4.a.2 Get all the src links in this file.
  1374. //preg_match_all("|((?i)src=\".*\" )|U",$file,$match);
  1375. $match = GetSRCTags($orig);
  1376. // 4.a.3 For each src tag found, do the following:
  1377. for ($i = 0; $i < count($match); $i ++) {
  1378. // 4.a.3.1 Get the tag (split from the key).
  1379. list ($key, $srctag) = each($match);
  1380. $src = $srctag;
  1381. // 4.a.3.2 Check the link kind (web or absolute/relative).
  1382. if (stristr($src, 'http') === false) {
  1383. // 4.a.3.2.a Do something only if relative (otherwise the user will be able to see it too anyway).
  1384. // 4.a.3.2.a.1 Get a proper URL and remove all './'
  1385. $src = urldecode($src); //mp3
  1386. //$src=str_replace('./','',$src);
  1387. $src = preg_replace('/^\.\//', '', $src);
  1388. // 4.a.3.2.a.2 Remove the player link from the URL (only use the mp3 file).
  1389. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1390. // 4.a.3.2.a.3 Remove funny link parts.
  1391. $src = str_replace('?0', '', $src); //mp3
  1392. // The previous lines are used when creating docs with Chamilo Document tool's htmlarea
  1393. // Rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1394. // and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1395. // created by any other software.
  1396. // 4.a.3.2.a.4 Prepare source and destination paths.
  1397. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1398. $dest = $expdir.'/data/'.$src;
  1399. //CopyNCreate($source,$dest);
  1400. rcopy($source, $dest);
  1401. } //else...?
  1402. }
  1403. // src tag check end
  1404. // sco communication insertion begin
  1405. // 4.a.4 If we want to add SCORM actions and a "Done" button, do the following:
  1406. if ($add_scorm_communications === true) {
  1407. if ($bodyclose = strpos($file, '</body>')) {
  1408. $file = substr_replace($file, $scocomfiles.$donebutton, $bodyclose, 7);
  1409. } elseif ($htmlclose = strpos($file, '</html>')) {
  1410. $file = substr_replace($file, $scocomfiles.$donebutton, $htmlclose, 7);
  1411. $file .= '</html>';
  1412. } else {
  1413. $file .= $scocomfiles.$donebutton;
  1414. }
  1415. } //sco communication insertion end
  1416. // 4.a.5 Replace the file's name by adding the element's ID before htm.
  1417. // This will not work with uppercase HTML though. Maybe use the preg_replace syntax proposed...
  1418. $filename = str_replace('.htm', $id.'.htm', $filename);
  1419. //$filename=preg_replace('/.*(\.htm(l)?)$/i',$id.$1,$filename);
  1420. // 4.a.6 Export these contents to a file and set the circle1_files array for later reuse.
  1421. exporttofile($filename, $LPname, $id, $file);
  1422. // The file has been copied, so ask not to copy it again.
  1423. $copyneeded = false;
  1424. } //if (htm(l) files) end
  1425. // 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.
  1426. if ($copyneeded) {
  1427. copy($orig, $expdir.'/'.$filename);
  1428. $circle1_files[0][] = $filename;
  1429. $circle1_files[1][] = $LPname;
  1430. $circle1_files[2][] = $id;
  1431. }
  1432. //echo $orig;
  1433. return;
  1434. // Introduction_text BEGIN
  1435. case 'Introduction_text':
  1436. // 1 Get the introduction text data from the database.
  1437. $TBL_INTRO = Database :: get_course_tool_intro_table();
  1438. // Modified by Ivan Tcholakov, 15-SEP-2008.
  1439. //$result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE id=1");
  1440. $result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE c_id = $course_id AND id='course_homepage'");
  1441. //
  1442. $myrow = Database::fetch_array($result);
  1443. $intro = $myrow['intro_text'];
  1444. // 2 Write introduction text to the export string.
  1445. $expcontent .= '<br />'.$intro;
  1446. break;
  1447. // HotPotatoes BEGIN
  1448. case 'HotPotatoes':
  1449. // 1. Get HotPotatoes data from the document table.
  1450. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1451. $result = Database::query("SELECT * FROM $tbl_document WHERE c_id = $course_id AND id=$item_id");
  1452. $myrow = Database::fetch_array($result);
  1453. // 2. Get the document path.
  1454. $testfile = api_get_path(SYS_COURSE_PATH).$_course['path']."/document".urldecode($myrow['path']);
  1455. // 3. Get the document contents into a string.
  1456. $content = file_get_contents($testfile);
  1457. // 4. Get the document filename (just the file, no path) - would probably be better to use PHP native function.
  1458. $pathname = explode('/', $myrow['path']);
  1459. $last = count($pathname) - 1;
  1460. $filename = 'data/'.$filename.$pathname[$last];
  1461. // 4beta - get all linked files and copy them (procedure copied from documents type).
  1462. // Get all the src links in this file.
  1463. $match = GetSRCTags($testfile);
  1464. // For each src tag found, do the following:
  1465. foreach ($match as $src) {
  1466. //Check the link kind (web or absolute/relative)
  1467. if (stristr($src, 'http') === false) {
  1468. // Do something only if relative (otherwise the user will be able to see it too anyway).
  1469. // Get a proper URL and remove all './'
  1470. $src = urldecode($src); //mp3
  1471. $src = str_replace('./', '', $src);
  1472. // Remove the player link from the URL (only use the mp3 file).
  1473. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1474. // Remove funny link parts.
  1475. $src = str_replace('?0', '', $src); //mp3
  1476. // The previous lines are used when creating docs with Chamilo Document tool's htmlarea.
  1477. // Rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1478. // and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1479. // created by any other software.
  1480. // Prepare source and destination paths.
  1481. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1482. $dest = $expdir.'/data/'.$src;
  1483. //CopyNCreate($source,$dest);
  1484. rcopy($source, $dest);
  1485. } //else...?
  1486. }
  1487. // 5. Prepare the special "close window" for this test.
  1488. $closewindow = "<html><head><link rel='stylesheet' type='text/css' href='../css/default.css'></head><body>"."<br /><div class='message'>$langHotPotatoesFinished</div></body></html>";
  1489. // Finish is the function of HP to save scores, we insert our scorm function calls to its beginning
  1490. // 'Score' is the variable that tracks the score in HP tests.
  1491. // 6.
  1492. $mit = "function Finish(){";
  1493. $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".
  1494. //the following function are implemented in SCOFunctions.js
  1495. " exitPageStatus = true;\n"." computeTime();\n"." doLMSSetValue( 'cmi.core.score.raw', Score );\n"." doLMSSetValue( 'cmi.core.lesson_status', 'completed' );\n"." doLMSCommit();\n"." doLMSFinish();\n".
  1496. // " document.write('".$closewindow."');\n".
  1497. //if you insert the previous row, the test does not appear correctly !!!!
  1498. " }\n"."}\n"."function Finish(){\n"." mySaveScore();";
  1499. $start = "<script type='text/javascript'> loadPage(); </script>";
  1500. // 7. Replace the current MIT function call by our set of functions. In clear, transform HP to SCORM.
  1501. $content = str_replace($mit, $js_content, $content);
  1502. // 8. Finally, add the API loading calls (although that might have been done first).
  1503. $content = str_replace("</script>", "</script>".$scocomfiles.$start, $content);
  1504. // 9. Change the filename to add the database ID and export to a new file,
  1505. // setting the circle1_files array for later reuse.
  1506. $filename = str_replace('.htm', $id.'.htm', $filename);
  1507. exporttofile($filename, $LPname, $id, $content);
  1508. return;
  1509. // Chamilo test BEGIN
  1510. case 'Exercise':
  1511. //1 Use the export_exercise() function to do the job of constructing the question's HTML table
  1512. $expcontent .= export_exercise($item_id);
  1513. break;
  1514. // POST BEGIN
  1515. case 'Post':
  1516. // 1. Get the forum post data from the database.
  1517. $tbl_posts =Database::get_course_table(TABLE_FORUM_POST);
  1518. $tbl_posts_text =Database::get_course_table(TOOL_FORUM_POST_TEXT_TABLE);
  1519. $result = Database::query("SELECT * FROM $tbl_posts where c_id = $course_id AND post_id=$item_id");
  1520. $myrow = Database::fetch_array($result);
  1521. // Grabbing the title of the post.
  1522. $sql_titel = "SELECT * FROM $tbl_posts_text WHERE c_id = $course_id AND post_id=".$myrow['post_id'];
  1523. $result_titel = Database::query($sql_titel);
  1524. $myrow_titel = Database::fetch_array($result_titel);
  1525. $posternom = $myrow['nom'];
  1526. $posterprenom = $myrow['prenom'];
  1527. $posttime = $myrow['post_time'];
  1528. $posttext = $myrow_titel['post_text'];
  1529. $posttitle = $myrow_titel['post_title'];
  1530. $posttext = str_replace('"', "'", $posttext);
  1531. //2 Export contents as an HTML table
  1532. $expcontent .= "<table border='0' cellpadding='3' cellspacing='1' width='100%'>
  1533. <tr>
  1534. <td colspan='2' bgcolor='#e6e6e6'><b>$posttitle</b><br />$posttext</td>
  1535. </tr>
  1536. <tr>
  1537. <td colspan='2'></td>
  1538. </tr>
  1539. <tr>
  1540. <td bgcolor='#cccccc' align='left'>$lang_author : $posterprenom $posternom</td>
  1541. <td align='right' bgcolor='#cccccc'>$lang_date : $posttime</td>
  1542. </tr>
  1543. <tr><td colspan='2' height='10'></td></tr>
  1544. </table>";
  1545. break;
  1546. // NOT IMPLEMENTED ITEMS BEGIN
  1547. case 'Forum':
  1548. case 'Thread':
  1549. case 'Dropbox':
  1550. case 'Assignments':
  1551. case 'Groups':
  1552. case 'Users':
  1553. // 1. Instead of building something, put an info message.
  1554. $langItemMissing1 = "There was a ";
  1555. $langItemMissing2 = "page (step) here in the original Chamilo Learning Path.";
  1556. $expcontent .= "<div class='message'>$langItemMissing1 $item_type $langItemMissing2</div>";
  1557. break;
  1558. // Link BEGIN
  1559. case 'Link _self':
  1560. case 'Link _blank':
  1561. // 1. Get the link data from the database.
  1562. $TABLETOOLLINK = Database :: get_course_link_table();
  1563. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$item_id");
  1564. $myrow = Database::fetch_array($result);
  1565. $thelink = $myrow['url'];
  1566. // 2. Check the link type (open in blank page or in current page).
  1567. if ($item_type == 'Link _blank')
  1568. {
  1569. $target = '_blank';
  1570. }
  1571. // 3. Write the link to the export string.
  1572. $expcontent .= "<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='".$target."'>$LPname</a>";
  1573. // 4. Change the element type for later changes (this is lost, however, so useless here).
  1574. $item_type = 'Link'; // To put this to the filename.
  1575. //$LPname="<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target=".$target.">$LPname</a>";
  1576. // I am still not sure about Link export : to export them as files or they can appear in the TOC at once ?
  1577. // To enable the second possibility, unrem the row $LPname=...
  1578. break;
  1579. }
  1580. // Now we add the Done button and the initialize function : loadpage()
  1581. // not in the case of Documents, HotP
  1582. if ($item_type != 'Exercise' and ($add_scorm_communications === true)) {
  1583. $expcontent .= $donebutton;
  1584. }
  1585. // End the export string with valid HTML tags.
  1586. $expcontent .= "</body></html>";
  1587. // Prepare new file name.
  1588. $filename = $item_type.$id.".htm";
  1589. // Write the export content to the new file.
  1590. exporttofile('data/'.$filename, $LPname, $id, $expcontent);
  1591. }
  1592. /**
  1593. * This function exports the given item's description into a separate file
  1594. * @param integer Item id
  1595. * @param string Item type
  1596. * @param string Description
  1597. * @return void
  1598. */
  1599. function exportdescription($id, $item_type, $description) {
  1600. global $expdir;
  1601. $filename = $item_type.$id.'.desc';
  1602. $expcontent = $description;
  1603. exporttofile($expdir.$filename, 'description_of_'.$item_type.$id, 'description_of_item_'.$id, $expcontent);
  1604. }
  1605. /**
  1606. * This function deletes an entire directory
  1607. * @param string The directory path
  1608. * @return boolean True on success, false on failure
  1609. */
  1610. function deldir($dir) {
  1611. $dh = opendir($dir);
  1612. while ($file = readdir($dh)) {
  1613. if ($file != '.' && $file != '..') {
  1614. $fullpath = $dir.'/'.$file;
  1615. if (!is_dir($fullpath)) {
  1616. unlink($fullpath);
  1617. } else {
  1618. deldir($fullpath);
  1619. }
  1620. }
  1621. }
  1622. closedir($dh);
  1623. if (rmdir($dir)) {
  1624. return true;
  1625. }
  1626. return false;
  1627. }
  1628. /**
  1629. * This functions exports the given path. This is the opener function, which is called first
  1630. * @deprecated this function is only called in the newscorm/scorm_admin.php which is deprecated
  1631. * @param integer The path id
  1632. * @return resource A zip file, containing a hopefully Scorm compliant course made from the LP. This might happen when we don't actually exit the function first :-)
  1633. */
  1634. function exportpath($learnpath_id) {
  1635. // 1. Initialise variables.
  1636. global $_course, $circle1_files, $LPnamesafe, $LPname, $expdir;
  1637. $course_id = api_get_course_int_id();
  1638. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  1639. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  1640. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  1641. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  1642. // Where applicable, add a scorm "Done" button at the end of all contents.
  1643. $add_scorm_button = true;
  1644. // 2. Get the name of the LP.
  1645. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  1646. $sql = "SELECT * FROM $tbl_learnpath_main WHERE c_id = $course_id AND (lp_id=$learnpath_id)";
  1647. $result = Database::query($sql);
  1648. $row = Database::fetch_array($result);
  1649. $LPname = $row['learnpath_name'];
  1650. $LPnamesafe = replace_dangerous_char($LPname, 'strict');
  1651. // 3. Get a temporary dir for creating the zip file.
  1652. $expdir = api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$LPnamesafe;
  1653. $fromdir = '../scorm/export/'; //this dir contains some standard files
  1654. deldir($expdir); // Make sure the temp dir is cleared.
  1655. mkdir($expdir, api_get_permissions_for_new_directories());
  1656. mkdir($expdir.'/css', api_get_permissions_for_new_directories());
  1657. mkdir($expdir.'/data', api_get_permissions_for_new_directories());
  1658. mkdir($expdir.'/js', api_get_permissions_for_new_directories());
  1659. mkdir($expdir.'/data/images', api_get_permissions_for_new_directories());
  1660. mkdir($expdir.'/data/audio', api_get_permissions_for_new_directories());
  1661. mkdir($expdir.'/data/videos', api_get_permissions_for_new_directories());
  1662. $circle1 = array( // This array contains the types of elements we want to export.
  1663. 'Chapter', 'Agenda', 'Ad_Valvas', 'Course_description', 'Document', 'Introduction_text', 'Link _self', 'Link _blank', 'Forum', 'Thread', 'Post', 'Exercise', 'HotPotatoes', 'Assignments', 'Dropbox', 'Users', 'Groups');
  1664. //$circle2 = array('');
  1665. // 4. Get the first level chapters - YW added parent_item_id condition for multi-level paths.
  1666. $sql = "SELECT * FROM $tbl_learnpath_chapter
  1667. WHERE c_id = $course_id AND (lp_id=$learnpath_id and parent_item_id=0)
  1668. ORDER BY display_order ASC";
  1669. // To get all the elements, we should use the function that builds the table of content get_learnpath_tree.
  1670. //WHERE (lp_id=$learnpath_id)
  1671. //ORDER BY parent_item_id, display_order ASC";
  1672. $result = Database::query($sql);
  1673. // 5. export the items listed in Circle I one by one.
  1674. while ($row = Database::fetch_array($result)) {
  1675. // 5.1. Get items data from the database for this chapter.
  1676. $parent_item_id = $row['id'];
  1677. //$sql2a = "SELECT * FROM $tbl_learnpath_chapter WHERE (lp_id=$learnpath_id and parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  1678. //$result2a = Database::query($sql);
  1679. $sql2b = "SELECT * FROM $tbl_learnpath_item WHERE c_id = $course_id AND (parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  1680. $result2b = Database::query($sql2b);
  1681. while ($row2 = Database::fetch_array($result2b)) {
  1682. // 5.1.1 Check if the element is in the circle1 array.
  1683. $tobeexported = false;
  1684. for ($i = 0; $i < count($circle1) && !$tobeexported; $i ++) {
  1685. // If the type is found in the circle1 array, ask for export.
  1686. if ($circle1[$i] == $row2['item_type']) {
  1687. $tobeexported = true;
  1688. }
  1689. }
  1690. // 5.1.2 If applicable, export the item to an HTML file (see exportitem function for more details).
  1691. if ($tobeexported) {
  1692. exportitem($row2['id'], $row2['item_id'], $row2['item_type'], $add_scorm_button);
  1693. /*if ($row2['description']) { // Put the description of items to a separate file (.desc).
  1694. exportdescription($row2['id'], $row2['item_type'], $row2['description']);
  1695. }*/
  1696. }
  1697. } //end of items loop
  1698. } //end of first-level chapters loop
  1699. // 6. Export the other necceassary files.
  1700. $filename = 'default.css';
  1701. copy('../css/'.$filename, $expdir.'/css/'.$filename);
  1702. $filename = 'ims_xml.xsd';
  1703. copy($fromdir.$filename, $expdir.'/'.$filename);
  1704. $filename = 'imscp_v1p1.xsd';
  1705. copy($fromdir.$filename, $expdir.'/'.$filename);
  1706. $filename = 'imsmd_v1p2.xsd';
  1707. copy($fromdir.$filename, $expdir.'/'.$filename);
  1708. $filename = 'APIWrapper.js';
  1709. copy($fromdir.$filename, $expdir.'/js/'.$filename);
  1710. $filename = 'SCOFunctions.js';
  1711. copy($fromdir.$filename, $expdir.'/js/'.$filename);
  1712. // In case circle1_files is not defined, build it
  1713. //$circle1_files
  1714. // 7. Create imsmanifest.xml.
  1715. createimsmanifest($circle1_files, $learnpath_id);
  1716. // 8. Put the files in the exportdir into a zip and force download.
  1717. include_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  1718. // Create zipfile of given directory.
  1719. $zip_folder = new PclZip(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$LPnamesafe.".zip");
  1720. $zip_folder->create(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$LPnamesafe."/", PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/");
  1721. //api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$LPnamesafe); // whitout folder
  1722. // Modified by imandak80
  1723. /* copy(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$LPnamesafe.".zip",
  1724. api_get_path(SYS_COURSE_PATH).$_course['path']."/document/".$LPnamesafe.".zip");
  1725. */
  1726. $zipfoldername = api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$LPnamesafe;
  1727. $zipfilename = $zipfoldername.".zip";
  1728. DocumentManager :: file_send_for_download($zipfilename, false, basename($LPnamesafe.".zip"));
  1729. // 9. Delete the temporary zip file and directory.
  1730. include_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  1731. // in fileManage.lib.php
  1732. my_delete($zipfilename);
  1733. my_delete($zipfoldername);
  1734. //exit;
  1735. // 10. Return the circle_files hash (array).
  1736. return ($circle1_files); //has been used before...
  1737. }
  1738. /**
  1739. * Export SCORM content into a zip file
  1740. *
  1741. * Basically, all this function does is put the scorm directory back into a zip file (like the one
  1742. * that was most probably used to import the course at first)
  1743. * @deprecated this function is only called in the newscorm/scorm_admin.php which is deprecated
  1744. *
  1745. * @param string Name of the SCORM path (or the directory under which it resides)
  1746. * @param array Not used right now. Should replace the use of global $_course
  1747. * @return void
  1748. * @author imandak80
  1749. */
  1750. function exportSCORM($scormname, $course) {
  1751. global $_course;
  1752. // Initialize.
  1753. $tmpname = api_get_path(SYS_COURSE_PATH).$_course['path'].'/scorm';
  1754. $zipfoldername = $tmpname.$scormname;
  1755. $zipfilename = $zipfoldername.'.zip';
  1756. // Create zipfile of given directory.
  1757. include_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  1758. $zip_folder = new PclZip($zipfilename);
  1759. $list = 1;
  1760. //$list = $zip_folder->create($zipfoldername.'/',PCLZIP_OPT_REMOVE_PATH,$tmpname.$scormname."/"); // whitout folder
  1761. $list = $zip_folder->create($zipfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $tmpname);
  1762. if ($list == 0) {
  1763. //echo "Error : ".$zip_folder->errorInfo(true);
  1764. }
  1765. // Send to client.
  1766. DocumentManager :: file_send_for_download($zipfilename, false, basename($scormname.'.zip'));
  1767. // Clear.
  1768. include_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  1769. my_delete($zipfilename);
  1770. }
  1771. /**
  1772. * This function returns an xml tag
  1773. * $data behaves as the content in case of full tags
  1774. * $data is an array of attributes in case of returning an opening tag
  1775. * @param string
  1776. * @param string
  1777. * @param array
  1778. * @param string
  1779. * @return string
  1780. */
  1781. function xmltagwrite($tagname, $which, $data, $linebreak = 'yes') {
  1782. switch ($which) {
  1783. case 'open':
  1784. $tag = '<'.$tagname;
  1785. $i = 0;
  1786. while ($data[0][$i]) {
  1787. $tag .= ' '.$data[0][$i]."=\"".$data[1][$i]."\"";
  1788. $i ++;
  1789. }
  1790. if ($tagname == 'file') {
  1791. $closing = '/';
  1792. }
  1793. $tag .= $closing.'>';
  1794. if ($linebreak != 'no_linebreak') {
  1795. $tag .= "\n";
  1796. }
  1797. break;
  1798. case 'close':
  1799. $tag = '</'.$tagname.'>';
  1800. if ($linebreak != 'no_linebreak') {
  1801. $tag .= "\n";
  1802. }
  1803. break;
  1804. case 'full':
  1805. $tag = '<'.$tagname;
  1806. $tag .= '>'.$data.'</'.$tagname.'>';
  1807. if ($linebreak != 'no_linebreak') {
  1808. $tag .= "\n";
  1809. }
  1810. break;
  1811. }
  1812. return $tag;
  1813. }
  1814. /**
  1815. * This function writes the imsmanifest.xml and exports the chapter names
  1816. * @param array Array containing filenames
  1817. * @param integer Learnpath_id
  1818. * @return void
  1819. */
  1820. function createimsmanifest($circle1_files, $learnpath_id) {
  1821. global $_course, $LPname, $expdir, $LPnamesafe;
  1822. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  1823. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  1824. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  1825. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  1826. include_once '../metadata/md_funcs.php'; // RH: export metadata
  1827. // Header
  1828. // Charset should be dependent on content.
  1829. $header = '<?xml version="1.0" encoding="'.api_get_system_encoding().'"?>'."\n<manifest identifier='".$LPnamesafe."' version='1.1'\n xmlns='http://www.imsproject.org/xsd/imscp_rootv1p1p2'\n xmlns:adlcp='http://www.adlnet.org/xsd/adlcp_rootv1p2'\n xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n xsi:schemaLocation='http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd\n http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd\n http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'>\n";
  1830. $org .= xmltagwrite('metadata', 'open');
  1831. $org .= ' '.xmltagwrite('schema', 'full', 'ADL SCORM');
  1832. $org .= ' '.xmltagwrite('schemaversion', 'full', '1.2');
  1833. $org .= xmltagwrite('metadata', 'close');
  1834. $defaultorgname = 'default_org';
  1835. $attributes[0][0] = 'default';
  1836. $attributes[1][0] = $defaultorgname;
  1837. $org .= xmltagwrite('organizations', 'open', $attributes);
  1838. $attributes[0][0] = 'identifier';
  1839. $attributes[1][0] = $defaultorgname;
  1840. $org .= ' '.xmltagwrite('organization', 'open', $attributes);
  1841. $org .= ' '.xmltagwrite('title', 'full', $LPname);
  1842. // Items list.
  1843. $i = 0;
  1844. $course_id = api_get_course_int_id();
  1845. $previous_item_id = '00';
  1846. while ($circle1_files[0][$i]) {
  1847. // Check whether we are in the border of two chapters.
  1848. //if (!$desc=strpos($circle1_files[2][$i],'scription')) { // This is needed if the descriptions are exported to file.
  1849. $sql = "SELECT * FROM $tbl_learnpath_item WHERE c_id = $course_id AND (id=".$circle1_files[2][$i].")";
  1850. $result = Database::query($sql);
  1851. $row = Database::fetch_array($result);
  1852. $parent_item_id = $row['parent_item_id'];
  1853. if ($parent_item_id != $previous_item_id) {
  1854. // We create the item tag for the chapter (without indifierref).
  1855. $sql2 = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (id=".$parent_item_id.")";
  1856. $result2 = Database::query($sql2);
  1857. $row2 = Database::fetch_array($result2);
  1858. $chapter_name = $row2['chapter_name'];
  1859. $attributes = '';
  1860. $attributes[0][] = 'identifier';
  1861. $attributes[1][] = 'chapter_'.$row2['id'];
  1862. $attributes[0][] = 'isvisible';
  1863. $attributes[1][] = '1';
  1864. if ($previous_item_id != '00') {
  1865. $org .= ' '.xmltagwrite('item', 'close');
  1866. }
  1867. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1868. $org .= ' '.xmltagwrite('title', 'full', $chapter_name);
  1869. if ($row2['chapter_description'] != '') {
  1870. // Chapter description.
  1871. $attributes = '';
  1872. $attributes[0][] = 'identifier';
  1873. $attributes[1][] = 'chapter_'.$row2['id'].'_desc';
  1874. $attributes[0][] = 'isvisible';
  1875. $attributes[1][] = '1';
  1876. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1877. $org .= ' '.xmltagwrite('title', 'full', ' '.$row2['chapter_description']);
  1878. $org .= ' '.xmltagwrite('item', 'close');
  1879. }
  1880. }
  1881. $previous_item_id = $parent_item_id;
  1882. //}
  1883. $attributes = ''; // Item output.
  1884. $attributes[0][] = 'identifier';
  1885. $attributes[1][] = 'item_'.$circle1_files[2][$i];
  1886. $attributes[0][] = 'identifierref';
  1887. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  1888. $attributes[0][] = 'isvisible';
  1889. $attributes[1][] = '1';
  1890. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1891. $org .= ' '.xmltagwrite('title', 'full', $circle1_files[1][$i]);
  1892. if ($row['prereq_id'] != '') {
  1893. // Item prerequisites.
  1894. $attributes = '';
  1895. $attributes[0][] = 'type';
  1896. $attributes[1][] = 'aicc_script';
  1897. $org .= ' '.xmltagwrite('adlcp:prerequisites', 'open', $attributes, 'no_linebreak');
  1898. if ($row['prereq_type'] == 'i') {
  1899. $org .= 'item_'.$row['prereq_id'];
  1900. }
  1901. if ($row['prereq_type'] == 'c') {
  1902. $org .= 'chapter_'.$row['prereq_id'];
  1903. }
  1904. $org .= xmltagwrite('adlcp:prerequisites', 'close', $attributes);
  1905. }
  1906. if ($row['description'] != '') {
  1907. // Item description.
  1908. $attributes = '';
  1909. $attributes[0][] = 'identifier';
  1910. $attributes[1][] = 'item_'.$circle1_files[2][$i].'_desc';
  1911. $attributes[0][] = 'isvisible';
  1912. $attributes[1][] = '1';
  1913. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1914. $org .= ' '.xmltagwrite('title', 'full', ' '.$row['description']);
  1915. $org .= ' '.xmltagwrite('item', 'close');
  1916. }
  1917. $mds = new mdstore(true); // RH: export metadata; if no table, create it
  1918. if (($mdt = $mds->mds_get($row['item_type'].'.'.$row['item_id'])))
  1919. if (($mdo = api_strpos($mdt, '<metadata>')) && ($mdc = api_strpos($mdt, '</metadata>')))
  1920. $org .= ' '.api_substr($mdt, $mdo, $mdc - $mdo + 11)."\n";
  1921. $org .= ' '.xmltagwrite('item', 'close');
  1922. $i ++;
  1923. }
  1924. if ($circle1_files) {
  1925. $org .= ' '.xmltagwrite('item', 'close');
  1926. } // Not needed in case of a blank path.
  1927. $org .= ' '.xmltagwrite('organization', 'close');
  1928. $org .= xmltagwrite('organizations', 'close');
  1929. $org .= xmltagwrite('resources', 'open');
  1930. // Resources list.
  1931. $i = 0;
  1932. while ($circle1_files[0][$i]) {
  1933. $attributes = '';
  1934. $attributes[0][] = 'identifier';
  1935. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  1936. $attributes[0][] = 'type';
  1937. $attributes[1][] = 'webcontent';
  1938. $attributes[0][] = 'adlcp:scormtype';
  1939. $attributes[1][] = 'sco';
  1940. $attributes[0][] = 'href';
  1941. $attributes[1][] = $circle1_files[0][$i];
  1942. $org .= ' '.xmltagwrite('resource', 'open', $attributes);
  1943. $org .= ' '.xmltagwrite('metadata', 'open');
  1944. $org .= ' '.xmltagwrite('schema', 'full', 'ADL SCORM');
  1945. $org .= ' '.xmltagwrite('schemaversion', 'full', '1.2');
  1946. $org .= ' '.xmltagwrite('metadata', 'close');
  1947. $attributes = '';
  1948. $attributes[0][] = 'href';
  1949. $attributes[1][] = $circle1_files[0][$i];
  1950. $org .= ' '.xmltagwrite('file', 'open', $attributes);
  1951. $org .= ' '.xmltagwrite('resource', 'close');
  1952. $i ++;
  1953. }
  1954. $org .= xmltagwrite('resources', 'close');
  1955. $org .= xmltagwrite('manifest', 'close');
  1956. $manifest = $header.$org;
  1957. exporttofile('imsmanifest.xml', 'Manifest file', '0', $manifest);
  1958. }
  1959. /**
  1960. * Gets the tags of the file given as parameter
  1961. *
  1962. * if $filename is not found, GetSRCTags(filename) will return FALSE
  1963. * @param string File path
  1964. * @return mixed Array of strings on success, false on failure
  1965. * @author unknown
  1966. * @author Included by imandak80
  1967. */
  1968. function GetSRCTags($fileName) {
  1969. if (!($fp = fopen($fileName, 'r'))) {
  1970. // Iif file can't be opened, return false.
  1971. return false;
  1972. }
  1973. // Read file contents.
  1974. $contents = fread($fp, filesize($fileName));
  1975. fclose($fp);
  1976. $matches = array();
  1977. $srcList = array();
  1978. // Get all src tags contents in this file. Use multi-line search.
  1979. preg_match_all('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/mi', $contents, $matches); // Get the img src as contained between " or '
  1980. foreach ($matches[3] as $match) {
  1981. if (!in_array($match, $srcList)) {
  1982. $srcList[] = $match;
  1983. }
  1984. }
  1985. if (count($srcList) == 0) {
  1986. return false;
  1987. }
  1988. return $srcList;
  1989. }
  1990. /**
  1991. * Copy file and create directories in the path if needed.
  1992. *
  1993. * @param string $source Source path
  1994. * @param string $dest Destination path
  1995. * @return boolean true on success, false on failure
  1996. */
  1997. function CopyNCreate($source, $dest) {
  1998. if (strcmp($source, $dest) == 0)
  1999. return false;
  2000. $dir = '';
  2001. $tdest = explode('/', $dest);
  2002. for ($i = 0; $i < sizeof($tdest) - 1; $i ++) {
  2003. $dir = $dir.$tdest[$i].'/';
  2004. if (!is_dir($dir))
  2005. if (!mkdir($dir, api_get_permissions_for_new_directories()))
  2006. return false;
  2007. }
  2008. if (!copy($source, $dest))
  2009. return false;
  2010. return true;
  2011. }
  2012. function rcopy($source, $dest) {
  2013. //error_log($source." -> ".$dest, 0);
  2014. if (!file_exists($source)) {
  2015. //error_log($source." does not exist", 0);
  2016. return false;
  2017. }
  2018. if (is_dir($source)) {
  2019. //error_log($source." is a dir", 0);
  2020. // This is a directory.
  2021. // Remove trailing '/'
  2022. if (strrpos($source, '/') == sizeof($source) - 1) {
  2023. $source = substr($source, 0, size_of($source) - 1);
  2024. }
  2025. if (strrpos($dest, '/') == sizeof($dest) - 1) {
  2026. $dest = substr($dest, 0, size_of($dest) - 1);
  2027. }
  2028. if (!is_dir($dest)) {
  2029. $res = @mkdir($dest, api_get_permissions_for_new_directories());
  2030. if ($res !== false) {
  2031. return true;
  2032. } else {
  2033. // Remove latest part of path and try creating that.
  2034. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/')))) {
  2035. return @mkdir($dest, api_get_permissions_for_new_directories());
  2036. } else {
  2037. return false;
  2038. }
  2039. }
  2040. }
  2041. return true;
  2042. } else {
  2043. // This is presumably a file.
  2044. //error_log($source." is a file", 0);
  2045. if (!@ copy($source, $dest)) {
  2046. //error_log("Could not simple-copy $source", 0);
  2047. $res = rcopy(dirname($source), dirname($dest));
  2048. if ($res === true) {
  2049. //error_log("Welcome dir created", 0);
  2050. return @ copy($source, $dest);
  2051. } else {
  2052. return false;
  2053. //error_log("Error creating path", 0);
  2054. }
  2055. } else {
  2056. //error_log("Could well simple-copy $source", 0);
  2057. return true;
  2058. }
  2059. }
  2060. }