learnpath_functions.inc.php 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  230. Database::query($sql2, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  268. Database::query($sql2, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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,__FILE__,__LINE__);
  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,__FILE__,__LINE__);
  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, __FILE__, __LINE__);
  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,__FILE__,__LINE__);
  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,__FILE__,__LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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. //3.1 Make the blue month bar appear only once.
  1518. if ($barreMois != date("m", strtotime($myrow["start_date"])))
  1519. {
  1520. //3.1.1 Update the check value for the month bar
  1521. $barreMois = date("m", strtotime($myrow["start_date"]));
  1522. //3.1.2 Display the month bar
  1523. $expcontent .= "<tr><td id=\"title\" colspan=\"2\" class=\"month\" valign=\"top\">".api_ucfirst(format_locale_date("%B %Y", strtotime($myrow["start_date"])))."</td></tr>";
  1524. }
  1525. //3.2 Display the agenda items (of this month): the date, hour and title
  1526. $db_date = (int) date(d, strtotime($myrow["start_date"]));
  1527. if ($_GET["day"] <> $db_date)
  1528. { //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'
  1529. $expcontent .= "<tr><td class=\"data\" colspan='2'>";
  1530. }
  1531. else
  1532. { //3.2.1.b Else (same day) use style 'datanow'
  1533. $expcontent .= "<tr><td class=\"datanow\" colspan='2'>";
  1534. }
  1535. //3.2.2 Mark an anchor for this date
  1536. $expcontent .= "<a name=\"".(int) date(d, strtotime($myrow["start_date"]))."\"></a>"; // anchoring
  1537. //3.2.3 Write the date and time of this event to the export string
  1538. $expcontent .= api_ucfirst(format_locale_date($dateFormatLong, strtotime($myrow["start_date"])))."&nbsp;&nbsp;&nbsp;";
  1539. $expcontent .= api_ucfirst(strftime($timeNoSecFormat, strtotime($myrow["start_time"])))."";
  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, __FILE__, __LINE__);
  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_ucfirst(format_locale_date($dateFormatLong, strtotime($last_post_date)))."</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", __FILE__, __LINE__);
  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, __FILE__, __LINE__);
  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. $result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE id=1");
  1746. $myrow = Database::fetch_array($result);
  1747. $intro = $myrow["intro_text"];
  1748. //2 Write introduction text to the export string
  1749. $expcontent .= "<br />".$intro;
  1750. break;
  1751. //------------------------HotPotatoes BEGIN-------------------
  1752. case "HotPotatoes" :
  1753. //1 Get HotPotatoes data from the document table
  1754. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1755. $result = Database::query("SELECT * FROM $tbl_document WHERE id=$item_id", __FILE__, __LINE__);
  1756. $myrow = Database::fetch_array($result);
  1757. //2 Get the document path
  1758. $testfile = api_get_path(SYS_COURSE_PATH).$_course['path']."/document".urldecode($myrow['path']);
  1759. //3 Get the document contents into a string
  1760. $content = file_get_contents($testfile);
  1761. //4 Get the document filename (just the file, no path) - would probably be better to use PHP native function
  1762. $pathname = explode("/", $myrow["path"]);
  1763. $last = count($pathname) - 1;
  1764. $filename = 'data/'.$filename.$pathname[$last];
  1765. //4beta - get all linked files and copy them (procedure copied from documents type)
  1766. //Get all the src links in this file
  1767. $match = GetSRCTags($testfile);
  1768. //For each src tag found, do the following:
  1769. foreach ($match as $src)
  1770. {
  1771. //Check the link kind (web or absolute/relative)
  1772. if (stristr($src, "http") === false)
  1773. {
  1774. //Do something only if relative (otherwise the user will be able to see it too anyway)
  1775. //Get a proper URL and remove all './'
  1776. $src = urldecode($src); //mp3
  1777. $src = str_replace('./', '', $src);
  1778. //Remove the player link from the URL (only use the mp3 file)
  1779. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1780. //Remove funny link parts
  1781. $src = str_replace('?0', '', $src); //mp3
  1782. //The previous lines are used when creating docs with Dokeos Document tool's htmlarea
  1783. //rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1784. //and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1785. //created by any other software
  1786. //Prepare source and destination paths
  1787. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname($myrow['path']).'/'.$src;
  1788. $dest = $expdir.'/data/'.$src;
  1789. //CopyNCreate($source,$dest);
  1790. rcopy($source, $dest);
  1791. } //else...?
  1792. }
  1793. //5 Prepare the special "close window" for this test
  1794. $closewindow = "<html><head><link rel='stylesheet' type='text/css' href='../css/default.css'></head><body>"."<br /><div class='message'>$langHotPotatoesFinished</div></body></html>";
  1795. //Finish is the function of HP to save scores, we insert our scorm function calls to its beginning
  1796. //'Score' is the variable that tracks the score in HP tests
  1797. //6
  1798. $mit = "function Finish(){";
  1799. $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".
  1800. //the following function are implemented in SCOFunctions.js
  1801. " exitPageStatus = true;\n"." computeTime();\n"." doLMSSetValue( 'cmi.core.score.raw', Score );\n"." doLMSSetValue( 'cmi.core.lesson_status', 'completed' );\n"." doLMSCommit();\n"." doLMSFinish();\n".
  1802. // " document.write('".$closewindow."');\n".
  1803. //if you insert the previous row, the test does not appear correctly !!!!
  1804. " }\n"."}\n"."function Finish(){\n"." mySaveScore();";
  1805. $start = "<script type='text/javascript'> loadPage(); </script>";
  1806. //7 Replace the current MIT function call by our set of functions. In clear, transform HP to SCORM
  1807. $content = str_replace($mit, $js_content, $content);
  1808. //8 Finally, add the API loading calls (although that might have been done first)
  1809. $content = str_replace("</script>", "</script>".$scocomfiles.$start, $content);
  1810. //9 Change the filename to add the database ID and export to a new file,
  1811. // setting the circle1_files array for later reuse
  1812. $filename = str_replace('.htm', $id.'.htm', $filename);
  1813. exporttofile($filename, $LPname, $id, $content);
  1814. return;
  1815. //------------------------Dokeos test BEGIN-------------------
  1816. case "Exercise" :
  1817. //1 Use the export_exercise() function to do the job of constructing the question's HTML table
  1818. $expcontent .= export_exercise($item_id);
  1819. break;
  1820. //------------------------POST BEGIN---------------------------------------
  1821. case "Post" :
  1822. //1 Get the forum post data from the database
  1823. $tbl_posts =Database::get_course_table(TABLE_FORUM_POST);
  1824. $tbl_posts_text =Database::get_course_table(TOOL_FORUM_POST_TEXT_TABLE);
  1825. $result = Database::query("SELECT * FROM $tbl_posts where post_id=$item_id", __FILE__, __LINE__);
  1826. $myrow = Database::fetch_array($result);
  1827. // grabbing the title of the post
  1828. $sql_titel = "SELECT * FROM $tbl_posts_text WHERE post_id=".$myrow["post_id"];
  1829. $result_titel = Database::query($sql_titel, __FILE__, __LINE__);
  1830. $myrow_titel = Database::fetch_array($result_titel);
  1831. $posternom = $myrow['nom'];
  1832. $posterprenom = $myrow['prenom'];
  1833. $posttime = $myrow['post_time'];
  1834. $posttext = $myrow_titel['post_text'];
  1835. $posttitle = $myrow_titel['post_title'];
  1836. $posttext = str_replace('"', "'", $posttext);
  1837. //2 Export contents as an HTML table
  1838. $expcontent .= "<table border='0' cellpadding='3' cellspacing='1' width='100%'>
  1839. <tr>
  1840. <td colspan='2' bgcolor='#e6e6e6'><b>$posttitle</b><br />$posttext</td>
  1841. </tr>
  1842. <tr>
  1843. <td colspan='2'></td>
  1844. </tr>
  1845. <tr>
  1846. <td bgcolor='#cccccc' align='left'>$lang_author : $posterprenom $posternom</td>
  1847. <td align='right' bgcolor='#cccccc'>$lang_date : $posttime</td>
  1848. </tr>
  1849. <tr><td colspan='2' height='10'></td></tr>
  1850. </table>";
  1851. break;
  1852. //------------------------NOT IMPLEMENTED ITEMS BEGIN-------------------
  1853. case "Forum" :
  1854. case "Thread" :
  1855. case "Dropbox" :
  1856. case "Assignments" :
  1857. case "Groups" :
  1858. case "Users" :
  1859. //1 Instead of building something, put an info message
  1860. $langItemMissing1 = "There was a ";
  1861. $langItemMissing2 = "page (step) here in the original Dokeos Learning Path.";
  1862. $expcontent .= "<div class='message'>$langItemMissing1 $item_type $langItemMissing2</div>";
  1863. break;
  1864. //------------------------Link BEGIN-------------------------------------
  1865. case "Link _self" :
  1866. case "Link _blank" :
  1867. //1 Get the link data from the database
  1868. $TABLETOOLLINK = Database :: get_course_link_table();
  1869. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE id=$item_id", __FILE__, __LINE__);
  1870. $myrow = Database::fetch_array($result);
  1871. $thelink = $myrow["url"];
  1872. //2 Check the link type (open in blank page or in current page)
  1873. if ($item_type == "Link _blank")
  1874. {
  1875. $target = "_blank";
  1876. }
  1877. //3 Write the link to the export string
  1878. $expcontent .= "<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='".$target."'>$LPname</a>";
  1879. //4 Change the element type for later changes (this is lost, however, so useless here)
  1880. $item_type = "Link"; //to put this to the filename
  1881. //$LPname="<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target=".$target.">$LPname</a>";
  1882. //i am still not sure about Link export : to export them as files or they can appear in the TOC at once ?
  1883. //to enable the second possibility, unrem the row $LPname=...
  1884. break;
  1885. }
  1886. //now we add the Done button and the initialize function : loadpage()
  1887. //not in the case of Documents, HotP
  1888. if ($item_type != 'Exercise' and ($add_scorm_communications === true))
  1889. {
  1890. $expcontent .= $donebutton;
  1891. }
  1892. //End the export string with valid HTML tags
  1893. $expcontent .= "</body></html>";
  1894. //Prepare new file name
  1895. $filename = $item_type.$id.".htm";
  1896. //Write the export content to the new file
  1897. exporttofile('data/'.$filename, $LPname, $id, $expcontent);
  1898. }
  1899. /**
  1900. * This function exports the given item's description into a separate file
  1901. * @param integer Item id
  1902. * @param string Item type
  1903. * @param string Description
  1904. * @return void
  1905. */
  1906. function exportdescription($id, $item_type, $description)
  1907. {
  1908. global $expdir;
  1909. $filename = $item_type.$id.".desc";
  1910. $expcontent = $description;
  1911. exporttofile($expdir.$filename, 'description_of_'.$item_type.$id, 'description_of_item_'.$id, $expcontent);
  1912. }
  1913. /**
  1914. * This function deletes an entire directory
  1915. * @param string The directory path
  1916. * @return boolean True on success, false on failure
  1917. */
  1918. function deldir($dir)
  1919. {
  1920. $dh = opendir($dir);
  1921. while ($file = readdir($dh))
  1922. {
  1923. if ($file != "." && $file != "..")
  1924. {
  1925. $fullpath = $dir."/".$file;
  1926. if (!is_dir($fullpath))
  1927. {
  1928. unlink($fullpath);
  1929. }
  1930. else
  1931. {
  1932. deldir($fullpath);
  1933. }
  1934. }
  1935. }
  1936. closedir($dh);
  1937. if (rmdir($dir))
  1938. {
  1939. return true;
  1940. }
  1941. else
  1942. {
  1943. return false;
  1944. }
  1945. }
  1946. /**
  1947. * This functions exports the given path. This is the opener function, which is called first
  1948. * @param integer The path id
  1949. * @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 :-)
  1950. */
  1951. function exportpath($learnpath_id)
  1952. {
  1953. //1 Initialise variables
  1954. global $_course, $circle1_files, $LPnamesafe, $LPname, $expdir;
  1955. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  1956. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  1957. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  1958. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  1959. //where applicable, add a scorm "Done" button at the end of all contents
  1960. $add_scorm_button = true;
  1961. //2 Get the name of the LP
  1962. include_once (api_get_path(LIBRARY_PATH)."fileUpload.lib.php");
  1963. $sql = "SELECT * FROM $tbl_learnpath_main WHERE (lp_id=$learnpath_id)";
  1964. $result = Database::query($sql, __FILE__, __LINE__);
  1965. $row = Database::fetch_array($result);
  1966. $LPname = $row['learnpath_name'];
  1967. $LPnamesafe = replace_dangerous_char($LPname, 'strict');
  1968. //3 Get a temporary dir for creating the zip file
  1969. $expdir = api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe;
  1970. $fromdir = '../scorm/export/'; //this dir contains some standard files
  1971. deldir($expdir); //make sure the temp dir is cleared
  1972. mkdir($expdir);
  1973. mkdir($expdir.'/css');
  1974. mkdir($expdir.'/data');
  1975. mkdir($expdir.'/js');
  1976. mkdir($expdir.'/data/images');
  1977. mkdir($expdir.'/data/audio');
  1978. mkdir($expdir.'/data/videos');
  1979. $circle1 = array (//this array contains the types of elements we want to export
  1980. 'Chapter', 'Agenda', 'Ad_Valvas', 'Course_description', 'Document', 'Introduction_text', 'Link _self', 'Link _blank', 'Forum', 'Thread', 'Post', 'Exercise', 'HotPotatoes', 'Assignments', 'Dropbox', 'Users', 'Groups');
  1981. //$circle2=array('');
  1982. //4 Get the first level chapters - YW added parent_item_id condition for multi-level paths
  1983. $sql = "SELECT * FROM $tbl_learnpath_chapter
  1984. WHERE (lp_id=$learnpath_id and parent_item_id=0)
  1985. ORDER BY display_order ASC";
  1986. //to get all the elements, we should use the function that builds the table of content get_learnpath_tree
  1987. //WHERE (lp_id=$learnpath_id)
  1988. //ORDER BY parent_item_id, display_order ASC";
  1989. $result = Database::query($sql, __FILE__, __LINE__);
  1990. //5 export the items listed in Circle I one by one
  1991. while ($row = Database::fetch_array($result))
  1992. {
  1993. //5.1 Get items data from the database for this chapter
  1994. $parent_item_id = $row['id'];
  1995. //$sql2a="SELECT * FROM $tbl_learnpath_chapter WHERE (lp_id=$learnpath_id and parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  1996. //$result2a=Database::query($sql,__FILE__,__LINE__);
  1997. $sql2b = "SELECT * FROM $tbl_learnpath_item WHERE (parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  1998. $result2b = Database::query($sql2b, __FILE__, __LINE__);
  1999. while ($row2 = Database::fetch_array($result2b))
  2000. {
  2001. //5.1.1 Check if the element is in the circle1 array
  2002. $tobeexported = false;
  2003. for ($i = 0; $i < count($circle1) && !$tobeexported; $i ++)
  2004. {
  2005. //if the type is found in the circle1 array, ask for export
  2006. if ($circle1[$i] == $row2['item_type'])
  2007. {
  2008. $tobeexported = true;
  2009. }
  2010. }
  2011. //5.1.2 If applicable, export the item to an HTML file (see exportitem function for more details)
  2012. if ($tobeexported)
  2013. {
  2014. exportitem($row2['id'], $row2['item_id'], $row2['item_type'], $add_scorm_button);
  2015. /*if ($row2['description']) { //put the description of items to a separate file (.desc)
  2016. exportdescription($row2['id'],$row2['item_type'],$row2['description']);
  2017. }*/
  2018. }
  2019. } //end of items loop
  2020. } //end of first-level chapters loop
  2021. //6 export the other necceassary files
  2022. $filename = 'default.css';
  2023. copy('../css/'.$filename, $expdir.'/css/'.$filename);
  2024. $filename = 'ims_xml.xsd';
  2025. copy($fromdir.$filename, $expdir.'/'.$filename);
  2026. $filename = 'imscp_v1p1.xsd';
  2027. copy($fromdir.$filename, $expdir.'/'.$filename);
  2028. $filename = 'imsmd_v1p2.xsd';
  2029. copy($fromdir.$filename, $expdir.'/'.$filename);
  2030. $filename = 'APIWrapper.js';
  2031. copy($fromdir.$filename, $expdir.'/js/'.$filename);
  2032. $filename = 'SCOFunctions.js';
  2033. copy($fromdir.$filename, $expdir.'/js/'.$filename);
  2034. //in case circle1_files is not defined, build it
  2035. //$circle1_files
  2036. //7 create imsmanifest.xml
  2037. createimsmanifest($circle1_files, $learnpath_id);
  2038. //8 put the files in the exportdir into a zip and force download
  2039. include_once (api_get_path(LIBRARY_PATH)."pclzip/pclzip.lib.php");
  2040. //create zipfile of given directory
  2041. $zip_folder = new PclZip(api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe.".zip");
  2042. $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/");
  2043. //api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe); // whitout folder
  2044. // modified by imandak80
  2045. /* copy(api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe.".zip",
  2046. api_get_path('SYS_COURSE_PATH').$_course['path']."/document/".$LPnamesafe.".zip");
  2047. */
  2048. $zipfoldername = api_get_path('SYS_COURSE_PATH').$_course['path']."/temp/".$LPnamesafe;
  2049. $zipfilename = $zipfoldername.".zip";
  2050. DocumentManager :: file_send_for_download($zipfilename, false, basename($LPnamesafe.".zip"));
  2051. //9 Delete the temporary zip file and directory
  2052. include_once (api_get_path(LIBRARY_PATH)."fileManage.lib.php");
  2053. // in fileManage.lib.php
  2054. my_delete($zipfilename);
  2055. my_delete($zipfoldername);
  2056. //exit;
  2057. //0 Return the circle_files hash (array)
  2058. return ($circle1_files); //has been used before...
  2059. }
  2060. /**
  2061. * Export SCORM content into a zip file
  2062. *
  2063. * Basically, all this function does is put the scorm directory back into a zip file (like the one
  2064. * that was most probably used to import the course at first)
  2065. * @param string Name of the SCORM path (or the directory under which it resides)
  2066. * @param array Not used right now. Should replace the use of global $_course
  2067. * @return void
  2068. * @author imandak80
  2069. */
  2070. function exportSCORM($scormname, $course)
  2071. {
  2072. global $_course;
  2073. //initialize
  2074. $tmpname = api_get_path('SYS_COURSE_PATH').$_course['path']."/scorm";
  2075. $zipfoldername = $tmpname.$scormname;
  2076. $zipfilename = $zipfoldername.".zip";
  2077. //create zipfile of given directory
  2078. include_once (api_get_path(LIBRARY_PATH)."pclzip/pclzip.lib.php");
  2079. $zip_folder = new PclZip($zipfilename);
  2080. $list = 1;
  2081. //$list = $zip_folder->create($zipfoldername."/",PCLZIP_OPT_REMOVE_PATH,$tmpname.$scormname."/"); // whitout folder
  2082. $list = $zip_folder->create($zipfoldername."/", PCLZIP_OPT_REMOVE_PATH, $tmpname);
  2083. if ($list == 0)
  2084. {
  2085. // echo "Error : ".$zip_folder->errorInfo(true);
  2086. }
  2087. //send to client
  2088. DocumentManager :: file_send_for_download($zipfilename, false, basename($scormname.".zip"));
  2089. //clear
  2090. include_once (api_get_path(LIBRARY_PATH)."fileManage.lib.php");
  2091. my_delete($zipfilename);
  2092. }
  2093. /**
  2094. * This function returns an xml tag
  2095. * $data behaves as the content in case of full tags
  2096. * $data is an array of attributes in case of returning an opening tag
  2097. * @param string
  2098. * @param string
  2099. * @param array
  2100. * @param string
  2101. * @return string
  2102. */
  2103. function xmltagwrite($tagname, $which, $data, $linebreak = "yes")
  2104. {
  2105. switch ($which)
  2106. {
  2107. case "open" :
  2108. $tag = "<".$tagname;
  2109. $i = 0;
  2110. while ($data[0][$i])
  2111. {
  2112. $tag .= " ".$data[0][$i]."=\"".$data[1][$i]."\"";
  2113. $i ++;
  2114. }
  2115. if ($tagname == 'file')
  2116. {
  2117. $closing = '/';
  2118. }
  2119. $tag .= $closing.">";
  2120. if ($linebreak != 'no_linebreak')
  2121. {
  2122. $tag .= "\n";
  2123. }
  2124. break;
  2125. case "close" :
  2126. $tag = "</".$tagname.">";
  2127. if ($linebreak != 'no_linebreak')
  2128. {
  2129. $tag .= "\n";
  2130. }
  2131. break;
  2132. case "full" :
  2133. $tag = "<".$tagname;
  2134. $tag .= ">".$data."</".$tagname.">";
  2135. if ($linebreak != 'no_linebreak')
  2136. {
  2137. $tag .= "\n";
  2138. }
  2139. break;
  2140. }
  2141. return $tag;
  2142. }
  2143. /**
  2144. * This function writes the imsmanifest.xml and exports the chapter names
  2145. * @param array Array containing filenames
  2146. * @param integer Learnpath_id
  2147. * @return void
  2148. */
  2149. function createimsmanifest($circle1_files, $learnpath_id)
  2150. {
  2151. global $charset;
  2152. global $_course, $LPname, $expdir, $LPnamesafe;
  2153. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  2154. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  2155. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  2156. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  2157. include_once ('../metadata/md_funcs.php'); // RH: export metadata
  2158. //1.1 header
  2159. /*
  2160. $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";
  2161. */
  2162. //1.2
  2163. //charset should be dependent on content
  2164. //$mycharset = 'ISO-8859-1';
  2165. $mycharset = $charset;
  2166. $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";
  2167. $org .= xmltagwrite('metadata', 'open');
  2168. $org .= " ".xmltagwrite('schema', 'full', 'ADL SCORM');
  2169. $org .= " ".xmltagwrite('schemaversion', 'full', '1.2');
  2170. $org .= xmltagwrite('metadata', 'close');
  2171. $defaultorgname = 'default_org';
  2172. $attributes[0][0] = 'default';
  2173. $attributes[1][0] = $defaultorgname;
  2174. $org .= xmltagwrite('organizations', 'open', $attributes);
  2175. $attributes[0][0] = 'identifier';
  2176. $attributes[1][0] = $defaultorgname;
  2177. $org .= " ".xmltagwrite('organization', 'open', $attributes);
  2178. $org .= " ".xmltagwrite('title', 'full', $LPname);
  2179. //items list
  2180. $i = 0;
  2181. $previous_item_id = '00';
  2182. while ($circle1_files[0][$i])
  2183. {
  2184. //check whether we are in the border of two chapters
  2185. //if (!$desc=strpos($circle1_files[2][$i],'scription')) { //this is is needed if the descriptions are exported to file
  2186. $sql = "SELECT * FROM $tbl_learnpath_item WHERE (id=".$circle1_files[2][$i].")";
  2187. $result = Database::query($sql, __FILE__, __LINE__);
  2188. $row = Database::fetch_array($result);
  2189. $parent_item_id = $row['parent_item_id'];
  2190. if ($parent_item_id != $previous_item_id)
  2191. {
  2192. //we create the item tag for the chapter (without indifierref)
  2193. $sql2 = "SELECT * FROM $tbl_learnpath_chapter WHERE (id=".$parent_item_id.")";
  2194. $result2 = Database::query($sql2, __FILE__, __LINE__);
  2195. $row2 = Database::fetch_array($result2);
  2196. $chapter_name = $row2['chapter_name'];
  2197. $attributes = '';
  2198. $attributes[0][] = 'identifier';
  2199. $attributes[1][] = 'chapter_'.$row2['id'];
  2200. $attributes[0][] = 'isvisible';
  2201. $attributes[1][] = '1';
  2202. if ($previous_item_id != '00')
  2203. {
  2204. $org .= " ".xmltagwrite('item', 'close');
  2205. }
  2206. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2207. $org .= " ".xmltagwrite('title', 'full', $chapter_name);
  2208. if ($row2['chapter_description'] != '')
  2209. {
  2210. //chapter description
  2211. $attributes = '';
  2212. $attributes[0][] = 'identifier';
  2213. $attributes[1][] = 'chapter_'.$row2['id'].'_desc';
  2214. $attributes[0][] = 'isvisible';
  2215. $attributes[1][] = '1';
  2216. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2217. $org .= " ".xmltagwrite('title', 'full', ' '.$row2['chapter_description']);
  2218. $org .= " ".xmltagwrite('item', 'close');
  2219. }
  2220. }
  2221. $previous_item_id = $parent_item_id;
  2222. //}
  2223. $attributes = ''; //item output
  2224. $attributes[0][] = 'identifier';
  2225. $attributes[1][] = 'item_'.$circle1_files[2][$i];
  2226. $attributes[0][] = 'identifierref';
  2227. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  2228. $attributes[0][] = 'isvisible';
  2229. $attributes[1][] = '1';
  2230. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2231. $org .= " ".xmltagwrite('title', 'full', $circle1_files[1][$i]);
  2232. if ($row['prereq_id'] != '')
  2233. { //item prerequisites
  2234. $attributes = '';
  2235. $attributes[0][] = 'type';
  2236. $attributes[1][] = 'aicc_script';
  2237. $org .= " ".xmltagwrite('adlcp:prerequisites', 'open', $attributes, "no_linebreak");
  2238. if ($row['prereq_type'] == 'i')
  2239. {
  2240. $org .= 'item_'.$row['prereq_id'];
  2241. }
  2242. if ($row['prereq_type'] == 'c')
  2243. {
  2244. $org .= 'chapter_'.$row['prereq_id'];
  2245. }
  2246. $org .= xmltagwrite('adlcp:prerequisites', 'close', $attributes);
  2247. }
  2248. if ($row['description'] != '')
  2249. {
  2250. //item description
  2251. $attributes = '';
  2252. $attributes[0][] = 'identifier';
  2253. $attributes[1][] = 'item_'.$circle1_files[2][$i].'_desc';
  2254. $attributes[0][] = 'isvisible';
  2255. $attributes[1][] = '1';
  2256. $org .= " ".xmltagwrite('item', 'open', $attributes);
  2257. $org .= " ".xmltagwrite('title', 'full', ' '.$row['description']);
  2258. $org .= " ".xmltagwrite('item', 'close');
  2259. }
  2260. $mds = new mdstore(TRUE); // RH: export metadata; if no table, create it
  2261. if (($mdt = $mds->mds_get($row['item_type'].'.'.$row['item_id'])))
  2262. if (($mdo = api_strpos($mdt, '<metadata>')) && ($mdc = api_strpos($mdt, '</metadata>')))
  2263. $org .= " ".api_substr($mdt, $mdo, $mdc - $mdo +11)."\n";
  2264. $org .= " ".xmltagwrite('item', 'close');
  2265. $i ++;
  2266. }
  2267. if ($circle1_files)
  2268. {
  2269. $org .= " ".xmltagwrite('item', 'close');
  2270. } //not needed in case of a blank path
  2271. $org .= " ".xmltagwrite('organization', 'close');
  2272. $org .= xmltagwrite('organizations', 'close');
  2273. $org .= xmltagwrite('resources', 'open');
  2274. //resources list
  2275. $i = 0;
  2276. while ($circle1_files[0][$i])
  2277. {
  2278. $attributes = '';
  2279. $attributes[0][] = 'identifier';
  2280. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  2281. $attributes[0][] = 'type';
  2282. $attributes[1][] = 'webcontent';
  2283. $attributes[0][] = 'adlcp:scormtype';
  2284. $attributes[1][] = 'sco';
  2285. $attributes[0][] = 'href';
  2286. $attributes[1][] = $circle1_files[0][$i];
  2287. $org .= " ".xmltagwrite('resource', 'open', $attributes);
  2288. $org .= " ".xmltagwrite('metadata', 'open');
  2289. $org .= " ".xmltagwrite('schema', 'full', 'ADL SCORM');
  2290. $org .= " ".xmltagwrite('schemaversion', 'full', '1.2');
  2291. $org .= " ".xmltagwrite('metadata', 'close');
  2292. $attributes = '';
  2293. $attributes[0][] = 'href';
  2294. $attributes[1][] = $circle1_files[0][$i];
  2295. $org .= " ".xmltagwrite('file', 'open', $attributes);
  2296. $org .= " ".xmltagwrite('resource', 'close');
  2297. $i ++;
  2298. }
  2299. $org .= xmltagwrite('resources', 'close');
  2300. $org .= xmltagwrite('manifest', 'close');
  2301. $manifest = $header.$org;
  2302. exporttofile('imsmanifest.xml', 'Manifest file', '0', $manifest);
  2303. }
  2304. /**
  2305. * Gets the tags of the file given as parameter
  2306. *
  2307. * if $filename is not found, GetSRCTags(filename) will return FALSE
  2308. * @param string file path
  2309. * @return mixed array of strings on success, false on failure
  2310. * @author unknown
  2311. * @author included by imandak80
  2312. */
  2313. function GetSRCTags($fileName)
  2314. {
  2315. if (!($fp = fopen($fileName, "r")))
  2316. {
  2317. //if file can't be opened, return false
  2318. return false;
  2319. }
  2320. //read file contents
  2321. $contents = fread($fp, filesize($fileName));
  2322. fclose($fp);
  2323. $matches = array ();
  2324. $srcList = array ();
  2325. //get all src tags contents in this file. Use multi-line search.
  2326. preg_match_all('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/mi', $contents, $matches); //get the img src as contained between " or '
  2327. foreach ($matches[3] as $match)
  2328. {
  2329. if (!in_array($match, $srcList))
  2330. {
  2331. $srcList[] = $match;
  2332. }
  2333. }
  2334. if (count($srcList) == 0)
  2335. {
  2336. return false;
  2337. }
  2338. return $srcList;
  2339. }
  2340. /**
  2341. * Copy file and create directories in the path if needed.
  2342. *
  2343. * @param string $source Source path
  2344. * @param string $dest Destination path
  2345. * @return boolean true on success, false on failure
  2346. */
  2347. function CopyNCreate($source, $dest)
  2348. {
  2349. if (strcmp($source, $dest) == 0)
  2350. return false;
  2351. $dir = "";
  2352. $tdest = explode('/', $dest);
  2353. for ($i = 0; $i < sizeof($tdest) - 1; $i ++)
  2354. {
  2355. $dir = $dir.$tdest[$i]."/";
  2356. if (!is_dir($dir))
  2357. if (!mkdir($dir))
  2358. return false;
  2359. }
  2360. if (!copy($source, $dest))
  2361. return false;
  2362. return true;
  2363. }
  2364. function rcopy($source, $dest)
  2365. {
  2366. //error_log($source." -> ".$dest,0);
  2367. if (!file_exists($source))
  2368. {
  2369. //error_log($source." does not exist",0);
  2370. return false;
  2371. }
  2372. if (is_dir($source))
  2373. {
  2374. //error_log($source." is a dir",0);
  2375. //this is a directory
  2376. //remove trailing '/'
  2377. if (strrpos($source, '/') == sizeof($source) - 1)
  2378. {
  2379. $source = substr($source, 0, size_of($source) - 1);
  2380. }
  2381. if (strrpos($dest, '/') == sizeof($dest) - 1)
  2382. {
  2383. $dest = substr($dest, 0, size_of($dest) - 1);
  2384. }
  2385. if (!is_dir($dest))
  2386. {
  2387. $res = @ mkdir($dest);
  2388. if ($res === true)
  2389. {
  2390. return true;
  2391. }
  2392. else
  2393. {
  2394. //remove latest part of path and try creating that
  2395. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/'))))
  2396. {
  2397. return @ mkdir($dest);
  2398. }
  2399. else
  2400. {
  2401. return false;
  2402. }
  2403. }
  2404. }
  2405. return true;
  2406. }
  2407. else
  2408. {
  2409. //this is presumably a file
  2410. //error_log($source." is a file",0);
  2411. if (!@ copy($source, $dest))
  2412. {
  2413. //error_log("Could not simple-copy $source",0);
  2414. $res = rcopy(dirname($source), dirname($dest));
  2415. if ($res === true)
  2416. {
  2417. //error_log("Welcome dir created",0);
  2418. return @ copy($source, $dest);
  2419. }
  2420. else
  2421. {
  2422. return false;
  2423. //error_log("Error creating path",0);
  2424. }
  2425. }
  2426. else
  2427. {
  2428. //error_log("Could well simple-copy $source",0);
  2429. return true;
  2430. }
  2431. }
  2432. }
  2433. ?>