lp_view.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This file was originally the copy of document.php, but many modifications happened since then ;
  6. * the direct file view is not needed anymore, if the user uploads a scorm zip file, a directory
  7. * will be automatically created for it, and the files will be uncompressed there for example ;
  8. *
  9. * @package chamilo.learnpath
  10. * @author Yannick Warnier <ywarnier@beeznest.org> - redesign
  11. * @author Denes Nagy, principal author
  12. * @author Isthvan Mandak, several new features
  13. * @author Roan Embrechts, code improvements and refactoring
  14. */
  15. $use_anonymous = true;
  16. $_SESSION['whereami'] = 'lp/view';
  17. $this_section = SECTION_COURSES;
  18. if ($lp_controller_touched != 1) {
  19. header('location: lp_controller.php?action=view&item_id='.intval($_REQUEST['item_id']));
  20. exit;
  21. }
  22. require_once __DIR__.'/../inc/global.inc.php';
  23. if (isset($_REQUEST['origin']) && $_REQUEST['origin'] === 'learnpath') {
  24. $_REQUEST['origin'] = '';
  25. }
  26. //To prevent the template class
  27. $show_learnpath = true;
  28. api_protect_course_script();
  29. $lp_id = !empty($_GET['lp_id']) ? intval($_GET['lp_id']) : 0;
  30. $sessionId = api_get_session_id();
  31. // Check if the learning path is visible for student - (LP requisites)
  32. if (!api_is_platform_admin()) {
  33. if (
  34. !api_is_allowed_to_edit(null, true, false, false) &&
  35. !learnpath::is_lp_visible_for_student($lp_id, api_get_user_id())
  36. ) {
  37. api_not_allowed(true);
  38. }
  39. }
  40. // Checking visibility (eye icon)
  41. $visibility = api_get_item_visibility(
  42. api_get_course_info(),
  43. TOOL_LEARNPATH,
  44. $lp_id,
  45. $action,
  46. api_get_user_id(),
  47. $sessionId
  48. );
  49. if (!api_is_allowed_to_edit(false, true, false, false) &&
  50. intval($visibility) == 0
  51. ) {
  52. api_not_allowed(true);
  53. }
  54. if (empty($_SESSION['oLP'])) {
  55. api_not_allowed(true);
  56. }
  57. $debug = 0;
  58. if ($debug) {
  59. error_log('------ Entering lp_view.php -------');
  60. }
  61. $_SESSION['oLP']->error = '';
  62. $lp_item_id = $_SESSION['oLP']->get_current_item_id();
  63. $lpType = $_SESSION['oLP']->get_type();
  64. if (!$is_allowed_to_edit) {
  65. $categoryId = $_SESSION['oLP']->getCategoryId();
  66. $em = Database::getManager();
  67. if (!empty($categoryId)) {
  68. /** @var \Chamilo\CourseBundle\Entity\CLpCategory $category */
  69. $category = $em->getRepository('ChamiloCourseBundle:CLpCategory')->find($categoryId);
  70. if ($category) {
  71. $users = $category->getUsers();
  72. if (!empty($users) && $users->count() > 0) {
  73. $user = UserManager::getRepository()->find($user_id);
  74. if (!$category->hasUserAdded($user)) {
  75. api_not_allowed(true);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. $course_code = api_get_course_id();
  82. $course_id = api_get_course_int_id();
  83. $user_id = api_get_user_id();
  84. $platform_theme = api_get_setting('stylesheets'); // Platform's css.
  85. $my_style = $platform_theme;
  86. $htmlHeadXtra[] = '<script>
  87. <!--
  88. var jQueryFrameReadyConfigPath = \''.api_get_jquery_web_path().'\';
  89. -->
  90. </script>';
  91. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.frameready.js"></script>';
  92. $htmlHeadXtra[] = '<script>
  93. $(document).ready(function() {
  94. $("div#log_content_cleaner").bind("click", function() {
  95. $("div#log_content").empty();
  96. });
  97. });
  98. var chamilo_xajax_handler = window.oxajax;
  99. </script>';
  100. if ($_SESSION['oLP']->mode == 'embedframe' || $_SESSION['oLP']->get_hide_toc_frame() == 1) {
  101. $htmlHeadXtra[] = '';
  102. }
  103. // Impress js
  104. if ($_SESSION['oLP']->mode == 'impress') {
  105. $lp_id = $_SESSION['oLP']->get_id();
  106. $url = api_get_path(WEB_CODE_PATH)."lp/lp_impress.php?lp_id=$lp_id&".api_get_cidreq();
  107. header("Location: $url");
  108. exit;
  109. }
  110. // Prepare variables for the test tool (just in case) - honestly, this should disappear later on.
  111. $_SESSION['scorm_view_id'] = $_SESSION['oLP']->get_view_id();
  112. $_SESSION['scorm_item_id'] = $lp_item_id;
  113. // Reinit exercises variables to avoid spacename clashes (see exercise tool)
  114. if (isset($exerciseResult) || isset($_SESSION['exerciseResult'])) {
  115. Session::erase('exerciseResult');
  116. Session::erase('objExercise');
  117. Session::erase('questionList');
  118. }
  119. // additional APIs
  120. $htmlHeadXtra[] = '<script>
  121. chamilo_courseCode = "' . $course_code.'";
  122. </script>';
  123. // Document API
  124. $htmlHeadXtra[] = '<script src="js/documentapi.js" type="text/javascript" language="javascript"></script>';
  125. // Storage API
  126. $htmlHeadXtra[] = '<script>
  127. var sv_user = \'' . api_get_user_id().'\';
  128. var sv_course = chamilo_courseCode;
  129. var sv_sco = \'' . $lp_id.'\';
  130. </script>'; // FIXME fetch sco and userid from a more reliable source directly in sotrageapi.js
  131. $htmlHeadXtra[] = '<script type="text/javascript" src="js/storageapi.js"></script>';
  132. /**
  133. * Get a link to the corresponding document.
  134. */
  135. if ($debug) {
  136. error_log(" src: $src ");
  137. error_log(" lp_type: $lpType ");
  138. }
  139. $get_toc_list = $_SESSION['oLP']->get_toc();
  140. $get_teacher_buttons = $_SESSION['oLP']->get_teacher_toc_buttons();
  141. $type_quiz = false;
  142. foreach ($get_toc_list as $toc) {
  143. if ($toc['id'] == $lp_item_id && $toc['type'] == 'quiz') {
  144. $type_quiz = true;
  145. }
  146. }
  147. if (!isset($src)) {
  148. $src = null;
  149. switch ($lpType) {
  150. case 1:
  151. $_SESSION['oLP']->stop_previous_item();
  152. $htmlHeadXtra[] = '<script src="scorm_api.php" type="text/javascript" language="javascript"></script>';
  153. $preReqCheck = $_SESSION['oLP']->prerequisites_match($lp_item_id);
  154. if ($preReqCheck === true) {
  155. $src = $_SESSION['oLP']->get_link(
  156. 'http',
  157. $lp_item_id,
  158. $get_toc_list
  159. );
  160. // Prevents FF 3.6 + Adobe Reader 9 bug see BT#794 when calling a pdf file in a LP.
  161. $file_info = parse_url($src);
  162. if (isset($file_info['path'])) {
  163. $file_info = pathinfo($file_info['path']);
  164. }
  165. if (isset($file_info['extension']) &&
  166. api_strtolower(substr($file_info['extension'], 0, 3) == 'pdf')
  167. ) {
  168. $src = api_get_path(WEB_CODE_PATH).'lp/lp_view_item.php?lp_item_id='.$lp_item_id.'&'.api_get_cidreq();
  169. }
  170. $src = $_SESSION['oLP']->fixBlockedLinks($src);
  171. $_SESSION['oLP']->start_current_item(); // starts time counter manually if asset
  172. } else {
  173. $src = 'blank.php?error=prerequisites';
  174. }
  175. break;
  176. case 2:
  177. // save old if asset
  178. $_SESSION['oLP']->stop_previous_item(); // save status manually if asset
  179. $htmlHeadXtra[] = '<script src="scorm_api.php" type="text/javascript" language="javascript"></script>';
  180. $preReqCheck = $_SESSION['oLP']->prerequisites_match($lp_item_id);
  181. if ($preReqCheck === true) {
  182. $src = $_SESSION['oLP']->get_link('http', $lp_item_id, $get_toc_list);
  183. $_SESSION['oLP']->start_current_item(); // starts time counter manually if asset
  184. } else {
  185. $src = 'blank.php?error=prerequisites';
  186. }
  187. break;
  188. case 3:
  189. // aicc
  190. $_SESSION['oLP']->stop_previous_item(); // save status manually if asset
  191. $htmlHeadXtra[] = '<script src="'.$_SESSION['oLP']->get_js_lib().'" type="text/javascript" language="javascript"></script>';
  192. $preReqCheck = $_SESSION['oLP']->prerequisites_match($lp_item_id);
  193. if ($preReqCheck === true) {
  194. $src = $_SESSION['oLP']->get_link(
  195. 'http',
  196. $lp_item_id,
  197. $get_toc_list
  198. );
  199. $_SESSION['oLP']->start_current_item(); // starts time counter manually if asset
  200. } else {
  201. $src = 'blank.php';
  202. }
  203. break;
  204. case 4:
  205. break;
  206. }
  207. }
  208. $autostart = 'true';
  209. // Update status, total_time from lp_item_view table when you finish the exercises in learning path.
  210. if ($debug) {
  211. error_log('$type_quiz: '.$type_quiz);
  212. error_log('$_REQUEST[exeId]: '.intval($_REQUEST['exeId']));
  213. error_log('$lp_id: '.$lp_id);
  214. error_log('$_GET[lp_item_id]: '.intval($_GET['lp_item_id']));
  215. }
  216. if (!empty($_REQUEST['exeId']) &&
  217. isset($lp_id) &&
  218. isset($_GET['lp_item_id'])
  219. ) {
  220. global $src;
  221. $_SESSION['oLP']->items[$_SESSION['oLP']->current]->write_to_db();
  222. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  223. $TBL_LP_ITEM_VIEW = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  224. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  225. $safe_item_id = intval($_GET['lp_item_id']);
  226. $safe_id = $lp_id;
  227. $safe_exe_id = intval($_REQUEST['exeId']);
  228. if ($safe_id == strval(intval($safe_id)) &&
  229. $safe_item_id == strval(intval($safe_item_id))
  230. ) {
  231. $sql = 'SELECT start_date, exe_date, exe_result, exe_weighting, exe_exo_id
  232. FROM ' . $TBL_TRACK_EXERCICES.'
  233. WHERE exe_id = ' . $safe_exe_id;
  234. $res = Database::query($sql);
  235. $row_dates = Database::fetch_array($res);
  236. $time_start_date = api_strtotime($row_dates['start_date'], 'UTC');
  237. $time_exe_date = api_strtotime($row_dates['exe_date'], 'UTC');
  238. $mytime = (int) $time_exe_date - (int) $time_start_date;
  239. $score = (float) $row_dates['exe_result'];
  240. $max_score = (float) $row_dates['exe_weighting'];
  241. $sql = "UPDATE $TBL_LP_ITEM SET
  242. max_score = '$max_score'
  243. WHERE c_id = $course_id AND id = '".$safe_item_id."'";
  244. Database::query($sql);
  245. $sql = "SELECT id FROM $TBL_LP_ITEM_VIEW
  246. WHERE
  247. c_id = $course_id AND
  248. lp_item_id = '$safe_item_id' AND
  249. lp_view_id = '".$_SESSION['oLP']->lp_view_id."'
  250. ORDER BY id DESC
  251. LIMIT 1";
  252. $res_last_attempt = Database::query($sql);
  253. if (Database::num_rows($res_last_attempt) && !api_is_invitee()) {
  254. $row_last_attempt = Database::fetch_row($res_last_attempt);
  255. $lp_item_view_id = $row_last_attempt[0];
  256. $exercise = new Exercise(api_get_course_int_id());
  257. $exercise->read($row_dates['exe_exo_id']);
  258. $status = 'completed';
  259. if (!empty($exercise->pass_percentage)) {
  260. $status = 'failed';
  261. $success = ExerciseLib::isSuccessExerciseResult(
  262. $score,
  263. $max_score,
  264. $exercise->pass_percentage
  265. );
  266. if ($success) {
  267. $status = 'passed';
  268. }
  269. }
  270. $sql = "UPDATE $TBL_LP_ITEM_VIEW SET
  271. status = '$status',
  272. score = $score,
  273. total_time = $mytime
  274. WHERE id='".$lp_item_view_id."' AND c_id = $course_id ";
  275. if ($debug) {
  276. error_log($sql);
  277. }
  278. Database::query($sql);
  279. $sql = "UPDATE $TBL_TRACK_EXERCICES SET
  280. orig_lp_item_view_id = $lp_item_view_id
  281. WHERE exe_id = ".$safe_exe_id;
  282. Database::query($sql);
  283. }
  284. }
  285. if (intval($_GET['fb_type']) > 0) {
  286. $src = 'blank.php?msg=exerciseFinished';
  287. } else {
  288. $src = api_get_path(WEB_CODE_PATH).'exercise/result.php?origin=learnpath&id='.$safe_exe_id.'&'.api_get_cidreq();
  289. if ($debug) {
  290. error_log('Calling URL: '.$src);
  291. }
  292. }
  293. $autostart = 'false';
  294. }
  295. $_SESSION['oLP']->set_previous_item($lp_item_id);
  296. $nameTools = Security::remove_XSS($_SESSION['oLP']->get_name());
  297. $save_setting = api_get_setting('show_navigation_menu');
  298. global $_setting;
  299. $_setting['show_navigation_menu'] = 'false';
  300. $scorm_css_header = true;
  301. $lp_theme_css = $_SESSION['oLP']->get_theme();
  302. // Sets the css theme of the LP this call is also use at the frames (toc, nav, message).
  303. if ($_SESSION['oLP']->mode == 'fullscreen') {
  304. $htmlHeadXtra[] = "<script>window.open('$src','content_id','toolbar=0,location=0,status=0,scrollbars=1,resizable=1');</script>";
  305. }
  306. // Not in fullscreen mode.
  307. // Check if audio recorder needs to be in studentview.
  308. if (isset($_SESSION['status']) && $_SESSION['status'][$course_code] == 5) {
  309. $audio_recorder_studentview = true;
  310. } else {
  311. $audio_recorder_studentview = false;
  312. }
  313. // Set flag to ensure lp_header.php is loaded by this script (flag is unset in lp_header.php).
  314. $_SESSION['loaded_lp_view'] = true;
  315. $display_none = '';
  316. $margin_left = '340px';
  317. //Media player code
  318. $display_mode = $_SESSION['oLP']->mode;
  319. $scorm_css_header = true;
  320. $lp_theme_css = $_SESSION['oLP']->get_theme();
  321. // Setting up the CSS theme if exists.
  322. if (!empty($lp_theme_css) && !empty($mycourselptheme) && $mycourselptheme != -1 && $mycourselptheme == 1) {
  323. global $lp_theme_css;
  324. } else {
  325. $lp_theme_css = $my_style;
  326. }
  327. $progress_bar = "";
  328. if (!api_is_invitee()) {
  329. $progress_bar = $_SESSION['oLP']->getProgressBar();
  330. }
  331. $navigation_bar = $_SESSION['oLP']->get_navigation_bar();
  332. $navigation_bar_bottom = $_SESSION['oLP']->get_navigation_bar("control-bottom", "display:none");
  333. $mediaplayer = $_SESSION['oLP']->get_mediaplayer($_SESSION['oLP']->current, $autostart);
  334. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  335. $show_audioplayer = false;
  336. // Getting all the information about the item.
  337. $sql = "SELECT audio FROM ".$tbl_lp_item."
  338. WHERE c_id = $course_id AND lp_id = '".$_SESSION['oLP']->lp_id."'";
  339. $res_media = Database::query($sql);
  340. if (Database::num_rows($res_media) > 0) {
  341. while ($row_media = Database::fetch_array($res_media)) {
  342. if (!empty($row_media['audio'])) {
  343. $show_audioplayer = true;
  344. break;
  345. }
  346. }
  347. }
  348. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, true, false);
  349. global $interbreadcrumb;
  350. if ($is_allowed_to_edit) {
  351. $interbreadcrumb[] = array(
  352. 'url' => api_get_self().'?action=list&isStudentView=false&'.api_get_cidreq(true, true, 'course'),
  353. 'name' => get_lang('LearningPaths')
  354. );
  355. $interbreadcrumb[] = array(
  356. 'url' => api_get_self()."?action=add_item&type=step&lp_id={$_SESSION['oLP']->lp_id}&isStudentView=false&".api_get_cidreq(true, true, 'course'),
  357. 'name' => $_SESSION['oLP']->get_name()
  358. );
  359. $interbreadcrumb[] = array(
  360. 'url' => '#',
  361. 'name' => get_lang('Preview')
  362. );
  363. }
  364. // Return to course home.
  365. if ($is_allowed_to_edit) {
  366. $buttonHomeUrl = 'lp_controller.php?'.api_get_cidreq(true, true, 'course').'&'.http_build_query([
  367. 'isStudentView' => 'false',
  368. 'action' => 'return_to_course_homepage'
  369. ]);
  370. } else {
  371. $buttonHomeUrl = 'lp_controller.php?'.api_get_cidreq(true, true, 'course').'&'.http_build_query([
  372. 'action' => 'return_to_course_homepage'
  373. ]);
  374. }
  375. $buttonHomeText = get_lang('CourseHomepageLink');
  376. $returnLink = api_get_course_setting('lp_return_link');
  377. switch ($returnLink) {
  378. case 1: // lp list
  379. $buttonHomeUrl .= '&redirectTo=lp_list';
  380. $buttonHomeText = get_lang('LearningPathList');
  381. break;
  382. case 2: // user portal
  383. $buttonHomeUrl .= '&redirectTo=my_courses';
  384. $buttonHomeText = get_lang('MyCourses');
  385. break;
  386. }
  387. $lpPreviewImagePath = Display::returnIconPath('unknown.png', ICON_SIZE_BIG);
  388. if ($_SESSION['oLP']->get_preview_image()) {
  389. $lpPreviewImagePath = $_SESSION['oLP']->get_preview_image_path();
  390. }
  391. if ($_SESSION['oLP']->current == $_SESSION['oLP']->get_last()) {
  392. $categories = Category::load(
  393. null,
  394. null,
  395. $course_code,
  396. null,
  397. null,
  398. $sessionId
  399. );
  400. if (!empty($categories)) {
  401. $gradebookEvaluations = $categories[0]->get_evaluations();
  402. $gradebookLinks = $categories[0]->get_links();
  403. if (count($gradebookEvaluations) === 0 &&
  404. count($gradebookLinks) === 1 &&
  405. $gradebookLinks[0]->get_type() == LINK_LEARNPATH &&
  406. $gradebookLinks[0]->get_ref_id() == $_SESSION['oLP']->lp_id
  407. ) {
  408. $gradebookMinScore = $categories[0]->get_certificate_min_score();
  409. $userScore = $gradebookLinks[0]->calc_score($user_id, 'best');
  410. if ($userScore[0] >= $gradebookMinScore) {
  411. Category::register_user_certificate($categories[0]->get_id(), $user_id);
  412. }
  413. }
  414. }
  415. }
  416. $template = new Template('', false, false, true, true, false);
  417. $template->assign('glossary_extra_tools', api_get_setting('show_glossary_in_extra_tools'));
  418. $fixLinkSetting = api_get_configuration_value('lp_fix_embed_content');
  419. $fixLink = '';
  420. if ($fixLinkSetting) {
  421. $fixLink = '{type:"script", id:"_fr10", src:"'.api_get_path(WEB_LIBRARY_PATH).'javascript/fixlinks.js"}';
  422. }
  423. $template->assign('fix_link', $fixLink);
  424. $template->assign(
  425. 'glossary_tool_availables',
  426. ['true', 'lp', 'exercise_and_lp']
  427. );
  428. // If the global gamification mode is enabled...
  429. $gamificationMode = api_get_setting('gamification_mode');
  430. // ...AND this learning path is set in gamification mode, then change the display
  431. $gamificationMode = $gamificationMode && $_SESSION['oLP']->seriousgame_mode;
  432. $template->assign('show_glossary_in_documents', api_get_setting('show_glossary_in_documents'));
  433. $template->assign('jquery_web_path', api_get_jquery_web_path());
  434. $template->assign('jquery_ui_js_web_path', api_get_jquery_ui_js_web_path());
  435. $template->assign('jquery_ui_css_web_path', api_get_jquery_ui_css_web_path());
  436. $template->assign('is_allowed_to_edit', $is_allowed_to_edit);
  437. $template->assign('gamification_mode', $gamificationMode);
  438. //$template->assign('breadcrumb', $breadcrumb);
  439. $template->assign('button_home_url', $buttonHomeUrl);
  440. $template->assign('button_home_text', $buttonHomeText);
  441. $template->assign('navigation_bar', $navigation_bar);
  442. $template->assign('progress_bar', $progress_bar);
  443. $template->assign('show_audio_player', $show_audioplayer);
  444. $template->assign('media_player', $mediaplayer);
  445. $template->assign('toc_list', $get_toc_list);
  446. $template->assign('teacher_toc_buttons', $get_teacher_buttons);
  447. $template->assign('iframe_src', $src);
  448. $template->assign('navigation_bar_bottom', $navigation_bar_bottom);
  449. if ($gamificationMode == 1) {
  450. $template->assign(
  451. 'gamification_stars',
  452. $_SESSION['oLP']->getCalculateStars($sessionId)
  453. );
  454. $template->assign(
  455. 'gamification_points',
  456. $_SESSION['oLP']->getCalculateScore($sessionId)
  457. );
  458. }
  459. $template->assign(
  460. 'lp_preview_image',
  461. Display::img(
  462. $lpPreviewImagePath,
  463. $_SESSION['oLP']->name,
  464. [],
  465. ICON_SIZE_BIG
  466. )
  467. );
  468. $template->assign('lp_author', $_SESSION['oLP']->get_author());
  469. $template->assign('lp_mode', $_SESSION['oLP']->mode);
  470. $template->assign('lp_title_scorm', $_SESSION['oLP']->name);
  471. $template->assign('data_list', $_SESSION['oLP']->getListArrayToc($get_toc_list));
  472. $template->assign('lp_id', $_SESSION['oLP']->lp_id);
  473. $template->assign('lp_current_item_id', $_SESSION['oLP']->get_current_item_id());
  474. $view = $template->get_template('learnpath/view.tpl');
  475. $content = $template->fetch($view);
  476. $template->assign('content', $content);
  477. $template->display_no_layout_template();
  478. // Restore a global setting.
  479. $_setting['show_navigation_menu'] = $save_setting;
  480. if ($debug) {
  481. error_log(' ------- end lp_view.php ------');
  482. }