readout_text.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CDocument;
  4. /**
  5. * Print a read-out text inside a session.
  6. *
  7. * @package chamilo.learnpath
  8. */
  9. $_in_course = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $current_course_tool = TOOL_LEARNPATH;
  12. api_protect_course_script(true);
  13. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  14. $lpId = isset($_GET['lp_id']) ? (int) $_GET['lp_id'] : 0;
  15. $courseInfo = api_get_course_info();
  16. $courseCode = $courseInfo['code'];
  17. $courseId = $courseInfo['real_id'];
  18. $userId = api_get_user_id();
  19. $sessionId = api_get_session_id();
  20. $em = Database::getManager();
  21. $documentRepo = $em->getRepository('ChamiloCourseBundle:CDocument');
  22. // This page can only be shown from inside a learning path
  23. if (!$id && !$lpId) {
  24. api_not_allowed(true);
  25. }
  26. /** @var CDocument $document */
  27. $document = $documentRepo->findOneBy(['cId' => $courseId, 'iid' => $id]);
  28. if (empty($document)) {
  29. // Try with normal id
  30. /** @var CDocument $document */
  31. $document = $documentRepo->findOneBy(['cId' => $courseId, 'id' => $id]);
  32. if (empty($document)) {
  33. Display::return_message(get_lang('The file was not found'), 'error');
  34. exit;
  35. }
  36. }
  37. $documentPathInfo = pathinfo($document->getPath());
  38. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'];
  39. $documentPath = '/document'.$document->getPath();
  40. $documentText = file_get_contents($coursePath.$documentPath);
  41. $documentText = api_remove_tags_with_space($documentText);
  42. $wordsInfo = preg_split('/ |\n/', $documentText, -1, PREG_SPLIT_OFFSET_CAPTURE);
  43. $words = [];
  44. foreach ($wordsInfo as $wordInfo) {
  45. $words[$wordInfo[1]] = nl2br($wordInfo[0]);
  46. }
  47. $htmlHeadXtra[] = '<script>
  48. var words = '.json_encode($words, JSON_OBJECT_AS_ARRAY).',
  49. wordsCount = '.count($words).'
  50. </script>';
  51. $htmlHeadXtra[] = api_get_js('readout_text/js/start.js');
  52. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_JS_PATH).'readout_text/css/start.css');
  53. $template = new Template(strip_tags($document->getTitle()));
  54. $template->display_blank_template();