lp_view.php 19 KB

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