lp_controller.php 51 KB

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