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