learnpath_functions.inc.php 103 KB

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