lp_controller.php 56 KB

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