lp_view.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CourseBundle\Entity\CLpCategory;
  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. $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 (!api_is_allowed_to_edit(null, true, false, false) &&
  34. !learnpath::is_lp_visible_for_student($lp_id, api_get_user_id())
  35. ) {
  36. api_not_allowed(true);
  37. }
  38. }
  39. // Checking visibility (eye icon)
  40. $visibility = api_get_item_visibility(
  41. api_get_course_info(),
  42. TOOL_LEARNPATH,
  43. $lp_id,
  44. $action,
  45. api_get_user_id(),
  46. $sessionId
  47. );
  48. if (!api_is_allowed_to_edit(false, true, false, false) &&
  49. intval($visibility) == 0
  50. ) {
  51. api_not_allowed(true);
  52. }
  53. if (empty($_SESSION['oLP'])) {
  54. api_not_allowed(true);
  55. }
  56. $debug = 0;
  57. if ($debug) {
  58. error_log('------ Entering lp_view.php -------');
  59. }
  60. $_SESSION['oLP']->error = '';
  61. $lp_item_id = $_SESSION['oLP']->get_current_item_id();
  62. $lpType = $_SESSION['oLP']->get_type();
  63. if (!$is_allowed_to_edit) {
  64. $categoryId = $_SESSION['oLP']->getCategoryId();
  65. $em = Database::getManager();
  66. if (!empty($categoryId)) {
  67. /** @var CLpCategory $category */
  68. $category = $em->getRepository('ChamiloCourseBundle:CLpCategory')->find($categoryId);
  69. $block = false;
  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. $block = true;
  76. }
  77. }
  78. $isVisible = learnpath::categoryIsVisibleForStudent(
  79. $category,
  80. $user
  81. );
  82. if ($isVisible) {
  83. $block = false;
  84. }
  85. if ($block) {
  86. api_not_allowed(true);
  87. }
  88. }
  89. }
  90. }
  91. $course_code = api_get_course_id();
  92. $course_id = api_get_course_int_id();
  93. $user_id = api_get_user_id();
  94. $platform_theme = api_get_setting('stylesheets'); // Platform's css.
  95. $my_style = $platform_theme;
  96. $htmlHeadXtra[] = '<script>
  97. <!--
  98. var jQueryFrameReadyConfigPath = \''.api_get_jquery_web_path().'\';
  99. -->
  100. </script>';
  101. $htmlHeadXtra[] = api_get_js('js/jquery.frameready.js');
  102. $htmlHeadXtra[] = '<script>
  103. $(document).ready(function() {
  104. $("div#log_content_cleaner").bind("click", function() {
  105. $("div#log_content").empty();
  106. });
  107. });
  108. var chamilo_xajax_handler = window.oxajax;
  109. </script>';
  110. if ($_SESSION['oLP']->mode == 'embedframe' || $_SESSION['oLP']->get_hide_toc_frame() == 1) {
  111. $htmlHeadXtra[] = '';
  112. }
  113. // Impress js
  114. if ($_SESSION['oLP']->mode == 'impress') {
  115. $lp_id = $_SESSION['oLP']->get_id();
  116. $url = api_get_path(WEB_CODE_PATH)."lp/lp_impress.php?lp_id=$lp_id&".api_get_cidreq();
  117. header("Location: $url");
  118. exit;
  119. }
  120. // Prepare variables for the test tool (just in case) - honestly, this should disappear later on.
  121. $_SESSION['scorm_view_id'] = $_SESSION['oLP']->get_view_id();
  122. $_SESSION['scorm_item_id'] = $lp_item_id;
  123. // Reinit exercises variables to avoid spacename clashes (see exercise tool)
  124. if (isset($exerciseResult) || isset($_SESSION['exerciseResult'])) {
  125. Session::erase('exerciseResult');
  126. Session::erase('objExercise');
  127. Session::erase('questionList');
  128. }
  129. // additional APIs
  130. $htmlHeadXtra[] = '<script>
  131. chamilo_courseCode = "' . $course_code.'";
  132. </script>';
  133. // Document API
  134. $htmlHeadXtra[] = api_get_js('js/document/documentapi.js');
  135. // Storage API
  136. $htmlHeadXtra[] = '<script>
  137. var sv_user = \'' . api_get_user_id().'\';
  138. var sv_course = chamilo_courseCode;
  139. var sv_sco = \'' . $lp_id.'\';
  140. </script>'; // FIXME fetch sco and userid from a more reliable source directly in sotrageapi.js
  141. $htmlHeadXtra[] = api_get_js('js/document/storageapi.js');
  142. /**
  143. * Get a link to the corresponding document.
  144. */
  145. if ($debug) {
  146. error_log(" src: $src ");
  147. error_log(" lp_type: $lpType ");
  148. }
  149. $get_toc_list = $_SESSION['oLP']->get_toc();
  150. $get_teacher_buttons = $_SESSION['oLP']->get_teacher_toc_buttons();
  151. $type_quiz = false;
  152. foreach ($get_toc_list as $toc) {
  153. if ($toc['id'] == $lp_item_id && $toc['type'] == 'quiz') {
  154. $type_quiz = true;
  155. }
  156. }
  157. if (!isset($src)) {
  158. $src = null;
  159. switch ($lpType) {
  160. case 1:
  161. $_SESSION['oLP']->stop_previous_item();
  162. $htmlHeadXtra[] = '<script src="scorm_api.php" type="text/javascript" language="javascript"></script>';
  163. $preReqCheck = $_SESSION['oLP']->prerequisites_match($lp_item_id);
  164. if ($preReqCheck === true) {
  165. $src = $_SESSION['oLP']->get_link(
  166. 'http',
  167. $lp_item_id,
  168. $get_toc_list
  169. );
  170. // Prevents FF 3.6 + Adobe Reader 9 bug see BT#794 when calling a pdf file in a LP.
  171. $file_info = parse_url($src);
  172. if (isset($file_info['path'])) {
  173. $file_info = pathinfo($file_info['path']);
  174. }
  175. if (isset($file_info['extension']) &&
  176. api_strtolower(substr($file_info['extension'], 0, 3) == 'pdf')
  177. ) {
  178. $src = api_get_path(WEB_CODE_PATH).'lp/lp_view_item.php?lp_item_id='.$lp_item_id.'&'.api_get_cidreq();
  179. }
  180. $src = $_SESSION['oLP']->fixBlockedLinks($src);
  181. $_SESSION['oLP']->start_current_item(); // starts time counter manually if asset
  182. } else {
  183. $src = 'blank.php?error=prerequisites';
  184. }
  185. break;
  186. case 2:
  187. // save old if asset
  188. $_SESSION['oLP']->stop_previous_item(); // save status manually if asset
  189. $htmlHeadXtra[] = '<script src="scorm_api.php" type="text/javascript" language="javascript"></script>';
  190. $preReqCheck = $_SESSION['oLP']->prerequisites_match($lp_item_id);
  191. if ($preReqCheck === true) {
  192. $src = $_SESSION['oLP']->get_link('http', $lp_item_id, $get_toc_list);
  193. $_SESSION['oLP']->start_current_item(); // starts time counter manually if asset
  194. } else {
  195. $src = 'blank.php?error=prerequisites';
  196. }
  197. break;
  198. case 3:
  199. // aicc
  200. $_SESSION['oLP']->stop_previous_item(); // save status manually if asset
  201. $htmlHeadXtra[] = '<script src="'.$_SESSION['oLP']->get_js_lib().'" type="text/javascript" language="javascript"></script>';
  202. $preReqCheck = $_SESSION['oLP']->prerequisites_match($lp_item_id);
  203. if ($preReqCheck === true) {
  204. $src = $_SESSION['oLP']->get_link(
  205. 'http',
  206. $lp_item_id,
  207. $get_toc_list
  208. );
  209. $_SESSION['oLP']->start_current_item(); // starts time counter manually if asset
  210. } else {
  211. $src = 'blank.php';
  212. }
  213. break;
  214. case 4:
  215. break;
  216. }
  217. }
  218. $autostart = 'true';
  219. // Update status, total_time from lp_item_view table when you finish the exercises in learning path.
  220. if ($debug) {
  221. error_log('$type_quiz: '.$type_quiz);
  222. error_log('$_REQUEST[exeId]: '.intval($_REQUEST['exeId']));
  223. error_log('$lp_id: '.$lp_id);
  224. error_log('$_GET[lp_item_id]: '.intval($_GET['lp_item_id']));
  225. }
  226. if (!empty($_REQUEST['exeId']) &&
  227. isset($lp_id) &&
  228. isset($_GET['lp_item_id'])
  229. ) {
  230. global $src;
  231. $_SESSION['oLP']->items[$_SESSION['oLP']->current]->write_to_db();
  232. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  233. $TBL_LP_ITEM_VIEW = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  234. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  235. $safe_item_id = intval($_GET['lp_item_id']);
  236. $safe_id = $lp_id;
  237. $safe_exe_id = intval($_REQUEST['exeId']);
  238. if ($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 = '".$_SESSION['oLP']->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::isSuccessExerciseResult(
  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).'exercise/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. $_SESSION['oLP']->set_previous_item($lp_item_id);
  306. $nameTools = Security::remove_XSS($_SESSION['oLP']->get_name());
  307. $save_setting = api_get_setting('show_navigation_menu');
  308. global $_setting;
  309. $_setting['show_navigation_menu'] = 'false';
  310. $scorm_css_header = true;
  311. $lp_theme_css = $_SESSION['oLP']->get_theme();
  312. // Sets the css theme of the LP this call is also use at the frames (toc, nav, message).
  313. if ($_SESSION['oLP']->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['loaded_lp_view'] = true;
  325. $display_none = '';
  326. $margin_left = '340px';
  327. //Media player code
  328. $display_mode = $_SESSION['oLP']->mode;
  329. $scorm_css_header = true;
  330. $lp_theme_css = $_SESSION['oLP']->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 = $_SESSION['oLP']->getProgressBar();
  340. }
  341. $navigation_bar = $_SESSION['oLP']->get_navigation_bar();
  342. $navigation_bar_bottom = $_SESSION['oLP']->get_navigation_bar("control-bottom", "display:none");
  343. $mediaplayer = $_SESSION['oLP']->get_mediaplayer($_SESSION['oLP']->current, $autostart);
  344. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  345. $show_audioplayer = false;
  346. // Getting all the information about the item.
  347. $sql = "SELECT audio FROM ".$tbl_lp_item."
  348. WHERE c_id = $course_id AND lp_id = '".$_SESSION['oLP']->lp_id."'";
  349. $res_media = Database::query($sql);
  350. if (Database::num_rows($res_media) > 0) {
  351. while ($row_media = Database::fetch_array($res_media)) {
  352. if (!empty($row_media['audio'])) {
  353. $show_audioplayer = true;
  354. break;
  355. }
  356. }
  357. }
  358. $is_allowed_to_edit = api_is_allowed_to_edit(false, true, true, false);
  359. global $interbreadcrumb;
  360. if ($is_allowed_to_edit) {
  361. $interbreadcrumb[] = array(
  362. 'url' => api_get_self().'?action=list&isStudentView=false&'.api_get_cidreq(true, true, 'course'),
  363. 'name' => get_lang('LearningPaths')
  364. );
  365. $interbreadcrumb[] = array(
  366. 'url' => api_get_self()."?action=add_item&type=step&lp_id={$_SESSION['oLP']->lp_id}&isStudentView=false&".api_get_cidreq(true, true, 'course'),
  367. 'name' => $_SESSION['oLP']->get_name()
  368. );
  369. $interbreadcrumb[] = array(
  370. 'url' => '#',
  371. 'name' => get_lang('Preview')
  372. );
  373. }
  374. // Return to course home.
  375. if ($is_allowed_to_edit) {
  376. $buttonHomeUrl = 'lp_controller.php?'.api_get_cidreq(true, true, 'course').'&'.http_build_query([
  377. 'isStudentView' => 'false',
  378. 'action' => 'return_to_course_homepage'
  379. ]);
  380. } else {
  381. $buttonHomeUrl = 'lp_controller.php?'.api_get_cidreq(true, true, 'course').'&'.http_build_query([
  382. 'action' => 'return_to_course_homepage'
  383. ]);
  384. }
  385. $buttonHomeText = get_lang('CourseHomepageLink');
  386. // Return to lp list
  387. if (api_get_course_setting('lp_return_link') == 1) {
  388. $buttonHomeUrl .= '&redirectTo=lp_list';
  389. $buttonHomeText = get_lang('LearningPathList');
  390. }
  391. $lpPreviewImagePath = Display::returnIconPath('unknown.png', ICON_SIZE_BIG);
  392. if ($_SESSION['oLP']->get_preview_image()) {
  393. $lpPreviewImagePath = $_SESSION['oLP']->get_preview_image_path();
  394. }
  395. if ($_SESSION['oLP']->current == $_SESSION['oLP']->get_last()) {
  396. $categories = Category::load(
  397. null,
  398. null,
  399. $course_code,
  400. null,
  401. null,
  402. $sessionId
  403. );
  404. if (!empty($categories)) {
  405. $gradebookEvaluations = $categories[0]->get_evaluations();
  406. $gradebookLinks = $categories[0]->get_links();
  407. if (count($gradebookEvaluations) === 0 &&
  408. count($gradebookLinks) === 1 &&
  409. $gradebookLinks[0]->get_type() == LINK_LEARNPATH &&
  410. $gradebookLinks[0]->get_ref_id() == $_SESSION['oLP']->lp_id
  411. ) {
  412. $gradebookMinScore = $categories[0]->getCertificateMinScore();
  413. $userScore = $gradebookLinks[0]->calc_score($user_id, 'best');
  414. if ($userScore[0] >= $gradebookMinScore) {
  415. Category::generateUserCertificate($categories[0]->get_id(), $user_id);
  416. }
  417. }
  418. }
  419. }
  420. $template = new Template('', false, false, true, true, false);
  421. $template->assign('glossary_extra_tools', api_get_setting('show_glossary_in_extra_tools'));
  422. $fixLinkSetting = api_get_configuration_value('lp_fix_embed_content');
  423. $fixLink = '';
  424. if ($fixLinkSetting) {
  425. $fixLink = '{type:"script", id:"_fr10", src:"'.api_get_path(WEB_LIBRARY_JS_PATH).'fixlinks.js"}';
  426. }
  427. $template->assign('fix_link', $fixLink);
  428. $template->assign(
  429. 'glossary_tool_availables',
  430. ['true', 'lp', 'exercise_and_lp']
  431. );
  432. // If the global gamification mode is enabled...
  433. $gamificationMode = api_get_setting('gamification_mode');
  434. // ...AND this learning path is set in gamification mode, then change the display
  435. $gamificationMode = $gamificationMode && $_SESSION['oLP']->seriousgame_mode;
  436. $template->assign('show_glossary_in_documents', api_get_setting('show_glossary_in_documents'));
  437. $template->assign('jquery_web_path', api_get_jquery_web_path());
  438. $template->assign('jquery_ui_js_web_path', api_get_jquery_ui_js_web_path());
  439. $template->assign('jquery_ui_css_web_path', api_get_jquery_ui_css_web_path());
  440. $template->assign('is_allowed_to_edit', $is_allowed_to_edit);
  441. $template->assign('gamification_mode', $gamificationMode);
  442. //$template->assign('breadcrumb', $breadcrumb);
  443. $template->assign('button_home_url', $buttonHomeUrl);
  444. $template->assign('button_home_text', $buttonHomeText);
  445. $template->assign('navigation_bar', $navigation_bar);
  446. $template->assign('progress_bar', $progress_bar);
  447. $template->assign('show_audio_player', $show_audioplayer);
  448. $template->assign('media_player', $mediaplayer);
  449. $template->assign('toc_list', $get_toc_list);
  450. $template->assign('teacher_toc_buttons', $get_teacher_buttons);
  451. $template->assign('iframe_src', $src);
  452. $template->assign('navigation_bar_bottom', $navigation_bar_bottom);
  453. if ($gamificationMode == 1) {
  454. $template->assign(
  455. 'gamification_stars',
  456. $_SESSION['oLP']->getCalculateStars($sessionId)
  457. );
  458. $template->assign(
  459. 'gamification_points',
  460. $_SESSION['oLP']->getCalculateScore($sessionId)
  461. );
  462. }
  463. $template->assign(
  464. 'lp_preview_image',
  465. Display::img(
  466. $lpPreviewImagePath,
  467. $_SESSION['oLP']->name,
  468. [],
  469. ICON_SIZE_BIG
  470. )
  471. );
  472. $template->assign('lp_author', $_SESSION['oLP']->get_author());
  473. $template->assign('lp_mode', $_SESSION['oLP']->mode);
  474. $template->assign('lp_title_scorm', $_SESSION['oLP']->name);
  475. $template->assign('data_list', $_SESSION['oLP']->getListArrayToc($get_toc_list));
  476. $template->assign('lp_id', $_SESSION['oLP']->lp_id);
  477. $template->assign('lp_current_item_id', $_SESSION['oLP']->get_current_item_id());
  478. $view = $template->get_template('learnpath/view.tpl');
  479. $content = $template->fetch($view);
  480. $template->assign('content', $content);
  481. $template->display_no_layout_template();
  482. // Restore a global setting.
  483. $_setting['show_navigation_menu'] = $save_setting;
  484. if ($debug) {
  485. error_log(' ------- end lp_view.php ------');
  486. }