lp_controller.php 53 KB

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