learnpath_functions.inc.php 102 KB

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