lp_controller.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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. if (api_get_configuration_value('save_titles_as_html')) {
  452. $post_title = $_POST['title'];
  453. }
  454. }
  455. }
  456. $redirectTo = '';
  457. if ($debug > 0) {
  458. error_log('action "'.$action.'" triggered');
  459. }
  460. switch ($action) {
  461. case 'send_notify_teacher':
  462. // Send notification to the teacher
  463. $studentInfo = api_get_user_info();
  464. $course_info = api_get_course_info();
  465. $sessionId = api_get_session_id();
  466. $courseName = $course_info['title'];
  467. $courseUrl = $course_info['course_public_url'];
  468. if (!empty($sessionId)) {
  469. $sessionInfo = api_get_session_info($sessionId);
  470. $courseName = $sessionInfo['name'];
  471. $courseUrl .= '?id_session='.$sessionId;
  472. }
  473. $url = Display::url($courseName, $courseUrl, ['title' => get_lang('GoToCourse')]);
  474. $coachList = CourseManager::get_coachs_from_course($sessionId, api_get_course_int_id());
  475. foreach ($coachList as $coach_course) {
  476. $recipientName = $coach_course['full_name'];
  477. $coachInfo = api_get_user_info($coach_course['user_id']);
  478. if (empty($coachInfo)) {
  479. continue;
  480. }
  481. $email = $coachInfo['email'];
  482. $tplContent = new Template(null, false, false, false, false, false);
  483. $tplContent->assign('name_teacher', $recipientName);
  484. $tplContent->assign('name_student', $studentInfo['complete_name']);
  485. $tplContent->assign('course_name', $courseName);
  486. $tplContent->assign('course_url', $url);
  487. $layoutContent = $tplContent->get_template('mail/content_ending_learnpath.tpl');
  488. $emailBody = $tplContent->fetch($layoutContent);
  489. api_mail_html(
  490. $recipientName,
  491. $email,
  492. sprintf(get_lang('StudentXFinishedLp'), $studentInfo['complete_name']),
  493. $emailBody,
  494. $studentInfo['complete_name'],
  495. $studentInfo['email'],
  496. true
  497. );
  498. }
  499. Display::addFlash(Display::return_message(get_lang('MessageSent')));
  500. $url = api_get_self().'?action=list&'.api_get_cidreq();
  501. header('Location: '.$url);
  502. exit;
  503. break;
  504. case 'add_item':
  505. if (!$is_allowed_to_edit) {
  506. api_not_allowed(true);
  507. }
  508. if (!$lp_found) {
  509. // Check if the learnpath ID was defined, otherwise send back to list
  510. require 'lp_list.php';
  511. } else {
  512. Session::write('refresh', 1);
  513. if (isset($_POST['submit_button']) && !empty($post_title)) {
  514. // If a title was submitted:
  515. // Updating the lp.modified_on
  516. $_SESSION['oLP']->set_modified_on();
  517. if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_POST['post_time']) {
  518. // Check post_time to ensure ??? (counter-hacking measure?)
  519. require 'lp_add_item.php';
  520. } else {
  521. Session::write('post_time', $_POST['post_time']);
  522. $directoryParentId = isset($_POST['directory_parent_id']) ? $_POST['directory_parent_id'] : 0;
  523. $courseInfo = api_get_course_info();
  524. if (empty($directoryParentId)) {
  525. $_SESSION['oLP']->generate_lp_folder($courseInfo);
  526. }
  527. $parent = isset($_POST['parent']) ? $_POST['parent'] : '';
  528. $previous = isset($_POST['previous']) ? $_POST['previous'] : '';
  529. $type = isset($_POST['type']) ? $_POST['type'] : '';
  530. $path = isset($_POST['path']) ? $_POST['path'] : '';
  531. $description = isset($_POST['description']) ? $_POST['description'] : '';
  532. $prerequisites = isset($_POST['prerequisites']) ? $_POST['prerequisites'] : '';
  533. $maxTimeAllowed = isset($_POST['maxTimeAllowed']) ? $_POST['maxTimeAllowed'] : '';
  534. if ($_POST['type'] == TOOL_DOCUMENT) {
  535. if (isset($_POST['path']) && $_GET['edit'] != 'true') {
  536. $document_id = $_POST['path'];
  537. } else {
  538. if ($_POST['content_lp']) {
  539. $document_id = $_SESSION['oLP']->create_document(
  540. $_course,
  541. $_POST['content_lp'],
  542. $_POST['title'],
  543. 'html',
  544. $directoryParentId
  545. );
  546. }
  547. }
  548. $_SESSION['oLP']->add_item(
  549. $parent,
  550. $previous,
  551. $type,
  552. $document_id,
  553. $post_title,
  554. $description,
  555. $prerequisites
  556. );
  557. } elseif ($_POST['type'] == TOOL_READOUT_TEXT) {
  558. if (isset($_POST['path']) && $_GET['edit'] != 'true') {
  559. $document_id = $_POST['path'];
  560. } else {
  561. $document_id = $_SESSION['oLP']->createReadOutText(
  562. $_course,
  563. $_POST['content_lp'],
  564. $_POST['title'],
  565. $directoryParentId
  566. );
  567. }
  568. $_SESSION['oLP']->add_item(
  569. $parent,
  570. $previous,
  571. TOOL_READOUT_TEXT,
  572. $document_id,
  573. $post_title,
  574. $description,
  575. $prerequisites
  576. );
  577. } else {
  578. // For all other item types than documents,
  579. // load the item using the item type and path rather than its ID.
  580. $_SESSION['oLP']->add_item(
  581. $parent,
  582. $previous,
  583. $type,
  584. $path,
  585. $post_title,
  586. $description,
  587. $prerequisites,
  588. $maxTimeAllowed
  589. );
  590. }
  591. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  592. header('Location: '.$url);
  593. exit;
  594. }
  595. } else {
  596. require 'lp_add_item.php';
  597. }
  598. }
  599. break;
  600. case 'add_users_to_category':
  601. if (!$is_allowed_to_edit) {
  602. api_not_allowed(true);
  603. }
  604. require 'lp_subscribe_users_to_category.php';
  605. break;
  606. case 'add_audio':
  607. if (!$is_allowed_to_edit) {
  608. api_not_allowed(true);
  609. }
  610. if (!$lp_found) {
  611. // Check if the learnpath ID was defined, otherwise send back to list
  612. require 'lp_list.php';
  613. } else {
  614. Session::write('refresh', 1);
  615. if (isset($_REQUEST['id'])) {
  616. $lp_item_obj = new learnpathItem($_REQUEST['id']);
  617. // Remove audio
  618. if (isset($_GET['delete_file']) && $_GET['delete_file'] == 1) {
  619. $lp_item_obj->remove_audio();
  620. $url = api_get_self().'?action=add_audio&lp_id='.intval($_SESSION['oLP']->lp_id).'&id='.$lp_item_obj->get_id().'&'.api_get_cidreq();
  621. header('Location: '.$url);
  622. exit;
  623. }
  624. // Upload audio
  625. if (isset($_FILES['file']) && !empty($_FILES['file'])) {
  626. // Updating the lp.modified_on
  627. $_SESSION['oLP']->set_modified_on();
  628. $lp_item_obj->add_audio();
  629. }
  630. //Add audio file from documents
  631. if (isset($_REQUEST['document_id']) && !empty($_REQUEST['document_id'])) {
  632. $_SESSION['oLP']->set_modified_on();
  633. $lp_item_obj->add_audio_from_documents($_REQUEST['document_id']);
  634. }
  635. // Display.
  636. require 'lp_add_audio.php';
  637. } else {
  638. require 'lp_add_audio.php';
  639. }
  640. }
  641. break;
  642. case 'add_lp_category':
  643. if (!$is_allowed_to_edit) {
  644. api_not_allowed(true);
  645. }
  646. require 'lp_add_category.php';
  647. break;
  648. case 'move_up_category':
  649. if (!$is_allowed_to_edit) {
  650. api_not_allowed(true);
  651. }
  652. if (isset($_REQUEST['id'])) {
  653. learnpath::moveUpCategory($_REQUEST['id']);
  654. }
  655. require 'lp_list.php';
  656. break;
  657. case 'move_down_category':
  658. if (!$is_allowed_to_edit) {
  659. api_not_allowed(true);
  660. }
  661. if (isset($_REQUEST['id'])) {
  662. learnpath::moveDownCategory($_REQUEST['id']);
  663. }
  664. require 'lp_list.php';
  665. break;
  666. case 'delete_lp_category':
  667. if (!$is_allowed_to_edit) {
  668. api_not_allowed(true);
  669. }
  670. if (isset($_REQUEST['id'])) {
  671. $result = learnpath::deleteCategory($_REQUEST['id']);
  672. if ($result) {
  673. Display::addFlash(Display::return_message(get_lang('Deleted')));
  674. }
  675. }
  676. require 'lp_list.php';
  677. break;
  678. case 'add_lp':
  679. if (!$is_allowed_to_edit) {
  680. api_not_allowed(true);
  681. }
  682. if (isset($_REQUEST['lp_name']) && !empty($_REQUEST['lp_name'])) {
  683. $_REQUEST['lp_name'] = trim($_REQUEST['lp_name']);
  684. Session::write('refresh', 1);
  685. if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_REQUEST['post_time']) {
  686. require 'lp_add.php';
  687. } else {
  688. Session::write('post_time', $_POST['post_time']);
  689. $publicated_on = null;
  690. if (isset($_REQUEST['activate_start_date_check']) &&
  691. $_REQUEST['activate_start_date_check'] == 1
  692. ) {
  693. $publicated_on = $_REQUEST['publicated_on'];
  694. }
  695. $expired_on = null;
  696. if (isset($_REQUEST['activate_end_date_check']) &&
  697. $_REQUEST['activate_end_date_check'] == 1
  698. ) {
  699. $expired_on = $_REQUEST['expired_on'];
  700. }
  701. $new_lp_id = learnpath::add_lp(
  702. api_get_course_id(),
  703. Security::remove_XSS($_REQUEST['lp_name']),
  704. '',
  705. 'chamilo',
  706. 'manual',
  707. '',
  708. $publicated_on,
  709. $expired_on,
  710. $_REQUEST['category_id']
  711. );
  712. if (is_numeric($new_lp_id)) {
  713. // Create temp form validator to save skills
  714. $form = new FormValidator('lp_add');
  715. $form->addSelect('skills', 'skills');
  716. Skill::saveSkills($form, ITEM_TYPE_LEARNPATH, $new_lp_id);
  717. $extraFieldValue = new ExtraFieldValue('lp');
  718. $_REQUEST['item_id'] = $new_lp_id;
  719. $extraFieldValue->saveFieldValues($_REQUEST);
  720. // TODO: Maybe create a first directory directly to avoid bugging the user with useless queries
  721. $_SESSION['oLP'] = new learnpath(
  722. api_get_course_id(),
  723. $new_lp_id,
  724. api_get_user_id()
  725. );
  726. $accumulateScormTime = isset($_REQUEST['accumulate_scorm_time']) ? $_REQUEST['accumulate_scorm_time'] : 'true';
  727. $_SESSION['oLP']->setAccumulateScormTime($accumulateScormTime);
  728. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($new_lp_id).'&'.api_get_cidreq();
  729. header("Location: $url&isStudentView=false");
  730. exit;
  731. }
  732. }
  733. } else {
  734. require 'lp_add.php';
  735. }
  736. break;
  737. case 'admin_view':
  738. if (!$is_allowed_to_edit) {
  739. api_not_allowed(true);
  740. }
  741. if (!$lp_found) {
  742. require 'lp_list.php';
  743. } else {
  744. Session::write('refresh', 1);
  745. require 'lp_admin_view.php';
  746. }
  747. break;
  748. case 'auto_launch':
  749. // Redirect to a specific LP
  750. if (api_get_course_setting('enable_lp_auto_launch') == 1) {
  751. if (!$is_allowed_to_edit) {
  752. api_not_allowed(true);
  753. }
  754. if (!$lp_found) {
  755. require 'lp_list.php';
  756. } else {
  757. $_SESSION['oLP']->set_autolaunch($_GET['lp_id'], $_GET['status']);
  758. require 'lp_list.php';
  759. exit;
  760. }
  761. }
  762. break;
  763. case 'build':
  764. if (!$is_allowed_to_edit) {
  765. api_not_allowed(true);
  766. }
  767. if (!$lp_found) {
  768. require 'lp_list.php';
  769. } else {
  770. Session::write('refresh', 1);
  771. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  772. header('Location: '.$url);
  773. exit;
  774. }
  775. break;
  776. case 'edit_item':
  777. if (!$is_allowed_to_edit) {
  778. api_not_allowed(true);
  779. }
  780. if (!$lp_found) {
  781. require 'lp_list.php';
  782. } else {
  783. Session::write('refresh', 1);
  784. if (isset($_POST['submit_button']) && !empty($post_title)) {
  785. // Updating the lp.modified_on
  786. $_SESSION['oLP']->set_modified_on();
  787. // TODO: mp3 edit
  788. $audio = [];
  789. if (isset($_FILES['mp3'])) {
  790. $audio = $_FILES['mp3'];
  791. }
  792. $description = isset($_POST['description']) ? $_POST['description'] : '';
  793. $prerequisites = isset($_POST['prerequisites']) ? $_POST['prerequisites'] : '';
  794. $maxTimeAllowed = isset($_POST['maxTimeAllowed']) ? $_POST['maxTimeAllowed'] : '';
  795. $url = isset($_POST['url']) ? $_POST['url'] : '';
  796. $_SESSION['oLP']->edit_item(
  797. $_REQUEST['id'],
  798. $_POST['parent'],
  799. $_POST['previous'],
  800. $post_title,
  801. $description,
  802. $prerequisites,
  803. $audio,
  804. $maxTimeAllowed,
  805. $url
  806. );
  807. if (isset($_POST['content_lp'])) {
  808. $_SESSION['oLP']->edit_document($_course);
  809. }
  810. Display::addFlash(Display::return_message(get_lang('Updated')));
  811. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  812. header('Location: '.$url);
  813. exit;
  814. }
  815. if (isset($_GET['view']) && $_GET['view'] === 'build') {
  816. require 'lp_edit_item.php';
  817. } else {
  818. require 'lp_admin_view.php';
  819. }
  820. }
  821. break;
  822. case 'edit_item_prereq':
  823. if (!$is_allowed_to_edit) {
  824. api_not_allowed(true);
  825. }
  826. if (!$lp_found) {
  827. require 'lp_list.php';
  828. } else {
  829. if (isset($_POST['submit_button'])) {
  830. // Updating the lp.modified_on
  831. $_SESSION['oLP']->set_modified_on();
  832. Session::write('refresh', 1);
  833. $min = isset($_POST['min_'.$_POST['prerequisites']]) ? $_POST['min_'.$_POST['prerequisites']] : '';
  834. $max = isset($_POST['max_'.$_POST['prerequisites']]) ? $_POST['max_'.$_POST['prerequisites']] : '';
  835. $editPrerequisite = $_SESSION['oLP']->edit_item_prereq(
  836. $_GET['id'],
  837. $_POST['prerequisites'],
  838. $min,
  839. $max
  840. );
  841. Display::addFlash(Display::return_message(get_lang('Updated')));
  842. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  843. header('Location: '.$url);
  844. exit;
  845. } else {
  846. require 'lp_edit_item_prereq.php';
  847. }
  848. }
  849. break;
  850. case 'move_item':
  851. if (!$is_allowed_to_edit) {
  852. api_not_allowed(true);
  853. }
  854. if (!$lp_found) {
  855. require 'lp_list.php';
  856. } else {
  857. Session::write('refresh', 1);
  858. if (isset($_POST['submit_button'])) {
  859. //Updating the lp.modified_on
  860. $_SESSION['oLP']->set_modified_on();
  861. $_SESSION['oLP']->edit_item(
  862. $_GET['id'],
  863. $_POST['parent'],
  864. $_POST['previous'],
  865. $post_title,
  866. $_POST['description']
  867. );
  868. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  869. header('Location: '.$url);
  870. exit;
  871. }
  872. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  873. require 'lp_move_item.php';
  874. } else {
  875. // Avoids weird behaviours see CT#967.
  876. $check = Security::check_token('get');
  877. if ($check) {
  878. $_SESSION['oLP']->move_item($_GET['id'], $_GET['direction']);
  879. }
  880. Security::clear_token();
  881. require 'lp_admin_view.php';
  882. }
  883. }
  884. break;
  885. case 'view_item':
  886. if (!$is_allowed_to_edit) {
  887. api_not_allowed(true);
  888. }
  889. if (!$lp_found) {
  890. require 'lp_list.php';
  891. } else {
  892. Session::write('refresh', 1);
  893. require 'lp_view_item.php';
  894. }
  895. break;
  896. case 'upload':
  897. if (!$is_allowed_to_edit) {
  898. api_not_allowed(true);
  899. }
  900. $cwdir = getcwd();
  901. require 'lp_upload.php';
  902. // Reinit current working directory as many functions in upload change it.
  903. chdir($cwdir);
  904. require 'lp_list.php';
  905. break;
  906. case 'copy':
  907. if (!$is_allowed_to_edit) {
  908. api_not_allowed(true);
  909. }
  910. $hideScormCopyLink = api_get_setting('hide_scorm_copy_link');
  911. if ($hideScormCopyLink === 'true') {
  912. api_not_allowed(true);
  913. }
  914. if (!$lp_found) {
  915. require 'lp_list.php';
  916. } else {
  917. $_SESSION['oLP']->copy();
  918. }
  919. require 'lp_list.php';
  920. break;
  921. case 'export':
  922. if (!$is_allowed_to_edit) {
  923. api_not_allowed(true);
  924. }
  925. $hideScormExportLink = api_get_setting('hide_scorm_export_link');
  926. if ($hideScormExportLink === 'true') {
  927. api_not_allowed(true);
  928. }
  929. if (!$lp_found) {
  930. require 'lp_list.php';
  931. } else {
  932. $_SESSION['oLP']->scormExport();
  933. exit();
  934. }
  935. break;
  936. case 'export_to_pdf':
  937. $hideScormPdfLink = api_get_setting('hide_scorm_pdf_link');
  938. if ($hideScormPdfLink === 'true') {
  939. api_not_allowed(true);
  940. }
  941. // Teachers can export to PDF
  942. if (!$is_allowed_to_edit) {
  943. if (!learnpath::is_lp_visible_for_student($_SESSION['oLP']->lp_id, api_get_user_id(), $_course)) {
  944. api_not_allowed();
  945. }
  946. }
  947. if (!$lp_found) {
  948. require 'lp_list.php';
  949. } else {
  950. $result = $_SESSION['oLP']->scorm_export_to_pdf($_GET['lp_id']);
  951. if (!$result) {
  952. require 'lp_list.php';
  953. }
  954. exit;
  955. }
  956. break;
  957. case 'export_to_course_build':
  958. $allowExport = api_get_configuration_value('allow_lp_chamilo_export');
  959. if (api_is_allowed_to_edit() && $allowExport) {
  960. if (!$lp_found) {
  961. require 'lp_list.php';
  962. } else {
  963. $result = $_SESSION['oLP']->exportToCourseBuildFormat($_GET['lp_id']);
  964. if (!$result) {
  965. require 'lp_list.php';
  966. }
  967. exit;
  968. }
  969. }
  970. require 'lp_list.php';
  971. break;
  972. case 'delete':
  973. if (!$is_allowed_to_edit) {
  974. api_not_allowed(true);
  975. }
  976. if (!$lp_found) {
  977. require 'lp_list.php';
  978. } else {
  979. Session::write('refresh', 1);
  980. $_SESSION['oLP']->delete(null, $_GET['lp_id'], 'remove');
  981. Skill::deleteSkillsFromItem($_GET['lp_id'], ITEM_TYPE_LEARNPATH);
  982. Display::addFlash(Display::return_message(get_lang('Deleted')));
  983. Session::erase('oLP');
  984. require 'lp_list.php';
  985. }
  986. break;
  987. case 'toggle_category_visibility':
  988. if (!$is_allowed_to_edit) {
  989. api_not_allowed(true);
  990. }
  991. learnpath::toggleCategoryVisibility($_REQUEST['id'], $_REQUEST['new_status']);
  992. Display::addFlash(Display::return_message(get_lang('Updated')));
  993. header('Location: '.api_get_self().'?'.api_get_cidreq());
  994. exit;
  995. case 'toggle_visible':
  996. // Change lp visibility (inside lp tool).
  997. if (!$is_allowed_to_edit) {
  998. api_not_allowed(true);
  999. }
  1000. if (!$lp_found) {
  1001. require 'lp_list.php';
  1002. } else {
  1003. learnpath::toggle_visibility($_REQUEST['lp_id'], $_REQUEST['new_status']);
  1004. Display::addFlash(Display::return_message(get_lang('Updated')));
  1005. require 'lp_list.php';
  1006. }
  1007. break;
  1008. case 'toggle_category_publish':
  1009. if (!$is_allowed_to_edit) {
  1010. api_not_allowed(true);
  1011. }
  1012. learnpath::toggleCategoryPublish($_REQUEST['id'], $_REQUEST['new_status']);
  1013. Display::addFlash(Display::return_message(get_lang('Updated')));
  1014. require 'lp_list.php';
  1015. break;
  1016. case 'toggle_publish':
  1017. // Change lp published status (visibility on homepage).
  1018. if (!$is_allowed_to_edit) {
  1019. api_not_allowed(true);
  1020. }
  1021. if (!$lp_found) {
  1022. require 'lp_list.php';
  1023. } else {
  1024. learnpath::toggle_publish($_REQUEST['lp_id'], $_REQUEST['new_status']);
  1025. Display::addFlash(Display::return_message(get_lang('Updated')));
  1026. require 'lp_list.php';
  1027. }
  1028. break;
  1029. case 'move_lp_up':
  1030. // Change lp published status (visibility on homepage)
  1031. if (!$is_allowed_to_edit) {
  1032. api_not_allowed(true);
  1033. }
  1034. if (!$lp_found) {
  1035. require 'lp_list.php';
  1036. } else {
  1037. learnpath::move_up($_REQUEST['lp_id'], $_REQUEST['category_id']);
  1038. Display::addFlash(Display::return_message(get_lang('Updated')));
  1039. require 'lp_list.php';
  1040. }
  1041. break;
  1042. case 'move_lp_down':
  1043. // Change lp published status (visibility on homepage)
  1044. if (!$is_allowed_to_edit) {
  1045. api_not_allowed(true);
  1046. }
  1047. if (!$lp_found) {
  1048. require 'lp_list.php';
  1049. } else {
  1050. learnpath::move_down($_REQUEST['lp_id'], $_REQUEST['category_id']);
  1051. Display::addFlash(Display::return_message(get_lang('Updated')));
  1052. require 'lp_list.php';
  1053. }
  1054. break;
  1055. case 'edit':
  1056. if (!$is_allowed_to_edit) {
  1057. api_not_allowed(true);
  1058. }
  1059. if (!$lp_found) {
  1060. require 'lp_list.php';
  1061. } else {
  1062. Session::write('refresh', 1);
  1063. require 'lp_edit.php';
  1064. }
  1065. break;
  1066. case 'update_lp':
  1067. if (!$is_allowed_to_edit) {
  1068. api_not_allowed(true);
  1069. }
  1070. if (!$lp_found) {
  1071. require 'lp_list.php';
  1072. } else {
  1073. Session::write('refresh', 1);
  1074. $lp_name = Security::remove_XSS($_REQUEST['lp_name']);
  1075. $_SESSION['oLP']->set_name($lp_name);
  1076. $author = $_REQUEST['lp_author'];
  1077. // Fixing the author name (no body or html tags).
  1078. $auth_init = stripos($author, '<p>');
  1079. if ($auth_init === false) {
  1080. $auth_init = stripos($author, '<body>');
  1081. $auth_end = $auth_init + stripos(substr($author, $auth_init + 6), '</body>') + 7;
  1082. $len = $auth_end - $auth_init + 6;
  1083. } else {
  1084. $auth_end = strripos($author, '</p>');
  1085. $len = $auth_end - $auth_init + 4;
  1086. }
  1087. $author_fixed = substr($author, $auth_init, $len);
  1088. $_SESSION['oLP']->set_author($author_fixed);
  1089. // TODO (as of Chamilo 1.8.8): Check in the future whether this field is needed.
  1090. $_SESSION['oLP']->set_encoding($_REQUEST['lp_encoding']);
  1091. if (isset($_REQUEST['lp_maker'])) {
  1092. $_SESSION['oLP']->set_maker($_REQUEST['lp_maker']);
  1093. }
  1094. if (isset($_REQUEST['lp_proximity'])) {
  1095. $_SESSION['oLP']->set_proximity($_REQUEST['lp_proximity']);
  1096. }
  1097. $_SESSION['oLP']->set_theme($_REQUEST['lp_theme']);
  1098. $hide_toc_frame = null;
  1099. if (isset($_REQUEST['hide_toc_frame']) && $_REQUEST['hide_toc_frame'] == 1) {
  1100. $hide_toc_frame = $_REQUEST['hide_toc_frame'];
  1101. }
  1102. $_SESSION['oLP']->set_hide_toc_frame($hide_toc_frame);
  1103. $_SESSION['oLP']->set_prerequisite(isset($_POST['prerequisites']) ? (int) $_POST['prerequisites'] : 0);
  1104. $_SESSION['oLP']->setAccumulateWorkTime(isset($_REQUEST['accumulate_work_time']) ? $_REQUEST['accumulate_work_time'] : 0);
  1105. $_SESSION['oLP']->set_use_max_score(isset($_POST['use_max_score']) ? 1 : 0);
  1106. $subscribeUsers = isset($_REQUEST['subscribe_users']) ? 1 : 0;
  1107. $_SESSION['oLP']->setSubscribeUsers($subscribeUsers);
  1108. $accumulateScormTime = isset($_REQUEST['accumulate_scorm_time']) ? $_REQUEST['accumulate_scorm_time'] : 'true';
  1109. $_SESSION['oLP']->setAccumulateScormTime($accumulateScormTime);
  1110. $publicated_on = null;
  1111. if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
  1112. $publicated_on = $_REQUEST['publicated_on'];
  1113. }
  1114. $expired_on = null;
  1115. if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
  1116. $expired_on = $_REQUEST['expired_on'];
  1117. }
  1118. $_SESSION['oLP']->setCategoryId($_REQUEST['category_id']);
  1119. $_SESSION['oLP']->set_modified_on();
  1120. $_SESSION['oLP']->set_publicated_on($publicated_on);
  1121. $_SESSION['oLP']->set_expired_on($expired_on);
  1122. if (isset($_REQUEST['remove_picture']) && $_REQUEST['remove_picture']) {
  1123. $_SESSION['oLP']->delete_lp_image();
  1124. }
  1125. $extraFieldValue = new ExtraFieldValue('lp');
  1126. $_REQUEST['item_id'] = $_SESSION['oLP']->lp_id;
  1127. $extraFieldValue->saveFieldValues($_REQUEST);
  1128. if ($_FILES['lp_preview_image']['size'] > 0) {
  1129. $_SESSION['oLP']->upload_image($_FILES['lp_preview_image']);
  1130. }
  1131. $form = new FormValidator('form1');
  1132. $form->addSelect('skills', 'skills');
  1133. Skill::saveSkills($form, ITEM_TYPE_LEARNPATH, $_SESSION['oLP']->get_id());
  1134. if (api_get_setting('search_enabled') === 'true') {
  1135. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1136. $specific_fields = get_specific_field_list();
  1137. foreach ($specific_fields as $specific_field) {
  1138. $_SESSION['oLP']->set_terms_by_prefix($_REQUEST[$specific_field['code']], $specific_field['code']);
  1139. $new_values = explode(',', trim($_REQUEST[$specific_field['code']]));
  1140. if (!empty($new_values)) {
  1141. array_walk($new_values, 'trim');
  1142. delete_all_specific_field_value(
  1143. api_get_course_id(),
  1144. $specific_field['id'],
  1145. TOOL_LEARNPATH,
  1146. $_SESSION['oLP']->lp_id
  1147. );
  1148. foreach ($new_values as $value) {
  1149. if (!empty($value)) {
  1150. add_specific_field_value(
  1151. $specific_field['id'],
  1152. api_get_course_id(),
  1153. TOOL_LEARNPATH,
  1154. $_SESSION['oLP']->lp_id,
  1155. $value
  1156. );
  1157. }
  1158. }
  1159. }
  1160. }
  1161. }
  1162. Display::addFlash(Display::return_message(get_lang('Updated')));
  1163. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  1164. header('Location: '.$url);
  1165. exit;
  1166. }
  1167. break;
  1168. case 'add_sub_item':
  1169. // Add an item inside a dir/chapter.
  1170. // @todo check if this is @deprecated
  1171. if (!$is_allowed_to_edit) {
  1172. api_not_allowed(true);
  1173. }
  1174. if (!$lp_found) {
  1175. require 'lp_list.php';
  1176. } else {
  1177. Session::write('refresh', 1);
  1178. if (!empty($_REQUEST['parent_item_id'])) {
  1179. $_SESSION['from_learnpath'] = 'yes';
  1180. $_SESSION['origintoolurl'] = 'lp_controller.php?action=admin_view&lp_id='.intval($_REQUEST['lp_id']);
  1181. } else {
  1182. require 'lp_admin_view.php';
  1183. }
  1184. }
  1185. break;
  1186. case 'deleteitem':
  1187. case 'delete_item':
  1188. if (!$is_allowed_to_edit) {
  1189. api_not_allowed(true);
  1190. }
  1191. if (!$lp_found) {
  1192. require 'lp_list.php';
  1193. } else {
  1194. if (!empty($_REQUEST['id'])) {
  1195. $_SESSION['oLP']->delete_item($_REQUEST['id']);
  1196. }
  1197. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_REQUEST['lp_id']).'&'.api_get_cidreq();
  1198. header('Location: '.$url);
  1199. exit;
  1200. }
  1201. break;
  1202. case 'restart':
  1203. if (!$lp_found) {
  1204. require 'lp_list.php';
  1205. } else {
  1206. $_SESSION['oLP']->restart();
  1207. require 'lp_view.php';
  1208. }
  1209. break;
  1210. case 'last':
  1211. if (!$lp_found) {
  1212. require 'lp_list.php';
  1213. } else {
  1214. $_SESSION['oLP']->last();
  1215. require 'lp_view.php';
  1216. }
  1217. break;
  1218. case 'first':
  1219. if (!$lp_found) {
  1220. require 'lp_list.php';
  1221. } else {
  1222. $_SESSION['oLP']->first();
  1223. require 'lp_view.php';
  1224. }
  1225. break;
  1226. case 'next':
  1227. if (!$lp_found) {
  1228. require 'lp_list.php';
  1229. } else {
  1230. $_SESSION['oLP']->next();
  1231. require 'lp_view.php';
  1232. }
  1233. break;
  1234. case 'previous':
  1235. if (!$lp_found) {
  1236. require 'lp_list.php';
  1237. } else {
  1238. $_SESSION['oLP']->previous();
  1239. require 'lp_view.php';
  1240. }
  1241. break;
  1242. case 'content':
  1243. if (!$lp_found) {
  1244. require 'lp_list.php';
  1245. } else {
  1246. $_SESSION['oLP']->save_last();
  1247. $_SESSION['oLP']->set_current_item($_GET['item_id']);
  1248. $_SESSION['oLP']->start_current_item();
  1249. require 'lp_content.php';
  1250. }
  1251. break;
  1252. case 'view':
  1253. if (!$lp_found) {
  1254. require 'lp_list.php';
  1255. } else {
  1256. if (!empty($_REQUEST['item_id'])) {
  1257. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  1258. }
  1259. require 'lp_view.php';
  1260. }
  1261. break;
  1262. case 'save':
  1263. if (!$lp_found) {
  1264. require 'lp_list.php';
  1265. } else {
  1266. $_SESSION['oLP']->save_item();
  1267. require 'lp_save.php';
  1268. }
  1269. break;
  1270. case 'stats':
  1271. if (!$lp_found) {
  1272. require 'lp_list.php';
  1273. } else {
  1274. $_SESSION['oLP']->save_current();
  1275. $_SESSION['oLP']->save_last();
  1276. $output = require 'lp_stats.php';
  1277. echo $output;
  1278. }
  1279. break;
  1280. case 'list':
  1281. if ($lp_found) {
  1282. Session::write('refresh', 1);
  1283. $_SESSION['oLP']->save_last();
  1284. }
  1285. require 'lp_list.php';
  1286. break;
  1287. case 'mode':
  1288. // Switch between fullscreen and embedded mode.
  1289. $mode = $_REQUEST['mode'];
  1290. if ($mode == 'fullscreen') {
  1291. $_SESSION['oLP']->mode = 'fullscreen';
  1292. } elseif ($mode == 'embedded') {
  1293. $_SESSION['oLP']->mode = 'embedded';
  1294. } elseif ($mode == 'embedframe') {
  1295. $_SESSION['oLP']->mode = 'embedframe';
  1296. } elseif ($mode == 'impress') {
  1297. $_SESSION['oLP']->mode = 'impress';
  1298. }
  1299. require 'lp_view.php';
  1300. break;
  1301. case 'switch_view_mode':
  1302. if (!$lp_found) {
  1303. require 'lp_list.php';
  1304. }
  1305. if (Security::check_token('get')) {
  1306. Session::write('refresh', 1);
  1307. $_SESSION['oLP']->update_default_view_mode();
  1308. }
  1309. require 'lp_list.php';
  1310. break;
  1311. case 'switch_force_commit':
  1312. if (!$lp_found) {
  1313. require 'lp_list.php';
  1314. }
  1315. Session::write('refresh', 1);
  1316. $_SESSION['oLP']->update_default_scorm_commit();
  1317. require 'lp_list.php';
  1318. break;
  1319. case 'switch_attempt_mode':
  1320. if (!$lp_found) {
  1321. require 'lp_list.php';
  1322. }
  1323. Session::write('refresh', 1);
  1324. $_SESSION['oLP']->switch_attempt_mode();
  1325. require 'lp_list.php';
  1326. break;
  1327. case 'switch_scorm_debug':
  1328. if (!$lp_found) {
  1329. require 'lp_list.php';
  1330. }
  1331. Session::write('refresh', 1);
  1332. $_SESSION['oLP']->update_scorm_debug();
  1333. require 'lp_list.php';
  1334. break;
  1335. case 'intro_cmdAdd':
  1336. // Add introduction section page.
  1337. break;
  1338. case 'return_to_course_homepage':
  1339. if (!$lp_found) {
  1340. require 'lp_list.php';
  1341. } else {
  1342. $_SESSION['oLP']->save_current();
  1343. $_SESSION['oLP']->save_last();
  1344. if ($debug > 0) {
  1345. error_log('save_current()');
  1346. error_log('save_last()');
  1347. }
  1348. $url = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php?id_session='.api_get_session_id();
  1349. $redirectTo = isset($_GET['redirectTo']) ? $_GET['redirectTo'] : '';
  1350. switch ($redirectTo) {
  1351. case 'lp_list':
  1352. $url = 'lp_controller.php?'.api_get_cidreq();
  1353. break;
  1354. case 'my_courses':
  1355. $url = api_get_path(WEB_PATH).'user_portal.php';
  1356. break;
  1357. }
  1358. header('location: '.$url);
  1359. exit;
  1360. }
  1361. break;
  1362. case 'search':
  1363. /* Include the search script, it's smart enough to know when we are
  1364. * searching or not.
  1365. */
  1366. require 'lp_list_search.php';
  1367. break;
  1368. case 'impress':
  1369. if (!$lp_found) {
  1370. require 'lp_list.php';
  1371. } else {
  1372. if ($debug > 0) {
  1373. error_log('Trying to impress this LP item to '.$_REQUEST['item_id'], 0);
  1374. }
  1375. if (!empty($_REQUEST['item_id'])) {
  1376. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  1377. }
  1378. require 'lp_impress.php';
  1379. }
  1380. break;
  1381. case 'set_previous_step_as_prerequisite':
  1382. $_SESSION['oLP']->set_previous_step_as_prerequisite_for_all_items();
  1383. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&".api_get_cidreq();
  1384. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  1385. header('Location: '.$url);
  1386. exit;
  1387. break;
  1388. case 'clear_prerequisites':
  1389. $_SESSION['oLP']->clear_prerequisites();
  1390. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&".api_get_cidreq();
  1391. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  1392. header('Location: '.$url);
  1393. exit;
  1394. break;
  1395. case 'toggle_seriousgame':
  1396. // activate/deactive seriousgame_mode
  1397. if (!$is_allowed_to_edit) {
  1398. api_not_allowed(true);
  1399. }
  1400. if (!$lp_found) {
  1401. require 'lp_list.php';
  1402. }
  1403. Session::write('refresh', 1);
  1404. $_SESSION['oLP']->set_seriousgame_mode();
  1405. require 'lp_list.php';
  1406. break;
  1407. case 'create_forum':
  1408. if (!isset($_GET['id'])) {
  1409. break;
  1410. }
  1411. $selectedItem = null;
  1412. foreach ($_SESSION['oLP']->items as $item) {
  1413. if ($item->db_id == $_GET['id']) {
  1414. $selectedItem = $item;
  1415. }
  1416. }
  1417. if (!empty($selectedItem)) {
  1418. $forumThread = $selectedItem->getForumThread(
  1419. $_SESSION['oLP']->course_int_id,
  1420. $_SESSION['oLP']->lp_session_id
  1421. );
  1422. if (empty($forumThread)) {
  1423. require '../forum/forumfunction.inc.php';
  1424. $forumCategory = getForumCategoryByTitle(
  1425. get_lang('LearningPaths'),
  1426. $_SESSION['oLP']->course_int_id,
  1427. $_SESSION['oLP']->lp_session_id
  1428. );
  1429. $forumCategoryId = !empty($forumCategory) ? $forumCategory['cat_id'] : 0;
  1430. if (empty($forumCategoryId)) {
  1431. $forumCategoryId = store_forumcategory(
  1432. [
  1433. 'lp_id' => 0,
  1434. 'forum_category_title' => get_lang('LearningPaths'),
  1435. 'forum_category_comment' => null,
  1436. ],
  1437. [],
  1438. false
  1439. );
  1440. }
  1441. if (!empty($forumCategoryId)) {
  1442. $forum = $_SESSION['oLP']->getForum(
  1443. $_SESSION['oLP']->lp_session_id
  1444. );
  1445. $forumId = !empty($forum) ? $forum['forum_id'] : 0;
  1446. if (empty($forumId)) {
  1447. $forumId = $_SESSION['oLP']->createForum($forumCategoryId);
  1448. }
  1449. if (!empty($forumId)) {
  1450. $selectedItem->createForumThread($forumId);
  1451. }
  1452. }
  1453. }
  1454. }
  1455. header('Location:'.api_get_self().'?'.http_build_query([
  1456. 'action' => 'add_item',
  1457. 'type' => 'step',
  1458. 'lp_id' => $_SESSION['oLP']->lp_id,
  1459. ]));
  1460. exit;
  1461. break;
  1462. case 'report':
  1463. require 'lp_report.php';
  1464. break;
  1465. case 'dissociate_forum':
  1466. if (!isset($_GET['id'])) {
  1467. break;
  1468. }
  1469. $selectedItem = null;
  1470. foreach ($_SESSION['oLP']->items as $item) {
  1471. if ($item->db_id != $_GET['id']) {
  1472. continue;
  1473. }
  1474. $selectedItem = $item;
  1475. }
  1476. if (!empty($selectedItem)) {
  1477. $forumThread = $selectedItem->getForumThread(
  1478. $_SESSION['oLP']->course_int_id,
  1479. $_SESSION['oLP']->lp_session_id
  1480. );
  1481. if (!empty($forumThread)) {
  1482. $dissociated = $selectedItem->dissociateForumThread($forumThread['iid']);
  1483. if ($dissociated) {
  1484. Display::addFlash(
  1485. Display::return_message(get_lang('ForumDissociate'), 'success')
  1486. );
  1487. }
  1488. }
  1489. }
  1490. header('Location:'.api_get_self().'?'.http_build_query([
  1491. 'action' => 'add_item',
  1492. 'type' => 'step',
  1493. 'lp_id' => $_SESSION['oLP']->lp_id,
  1494. ]));
  1495. exit;
  1496. break;
  1497. case 'add_final_item':
  1498. if (!$lp_found) {
  1499. Display::addFlash(
  1500. Display::return_message(get_lang('NoLPFound'), 'error')
  1501. );
  1502. break;
  1503. }
  1504. Session::write('refresh', 1);
  1505. if (!isset($_POST['submit']) || empty($post_title)) {
  1506. break;
  1507. }
  1508. $_SESSION['oLP']->getFinalItemForm();
  1509. $redirectTo = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  1510. 'action' => 'add_item',
  1511. 'type' => 'step',
  1512. 'lp_id' => intval($_SESSION['oLP']->lp_id),
  1513. ]);
  1514. break;
  1515. default:
  1516. require 'lp_list.php';
  1517. break;
  1518. }
  1519. if (!empty($_SESSION['oLP'])) {
  1520. $_SESSION['lpobject'] = serialize($_SESSION['oLP']);
  1521. if ($debug > 0) {
  1522. error_log('lpobject is serialized in session', 0);
  1523. }
  1524. }
  1525. if (!empty($redirectTo)) {
  1526. header("Location: $redirectTo");
  1527. exit;
  1528. }