learnpath_functions.inc.php 110 KB

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