lp_controller.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Controller script. Prepares the common background variables to give to the scripts corresponding to
  6. * the requested action.
  7. *
  8. * @todo remove repeated if $lp_found redirect
  9. *
  10. * @package chamilo.learnpath
  11. *
  12. * @author Yannick Warnier <ywarnier@beeznest.org>
  13. */
  14. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  15. $use_anonymous = true;
  16. $debug = 0;
  17. require_once __DIR__.'/../inc/global.inc.php';
  18. api_protect_course_script(true);
  19. $current_course_tool = TOOL_LEARNPATH;
  20. $_course = api_get_course_info();
  21. $glossaryExtraTools = api_get_setting('show_glossary_in_extra_tools');
  22. $showGlossary = in_array($glossaryExtraTools, ['true', 'lp', 'exercise_and_lp']);
  23. if ($showGlossary) {
  24. if (api_get_setting('show_glossary_in_documents') === 'ismanual' ||
  25. api_get_setting('show_glossary_in_documents') === 'isautomatic'
  26. ) {
  27. $htmlHeadXtra[] = '<script>
  28. <!--
  29. var jQueryFrameReadyConfigPath = \''.api_get_jquery_web_path().'\';
  30. -->
  31. </script>';
  32. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.frameready.js" type="text/javascript" language="javascript"></script>';
  33. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.highlight.js" type="text/javascript" language="javascript"></script>';
  34. }
  35. }
  36. $htmlHeadXtra[] = '<script>
  37. function setFocus(){
  38. $("#idTitle").focus();
  39. }
  40. $(window).on("load", function () {
  41. setFocus();
  42. });
  43. </script>';
  44. $ajax_url = api_get_path(WEB_AJAX_PATH).'lp.ajax.php?'.api_get_cidreq();
  45. $htmlHeadXtra[] = '
  46. <script>
  47. /*
  48. Script to manipulate Learning Path items with Drag and drop
  49. */
  50. var newOrderData = "";
  51. function buildLPtree(in_elem, in_parent_id) {
  52. var item_tag = in_elem.get(0).tagName;
  53. var item_id = in_elem.attr("id");
  54. var parent_id = item_id;
  55. if (item_tag == "LI" && item_id != undefined) {
  56. // in_parent_id de la forme UL_x
  57. newOrderData += item_id+"|"+get_UL_integer_id(in_parent_id)+"^";
  58. }
  59. in_elem.children().each(function () {
  60. buildLPtree($(this), parent_id);
  61. });
  62. }
  63. // return the interge part of an UL id
  64. // (0 for lp_item_list)
  65. function get_UL_integer_id(in_ul_id) {
  66. in_parent_integer_id = in_ul_id;
  67. in_parent_integer_id = in_parent_integer_id.replace("lp_item_list", "0");
  68. in_parent_integer_id = in_parent_integer_id.replace("UL_", "");
  69. return in_parent_integer_id;
  70. }
  71. $(function() {
  72. $(".lp_resource").sortable({
  73. items: ".lp_resource_element ",
  74. handle: ".moved", //only the class "moved"
  75. cursor: "move",
  76. connectWith: "#lp_item_list",
  77. placeholder: "ui-state-highlight", //defines the yellow highlight
  78. start: function(event, ui) {
  79. $(ui.item).css("width", "350px");
  80. $(ui.item).find(".item_data").attr("style", "");
  81. },
  82. stop: function(event, ui) {
  83. $(ui.item).css("width", "100%");
  84. }
  85. });
  86. $(".li_container .order_items").click(function(e) {
  87. var dir = $(this).data("dir");
  88. var itemId = $(this).data("id");
  89. var jItems = $("#lp_item_list li.li_container");
  90. var jItem = $("#"+ itemId);
  91. var index = jItems.index(jItem);
  92. var total = jItems.length;
  93. //console.log("total " + total);
  94. //console.log("current " + index);
  95. switch (dir) {
  96. case "up":
  97. if (index != 0 && jItems[index - 1]) {
  98. /*var subItems = $(jItems[index - 1]).find("li.sub_item");
  99. if (subItems.length >= 0) {
  100. index = index - 1;
  101. }*/
  102. var subItems = $(jItems[index - 1]).find("li.sub_item");
  103. var parentClass = $(jItems[index - 1]).parent().parent().attr("class");
  104. var parentId = $(jItems[index]).parent().parent().attr("id");
  105. var myParentId = $(jItems[index - 1]).parent().parent().attr("id");
  106. //console.log(parentId + " - " + myParentId);
  107. // We are brothers!
  108. if (parentId == myParentId) {
  109. console.log("Brothers");
  110. console.log(subItems.length);
  111. if (subItems.length > 0) {
  112. var lastItem = $(jItems[index - 1]).find("li.sub_item");
  113. parentIndex = jItems.index(lastItem);
  114. console.log(parentIndex);
  115. jItem.detach().insertAfter(lastItem);
  116. //console.log("not classic");
  117. } else {
  118. //console.log("classic");
  119. jItem.detach().insertBefore(jItems[index - 1]);
  120. }
  121. break;
  122. }
  123. //console.log(parentClass);
  124. if (parentClass == "record li_container") {
  125. // previous is a chapter
  126. var lastItem = $(jItems[index - 1]).parent().parent().find("li.li_container").last();
  127. parentIndex = jItems.index(lastItem);
  128. //console.log(parentIndex);
  129. jItem.detach().insertAfter(jItems[parentIndex]);
  130. } else {
  131. jItem.detach().insertBefore(jItems[index - 1]);
  132. }
  133. }
  134. break;
  135. case "down":
  136. if (index != total - 1) {
  137. const originIndex = index;
  138. // The element is a chapter with items
  139. var subItems = jItem.find("li.li_container");
  140. if (subItems.length > 0) {
  141. index = subItems.length + index;
  142. //console.log("element is a chapter with items");
  143. //console.log("new index = " + index);
  144. }
  145. var subItems = $(jItems[index + 1]).find("li.sub_item");
  146. //console.log("next subItems.length: "+subItems.length);
  147. // This is an element entering in a chapter
  148. if (subItems.length > 0) {
  149. // Check if im a child
  150. var parentClass = jItem.parent().parent().attr("class");
  151. //console.log(parentClass);
  152. if (parentClass == "record li_container") {
  153. // Parent position
  154. var parentIndex = jItems.index(jItem.parent().parent());
  155. //console.log(jItem.parent().parent().attr("id"));
  156. //console.log(parentIndex);
  157. jItem.detach().insertAfter(jItems[parentIndex]);
  158. } else {
  159. jItem.detach().insertAfter(subItems);
  160. }
  161. break;
  162. }
  163. var currentSubItems = $(jItems[index]).parent().find("li.sub_item");
  164. //console.log("currentSubItems"+currentSubItems.length);
  165. var parentId = $(jItems[originIndex]).parent().parent().attr("id");
  166. var myParentId = $(jItems[index + 1]).parent().parent().attr("id");
  167. //console.log("parent ids: "+ parentId + " - " + myParentId);
  168. // We are brothers!
  169. if (parentId == myParentId) {
  170. if ((index + 1) < total) {
  171. //console.log(index + 1);
  172. //console.log("We are brothers");
  173. jItem.detach().insertAfter(jItems[index + 1]);
  174. }
  175. break;
  176. }
  177. if (currentSubItems.length > 0) {
  178. var parentIndex = jItems.index(jItem.parent().parent());
  179. //console.log("has currentSubItems");
  180. //console.log("id " + jItem.parent().parent().attr("id"));
  181. //console.log("parentIndex: " + parentIndex);
  182. if (parentIndex >= 0) {
  183. jItem.detach().insertAfter(jItems[parentIndex]);
  184. break;
  185. }
  186. //jItem.detach().insertAfter($(jItems[index]).parent().parent());
  187. }
  188. //var lastItem = $(jItems[index + 1]).parent().parent().find("li.li_container").last();
  189. if (subItems.length > 0) {
  190. index = originIndex;
  191. }
  192. if ((index + 1) < total) {
  193. //console.log(index + 1);
  194. //console.log("changed");
  195. jItem.detach().insertAfter(jItems[index + 1]);
  196. }
  197. }
  198. break;
  199. }
  200. //console.log("rebuild");
  201. buildLPtree($("#lp_item_list"), 0);
  202. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  203. $.post(
  204. "'.$ajax_url.'",
  205. order,
  206. function(reponse) {
  207. $("#message").html(reponse);
  208. order = "";
  209. newOrderData = "";
  210. }
  211. );
  212. });
  213. $("#lp_item_list").sortable({
  214. items: "li",
  215. handle: ".moved", //only the class "moved"
  216. cursor: "move",
  217. placeholder: "ui-state-highlight", //defines the yellow highlight
  218. update: function(event, ui) {
  219. buildLPtree($("#lp_item_list"), 0);
  220. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  221. $.post(
  222. "'.$ajax_url.'",
  223. order,
  224. function(reponse) {
  225. $("#message").html(reponse);
  226. order = "";
  227. newOrderData = "";
  228. }
  229. );
  230. },
  231. receive: function(event, ui) {
  232. var id = $(ui.item).attr("data_id");
  233. var type = $(ui.item).attr("data_type");
  234. var title = $(ui.item).attr("title");
  235. processReceive = true;
  236. if (ui.item.parent()[0]) {
  237. var parent_id = $(ui.item.parent()[0]).attr("id");
  238. var previous_id = $(ui.item.prev()).attr("id");
  239. if (parent_id) {
  240. parent_id = parent_id.split("_")[1];
  241. var params = {
  242. "a": "add_lp_item",
  243. "id": id,
  244. "parent_id": parent_id,
  245. "previous_id": previous_id,
  246. "type": type,
  247. "title" : title
  248. };
  249. $.ajax({
  250. type: "GET",
  251. url: "'.$ajax_url.'",
  252. data: params,
  253. async: false,
  254. success: function(data) {
  255. $("#lp_item_list").html(data);
  256. }
  257. });
  258. }
  259. }
  260. } // End receive
  261. });
  262. processReceive = false;
  263. });
  264. </script>';
  265. $session_id = api_get_session_id();
  266. $lpfound = false;
  267. $myrefresh = 0;
  268. $myrefresh_id = 0;
  269. $refresh = Session::read('refresh');
  270. if ($refresh == 1) {
  271. // Check if we should do a refresh of the oLP object (for example after editing the LP).
  272. // If refresh is set, we regenerate the oLP object from the database (kind of flush).
  273. Session::erase('refresh');
  274. $myrefresh = 1;
  275. }
  276. if ($debug > 0) {
  277. error_log(' $refresh: '.$refresh);
  278. error_log(' $myrefresh: '.$myrefresh);
  279. }
  280. $lp_controller_touched = 1;
  281. $lp_found = false;
  282. $lpObject = Session::read('lpobject');
  283. if (!empty($lpObject)) {
  284. if ($debug) {
  285. error_log(' SESSION[lpobject] is defined');
  286. }
  287. /** @var learnpath $oLP */
  288. $oLP = UnserializeApi::unserialize('lp', $lpObject);
  289. if (isset($oLP) && is_object($oLP)) {
  290. if ($debug) {
  291. error_log(' oLP is object');
  292. }
  293. if ($myrefresh == 1 ||
  294. empty($oLP->cc) ||
  295. $oLP->cc != api_get_course_id() ||
  296. $oLP->lp_view_session_id != $session_id
  297. ) {
  298. if ($debug) {
  299. error_log('Course has changed, discard lp object');
  300. error_log('$oLP->lp_view_session_id: '.$oLP->lp_view_session_id);
  301. error_log('api_get_session_id(): '.$session_id);
  302. error_log('$oLP->cc: '.$oLP->cc);
  303. error_log('api_get_course_id(): '.api_get_course_id());
  304. }
  305. if ($myrefresh == 1) {
  306. $myrefresh_id = $oLP->get_id();
  307. }
  308. $oLP = null;
  309. Session::erase('oLP');
  310. Session::erase('lpobject');
  311. } else {
  312. Session::write('oLP', $oLP);
  313. $lp_found = true;
  314. }
  315. }
  316. }
  317. if ($debug) {
  318. error_log('$lp_found: '.$lp_found);
  319. error_log('$myrefresh_id: '.$myrefresh_id);
  320. }
  321. $course_id = api_get_course_int_id();
  322. if (!$lp_found || (!empty($_REQUEST['lp_id']) && $_SESSION['oLP']->get_id() != $_REQUEST['lp_id'])) {
  323. if ($debug > 0) {
  324. error_log(' oLP is not object, has changed or refresh been asked, getting new');
  325. }
  326. // Regenerate a new lp object? Not always as some pages don't need the object (like upload?)
  327. if (!empty($_REQUEST['lp_id']) || !empty($myrefresh_id)) {
  328. // Select the lp in the database and check which type it is (scorm/chamilo/aicc) to generate the
  329. // right object.
  330. if (!empty($_REQUEST['lp_id'])) {
  331. $lp_id = $_REQUEST['lp_id'];
  332. } else {
  333. $lp_id = $myrefresh_id;
  334. }
  335. $lp_id = (int) $lp_id;
  336. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  337. if (!empty($lp_id)) {
  338. $sel = "SELECT iid, lp_type FROM $lp_table WHERE c_id = $course_id AND id = $lp_id";
  339. if ($debug > 0) {
  340. error_log(' querying '.$sel);
  341. }
  342. $res = Database::query($sel);
  343. if (Database::num_rows($res)) {
  344. $row = Database::fetch_array($res);
  345. $lpIid = $row['iid'];
  346. $type = $row['lp_type'];
  347. if ($debug > 0) {
  348. error_log('Found row type '.$type);
  349. error_log('Calling constructor: '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id());
  350. }
  351. $logInfo = [
  352. 'tool' => TOOL_LEARNPATH,
  353. 'tool_id' => 0,
  354. 'tool_id_detail' => 0,
  355. 'action' => 'lp_load',
  356. ];
  357. Event::registerLog($logInfo);
  358. switch ($type) {
  359. case 1:
  360. $oLP = new learnpath(api_get_course_id(), $lpIid, api_get_user_id());
  361. if ($oLP !== false) {
  362. $lp_found = true;
  363. }
  364. break;
  365. case 2:
  366. $oLP = new scorm(api_get_course_id(), $lpIid, api_get_user_id());
  367. if ($oLP !== false) {
  368. $lp_found = true;
  369. }
  370. break;
  371. case 3:
  372. $oLP = new aicc(api_get_course_id(), $lpIid, api_get_user_id());
  373. if ($oLP !== false) {
  374. $lp_found = true;
  375. }
  376. break;
  377. default:
  378. $oLP = new learnpath(api_get_course_id(), $lpIid, api_get_user_id());
  379. if ($oLP !== false) {
  380. $lp_found = true;
  381. }
  382. break;
  383. }
  384. }
  385. } else {
  386. if ($debug > 0) {
  387. error_log(' Request[lp_id] is not numeric');
  388. }
  389. }
  390. } else {
  391. if ($debug > 0) {
  392. error_log(' Request[lp_id] and refresh_id were empty');
  393. }
  394. }
  395. if ($lp_found) {
  396. Session::write('oLP', $oLP);
  397. }
  398. }
  399. if ($debug > 0) {
  400. error_log('Passed oLP creation check');
  401. }
  402. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, false, false);
  403. if (isset($_SESSION['oLP'])) {
  404. // Reinitialises array used by javascript to update items in the TOC.
  405. $_SESSION['oLP']->update_queue = [];
  406. }
  407. $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
  408. if ($debug) {
  409. error_log('Entered lp_controller.php -+- (action: '.$action.')');
  410. }
  411. $eventLpId = $lp_id = !empty($_REQUEST['lp_id']) ? (int) $_REQUEST['lp_id'] : 0;
  412. if (empty($lp_id)) {
  413. if (isset($_SESSION['oLP'])) {
  414. $eventLpId = $_SESSION['oLP']->get_id();
  415. }
  416. }
  417. $lp_detail_id = 0;
  418. $attemptId = 0;
  419. switch ($action) {
  420. case '':
  421. case 'list':
  422. $eventLpId = 0;
  423. break;
  424. case 'view':
  425. case 'content':
  426. $lp_detail_id = $oLP->get_current_item_id();
  427. $attemptId = $oLP->getCurrentAttempt();
  428. break;
  429. default:
  430. $lp_detail_id = (!empty($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0);
  431. break;
  432. }
  433. $logInfo = [
  434. 'tool' => TOOL_LEARNPATH,
  435. 'tool_id' => $eventLpId,
  436. 'tool_id_detail' => $lp_detail_id,
  437. 'action_details' => $attemptId,
  438. 'action' => !empty($action) ? $action : 'list',
  439. ];
  440. Event::registerLog($logInfo);
  441. // format title to be displayed correctly if QUIZ
  442. $post_title = '';
  443. if (isset($_POST['title'])) {
  444. $post_title = Security::remove_XSS($_POST['title']);
  445. if (isset($_POST['type']) &&
  446. isset($_POST['title']) &&
  447. $_POST['type'] == TOOL_QUIZ &&
  448. !empty($_POST['title'])
  449. ) {
  450. $post_title = Exercise::format_title_variable($_POST['title']);
  451. }
  452. }
  453. $redirectTo = '';
  454. if ($debug > 0) {
  455. error_log('action "'.$action.'" triggered');
  456. }
  457. switch ($action) {
  458. case 'send_notify_teacher':
  459. // Send notification to the teacher
  460. $studentInfo = api_get_user_info();
  461. $course_info = api_get_course_info();
  462. $sessionId = api_get_session_id();
  463. $courseName = $course_info['title'];
  464. $courseUrl = $course_info['course_public_url'];
  465. if (!empty($sessionId)) {
  466. $sessionInfo = api_get_session_info($sessionId);
  467. $courseName = $sessionInfo['name'];
  468. $courseUrl .= '?id_session='.$sessionId;
  469. }
  470. $url = Display::url($courseName, $courseUrl, ['title' => get_lang('GoToCourse')]);
  471. $coachList = CourseManager::get_coachs_from_course($sessionId, api_get_course_int_id());
  472. foreach ($coachList as $coach_course) {
  473. $recipientName = $coach_course['full_name'];
  474. $coachInfo = api_get_user_info($coach_course['user_id']);
  475. if (empty($coachInfo)) {
  476. continue;
  477. }
  478. $email = $coachInfo['email'];
  479. $tplContent = new Template(null, false, false, false, false, false);
  480. $tplContent->assign('name_teacher', $recipientName);
  481. $tplContent->assign('name_student', $studentInfo['complete_name']);
  482. $tplContent->assign('course_name', $courseName);
  483. $tplContent->assign('course_url', $url);
  484. $layoutContent = $tplContent->get_template('mail/content_ending_learnpath.tpl');
  485. $emailBody = $tplContent->fetch($layoutContent);
  486. api_mail_html(
  487. $recipientName,
  488. $email,
  489. sprintf(get_lang('StudentXFinishedLp'), $studentInfo['complete_name']),
  490. $emailBody,
  491. $studentInfo['complete_name'],
  492. $studentInfo['email'],
  493. true
  494. );
  495. }
  496. Display::addFlash(Display::return_message(get_lang('MessageSent')));
  497. $url = api_get_self().'?action=list&'.api_get_cidreq();
  498. header('Location: '.$url);
  499. exit;
  500. break;
  501. case 'add_item':
  502. if (!$is_allowed_to_edit) {
  503. api_not_allowed(true);
  504. }
  505. if (!$lp_found) {
  506. // Check if the learnpath ID was defined, otherwise send back to list
  507. require 'lp_list.php';
  508. } else {
  509. Session::write('refresh', 1);
  510. if (isset($_POST['submit_button']) && !empty($post_title)) {
  511. // If a title was submitted:
  512. // Updating the lp.modified_on
  513. $_SESSION['oLP']->set_modified_on();
  514. if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_POST['post_time']) {
  515. // Check post_time to ensure ??? (counter-hacking measure?)
  516. require 'lp_add_item.php';
  517. } else {
  518. Session::write('post_time', $_POST['post_time']);
  519. $directoryParentId = isset($_POST['directory_parent_id']) ? $_POST['directory_parent_id'] : 0;
  520. $courseInfo = api_get_course_info();
  521. if (empty($directoryParentId)) {
  522. $_SESSION['oLP']->generate_lp_folder($courseInfo);
  523. }
  524. $parent = isset($_POST['parent']) ? $_POST['parent'] : '';
  525. $previous = isset($_POST['previous']) ? $_POST['previous'] : '';
  526. $type = isset($_POST['type']) ? $_POST['type'] : '';
  527. $path = isset($_POST['path']) ? $_POST['path'] : '';
  528. $description = isset($_POST['description']) ? $_POST['description'] : '';
  529. $prerequisites = isset($_POST['prerequisites']) ? $_POST['prerequisites'] : '';
  530. $maxTimeAllowed = isset($_POST['maxTimeAllowed']) ? $_POST['maxTimeAllowed'] : '';
  531. if ($_POST['type'] == TOOL_DOCUMENT) {
  532. if (isset($_POST['path']) && $_GET['edit'] != 'true') {
  533. $document_id = $_POST['path'];
  534. } else {
  535. if ($_POST['content_lp']) {
  536. $document_id = $_SESSION['oLP']->create_document(
  537. $_course,
  538. $_POST['content_lp'],
  539. $_POST['title'],
  540. 'html',
  541. $directoryParentId
  542. );
  543. }
  544. }
  545. $new_item_id = $_SESSION['oLP']->add_item(
  546. $parent,
  547. $previous,
  548. $type,
  549. $document_id,
  550. $post_title,
  551. $description,
  552. $prerequisites
  553. );
  554. } elseif ($_POST['type'] == TOOL_READOUT_TEXT) {
  555. if (isset($_POST['path']) && $_GET['edit'] != 'true') {
  556. $document_id = $_POST['path'];
  557. } else {
  558. $document_id = $_SESSION['oLP']->createReadOutText(
  559. $_course,
  560. $_POST['content_lp'],
  561. $_POST['title'],
  562. $directoryParentId
  563. );
  564. }
  565. $new_item_id = $_SESSION['oLP']->add_item(
  566. $parent,
  567. $previous,
  568. TOOL_READOUT_TEXT,
  569. $document_id,
  570. $post_title,
  571. $description,
  572. $prerequisites
  573. );
  574. } else {
  575. // For all other item types than documents,
  576. // load the item using the item type and path rather than its ID.
  577. $new_item_id = $_SESSION['oLP']->add_item(
  578. $parent,
  579. $previous,
  580. $type,
  581. $path,
  582. $post_title,
  583. $description,
  584. $prerequisites,
  585. $maxTimeAllowed
  586. );
  587. }
  588. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  589. header('Location: '.$url);
  590. exit;
  591. }
  592. } else {
  593. require 'lp_add_item.php';
  594. }
  595. }
  596. break;
  597. case 'add_users_to_category':
  598. if (!$is_allowed_to_edit) {
  599. api_not_allowed(true);
  600. }
  601. require 'lp_subscribe_users_to_category.php';
  602. break;
  603. case 'add_audio':
  604. if (!$is_allowed_to_edit) {
  605. api_not_allowed(true);
  606. }
  607. if (!$lp_found) {
  608. // Check if the learnpath ID was defined, otherwise send back to list
  609. require 'lp_list.php';
  610. } else {
  611. Session::write('refresh', 1);
  612. if (isset($_REQUEST['id'])) {
  613. $lp_item_obj = new learnpathItem($_REQUEST['id']);
  614. // Remove audio
  615. if (isset($_GET['delete_file']) && $_GET['delete_file'] == 1) {
  616. $lp_item_obj->remove_audio();
  617. $url = api_get_self().'?action=add_audio&lp_id='.intval($_SESSION['oLP']->lp_id).'&id='.$lp_item_obj->get_id().'&'.api_get_cidreq();
  618. header('Location: '.$url);
  619. exit;
  620. }
  621. // Upload audio
  622. if (isset($_FILES['file']) && !empty($_FILES['file'])) {
  623. // Updating the lp.modified_on
  624. $_SESSION['oLP']->set_modified_on();
  625. $lp_item_obj->add_audio();
  626. }
  627. //Add audio file from documents
  628. if (isset($_REQUEST['document_id']) && !empty($_REQUEST['document_id'])) {
  629. $_SESSION['oLP']->set_modified_on();
  630. $lp_item_obj->add_audio_from_documents($_REQUEST['document_id']);
  631. }
  632. // Display.
  633. require 'lp_add_audio.php';
  634. } else {
  635. require 'lp_add_audio.php';
  636. }
  637. }
  638. break;
  639. case 'add_lp_category':
  640. if (!$is_allowed_to_edit) {
  641. api_not_allowed(true);
  642. }
  643. require 'lp_add_category.php';
  644. break;
  645. case 'move_up_category':
  646. if (!$is_allowed_to_edit) {
  647. api_not_allowed(true);
  648. }
  649. if (isset($_REQUEST['id'])) {
  650. learnpath::moveUpCategory($_REQUEST['id']);
  651. }
  652. require 'lp_list.php';
  653. break;
  654. case 'move_down_category':
  655. if (!$is_allowed_to_edit) {
  656. api_not_allowed(true);
  657. }
  658. if (isset($_REQUEST['id'])) {
  659. learnpath::moveDownCategory($_REQUEST['id']);
  660. }
  661. require 'lp_list.php';
  662. break;
  663. case 'delete_lp_category':
  664. if (!$is_allowed_to_edit) {
  665. api_not_allowed(true);
  666. }
  667. if (isset($_REQUEST['id'])) {
  668. $result = learnpath::deleteCategory($_REQUEST['id']);
  669. if ($result) {
  670. Display::addFlash(Display::return_message(get_lang('Deleted')));
  671. }
  672. }
  673. require 'lp_list.php';
  674. break;
  675. case 'add_lp':
  676. if (!$is_allowed_to_edit) {
  677. api_not_allowed(true);
  678. }
  679. if (isset($_REQUEST['lp_name']) && !empty($_REQUEST['lp_name'])) {
  680. $_REQUEST['lp_name'] = trim($_REQUEST['lp_name']);
  681. Session::write('refresh', 1);
  682. if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_REQUEST['post_time']) {
  683. require 'lp_add.php';
  684. } else {
  685. Session::write('post_time', $_POST['post_time']);
  686. $publicated_on = null;
  687. if (isset($_REQUEST['activate_start_date_check']) &&
  688. $_REQUEST['activate_start_date_check'] == 1
  689. ) {
  690. $publicated_on = $_REQUEST['publicated_on'];
  691. }
  692. $expired_on = null;
  693. if (isset($_REQUEST['activate_end_date_check']) &&
  694. $_REQUEST['activate_end_date_check'] == 1
  695. ) {
  696. $expired_on = $_REQUEST['expired_on'];
  697. }
  698. $new_lp_id = learnpath::add_lp(
  699. api_get_course_id(),
  700. Security::remove_XSS($_REQUEST['lp_name']),
  701. '',
  702. 'chamilo',
  703. 'manual',
  704. '',
  705. $publicated_on,
  706. $expired_on,
  707. $_REQUEST['category_id']
  708. );
  709. if (is_numeric($new_lp_id)) {
  710. // Create temp form validator to save skills
  711. $form = new FormValidator('lp_add');
  712. $form->addSelect('skills', 'skills');
  713. Skill::saveSkills($form, ITEM_TYPE_LEARNPATH, $new_lp_id);
  714. $extraFieldValue = new ExtraFieldValue('lp');
  715. $_REQUEST['item_id'] = $new_lp_id;
  716. $extraFieldValue->saveFieldValues($_REQUEST);
  717. // TODO: Maybe create a first directory directly to avoid bugging the user with useless queries
  718. $_SESSION['oLP'] = new learnpath(
  719. api_get_course_id(),
  720. $new_lp_id,
  721. api_get_user_id()
  722. );
  723. $accumulateScormTime = isset($_REQUEST['accumulate_scorm_time']) ? $_REQUEST['accumulate_scorm_time'] : 'true';
  724. $_SESSION['oLP']->setAccumulateScormTime($accumulateScormTime);
  725. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($new_lp_id).'&'.api_get_cidreq();
  726. header("Location: $url&isStudentView=false");
  727. exit;
  728. }
  729. }
  730. } else {
  731. require 'lp_add.php';
  732. }
  733. break;
  734. case 'admin_view':
  735. if (!$is_allowed_to_edit) {
  736. api_not_allowed(true);
  737. }
  738. if (!$lp_found) {
  739. require 'lp_list.php';
  740. } else {
  741. Session::write('refresh', 1);
  742. require 'lp_admin_view.php';
  743. }
  744. break;
  745. case 'auto_launch':
  746. // Redirect to a specific LP
  747. if (api_get_course_setting('enable_lp_auto_launch') == 1) {
  748. if (!$is_allowed_to_edit) {
  749. api_not_allowed(true);
  750. }
  751. if (!$lp_found) {
  752. require 'lp_list.php';
  753. } else {
  754. $_SESSION['oLP']->set_autolaunch($_GET['lp_id'], $_GET['status']);
  755. require 'lp_list.php';
  756. exit;
  757. }
  758. }
  759. break;
  760. case 'build':
  761. if (!$is_allowed_to_edit) {
  762. api_not_allowed(true);
  763. }
  764. if (!$lp_found) {
  765. require 'lp_list.php';
  766. } else {
  767. Session::write('refresh', 1);
  768. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  769. header('Location: '.$url);
  770. exit;
  771. }
  772. break;
  773. case 'edit_item':
  774. if (!$is_allowed_to_edit) {
  775. api_not_allowed(true);
  776. }
  777. if (!$lp_found) {
  778. require 'lp_list.php';
  779. } else {
  780. Session::write('refresh', 1);
  781. if (isset($_POST['submit_button']) && !empty($post_title)) {
  782. // Updating the lp.modified_on
  783. $_SESSION['oLP']->set_modified_on();
  784. // TODO: mp3 edit
  785. $audio = [];
  786. if (isset($_FILES['mp3'])) {
  787. $audio = $_FILES['mp3'];
  788. }
  789. $description = isset($_POST['description']) ? $_POST['description'] : '';
  790. $prerequisites = isset($_POST['prerequisites']) ? $_POST['prerequisites'] : '';
  791. $maxTimeAllowed = isset($_POST['maxTimeAllowed']) ? $_POST['maxTimeAllowed'] : '';
  792. $url = isset($_POST['url']) ? $_POST['url'] : '';
  793. $_SESSION['oLP']->edit_item(
  794. $_REQUEST['id'],
  795. $_POST['parent'],
  796. $_POST['previous'],
  797. $post_title,
  798. $description,
  799. $prerequisites,
  800. $audio,
  801. $maxTimeAllowed,
  802. $url
  803. );
  804. if (isset($_POST['content_lp'])) {
  805. $_SESSION['oLP']->edit_document($_course);
  806. }
  807. Display::addFlash(Display::return_message(get_lang('Updated')));
  808. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  809. header('Location: '.$url);
  810. exit;
  811. }
  812. if (isset($_GET['view']) && $_GET['view'] === 'build') {
  813. require 'lp_edit_item.php';
  814. } else {
  815. require 'lp_admin_view.php';
  816. }
  817. }
  818. break;
  819. case 'edit_item_prereq':
  820. if (!$is_allowed_to_edit) {
  821. api_not_allowed(true);
  822. }
  823. if (!$lp_found) {
  824. require 'lp_list.php';
  825. } else {
  826. if (isset($_POST['submit_button'])) {
  827. // Updating the lp.modified_on
  828. $_SESSION['oLP']->set_modified_on();
  829. Session::write('refresh', 1);
  830. $min = isset($_POST['min_'.$_POST['prerequisites']]) ? $_POST['min_'.$_POST['prerequisites']] : '';
  831. $max = isset($_POST['max_'.$_POST['prerequisites']]) ? $_POST['max_'.$_POST['prerequisites']] : '';
  832. $editPrerequisite = $_SESSION['oLP']->edit_item_prereq(
  833. $_GET['id'],
  834. $_POST['prerequisites'],
  835. $min,
  836. $max
  837. );
  838. Display::addFlash(Display::return_message(get_lang('Updated')));
  839. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  840. header('Location: '.$url);
  841. exit;
  842. } else {
  843. require 'lp_edit_item_prereq.php';
  844. }
  845. }
  846. break;
  847. case 'move_item':
  848. if (!$is_allowed_to_edit) {
  849. api_not_allowed(true);
  850. }
  851. if (!$lp_found) {
  852. require 'lp_list.php';
  853. } else {
  854. Session::write('refresh', 1);
  855. if (isset($_POST['submit_button'])) {
  856. //Updating the lp.modified_on
  857. $_SESSION['oLP']->set_modified_on();
  858. $_SESSION['oLP']->edit_item(
  859. $_GET['id'],
  860. $_POST['parent'],
  861. $_POST['previous'],
  862. $post_title,
  863. $_POST['description']
  864. );
  865. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  866. header('Location: '.$url);
  867. exit;
  868. }
  869. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  870. require 'lp_move_item.php';
  871. } else {
  872. // Avoids weird behaviours see CT#967.
  873. $check = Security::check_token('get');
  874. if ($check) {
  875. $_SESSION['oLP']->move_item($_GET['id'], $_GET['direction']);
  876. }
  877. Security::clear_token();
  878. require 'lp_admin_view.php';
  879. }
  880. }
  881. break;
  882. case 'view_item':
  883. if (!$is_allowed_to_edit) {
  884. api_not_allowed(true);
  885. }
  886. if (!$lp_found) {
  887. require 'lp_list.php';
  888. } else {
  889. Session::write('refresh', 1);
  890. require 'lp_view_item.php';
  891. }
  892. break;
  893. case 'upload':
  894. if (!$is_allowed_to_edit) {
  895. api_not_allowed(true);
  896. }
  897. $cwdir = getcwd();
  898. require 'lp_upload.php';
  899. // Reinit current working directory as many functions in upload change it.
  900. chdir($cwdir);
  901. require 'lp_list.php';
  902. break;
  903. case 'copy':
  904. if (!$is_allowed_to_edit) {
  905. api_not_allowed(true);
  906. }
  907. $hideScormCopyLink = api_get_setting('hide_scorm_copy_link');
  908. if ($hideScormCopyLink === 'true') {
  909. api_not_allowed(true);
  910. }
  911. if (!$lp_found) {
  912. require 'lp_list.php';
  913. } else {
  914. $_SESSION['oLP']->copy();
  915. }
  916. require 'lp_list.php';
  917. break;
  918. case 'export':
  919. if (!$is_allowed_to_edit) {
  920. api_not_allowed(true);
  921. }
  922. $hideScormExportLink = api_get_setting('hide_scorm_export_link');
  923. if ($hideScormExportLink === 'true') {
  924. api_not_allowed(true);
  925. }
  926. if (!$lp_found) {
  927. require 'lp_list.php';
  928. } else {
  929. $_SESSION['oLP']->scormExport();
  930. exit();
  931. }
  932. break;
  933. case 'export_to_pdf':
  934. $hideScormPdfLink = api_get_setting('hide_scorm_pdf_link');
  935. if ($hideScormPdfLink === 'true') {
  936. api_not_allowed(true);
  937. }
  938. // Teachers can export to PDF
  939. if (!$is_allowed_to_edit) {
  940. if (!learnpath::is_lp_visible_for_student($_SESSION['oLP']->lp_id, api_get_user_id(), $_course)) {
  941. api_not_allowed();
  942. }
  943. }
  944. if (!$lp_found) {
  945. require 'lp_list.php';
  946. } else {
  947. $result = $_SESSION['oLP']->scorm_export_to_pdf($_GET['lp_id']);
  948. if (!$result) {
  949. require 'lp_list.php';
  950. }
  951. exit;
  952. }
  953. break;
  954. case 'export_to_course_build':
  955. $allowExport = api_get_configuration_value('allow_lp_chamilo_export');
  956. if (api_is_allowed_to_edit() && $allowExport) {
  957. if (!$lp_found) {
  958. require 'lp_list.php';
  959. } else {
  960. $result = $_SESSION['oLP']->exportToCourseBuildFormat($_GET['lp_id']);
  961. if (!$result) {
  962. require 'lp_list.php';
  963. }
  964. exit;
  965. }
  966. }
  967. require 'lp_list.php';
  968. break;
  969. case 'delete':
  970. if (!$is_allowed_to_edit) {
  971. api_not_allowed(true);
  972. }
  973. if (!$lp_found) {
  974. require 'lp_list.php';
  975. } else {
  976. Session::write('refresh', 1);
  977. $_SESSION['oLP']->delete(null, $_GET['lp_id'], 'remove');
  978. Skill::deleteSkillsFromItem($_GET['lp_id'], ITEM_TYPE_LEARNPATH);
  979. Display::addFlash(Display::return_message(get_lang('Deleted')));
  980. Session::erase('oLP');
  981. require 'lp_list.php';
  982. }
  983. break;
  984. case 'toggle_category_visibility':
  985. if (!$is_allowed_to_edit) {
  986. api_not_allowed(true);
  987. }
  988. learnpath::toggleCategoryVisibility($_REQUEST['id'], $_REQUEST['new_status']);
  989. Display::addFlash(Display::return_message(get_lang('Updated')));
  990. header('Location: '.api_get_self().'?'.api_get_cidreq());
  991. exit;
  992. case 'toggle_visible':
  993. // Change lp visibility (inside lp tool).
  994. if (!$is_allowed_to_edit) {
  995. api_not_allowed(true);
  996. }
  997. if (!$lp_found) {
  998. require 'lp_list.php';
  999. } else {
  1000. learnpath::toggle_visibility($_REQUEST['lp_id'], $_REQUEST['new_status']);
  1001. Display::addFlash(Display::return_message(get_lang('Updated')));
  1002. require 'lp_list.php';
  1003. }
  1004. break;
  1005. case 'toggle_category_publish':
  1006. if (!$is_allowed_to_edit) {
  1007. api_not_allowed(true);
  1008. }
  1009. learnpath::toggleCategoryPublish($_REQUEST['id'], $_REQUEST['new_status']);
  1010. Display::addFlash(Display::return_message(get_lang('Updated')));
  1011. require 'lp_list.php';
  1012. break;
  1013. case 'toggle_publish':
  1014. // Change lp published status (visibility on homepage).
  1015. if (!$is_allowed_to_edit) {
  1016. api_not_allowed(true);
  1017. }
  1018. if (!$lp_found) {
  1019. require 'lp_list.php';
  1020. } else {
  1021. learnpath::toggle_publish($_REQUEST['lp_id'], $_REQUEST['new_status']);
  1022. Display::addFlash(Display::return_message(get_lang('Updated')));
  1023. require 'lp_list.php';
  1024. }
  1025. break;
  1026. case 'move_lp_up':
  1027. // Change lp published status (visibility on homepage)
  1028. if (!$is_allowed_to_edit) {
  1029. api_not_allowed(true);
  1030. }
  1031. if (!$lp_found) {
  1032. require 'lp_list.php';
  1033. } else {
  1034. learnpath::move_up($_REQUEST['lp_id'], $_REQUEST['category_id']);
  1035. Display::addFlash(Display::return_message(get_lang('Updated')));
  1036. require 'lp_list.php';
  1037. }
  1038. break;
  1039. case 'move_lp_down':
  1040. // Change lp published status (visibility on homepage)
  1041. if (!$is_allowed_to_edit) {
  1042. api_not_allowed(true);
  1043. }
  1044. if (!$lp_found) {
  1045. require 'lp_list.php';
  1046. } else {
  1047. learnpath::move_down($_REQUEST['lp_id'], $_REQUEST['category_id']);
  1048. Display::addFlash(Display::return_message(get_lang('Updated')));
  1049. require 'lp_list.php';
  1050. }
  1051. break;
  1052. case 'edit':
  1053. if (!$is_allowed_to_edit) {
  1054. api_not_allowed(true);
  1055. }
  1056. if (!$lp_found) {
  1057. require 'lp_list.php';
  1058. } else {
  1059. Session::write('refresh', 1);
  1060. require 'lp_edit.php';
  1061. }
  1062. break;
  1063. case 'update_lp':
  1064. if (!$is_allowed_to_edit) {
  1065. api_not_allowed(true);
  1066. }
  1067. if (!$lp_found) {
  1068. require 'lp_list.php';
  1069. } else {
  1070. Session::write('refresh', 1);
  1071. $lp_name = Security::remove_XSS($_REQUEST['lp_name']);
  1072. $_SESSION['oLP']->set_name($lp_name);
  1073. $author = $_REQUEST['lp_author'];
  1074. // Fixing the author name (no body or html tags).
  1075. $auth_init = stripos($author, '<p>');
  1076. if ($auth_init === false) {
  1077. $auth_init = stripos($author, '<body>');
  1078. $auth_end = $auth_init + stripos(substr($author, $auth_init + 6), '</body>') + 7;
  1079. $len = $auth_end - $auth_init + 6;
  1080. } else {
  1081. $auth_end = strripos($author, '</p>');
  1082. $len = $auth_end - $auth_init + 4;
  1083. }
  1084. $author_fixed = substr($author, $auth_init, $len);
  1085. $_SESSION['oLP']->set_author($author_fixed);
  1086. // TODO (as of Chamilo 1.8.8): Check in the future whether this field is needed.
  1087. $_SESSION['oLP']->set_encoding($_REQUEST['lp_encoding']);
  1088. if (isset($_REQUEST['lp_maker'])) {
  1089. $_SESSION['oLP']->set_maker($_REQUEST['lp_maker']);
  1090. }
  1091. if (isset($_REQUEST['lp_proximity'])) {
  1092. $_SESSION['oLP']->set_proximity($_REQUEST['lp_proximity']);
  1093. }
  1094. $_SESSION['oLP']->set_theme($_REQUEST['lp_theme']);
  1095. $hide_toc_frame = null;
  1096. if (isset($_REQUEST['hide_toc_frame']) && $_REQUEST['hide_toc_frame'] == 1) {
  1097. $hide_toc_frame = $_REQUEST['hide_toc_frame'];
  1098. }
  1099. $_SESSION['oLP']->set_hide_toc_frame($hide_toc_frame);
  1100. $_SESSION['oLP']->set_prerequisite(isset($_POST['prerequisites']) ? (int) $_POST['prerequisites'] : 0);
  1101. $_SESSION['oLP']->setAccumulateWorkTime(isset($_REQUEST['accumulate_work_time']) ? $_REQUEST['accumulate_work_time'] : 0);
  1102. $_SESSION['oLP']->set_use_max_score(isset($_POST['use_max_score']) ? 1 : 0);
  1103. $subscribeUsers = isset($_REQUEST['subscribe_users']) ? 1 : 0;
  1104. $_SESSION['oLP']->setSubscribeUsers($subscribeUsers);
  1105. $accumulateScormTime = isset($_REQUEST['accumulate_scorm_time']) ? $_REQUEST['accumulate_scorm_time'] : 'true';
  1106. $_SESSION['oLP']->setAccumulateScormTime($accumulateScormTime);
  1107. $publicated_on = null;
  1108. if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
  1109. $publicated_on = $_REQUEST['publicated_on'];
  1110. }
  1111. $expired_on = null;
  1112. if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
  1113. $expired_on = $_REQUEST['expired_on'];
  1114. }
  1115. $_SESSION['oLP']->setCategoryId($_REQUEST['category_id']);
  1116. $_SESSION['oLP']->set_modified_on();
  1117. $_SESSION['oLP']->set_publicated_on($publicated_on);
  1118. $_SESSION['oLP']->set_expired_on($expired_on);
  1119. if (isset($_REQUEST['remove_picture']) && $_REQUEST['remove_picture']) {
  1120. $_SESSION['oLP']->delete_lp_image();
  1121. }
  1122. $extraFieldValue = new ExtraFieldValue('lp');
  1123. $_REQUEST['item_id'] = $_SESSION['oLP']->lp_id;
  1124. $extraFieldValue->saveFieldValues($_REQUEST);
  1125. if ($_FILES['lp_preview_image']['size'] > 0) {
  1126. $_SESSION['oLP']->upload_image($_FILES['lp_preview_image']);
  1127. }
  1128. $form = new FormValidator('form1');
  1129. $form->addSelect('skills', 'skills');
  1130. Skill::saveSkills($form, ITEM_TYPE_LEARNPATH, $_SESSION['oLP']->get_id());
  1131. if (api_get_setting('search_enabled') === 'true') {
  1132. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1133. $specific_fields = get_specific_field_list();
  1134. foreach ($specific_fields as $specific_field) {
  1135. $_SESSION['oLP']->set_terms_by_prefix($_REQUEST[$specific_field['code']], $specific_field['code']);
  1136. $new_values = explode(',', trim($_REQUEST[$specific_field['code']]));
  1137. if (!empty($new_values)) {
  1138. array_walk($new_values, 'trim');
  1139. delete_all_specific_field_value(
  1140. api_get_course_id(),
  1141. $specific_field['id'],
  1142. TOOL_LEARNPATH,
  1143. $_SESSION['oLP']->lp_id
  1144. );
  1145. foreach ($new_values as $value) {
  1146. if (!empty($value)) {
  1147. add_specific_field_value(
  1148. $specific_field['id'],
  1149. api_get_course_id(),
  1150. TOOL_LEARNPATH,
  1151. $_SESSION['oLP']->lp_id,
  1152. $value
  1153. );
  1154. }
  1155. }
  1156. }
  1157. }
  1158. }
  1159. Display::addFlash(Display::return_message(get_lang('Updated')));
  1160. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  1161. header('Location: '.$url);
  1162. exit;
  1163. }
  1164. break;
  1165. case 'add_sub_item':
  1166. // Add an item inside a dir/chapter.
  1167. // @todo check if this is @deprecated
  1168. if (!$is_allowed_to_edit) {
  1169. api_not_allowed(true);
  1170. }
  1171. if (!$lp_found) {
  1172. require 'lp_list.php';
  1173. } else {
  1174. Session::write('refresh', 1);
  1175. if (!empty($_REQUEST['parent_item_id'])) {
  1176. $_SESSION['from_learnpath'] = 'yes';
  1177. $_SESSION['origintoolurl'] = 'lp_controller.php?action=admin_view&lp_id='.intval($_REQUEST['lp_id']);
  1178. } else {
  1179. require 'lp_admin_view.php';
  1180. }
  1181. }
  1182. break;
  1183. case 'deleteitem':
  1184. case 'delete_item':
  1185. if (!$is_allowed_to_edit) {
  1186. api_not_allowed(true);
  1187. }
  1188. if (!$lp_found) {
  1189. require 'lp_list.php';
  1190. } else {
  1191. if (!empty($_REQUEST['id'])) {
  1192. $_SESSION['oLP']->delete_item($_REQUEST['id']);
  1193. }
  1194. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_REQUEST['lp_id']).'&'.api_get_cidreq();
  1195. header('Location: '.$url);
  1196. exit;
  1197. }
  1198. break;
  1199. case 'restart':
  1200. if (!$lp_found) {
  1201. require 'lp_list.php';
  1202. } else {
  1203. $_SESSION['oLP']->restart();
  1204. require 'lp_view.php';
  1205. }
  1206. break;
  1207. case 'last':
  1208. if (!$lp_found) {
  1209. require 'lp_list.php';
  1210. } else {
  1211. $_SESSION['oLP']->last();
  1212. require 'lp_view.php';
  1213. }
  1214. break;
  1215. case 'first':
  1216. if (!$lp_found) {
  1217. require 'lp_list.php';
  1218. } else {
  1219. $_SESSION['oLP']->first();
  1220. require 'lp_view.php';
  1221. }
  1222. break;
  1223. case 'next':
  1224. if (!$lp_found) {
  1225. require 'lp_list.php';
  1226. } else {
  1227. $_SESSION['oLP']->next();
  1228. require 'lp_view.php';
  1229. }
  1230. break;
  1231. case 'previous':
  1232. if (!$lp_found) {
  1233. require 'lp_list.php';
  1234. } else {
  1235. $_SESSION['oLP']->previous();
  1236. require 'lp_view.php';
  1237. }
  1238. break;
  1239. case 'content':
  1240. if (!$lp_found) {
  1241. require 'lp_list.php';
  1242. } else {
  1243. $_SESSION['oLP']->save_last();
  1244. $_SESSION['oLP']->set_current_item($_GET['item_id']);
  1245. $_SESSION['oLP']->start_current_item();
  1246. require 'lp_content.php';
  1247. }
  1248. break;
  1249. case 'view':
  1250. if (!$lp_found) {
  1251. require 'lp_list.php';
  1252. } else {
  1253. if (!empty($_REQUEST['item_id'])) {
  1254. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  1255. }
  1256. require 'lp_view.php';
  1257. }
  1258. break;
  1259. case 'save':
  1260. if (!$lp_found) {
  1261. require 'lp_list.php';
  1262. } else {
  1263. $_SESSION['oLP']->save_item();
  1264. require 'lp_save.php';
  1265. }
  1266. break;
  1267. case 'stats':
  1268. if (!$lp_found) {
  1269. require 'lp_list.php';
  1270. } else {
  1271. $_SESSION['oLP']->save_current();
  1272. $_SESSION['oLP']->save_last();
  1273. $output = require 'lp_stats.php';
  1274. echo $output;
  1275. }
  1276. break;
  1277. case 'list':
  1278. if ($lp_found) {
  1279. Session::write('refresh', 1);
  1280. $_SESSION['oLP']->save_last();
  1281. }
  1282. require 'lp_list.php';
  1283. break;
  1284. case 'mode':
  1285. // Switch between fullscreen and embedded mode.
  1286. $mode = $_REQUEST['mode'];
  1287. if ($mode == 'fullscreen') {
  1288. $_SESSION['oLP']->mode = 'fullscreen';
  1289. } elseif ($mode == 'embedded') {
  1290. $_SESSION['oLP']->mode = 'embedded';
  1291. } elseif ($mode == 'embedframe') {
  1292. $_SESSION['oLP']->mode = 'embedframe';
  1293. } elseif ($mode == 'impress') {
  1294. $_SESSION['oLP']->mode = 'impress';
  1295. }
  1296. require 'lp_view.php';
  1297. break;
  1298. case 'switch_view_mode':
  1299. if (!$lp_found) {
  1300. require 'lp_list.php';
  1301. }
  1302. if (Security::check_token('get')) {
  1303. Session::write('refresh', 1);
  1304. $_SESSION['oLP']->update_default_view_mode();
  1305. }
  1306. require 'lp_list.php';
  1307. break;
  1308. case 'switch_force_commit':
  1309. if (!$lp_found) {
  1310. require 'lp_list.php';
  1311. }
  1312. Session::write('refresh', 1);
  1313. $_SESSION['oLP']->update_default_scorm_commit();
  1314. require 'lp_list.php';
  1315. break;
  1316. case 'switch_attempt_mode':
  1317. if (!$lp_found) {
  1318. require 'lp_list.php';
  1319. }
  1320. Session::write('refresh', 1);
  1321. $_SESSION['oLP']->switch_attempt_mode();
  1322. require 'lp_list.php';
  1323. break;
  1324. case 'switch_scorm_debug':
  1325. if (!$lp_found) {
  1326. require 'lp_list.php';
  1327. }
  1328. Session::write('refresh', 1);
  1329. $_SESSION['oLP']->update_scorm_debug();
  1330. require 'lp_list.php';
  1331. break;
  1332. case 'intro_cmdAdd':
  1333. // Add introduction section page.
  1334. break;
  1335. case 'return_to_course_homepage':
  1336. if (!$lp_found) {
  1337. require 'lp_list.php';
  1338. } else {
  1339. $_SESSION['oLP']->save_current();
  1340. $_SESSION['oLP']->save_last();
  1341. if ($debug > 0) {
  1342. error_log('save_current()');
  1343. error_log('save_last()');
  1344. }
  1345. $url = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php?id_session='.api_get_session_id();
  1346. $redirectTo = isset($_GET['redirectTo']) ? $_GET['redirectTo'] : '';
  1347. switch ($redirectTo) {
  1348. case 'lp_list':
  1349. $url = 'lp_controller.php?'.api_get_cidreq();
  1350. break;
  1351. case 'my_courses':
  1352. $url = api_get_path(WEB_PATH).'user_portal.php';
  1353. break;
  1354. }
  1355. header('location: '.$url);
  1356. exit;
  1357. }
  1358. break;
  1359. case 'search':
  1360. /* Include the search script, it's smart enough to know when we are
  1361. * searching or not.
  1362. */
  1363. require 'lp_list_search.php';
  1364. break;
  1365. case 'impress':
  1366. if (!$lp_found) {
  1367. require 'lp_list.php';
  1368. } else {
  1369. if ($debug > 0) {
  1370. error_log('Trying to impress this LP item to '.$_REQUEST['item_id'], 0);
  1371. }
  1372. if (!empty($_REQUEST['item_id'])) {
  1373. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  1374. }
  1375. require 'lp_impress.php';
  1376. }
  1377. break;
  1378. case 'set_previous_step_as_prerequisite':
  1379. $_SESSION['oLP']->set_previous_step_as_prerequisite_for_all_items();
  1380. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&".api_get_cidreq();
  1381. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  1382. header('Location: '.$url);
  1383. exit;
  1384. break;
  1385. case 'clear_prerequisites':
  1386. $_SESSION['oLP']->clear_prerequisites();
  1387. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&".api_get_cidreq();
  1388. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  1389. header('Location: '.$url);
  1390. exit;
  1391. break;
  1392. case 'toggle_seriousgame':
  1393. // activate/deactive seriousgame_mode
  1394. if (!$is_allowed_to_edit) {
  1395. api_not_allowed(true);
  1396. }
  1397. if (!$lp_found) {
  1398. require 'lp_list.php';
  1399. }
  1400. Session::write('refresh', 1);
  1401. $_SESSION['oLP']->set_seriousgame_mode();
  1402. require 'lp_list.php';
  1403. break;
  1404. case 'create_forum':
  1405. if (!isset($_GET['id'])) {
  1406. break;
  1407. }
  1408. $selectedItem = null;
  1409. foreach ($_SESSION['oLP']->items as $item) {
  1410. if ($item->db_id == $_GET['id']) {
  1411. $selectedItem = $item;
  1412. }
  1413. }
  1414. if (!empty($selectedItem)) {
  1415. $forumThread = $selectedItem->getForumThread(
  1416. $_SESSION['oLP']->course_int_id,
  1417. $_SESSION['oLP']->lp_session_id
  1418. );
  1419. if (empty($forumThread)) {
  1420. require '../forum/forumfunction.inc.php';
  1421. $forumCategory = getForumCategoryByTitle(
  1422. get_lang('LearningPaths'),
  1423. $_SESSION['oLP']->course_int_id,
  1424. $_SESSION['oLP']->lp_session_id
  1425. );
  1426. $forumCategoryId = !empty($forumCategory) ? $forumCategory['cat_id'] : 0;
  1427. if (empty($forumCategoryId)) {
  1428. $forumCategoryId = store_forumcategory(
  1429. [
  1430. 'lp_id' => 0,
  1431. 'forum_category_title' => get_lang('LearningPaths'),
  1432. 'forum_category_comment' => null,
  1433. ],
  1434. [],
  1435. false
  1436. );
  1437. }
  1438. if (!empty($forumCategoryId)) {
  1439. $forum = $_SESSION['oLP']->getForum(
  1440. $_SESSION['oLP']->lp_session_id
  1441. );
  1442. $forumId = !empty($forum) ? $forum['forum_id'] : 0;
  1443. if (empty($forumId)) {
  1444. $forumId = $_SESSION['oLP']->createForum($forumCategoryId);
  1445. }
  1446. if (!empty($forumId)) {
  1447. $selectedItem->createForumThread($forumId);
  1448. }
  1449. }
  1450. }
  1451. }
  1452. header('Location:'.api_get_self().'?'.http_build_query([
  1453. 'action' => 'add_item',
  1454. 'type' => 'step',
  1455. 'lp_id' => $_SESSION['oLP']->lp_id,
  1456. ]));
  1457. exit;
  1458. break;
  1459. case 'report':
  1460. require 'lp_report.php';
  1461. break;
  1462. case 'dissociate_forum':
  1463. if (!isset($_GET['id'])) {
  1464. break;
  1465. }
  1466. $selectedItem = null;
  1467. foreach ($_SESSION['oLP']->items as $item) {
  1468. if ($item->db_id != $_GET['id']) {
  1469. continue;
  1470. }
  1471. $selectedItem = $item;
  1472. }
  1473. if (!empty($selectedItem)) {
  1474. $forumThread = $selectedItem->getForumThread(
  1475. $_SESSION['oLP']->course_int_id,
  1476. $_SESSION['oLP']->lp_session_id
  1477. );
  1478. if (!empty($forumThread)) {
  1479. $dissociated = $selectedItem->dissociateForumThread($forumThread['iid']);
  1480. if ($dissociated) {
  1481. Display::addFlash(
  1482. Display::return_message(get_lang('ForumDissociate'), 'success')
  1483. );
  1484. }
  1485. }
  1486. }
  1487. header('Location:'.api_get_self().'?'.http_build_query([
  1488. 'action' => 'add_item',
  1489. 'type' => 'step',
  1490. 'lp_id' => $_SESSION['oLP']->lp_id,
  1491. ]));
  1492. exit;
  1493. break;
  1494. case 'add_final_item':
  1495. if (!$lp_found) {
  1496. Display::addFlash(
  1497. Display::return_message(get_lang('NoLPFound'), 'error')
  1498. );
  1499. break;
  1500. }
  1501. Session::write('refresh', 1);
  1502. if (!isset($_POST['submit']) || empty($post_title)) {
  1503. break;
  1504. }
  1505. $_SESSION['oLP']->getFinalItemForm();
  1506. $redirectTo = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  1507. 'action' => 'add_item',
  1508. 'type' => 'step',
  1509. 'lp_id' => intval($_SESSION['oLP']->lp_id),
  1510. ]);
  1511. break;
  1512. default:
  1513. require 'lp_list.php';
  1514. break;
  1515. }
  1516. if (!empty($_SESSION['oLP'])) {
  1517. $_SESSION['lpobject'] = serialize($_SESSION['oLP']);
  1518. if ($debug > 0) {
  1519. error_log('lpobject is serialized in session', 0);
  1520. }
  1521. }
  1522. if (!empty($redirectTo)) {
  1523. header("Location: $redirectTo");
  1524. exit;
  1525. }