learnpath_functions.inc.php 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  1. <?php // $Id: index.php 16620 2008-10-25 20:03:54Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue Notre Dame, 152, B-1140 Evere, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This is a function library for the learning path.
  20. *
  21. * Due to the face that the learning path has been built upon the resoucelinker,
  22. * naming conventions have changed at least 2 times. You can see here in order the :
  23. * 1. name used in the first version of the resourcelinker
  24. * 2. name used in the first version of the LP
  25. * 3. name used in the second (current) version of the LP
  26. *
  27. * 1. 2. 3.
  28. * Category = Chapter = Module
  29. * Item (?) = Item = Step
  30. *
  31. * @author Denes Nagy <darkden@evk.bke.hu>, main author
  32. * @author Roan Embrechts, some code cleaning
  33. * @author Yannick Warnier <yannick.warnier@dokeos.com>, multi-level learnpath behaviour + new SCORM tool
  34. * @access public
  35. * @package dokeos.learnpath
  36. * @todo rename functions to coding conventions: not deleteitem but delete_item, etc
  37. * @todo rewrite functions to comply with phpDocumentor
  38. * @todo remove code duplication
  39. ==============================================================================
  40. */
  41. /**
  42. * This function deletes an item
  43. * @param integer $id: the item we want to delete
  44. * @return boolean True if item was deleted, false if not found or error
  45. **/
  46. function deleteitem($id)
  47. {
  48. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  49. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  50. //get the display order for this item before it is deleted
  51. $sql = "SELECT display_order, parent_item_id FROM $tbl_lp_item WHERE id=$id";
  52. $result = Database::query($sql);
  53. if (Database::num_rows($result) == 0)
  54. {
  55. return false;
  56. }
  57. $row = Database::fetch_row($result);
  58. $display_order = $row[0];
  59. $parent_item_id = $row[1];
  60. // delete the item
  61. $sql = "DELETE FROM $tbl_learnpath_item WHERE id='$id'";
  62. $result = Database::query($sql);
  63. if ($result === false)
  64. {
  65. return false;
  66. }
  67. // update the other items and chapters
  68. $sql = "UPDATE $tbl_learnpath_item SET display_order = display_order-1 WHERE display_order > $display_order AND parent_item_id = $parent_item_id";
  69. $result = Database::query($sql);
  70. $sql = "UPDATE $tbl_learnpath_chapter SET display_order = display_order-1 WHERE display_order > $display_order AND parent_item_id = $parent_item_id";
  71. $result = Database::query($sql);
  72. //return
  73. return true;
  74. }
  75. /**
  76. * This function deletes a module(chapter) and all its steps(items).
  77. *
  78. * @param integer id of the chapter we want to delete
  79. * @return boolean True on success and false if not found or error
  80. **/
  81. function deletemodule($parent_item_id)
  82. {
  83. global $learnpath_id;
  84. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  85. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  86. //Added for multi-level behaviour - slightly recursive
  87. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE lp_id=$learnpath_id";
  88. $result = Database::query($sql);
  89. while ($row = Database::fetch_array($result))
  90. {
  91. if ($row['parent_item_id'] == $parent_item_id)
  92. {
  93. //delete every subchapter
  94. if (deletemodule($row['id']) === false)
  95. {
  96. return false;
  97. }
  98. }
  99. }
  100. //get this chapter's display order
  101. $sql = "SELECT display_order, parent_item_id FROM $tbl_learnpath_chapter WHERE id=$parent_item_id and lp_id=$learnpath_id";
  102. $result = Database::query($sql);
  103. if (Database::num_rows($result) == 0)
  104. {
  105. return false;
  106. }
  107. $row = Database::fetch_row($result);
  108. $display_order = $row[0];
  109. $parent_id = $row[1];
  110. //delete the chapter itself
  111. $sql = "DELETE FROM $tbl_learnpath_chapter WHERE (id=$parent_item_id and lp_id=$learnpath_id)";
  112. $result = Database::query($sql);
  113. //delete items from that chapter
  114. $sql2 = "DELETE FROM $tbl_learnpath_item WHERE parent_item_id=$parent_item_id";
  115. $result = Database::query($sql2);
  116. //update all other chapters accordingly
  117. $sql = "UPDATE $tbl_learnpath_item SET display_order = display_order-1 WHERE display_order > $display_order AND parent_item_id = $parent_id";
  118. $result = Database::query($sql);
  119. $sql = "UPDATE $tbl_learnpath_chapter SET display_order = display_order-1 WHERE display_order > $display_order AND parent_item_id = $parent_id";
  120. $result = Database::query($sql);
  121. return true;
  122. }
  123. /**
  124. * This function deletes an entire path.
  125. *
  126. * @param integer $id: the path we want to delete
  127. * @return void
  128. **/
  129. function deletepath($path_id)
  130. {
  131. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  132. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  133. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  134. $sql = "DELETE FROM $tbl_learnpath_main WHERE lp_id='$path_id'";
  135. $result = Database::query($sql);
  136. //@TODO check how this function is used before uncommenting the following
  137. //also delete all elements inside that path
  138. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE lp_id=$path_id";
  139. $result = Database::query($sql);
  140. while ($row = Database::fetch_array($result))
  141. {
  142. deletemodule($row['id']);
  143. }
  144. }
  145. /**
  146. * This function moves an item.
  147. *
  148. * @param string $direction: move the given chapter up or down
  149. * @param integer Item ID
  150. * @param integer $moduleid: the id of the chapter the element resides in
  151. * @return boolean Returns false on error
  152. * @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'
  153. **/
  154. function moveitem($direction, $id, $moduleid, $type = 'item')
  155. {
  156. global $learnpath_id;
  157. $tbl_learnpath_item = Database::get_course_table(TABLE_LEARNPATH_ITEM);
  158. $tbl_learnpath_chapter = Database::get_course_table(TABLE_LEARNPATH_CHAPTER);
  159. $tree = get_learnpath_tree($learnpath_id);
  160. $orig_order = 0;
  161. $orig_type = '';
  162. $orig_id = $id;
  163. foreach ($tree[$moduleid] as $row)
  164. {
  165. //if this is the element we want (be it a chapter or an item), get its data
  166. if (($row['id'] == $id) && ($row['type'] == $type))
  167. {
  168. $orig_order = $row['display_order'];
  169. $orig_type = $row['type'];
  170. break;
  171. }
  172. }
  173. $dest_order = 0;
  174. $dest_type = '';
  175. $dest_id = 0;
  176. if ($direction == "up")
  177. {
  178. if (!empty ($tree[$moduleid][$orig_order -1]))
  179. {
  180. $dest_order = $orig_order -1;
  181. $dest_type = $tree[$moduleid][$orig_order -1]['type'];
  182. $dest_id = $tree[$moduleid][$orig_order -1]['id'];
  183. }
  184. else
  185. {
  186. return false;
  187. }
  188. }
  189. else
  190. {
  191. //move down
  192. if (!empty ($tree[$moduleid][$orig_order +1]))
  193. {
  194. $dest_order = $orig_order +1;
  195. $dest_type = $tree[$moduleid][$orig_order +1]['type'];
  196. $dest_id = $tree[$moduleid][$orig_order +1]['id'];
  197. }
  198. else
  199. {
  200. return false;
  201. }
  202. }
  203. $sql1 = '';
  204. $sql2 = '';
  205. if ($orig_type == 'chapter')
  206. {
  207. $sql1 = "UPDATE $tbl_learnpath_chapter SET display_order = ".$dest_order." WHERE (id=$orig_id and parent_item_id=$moduleid)";
  208. }
  209. elseif ($orig_type == 'item')
  210. {
  211. $sql1 = "UPDATE $tbl_learnpath_item SET display_order = ".$dest_order." WHERE (id=$orig_id and parent_item_id=$moduleid)";
  212. }
  213. else
  214. {
  215. return false;
  216. }
  217. if ($dest_type == 'chapter')
  218. {
  219. $sql2 = "UPDATE $tbl_learnpath_chapter SET display_order = ".$orig_order." WHERE (id='$dest_id' and parent_item_id=$moduleid)";
  220. }
  221. elseif ($dest_type == 'item')
  222. {
  223. $sql2 = "UPDATE $tbl_learnpath_item SET display_order = ".$orig_order." WHERE (id='$dest_id' and parent_item_id=$moduleid)";
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. Database::query($sql1);
  230. Database::query($sql2);
  231. }
  232. /**
  233. * This function moves a module (also called chapter or category).
  234. *
  235. * @param string $direction: move the given chapter up or down
  236. * @param integer $id: the id of the chapter we want to move
  237. * @return void
  238. **/
  239. function movemodule($direction, $id)
  240. {
  241. global $learnpath_id;
  242. $tbl_learnpath_chapter = Database :: get_course_learnpath_chapter_table();
  243. if ($direction == "up")
  244. {
  245. $sortDirection = "DESC";
  246. }
  247. else
  248. {
  249. $sortDirection = "ASC";
  250. }
  251. // Select all chapters of first level (parent_item_id = 0)
  252. $sql = "SELECT * FROM $tbl_learnpath_chapter where (lp_id=$learnpath_id AND parent_item_id = 0) ORDER BY display_order $sortDirection";
  253. $result = Database::query($sql);
  254. $previousrow = "";
  255. // see similar comment in moveitem() function
  256. // @TODO: this only works for chapters in multi-level mode. Why not gather
  257. // this function and moveitem to keep only one multi-uses function?
  258. while ($row = Database::fetch_array($result))
  259. {
  260. // step 2: performing the move (only happens when passed trhough step 1 at least once)
  261. if (!empty ($this_cat_order))
  262. {
  263. $next_cat_order = $row["display_order"];
  264. $next_cat_id = $row["id"];
  265. $sql1 = "UPDATE $tbl_learnpath_chapter SET display_order = '$next_cat_order' WHERE (id='$this_cat_id' and lp_id=$learnpath_id)";
  266. $sql2 = "UPDATE $tbl_learnpath_chapter SET display_order = '$this_cat_order' WHERE (id='$next_cat_id' and lp_id=$learnpath_id)";
  267. Database::query($sql1);
  268. Database::query($sql2);
  269. unset ($this_cat_order);
  270. unset ($this_cat_id);
  271. unset ($next_cat_order);
  272. unset ($next_cat_id);
  273. break;
  274. }
  275. // step 1: looking for the order of the row we want to move
  276. if ($row["id"] == $id)
  277. {
  278. $this_cat_order = $row["display_order"];
  279. $this_cat_id = $id;
  280. }
  281. }
  282. }
  283. /**
  284. * Inserts a new element in a learnpath table (item or chapter)
  285. * @param string Element type ('chapter' or 'item')
  286. * @param string Chapter name
  287. * @param string Chapter description (optional)
  288. * @param integer Parent chapter ID (default: 0)
  289. * @param integer Learnpath ID
  290. * @param mixed If type 'item', then array(prereq_id=>value, prereq_..)
  291. * @return integer The new chapter ID, or false on failure
  292. * @TODO Finish this function before it is used. Currently only chapters can be added using it.
  293. * @note This function is currently never used!
  294. */
  295. function insert_item($type = 'item', $name, $chapter_description = '', $parent_id = 0, $learnpath_id = 0, $params = null)
  296. {
  297. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  298. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  299. // getting the last order number from the chapters table, in this learnpath, for the parent chapter given
  300. $sql = "SELECT * FROM $tbl_learnpath_chapter
  301. WHERE lp_id=$learnpath_id
  302. AND parent_item_id = $parent_id
  303. ORDER BY display_order DESC";
  304. $result = Database::query($sql);
  305. $row = Database::fetch_array($result);
  306. $last_chapter_order = $row["display_order"];
  307. // getting the last order number of the items
  308. $sql = "SELECT * FROM $tbl_learnpath_item
  309. AND parent_item_id = $parent_id
  310. ORDER BY display_order DESC";
  311. $result = Database::query($sql);
  312. $row = Database::fetch_array($result);
  313. $last_item_order = $row["display_order"];
  314. $new_order = max($last_chapter_order, $last_item_order) + 1;
  315. if ($type === 'chapter')
  316. {
  317. $sql = "INSERT INTO $tbl_learnpath_chapter
  318. (lp_id, chapter_name, chapter_description, display_order)
  319. VALUES ('".domesticate($learnpath_id)."',
  320. '".domesticate(htmlspecialchars($chapter_name))."',
  321. '".domesticate(htmlspecialchars($chapter_description))."',
  322. $new_order )";
  323. $result = Database::query($sql);
  324. if ($result === false)
  325. {
  326. return false;
  327. }
  328. $id = Database :: insert_id();
  329. }
  330. elseif ($type === 'item')
  331. {
  332. $sql = "INSERT INTO $tbl_learnpath_item
  333. (parent_item_id, item_type, item_id, display_order)
  334. VALUES ('".domesticate($parent_id)."',
  335. '".domesticate(htmlspecialchars($item_type))."',
  336. '".domesticate(htmlspecialchars($item_id))."',
  337. $new_order )";
  338. $result = Database::query($sql);
  339. if ($result === false)
  340. {
  341. return false;
  342. }
  343. $id = Database :: insert_id();
  344. }
  345. return $id;
  346. }
  347. /**
  348. * This function returns an array with all the learnpath categories/chapters
  349. * @return array List of learnpath chapter titles
  350. **/
  351. function array_learnpath_categories()
  352. {
  353. #global $tbl_learnpath_chapter;
  354. global $learnpath_id;
  355. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  356. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE (lp_id=$learnpath_id) ORDER BY display_order ASC";
  357. $result = Database::query($sql);
  358. while ($row = Database::fetch_array($result))
  359. {
  360. $array_learnpath_categories[] = array ($row["id"], $row["chapter_name"]);
  361. }
  362. //$array_learnpath_categories=array($array_learnpath_categories_name,$array_learnpath_categories_id);
  363. return $array_learnpath_categories;
  364. }
  365. /**
  366. * Displays the learnpath chapters(=modules,categories) and their contents.
  367. * @param integer Chapter ID to display now (enables recursive behaviour)
  368. * @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
  369. * @param integer Level (the depth of the call - helps in display)
  370. * @todo eliminate all global $lang declarations, use get_lang, improve structure.
  371. * @author Denes Nagy
  372. * @author Roan Embrechts
  373. * @author Yannick Warnier <yannick.warnier@dokeos.com> - complete redesign for multi-level learnpath chapters
  374. */
  375. function display_learnpath_chapters($parent_item_id = 0, $tree = array (), $level = 0)
  376. {
  377. //error_log('New LP - In learnpath_functions::display_learnpath_chapters',0);
  378. global $color2;
  379. global $xml_output;
  380. global $learnpath_id;
  381. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  382. // @todo: coding standards: Language variables are CaMMelCaSe, all other variables should use the underscoring method.
  383. $lang_move_down = get_lang('_move_down');
  384. $lang_move_up = get_lang('lang_move_up');
  385. $lang_edit_learnpath_module = get_lang('lang_edit_learnpath_module');
  386. $lang_delete_learnpath_module = get_lang('lang_delete_learnpath_module');
  387. $lang_nochapters = get_lang('lang_nochapters');
  388. $lang_prerequisites = get_lang('lang_prerequisites');
  389. $lang_prerequisites_limit = get_lang('lang_prerequisites_limit');
  390. $lang_add_learnpath_item = get_lang('lang_add_learnpath_item');
  391. $lang_add_item = get_lang('lang_add_item');
  392. $lang_title_and_desc = get_lang('lang_title_and_desc');
  393. $lang_change_order = get_lang('lang_change_order');
  394. $lang_add_prereqi = get_lang('lang_add_prereqi');
  395. $lang_add_title_and_desc = get_lang('lang_add_title_and_desc');
  396. $lang_delete = get_lang('Delete');
  397. if ($parent_item_id === 0)
  398. {
  399. // this is the first time we use the function, define the tree and display learnpath name
  400. $tree = get_learnpath_tree($learnpath_id);
  401. $num_modules = count($tree);
  402. //$num_modules=Database::num_rows($result);
  403. if ($num_modules == 0)
  404. {
  405. // do not diplay useless information
  406. //echo "<tr><td>&nbsp;$lang_nochapters</td></tr>";
  407. }
  408. else
  409. {
  410. echo " <tr align='center' valign='top'>\n"." <td><b>&nbsp;$lang_title_and_desc </b></td>\n"." <td><b>&nbsp;$lang_add_item </b></td>\n";
  411. if (is_prereq($learnpath_id))
  412. {
  413. echo " <td bgcolor='#ddddee'><b>&nbsp;$lang_prerequisites_limit </b></td>\n";
  414. }
  415. else
  416. echo " <td><b>&nbsp;$lang_prerequisites </b></td>\n";
  417. 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";
  418. }
  419. }
  420. $i = 1;
  421. $counter = 0;
  422. $num_modules = count($tree[$parent_item_id]);
  423. //while ($row=Database::fetch_array($result))
  424. if (isset ($tree[$parent_item_id]))
  425. {
  426. foreach ($tree[$parent_item_id] as $row)
  427. {
  428. if ($row['item_type'] === 'dokeos_chapter')
  429. {
  430. $xml_output .= "<chapter>";
  431. $xml_output .= "<chaptertitle>".$row["title"]."</chaptertitle>";
  432. $xml_output .= "<chapterdescription>".$row["description"]."</chapterdescription>";
  433. $counter ++;
  434. if (($counter % 2) == 0)
  435. {
  436. $oddclass = "row_odd";
  437. }
  438. else
  439. {
  440. $oddclass = "row_even";
  441. }
  442. //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";
  443. 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";
  444. if (is_prereq($learnpath_id))
  445. {
  446. echo " bgcolor='#ddddee'";
  447. }
  448. echo ">".$row['prerequisite']."</td>\n";
  449. // showing the edit, delete and move icons
  450. if (is_allowed_to_edit())
  451. {
  452. $myaction = 'move_item';
  453. if ($i < $num_modules)
  454. {
  455. // if we are still under the number of chapters in this section, show "move down"
  456. //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";
  457. 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";
  458. }
  459. else
  460. {
  461. echo ' <td align="center">&nbsp;</td>'."\n";
  462. }
  463. if ($i > 1)
  464. {
  465. //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";
  466. 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";
  467. }
  468. else
  469. {
  470. echo ' <td align="center">&nbsp;</td>'."\n";
  471. }
  472. echo " <td align='center'>&nbsp;</td>\n";
  473. //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";
  474. 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";
  475. //echo " <td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&amp;action=deletemodule&amp;id=".$row["id"]."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9 onclick=\"return confirmation('".$row['chapter_name']."');\">"."<img src=\"../img/delete.gif\" border=\"0\" title=\"$lang_delete_learnpath_module\">"."</a>"."</td>\n";
  476. echo " <td align='center'>"."<a href='".api_get_self()."?lp_id=$learnpath_id&action=delete_item&id=".$row["id"]."' onclick=\"return confirmation('".$row['title']."');\">"."<img src=\"../img/delete.gif\" border=\"0\" title=\"$lang_delete_learnpath_module\">"."</a>"."</td>\n";
  477. }
  478. echo "</tr>\n";
  479. $i ++;
  480. $xml_output .= "<items>";
  481. #display_learnpath_items($row["id"]);
  482. display_learnpath_chapters($row['id'], $tree, $level +1);
  483. $xml_output .= "</items>";
  484. $xml_output .= "</chapter>";
  485. }
  486. else //if //($row['item_type'] === 'item')
  487. {
  488. $row_items = $row;
  489. echo "<tr>\n <td colspan='2' valign='top'>";
  490. //require('resourcelinker.inc.php');
  491. display_addedresource_link_in_learnpath($row_items["item_type"], $row_items["ref"], '', $row_items["id"], 'builder', 'icon', $level);
  492. if ($row_items["description"])
  493. {
  494. echo "<div align='justify'>&nbsp;&nbsp;&nbsp;{$row_items['description']}";
  495. }
  496. echo "</td>";
  497. if (is_prereq($learnpath_id))
  498. {
  499. echo '<td bgcolor="#EEEEFF">';
  500. }
  501. else
  502. {
  503. echo "<td>";
  504. }
  505. if (is_allowed_to_edit())
  506. {
  507. if ($row_items["prerequisite"] <> '')
  508. {
  509. $prereq = $row_items["prerequisite"];
  510. //if ($row_items["prereq_type"] == 'i')
  511. //{
  512. //item
  513. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE id='$prereq'"; //check if prereq has been deleted
  514. $result_items2 = Database::query($sql_items2);
  515. $number_items2 = Database::num_rows($result_items2);
  516. if ($number_items2 == 0)
  517. {
  518. echo get_lang('prereq_deleted_error');
  519. }
  520. $row_items2 = Database::fetch_array($result_items2);
  521. display_addedresource_link_in_learnpath($row_items2["item_type"], $row_items2["ref"], '', $row_items2["id"], 'builder', '', 0);
  522. if ((($row_items2["item_type"] == TOOL_QUIZ) or ($row_items2["item_type"] == 'HotPotatoes')) and ($row_items['prerequisite']))
  523. {
  524. //echo "&nbsp;({$row_items2['title']})";
  525. }
  526. //}
  527. /*
  528. if ($row_items["prereq_type"] == 'c')
  529. {
  530. //chapter
  531. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE id='$prereq' AND item_type='dokeos_chapter'"; //check if prereq has been deleted
  532. $result_items2 = Database::query($sql_items2);
  533. $number_items2 = Database::num_rows($result_items2);
  534. if ($number_items2 == 0)
  535. {
  536. echo "<font color='red'>$lang_prereq_deleted_error</font>";
  537. }
  538. $row_items2 = Database::fetch_array($result_items2);
  539. echo " {$row_items2['title']}";
  540. }*/
  541. }
  542. echo "</font></td>";
  543. $xml_output .= "<element_type>".$row_items["item_type"]."</element_type>";
  544. $xml_output .= "<element_id>".$row_items["item_id"]."</element_id>";
  545. // move
  546. if ($i < $num_modules)
  547. {
  548. 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>";
  549. }
  550. else
  551. {
  552. echo "<td width='30' align='center'>&nbsp;</td>";
  553. }
  554. if ($i > 1)
  555. {
  556. 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>";
  557. }
  558. else
  559. {
  560. echo "<td width='30' align='center'>&nbsp;</td>";
  561. }
  562. echo "</td>"."<td align='center'>";
  563. // edit prereq
  564. 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>";
  565. // edit
  566. 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>";
  567. // delete
  568. 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=\"return confirmation('".$row_items["item_type"]."');\">"."</a>";
  569. }
  570. $i ++;
  571. echo "</td></tr>";
  572. }
  573. }
  574. }
  575. }
  576. /**
  577. * Displays all learning paths.
  578. * @return void
  579. * @todo eliminate all global $lang declarations, use get_lang, improve structure.
  580. */
  581. function display_all_learnpath()
  582. {
  583. global $tbl_tool, $color2;
  584. global $xml_output, $lang_edit_learnpath, $lang_delete_learnpath, $lang_publish, $lang_no_publish, $lang_add_learnpath_chapter_to_path;
  585. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  586. $sql = "SELECT * FROM $tbl_learnpath_main ORDER BY learnpath_name";
  587. $result = Database::query($sql);
  588. $i = 1;
  589. $num_modules = Database::num_rows($result);
  590. while ($row = Database::fetch_array($result))
  591. {
  592. //other grey color : #E6E6E6
  593. echo "<tr><td bgcolor=\"$color2\" width=400><b>&nbsp;";
  594. echo "<a href='learnpath_handler.php?&lp_id={$row['lp_id']}'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9>{$row['learnpath_name']}</a>";
  595. echo "</b><br><i><div class=text align=justify>{$row['learnpath_description']}</div></i></td>";
  596. // showing the edit, delete and publish icons
  597. if (is_allowed_to_edit())
  598. {
  599. echo "<td bgcolor=\"$color2\" align=center><a href='learnpath_handler.php?lp_id={$row['lp_id']}&action=add&type=learnpathcategory'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src='../img/0.gif' width='13' height='13' border='0' title='$lang_add_learnpath_chapter_to_path'></a></td>";
  600. echo "<td bgcolor=\"$color2\" align=center><a href='".api_get_self()."?action=editpath&id=".$row["lp_id"]."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/edit.gif\" border=\"0\" title=\"$lang_edit_learnpath\"></a></td>";
  601. echo "<td bgcolor=\"$color2\" align=center><a href='".api_get_self()."?action=deletepath&id=".$row["lp_id"]."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/delete.gif\" border=\"0\" title=\"$lang_delete_learnpath\" onclick=\"return confirmation('".$row2['learnpath_name']."');\"></a></td>";
  602. $id = $row["lp_id"];
  603. $sql2 = "SELECT * FROM $tbl_learnpath_main where lp_id=$id";
  604. $result2 = Database::query($sql2);
  605. $row2 = Database::fetch_array($result2);
  606. $name = $row2['learnpath_name'];
  607. $sql3 = "SELECT * FROM $tbl_tool where (name=\"$name\" and image='scormbuilder.gif')";
  608. $result3 = Database::query($sql3);
  609. $row3 = Database::fetch_array($result3);
  610. if (($row3["visibility"]) == '1')
  611. {
  612. echo "<td bgcolor=\"$color2\" align=center><a href='".api_get_self()."?action=publishpath&set=i&id=".$row["lp_id"]."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/visible.gif\" border=\"0\" title=\"$lang_no_publish\"></a></td>";
  613. }
  614. else
  615. {
  616. echo "<td bgcolor=\"$color2\" align=center><a href='".api_get_self()."?action=publishpath&set=v&id=".$row["lp_id"]."'&SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9><img src=\"../img/invisible.gif\" border=\"0\" title=\"$lang_publish\"></a></td>";
  617. }
  618. }
  619. $i ++;
  620. echo "<tr><td>&nbsp;</td></tr>";
  621. }
  622. }
  623. /**
  624. * Displays the learning path items/steps.
  625. * @param integer Category ID
  626. * @return void
  627. * @todo eliminate all global $lang declarations, use get_lang, improve structure.
  628. */
  629. function display_learnpath_items($categoryid)
  630. {
  631. global $xml_output;
  632. 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;
  633. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  634. $sql_items = "SELECT * FROM $tbl_lp_item WHERE parent_item_id='$categoryid' ORDER BY display_order ASC";
  635. $result_items = Database::query($sql_items);
  636. $number_items = Database::num_rows($result_items);
  637. $i = 1;
  638. error_log('Selected item under '.$categoryid,0);
  639. while ($row_items = Database::fetch_array($result_items))
  640. {
  641. echo "<tr><td colspan='2' valign='top'>";
  642. display_addedresource_link_in_learnpath($row_items["item_type"], $row_items["ref"], '', $row_items["id"], 'builder', 'icon');
  643. if ($row_items["description"])
  644. {
  645. echo "<div align='justify'><font color='#999999'>&nbsp;&nbsp;&nbsp;{$row_items['description']}</font>";
  646. }
  647. echo "</td>";
  648. if (is_prereq($learnpath_id))
  649. {
  650. echo '<td bgcolor="#EEEEFF">';
  651. }
  652. else
  653. {
  654. echo "<td>";
  655. }
  656. if (is_allowed_to_edit())
  657. {
  658. error_log('Is allowed to edit item'.$row_items['id'],0);
  659. //TODO: fix by adding true prerequisites parsing (and cycle through)
  660. //Over simplification here, we transform prereq_id field into prerequisite field
  661. if ($row_items["prerequisite"] <> '')
  662. {
  663. $prereq = $row_items["prerequisite"];
  664. //if ($row_items["prereq_type"] == 'i')
  665. //{
  666. //item
  667. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE id='$prereq'"; //check if prereq has been deleted
  668. $result_items2 = Database::query($sql_items2);
  669. $number_items2 = Database::num_rows($result_items2);
  670. if ($number_items2 == 0)
  671. {
  672. echo "<font color=red>$lang_prereq_deleted_error</font>";
  673. }
  674. $row_items2 = Database::fetch_array($result_items2);
  675. display_addedresource_link_in_learnpath($row_items2["item_type"], $row_items2["ref"], '', $row_items2["id"], 'builder', '');
  676. if ((($row_items2["item_type"] == 'Exercise') or ($row_items2["item_type"] == 'HotPotatoes')) and ($row_items['prerequisites']))
  677. {
  678. echo "&nbsp;({$row_items2['title']})";
  679. }
  680. //}
  681. /*if ($row_items["prereq_type"] == 'c')
  682. {
  683. //chapter
  684. $sql_items2 = "SELECT * FROM $tbl_learnpath_chapter WHERE id='$prereq'"; //check if prereq has been deleted
  685. $result_items2 = Database::query($sql_items2);
  686. $number_items2 = Database::num_rows($result_items2);
  687. if ($number_items2 == 0)
  688. {
  689. echo "<font color=red>$lang_prereq_deleted_error</font>";
  690. }
  691. $row_items2 = Database::fetch_array($result_items2);
  692. echo " {$row_items2['chapter_name']}";
  693. }*/
  694. }
  695. echo "</font></td>";
  696. $xml_output .= "<element_type>".$row_items["item_type"]."</element_type>";
  697. $xml_output .= "<element_id>".$row_items["id"]."</element_id>";
  698. // move
  699. if ($i < $number_items)
  700. {
  701. 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>";
  702. }
  703. else
  704. {
  705. echo "<td width='30' align='center'>&nbsp;</td>";
  706. }
  707. if ($i > 1)
  708. {
  709. 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>";
  710. }
  711. else
  712. {
  713. echo "<td width='30' align='center'>&nbsp;</td>";
  714. }
  715. echo "</td><td align='center'>";
  716. // edit prereq
  717. 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>";
  718. // edit
  719. 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>";
  720. // delete
  721. echo "<td align='center'>";
  722. 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=\"return confirmation('".$langThisItem."');\"></a>";
  723. }
  724. $i ++;
  725. echo "</td></tr>";
  726. }
  727. }
  728. /**
  729. * This function returns the items belonging to the chapter that contains the given item (brother items)
  730. * @param integer Item id
  731. * @return array Table containing the items
  732. */
  733. function learnpath_items($itemid)
  734. {
  735. global $xml_output;
  736. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  737. $sql_items = "SELECT parent_item_id FROM $tbl_lp_item WHERE id='$itemid'";
  738. $moduleid_sql = Database::query($sql_items);
  739. $moduleid_array = Database::fetch_array($moduleid_sql); //first row of the results
  740. $moduleid = $moduleid_array["parent_item_id"];
  741. $sql_items = "SELECT * FROM $tbl_lp_item WHERE parent_item_id='$moduleid' ORDER BY display_order ASC";
  742. $result_items = Database::query($sql_items);
  743. $ar = Database::fetch_array($result_items);
  744. while ($ar != '')
  745. {
  746. $result[] = $ar;
  747. $ar = Database::fetch_array($result_items);
  748. }
  749. return $result;
  750. }
  751. /**
  752. * This function returns the chapters belonging to the path that contais the given chapter (brother chapters)
  753. * @param integer Learnpath id
  754. * @return array Table containing the chapters
  755. */
  756. function learnpath_chapters($learnpath_id)
  757. {
  758. global $xml_output, $learnpath_id;
  759. //$tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  760. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  761. $sql_items = "SELECT * FROM $tbl_lp_item WHERE lp_id='$learnpath_id' AND item_type='dokeos_chapter' ORDER BY display_order ASC";
  762. //$sql_items = "SELECT * FROM $tbl_learnpath_chapter WHERE lp_id='$learnpath_id' ORDER BY display_order ASC";
  763. $result_items = Database::query($sql_items);
  764. $ar = Database::fetch_array($result_items);
  765. while ($ar != '')
  766. {
  767. $result[] = $ar;
  768. $ar = Database::fetch_array($result_items);
  769. }
  770. return $result;
  771. }
  772. /**
  773. * This function tells if a learnpath contains items which are prerequisite to other items
  774. * @param integer Learnpath id
  775. * @return boolean True if this learnpath contains an item which is a prerequisite to something
  776. */
  777. function is_prereq($learnpath_id)
  778. {
  779. global $xml_output;
  780. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  781. $prereq = false;
  782. $sql_items = "SELECT * FROM $tbl_lp_item WHERE lp_id='$learnpath_id' AND parent_item_id=0 ORDER BY display_order ASC";
  783. $result_items = Database::query($sql_items);
  784. while ($ar = Database::fetch_array($result_items))
  785. {
  786. $c = $ar['id'];
  787. $sql_items2 = "SELECT * FROM $tbl_lp_item WHERE lp_id = $learnpath_id AND parent_item_id='$c' ORDER BY display_order ASC";
  788. $result_items2 = Database::query($sql_items2);
  789. while ($ar2 = Database::fetch_array($result_items2))
  790. {
  791. if ($ar2['prerequisite'] != '')
  792. {
  793. $prereq = true;
  794. }
  795. }
  796. }
  797. return ($prereq);
  798. }
  799. /**
  800. * This function returns the prerequisite sentence
  801. * @param integer Item ID
  802. * @return string Prerequisite warning text
  803. */
  804. function prereqcheck($id_in_path)
  805. {
  806. //1 Initialise and import working vars
  807. global $learnpath_id, $_user;
  808. global $langPrereqToEnter, $langPrereqTestLimit1, $langPrereqTestLimit2, $langPrereqTestLimitNow, $langPrereqFirstNeedTo, $langPrereqModuleMinimum1, $langPrereqModuleMinimum2;
  809. $tbl_learnpath_user = Database :: get_course_table(TABLE_LEARNPATH_USER);
  810. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  811. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  812. //2 Initialise return value
  813. $prereq = false;
  814. //3 Get item data from the database
  815. $sql_items = "SELECT * FROM $tbl_learnpath_item WHERE id='$id_in_path'";
  816. $result_items = Database::query($sql_items);
  817. $row = Database::fetch_array($result_items);
  818. //4 Check prerequisite's type
  819. if ($row['prereq_type'] == 'i')
  820. {
  821. //4.a If prerequisite is of type 'i' (item)
  822. //4.a.1 Get data ready for use
  823. $id_in_path3 = $row['prereq_id'];
  824. $prereq_limit = $row['prereq_completion_limit'];
  825. //4.a.2 Get data from the user-item relation
  826. if ($_user['user_id'] == '')
  827. {
  828. $user_id = '0';
  829. }
  830. else
  831. {
  832. $user_id = $_user['user_id'];
  833. }
  834. $sql_items3 = "SELECT * FROM $tbl_learnpath_user WHERE (learnpath_item_id='$id_in_path3' and user_id=$user_id)";
  835. $result_items3 = Database::query($sql_items3);
  836. $row3 = Database::fetch_array($result_items3);
  837. //4.a.3 Get the link that needs to be shown for the current item (not the prereq)
  838. $stepname = display_addedresource_link_in_learnpath($row['item_type'], $row['ref'], '', $id_in_path, 'builder', 'nolink');
  839. //this is the step we want to open
  840. $stepname = trim($stepname); //to remove occasional line breaks and white spaces
  841. //4.a.4 Get the prerequisite item
  842. $sql6 = "SELECT * FROM $tbl_learnpath_item WHERE (id='$id_in_path3')";
  843. $result6 = Database::query($sql6);
  844. $row6 = Database::fetch_array($result6);
  845. //4.a.5 Get a link to the prerequisite item
  846. $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
  847. //4.a.5 Initialise limit value
  848. $limitok = true;
  849. //4.a.6 Get prerequisite limit
  850. if ($prereq_limit)
  851. {
  852. //4.a.6.a If the completion limit exists
  853. if ($row3['score'] < $prereq_limit)
  854. {
  855. //4.a.6.a.a If the completion limit hasn't been reached, then display the corresponding message
  856. $prereq = $langPrereqToEnter.$stepname.$langPrereqTestLimit1."$prereq_limit".$langPrereqTestLimit2.$prereqname.". (".$langPrereqTestLimitNow.$row3['score'].")";
  857. }
  858. else
  859. {
  860. //4.a.6.a.b The completion limit has been reached. Prepare to return false (no prereq hanging)
  861. $prereq = false;
  862. }
  863. }
  864. else
  865. {
  866. //4.a.6.b If the completion limit doesn't exist
  867. if ($row3['status'] == "completed" or $row3['status'] == 'passed')
  868. {
  869. //4.a.6.b.a If the prerequisite status is 'completed'
  870. $prereq = false;
  871. }
  872. else
  873. {
  874. //4.a.6.b.b The prerequisite status is not 'completed', return corresponding message
  875. $prereq = $langPrereqToEnter.$stepname.$langPrereqFirstNeedTo.$prereqname.'.';
  876. }
  877. }
  878. }
  879. elseif ($row['prereq_type'] == 'c')
  880. {
  881. //4.b If prerequisite is of type 'c' (chapter)
  882. //4.b.1 Get data ready to use
  883. $id_in_path2 = $row['prereq_id'];
  884. //4.b.2 Get all items in the prerequisite chapter
  885. $sql_items3 = "SELECT * FROM $tbl_lp_item WHERE parent_item_id='$id_in_path2'";
  886. $result_items3 = Database::query($sql_items3);
  887. $allcompleted = true;
  888. while ($row3 = Database::fetch_array($result_items3))
  889. {
  890. //4.b.3 Cycle through items in the prerequisite chapter
  891. //4.b.3.1 Get data ready to use
  892. $id_in_path4 = $row3['id'];
  893. if ($_user['user_id'] == '')
  894. {
  895. $user_id = '0';
  896. }
  897. else
  898. {
  899. $user_id = $_user['user_id'];
  900. }
  901. //4.b.3.2 Get user-item relation
  902. $sql_items4 = "SELECT * FROM $tbl_learnpath_user WHERE (learnpath_item_id='$id_in_path4' and user_id=$user_id)";
  903. $result_items4 = Database::query($sql_items4);
  904. $row4 = Database::fetch_array($result_items4);
  905. //4.b.3.3 If any of these elements is not 'completed', the overall completion status is false
  906. if ($row4['status'] != "completed" and $row4['status'] != 'passed')
  907. {
  908. $allcompleted = false;
  909. }
  910. }
  911. if ($allcompleted)
  912. {
  913. //4.b.4.a All items were completed, prepare to return that there is no prerequisite blocking the way
  914. $prereq = false;
  915. }
  916. else
  917. {
  918. //4.b.4.b Something was not completed. Return corresponding message
  919. $sql5 = "SELECT * FROM $tbl_learnpath_chapter WHERE (lp_id='$learnpath_id' and id='$id_in_path2')";
  920. $result5 = Database::query($sql5);
  921. $row5 = Database::fetch_array($result5);
  922. $prereqmodulename = trim($row5['chapter_name']);
  923. $prereq = $langPrereqModuleMinimum1.$prereqmodulename.$langPrereqModuleMinimum2;
  924. }
  925. }
  926. else
  927. {
  928. //5 If prerequisite type undefined, no prereq
  929. $prereq = false;
  930. }
  931. //6 Return the message (or false if no prerequisite waiting)
  932. return ($prereq);
  933. }
  934. /**
  935. * Constructs the tree that will be used to build the learnpath structure
  936. * @params integer Learnpath_id
  937. * @return array Tree of the learnpath structure
  938. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  939. * @comment This is a temporary function, which exists while the chapters and items
  940. * are still in separate tables in the database. This function gathers the data in a unique tree.
  941. **/
  942. function get_learnpath_tree($learnpath_id)
  943. {
  944. //error_log('New LP - In learnpath_functions::get_learnpath_tree',0);
  945. //init elems
  946. #global $tbl_learnpath_item, $tbl_learnpath_chapter;
  947. /*
  948. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  949. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  950. */
  951. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  952. $tree = array ();
  953. $chapters = array ();
  954. //$chapters = array();
  955. //$chapters_by_parent = array();
  956. //$items = array();
  957. //$items_by_chapter = array();
  958. $all_items_by_chapter = array ();
  959. $sql = "SELECT * FROM $tbl_lp_item WHERE lp_id = ".$learnpath_id." AND item_type='dokeos_chapter' ORDER BY display_order";
  960. //error_log('New LP - learnpath_functions - get_learnpath_tree: '.$sql,0);
  961. $res = Database::query($sql);
  962. // format the $chapters_by_parent array so we have a suitable structure to work with
  963. while ($row = Database::fetch_array($res))
  964. {
  965. $chapters[] = $row;
  966. //shouldn't be necessary (check no null value)
  967. if (empty ($row['parent_item_id']))
  968. {
  969. $row['parent_item_id'] = 0;
  970. }
  971. //$chapters_by_parent[$row['parent_item_id']][$row['previous_item_id']] = $row;
  972. $all_items_by_chapter[$row['parent_item_id']][$row['display_order']] = $row;
  973. $all_items_by_chapter[$row['parent_item_id']][$row['display_order']]['type'] = 'dokeos_chapter';
  974. }
  975. // now for every item in each chapter, get a suitable structure too
  976. foreach ($chapters as $row)
  977. {
  978. // select items from this chapter
  979. $sql = "SELECT * FROM $tbl_lp_item WHERE lp_id = $learnpath_id AND parent_item_id = ".$row['id']." ORDER BY display_order";
  980. //error_log('New LP - learnpath_functions - get_learnpath_tree: '.$sql,0);
  981. $res = Database::query($sql);
  982. //error_log('New LP - learnpath_functions - get_learnpath_tree: Found '.Database::num_rows($res).' results',0);
  983. while ($myrow = Database::fetch_array($res, 'ASSOC'))
  984. {
  985. //$items[] = $myrow;
  986. //$items_by_chapter[$myrow['parent_item_id']][$myrow['display_order']] = $myrow;
  987. $all_items_by_chapter[$row['id']][$myrow['display_order']] = $myrow;
  988. $all_items_by_chapter[$row['id']][$myrow['display_order']]['type'] = 'item';
  989. }
  990. }
  991. //array_multisort($all_items_by_chapter[0], SORT_ASC, SORT_NUMERIC);
  992. foreach ($all_items_by_chapter as $key => $subrow)
  993. {
  994. ksort($all_items_by_chapter[$key]);
  995. }
  996. //all items should now be well-ordered
  997. //error_log('New LP - In get_learnpath_tree, returning '.print_r($all_items_by_chapter,true),0);
  998. return $all_items_by_chapter;
  999. }
  1000. /**
  1001. * Gives a list of sequencial elements IDs for next/previous actions
  1002. * @param array The elements tree as returned by get_learnpath_tree()
  1003. * @param integer The chapter id to start from
  1004. * @param boolean Whether to include chapters or not
  1005. * @return array List of elements in the first to last order
  1006. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  1007. **/
  1008. function get_ordered_items_list($tree, $chapter = 0, $include_chapters = false)
  1009. {
  1010. $list = array ();
  1011. foreach ($tree[$chapter] as $order => $elem)
  1012. {
  1013. if ($elem['type'] == 'chapter')
  1014. {
  1015. if ($include_chapters === true)
  1016. {
  1017. $list[] = array ('id' => $elem['id'], 'type' => $elem['type']);
  1018. }
  1019. $res = get_ordered_items_list($tree, $elem['id'], $include_chapters);
  1020. foreach ($res as $elem)
  1021. {
  1022. $list[] = $elem;
  1023. }
  1024. }
  1025. elseif ($elem['type'] == 'item')
  1026. {
  1027. $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']);
  1028. }
  1029. }
  1030. return $list;
  1031. }
  1032. /**
  1033. * Displays the structure of a chapter recursively. Takes the result of get_learnpath_tree as argument
  1034. * @param array Chapter structure
  1035. * @param integer Chapter ID (start point in the tree)
  1036. * @param integer Learnpath ID
  1037. * @param integer User ID
  1038. * @param boolean Indicates if the style is wrapped (true) or extended (false)
  1039. * @param integer Level reached so far in the tree depth (enables recursive behaviour)
  1040. * @return array Number of items, Number of items completed
  1041. * @author Many changes by Yannick Warnier <yannick.warnier@dokeos.com>
  1042. **/
  1043. function display_toc_chapter_contents($tree, $parent_item_id = 0, $learnpath_id, $uid, $wrap, $level = 0)
  1044. {
  1045. #global $tbl_learnpath_user;
  1046. $tbl_learnpath_user = Database :: get_course_table(TABLE_LEARNPATH_USER);
  1047. $num = 0;
  1048. $num_completed = 0;
  1049. foreach ($tree[$parent_item_id] as $order => $elem)
  1050. {
  1051. $bold = false;
  1052. if (!empty ($_SESSION['cur_open']) && ($elem['id'] == $_SESSION['cur_open']))
  1053. {
  1054. $bold = true;
  1055. }
  1056. if ($elem['type'] === 'chapter')
  1057. {
  1058. if ($wrap)
  1059. {
  1060. echo str_repeat("&nbsp;&nbsp;", $level).shorten(strip_tags($elem['chapter_name']), (35 - 3 * $level))."<br />\n";
  1061. }
  1062. else
  1063. {
  1064. echo "<tr><td colspan='3'>".str_repeat("&nbsp;&nbsp;", $level).shorten($elem['chapter_name'], (35 - 3 * $level))."</td></tr>\n";
  1065. }
  1066. if ($wrap)
  1067. {
  1068. if ($elem['chapter_description'] != '')
  1069. {
  1070. echo "<div class='description'>".str_repeat("&nbsp;&nbsp;", $level)."&nbsp;".shorten($elem['chapter_description'], (35 - 3 * $level))."</div>\n";
  1071. }
  1072. }
  1073. else
  1074. {
  1075. if ($elem['chapter_description'] != '')
  1076. {
  1077. 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";
  1078. }
  1079. }
  1080. list ($a, $b) = display_toc_chapter_contents($tree, $elem['id'], $learnpath_id, $uid, $wrap, $level +1);
  1081. $num += $a;
  1082. $num_completed += $b;
  1083. }
  1084. elseif ($elem['type'] === 'item')
  1085. {
  1086. // If this element is an item (understand: not a directory/module)
  1087. $sql0 = "SELECT * FROM $tbl_learnpath_user WHERE (user_id='".$uid."' and learnpath_item_id='".$elem['id']."' and lp_id='".$learnpath_id."')";
  1088. $result0 = Database::query($sql0);
  1089. $row0 = Database::fetch_array($result0);
  1090. $completed = '';
  1091. if (($row0['status'] == 'completed') or ($row0['status'] == 'passed'))
  1092. {
  1093. $completed = 'completed';
  1094. $num_completed ++;
  1095. }
  1096. if ($wrap)
  1097. {
  1098. echo str_repeat("&nbsp;&nbsp;", $level)."<a name='{$elem['id']}' />\n";
  1099. }
  1100. else
  1101. {
  1102. echo "<tr><td>".str_repeat("&nbsp;&nbsp;", $level-1)."<a name='{$elem['id']}' />\n";
  1103. }
  1104. if ($wrap)
  1105. {
  1106. $icon = 'wrap';
  1107. }
  1108. if ($bold)
  1109. {
  1110. echo "<b>";
  1111. }
  1112. display_addedresource_link_in_learnpath($elem['item_type'], $elem['ref'], $completed, $elem['id'], 'player', $icon);
  1113. if ($bold)
  1114. {
  1115. echo "</b>";
  1116. }
  1117. if ($wrap)
  1118. {
  1119. echo "<br />\n";
  1120. }
  1121. else
  1122. {
  1123. echo "</td></tr>\n";
  1124. }
  1125. $num ++;
  1126. }
  1127. }
  1128. return array ($num, $num_completed);
  1129. }
  1130. /**
  1131. * Returns a string to display in the tracking frame within the contents.php page (for example)
  1132. * @param integer Learnpath id
  1133. * @param integer Current user id
  1134. * @param integer Starting chapter id
  1135. * @param array Tree of elements as returned by get_learnpath_tree()
  1136. * @param integer Level of recursivity we have reached
  1137. * @param integer Counter of elements already displayed
  1138. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  1139. * @note : forced display because of display_addedresource_link_in_learnpath behaviour (outputing a string would be better)
  1140. **/
  1141. function get_tracking_table($learnpath_id, $user_id, $parent_item_id = 0, $tree = false, $level = 0, $counter = 0)
  1142. {
  1143. $tbl_learnpath_chapter = Database :: get_course_learnpath_chapter_table();
  1144. $tbl_learnpath_item = Database :: get_course_learnpath_item_table();
  1145. $tbl_learnpath_user = Database :: get_course_learnpath_user_table();
  1146. //$mytable = '';
  1147. $include_chapters = true;
  1148. if (!is_array($tree))
  1149. {
  1150. //get a tree of the current learnpath elements
  1151. $tree = get_learnpath_tree($learnpath_id);
  1152. }
  1153. foreach ($tree[$parent_item_id] as $order => $elem)
  1154. {
  1155. if (($counter % 2) == 0)
  1156. {
  1157. $oddclass = 'row_odd';
  1158. }
  1159. else
  1160. {
  1161. $oddclass = 'row_even';
  1162. }
  1163. if ($elem['type'] == 'chapter')
  1164. {
  1165. if ($include_chapters === true)
  1166. {
  1167. //$mytable .= "<tr class='$oddclass'><td colspan = '3'>".str_repeat('&nbsp;',$level*2+2).$elem['chapter_name']."</td></tr>\n";
  1168. echo "<tr class='$oddclass'><td colspan = '3'>".str_repeat('&nbsp;', $level * 2 + 2).$elem['chapter_name']."</td></tr>\n";
  1169. }
  1170. $counter ++;
  1171. //$mytable .= get_tracking_table($learnpath_id, $user_id, $elem['id'], $tree, $level + 1, $counter );
  1172. get_tracking_table($learnpath_id, $user_id, $elem['id'], $tree, $level +1, $counter);
  1173. }
  1174. elseif ($elem['type'] == 'item')
  1175. {
  1176. $sql = "SELECT * FROM $tbl_learnpath_user "."WHERE user_id = $user_id "."AND lp_id = $learnpath_id "."AND learnpath_item_id = ".$elem['id'];
  1177. $res = Database::query($sql);
  1178. $myrow = Database::fetch_array($res);
  1179. if (($myrow['status'] == 'completed') || ($myrow['status'] == 'passed'))
  1180. {
  1181. $color = 'blue';
  1182. $statusmessage = get_lang('Complete');
  1183. }
  1184. else
  1185. {
  1186. $color = 'black';
  1187. $statusmessage = get_lang('Incomplete');
  1188. }
  1189. $link = get_addedresource_link_in_learnpath($elem['item_type'], $elem['id'], $elem['item_id']);
  1190. //$link = display_addedresource_link_in_learnpath($elem['item_type'], $elem['id'], $row['status'], $elem['item_id'], 'player', 'none');
  1191. //$mytable .= "<tr class='$oddclass'>"
  1192. echo "<tr class='$oddclass'>"."<td class='mystatus'>".str_repeat("&nbsp;", $level * 2 + 2);
  1193. //."<a href='$link?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='toc'>hop</a>"
  1194. display_addedresource_link_in_learnpath($elem['item_type'], $elem['ref'], $myrow['status'], $elem['id'], 'player', 'wrap');
  1195. //we should also add the total score here
  1196. 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";
  1197. $counter ++;
  1198. }
  1199. }
  1200. //return $mytable;
  1201. return true;
  1202. }
  1203. /**
  1204. * This function returns false if there is at least one item in the path
  1205. * @param Learnpath ID
  1206. * @return boolean True if nothing was found, false otherwise
  1207. */
  1208. function is_empty($id)
  1209. {
  1210. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  1211. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  1212. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE lp_id=$id ORDER BY display_order ASC";
  1213. $result = Database::query($sql);
  1214. $num_modules = Database::num_rows($result);
  1215. $empty = true;
  1216. if ($num_modules != 0)
  1217. {
  1218. while ($row = Database::fetch_array($result))
  1219. {
  1220. $num_items = 0;
  1221. $parent_item_id = $row['id'];
  1222. $sql2 = "SELECT * FROM $tbl_learnpath_item WHERE (parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  1223. $result2 = Database::query($sql2);
  1224. $num_items = Database::num_rows($result2);
  1225. if ($num_items > 0)
  1226. {
  1227. $empty = false;
  1228. }
  1229. }
  1230. }
  1231. return ($empty);
  1232. }
  1233. /**
  1234. * This function writes $content to $filename
  1235. * @param string Destination filename
  1236. * @param string Learnpath name
  1237. * @param integer Learnpath ID
  1238. * @param string Content to write
  1239. * @return void
  1240. */
  1241. function exporttofile($filename, $LPname, $LPid, $content)
  1242. {
  1243. global $circle1_files; //this keeps all the files which are exported [0]:filename [1]:LP name
  1244. //the $circle1_files variable is going to be used to a deep extent in the imsmanifest.xml
  1245. global $expdir;
  1246. if (!$handle = fopen($expdir.'/'.$filename, 'w'))
  1247. {
  1248. echo "Cannot open file ($filename)";
  1249. }
  1250. if (fwrite($handle, $content) === FALSE)
  1251. {
  1252. echo "Cannot write to file ($filename)";
  1253. exit;
  1254. }
  1255. fclose($handle);
  1256. $circle1_files[0][] = $filename;
  1257. $circle1_files[1][] = $LPname;
  1258. $circle1_files[2][] = $LPid;
  1259. }
  1260. /**
  1261. * This function exports the given Dokeos test
  1262. * @param integer Test ID
  1263. * @return string The test itself as an HTML string
  1264. */
  1265. function export_exercise($item_id)
  1266. {
  1267. global $expdir, $_course, $_configuration, $_SESSION, $_SERVER, $language_interface, $langExerciseNotFound, $langQuestion, $langOk;
  1268. $exerciseId = $item_id;
  1269. require_once ('../exercice/exercise.class.php');
  1270. require_once ('../exercice/question.class.php');
  1271. require_once ('../exercice/answer.class.php');
  1272. require_once ('../exercice/exercise.lib.php');
  1273. // answer types
  1274. define('UNIQUE_ANSWER', 1);
  1275. define('MULTIPLE_ANSWER', 2);
  1276. define('FILL_IN_BLANKS', 3);
  1277. define('MATCHING', 4);
  1278. define('FREE_ANSWER', 5);
  1279. include_once (api_get_path(LIBRARY_PATH).'/text.lib.php');
  1280. $TBL_EXERCISES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1281. /*******************************/
  1282. /* Clears the exercise session */
  1283. /*******************************/
  1284. if (isset ($_SESSION['objExercise']))
  1285. {
  1286. api_session_unregister('objExercise');
  1287. unset ($objExercise);
  1288. }
  1289. if (isset ($_SESSION['objQuestion']))
  1290. {
  1291. api_session_unregister('objQuestion');
  1292. unset ($objQuestion);
  1293. }
  1294. if (isset ($_SESSION['objAnswer']))
  1295. {
  1296. api_session_unregister('objAnswer');
  1297. unset ($objAnswer);
  1298. }
  1299. if (isset ($_SESSION['questionList']))
  1300. {
  1301. api_session_unregister('questionList');
  1302. unset ($questionList);
  1303. }
  1304. if (isset ($_SESSION['exerciseResult']))
  1305. {
  1306. api_session_unregister('exerciseResult');
  1307. unset ($exerciseResult);
  1308. }
  1309. // if the object is not in the session
  1310. if (!isset ($_SESSION['objExercise']))
  1311. {
  1312. // construction of Exercise
  1313. $objExercise = new Exercise();
  1314. $sql = "SELECT title,description,sound,type,random,active FROM $TBL_EXERCISES WHERE id='$exerciseId'";
  1315. // if the specified exercise doesn't exist or is disabled
  1316. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath')))
  1317. {
  1318. die($langExerciseNotFound);
  1319. }
  1320. // saves the object into the session
  1321. api_session_register('objExercise');
  1322. }
  1323. $exerciseTitle = $objExercise->selectTitle();
  1324. $exerciseDescription = $objExercise->selectDescription();
  1325. $exerciseSound = $objExercise->selectSound();
  1326. $randomQuestions = $objExercise->isRandom();
  1327. $exerciseType = $objExercise->selectType();
  1328. if (!isset ($_SESSION['questionList']))
  1329. {
  1330. // selects the list of question ID
  1331. $questionList = $randomQuestions ? $objExercise->selectRandomList() : $objExercise->selectQuestionList();
  1332. // saves the question list into the session
  1333. api_session_register('questionList');
  1334. }
  1335. $nbrQuestions = sizeof($questionList);
  1336. // if questionNum comes from POST and not from GET
  1337. if (!$questionNum || $_POST['questionNum'])
  1338. {
  1339. // only used for sequential exercises (see $exerciseType)
  1340. if (!$questionNum)
  1341. {
  1342. $questionNum = 1;
  1343. }
  1344. else
  1345. {
  1346. $questionNum ++;
  1347. }
  1348. }
  1349. $exerciseTitle = api_parse_tex($exerciseTitle);
  1350. $test .= "<h3>".$exerciseTitle."</h3>";
  1351. if (!empty ($exerciseSound))
  1352. {
  1353. $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>";
  1354. }
  1355. $exerciseDescription = api_parse_tex($exerciseDescription);
  1356. // --------- writing the .js file with to check the correct answers begin -----------------------
  1357. $scriptfilename = "Exercice".$item_id.".js";
  1358. $s = "<script type=\"text/javascript\" src='../js/".$scriptfilename."'></script>";
  1359. $test .= $s;
  1360. $content = "function evaluate() {
  1361. alert('Test evaluated.');
  1362. }
  1363. ";
  1364. if (!$handle = fopen($expdir.'/js/'.$scriptfilename, 'w'))
  1365. {
  1366. echo "Cannot open file ($scriptfilename)";
  1367. }
  1368. if (fwrite($handle, $content) === FALSE)
  1369. {
  1370. echo "Cannot write to file ($filename)";
  1371. exit;
  1372. }
  1373. fclose($handle);
  1374. // --------- writing the .js file with to check the correct answers end -----------------------
  1375. $s = "
  1376. <p>$exerciseDescription</p>
  1377. <table width='100%' border='0' cellpadding='1' cellspacing='0'>
  1378. <form method='post' action=''><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1379. <input type='hidden' name='formSent' value='1' />
  1380. <input type='hidden' name='exerciseType' value='".$exerciseType."' />
  1381. <input type='hidden' name='questionNum' value='".$questionNum."' />
  1382. <input type='hidden' name='nbrQuestions' value='".$nbrQuestions."' />
  1383. <tr>
  1384. <td>
  1385. <table width='100%' cellpadding='4' cellspacing='2' border='0'>";
  1386. $exerciseType = 1; //so to list all questions in one page
  1387. $test .= $s;
  1388. $i = 0;
  1389. foreach ($questionList as $questionId) {
  1390. $i ++;
  1391. // for sequential exercises
  1392. if ($exerciseType == 2) {
  1393. // if it is not the right question, goes to the next loop iteration
  1394. if ($questionNum != $i)
  1395. {
  1396. continue;
  1397. } else {
  1398. // if the user has already answered this question
  1399. if (isset ($exerciseResult[$questionId])) {
  1400. // construction of the Question object
  1401. $objQuestionTmp = new Question();
  1402. // reads question informations
  1403. $objQuestionTmp->read($questionId);
  1404. $questionName = $objQuestionTmp->selectTitle();
  1405. // destruction of the Question object
  1406. unset ($objQuestionTmp);
  1407. $test .= '<tr><td>'.get_lang("AlreadyAnswered").' &quot;'.$questionName.'&quot;</td></tr>';
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. echo $s = "<tr bgcolor='#e6e6e6'><td valign='top' colspan='2'>".get_lang('Question')." ";
  1413. //Call the showQuestion() function from exercise.lib.php. This basically displays the question in a table
  1414. $test .= showQuestion($questionId, false, 'export', $i, $nbrQuestions);
  1415. } // end foreach()
  1416. $s = "</table></td></tr><tr><td><br/><input type='button' value='".$langOk."' onClick=\"javascript:evaluate();alert('Evaluated.');\">";
  1417. $s .= "</td></tr></form></table>";
  1418. $s .= "<script type='text/javascript'> loadPage(); </script>";
  1419. $b = 2;
  1420. $test .= $s;
  1421. return ($test);
  1422. }
  1423. /**
  1424. * This function exports the given item
  1425. * @param integer Id from learnpath_items table
  1426. * @param integer Item id
  1427. * @param string Itm type
  1428. * @param boolean Shall the SCORM communications features be added? (true). Default: false.
  1429. * @return void (outputs a zip file)
  1430. * @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
  1431. */
  1432. function exportitem($id, $item_id, $item_type, $add_scorm_communications = false)
  1433. {
  1434. global $circle1_files, $expdir, $_course, $_SESSION, $GLOBALS;
  1435. 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;
  1436. // $_course=$_SESSION['course'];
  1437. require_once (api_get_path(LIBRARY_PATH)."database.lib.php");
  1438. //$tbl_learnpath_item = Database::get_course_learnpath_item_table();
  1439. include_once ('exercise.class.php');
  1440. include_once ('question.class.php');
  1441. include_once ('answer.class.php');
  1442. include_once ('exercise.lib.php');
  1443. include_once ('../lang/english/announcements.inc.php'); //this line is here only for $langPubl in announcements
  1444. include_once ("../lang/".$language_interface."/announcements.inc.php"); //this line is here only for $langPubl in announcements
  1445. include_once ('../lang/english/course_description.inc.php'); //this line is here only for $langThisCourseDescriptionIsEmpty
  1446. include_once ("../lang/".$language_interface."/course_description.inc.php"); // -||-
  1447. include_once ('../lang/english/resourcelinker.inc.php');
  1448. include_once ("../lang/".$language_interface."/resourcelinker.inc.php");
  1449. include_once ('../lang/english/learnpath.inc.php');
  1450. include_once ("../lang/".$language_interface."/learnpath.inc.php");
  1451. include_once ('../lang/english/exercice.inc.php');
  1452. include_once ("../lang/".$language_interface."/exercice.inc.php");
  1453. include_once (api_get_path(LIBRARY_PATH).'text.lib.php');
  1454. include_once ("../resourcelinker/resourcelinker.inc.php");
  1455. $LPname = display_addedresource_link_in_learnpath($item_type, $item_id, '', $id, 'builder', 'nolink');
  1456. $expcontent = "<!--
  1457. This is an exported file from Dokeos Learning Path belonging to a Scorm compliant content package.
  1458. Do not modify or replace individually.
  1459. Export module author : Denes Nagy <darkden@evk.bke.hu>
  1460. -->
  1461. ";
  1462. //files needed for communicating with the scos
  1463. $scocomfiles = "<script type='text/javascript' src='../js/APIWrapper.js'></script>"."<script type='text/javascript' src='../js/SCOFunctions.js'></script>";
  1464. $expcontent .= '<html><head><link rel="stylesheet" href="../css/default.css" type="text/css" media="screen,projection" />'.$scocomfiles.'</head><body>';
  1465. $donebutton .= "<script type='text/javascript'>
  1466. /* <![CDATA[ */
  1467. loadPage();
  1468. var studentName = '!';
  1469. var lmsStudentName = doLMSGetValue( 'cmi.core.student_name' );
  1470. if ( lmsStudentName != '' )
  1471. {
  1472. studentName = ' ' + lmsStudentName + '!';
  1473. }
  1474. /* ]]> */
  1475. </script>
  1476. <br /><br />
  1477. <form><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1478. <table cols='3' width='100%' align='center'>
  1479. <tr>
  1480. <td align='middle'><input type = 'button' value = ' ".$langDone." ' onclick = \"doQuit('completed')\" id='button2' name='button2'></td>
  1481. </tr>
  1482. </table>
  1483. </form>";
  1484. /**
  1485. * switch between the different element types, namely:
  1486. * - Agenda
  1487. * - Ad_Valvas
  1488. * - Course_description
  1489. * - Document
  1490. * - Introduction_text
  1491. * - HotPotatoes
  1492. * - Exercise
  1493. * - Post
  1494. * - Forum ]
  1495. * - Thread ]
  1496. * - Dropbox ]
  1497. * - Assignments ] Theses elements are all replaced by a simple message in the exported document
  1498. * - Groups ]
  1499. * - Users ]
  1500. * - Link _self
  1501. * - Link _blank
  1502. */
  1503. switch ($item_type)
  1504. {
  1505. //------------------------AGENDA BEGIN-------------------
  1506. case "Agenda" :
  1507. //1 Get agenda event data from the database table
  1508. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  1509. $sql = "SELECT * FROM ".$TABLEAGENDA." where (id=$item_id)";
  1510. $result = Database::query($sql);
  1511. //2 Prepare table output
  1512. $expcontent .= "<table class=\"data_table\" >";
  1513. $barreMois = "";
  1514. //3 For each event corresponding to this agenda, do the following:
  1515. while ($myrow = Database::fetch_array($result))
  1516. {
  1517. $start_date_local = api_get_local_time($myrow["start_date"], null, date_default_timezone_get());
  1518. //3.1 Make the blue month bar appear only once.
  1519. if ($barreMois != api_format_date($start_date_local, "%m"))
  1520. {
  1521. //3.1.1 Update the check value for the month bar
  1522. $barreMois = api_format_date($start_date_local, "%m");
  1523. //3.1.2 Display the month bar
  1524. $expcontent .= "<tr><td id=\"title\" colspan=\"2\" class=\"month\" valign=\"top\">".api_format_date($start_date_local, "%B %Y")."</td></tr>";
  1525. }
  1526. //3.2 Display the agenda items (of this month): the date, hour and title
  1527. $db_date = (int) api_format_date($start_date_local, "%d");
  1528. if ($_GET["day"] <> $db_date)
  1529. { //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'
  1530. $expcontent .= "<tr><td class=\"data\" colspan='2'>";
  1531. }
  1532. else
  1533. { //3.2.1.b Else (same day) use style 'datanow'
  1534. $expcontent .= "<tr><td class=\"datanow\" colspan='2'>";
  1535. }
  1536. //3.2.2 Mark an anchor for this date
  1537. $expcontent .= "<a name=\"".$db_date."\"></a>"; // anchoring
  1538. //3.2.3 Write the date and time of this event to the export string
  1539. $expcontent .= api_format_date($start_date_local);
  1540. //3.2.4 If a duration is set, write it, otherwise ignore
  1541. if ($myrow["duration"] == "")
  1542. {
  1543. $expcontent .= "<br />";
  1544. }
  1545. else
  1546. {
  1547. $expcontent .= " / ".$lang_lasting." ".$myrow["duration"]."<br/>";
  1548. }
  1549. //3.2.5 Write the title
  1550. $expcontent .= $myrow["title"];
  1551. $expcontent .= "</td></tr>";
  1552. //3.2.6 Prepare the content of the agenda item
  1553. $content = $myrow["content"];
  1554. //3.2.7 Make clickable???
  1555. $content = make_clickable($content);
  1556. $content = api_parse_tex($content);
  1557. //3.2.8 Write the prepared content to the export string
  1558. $expcontent .= "<tr><td class=\"text\" colspan='2'>";
  1559. $expcontent .= $content;
  1560. $expcontent .= "</td></tr>";
  1561. // displaying the agenda item of this month: the added resources
  1562. // this part is not included into LP export
  1563. /*if (check_added_resources("Agenda", $myrow["id"]))
  1564. {
  1565. $content.= "<tr><td colspan='2'>";
  1566. $content.= "<i>".get_lang('AddedResources')."</i><br/>";
  1567. display_added_resources("Agenda", $myrow["id"]);
  1568. $content.= "</td></tr>";
  1569. }*/
  1570. }
  1571. //4 Finish the export string
  1572. $expcontent .= "<tr></table>";
  1573. break;
  1574. //------------------------ANNOUNCEMENT BEGIN-------------------
  1575. case "Ad_Valvas" :
  1576. //1 Get the announcement data from the database
  1577. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1578. $sql = "SELECT * FROM $tbl_announcement where id='$item_id'";
  1579. $result = Database::query($sql);
  1580. //2 Initialise export string
  1581. $expcontent .= "<table class=\"data_table\">";
  1582. //3 For each announcement matching the query
  1583. while ($myrow = Database::fetch_array($result))
  1584. {
  1585. //3.1 Get the __ field data
  1586. $content = $myrow[1];
  1587. //$content = nl2br($content);
  1588. //3.2 Prepare the data for export
  1589. $content = make_clickable($content);
  1590. $content = api_parse_tex($content);
  1591. //3.3 Get a UNIX(?<-mktime) Timestamp of the end_date for this announcement
  1592. $last_post_datetime = $myrow['end_date']; // post time format datetime de mysql
  1593. list ($last_post_date, $last_post_time) = split(" ", $last_post_datetime);
  1594. list ($year, $month, $day) = explode("-", $last_post_date);
  1595. list ($hour, $min) = explode(":", $last_post_time);
  1596. $announceDate = mktime($hour, $min, 0, $month, $day, $year);
  1597. //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)
  1598. if ($announceDate > $_SESSION['user_last_login_datetime'])
  1599. {
  1600. $colorBecauseNew = " color=\"red\" ";
  1601. }
  1602. else
  1603. {
  1604. $colorBecauseNew = " ";
  1605. }
  1606. //3.5 Write this content to the export string (formatted HTML array)
  1607. $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";
  1608. } // while loop
  1609. //4 Finish the export string
  1610. $expcontent .= "</table>";
  1611. break;
  1612. //------------------------Course_description BEGIN-------------------
  1613. case "Course_description" :
  1614. //1 Get course description data from database
  1615. $tbl_course_description = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  1616. $result = Database::query("SELECT id, title, content FROM ".$tbl_course_description." ORDER BY id");
  1617. //2 Check this element
  1618. if (Database::num_rows($result))
  1619. {
  1620. //2.a This course has one (or more) description in the database
  1621. $expcontent .= "<hr noshade=\"noshade\" size=\"1\" />";
  1622. //2.a.1 For each description available for this course
  1623. while ($row = Database::fetch_array($result))
  1624. {
  1625. //2.a.1.1 Write title to export string
  1626. $expcontent .= "<h4>".$row['title']."</h4>";
  1627. //2.a.1.2 Prepare content
  1628. $content = make_clickable(nl2br($row['content']));
  1629. $content = api_parse_tex($content);
  1630. //2.a.1.3 Write content to the export string
  1631. $expcontent .= $content;
  1632. }
  1633. }
  1634. else
  1635. {
  1636. //2.b This course has no description available
  1637. $expcontent .= "<br /><h4>$langThisCourseDescriptionIsEmpty</h4>";
  1638. }
  1639. break;
  1640. //------------------------DOCUMENT BEGIN-------------------
  1641. case "Document" :
  1642. //1 Get the document data from the database
  1643. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1644. $sql_query = "SELECT * FROM $tbl_document WHERE id=$item_id";
  1645. $sql_result = Database::query($sql_query);
  1646. $myrow = Database::fetch_array($sql_result);
  1647. //2 Get the origin path of the document to treat it internally
  1648. $orig = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$myrow["path"];
  1649. //3 Make some kind of strange transformation to get the destination filepath ???
  1650. $pathname = explode("/", $myrow["path"]);
  1651. $last = count($pathname) - 1;
  1652. $filename = 'data/'.$filename.$pathname[$last];
  1653. $copyneeded = true;
  1654. //htm files do not need to be copied as the ok button is inserted into them,
  1655. //so don't copy directly
  1656. $extension = explode(".", $pathname[$last]);
  1657. //This old condition was WRONG for names like design.html.old. Instead, we now get the extension
  1658. // by using preg_match to match case-insensitive (probably faster than 4 conditions)
  1659. //if (($extension[1]=='htm') or ($extension[1]=='html') or ($extension[1]=='HTM') or ($extension[1]=='HTML')) {
  1660. //4 Check the file extension
  1661. if (preg_match('/.*(\.htm(l)?)$/i', $pathname[$last]))
  1662. {
  1663. //4.a If this file ends with ".htm(l)", we consider it's an HTML file
  1664. //src tag check begin
  1665. //we now check if there is any src attribute in htm(l) files, if yes, we have to also export
  1666. //the target file (swf, mp3, video,...) of that src tag
  1667. //In case of absolute links (http://) this is not neccessary, but makes no error.
  1668. //however still missing : relative links case with subdirs -> the necessary dirs are not created in the exported package
  1669. //4.a.1 Get the file contents into $file
  1670. $file = file_get_contents($orig);
  1671. //4.a.2 Get all the src links in this file
  1672. //preg_match_all("|((?i)src=\".*\" )|U",$file,$match);
  1673. $match = GetSRCTags($orig);
  1674. //4.a.3 For each src tag found, do the following:
  1675. for ($i = 0; $i < count($match); $i ++)
  1676. {
  1677. //4.a.3.1 Get the tag (split from the key)
  1678. list ($key, $srctag) = each($match);
  1679. $src = $srctag;
  1680. //4.a.3.2 Check the link kind (web or absolute/relative)
  1681. if (stristr($src, "http") === false)
  1682. {
  1683. //4.a.3.2.a Do something only if relative (otherwise the user will be able to see it too anyway)
  1684. //4.a.3.2.a.1 Get a proper URL and remove all './'
  1685. $src = urldecode($src); //mp3
  1686. //$src=str_replace('./','',$src);
  1687. $src = preg_replace('/^\.\//', '', $src);
  1688. //4.a.3.2.a.2 Remove the player link from the URL (only use the mp3 file)
  1689. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1690. //4.a.3.2.a.3 Remove funny link parts
  1691. $src = str_replace('?0', '', $src); //mp3
  1692. //the previous lines are used when creating docs with Dokeos Document tool's htmlarea
  1693. //rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1694. //and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1695. //created by any other software
  1696. //4.a.3.2.a.4 Prepare source and destination paths
  1697. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1698. $dest = $expdir.'/data/'.$src;
  1699. //CopyNCreate($source,$dest);
  1700. rcopy($source, $dest);
  1701. } //else...?
  1702. }
  1703. //src tag check end
  1704. //sco communication insertion begin
  1705. //4.a.4 If we want to add SCORM actions and a "Done" button, do the following:
  1706. if ($add_scorm_communications === true)
  1707. {
  1708. if ($bodyclose = strpos($file, '</body>'))
  1709. {
  1710. $file = substr_replace($file, $scocomfiles.$donebutton, $bodyclose, 7);
  1711. }
  1712. elseif ($htmlclose = strpos($file, '</html>'))
  1713. {
  1714. $file = substr_replace($file, $scocomfiles.$donebutton, $htmlclose, 7);
  1715. $file .= '</html>';
  1716. }
  1717. else
  1718. {
  1719. $file .= $scocomfiles.$donebutton;
  1720. }
  1721. } //sco communication insertion end
  1722. //4.a.5 Replace the file's name by adding the element's ID before htm
  1723. // This will not work with uppercase HTML though. Maybe use the preg_replace syntax proposed...
  1724. $filename = str_replace('.htm', $id.'.htm', $filename);
  1725. //$filename=preg_replace('/.*(\.htm(l)?)$/i',$id.$1,$filename);
  1726. //4.a.6 Export these contents to a file and set the circle1_files array for later reuse
  1727. exporttofile($filename, $LPname, $id, $file);
  1728. //The file has been copied, so ask not to copy it again
  1729. $copyneeded = false;
  1730. } //if (htm(l) files) end
  1731. //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
  1732. if ($copyneeded)
  1733. {
  1734. copy($orig, $expdir.'/'.$filename);
  1735. $circle1_files[0][] = $filename;
  1736. $circle1_files[1][] = $LPname;
  1737. $circle1_files[2][] = $id;
  1738. }
  1739. //echo $orig;
  1740. return;
  1741. //------------------------Introduction_text BEGIN-------------------
  1742. case "Introduction_text" :
  1743. //1 Get the introduction text data from the database
  1744. $TBL_INTRO = Database :: get_course_tool_intro_table();
  1745. // Modified by Ivan Tcholakov, 15-SEP-2008.
  1746. //$result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE id=1");
  1747. $result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE id='course_homepage'");
  1748. //
  1749. $myrow = Database::fetch_array($result);
  1750. $intro = $myrow["intro_text"];
  1751. //2 Write introduction text to the export string
  1752. $expcontent .= "<br />".$intro;
  1753. break;
  1754. //------------------------HotPotatoes BEGIN-------------------
  1755. case "HotPotatoes" :
  1756. //1 Get HotPotatoes data from the document table
  1757. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1758. $result = Database::query("SELECT * FROM $tbl_document WHERE id=$item_id");
  1759. $myrow = Database::fetch_array($result);
  1760. //2 Get the document path
  1761. $testfile = api_get_path(SYS_COURSE_PATH).$_course['path']."/document".urldecode($myrow['path']);
  1762. //3 Get the document contents into a string
  1763. $content = file_get_contents($testfile);
  1764. //4 Get the document filename (just the file, no path) - would probably be better to use PHP native function
  1765. $pathname = explode("/", $myrow["path"]);
  1766. $last = count($pathname) - 1;
  1767. $filename = 'data/'.$filename.$pathname[$last];
  1768. //4beta - get all linked files and copy them (procedure copied from documents type)
  1769. //Get all the src links in this file
  1770. $match = GetSRCTags($testfile);
  1771. //For each src tag found, do the following:
  1772. foreach ($match as $src)
  1773. {
  1774. //Check the link kind (web or absolute/relative)
  1775. if (stristr($src, "http") === false)
  1776. {
  1777. //Do something only if relative (otherwise the user will be able to see it too anyway)
  1778. //Get a proper URL and remove all './'
  1779. $src = urldecode($src); //mp3
  1780. $src = str_replace('./', '', $src);
  1781. //Remove the player link from the URL (only use the mp3 file)
  1782. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1783. //Remove funny link parts
  1784. $src = str_replace('?0', '', $src); //mp3
  1785. //The previous lines are used when creating docs with Dokeos Document tool's htmlarea
  1786. //rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1787. //and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1788. //created by any other software
  1789. //Prepare source and destination paths
  1790. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1791. $dest = $expdir.'/data/'.$src;
  1792. //CopyNCreate($source,$dest);
  1793. rcopy($source, $dest);
  1794. } //else...?
  1795. }
  1796. //5 Prepare the special "close window" for this test
  1797. $closewindow = "<html><head><link rel='stylesheet' type='text/css' href='../css/default.css'></head><body>"."<br /><div class='message'>$langHotPotatoesFinished</div></body></html>";
  1798. //Finish is the function of HP to save scores, we insert our scorm function calls to its beginning
  1799. //'Score' is the variable that tracks the score in HP tests
  1800. //6
  1801. $mit = "function Finish(){";
  1802. $js_content = "var SaveScoreVariable = 0; // This variable is included by Dokeos LP export\n"."function mySaveScore() // This function is included by Dokeos LP export\n"."{\n"." if (SaveScoreVariable==0)\n"." {\n"." SaveScoreVariable = 1;\n".
  1803. //the following function are implemented in SCOFunctions.js
  1804. " exitPageStatus = true;\n"." computeTime();\n"." doLMSSetValue( 'cmi.core.score.raw', Score );\n"." doLMSSetValue( 'cmi.core.lesson_status', 'completed' );\n"." doLMSCommit();\n"." doLMSFinish();\n".
  1805. // " document.write('".$closewindow."');\n".
  1806. //if you insert the previous row, the test does not appear correctly !!!!
  1807. " }\n"."}\n"."function Finish(){\n"." mySaveScore();";
  1808. $start = "<script type='text/javascript'> loadPage(); </script>";
  1809. //7 Replace the current MIT function call by our set of functions. In clear, transform HP to SCORM
  1810. $content = str_replace($mit, $js_content, $content);
  1811. //8 Finally, add the API loading calls (although that might have been done first)
  1812. $content = str_replace("</script>", "</script>".$scocomfiles.$start, $content);
  1813. //9 Change the filename to add the database ID and export to a new file,
  1814. // setting the circle1_files array for later reuse
  1815. $filename = str_replace('.htm', $id.'.htm', $filename);
  1816. exporttofile($filename, $LPname, $id, $content);
  1817. return;
  1818. //------------------------Dokeos test BEGIN-------------------
  1819. case "Exercise" :
  1820. //1 Use the export_exercise() function to do the job of constructing the question's HTML table
  1821. $expcontent .= export_exercise($item_id);
  1822. break;
  1823. //------------------------POST BEGIN---------------------------------------
  1824. case "Post" :
  1825. //1 Get the forum post data from the database
  1826. $tbl_posts =Database::get_course_table(TABLE_FORUM_POST);
  1827. $tbl_posts_text =Database::get_course_table(TOOL_FORUM_POST_TEXT_TABLE);
  1828. $result = Database::query("SELECT * FROM $tbl_posts where post_id=$item_id");
  1829. $myrow = Database::fetch_array($result);
  1830. // grabbing the title of the post
  1831. $sql_titel = "SELECT * FROM $tbl_posts_text WHERE post_id=".$myrow["post_id"];
  1832. $result_titel = Database::query($sql_titel);
  1833. $myrow_titel = Database::fetch_array($result_titel);
  1834. $posternom = $myrow['nom'];
  1835. $posterprenom = $myrow['prenom'];
  1836. $posttime = $myrow['post_time'];
  1837. $posttext = $myrow_titel['post_text'];
  1838. $posttitle = $myrow_titel['post_title'];
  1839. $posttext = str_replace('"', "'", $posttext);
  1840. //2 Export contents as an HTML table
  1841. $expcontent .= "<table border='0' cellpadding='3' cellspacing='1' width='100%'>
  1842. <tr>
  1843. <td colspan='2' bgcolor='#e6e6e6'><b>$posttitle</b><br />$posttext</td>
  1844. </tr>
  1845. <tr>
  1846. <td colspan='2'></td>
  1847. </tr>
  1848. <tr>
  1849. <td bgcolor='#cccccc' align='left'>$lang_author : $posterprenom $posternom</td>
  1850. <td align='right' bgcolor='#cccccc'>$lang_date : $posttime</td>
  1851. </tr>
  1852. <tr><td colspan='2' height='10'></td></tr>
  1853. </table>";
  1854. break;
  1855. //------------------------NOT IMPLEMENTED ITEMS BEGIN-------------------
  1856. case "Forum" :
  1857. case "Thread" :
  1858. case "Dropbox" :
  1859. case "Assignments" :
  1860. case "Groups" :
  1861. case "Users" :
  1862. //1 Instead of building something, put an info message
  1863. $langItemMissing1 = "There was a ";
  1864. $langItemMissing2 = "page (step) here in the original Dokeos Learning Path.";
  1865. $expcontent .= "<div class='message'>$langItemMissing1 $item_type $langItemMissing2</div>";
  1866. break;
  1867. //------------------------Link BEGIN-------------------------------------
  1868. case "Link _self" :
  1869. case "Link _blank" :
  1870. //1 Get the link data from the database
  1871. $TABLETOOLLINK = Database :: get_course_link_table();
  1872. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE id=$item_id");
  1873. $myrow = Database::fetch_array($result);
  1874. $thelink = $myrow["url"];
  1875. //2 Check the link type (open in blank page or in current page)
  1876. if ($item_type == "Link _blank")
  1877. {
  1878. $target = "_blank";
  1879. }
  1880. //3 Write the link to the export string
  1881. $expcontent .= "<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='".$target."'>$LPname</a>";
  1882. //4 Change the element type for later changes (this is lost, however, so useless here)
  1883. $item_type = "Link"; //to put this to the filename
  1884. //$LPname="<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target=".$target.">$LPname</a>";
  1885. //i am still not sure about Link export : to export them as files or they can appear in the TOC at once ?
  1886. //to enable the second possibility, unrem the row $LPname=...
  1887. break;
  1888. }
  1889. //now we add the Done button and the initialize function : loadpage()
  1890. //not in the case of Documents, HotP
  1891. if ($item_type != 'Exercise' and ($add_scorm_communications === true))
  1892. {
  1893. $expcontent .= $donebutton;
  1894. }
  1895. //End the export string with valid HTML tags
  1896. $expcontent .= "</body></html>";
  1897. //Prepare new file name
  1898. $filename = $item_type.$id.".htm";
  1899. //Write the export content to the new file
  1900. exporttofile('data/'.$filename, $LPname, $id, $expcontent);
  1901. }
  1902. /**
  1903. * This function exports the given item's description into a separate file
  1904. * @param integer Item id
  1905. * @param string Item type
  1906. * @param string Description
  1907. * @return void
  1908. */
  1909. function exportdescription($id, $item_type, $description)
  1910. {
  1911. global $expdir;
  1912. $filename = $item_type.$id.".desc";
  1913. $expcontent = $description;
  1914. exporttofile($expdir.$filename, 'description_of_'.$item_type.$id, 'description_of_item_'.$id, $expcontent);
  1915. }
  1916. /**
  1917. * This function deletes an entire directory
  1918. * @param string The directory path
  1919. * @return boolean True on success, false on failure
  1920. */
  1921. function deldir($dir)
  1922. {
  1923. $dh = opendir($dir);
  1924. while ($file = readdir($dh))
  1925. {
  1926. if ($file != "." && $file != "..")
  1927. {
  1928. $fullpath = $dir."/".$file;
  1929. if (!is_dir($fullpath))
  1930. {
  1931. unlink($fullpath);
  1932. }
  1933. else
  1934. {
  1935. deldir($fullpath);
  1936. }
  1937. }
  1938. }
  1939. closedir($dh);
  1940. if (rmdir($dir))
  1941. {
  1942. return true;
  1943. }
  1944. else
  1945. {
  1946. return false;
  1947. }
  1948. }
  1949. /**
  1950. * This functions exports the given path. This is the opener function, which is called first
  1951. * @param integer The path id
  1952. * @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 :-)
  1953. */
  1954. function exportpath($learnpath_id)
  1955. {
  1956. //1 Initialise variables
  1957. global $_course, $circle1_files, $LPnamesafe, $LPname, $expdir;
  1958. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  1959. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  1960. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  1961. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  1962. //where applicable, add a scorm "Done" button at the end of all contents
  1963. $add_scorm_button = true;
  1964. //2 Get the name of the LP
  1965. include_once (api_get_path(LIBRARY_PATH)."fileUpload.lib.php");
  1966. $sql = "SELECT * FROM $tbl_learnpath_main WHERE (lp_id=$learnpath_id)";
  1967. $result = Database::query($sql);
  1968. $row = Database::fetch_array($result);
  1969. $LPname = $row['learnpath_name'];
  1970. $LPnamesafe = replace_dangerous_char($LPname, 'strict');
  1971. //3 Get a temporary dir for creating the zip file
  1972. $expdir = api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe;
  1973. $fromdir = '../scorm/export/'; //this dir contains some standard files
  1974. deldir($expdir); //make sure the temp dir is cleared
  1975. mkdir($expdir, api_get_permissions_for_new_directories());
  1976. mkdir($expdir.'/css', api_get_permissions_for_new_directories());
  1977. mkdir($expdir.'/data', api_get_permissions_for_new_directories());
  1978. mkdir($expdir.'/js', api_get_permissions_for_new_directories());
  1979. mkdir($expdir.'/data/images', api_get_permissions_for_new_directories());
  1980. mkdir($expdir.'/data/audio', api_get_permissions_for_new_directories());
  1981. mkdir($expdir.'/data/videos', api_get_permissions_for_new_directories());
  1982. $circle1 = array (//this array contains the types of elements we want to export
  1983. 'Chapter', 'Agenda', 'Ad_Valvas', 'Course_description', 'Document', 'Introduction_text', 'Link _self', 'Link _blank', 'Forum', 'Thread', 'Post', 'Exercise', 'HotPotatoes', 'Assignments', 'Dropbox', 'Users', 'Groups');
  1984. //$circle2=array('');
  1985. //4 Get the first level chapters - YW added parent_item_id condition for multi-level paths
  1986. $sql = "SELECT * FROM $tbl_learnpath_chapter
  1987. WHERE (lp_id=$learnpath_id and parent_item_id=0)
  1988. ORDER BY display_order ASC";
  1989. //to get all the elements, we should use the function that builds the table of content get_learnpath_tree
  1990. //WHERE (lp_id=$learnpath_id)
  1991. //ORDER BY parent_item_id, display_order ASC";
  1992. $result = Database::query($sql);
  1993. //5 export the items listed in Circle I one by one
  1994. while ($row = Database::fetch_array($result))
  1995. {
  1996. //5.1 Get items data from the database for this chapter
  1997. $parent_item_id = $row['id'];
  1998. //$sql2a="SELECT * FROM $tbl_learnpath_chapter WHERE (lp_id=$learnpath_id and parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  1999. //$result2a=Database::query($sql);
  2000. $sql2b = "SELECT * FROM $tbl_learnpath_item WHERE (parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  2001. $result2b = Database::query($sql2b);
  2002. while ($row2 = Database::fetch_array($result2b))
  2003. {
  2004. //5.1.1 Check if the element is in the circle1 array
  2005. $tobeexported = false;
  2006. for ($i = 0; $i < count($circle1) && !$tobeexported; $i ++)
  2007. {
  2008. //if the type is found in the circle1 array, ask for export
  2009. if ($circle1[$i] == $row2['item_type'])
  2010. {
  2011. $tobeexported = true;
  2012. }
  2013. }
  2014. //5.1.2 If applicable, export the item to an HTML file (see exportitem function for more details)
  2015. if ($tobeexported)
  2016. {
  2017. exportitem($row2['id'], $row2['item_id'], $row2['item_type'], $add_scorm_button);
  2018. /*if ($row2['description']) { //put the description of items to a separate file (.desc)
  2019. exportdescription($row2['id'],$row2['item_type'],$row2['description']);
  2020. }*/
  2021. }
  2022. } //end of items loop
  2023. } //end of first-level chapters loop
  2024. //6 export the other necceassary files
  2025. $filename = 'default.css';
  2026. copy('../css/'.$filename, $expdir.'/css/'.$filename);
  2027. $filename = 'ims_xml.xsd';
  2028. copy($fromdir.$filename, $expdir.'/'.$filename);
  2029. $filename = 'imscp_v1p1.xsd';
  2030. copy($fromdir.$filename, $expdir.'/'.$filename);
  2031. $filename = 'imsmd_v1p2.xsd';
  2032. copy($fromdir.$filename, $expdir.'/'.$filename);
  2033. $filename = 'APIWrapper.js';
  2034. copy($fromdir.$filename, $expdir.'/js/'.$filename);
  2035. $filename = 'SCOFunctions.js';
  2036. copy($fromdir.$filename, $expdir.'/js/'.$filename);
  2037. //in case circle1_files is not defined, build it
  2038. //$circle1_files
  2039. //7 create imsmanifest.xml
  2040. createimsmanifest($circle1_files, $learnpath_id);
  2041. //8 put the files in the exportdir into a zip and force download
  2042. include_once (api_get_path(LIBRARY_PATH)."pclzip/pclzip.lib.php");
  2043. //create zipfile of given directory
  2044. $zip_folder = new PclZip(api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe.".zip");
  2045. $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/");
  2046. //api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe); // whitout folder
  2047. // modified by imandak80
  2048. /* copy(api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe.".zip",
  2049. api_get_path('SYS_COURSE_PATH').$_course['path']."/document/".$LPnamesafe.".zip");
  2050. */
  2051. $zipfoldername = api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe;
  2052. $zipfilename = $zipfoldername.".zip";
  2053. DocumentManager :: file_send_for_download($zipfilename, false, basename($LPnamesafe.".zip"));
  2054. //9 Delete the temporary zip file and directory
  2055. include_once (api_get_path(LIBRARY_PATH)."fileManage.lib.php");
  2056. // in fileManage.lib.php
  2057. my_delete($zipfilename);
  2058. my_delete($zipfoldername);
  2059. //exit;
  2060. //0 Return the circle_files hash (array)
  2061. return ($circle1_files); //has been used before...
  2062. }
  2063. /**
  2064. * Export SCORM content into a zip file
  2065. *
  2066. * Basically, all this function does is put the scorm directory back into a zip file (like the one
  2067. * that was most probably used to import the course at first)
  2068. * @param string Name of the SCORM path (or the directory under which it resides)
  2069. * @param array Not used right now. Should replace the use of global $_course
  2070. * @return void
  2071. * @author imandak80
  2072. */
  2073. function exportSCORM($scormname, $course)
  2074. {
  2075. global $_course;
  2076. //initialize
  2077. $tmpname = api_get_path('SYS_COURSE_PATH').$_course['path']."/scorm";
  2078. $zipfoldername = $tmpname.$scormname;
  2079. $zipfilename = $zipfoldername.".zip";
  2080. //create zipfile of given directory
  2081. include_once (api_get_path(LIBRARY_PATH)."pclzip/pclzip.lib.php");
  2082. $zip_folder = new PclZip($zipfilename);
  2083. $list = 1;
  2084. //$list = $zip_folder->create($zipfoldername."/",PCLZIP_OPT_REMOVE_PATH,$tmpname.$scormname."/"); // whitout folder
  2085. $list = $zip_folder->create($zipfoldername."/", PCLZIP_OPT_REMOVE_PATH, $tmpname);
  2086. if ($list == 0)
  2087. {
  2088. // echo "Error : ".$zip_folder->errorInfo(true);
  2089. }
  2090. //send to client
  2091. DocumentManager :: file_send_for_download($zipfilename, false, basename($scormname.".zip"));
  2092. //clear
  2093. include_once (api_get_path(LIBRARY_PATH)."fileManage.lib.php");
  2094. my_delete($zipfilename);
  2095. }
  2096. /**
  2097. * This function returns an xml tag
  2098. * $data behaves as the content in case of full tags
  2099. * $data is an array of attributes in case of returning an opening tag
  2100. * @param string
  2101. * @param string
  2102. * @param array
  2103. * @param string
  2104. * @return string
  2105. */
  2106. function xmltagwrite($tagname, $which, $data, $linebreak = "yes")
  2107. {
  2108. switch ($which)
  2109. {
  2110. case "open" :
  2111. $tag = "<".$tagname;
  2112. $i = 0;
  2113. while ($data[0][$i])
  2114. {
  2115. $tag .= " ".$data[0][$i]."=\"".$data[1][$i]."\"";
  2116. $i ++;
  2117. }
  2118. if ($tagname == 'file')
  2119. {
  2120. $closing = '/';
  2121. }
  2122. $tag .= $closing.">";
  2123. if ($linebreak != 'no_linebreak')
  2124. {
  2125. $tag .= "\n";
  2126. }
  2127. break;
  2128. case "close" :
  2129. $tag = "</".$tagname.">";
  2130. if ($linebreak != 'no_linebreak')
  2131. {
  2132. $tag .= "\n";
  2133. }
  2134. break;
  2135. case "full" :
  2136. $tag = "<".$tagname;
  2137. $tag .= ">".$data."</".$tagname.">";
  2138. if ($linebreak != 'no_linebreak')
  2139. {
  2140. $tag .= "\n";
  2141. }
  2142. break;
  2143. }
  2144. return $tag;
  2145. }
  2146. /**
  2147. * This function writes the imsmanifest.xml and exports the chapter names
  2148. * @param array Array containing filenames
  2149. * @param integer Learnpath_id
  2150. * @return void
  2151. */
  2152. function createimsmanifest($circle1_files, $learnpath_id)
  2153. {
  2154. global $_course, $LPname, $expdir, $LPnamesafe;
  2155. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  2156. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  2157. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  2158. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  2159. include_once ('../metadata/md_funcs.php'); // RH: export metadata
  2160. //1.1 header
  2161. /*
  2162. $header='<?xml version="1.0" encoding="UTF-8"?><manifest identifier="'.$LPnamesafe.'" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2.xsd" version="IMS CP 1.1.3">'."\n";
  2163. */
  2164. //1.2
  2165. //charset should be dependent on content
  2166. $mycharset = api_get_system_encoding();
  2167. $header = '<?xml version="1.0" encoding="'.$mycharset.'"?>'."\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";
  2168. $org .= xmltagwrite('metadata', 'open');
  2169. $org .= " ".xmltagwrite('schema', 'full', 'ADL SCORM');
  2170. $org .= " ".xmltagwrite('schemaversion', 'full', '1.2');
  2171. $org .= xmltagwrite('metadata', 'close');
  2172. $defaultorgname = 'default_org';
  2173. $attributes[0][0] = 'default';
  2174. $attributes[1][0] = $defaultorgname;
  2175. $org .= xmltagwrite('organizations', 'open', $attributes);
  2176. $attributes[0][0] = 'identifier';
  2177. $attributes[1][0] = $defaultorgname;
  2178. $org .= " ".xmltagwrite('organization', 'open', $attributes);
  2179. $org .= " ".xmltagwrite('title', 'full', $LPname);
  2180. //items list
  2181. $i = 0;
  2182. $previous_item_id = '00';
  2183. while ($circle1_files[0][$i])
  2184. {
  2185. //check whether we are in the border of two chapters
  2186. //if (!$desc=strpos($circle1_files[2][$i],'scription')) { //this is is needed if the descriptions are exported to file
  2187. $sql = "SELECT * FROM $tbl_learnpath_item WHERE (id=".$circle1_files[2][$i].")";
  2188. $result = Database::query($sql);
  2189. $row = Database::fetch_array($result);
  2190. $parent_item_id = $row['parent_item_id'];
  2191. if ($parent_item_id != $previous_item_id)
  2192. {
  2193. //we create the item tag for the chapter (without indifierref)
  2194. $sql2 = "SELECT * FROM $tbl_learnpath_chapter WHERE (id=".$parent_item_id.")";
  2195. $result2 = Database::query($sql2);
  2196. $row2 = Database::fetch_array($result2);
  2197. $chapter_name = $row2['chapter_name'];
  2198. $attributes = '';
  2199. $attributes[0][] = 'identifier';
  2200. $attributes[1][] = 'chapter_'.$row2['id'];
  2201. $attributes[0][] = 'isvisible';
  2202. $attributes[1][] = '1';
  2203. if ($previous_item_id != '00')
  2204. {
  2205. $org .= " ".xmltagwrite('item', 'close');
  2206. }
  2207. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2208. $org .= " ".xmltagwrite('title', 'full', $chapter_name);
  2209. if ($row2['chapter_description'] != '')
  2210. {
  2211. //chapter description
  2212. $attributes = '';
  2213. $attributes[0][] = 'identifier';
  2214. $attributes[1][] = 'chapter_'.$row2['id'].'_desc';
  2215. $attributes[0][] = 'isvisible';
  2216. $attributes[1][] = '1';
  2217. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2218. $org .= " ".xmltagwrite('title', 'full', ' '.$row2['chapter_description']);
  2219. $org .= " ".xmltagwrite('item', 'close');
  2220. }
  2221. }
  2222. $previous_item_id = $parent_item_id;
  2223. //}
  2224. $attributes = ''; //item output
  2225. $attributes[0][] = 'identifier';
  2226. $attributes[1][] = 'item_'.$circle1_files[2][$i];
  2227. $attributes[0][] = 'identifierref';
  2228. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  2229. $attributes[0][] = 'isvisible';
  2230. $attributes[1][] = '1';
  2231. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2232. $org .= " ".xmltagwrite('title', 'full', $circle1_files[1][$i]);
  2233. if ($row['prereq_id'] != '')
  2234. { //item prerequisites
  2235. $attributes = '';
  2236. $attributes[0][] = 'type';
  2237. $attributes[1][] = 'aicc_script';
  2238. $org .= " ".xmltagwrite('adlcp:prerequisites', 'open', $attributes, "no_linebreak");
  2239. if ($row['prereq_type'] == 'i')
  2240. {
  2241. $org .= 'item_'.$row['prereq_id'];
  2242. }
  2243. if ($row['prereq_type'] == 'c')
  2244. {
  2245. $org .= 'chapter_'.$row['prereq_id'];
  2246. }
  2247. $org .= xmltagwrite('adlcp:prerequisites', 'close', $attributes);
  2248. }
  2249. if ($row['description'] != '')
  2250. {
  2251. //item description
  2252. $attributes = '';
  2253. $attributes[0][] = 'identifier';
  2254. $attributes[1][] = 'item_'.$circle1_files[2][$i].'_desc';
  2255. $attributes[0][] = 'isvisible';
  2256. $attributes[1][] = '1';
  2257. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2258. $org .= " ".xmltagwrite('title', 'full', ' '.$row['description']);
  2259. $org .= " ".xmltagwrite('item', 'close');
  2260. }
  2261. $mds = new mdstore(TRUE); // RH: export metadata; if no table, create it
  2262. if (($mdt = $mds->mds_get($row['item_type'].'.'.$row['item_id'])))
  2263. if (($mdo = api_strpos($mdt, '<metadata>')) && ($mdc = api_strpos($mdt, '</metadata>')))
  2264. $org .= " ".api_substr($mdt, $mdo, $mdc - $mdo +11)."\n";
  2265. $org .= " ".xmltagwrite('item', 'close');
  2266. $i ++;
  2267. }
  2268. if ($circle1_files)
  2269. {
  2270. $org .= " ".xmltagwrite('item', 'close');
  2271. } //not needed in case of a blank path
  2272. $org .= " ".xmltagwrite('organization', 'close');
  2273. $org .= xmltagwrite('organizations', 'close');
  2274. $org .= xmltagwrite('resources', 'open');
  2275. //resources list
  2276. $i = 0;
  2277. while ($circle1_files[0][$i])
  2278. {
  2279. $attributes = '';
  2280. $attributes[0][] = 'identifier';
  2281. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  2282. $attributes[0][] = 'type';
  2283. $attributes[1][] = 'webcontent';
  2284. $attributes[0][] = 'adlcp:scormtype';
  2285. $attributes[1][] = 'sco';
  2286. $attributes[0][] = 'href';
  2287. $attributes[1][] = $circle1_files[0][$i];
  2288. $org .= " ".xmltagwrite('resource', 'open', $attributes);
  2289. $org .= " ".xmltagwrite('metadata', 'open');
  2290. $org .= " ".xmltagwrite('schema', 'full', 'ADL SCORM');
  2291. $org .= " ".xmltagwrite('schemaversion', 'full', '1.2');
  2292. $org .= " ".xmltagwrite('metadata', 'close');
  2293. $attributes = '';
  2294. $attributes[0][] = 'href';
  2295. $attributes[1][] = $circle1_files[0][$i];
  2296. $org .= " ".xmltagwrite('file', 'open', $attributes);
  2297. $org .= " ".xmltagwrite('resource', 'close');
  2298. $i ++;
  2299. }
  2300. $org .= xmltagwrite('resources', 'close');
  2301. $org .= xmltagwrite('manifest', 'close');
  2302. $manifest = $header.$org;
  2303. exporttofile('imsmanifest.xml', 'Manifest file', '0', $manifest);
  2304. }
  2305. /**
  2306. * Gets the tags of the file given as parameter
  2307. *
  2308. * if $filename is not found, GetSRCTags(filename) will return FALSE
  2309. * @param string file path
  2310. * @return mixed array of strings on success, false on failure
  2311. * @author unknown
  2312. * @author included by imandak80
  2313. */
  2314. function GetSRCTags($fileName)
  2315. {
  2316. if (!($fp = fopen($fileName, "r")))
  2317. {
  2318. //if file can't be opened, return false
  2319. return false;
  2320. }
  2321. //read file contents
  2322. $contents = fread($fp, filesize($fileName));
  2323. fclose($fp);
  2324. $matches = array ();
  2325. $srcList = array ();
  2326. //get all src tags contents in this file. Use multi-line search.
  2327. preg_match_all('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/mi', $contents, $matches); //get the img src as contained between " or '
  2328. foreach ($matches[3] as $match)
  2329. {
  2330. if (!in_array($match, $srcList))
  2331. {
  2332. $srcList[] = $match;
  2333. }
  2334. }
  2335. if (count($srcList) == 0)
  2336. {
  2337. return false;
  2338. }
  2339. return $srcList;
  2340. }
  2341. /**
  2342. * Copy file and create directories in the path if needed.
  2343. *
  2344. * @param string $source Source path
  2345. * @param string $dest Destination path
  2346. * @return boolean true on success, false on failure
  2347. */
  2348. function CopyNCreate($source, $dest)
  2349. {
  2350. if (strcmp($source, $dest) == 0)
  2351. return false;
  2352. $dir = "";
  2353. $tdest = explode('/', $dest);
  2354. for ($i = 0; $i < sizeof($tdest) - 1; $i ++)
  2355. {
  2356. $dir = $dir.$tdest[$i]."/";
  2357. if (!is_dir($dir))
  2358. if (!mkdir($dir, api_get_permissions_for_new_directories()))
  2359. return false;
  2360. }
  2361. if (!copy($source, $dest))
  2362. return false;
  2363. return true;
  2364. }
  2365. function rcopy($source, $dest)
  2366. {
  2367. //error_log($source." -> ".$dest,0);
  2368. if (!file_exists($source))
  2369. {
  2370. //error_log($source." does not exist",0);
  2371. return false;
  2372. }
  2373. if (is_dir($source))
  2374. {
  2375. //error_log($source." is a dir",0);
  2376. //this is a directory
  2377. //remove trailing '/'
  2378. if (strrpos($source, '/') == sizeof($source) - 1)
  2379. {
  2380. $source = substr($source, 0, size_of($source) - 1);
  2381. }
  2382. if (strrpos($dest, '/') == sizeof($dest) - 1)
  2383. {
  2384. $dest = substr($dest, 0, size_of($dest) - 1);
  2385. }
  2386. if (!is_dir($dest))
  2387. {
  2388. $res = @mkdir($dest, api_get_permissions_for_new_directories());
  2389. if ($res !== false)
  2390. {
  2391. return true;
  2392. }
  2393. else
  2394. {
  2395. //remove latest part of path and try creating that
  2396. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/'))))
  2397. {
  2398. return @mkdir($dest, api_get_permissions_for_new_directories());
  2399. }
  2400. else
  2401. {
  2402. return false;
  2403. }
  2404. }
  2405. }
  2406. return true;
  2407. }
  2408. else
  2409. {
  2410. //this is presumably a file
  2411. //error_log($source." is a file",0);
  2412. if (!@ copy($source, $dest))
  2413. {
  2414. //error_log("Could not simple-copy $source",0);
  2415. $res = rcopy(dirname($source), dirname($dest));
  2416. if ($res === true)
  2417. {
  2418. //error_log("Welcome dir created",0);
  2419. return @ copy($source, $dest);
  2420. }
  2421. else
  2422. {
  2423. return false;
  2424. //error_log("Error creating path",0);
  2425. }
  2426. }
  2427. else
  2428. {
  2429. //error_log("Could well simple-copy $source",0);
  2430. return true;
  2431. }
  2432. }
  2433. }
  2434. ?>