learnpath_functions.inc.php 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  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/testcategory.class.php';
  1140. require_once '../exercice/exercise.class.php';
  1141. require_once '../exercice/question.class.php';
  1142. require_once '../exercice/answer.class.php';
  1143. require_once '../exercice/exercise.lib.php';
  1144. $TBL_EXERCISES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1145. /* Clears the exercise session */
  1146. if (isset ($_SESSION['objExercise'])) {
  1147. Session::erase('objExercise');
  1148. }
  1149. if (isset ($_SESSION['objQuestion'])) {
  1150. Session::erase('objQuestion');
  1151. }
  1152. if (isset ($_SESSION['objAnswer'])) {
  1153. Session::erase('objAnswer');
  1154. }
  1155. if (isset ($_SESSION['questionList'])) {
  1156. Session::erase('questionList');
  1157. }
  1158. if (isset ($_SESSION['exerciseResult'])) {
  1159. Session::erase('exerciseResult');
  1160. }
  1161. // If the object is not in the session:
  1162. if (!isset ($_SESSION['objExercise'])) {
  1163. // Construction of Exercise.
  1164. $objExercise = new Exercise();
  1165. $sql = "SELECT title,description,sound,type,random,active FROM $TBL_EXERCISES WHERE id='$exerciseId'";
  1166. // If the specified exercise doesn't exist or is disabled:
  1167. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !api_is_allowed_to_edit(
  1168. ) && ($origin != 'learnpath'))
  1169. ) {
  1170. die($langExerciseNotFound);
  1171. }
  1172. // Saves the object into the session.
  1173. Session::write('objExercise', $objExercise);
  1174. }
  1175. $exerciseTitle = $objExercise->selectTitle();
  1176. $exerciseDescription = $objExercise->selectDescription();
  1177. $exerciseSound = $objExercise->selectSound();
  1178. $randomQuestions = $objExercise->isRandom();
  1179. $exerciseType = $objExercise->selectType();
  1180. if (!isset ($_SESSION['questionList'])) {
  1181. // Selects the list of question ID.
  1182. $questionList = $randomQuestions ? $objExercise->selectRandomList() : $objExercise->selectQuestionList();
  1183. // Saves the question list into the session.
  1184. Session::write('questionList', $questionList);
  1185. }
  1186. $nbrQuestions = sizeof($questionList);
  1187. // If questionNum comes from POST and not from GET:
  1188. if (!$questionNum || $_POST['questionNum']) {
  1189. // Only used for sequential exercises (see $exerciseType).
  1190. if (!$questionNum) {
  1191. $questionNum = 1;
  1192. } else {
  1193. $questionNum++;
  1194. }
  1195. }
  1196. $test .= "<h3>".$exerciseTitle."</h3>";
  1197. if (!empty ($exerciseSound)) {
  1198. $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(
  1199. "Sound"
  1200. )."\" /></a>";
  1201. }
  1202. // Writing the .js file with to check the correct answers begin.
  1203. $scriptfilename = "Exercice".$item_id.".js";
  1204. $s = "<script type=\"text/javascript\" src='../js/".$scriptfilename."'></script>";
  1205. $test .= $s;
  1206. $content = "function evaluate() {
  1207. alert('Test evaluated.');
  1208. }
  1209. ";
  1210. if (!$handle = fopen($expdir.'/js/'.$scriptfilename, 'w')) {
  1211. echo "Cannot open file ($scriptfilename)";
  1212. }
  1213. if (fwrite($handle, $content) === false) {
  1214. echo "Cannot write to file ($filename)";
  1215. exit;
  1216. }
  1217. fclose($handle);
  1218. // Writing the .js file with to check the correct answers end.
  1219. $s = "
  1220. <p>$exerciseDescription</p>
  1221. <table width='100%' border='0' cellpadding='1' cellspacing='0'>
  1222. <form method='post' action=''><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1223. <input type='hidden' name='formSent' value='1' />
  1224. <input type='hidden' name='exerciseType' value='".$exerciseType."' />
  1225. <input type='hidden' name='questionNum' value='".$questionNum."' />
  1226. <input type='hidden' name='nbrQuestions' value='".$nbrQuestions."' />
  1227. <tr>
  1228. <td>
  1229. <table width='100%' cellpadding='4' cellspacing='2' border='0'>";
  1230. $exerciseType = 1; // So to list all questions in one page.
  1231. $test .= $s;
  1232. $i = 0;
  1233. foreach ($questionList as $questionId) {
  1234. $i++;
  1235. // For sequential exercises.
  1236. if ($exerciseType == 2) {
  1237. // If it is not the right question, goes to the next loop iteration.
  1238. if ($questionNum != $i) {
  1239. continue;
  1240. } else {
  1241. // if the user has already answered this question:
  1242. if (isset ($exerciseResult[$questionId])) {
  1243. // Construction of the Question object.
  1244. $objQuestionTmp = new Question();
  1245. // Reads question informations.
  1246. $objQuestionTmp->read($questionId);
  1247. $questionName = $objQuestionTmp->selectTitle();
  1248. // Destruction of the Question object.
  1249. unset ($objQuestionTmp);
  1250. $test .= '<tr><td>'.get_lang('AlreadyAnswered').' &quot;'.$questionName.'&quot;</td></tr>';
  1251. break;
  1252. }
  1253. }
  1254. }
  1255. echo $s = "<tr bgcolor='#e6e6e6'><td valign='top' colspan='2'>".get_lang('Question')." ";
  1256. // Call the showQuestion() function from exercise.lib.php. This basically displays the question in a table.
  1257. $test .= showQuestion($questionId, false, 'export', $i);
  1258. } // end foreach()
  1259. $s = "</table></td></tr><tr><td><br/><input type='button' value='".$langOk."' onclick=\"javascript: evaluate(); alert('Evaluated.');\">";
  1260. $s .= "</td></tr></form></table>";
  1261. $s .= "<script type='text/javascript'> loadPage(); </script>";
  1262. $b = 2;
  1263. $test .= $s;
  1264. return ($test);
  1265. }
  1266. /**
  1267. * This function exports the given item
  1268. * @param integer Id from learnpath_items table
  1269. * @param integer Item id
  1270. * @param string Itm type
  1271. * @param boolean Shall the SCORM communications features be added? (true). Default: false.
  1272. * @return void (outputs a zip file)
  1273. * @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
  1274. */
  1275. function exportitem($id, $item_id, $item_type, $add_scorm_communications = false)
  1276. {
  1277. $course_id = api_get_course_int_id();
  1278. global $circle1_files, $expdir, $_course, $_SESSION, $GLOBALS;
  1279. 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;
  1280. $libp = api_get_path(SYS_CODE_PAH);
  1281. include_once $libp.'exercice/exercise.class.php';
  1282. include_once $libp.'question.class.php';
  1283. include_once $libp.'answer.class.php';
  1284. include_once $libp.'exercise.lib.php';
  1285. $langLasting = ''; //avoid code parser warning
  1286. include_once $libp.'lang/english/announcements.inc.php'; //this line is here only for $langPubl in announcements
  1287. include_once $libp.'lang/'.$language_interface.'/announcements.inc.php'; //this line is here only for $langPubl in announcements
  1288. include_once $libp.'lang/english/agenda.inc.php'; //this line is here only for $langLasting
  1289. include_once $libp.'lang/'.$language_interface.'/agenda.inc.php'; //this line is here only for $langLasting
  1290. include_once $libp.'lang/english/course_description.inc.php'; //this line is here only for $langThisCourseDescriptionIsEmpty
  1291. include_once $libp.'lang/'.$language_interface.'/course_description.inc.php'; // -||-
  1292. include_once $libp.'lang/english/resourcelinker.inc.php';
  1293. include_once $libp.'lang/'.$language_interface.'/resourcelinker.inc.php';
  1294. include_once $libp.'lang/english/learnpath.inc.php';
  1295. include_once $libp.'lang/'.$language_interface.'/learnpath.inc.php';
  1296. include_once $libp.'lang/english/exercice.inc.php';
  1297. include_once $libp.'lang/'.$language_interface.'/exercice.inc.php';
  1298. include_once '../resourcelinker/resourcelinker.inc.php';
  1299. $LPname = display_addedresource_link_in_learnpath($item_type, $item_id, '', $id, 'builder', 'nolink');
  1300. $expcontent = "<!--
  1301. This is an exported file from Chamilo Learning Path belonging to a Scorm compliant content package.
  1302. Do not modify or replace individually.
  1303. Export module author : Denes Nagy <darkden@evk.bke.hu>
  1304. -->
  1305. ";
  1306. // Files needed for communicating with the scos.
  1307. $scocomfiles = "<script type='text/javascript' src='../js/APIWrapper.js'></script>"."<script type='text/javascript' src='../js/SCOFunctions.js'></script>";
  1308. $expcontent .= '<html><head><link rel="stylesheet" href="../css/default.css" type="text/css" media="screen,projection" />'.$scocomfiles.'</head><body>';
  1309. $donebutton .= "<script type='text/javascript'>
  1310. /* <![CDATA[ */
  1311. loadPage();
  1312. var studentName = '!';
  1313. var lmsStudentName = doLMSGetValue( 'cmi.core.student_name' );
  1314. if ( lmsStudentName != '' )
  1315. {
  1316. studentName = ' ' + lmsStudentName + '!';
  1317. }
  1318. /* ]]> */
  1319. </script>
  1320. <br /><br />
  1321. <form><input type=\"hidden\" name=\"SQMSESSID\" value=\"36812c2dea7d8d6e708d5e6a2f09b0b9\" />
  1322. <table cols='3' width='100%' align='center'>
  1323. <tr>
  1324. <td align='middle'><input type = 'button' value = ' ".$langDone." ' onclick = \"javascript: doQuit('completed');\" id='button2' name='button2'></td>
  1325. </tr>
  1326. </table>
  1327. </form>";
  1328. /**
  1329. * Switch between the different element types, namely:
  1330. * - Agenda
  1331. * - Ad_Valvas
  1332. * - Course_description
  1333. * - Document
  1334. * - Introduction_text
  1335. * - HotPotatoes
  1336. * - Exercise
  1337. * - Post
  1338. * - Forum ]
  1339. * - Thread ]
  1340. * - Dropbox ]
  1341. * - Assignments ] These elements are all replaced by a simple message in the exported document.
  1342. * - Groups ]
  1343. * - Users ]
  1344. * - Link _self
  1345. * - Link _blank
  1346. */
  1347. switch ($item_type) {
  1348. // AGENDA BEGIN
  1349. case 'Agenda':
  1350. // 1. Get agenda event data from the database table.
  1351. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  1352. $sql = "SELECT * FROM ".$TABLEAGENDA." where c_id = $course_id AND (id=$item_id)";
  1353. $result = Database::query($sql);
  1354. // 2. Prepare table output.
  1355. $expcontent .= "<table class=\"data_table\" >";
  1356. $barreMois = '';
  1357. // 3. For each event corresponding to this agenda, do the following:
  1358. while ($myrow = Database::fetch_array($result)) {
  1359. $start_date_local = api_get_local_time($myrow['start_date'], null, date_default_timezone_get());
  1360. //3.1 Make the blue month bar appear only once.
  1361. if ($barreMois != api_format_date($start_date_local, "%m")) {
  1362. // 3.1.1 Update the check value for the month bar.
  1363. $barreMois = api_format_date($start_date_local, "%m");
  1364. // 3.1.2 Display the month bar.
  1365. $expcontent .= "<tr><td id=\"title\" colspan=\"2\" class=\"month\" valign=\"top\">".api_format_date(
  1366. $start_date_local,
  1367. "%B %Y"
  1368. )."</td></tr>";
  1369. }
  1370. // 3.2 Display the agenda items (of this month): the date, hour and title.
  1371. $db_date = (int)api_format_date($start_date_local, "%d");
  1372. if ($_GET['day'] != $db_date) {
  1373. // 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'.
  1374. $expcontent .= "<tr><td class=\"data\" colspan='2'>";
  1375. } else {
  1376. // 3.2.1.b Else (same day) use style 'datanow'.
  1377. $expcontent .= "<tr><td class=\"datanow\" colspan='2'>";
  1378. }
  1379. // 3.2.2 Mark an anchor for this date.
  1380. $expcontent .= "<a name=\"".$db_date."\"></a>"; // anchoring
  1381. // 3.2.3 Write the date and time of this event to the export string.
  1382. $expcontent .= api_format_date($start_date_local);
  1383. // 3.2.4 If a duration is set, write it, otherwise ignore.
  1384. if ($myrow['duration'] == '') {
  1385. $expcontent .= "<br />";
  1386. } else {
  1387. $expcontent .= " / ".$langLasting." ".$myrow['duration']."<br />"; //langLasting comes from lang/x/agenda.inc.php
  1388. }
  1389. // 3.2.5 Write the title.
  1390. $expcontent .= $myrow['title'];
  1391. $expcontent .= "</td></tr>";
  1392. // 3.2.6 Prepare the content of the agenda item.
  1393. $content = $myrow['content'];
  1394. // 3.2.7 Make clickable???
  1395. $content = Text::make_clickable($content);
  1396. // 3.2.8 Write the prepared content to the export string.
  1397. $expcontent .= "<tr><td class=\"text\" colspan='2'>";
  1398. $expcontent .= $content;
  1399. $expcontent .= "</td></tr>";
  1400. // Displaying the agenda item of this month: the added resources.
  1401. // This part is not included into LP export.
  1402. /*if (check_added_resources("Agenda", $myrow['id'])) {
  1403. $content.= "<tr><td colspan='2'>";
  1404. $content.= "<i>".get_lang('AddedResources')."</i><br />";
  1405. display_added_resources("Agenda", $myrow['id']);
  1406. $content.= "</td></tr>";
  1407. }*/
  1408. }
  1409. // 4. Finish the export string.
  1410. $expcontent .= "<tr></table>";
  1411. break;
  1412. // ANNOUNCEMENT BEGIN
  1413. case 'Ad_Valvas':
  1414. // 1. Get the announcement data from the database
  1415. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1416. $sql = "SELECT * FROM $tbl_announcement WHERE c_id = $course_id AND id='$item_id'";
  1417. $result = Database::query($sql);
  1418. // 2. Initialise export string
  1419. $expcontent .= "<table class=\"data_table\">";
  1420. // 3. For each announcement matching the query
  1421. while ($myrow = Database::fetch_array($result)) {
  1422. // 3.1 Get the __ field data.
  1423. $content = $myrow[1];
  1424. //$content = nl2br($content);
  1425. // 3.2 Prepare the data for export.
  1426. $content = Text::make_clickable($content);
  1427. // 3.3 Get a UNIX(?<-mktime) Timestamp of the end_date for this announcement.
  1428. $last_post_datetime = $myrow['end_date']; // post time format datetime of database layer (MySQL is assumed)
  1429. list ($last_post_date, $last_post_time) = split(' ', $last_post_datetime);
  1430. list ($year, $month, $day) = explode('-', $last_post_date);
  1431. list ($hour, $min) = explode(':', $last_post_time);
  1432. $announceDate = mktime($hour, $min, 0, $month, $day, $year);
  1433. // 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).
  1434. if ($announceDate > $_SESSION['user_last_login_datetime']) {
  1435. $colorBecauseNew = " color=\"red\" ";
  1436. } else {
  1437. $colorBecauseNew = ' ';
  1438. }
  1439. // 3.5 Write this content to the export string (formatted HTML array).
  1440. $expcontent .= "<tr>\n"."<td class=\"cell_header\">\n"."<font ".$colorBecauseNew.">".$langPubl." : ".api_convert_and_format_date(
  1441. $last_post_datetime,
  1442. null,
  1443. date_default_timezone_get()
  1444. )."</font>\n"."</td>\n"."</tr>\n"."<tr>\n"."<td>\n".$content."</td>\n"."</tr>\n";
  1445. } // while loop
  1446. // 4 Finish the export string.
  1447. $expcontent .= "</table>";
  1448. break;
  1449. // Course_description BEGIN
  1450. case 'Course_description':
  1451. // 1. Get course description data from database.
  1452. $tbl_course_description = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  1453. $result = Database::query(
  1454. "SELECT id, title, content FROM ".$tbl_course_description." WHERE c_id = $course_id ORDER BY id"
  1455. );
  1456. // 2. Check this element.
  1457. if (Database::num_rows($result)) {
  1458. // 2.a This course has one (or more) description in the database.
  1459. $expcontent .= "<hr noshade=\"noshade\" size=\"1\" />";
  1460. // 2.a.1 For each description available for this course.
  1461. while ($row = Database::fetch_array($result)) {
  1462. // 2.a.1.1 Write title to export string.
  1463. $expcontent .= "<h4>".$row['title']."</h4>";
  1464. // 2.a.1.2 Prepare content.
  1465. $content = Text::make_clickable(nl2br($row['content']));
  1466. // 2.a.1.3 Write content to the export string.
  1467. $expcontent .= $content;
  1468. }
  1469. } else {
  1470. // 2.b This course has no description available.
  1471. $expcontent .= "<br /><h4>$langThisCourseDescriptionIsEmpty</h4>";
  1472. }
  1473. break;
  1474. // DOCUMENT BEGIN
  1475. case 'Document':
  1476. // 1. Get the document data from the database.
  1477. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1478. $sql_query = "SELECT * FROM $tbl_document WHERE c_id = $course_id AND id=$item_id";
  1479. $sql_result = Database::query($sql_query);
  1480. $myrow = Database::fetch_array($sql_result);
  1481. // 2. Get the origin path of the document to treat it internally.
  1482. $orig = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$myrow['path'];
  1483. // 3. Make some kind of strange transformation to get the destination filepath ???
  1484. $pathname = explode('/', $myrow['path']);
  1485. $last = count($pathname) - 1;
  1486. $filename = 'data/'.$filename.$pathname[$last];
  1487. $copyneeded = true;
  1488. // Html files do not need to be copied as the ok button is inserted into them,
  1489. // so don't copy directly.
  1490. $extension = explode('.', $pathname[$last]);
  1491. // This old condition was WRONG for names like design.html.old. Instead, we now get the extension
  1492. // by using preg_match to match case-insensitive (probably faster than 4 conditions).
  1493. //if (($extension[1]=='htm') or ($extension[1]=='html') or ($extension[1]=='HTM') or ($extension[1]=='HTML')) {
  1494. // 4. Check the file extension.
  1495. if (preg_match('/.*(\.htm(l)?)$/i', $pathname[$last])) {
  1496. // 4.a If this file ends with ".htm(l)", we consider it's an HTML file.
  1497. // src tag check begin
  1498. // We now check if there is any src attribute in htm(l) files, if yes, we have to also export
  1499. // the target file (swf, mp3, video,...) of that src tag.
  1500. // In case of absolute links (http://) this is not neccessary, but makes no error.
  1501. // However still missing : relative links case with subdirs -> the necessary dirs are not created in the exported package.
  1502. // 4.a.1 Get the file contents into $file.
  1503. $file = file_get_contents($orig);
  1504. // 4.a.2 Get all the src links in this file.
  1505. //preg_match_all("|((?i)src=\".*\" )|U",$file,$match);
  1506. $match = GetSRCTags($orig);
  1507. // 4.a.3 For each src tag found, do the following:
  1508. for ($i = 0; $i < count($match); $i++) {
  1509. // 4.a.3.1 Get the tag (split from the key).
  1510. list ($key, $srctag) = each($match);
  1511. $src = $srctag;
  1512. // 4.a.3.2 Check the link kind (web or absolute/relative).
  1513. if (stristr($src, 'http') === false) {
  1514. // 4.a.3.2.a Do something only if relative (otherwise the user will be able to see it too anyway).
  1515. // 4.a.3.2.a.1 Get a proper URL and remove all './'
  1516. $src = urldecode($src); //mp3
  1517. //$src=str_replace('./','',$src);
  1518. $src = preg_replace('/^\.\//', '', $src);
  1519. // 4.a.3.2.a.2 Remove the player link from the URL (only use the mp3 file).
  1520. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1521. // 4.a.3.2.a.3 Remove funny link parts.
  1522. $src = str_replace('?0', '', $src); //mp3
  1523. // The previous lines are used when creating docs with Chamilo Document tool's htmlarea
  1524. // Rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1525. // and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1526. // created by any other software.
  1527. // 4.a.3.2.a.4 Prepare source and destination paths.
  1528. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname(
  1529. $myrow['path']
  1530. ).'/'.$src;
  1531. $dest = $expdir.'/data/'.$src;
  1532. //CopyNCreate($source,$dest);
  1533. rcopy($source, $dest);
  1534. } //else...?
  1535. }
  1536. // src tag check end
  1537. // sco communication insertion begin
  1538. // 4.a.4 If we want to add SCORM actions and a "Done" button, do the following:
  1539. if ($add_scorm_communications === true) {
  1540. if ($bodyclose = strpos($file, '</body>')) {
  1541. $file = substr_replace($file, $scocomfiles.$donebutton, $bodyclose, 7);
  1542. } elseif ($htmlclose = strpos($file, '</html>')) {
  1543. $file = substr_replace($file, $scocomfiles.$donebutton, $htmlclose, 7);
  1544. $file .= '</html>';
  1545. } else {
  1546. $file .= $scocomfiles.$donebutton;
  1547. }
  1548. } //sco communication insertion end
  1549. // 4.a.5 Replace the file's name by adding the element's ID before htm.
  1550. // This will not work with uppercase HTML though. Maybe use the preg_replace syntax proposed...
  1551. $filename = str_replace('.htm', $id.'.htm', $filename);
  1552. //$filename=preg_replace('/.*(\.htm(l)?)$/i',$id.$1,$filename);
  1553. // 4.a.6 Export these contents to a file and set the circle1_files array for later reuse.
  1554. exporttofile($filename, $LPname, $id, $file);
  1555. // The file has been copied, so ask not to copy it again.
  1556. $copyneeded = false;
  1557. } //if (htm(l) files) end
  1558. // 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.
  1559. if ($copyneeded) {
  1560. copy($orig, $expdir.'/'.$filename);
  1561. $circle1_files[0][] = $filename;
  1562. $circle1_files[1][] = $LPname;
  1563. $circle1_files[2][] = $id;
  1564. }
  1565. //echo $orig;
  1566. return;
  1567. // Introduction_text BEGIN
  1568. case 'Introduction_text':
  1569. // 1 Get the introduction text data from the database.
  1570. $TBL_INTRO = Database :: get_course_tool_intro_table();
  1571. // Modified by Ivan Tcholakov, 15-SEP-2008.
  1572. //$result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE id=1");
  1573. $result = Database::query("SELECT * FROM ".$TBL_INTRO." WHERE c_id = $course_id AND id='course_homepage'");
  1574. //
  1575. $myrow = Database::fetch_array($result);
  1576. $intro = $myrow['intro_text'];
  1577. // 2 Write introduction text to the export string.
  1578. $expcontent .= '<br />'.$intro;
  1579. break;
  1580. // HotPotatoes BEGIN
  1581. case 'HotPotatoes':
  1582. // 1. Get HotPotatoes data from the document table.
  1583. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1584. $result = Database::query("SELECT * FROM $tbl_document WHERE c_id = $course_id AND id=$item_id");
  1585. $myrow = Database::fetch_array($result);
  1586. // 2. Get the document path.
  1587. $testfile = api_get_path(SYS_COURSE_PATH).$_course['path']."/document".urldecode($myrow['path']);
  1588. // 3. Get the document contents into a string.
  1589. $content = file_get_contents($testfile);
  1590. // 4. Get the document filename (just the file, no path) - would probably be better to use PHP native function.
  1591. $pathname = explode('/', $myrow['path']);
  1592. $last = count($pathname) - 1;
  1593. $filename = 'data/'.$filename.$pathname[$last];
  1594. // 4beta - get all linked files and copy them (procedure copied from documents type).
  1595. // Get all the src links in this file.
  1596. $match = GetSRCTags($testfile);
  1597. // For each src tag found, do the following:
  1598. foreach ($match as $src) {
  1599. //Check the link kind (web or absolute/relative)
  1600. if (stristr($src, 'http') === false) {
  1601. // Do something only if relative (otherwise the user will be able to see it too anyway).
  1602. // Get a proper URL and remove all './'
  1603. $src = urldecode($src); //mp3
  1604. $src = str_replace('./', '', $src);
  1605. // Remove the player link from the URL (only use the mp3 file).
  1606. $src = str_replace('mp3player.swf?son=', '', $src); //mp3
  1607. // Remove funny link parts.
  1608. $src = str_replace('?0', '', $src); //mp3
  1609. // The previous lines are used when creating docs with Chamilo Document tool's htmlarea.
  1610. // Rows marked by 'mp3' are needed because the mp3 plugin inserts the swf-mp3 links in a very strange way
  1611. // and we can decode them with those 3 lines, hoping this will not cause errors in case of other htmls,
  1612. // created by any other software.
  1613. // Prepare source and destination paths.
  1614. $source = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.dirname(
  1615. $myrow['path']
  1616. ).'/'.$src;
  1617. $dest = $expdir.'/data/'.$src;
  1618. //CopyNCreate($source,$dest);
  1619. rcopy($source, $dest);
  1620. } //else...?
  1621. }
  1622. // 5. Prepare the special "close window" for this test.
  1623. $closewindow = "<html><head><link rel='stylesheet' type='text/css' href='../css/default.css'></head><body>"."<br /><div class='message'>$langHotPotatoesFinished</div></body></html>";
  1624. // Finish is the function of HP to save scores, we insert our scorm function calls to its beginning
  1625. // 'Score' is the variable that tracks the score in HP tests.
  1626. // 6.
  1627. $mit = "function Finish(){";
  1628. $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".
  1629. //the following function are implemented in SCOFunctions.js
  1630. " exitPageStatus = true;\n"." computeTime();\n"." doLMSSetValue( 'cmi.core.score.raw', Score );\n"." doLMSSetValue( 'cmi.core.lesson_status', 'completed' );\n"." doLMSCommit();\n"." doLMSFinish();\n".
  1631. // " document.write('".$closewindow."');\n".
  1632. //if you insert the previous row, the test does not appear correctly !!!!
  1633. " }\n"."}\n"."function Finish(){\n"." mySaveScore();";
  1634. $start = "<script type='text/javascript'> loadPage(); </script>";
  1635. // 7. Replace the current MIT function call by our set of functions. In clear, transform HP to SCORM.
  1636. $content = str_replace($mit, $js_content, $content);
  1637. // 8. Finally, add the API loading calls (although that might have been done first).
  1638. $content = str_replace("</script>", "</script>".$scocomfiles.$start, $content);
  1639. // 9. Change the filename to add the database ID and export to a new file,
  1640. // setting the circle1_files array for later reuse.
  1641. $filename = str_replace('.htm', $id.'.htm', $filename);
  1642. exporttofile($filename, $LPname, $id, $content);
  1643. return;
  1644. // Chamilo test BEGIN
  1645. case 'Exercise':
  1646. //1 Use the export_exercise() function to do the job of constructing the question's HTML table
  1647. $expcontent .= export_exercise($item_id);
  1648. break;
  1649. // POST BEGIN
  1650. case 'Post':
  1651. // 1. Get the forum post data from the database.
  1652. $tbl_posts = Database::get_course_table(TABLE_FORUM_POST);
  1653. $tbl_posts_text = Database::get_course_table(TOOL_FORUM_POST_TEXT_TABLE);
  1654. $result = Database::query("SELECT * FROM $tbl_posts where c_id = $course_id AND post_id=$item_id");
  1655. $myrow = Database::fetch_array($result);
  1656. // Grabbing the title of the post.
  1657. $sql_titel = "SELECT * FROM $tbl_posts_text WHERE c_id = $course_id AND post_id=".$myrow['post_id'];
  1658. $result_titel = Database::query($sql_titel);
  1659. $myrow_titel = Database::fetch_array($result_titel);
  1660. $posternom = $myrow['nom'];
  1661. $posterprenom = $myrow['prenom'];
  1662. $posttime = $myrow['post_time'];
  1663. $posttext = $myrow_titel['post_text'];
  1664. $posttitle = $myrow_titel['post_title'];
  1665. $posttext = str_replace('"', "'", $posttext);
  1666. //2 Export contents as an HTML table
  1667. $expcontent .= "<table border='0' cellpadding='3' cellspacing='1' width='100%'>
  1668. <tr>
  1669. <td colspan='2' bgcolor='#e6e6e6'><b>$posttitle</b><br />$posttext</td>
  1670. </tr>
  1671. <tr>
  1672. <td colspan='2'></td>
  1673. </tr>
  1674. <tr>
  1675. <td bgcolor='#cccccc' align='left'>$lg_author : $posterprenom $posternom</td>
  1676. <td align='right' bgcolor='#cccccc'>$lg_date : $posttime</td>
  1677. </tr>
  1678. <tr><td colspan='2' height='10'></td></tr>
  1679. </table>";
  1680. break;
  1681. // NOT IMPLEMENTED ITEMS BEGIN
  1682. case 'Forum':
  1683. case 'Thread':
  1684. case 'Dropbox':
  1685. case 'Assignments':
  1686. case 'Groups':
  1687. case 'Users':
  1688. // 1. Instead of building something, put an info message.
  1689. $langItemMissing1 = "There was a ";
  1690. $langItemMissing2 = "page (step) here in the original Chamilo Learning Path.";
  1691. $expcontent .= "<div class='message'>$langItemMissing1 $item_type $langItemMissing2</div>";
  1692. break;
  1693. // Link BEGIN
  1694. case 'Link _self':
  1695. case 'Link _blank':
  1696. // 1. Get the link data from the database.
  1697. $TABLETOOLLINK = Database :: get_course_link_table();
  1698. $result = Database::query("SELECT * FROM $TABLETOOLLINK WHERE c_id = $course_id AND id=$item_id");
  1699. $myrow = Database::fetch_array($result);
  1700. $thelink = $myrow['url'];
  1701. // 2. Check the link type (open in blank page or in current page).
  1702. if ($item_type == 'Link _blank') {
  1703. $target = '_blank';
  1704. }
  1705. // 3. Write the link to the export string.
  1706. $expcontent .= "<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target='".$target."'>$LPname</a>";
  1707. // 4. Change the element type for later changes (this is lost, however, so useless here).
  1708. $item_type = 'Link'; // To put this to the filename.
  1709. //$LPname="<a href='$thelink?SQMSESSID=36812c2dea7d8d6e708d5e6a2f09b0b9' target=".$target.">$LPname</a>";
  1710. // I am still not sure about Link export : to export them as files or they can appear in the TOC at once ?
  1711. // To enable the second possibility, unrem the row $LPname=...
  1712. break;
  1713. }
  1714. // Now we add the Done button and the initialize function : loadpage()
  1715. // not in the case of Documents, HotP
  1716. if ($item_type != 'Exercise' and ($add_scorm_communications === true)) {
  1717. $expcontent .= $donebutton;
  1718. }
  1719. // End the export string with valid HTML tags.
  1720. $expcontent .= "</body></html>";
  1721. // Prepare new file name.
  1722. $filename = $item_type.$id.".htm";
  1723. // Write the export content to the new file.
  1724. exporttofile('data/'.$filename, $LPname, $id, $expcontent);
  1725. }
  1726. /**
  1727. * This function exports the given item's description into a separate file
  1728. * @param integer Item id
  1729. * @param string Item type
  1730. * @param string Description
  1731. * @return void
  1732. */
  1733. function exportdescription($id, $item_type, $description)
  1734. {
  1735. global $expdir;
  1736. $filename = $item_type.$id.'.desc';
  1737. $expcontent = $description;
  1738. exporttofile($expdir.$filename, 'description_of_'.$item_type.$id, 'description_of_item_'.$id, $expcontent);
  1739. }
  1740. /**
  1741. * This function deletes an entire directory
  1742. * @param string The directory path
  1743. * @return boolean True on success, false on failure
  1744. */
  1745. function deldir($dir)
  1746. {
  1747. $dh = opendir($dir);
  1748. while ($file = readdir($dh)) {
  1749. if ($file != '.' && $file != '..') {
  1750. $fullpath = $dir.'/'.$file;
  1751. if (!is_dir($fullpath)) {
  1752. unlink($fullpath);
  1753. } else {
  1754. deldir($fullpath);
  1755. }
  1756. }
  1757. }
  1758. closedir($dh);
  1759. if (rmdir($dir)) {
  1760. return true;
  1761. }
  1762. return false;
  1763. }
  1764. /**
  1765. * Export SCORM content into a zip file
  1766. *
  1767. * Basically, all this function does is put the scorm directory back into a zip file (like the one
  1768. * that was most probably used to import the course at first)
  1769. * @deprecated this function is only called in the newscorm/scorm_admin.php which is deprecated
  1770. *
  1771. * @param string Name of the SCORM path (or the directory under which it resides)
  1772. * @param array Not used right now. Should replace the use of global $_course
  1773. * @return void
  1774. * @author imandak80
  1775. */
  1776. function exportSCORM($scormname, $course)
  1777. {
  1778. global $_course;
  1779. // Initialize.
  1780. $tmpname = api_get_path(SYS_COURSE_PATH).$_course['path'].'/scorm';
  1781. $zipfoldername = $tmpname.$scormname;
  1782. $zipfilename = $zipfoldername.'.zip';
  1783. // Create zipfile of given directory.
  1784. include_once api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  1785. $zip_folder = new PclZip($zipfilename);
  1786. $list = 1;
  1787. //$list = $zip_folder->create($zipfoldername.'/',PCLZIP_OPT_REMOVE_PATH,$tmpname.$scormname."/"); // whitout folder
  1788. $list = $zip_folder->create($zipfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $tmpname);
  1789. if ($list == 0) {
  1790. //echo "Error : ".$zip_folder->errorInfo(true);
  1791. }
  1792. // Send to client.
  1793. DocumentManager :: file_send_for_download($zipfilename, false, basename($scormname.'.zip'));
  1794. FileManager::my_delete($zipfilename);
  1795. }
  1796. /**
  1797. * This function returns an xml tag
  1798. * $data behaves as the content in case of full tags
  1799. * $data is an array of attributes in case of returning an opening tag
  1800. * @param string
  1801. * @param string
  1802. * @param array
  1803. * @param string
  1804. * @return string
  1805. */
  1806. function xmltagwrite($tagname, $which, $data, $linebreak = 'yes')
  1807. {
  1808. switch ($which) {
  1809. case 'open':
  1810. $tag = '<'.$tagname;
  1811. $i = 0;
  1812. while ($data[0][$i]) {
  1813. $tag .= ' '.$data[0][$i]."=\"".$data[1][$i]."\"";
  1814. $i++;
  1815. }
  1816. if ($tagname == 'file') {
  1817. $closing = '/';
  1818. }
  1819. $tag .= $closing.'>';
  1820. if ($linebreak != 'no_linebreak') {
  1821. $tag .= "\n";
  1822. }
  1823. break;
  1824. case 'close':
  1825. $tag = '</'.$tagname.'>';
  1826. if ($linebreak != 'no_linebreak') {
  1827. $tag .= "\n";
  1828. }
  1829. break;
  1830. case 'full':
  1831. $tag = '<'.$tagname;
  1832. $tag .= '>'.$data.'</'.$tagname.'>';
  1833. if ($linebreak != 'no_linebreak') {
  1834. $tag .= "\n";
  1835. }
  1836. break;
  1837. }
  1838. return $tag;
  1839. }
  1840. /**
  1841. * This function writes the imsmanifest.xml and exports the chapter names
  1842. * @param array Array containing filenames
  1843. * @param integer Learnpath_id
  1844. * @return void
  1845. */
  1846. function createimsmanifest($circle1_files, $learnpath_id)
  1847. {
  1848. global $_course, $LPname, $expdir, $LPnamesafe;
  1849. //$tbl_learnpath_main, $tbl_learnpath_chapter, $tbl_learnpath_item,
  1850. $tbl_learnpath_main = Database :: get_course_table(TABLE_LEARNPATH_MAIN);
  1851. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  1852. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  1853. include_once '../metadata/md_funcs.php'; // RH: export metadata
  1854. // Header
  1855. // Charset should be dependent on content.
  1856. $header = '<?xml version="1.0" encoding="'.api_get_system_encoding(
  1857. ).'"?>'."\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";
  1858. $org .= xmltagwrite('metadata', 'open');
  1859. $org .= ' '.xmltagwrite('schema', 'full', 'ADL SCORM');
  1860. $org .= ' '.xmltagwrite('schemaversion', 'full', '1.2');
  1861. $org .= xmltagwrite('metadata', 'close');
  1862. $defaultorgname = 'default_org';
  1863. $attributes[0][0] = 'default';
  1864. $attributes[1][0] = $defaultorgname;
  1865. $org .= xmltagwrite('organizations', 'open', $attributes);
  1866. $attributes[0][0] = 'identifier';
  1867. $attributes[1][0] = $defaultorgname;
  1868. $org .= ' '.xmltagwrite('organization', 'open', $attributes);
  1869. $org .= ' '.xmltagwrite('title', 'full', $LPname);
  1870. // Items list.
  1871. $i = 0;
  1872. $course_id = api_get_course_int_id();
  1873. $previous_item_id = '00';
  1874. while ($circle1_files[0][$i]) {
  1875. // Check whether we are in the border of two chapters.
  1876. //if (!$desc=strpos($circle1_files[2][$i],'scription')) { // This is needed if the descriptions are exported to file.
  1877. $sql = "SELECT * FROM $tbl_learnpath_item WHERE c_id = $course_id AND (id=".$circle1_files[2][$i].")";
  1878. $result = Database::query($sql);
  1879. $row = Database::fetch_array($result);
  1880. $parent_item_id = $row['parent_item_id'];
  1881. if ($parent_item_id != $previous_item_id) {
  1882. // We create the item tag for the chapter (without indifierref).
  1883. $sql2 = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND (id=".$parent_item_id.")";
  1884. $result2 = Database::query($sql2);
  1885. $row2 = Database::fetch_array($result2);
  1886. $chapter_name = $row2['chapter_name'];
  1887. $attributes = '';
  1888. $attributes[0][] = 'identifier';
  1889. $attributes[1][] = 'chapter_'.$row2['id'];
  1890. $attributes[0][] = 'isvisible';
  1891. $attributes[1][] = '1';
  1892. if ($previous_item_id != '00') {
  1893. $org .= ' '.xmltagwrite('item', 'close');
  1894. }
  1895. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1896. $org .= ' '.xmltagwrite('title', 'full', $chapter_name);
  1897. if ($row2['chapter_description'] != '') {
  1898. // Chapter description.
  1899. $attributes = '';
  1900. $attributes[0][] = 'identifier';
  1901. $attributes[1][] = 'chapter_'.$row2['id'].'_desc';
  1902. $attributes[0][] = 'isvisible';
  1903. $attributes[1][] = '1';
  1904. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1905. $org .= ' '.xmltagwrite('title', 'full', ' '.$row2['chapter_description']);
  1906. $org .= ' '.xmltagwrite('item', 'close');
  1907. }
  1908. }
  1909. $previous_item_id = $parent_item_id;
  1910. //}
  1911. $attributes = ''; // Item output.
  1912. $attributes[0][] = 'identifier';
  1913. $attributes[1][] = 'item_'.$circle1_files[2][$i];
  1914. $attributes[0][] = 'identifierref';
  1915. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  1916. $attributes[0][] = 'isvisible';
  1917. $attributes[1][] = '1';
  1918. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1919. $org .= ' '.xmltagwrite('title', 'full', $circle1_files[1][$i]);
  1920. if ($row['prereq_id'] != '') {
  1921. // Item prerequisites.
  1922. $attributes = '';
  1923. $attributes[0][] = 'type';
  1924. $attributes[1][] = 'aicc_script';
  1925. $org .= ' '.xmltagwrite('adlcp:prerequisites', 'open', $attributes, 'no_linebreak');
  1926. if ($row['prereq_type'] == 'i') {
  1927. $org .= 'item_'.$row['prereq_id'];
  1928. }
  1929. if ($row['prereq_type'] == 'c') {
  1930. $org .= 'chapter_'.$row['prereq_id'];
  1931. }
  1932. $org .= xmltagwrite('adlcp:prerequisites', 'close', $attributes);
  1933. }
  1934. if ($row['description'] != '') {
  1935. // Item description.
  1936. $attributes = '';
  1937. $attributes[0][] = 'identifier';
  1938. $attributes[1][] = 'item_'.$circle1_files[2][$i].'_desc';
  1939. $attributes[0][] = 'isvisible';
  1940. $attributes[1][] = '1';
  1941. $org .= ' '.xmltagwrite('item', 'open', $attributes);
  1942. $org .= ' '.xmltagwrite('title', 'full', ' '.$row['description']);
  1943. $org .= ' '.xmltagwrite('item', 'close');
  1944. }
  1945. $mds = new mdstore(true); // RH: export metadata; if no table, create it
  1946. if (($mdt = $mds->mds_get($row['item_type'].'.'.$row['item_id']))) {
  1947. if (($mdo = api_strpos($mdt, '<metadata>')) && ($mdc = api_strpos($mdt, '</metadata>'))) {
  1948. $org .= ' '.api_substr($mdt, $mdo, $mdc - $mdo + 11)."\n";
  1949. }
  1950. }
  1951. $org .= ' '.xmltagwrite('item', 'close');
  1952. $i++;
  1953. }
  1954. if ($circle1_files) {
  1955. $org .= ' '.xmltagwrite('item', 'close');
  1956. } // Not needed in case of a blank path.
  1957. $org .= ' '.xmltagwrite('organization', 'close');
  1958. $org .= xmltagwrite('organizations', 'close');
  1959. $org .= xmltagwrite('resources', 'open');
  1960. // Resources list.
  1961. $i = 0;
  1962. while ($circle1_files[0][$i]) {
  1963. $attributes = '';
  1964. $attributes[0][] = 'identifier';
  1965. $attributes[1][] = 'item_ref_'.$circle1_files[2][$i];
  1966. $attributes[0][] = 'type';
  1967. $attributes[1][] = 'webcontent';
  1968. $attributes[0][] = 'adlcp:scormtype';
  1969. $attributes[1][] = 'sco';
  1970. $attributes[0][] = 'href';
  1971. $attributes[1][] = $circle1_files[0][$i];
  1972. $org .= ' '.xmltagwrite('resource', 'open', $attributes);
  1973. $org .= ' '.xmltagwrite('metadata', 'open');
  1974. $org .= ' '.xmltagwrite('schema', 'full', 'ADL SCORM');
  1975. $org .= ' '.xmltagwrite('schemaversion', 'full', '1.2');
  1976. $org .= ' '.xmltagwrite('metadata', 'close');
  1977. $attributes = '';
  1978. $attributes[0][] = 'href';
  1979. $attributes[1][] = $circle1_files[0][$i];
  1980. $org .= ' '.xmltagwrite('file', 'open', $attributes);
  1981. $org .= ' '.xmltagwrite('resource', 'close');
  1982. $i++;
  1983. }
  1984. $org .= xmltagwrite('resources', 'close');
  1985. $org .= xmltagwrite('manifest', 'close');
  1986. $manifest = $header.$org;
  1987. exporttofile('imsmanifest.xml', 'Manifest file', '0', $manifest);
  1988. }
  1989. /**
  1990. * Gets the tags of the file given as parameter
  1991. *
  1992. * if $filename is not found, GetSRCTags(filename) will return FALSE
  1993. * @param string File path
  1994. * @return mixed Array of strings on success, false on failure
  1995. * @author unknown
  1996. * @author Included by imandak80
  1997. */
  1998. function GetSRCTags($fileName)
  1999. {
  2000. if (!($fp = fopen($fileName, 'r'))) {
  2001. // Iif file can't be opened, return false.
  2002. return false;
  2003. }
  2004. // Read file contents.
  2005. $contents = fread($fp, filesize($fileName));
  2006. fclose($fp);
  2007. $matches = array();
  2008. $srcList = array();
  2009. // Get all src tags contents in this file. Use multi-line search.
  2010. preg_match_all(
  2011. '/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/mi',
  2012. $contents,
  2013. $matches
  2014. ); // Get the img src as contained between " or '
  2015. foreach ($matches[3] as $match) {
  2016. if (!in_array($match, $srcList)) {
  2017. $srcList[] = $match;
  2018. }
  2019. }
  2020. if (count($srcList) == 0) {
  2021. return false;
  2022. }
  2023. return $srcList;
  2024. }
  2025. /**
  2026. * Copy file and create directories in the path if needed.
  2027. *
  2028. * @param string $source Source path
  2029. * @param string $dest Destination path
  2030. * @return boolean true on success, false on failure
  2031. */
  2032. function CopyNCreate($source, $dest)
  2033. {
  2034. if (strcmp($source, $dest) == 0) {
  2035. return false;
  2036. }
  2037. $dir = '';
  2038. $tdest = explode('/', $dest);
  2039. for ($i = 0; $i < sizeof($tdest) - 1; $i++) {
  2040. $dir = $dir.$tdest[$i].'/';
  2041. if (!is_dir($dir)) {
  2042. if (!mkdir($dir, api_get_permissions_for_new_directories())) {
  2043. return false;
  2044. }
  2045. }
  2046. }
  2047. if (!copy($source, $dest)) {
  2048. return false;
  2049. }
  2050. return true;
  2051. }
  2052. function rcopy($source, $dest)
  2053. {
  2054. //error_log($source." -> ".$dest, 0);
  2055. if (!file_exists($source)) {
  2056. //error_log($source." does not exist", 0);
  2057. return false;
  2058. }
  2059. if (is_dir($source)) {
  2060. //error_log($source." is a dir", 0);
  2061. // This is a directory.
  2062. // Remove trailing '/'
  2063. if (strrpos($source, '/') == sizeof($source) - 1) {
  2064. $source = substr($source, 0, size_of($source) - 1);
  2065. }
  2066. if (strrpos($dest, '/') == sizeof($dest) - 1) {
  2067. $dest = substr($dest, 0, size_of($dest) - 1);
  2068. }
  2069. if (!is_dir($dest)) {
  2070. $res = @mkdir($dest, api_get_permissions_for_new_directories());
  2071. if ($res !== false) {
  2072. return true;
  2073. } else {
  2074. // Remove latest part of path and try creating that.
  2075. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/')))) {
  2076. return @mkdir($dest, api_get_permissions_for_new_directories());
  2077. } else {
  2078. return false;
  2079. }
  2080. }
  2081. }
  2082. return true;
  2083. } else {
  2084. // This is presumably a file.
  2085. //error_log($source." is a file", 0);
  2086. if (!@ copy($source, $dest)) {
  2087. //error_log("Could not simple-copy $source", 0);
  2088. $res = rcopy(dirname($source), dirname($dest));
  2089. if ($res === true) {
  2090. //error_log("Welcome dir created", 0);
  2091. return @ copy($source, $dest);
  2092. } else {
  2093. return false;
  2094. //error_log("Error creating path", 0);
  2095. }
  2096. } else {
  2097. //error_log("Could well simple-copy $source", 0);
  2098. return true;
  2099. }
  2100. }
  2101. }