lp_controller.php 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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. if (isset($_REQUEST['hide_toc_frame']) && $_REQUEST['hide_toc_frame'] == 1) {
  1096. $hide_toc_frame = $_REQUEST['hide_toc_frame'];
  1097. } else {
  1098. $hide_toc_frame = null;
  1099. }
  1100. $_SESSION['oLP']->set_hide_toc_frame($hide_toc_frame);
  1101. $_SESSION['oLP']->set_prerequisite(isset($_POST['prerequisites']) ? (int) $_POST['prerequisites'] : 0);
  1102. $_SESSION['oLP']->setAccumulateWorkTime(isset($_REQUEST['accumulate_work_time']) ? $_REQUEST['accumulate_work_time'] : 0);
  1103. $_SESSION['oLP']->set_use_max_score(isset($_POST['use_max_score']) ? 1 : 0);
  1104. $subscribeUsers = isset($_REQUEST['subscribe_users']) ? 1 : 0;
  1105. $_SESSION['oLP']->setSubscribeUsers($subscribeUsers);
  1106. $accumulateScormTime = isset($_REQUEST['accumulate_scorm_time']) ? $_REQUEST['accumulate_scorm_time'] : 'true';
  1107. $_SESSION['oLP']->setAccumulateScormTime($accumulateScormTime);
  1108. $publicated_on = null;
  1109. if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
  1110. $publicated_on = $_REQUEST['publicated_on'];
  1111. }
  1112. $expired_on = null;
  1113. if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
  1114. $expired_on = $_REQUEST['expired_on'];
  1115. }
  1116. $_SESSION['oLP']->setCategoryId($_REQUEST['category_id']);
  1117. $_SESSION['oLP']->set_modified_on();
  1118. $_SESSION['oLP']->set_publicated_on($publicated_on);
  1119. $_SESSION['oLP']->set_expired_on($expired_on);
  1120. if (isset($_REQUEST['remove_picture']) && $_REQUEST['remove_picture']) {
  1121. $_SESSION['oLP']->delete_lp_image();
  1122. }
  1123. $extraFieldValue = new ExtraFieldValue('lp');
  1124. $_REQUEST['item_id'] = $_SESSION['oLP']->lp_id;
  1125. $extraFieldValue->saveFieldValues($_REQUEST);
  1126. if ($_FILES['lp_preview_image']['size'] > 0) {
  1127. $_SESSION['oLP']->upload_image($_FILES['lp_preview_image']);
  1128. }
  1129. $form = new FormValidator('form1');
  1130. $form->addSelect('skills', 'skills');
  1131. Skill::saveSkills($form, ITEM_TYPE_LEARNPATH, $_SESSION['oLP']->get_id());
  1132. if (api_get_setting('search_enabled') === 'true') {
  1133. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1134. $specific_fields = get_specific_field_list();
  1135. foreach ($specific_fields as $specific_field) {
  1136. $_SESSION['oLP']->set_terms_by_prefix($_REQUEST[$specific_field['code']], $specific_field['code']);
  1137. $new_values = explode(',', trim($_REQUEST[$specific_field['code']]));
  1138. if (!empty($new_values)) {
  1139. array_walk($new_values, 'trim');
  1140. delete_all_specific_field_value(
  1141. api_get_course_id(),
  1142. $specific_field['id'],
  1143. TOOL_LEARNPATH,
  1144. $_SESSION['oLP']->lp_id
  1145. );
  1146. foreach ($new_values as $value) {
  1147. if (!empty($value)) {
  1148. add_specific_field_value(
  1149. $specific_field['id'],
  1150. api_get_course_id(),
  1151. TOOL_LEARNPATH,
  1152. $_SESSION['oLP']->lp_id,
  1153. $value
  1154. );
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. Display::addFlash(Display::return_message(get_lang('Updated')));
  1161. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  1162. header('Location: '.$url);
  1163. exit;
  1164. }
  1165. break;
  1166. case 'add_sub_item':
  1167. // Add an item inside a dir/chapter.
  1168. // @todo check if this is @deprecated
  1169. if (!$is_allowed_to_edit) {
  1170. api_not_allowed(true);
  1171. }
  1172. if (!$lp_found) {
  1173. require 'lp_list.php';
  1174. } else {
  1175. Session::write('refresh', 1);
  1176. if (!empty($_REQUEST['parent_item_id'])) {
  1177. $_SESSION['from_learnpath'] = 'yes';
  1178. $_SESSION['origintoolurl'] = 'lp_controller.php?action=admin_view&lp_id='.intval($_REQUEST['lp_id']);
  1179. } else {
  1180. require 'lp_admin_view.php';
  1181. }
  1182. }
  1183. break;
  1184. case 'deleteitem':
  1185. case 'delete_item':
  1186. if (!$is_allowed_to_edit) {
  1187. api_not_allowed(true);
  1188. }
  1189. if (!$lp_found) {
  1190. require 'lp_list.php';
  1191. } else {
  1192. if (!empty($_REQUEST['id'])) {
  1193. $_SESSION['oLP']->delete_item($_REQUEST['id']);
  1194. }
  1195. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_REQUEST['lp_id']).'&'.api_get_cidreq();
  1196. header('Location: '.$url);
  1197. exit;
  1198. }
  1199. break;
  1200. case 'restart':
  1201. if (!$lp_found) {
  1202. require 'lp_list.php';
  1203. } else {
  1204. $_SESSION['oLP']->restart();
  1205. require 'lp_view.php';
  1206. }
  1207. break;
  1208. case 'last':
  1209. if (!$lp_found) {
  1210. require 'lp_list.php';
  1211. } else {
  1212. $_SESSION['oLP']->last();
  1213. require 'lp_view.php';
  1214. }
  1215. break;
  1216. case 'first':
  1217. if (!$lp_found) {
  1218. require 'lp_list.php';
  1219. } else {
  1220. $_SESSION['oLP']->first();
  1221. require 'lp_view.php';
  1222. }
  1223. break;
  1224. case 'next':
  1225. if (!$lp_found) {
  1226. require 'lp_list.php';
  1227. } else {
  1228. $_SESSION['oLP']->next();
  1229. require 'lp_view.php';
  1230. }
  1231. break;
  1232. case 'previous':
  1233. if (!$lp_found) {
  1234. require 'lp_list.php';
  1235. } else {
  1236. $_SESSION['oLP']->previous();
  1237. require 'lp_view.php';
  1238. }
  1239. break;
  1240. case 'content':
  1241. if (!$lp_found) {
  1242. require 'lp_list.php';
  1243. } else {
  1244. $_SESSION['oLP']->save_last();
  1245. $_SESSION['oLP']->set_current_item($_GET['item_id']);
  1246. $_SESSION['oLP']->start_current_item();
  1247. require 'lp_content.php';
  1248. }
  1249. break;
  1250. case 'view':
  1251. if (!$lp_found) {
  1252. require 'lp_list.php';
  1253. } else {
  1254. if (!empty($_REQUEST['item_id'])) {
  1255. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  1256. }
  1257. require 'lp_view.php';
  1258. }
  1259. break;
  1260. case 'save':
  1261. if (!$lp_found) {
  1262. require 'lp_list.php';
  1263. } else {
  1264. $_SESSION['oLP']->save_item();
  1265. require 'lp_save.php';
  1266. }
  1267. break;
  1268. case 'stats':
  1269. if (!$lp_found) {
  1270. require 'lp_list.php';
  1271. } else {
  1272. $_SESSION['oLP']->save_current();
  1273. $_SESSION['oLP']->save_last();
  1274. $output = require 'lp_stats.php';
  1275. echo $output;
  1276. }
  1277. break;
  1278. case 'list':
  1279. if ($lp_found) {
  1280. Session::write('refresh', 1);
  1281. $_SESSION['oLP']->save_last();
  1282. }
  1283. require 'lp_list.php';
  1284. break;
  1285. case 'mode':
  1286. // Switch between fullscreen and embedded mode.
  1287. $mode = $_REQUEST['mode'];
  1288. if ($mode == 'fullscreen') {
  1289. $_SESSION['oLP']->mode = 'fullscreen';
  1290. } elseif ($mode == 'embedded') {
  1291. $_SESSION['oLP']->mode = 'embedded';
  1292. } elseif ($mode == 'embedframe') {
  1293. $_SESSION['oLP']->mode = 'embedframe';
  1294. } elseif ($mode == 'impress') {
  1295. $_SESSION['oLP']->mode = 'impress';
  1296. }
  1297. require 'lp_view.php';
  1298. break;
  1299. case 'switch_view_mode':
  1300. if (!$lp_found) {
  1301. require 'lp_list.php';
  1302. }
  1303. if (Security::check_token('get')) {
  1304. Session::write('refresh', 1);
  1305. $_SESSION['oLP']->update_default_view_mode();
  1306. }
  1307. require 'lp_list.php';
  1308. break;
  1309. case 'switch_force_commit':
  1310. if (!$lp_found) {
  1311. require 'lp_list.php';
  1312. }
  1313. Session::write('refresh', 1);
  1314. $_SESSION['oLP']->update_default_scorm_commit();
  1315. require 'lp_list.php';
  1316. break;
  1317. case 'switch_attempt_mode':
  1318. if (!$lp_found) {
  1319. require 'lp_list.php';
  1320. }
  1321. Session::write('refresh', 1);
  1322. $_SESSION['oLP']->switch_attempt_mode();
  1323. require 'lp_list.php';
  1324. break;
  1325. case 'switch_scorm_debug':
  1326. if (!$lp_found) {
  1327. require 'lp_list.php';
  1328. }
  1329. Session::write('refresh', 1);
  1330. $_SESSION['oLP']->update_scorm_debug();
  1331. require 'lp_list.php';
  1332. break;
  1333. case 'intro_cmdAdd':
  1334. // Add introduction section page.
  1335. break;
  1336. case 'return_to_course_homepage':
  1337. if (!$lp_found) {
  1338. require 'lp_list.php';
  1339. } else {
  1340. $_SESSION['oLP']->save_current();
  1341. $_SESSION['oLP']->save_last();
  1342. if ($debug > 0) {
  1343. error_log('save_current()');
  1344. error_log('save_last()');
  1345. }
  1346. $url = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php?id_session='.api_get_session_id();
  1347. $redirectTo = isset($_GET['redirectTo']) ? $_GET['redirectTo'] : '';
  1348. switch ($redirectTo) {
  1349. case 'lp_list':
  1350. $url = 'lp_controller.php?'.api_get_cidreq();
  1351. break;
  1352. case 'my_courses':
  1353. $url = api_get_path(WEB_PATH).'user_portal.php';
  1354. break;
  1355. }
  1356. header('location: '.$url);
  1357. exit;
  1358. }
  1359. break;
  1360. case 'search':
  1361. /* Include the search script, it's smart enough to know when we are
  1362. * searching or not.
  1363. */
  1364. require 'lp_list_search.php';
  1365. break;
  1366. case 'impress':
  1367. if (!$lp_found) {
  1368. require 'lp_list.php';
  1369. } else {
  1370. if ($debug > 0) {
  1371. error_log('Trying to impress this LP item to '.$_REQUEST['item_id'], 0);
  1372. }
  1373. if (!empty($_REQUEST['item_id'])) {
  1374. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  1375. }
  1376. require 'lp_impress.php';
  1377. }
  1378. break;
  1379. case 'set_previous_step_as_prerequisite':
  1380. $_SESSION['oLP']->set_previous_step_as_prerequisite_for_all_items();
  1381. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&".api_get_cidreq();
  1382. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  1383. header('Location: '.$url);
  1384. exit;
  1385. break;
  1386. case 'clear_prerequisites':
  1387. $_SESSION['oLP']->clear_prerequisites();
  1388. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&".api_get_cidreq();
  1389. Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
  1390. header('Location: '.$url);
  1391. exit;
  1392. break;
  1393. case 'toggle_seriousgame':
  1394. // activate/deactive seriousgame_mode
  1395. if (!$is_allowed_to_edit) {
  1396. api_not_allowed(true);
  1397. }
  1398. if (!$lp_found) {
  1399. require 'lp_list.php';
  1400. }
  1401. Session::write('refresh', 1);
  1402. $_SESSION['oLP']->set_seriousgame_mode();
  1403. require 'lp_list.php';
  1404. break;
  1405. case 'create_forum':
  1406. if (!isset($_GET['id'])) {
  1407. break;
  1408. }
  1409. $selectedItem = null;
  1410. foreach ($_SESSION['oLP']->items as $item) {
  1411. if ($item->db_id == $_GET['id']) {
  1412. $selectedItem = $item;
  1413. }
  1414. }
  1415. if (!empty($selectedItem)) {
  1416. $forumThread = $selectedItem->getForumThread(
  1417. $_SESSION['oLP']->course_int_id,
  1418. $_SESSION['oLP']->lp_session_id
  1419. );
  1420. if (empty($forumThread)) {
  1421. require '../forum/forumfunction.inc.php';
  1422. $forumCategory = getForumCategoryByTitle(
  1423. get_lang('LearningPaths'),
  1424. $_SESSION['oLP']->course_int_id,
  1425. $_SESSION['oLP']->lp_session_id
  1426. );
  1427. $forumCategoryId = !empty($forumCategory) ? $forumCategory['cat_id'] : 0;
  1428. if (empty($forumCategoryId)) {
  1429. $forumCategoryId = store_forumcategory(
  1430. [
  1431. 'lp_id' => 0,
  1432. 'forum_category_title' => get_lang('LearningPaths'),
  1433. 'forum_category_comment' => null,
  1434. ],
  1435. [],
  1436. false
  1437. );
  1438. }
  1439. if (!empty($forumCategoryId)) {
  1440. $forum = $_SESSION['oLP']->getForum(
  1441. $_SESSION['oLP']->lp_session_id
  1442. );
  1443. $forumId = !empty($forum) ? $forum['forum_id'] : 0;
  1444. if (empty($forumId)) {
  1445. $forumId = $_SESSION['oLP']->createForum($forumCategoryId);
  1446. }
  1447. if (!empty($forumId)) {
  1448. $selectedItem->createForumThread($forumId);
  1449. }
  1450. }
  1451. }
  1452. }
  1453. header('Location:'.api_get_self().'?'.http_build_query([
  1454. 'action' => 'add_item',
  1455. 'type' => 'step',
  1456. 'lp_id' => $_SESSION['oLP']->lp_id,
  1457. ]));
  1458. exit;
  1459. break;
  1460. case 'report':
  1461. require 'lp_report.php';
  1462. break;
  1463. case 'dissociate_forum':
  1464. if (!isset($_GET['id'])) {
  1465. break;
  1466. }
  1467. $selectedItem = null;
  1468. foreach ($_SESSION['oLP']->items as $item) {
  1469. if ($item->db_id != $_GET['id']) {
  1470. continue;
  1471. }
  1472. $selectedItem = $item;
  1473. }
  1474. if (!empty($selectedItem)) {
  1475. $forumThread = $selectedItem->getForumThread(
  1476. $_SESSION['oLP']->course_int_id,
  1477. $_SESSION['oLP']->lp_session_id
  1478. );
  1479. if (!empty($forumThread)) {
  1480. $dissociated = $selectedItem->dissociateForumThread($forumThread['iid']);
  1481. if ($dissociated) {
  1482. Display::addFlash(
  1483. Display::return_message(get_lang('ForumDissociate'), 'success')
  1484. );
  1485. }
  1486. }
  1487. }
  1488. header('Location:'.api_get_self().'?'.http_build_query([
  1489. 'action' => 'add_item',
  1490. 'type' => 'step',
  1491. 'lp_id' => $_SESSION['oLP']->lp_id,
  1492. ]));
  1493. exit;
  1494. break;
  1495. case 'add_final_item':
  1496. if (!$lp_found) {
  1497. Display::addFlash(
  1498. Display::return_message(get_lang('NoLPFound'), 'error')
  1499. );
  1500. break;
  1501. }
  1502. Session::write('refresh', 1);
  1503. if (!isset($_POST['submit']) || empty($post_title)) {
  1504. break;
  1505. }
  1506. $_SESSION['oLP']->getFinalItemForm();
  1507. $redirectTo = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
  1508. 'action' => 'add_item',
  1509. 'type' => 'step',
  1510. 'lp_id' => intval($_SESSION['oLP']->lp_id),
  1511. ]);
  1512. break;
  1513. default:
  1514. require 'lp_list.php';
  1515. break;
  1516. }
  1517. if (!empty($_SESSION['oLP'])) {
  1518. $_SESSION['lpobject'] = serialize($_SESSION['oLP']);
  1519. if ($debug > 0) {
  1520. error_log('lpobject is serialized in session', 0);
  1521. }
  1522. }
  1523. if (!empty($redirectTo)) {
  1524. header("Location: $redirectTo");
  1525. exit;
  1526. }