lp_controller.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Controller script. Prepares the common background variables to give to the scripts corresponding to
  5. * the requested action
  6. * @package chamilo.learnpath
  7. * @author Yannick Warnier <ywarnier@beeznest.org>
  8. */
  9. use \ChamiloSession as Session;
  10. $debug = 0;
  11. if ($debug > 0) error_log('New LP -+- Entered lp_controller.php -+- (action: '.$_REQUEST['action'].')', 0);
  12. // Language files that needs to be included.
  13. if (isset($_GET['action'])) {
  14. if ($_GET['action'] == 'export') {
  15. // Only needed on export.
  16. $language_file[] = 'hotspot';
  17. }
  18. }
  19. $language_file[] = 'course_home';
  20. $language_file[] = 'scormdocument';
  21. $language_file[] = 'document';
  22. $language_file[] = 'scorm';
  23. $language_file[] = 'learnpath';
  24. $language_file[] = 'resourcelinker';
  25. $language_file[] = 'registration';
  26. $language_file[] = 'exercice';
  27. // Including the global initialization file.
  28. //require_once '../inc/global.inc.php';
  29. $current_course_tool = TOOL_LEARNPATH;
  30. if (api_get_setting('show_glossary_in_documents') == 'ismanual' || api_get_setting('show_glossary_in_documents') == 'isautomatic' ) {
  31. $htmlHeadXtra[] = '<script>
  32. <!--
  33. var jQueryFrameReadyConfigPath = \''.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.js\';
  34. -->
  35. </script>';
  36. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.frameready.js" type="text/javascript" language="javascript"></script>';
  37. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.highlight.js" type="text/javascript" language="javascript"></script>';
  38. }
  39. $htmlHeadXtra[] = '<script>
  40. function setFocus(){
  41. $("#idTitle").focus();
  42. }
  43. $(window).load(function () {
  44. setFocus();
  45. });
  46. </script>
  47. <style>
  48. form .label {
  49. padding: 1px 3px 2px;
  50. font-size: 100%;
  51. font-weight: normal;
  52. color: #ffffff;
  53. text-transform: none;
  54. background: none;
  55. border-radius: none;
  56. color: #404040;
  57. float: left;
  58. line-height: 18px;
  59. padding-top: 6px;
  60. text-align: right;
  61. width: 150px;
  62. text-shadow:none;
  63. }
  64. </style>';
  65. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  66. $use_anonymous = true;
  67. // Include class definitions before session_start() to ensure availability when touching
  68. // session vars containing learning paths.
  69. require_once 'learnpath.class.php';
  70. if ($debug > 0) error_log('New LP - Included learnpath', 0);
  71. require_once 'learnpathItem.class.php';
  72. if ($debug > 0) error_log('New LP - Included learnpathItem', 0);
  73. require_once 'scorm.class.php';
  74. if ($debug > 0) error_log('New LP - Included scorm', 0);
  75. require_once 'scormItem.class.php';
  76. if ($debug > 0) error_log('New LP - Included scormItem', 0);
  77. require_once 'aicc.class.php';
  78. if ($debug > 0) error_log('New LP - Included aicc', 0);
  79. require_once 'aiccItem.class.php';
  80. if ($debug > 0) error_log('New LP - Included aiccItem', 0);
  81. //require_once '../inc/global.inc.php';
  82. if ($debug > 0) error_log('New LP - Included back_compat', 0);
  83. $session_id = api_get_session_id();
  84. api_protect_course_script(true);
  85. $lpfound = false;
  86. $myrefresh = 0;
  87. $myrefresh_id = 0;
  88. if (!empty($_SESSION['refresh']) && $_SESSION['refresh'] == 1) {
  89. // Check if we should do a refresh of the oLP object (for example after editing the LP).
  90. // If refresh is set, we regenerate the oLP object from the database (kind of flush).
  91. Session::erase('refresh');
  92. $myrefresh = 1;
  93. if ($debug > 0) error_log('New LP - Refresh asked', 0);
  94. }
  95. if ($debug > 0) error_log('New LP - Passed refresh check', 0);
  96. if (!empty($_REQUEST['dialog_box'])) {
  97. $dialog_box = stripslashes(urldecode($_REQUEST['dialog_box']));
  98. }
  99. $lp_controller_touched = 1;
  100. $lp_found = false;
  101. if (isset($_SESSION['lpobject'])) {
  102. if ($debug > 0) error_log('New LP - SESSION[lpobject] is defined', 0);
  103. $oLP = unserialize($_SESSION['lpobject']);
  104. if (isset($oLP) && is_object($oLP)) {
  105. if ($debug > 0) error_log('New LP - oLP is object', 0);
  106. if ($myrefresh == 1 OR empty($oLP->cc) OR $oLP->cc != api_get_course_id() OR $oLP->lp_view_session_id != $session_id OR $oLP->scorm_debug == '1') {
  107. if ($debug > 0) error_log('New LP - Course has changed, discard lp object', 0);
  108. if ($myrefresh == 1) { $myrefresh_id = $oLP->get_id(); }
  109. $oLP = null;
  110. Session::erase('oLP');
  111. Session::erase('lpobject');
  112. } else {
  113. $_SESSION['oLP'] = $oLP;
  114. $lp_found = true;
  115. }
  116. }
  117. }
  118. $course_id = api_get_course_int_id();
  119. if ($debug>0) error_log('New LP - Passed data remains check', 0);
  120. if (!$lp_found || (!empty($_REQUEST['lp_id']) && $_SESSION['oLP']->get_id() != $_REQUEST['lp_id'])) {
  121. if ($debug > 0) error_log('New LP - oLP is not object, has changed or refresh been asked, getting new', 0);
  122. // Regenerate a new lp object? Not always as some pages don't need the object (like upload?)
  123. if (!empty($_REQUEST['lp_id']) || !empty($myrefresh_id)) {
  124. if ($debug > 0) error_log('New LP - lp_id is defined', 0);
  125. // Select the lp in the database and check which type it is (scorm/dokeos/aicc) to generate the
  126. // right object.
  127. if (!empty($_REQUEST['lp_id'])) {
  128. $lp_id = intval($_REQUEST['lp_id']);
  129. } else {
  130. $lp_id = intval($myrefresh_id);
  131. }
  132. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  133. if (is_numeric($lp_id)) {
  134. $sel = "SELECT lp_type FROM $lp_table WHERE c_id = $course_id AND id = $lp_id";
  135. if ($debug > 0) error_log('New LP - querying '.$sel, 0);
  136. $res = Database::query($sel);
  137. if (Database::num_rows($res)) {
  138. $row = Database::fetch_array($res);
  139. $type = $row['lp_type'];
  140. if ($debug > 0) error_log('New LP - found row - type '.$type. ' - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
  141. switch ($type) {
  142. case 1:
  143. if ($debug > 0) error_log('New LP - found row - type dokeos - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
  144. $oLP = new learnpath(api_get_course_id(), $lp_id, api_get_user_id());
  145. if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
  146. break;
  147. case 2:
  148. if ($debug > 0) error_log('New LP - found row - type scorm - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
  149. $oLP = new scorm(api_get_course_id(), $lp_id, api_get_user_id());
  150. if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
  151. break;
  152. case 3:
  153. if ($debug > 0) error_log('New LP - found row - type aicc - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
  154. $oLP = new aicc(api_get_course_id(),$lp_id,api_get_user_id());
  155. if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
  156. break;
  157. default:
  158. if ($debug > 0) error_log('New LP - found row - type other - Calling constructor with '.api_get_course_id().' - '.$lp_id.' - '.api_get_user_id(), 0);
  159. $oLP = new learnpath(api_get_course_id(),$lp_id,api_get_user_id());
  160. if ($oLP !== false) { $lp_found = true; } else { error_log($oLP->error, 0); }
  161. break;
  162. }
  163. }
  164. } else {
  165. if ($debug > 0) error_log('New LP - Request[lp_id] is not numeric', 0);
  166. }
  167. } else {
  168. if ($debug > 0) error_log('New LP - Request[lp_id] and refresh_id were empty', 0);
  169. }
  170. if ($lp_found) {
  171. $_SESSION['oLP'] = $oLP;
  172. }
  173. }
  174. if ($debug > 0) error_log('New LP - Passed oLP creation check', 0);
  175. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, false, false);
  176. if (isset($_SESSION['oLP'])) {
  177. $_SESSION['oLP']->update_queue = array(); // Reinitialises array used by javascript to update items in the TOC.
  178. $_SESSION['oLP']->message = ''; // Should use ->clear_message() method but doesn't work.
  179. }
  180. if (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'true') {
  181. if ($_REQUEST['action'] != 'list' AND $_REQUEST['action'] != 'view') {
  182. if (!empty($_REQUEST['lp_id'])) {
  183. $_REQUEST['action'] = 'view';
  184. } else {
  185. $_REQUEST['action'] = 'list';
  186. }
  187. }
  188. $_SESSION['studentview'] = "studentview";
  189. } else {
  190. if ($is_allowed_to_edit) {
  191. if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'view' && !isset($_REQUEST['exeId'])) {
  192. $_REQUEST['action'] = 'build';
  193. }
  194. $_SESSION['studentview'] = "teacherview";
  195. }
  196. }
  197. $action = (!empty($_REQUEST['action']) ? $_REQUEST['action'] : '');
  198. switch ($action) {
  199. case 'add_item':
  200. if (!$is_allowed_to_edit) {
  201. api_not_allowed(true);
  202. }
  203. if ($debug > 0) error_log('New LP - add item action triggered', 0);
  204. if (!$lp_found) {
  205. //check if the learnpath ID was defined, otherwise send back to list
  206. if ($debug > 0) error_log('New LP - No learnpath given for add item', 0);
  207. require 'lp_list.php';
  208. } else {
  209. $_SESSION['refresh'] = 1;
  210. if (isset($_POST['submit_button']) && !empty($_POST['title'])) {
  211. // If a title was sumbitted:
  212. //Updating the lp.modified_on
  213. $_SESSION['oLP']->set_modified_on();
  214. if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_POST['post_time']) {
  215. // Check post_time to ensure ??? (counter-hacking measure?)
  216. require 'lp_add_item.php';
  217. } else {
  218. $_SESSION['post_time'] = $_POST['post_time'];
  219. if ($_POST['type'] == TOOL_DOCUMENT) {
  220. if (isset($_POST['path']) && $_GET['edit'] != 'true') {
  221. $document_id = $_POST['path'];
  222. } else {
  223. $document_id = $_SESSION['oLP']->create_document($_course);
  224. }
  225. $new_item_id = $_SESSION['oLP']->add_item($_POST['parent'], $_POST['previous'], $_POST['type'], $document_id, $_POST['title'], $_POST['description'], $_POST['prerequisites']);
  226. } else {
  227. // For all other item types than documents, load the item using the item type and path rather than its ID.
  228. $new_item_id = $_SESSION['oLP']->add_item($_POST['parent'], $_POST['previous'], $_POST['type'], $_POST['path'], $_POST['title'], $_POST['description'], $_POST['prerequisites'], $_POST['maxTimeAllowed']);
  229. }
  230. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id);
  231. header('Location: '.$url);
  232. exit;
  233. }
  234. } else {
  235. require 'lp_add_item.php';
  236. }
  237. }
  238. break;
  239. case 'add_audio':
  240. if (!$is_allowed_to_edit) {
  241. api_not_allowed(true);
  242. }
  243. if ($debug > 0) error_log('New LP - add audio action triggered', 0);
  244. if (!$lp_found) {
  245. //check if the learnpath ID was defined, otherwise send back to list
  246. if ($debug > 0) error_log('New LP - No learnpath given for add audio', 0);
  247. require 'lp_list.php';
  248. } else {
  249. $_SESSION['refresh'] = 1;
  250. if (isset($_REQUEST['id'])) {
  251. $lp_item_obj = new learnpathItem($_REQUEST['id']);
  252. //Remove audio
  253. if (isset($_POST['delete_file']) && $_POST['delete_file'] == 1) {
  254. $lp_item_obj->remove_audio();
  255. }
  256. //Upload audio
  257. if (isset($_FILES['file']) && !empty($_FILES['file'])) {
  258. //Updating the lp.modified_on
  259. $_SESSION['oLP']->set_modified_on();
  260. $lp_item_obj->add_audio();
  261. }
  262. //Add audio file from documents
  263. if (isset($_REQUEST['document_id']) && !empty($_REQUEST['document_id'])) {
  264. $_SESSION['oLP']->set_modified_on();
  265. $lp_item_obj->add_audio_from_documents($_REQUEST['document_id']);
  266. }
  267. // Display.
  268. require 'lp_add_audio.php';
  269. } else {
  270. require 'lp_add_audio.php';
  271. }
  272. }
  273. break;
  274. case 'add_lp_category':
  275. if (!$is_allowed_to_edit) {
  276. api_not_allowed(true);
  277. }
  278. require 'lp_add_category.php';
  279. break;
  280. case 'move_up_category':
  281. if (!$is_allowed_to_edit) {
  282. api_not_allowed(true);
  283. }
  284. if (isset($_REQUEST['id'])) {
  285. learnpath::move_up_category($_REQUEST['id']);
  286. }
  287. require 'lp_list.php';
  288. break;
  289. case 'move_down_category':
  290. if (!$is_allowed_to_edit) {
  291. api_not_allowed(true);
  292. }
  293. if (isset($_REQUEST['id'])) {
  294. learnpath::move_down_category($_REQUEST['id']);
  295. }
  296. require 'lp_list.php';
  297. break;
  298. case 'delete_lp_category':
  299. if (!$is_allowed_to_edit) {
  300. api_not_allowed(true);
  301. }
  302. if (isset($_REQUEST['id'])) {
  303. learnpath::delete_category($_REQUEST['id']);
  304. }
  305. require 'lp_list.php';
  306. break;
  307. case 'add_lp':
  308. if (!$is_allowed_to_edit) {
  309. api_not_allowed(true);
  310. }
  311. if ($debug > 0) error_log('New LP - add_lp action triggered', 0);
  312. if (isset($_REQUEST['lp_name']) && !empty($_REQUEST['lp_name'])) {
  313. $_REQUEST['lp_name'] = trim($_REQUEST['lp_name']);
  314. $_SESSION['refresh'] = 1;
  315. if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_REQUEST['post_time']) {
  316. require 'lp_add.php';
  317. } else {
  318. $_SESSION['post_time'] = $_REQUEST['post_time'];
  319. if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
  320. $publicated_on = $_REQUEST['publicated_on'];
  321. $publicated_on = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
  322. } else {
  323. $publicated_on = null;
  324. }
  325. if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
  326. $expired_on = $_REQUEST['expired_on'];
  327. $expired_on = $expired_on['Y'].'-'.$expired_on['F'].'-'.$expired_on['d'].' '.$expired_on['H'].':'.$expired_on['i'].':00';
  328. } else {
  329. $expired_on = null;
  330. }
  331. $new_lp_id = learnpath::add_lp(api_get_course_id(), Security::remove_XSS($_REQUEST['lp_name']), '', 'chamilo', 'manual', '', $publicated_on, $expired_on, $_REQUEST['category_id']);
  332. if (is_numeric($new_lp_id)) {
  333. // TODO: Maybe create a first module directly to avoid bugging the user with useless queries
  334. $_SESSION['oLP'] = new learnpath(api_get_course_id(),$new_lp_id,api_get_user_id());
  335. //require 'lp_build.php';
  336. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($new_lp_id);
  337. header('Location: '.$url);
  338. exit;
  339. }
  340. }
  341. } else {
  342. require 'lp_add.php';
  343. }
  344. break;
  345. case 'admin_view':
  346. if (!$is_allowed_to_edit) {
  347. api_not_allowed(true);
  348. }
  349. if ($debug > 0) error_log('New LP - admin_view action triggered', 0);
  350. if (!$lp_found) { error_log('New LP - No learnpath given for admin_view', 0); require 'lp_list.php'; }
  351. else {
  352. $_SESSION['refresh'] = 1;
  353. require 'lp_admin_view.php';
  354. }
  355. break;
  356. case 'auto_launch':
  357. if (api_get_course_setting('enable_lp_auto_launch') == 1) { //Redirect to a specific LP
  358. if (!$is_allowed_to_edit) {
  359. api_not_allowed(true);
  360. }
  361. if ($debug > 0) error_log('New LP - auto_launch action triggered', 0);
  362. if (!$lp_found) { error_log('New LP - No learnpath given for set_autolunch', 0); require 'lp_list.php'; }
  363. else {
  364. $_SESSION['oLP']->set_autolunch($_GET['lp_id'], $_GET['status']);
  365. require 'lp_list.php';
  366. exit;
  367. }
  368. }
  369. break;
  370. case 'build':
  371. if (!$is_allowed_to_edit) {
  372. api_not_allowed(true);
  373. }
  374. if ($debug > 0) error_log('New LP - build action triggered', 0);
  375. if (!$lp_found) { error_log('New LP - No learnpath given for build', 0); require 'lp_list.php'; }
  376. else {
  377. $_SESSION['refresh'] = 1;
  378. //require 'lp_build.php';
  379. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id);
  380. header('Location: '.$url);
  381. exit;
  382. }
  383. break;
  384. case 'edit_item':
  385. if (!$is_allowed_to_edit) {
  386. api_not_allowed(true);
  387. }
  388. if ($debug > 0) error_log('New LP - edit item action triggered', 0);
  389. if (!$lp_found) { error_log('New LP - No learnpath given for edit item', 0); require 'lp_list.php'; }
  390. else {
  391. $_SESSION['refresh'] = 1;
  392. if (isset($_POST['submit_button']) && !empty($_POST['title'])) {
  393. //Updating the lp.modified_on
  394. $_SESSION['oLP']->set_modified_on();
  395. // TODO: mp3 edit
  396. $audio = array();
  397. if (isset($_FILES['mp3'])) {
  398. $audio = $_FILES['mp3'];
  399. }
  400. $_SESSION['oLP']->edit_item($_REQUEST['id'], $_POST['parent'], $_POST['previous'], $_POST['title'], $_POST['description'], $_POST['prerequisites'], $audio, $_POST['maxTimeAllowed']);
  401. if (isset($_POST['content_lp'])) {
  402. $_SESSION['oLP']->edit_document($_course);
  403. }
  404. $is_success = true;
  405. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id);
  406. header('Location: '.$url);
  407. exit;
  408. }
  409. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  410. require 'lp_edit_item.php';
  411. } else {
  412. require 'lp_admin_view.php';
  413. }
  414. }
  415. break;
  416. case 'edit_item_prereq':
  417. if (!$is_allowed_to_edit) {
  418. api_not_allowed(true);
  419. }
  420. if ($debug > 0) error_log('New LP - edit item prereq action triggered', 0);
  421. if (!$lp_found) { error_log('New LP - No learnpath given for edit item prereq', 0); require 'lp_list.php'; }
  422. else {
  423. if (isset($_POST['submit_button'])) {
  424. //Updating the lp.modified_on
  425. $_SESSION['oLP']->set_modified_on();
  426. $_SESSION['refresh'] = 1;
  427. if ($_SESSION['oLP']->edit_item_prereq($_GET['id'], $_POST['prerequisites'], $_POST['min_' . $_POST['prerequisites']], $_POST['max_' . $_POST['prerequisites']])) {
  428. $is_success = true;
  429. }
  430. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id);
  431. header('Location: '.$url);
  432. exit;
  433. } else {
  434. require 'lp_edit_item_prereq.php';
  435. }
  436. }
  437. break;
  438. case 'move_item':
  439. if (!$is_allowed_to_edit) {
  440. api_not_allowed(true);
  441. }
  442. if ($debug > 0) error_log('New LP - move item action triggered', 0);
  443. if (!$lp_found) { error_log('New LP - No learnpath given for move item', 0); require 'lp_list.php'; }
  444. else {
  445. $_SESSION['refresh'] = 1;
  446. if (isset($_POST['submit_button'])) {
  447. //Updating the lp.modified_on
  448. $_SESSION['oLP']->set_modified_on();
  449. $_SESSION['oLP']->edit_item($_GET['id'], $_POST['parent'], $_POST['previous'], $_POST['title'], $_POST['description']);
  450. $is_success = true;
  451. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id);
  452. header('Location: '.$url);
  453. exit;
  454. }
  455. if (isset($_GET['view']) && $_GET['view'] == 'build') {
  456. require 'lp_move_item.php';
  457. } else {
  458. // Avoids weird behaviours see CT#967.
  459. $check = Security::check_token('get');
  460. if ($check) {
  461. $_SESSION['oLP']->move_item($_GET['id'], $_GET['direction']);
  462. }
  463. Security::clear_token();
  464. require 'lp_admin_view.php';
  465. }
  466. }
  467. break;
  468. case 'view_item':
  469. if (!$is_allowed_to_edit) {
  470. api_not_allowed(true);
  471. }
  472. if ($debug > 0) error_log('New LP - view_item action triggered', 0);
  473. if (!$lp_found) {
  474. error_log('New LP - No learnpath given for view item', 0); require 'lp_list.php';
  475. } else {
  476. $_SESSION['refresh'] = 1;
  477. require 'lp_view_item.php';
  478. }
  479. break;
  480. case 'upload':
  481. if (!$is_allowed_to_edit) {
  482. api_not_allowed(true);
  483. }
  484. if ($debug > 0) error_log('New LP - upload action triggered', 0);
  485. $cwdir = getcwd();
  486. require 'lp_upload.php';
  487. // Reinit current working directory as many functions in upload change it.
  488. chdir($cwdir);
  489. require 'lp_list.php';
  490. break;
  491. case 'copy':
  492. if (!$is_allowed_to_edit) {
  493. api_not_allowed(true);
  494. }
  495. if ($debug > 0) error_log('New LP - export action triggered', 0);
  496. if (!$lp_found) { error_log('New LP - No learnpath given for copy', 0); require 'lp_list.php'; }
  497. else {
  498. $_SESSION['oLP']->copy();
  499. }
  500. require 'lp_list.php';
  501. break;
  502. case 'export':
  503. if (!$is_allowed_to_edit) {
  504. api_not_allowed(true);
  505. }
  506. if ($debug > 0) error_log('New LP - export action triggered', 0);
  507. if (!$lp_found) { error_log('New LP - No learnpath given for export', 0); require 'lp_list.php'; }
  508. else {
  509. $_SESSION['oLP']->scorm_export();
  510. exit();
  511. //require 'lp_list.php';
  512. }
  513. break;
  514. case 'export_to_pdf':
  515. if (!learnpath::is_lp_visible_for_student($_SESSION['oLP']->lp_id, api_get_user_id())) {
  516. api_not_allowed();
  517. }
  518. if ($debug > 0) error_log('New LP - export action triggered', 0);
  519. if (!$lp_found) { error_log('New LP - No learnpath given for export_to_pdf', 0); require 'lp_list.php';
  520. } else {
  521. $result = $_SESSION['oLP']->scorm_export_to_pdf($_GET['lp_id']);
  522. if (!$result) {
  523. require 'lp_list.php';
  524. }
  525. exit;
  526. }
  527. break;
  528. case 'delete':
  529. if (!$is_allowed_to_edit) {
  530. api_not_allowed(true);
  531. }
  532. if ($debug > 0) error_log('New LP - delete action triggered', 0);
  533. if (!$lp_found) { error_log('New LP - No learnpath given for delete', 0); require 'lp_list.php'; }
  534. else {
  535. $_SESSION['refresh'] = 1;
  536. // Remove lp from homepage if it is there.
  537. //$_SESSION['oLP']->toggle_visibility((int)$_GET['lp_id'],'i');
  538. $_SESSION['oLP']->delete(null,(int)$_GET['lp_id'],'remove');
  539. Session::erase('oLP');
  540. require 'lp_list.php';
  541. }
  542. break;
  543. case 'toggle_visible': // Change lp visibility (inside lp tool).
  544. if (!$is_allowed_to_edit) {
  545. api_not_allowed(true);
  546. }
  547. if ($debug > 0) error_log('New LP - visibility action triggered', 0);
  548. if (!$lp_found) { error_log('New LP - No learnpath given for visibility', 0); require 'lp_list.php'; }
  549. else {
  550. learnpath::toggle_visibility($_REQUEST['lp_id'], $_REQUEST['new_status']);
  551. require 'lp_list.php';
  552. }
  553. break;
  554. case 'toggle_publish': // Change lp published status (visibility on homepage).
  555. if (!$is_allowed_to_edit) {
  556. api_not_allowed(true);
  557. }
  558. if ($debug > 0) error_log('New LP - publish action triggered', 0);
  559. if (!$lp_found) { error_log('New LP - No learnpath given for publish', 0); require 'lp_list.php'; }
  560. else {
  561. learnpath::toggle_publish($_REQUEST['lp_id'],$_REQUEST['new_status']);
  562. require 'lp_list.php';
  563. }
  564. break;
  565. case 'move_lp_up': // Change lp published status (visibility on homepage)
  566. if (!$is_allowed_to_edit) {
  567. api_not_allowed(true);
  568. }
  569. if ($debug > 0) error_log('New LP - publish action triggered', 0);
  570. if (!$lp_found) {
  571. error_log('New LP - No learnpath given for publish', 0);
  572. require 'lp_list.php';
  573. } else {
  574. learnpath::move_up($_REQUEST['lp_id']);
  575. require 'lp_list.php';
  576. }
  577. break;
  578. case 'move_lp_down': //change lp published status (visibility on homepage)
  579. if (!$is_allowed_to_edit) {
  580. api_not_allowed(true);
  581. }
  582. if ($debug > 0) error_log('New LP - publish action triggered', 0);
  583. if (!$lp_found) {
  584. error_log('New LP - No learnpath given for publish', 0);
  585. require 'lp_list.php';
  586. } else {
  587. learnpath::move_down($_REQUEST['lp_id']);
  588. require 'lp_list.php';
  589. }
  590. break;
  591. case 'edit':
  592. if (!$is_allowed_to_edit) {
  593. api_not_allowed(true);
  594. }
  595. if ($debug > 0) error_log('New LP - edit action triggered', 0);
  596. if (!$lp_found) { error_log('New LP - No learnpath given for edit', 0); require 'lp_list.php'; }
  597. else {
  598. $_SESSION['refresh'] = 1;
  599. require 'lp_edit.php';
  600. }
  601. break;
  602. case 'update_lp':
  603. if (!$is_allowed_to_edit) {
  604. api_not_allowed(true);
  605. }
  606. if ($debug > 0) error_log('New LP - update_lp action triggered', 0);
  607. if (!$lp_found) { error_log('New LP - No learnpath given for edit', 0); require 'lp_list.php'; }
  608. else {
  609. $_SESSION['refresh'] = 1;
  610. $lp_name = Security::remove_XSS($_REQUEST['lp_name']);
  611. $_SESSION['oLP']->set_name($lp_name);
  612. $author = $_REQUEST['lp_author'];
  613. // Fixing the author name (no body or html tags).
  614. $auth_init = stripos($author, '<p>');
  615. if ($auth_init === false) {
  616. $auth_init = stripos($author, '<body>');
  617. $auth_end = $auth_init + stripos(substr($author, $auth_init + 6), '</body>') + 7;
  618. $len = $auth_end - $auth_init + 6;
  619. } else {
  620. $auth_end = strripos($author, '</p>');
  621. $len = $auth_end - $auth_init + 4;
  622. }
  623. $author_fixed = substr($author, $auth_init, $len);
  624. //$author_fixed = $author;
  625. $_SESSION['oLP']->set_author($author_fixed);
  626. $_SESSION['oLP']->set_encoding($_REQUEST['lp_encoding']); // TODO (as of Chamilo 1.8.8): Check in the future whether this field is needed.
  627. if (isset($_REQUEST['lp_maker'])) {
  628. $_SESSION['oLP']->set_maker($_REQUEST['lp_maker']);
  629. }
  630. if (isset($_REQUEST['lp_proximity'])) {
  631. $_SESSION['oLP']->set_proximity($_REQUEST['lp_proximity']);
  632. }
  633. $_SESSION['oLP']->set_theme($_REQUEST['lp_theme']);
  634. if (isset($_REQUEST['hide_toc_frame']) && $_REQUEST['hide_toc_frame'] == 1) {
  635. $hide_toc_frame = $_REQUEST['hide_toc_frame'];
  636. } else {
  637. $hide_toc_frame = null;
  638. }
  639. $_SESSION['oLP']->set_hide_toc_frame($hide_toc_frame);
  640. $_SESSION['oLP']->set_prerequisite($_REQUEST['prerequisites']);
  641. $_SESSION['oLP']->set_use_max_score($_REQUEST['use_max_score']);
  642. $_SESSION['oLP']->set_subscribe_users($_REQUEST['subscribe_users']);
  643. $_SESSION['oLP']->set_max_attempts($_REQUEST['max_attempts']);
  644. if (isset($_REQUEST['activate_start_date_check']) && $_REQUEST['activate_start_date_check'] == 1) {
  645. $publicated_on = $_REQUEST['publicated_on'];
  646. $publicated_on = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
  647. } else {
  648. $publicated_on = null;
  649. }
  650. if (isset($_REQUEST['activate_end_date_check']) && $_REQUEST['activate_end_date_check'] == 1) {
  651. $expired_on = $_REQUEST['expired_on'];
  652. $expired_on = $expired_on['Y'].'-'.$expired_on['F'].'-'.$expired_on['d'].' '.$expired_on['H'].':'.$expired_on['i'].':00';
  653. } else {
  654. $expired_on = null;
  655. }
  656. $_SESSION['oLP']->set_category_id($_REQUEST['category_id']);
  657. $_SESSION['oLP']->set_modified_on();
  658. $_SESSION['oLP']->set_publicated_on($publicated_on);
  659. $_SESSION['oLP']->set_expired_on($expired_on);
  660. if (isset($_REQUEST['remove_picture']) && $_REQUEST['remove_picture']) {
  661. $_SESSION['oLP']->delete_lp_image();
  662. }
  663. $extraFieldValue = new ExtraFieldValue('lp');
  664. $params = array(
  665. 'lp_id' => $_SESSION['oLP']->id
  666. );
  667. $extraFieldValue->save_field_values($_REQUEST);
  668. if ($_FILES['lp_preview_image']['size'] > 0)
  669. $_SESSION['oLP']->upload_image($_FILES['lp_preview_image']);
  670. if (api_get_setting('search_enabled') === 'true') {
  671. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  672. $specific_fields = get_specific_field_list();
  673. foreach ($specific_fields as $specific_field) {
  674. $_SESSION['oLP']->set_terms_by_prefix($_REQUEST[$specific_field['code']], $specific_field['code']);
  675. $new_values = explode(',', trim($_REQUEST[$specific_field['code']]));
  676. if (!empty($new_values)) {
  677. array_walk($new_values, 'trim');
  678. delete_all_specific_field_value(api_get_course_id(), $specific_field['id'], TOOL_LEARNPATH, $_SESSION['oLP']->lp_id);
  679. foreach ($new_values as $value) {
  680. if (!empty($value)) {
  681. add_specific_field_value($specific_field['id'], api_get_course_id(), TOOL_LEARNPATH, $_SESSION['oLP']->lp_id, $value);
  682. }
  683. }
  684. }
  685. }
  686. }
  687. $referer = isset($_REQUEST['referer']) ? $_REQUEST['referer'] : 'add_item';
  688. switch ($referer) {
  689. case 'lplist':
  690. $url = api_get_self().'?action=list&'.api_get_cidreq();
  691. break;
  692. default:
  693. case 'add_item':
  694. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  695. break;
  696. }
  697. header('Location: '.$url);
  698. exit;
  699. }
  700. break;
  701. case 'add_sub_item': // Add an item inside a chapter.
  702. if (!$is_allowed_to_edit) {
  703. api_not_allowed(true);
  704. }
  705. if ($debug > 0) error_log('New LP - add sub item action triggered', 0);
  706. if (!$lp_found) { error_log('New LP - No learnpath given for add sub item', 0); require 'lp_list.php'; }
  707. else {
  708. $_SESSION['refresh'] = 1;
  709. if (!empty($_REQUEST['parent_item_id'])) {
  710. $_SESSION['from_learnpath']='yes';
  711. $_SESSION['origintoolurl'] = 'lp_controller.php?action=admin_view&lp_id='.Security::remove_XSS($_REQUEST['lp_id']);
  712. require 'resourcelinker.php';
  713. //$_SESSION['oLP']->add_sub_item($_REQUEST['parent_item_id'], $_REQUEST['previous'], $_REQUEST['type'], $_REQUEST['path'], $_REQUEST['title']);
  714. } else {
  715. require 'lp_admin_view.php';
  716. }
  717. }
  718. break;
  719. case 'deleteitem':
  720. case 'delete_item':
  721. if (!$is_allowed_to_edit) {
  722. api_not_allowed(true);
  723. }
  724. if ($debug > 0) error_log('New LP - delete item action triggered', 0);
  725. if (!$lp_found) { error_log('New LP - No learnpath given for delete item', 0); require 'lp_list.php'; }
  726. else {
  727. //$_SESSION['refresh'] = 1;
  728. if (!empty($_REQUEST['id'])) {
  729. $_SESSION['oLP']->delete_item($_REQUEST['id']);
  730. }
  731. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_REQUEST['lp_id']);
  732. header('Location: '.$url);
  733. exit;
  734. }
  735. break;
  736. case 'edititemprereq':
  737. case 'edit_item_prereq':
  738. if (!$is_allowed_to_edit) {
  739. api_not_allowed(true);
  740. }
  741. if ($debug > 0) error_log('New LP - edit item prereq action triggered', 0);
  742. if (!$lp_found) { error_log('New LP - No learnpath given for edit item prereq', 0); require 'lp_list.php'; }
  743. else {
  744. if (!empty($_REQUEST['id']) && !empty($_REQUEST['submit_item'])) {
  745. $_SESSION['refresh'] = 1;
  746. $_SESSION['oLP']->edit_item_prereq($_REQUEST['id'], $_REQUEST['prereq']);
  747. }
  748. require 'lp_admin_view.php';
  749. }
  750. break;
  751. case 'restart':
  752. if ($debug > 0) error_log('New LP - restart action triggered', 0);
  753. if (!$lp_found) { error_log('New LP - No learnpath given for restart', 0); require 'lp_list.php'; }
  754. else {
  755. $_SESSION['oLP']->restart();
  756. require 'lp_view.php';
  757. }
  758. break;
  759. case 'last':
  760. if ($debug > 0) error_log('New LP - last action triggered', 0);
  761. if (!$lp_found) { error_log('New LP - No learnpath given for last', 0); require 'lp_list.php'; }
  762. else {
  763. $_SESSION['oLP']->last();
  764. require 'lp_view.php';
  765. }
  766. break;
  767. case 'first':
  768. if ($debug > 0) error_log('New LP - first action triggered', 0);
  769. if (!$lp_found) { error_log('New LP - No learnpath given for first', 0); require 'lp_list.php'; }
  770. else {
  771. $_SESSION['oLP']->first();
  772. require 'lp_view.php';
  773. }
  774. break;
  775. case 'next':
  776. if ($debug > 0) error_log('New LP - next action triggered', 0);
  777. if (!$lp_found) { error_log('New LP - No learnpath given for next', 0); require 'lp_list.php'; }
  778. else {
  779. $_SESSION['oLP']->next();
  780. require 'lp_view.php';
  781. }
  782. break;
  783. case 'previous':
  784. if ($debug > 0) error_log('New LP - previous action triggered', 0);
  785. if (!$lp_found) { error_log('New LP - No learnpath given for previous', 0); require 'lp_list.php'; }
  786. else {
  787. $_SESSION['oLP']->previous();
  788. require 'lp_view.php';
  789. }
  790. break;
  791. case 'content':
  792. if ($debug > 0) error_log('New LP - content action triggered', 0);
  793. if ($debug > 0) error_log('New LP - Item id is '.$_GET['item_id'], 0);
  794. if (!$lp_found) { error_log('New LP - No learnpath given for content', 0); require 'lp_list.php'; }
  795. else {
  796. $_SESSION['oLP']->save_last();
  797. $_SESSION['oLP']->set_current_item($_GET['item_id']);
  798. $_SESSION['oLP']->start_current_item();
  799. require 'lp_content.php';
  800. }
  801. break;
  802. case 'view':
  803. if ($debug > 0)
  804. error_log('New LP - view action triggered', 0);
  805. if (!$lp_found) {
  806. error_log('New LP - No learnpath given for view', 0);
  807. require 'lp_list.php';
  808. } else {
  809. if ($debug > 0) {error_log('New LP - Trying to set current item to ' . $_REQUEST['item_id'], 0); }
  810. if ( !empty($_REQUEST['item_id']) ) {
  811. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  812. }
  813. require 'lp_view.php';
  814. }
  815. break;
  816. case 'save':
  817. if ($debug > 0) error_log('New LP - save action triggered', 0);
  818. if (!$lp_found) { error_log('New LP - No learnpath given for save', 0); require 'lp_list.php'; }
  819. else {
  820. $_SESSION['oLP']->save_item();
  821. require 'lp_save.php';
  822. }
  823. break;
  824. case 'stats':
  825. if ($debug > 0) error_log('New LP - stats action triggered', 0);
  826. if (!$lp_found) { error_log('New LP - No learnpath given for stats', 0); require 'lp_list.php'; }
  827. else {
  828. $_SESSION['oLP']->save_current();
  829. $_SESSION['oLP']->save_last();
  830. require 'lp_stats.php';
  831. }
  832. break;
  833. case 'list':
  834. if ($debug > 0) error_log('New LP - list action triggered', 0);
  835. if ($lp_found) {
  836. $_SESSION['refresh'] = 1;
  837. $_SESSION['oLP']->save_last();
  838. }
  839. require 'lp_list.php';
  840. break;
  841. case 'mode':
  842. // Switch between fullscreen and embedded mode.
  843. if ($debug > 0) error_log('New LP - mode change triggered', 0);
  844. $mode = $_REQUEST['mode'];
  845. if ($mode == 'fullscreen') {
  846. $_SESSION['oLP']->mode = 'fullscreen';
  847. } elseif ($mode == 'embedded') {
  848. $_SESSION['oLP']->mode = 'embedded';
  849. } elseif ($mode == 'embedframe') {
  850. $_SESSION['oLP']->mode = 'embedframe';
  851. } elseif ($mode == 'impress') {
  852. $_SESSION['oLP']->mode = 'impress';
  853. }
  854. require 'lp_view.php';
  855. break;
  856. case 'switch_view_mode':
  857. if ($debug > 0) error_log('New LP - switch_view_mode action triggered', 0);
  858. if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
  859. if (Security::check_token('get')) {
  860. $_SESSION['refresh'] = 1;
  861. $_SESSION['oLP']->update_default_view_mode();
  862. }
  863. require 'lp_list.php';
  864. break;
  865. case 'switch_force_commit':
  866. if ($debug > 0) error_log('New LP - switch_force_commit action triggered', 0);
  867. if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
  868. $_SESSION['refresh'] = 1;
  869. $_SESSION['oLP']->update_default_scorm_commit();
  870. require 'lp_list.php';
  871. break;
  872. /* Those 2 switches have been replaced by switc_attempt_mode switch
  873. case 'switch_reinit':
  874. if ($debug > 0) error_log('New LP - switch_reinit action triggered', 0);
  875. if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
  876. $_SESSION['refresh'] = 1;
  877. $_SESSION['oLP']->update_reinit();
  878. require 'lp_list.php';
  879. break;
  880. case 'switch_seriousgame_mode':
  881. if($debug>0) error_log('New LP - switch_seriousgame_mode action triggered',0);
  882. if(!$lp_found){ error_log('New LP - No learnpath given for switch',0); require 'lp_list.php'; }
  883. $_SESSION['refresh'] = 1;
  884. $_SESSION['oLP']->set_seriousgame_mode();
  885. require 'lp_list.php';
  886. break;
  887. */
  888. case 'switch_attempt_mode':
  889. if($debug>0) error_log('New LP - switch_reinit action triggered',0);
  890. if(!$lp_found){ error_log('New LP - No learnpath given for switch',0); require 'lp_list.php'; }
  891. $_SESSION['refresh'] = 1;
  892. $_SESSION['oLP']->switch_attempt_mode();
  893. require 'lp_list.php';
  894. break;
  895. case 'switch_scorm_debug':
  896. if ($debug > 0) error_log('New LP - switch_scorm_debug action triggered', 0);
  897. if (!$lp_found) { error_log('New LP - No learnpath given for switch', 0); require 'lp_list.php'; }
  898. $_SESSION['refresh'] = 1;
  899. $_SESSION['oLP']->update_scorm_debug();
  900. require 'lp_list.php';
  901. break;
  902. case 'intro_cmdAdd':
  903. if ($debug > 0) error_log('New LP - intro_cmdAdd action triggered', 0);
  904. // Add introduction section page.
  905. break;
  906. case 'js_api_refresh':
  907. if ($debug > 0) error_log('New LP - js_api_refresh action triggered', 0);
  908. if (!$lp_found) { error_log('New LP - No learnpath given for js_api_refresh', 0); require 'lp_message.php'; }
  909. if (isset($_REQUEST['item_id'])) {
  910. $htmlHeadXtra[] = $_SESSION['oLP']->get_js_info($_REQUEST['item_id']);
  911. }
  912. require 'lp_message.php';
  913. break;
  914. case 'return_to_course_homepage':
  915. if (!$lp_found) { error_log('New LP - No learnpath given for stats', 0); require 'lp_list.php'; }
  916. else {
  917. $_SESSION['oLP']->save_current();
  918. $_SESSION['oLP']->save_last();
  919. header('location: '.api_get_path(WEB_COURSE_PATH).api_get_course_path().'/index.php?id_session='.api_get_session_id());
  920. exit;
  921. }
  922. break;
  923. case 'search':
  924. /* Include the search script, it's smart enough to know when we are
  925. * searching or not.
  926. */
  927. require 'lp_list_search.php';
  928. break;
  929. case 'impress':
  930. if ($debug > 0)
  931. error_log('New LP - view action triggered', 0);
  932. if (!$lp_found) {
  933. error_log('New LP - No learnpath given for view', 0);
  934. require 'lp_list.php';
  935. } else {
  936. if ($debug > 0) {error_log('New LP - Trying to impress this LP item to ' . $_REQUEST['item_id'], 0); }
  937. if (!empty($_REQUEST['item_id']) ) {
  938. $_SESSION['oLP']->set_current_item($_REQUEST['item_id']);
  939. }
  940. require 'lp_impress.php';
  941. }
  942. break;
  943. case 'set_previous_step_as_prerequisite':
  944. $_SESSION['oLP']->set_previous_step_as_prerequisite_for_all_items();
  945. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&message=ItemUpdated";
  946. header('Location: '.$url);
  947. exit;
  948. break;
  949. case 'clear_prerequisites':
  950. $_SESSION['oLP']->clear_prerequisites();
  951. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id)."&message=ItemUpdated";
  952. header('Location: '.$url);
  953. exit;
  954. break;
  955. default:
  956. if ($debug > 0) error_log('New LP - default action triggered', 0);
  957. require 'lp_list.php';
  958. break;
  959. }
  960. if (!empty($_SESSION['oLP'])) {
  961. $_SESSION['lpobject'] = serialize($_SESSION['oLP']);
  962. if ($debug > 0) error_log('New LP - lpobject is serialized in session', 0);
  963. }