introductionSection.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CToolIntro;
  4. /**
  5. * The INTRODUCTION MICRO MODULE is used to insert and edit
  6. * an introduction section on a Chamilo module or on the course homepage.
  7. * It can be inserted on any Chamilo module, provided the corresponding setting
  8. * is enabled in the administration section.
  9. *
  10. * The introduction content are stored in a table called "tool_intro"
  11. * in the course Database. Each module introduction has an Id stored in
  12. * the table, which matches a specific module.
  13. *
  14. * '(c_)tool_intro' table description
  15. * c_id: int
  16. * id : int
  17. * intro_text :text
  18. * session_id: int
  19. *
  20. * usage :
  21. *
  22. * $moduleId = 'XX'; // specifying the module tool (string value)
  23. * include(introductionSection.inc.php);
  24. *
  25. * This script is also used since Chamilo 1.9 to show course progress (from the
  26. * course_progress module)
  27. *
  28. * @package chamilo.include
  29. */
  30. $em = Database::getManager();
  31. $intro_editAllowed = $is_allowed_to_edit = api_is_allowed_to_edit();
  32. $session_id = api_get_session_id();
  33. $blogParam = isset($_GET['blog_id']) ? ('&blog_id='.(int) $_GET['blog_id']) : '';
  34. $introduction_section = '';
  35. global $charset;
  36. $intro_cmdEdit = empty($_GET['intro_cmdEdit']) ? '' : $_GET['intro_cmdEdit'];
  37. $intro_cmdUpdate = isset($_POST['intro_cmdUpdate']);
  38. $intro_cmdDel = empty($_GET['intro_cmdDel']) ? '' : $_GET['intro_cmdDel'];
  39. $intro_cmdAdd = empty($_GET['intro_cmdAdd']) ? '' : $_GET['intro_cmdAdd'];
  40. $courseId = api_get_course_id();
  41. if (!empty($courseId)) {
  42. $form = new FormValidator(
  43. 'introduction_text',
  44. 'post',
  45. api_get_self().'?'.api_get_cidreq().$blogParam
  46. );
  47. } else {
  48. $form = new FormValidator('introduction_text');
  49. }
  50. $config = [
  51. 'ToolbarSet' => 'IntroductionSection',
  52. 'Width' => '100%',
  53. 'Height' => '300',
  54. ];
  55. $form->addHtmlEditor('intro_content', null, false, false, $config, ['data-block' => true]);
  56. $form->addButtonSave(get_lang('Save introductory text'), 'intro_cmdUpdate', false, ['data-block' => true]);
  57. /* INTRODUCTION MICRO MODULE - COMMANDS SECTION (IF ALLOWED) */
  58. $course_id = api_get_course_int_id();
  59. if ($intro_editAllowed) {
  60. /** @var CToolIntro $toolIntro */
  61. $toolIntro = $em
  62. ->getRepository('ChamiloCourseBundle:CToolIntro')
  63. ->findOneBy(['cId' => $course_id, 'id' => $moduleId, 'sessionId' => $session_id]);
  64. /* Replace command */
  65. if ($intro_cmdUpdate) {
  66. if ($form->validate()) {
  67. $form_values = $form->exportValues();
  68. $intro_content = $form_values['intro_content'];
  69. if (!empty($intro_content)) {
  70. if (!$toolIntro) {
  71. $toolIntro = new CToolIntro();
  72. $toolIntro
  73. ->setSessionId($session_id)
  74. ->setCId($course_id)
  75. ->setId($moduleId);
  76. }
  77. $toolIntro->setIntroText($intro_content);
  78. $em->persist($toolIntro);
  79. $em->flush();
  80. Display::addFlash(Display::return_message(get_lang('Intro was updated'), 'confirmation', false));
  81. } else {
  82. // got to the delete command
  83. $intro_cmdDel = true;
  84. }
  85. } else {
  86. $intro_cmdEdit = true;
  87. }
  88. }
  89. /* Delete Command */
  90. if ($intro_cmdDel && $toolIntro) {
  91. $em->remove($toolIntro);
  92. $em->flush();
  93. Display::addFlash(Display::return_message(get_lang('Intro was deleted'), 'confirmation'));
  94. }
  95. }
  96. /* INTRODUCTION MICRO MODULE - DISPLAY SECTION */
  97. /* Retrieves the module introduction text, if exist */
  98. // Getting course intro
  99. /** @var CToolIntro $toolIntro */
  100. $toolIntro = $em
  101. ->getRepository('ChamiloCourseBundle:CToolIntro')
  102. ->findOneBy(['cId' => $course_id, 'id' => $moduleId, 'sessionId' => 0]);
  103. $intro_content = $toolIntro ? $toolIntro->getIntroText() : '';
  104. if ($session_id) {
  105. /** @var CToolIntro $toolIntro */
  106. $toolIntro = $em
  107. ->getRepository('ChamiloCourseBundle:CToolIntro')
  108. ->findOneBy(['cId' => $course_id, 'id' => $moduleId, 'sessionId' => $session_id]);
  109. $introSessionContent = $toolIntro && $toolIntro->getIntroText() ? $toolIntro->getIntroText() : '';
  110. $intro_content = $introSessionContent ?: $intro_content;
  111. }
  112. // Default behaviour show iframes.
  113. $userStatus = COURSEMANAGERLOWSECURITY;
  114. // Allows to do a remove_XSS in course introduction with user status COURSEMANAGERLOWSECURITY
  115. // Block embed type videos (like vimeo, wistia, etc) - see BT#12244 BT#12556
  116. if (api_get_configuration_value('course_introduction_html_strict_filtering')) {
  117. $userStatus = COURSEMANAGER;
  118. }
  119. // Ignore editor.css
  120. $cssEditor = api_get_path(WEB_CSS_PATH).'editor.css';
  121. $linkToReplace = [
  122. '<link href="'.$cssEditor.'" rel="stylesheet" type="text/css" />',
  123. '<link href="'.$cssEditor.'" media="screen" rel="stylesheet" type="text/css" />',
  124. ];
  125. $intro_content = str_replace($linkToReplace, '', $intro_content);
  126. $intro_content = Security::remove_XSS($intro_content, $userStatus);
  127. /* Determines the correct display */
  128. if ($intro_cmdEdit || $intro_cmdAdd) {
  129. $intro_dispDefault = false;
  130. $intro_dispForm = true;
  131. $intro_dispCommand = false;
  132. } else {
  133. $intro_dispDefault = true;
  134. $intro_dispForm = false;
  135. if ($intro_editAllowed) {
  136. $intro_dispCommand = true;
  137. } else {
  138. $intro_dispCommand = false;
  139. }
  140. }
  141. /* Executes the display */
  142. // display thematic advance inside a postit
  143. if ($intro_dispForm) {
  144. $default['intro_content'] = $intro_content;
  145. $form->setDefaults($default);
  146. $introduction_section .= $form->returnForm();
  147. }
  148. $thematic_description_html = '';
  149. $thematicItemTwo = '';
  150. if ($tool == TOOL_COURSE_HOMEPAGE && !isset($_GET['intro_cmdEdit'])) {
  151. // Only show this if we're on the course homepage and we're not currently editing
  152. $thematic = new Thematic();
  153. $displayMode = api_get_course_setting('display_info_advance_inside_homecourse');
  154. $class1 = '';
  155. if ($displayMode == '1') {
  156. // Show only the current course progress step
  157. $last_done_advance = $thematic->get_last_done_thematic_advance();
  158. $thematic_advance_info = $thematic->get_thematic_advance_list($last_done_advance);
  159. $subTitle1 = get_lang('Current topic');
  160. $class1 = ' current';
  161. } elseif ($displayMode == '2') {
  162. // Show only the two next course progress steps
  163. $last_done_advance = $thematic->get_next_thematic_advance_not_done();
  164. $next_advance_not_done = $thematic->get_next_thematic_advance_not_done(2);
  165. $thematic_advance_info = $thematic->get_thematic_advance_list($last_done_advance);
  166. $thematic_advance_info2 = $thematic->get_thematic_advance_list($next_advance_not_done);
  167. $subTitle1 = $subTitle2 = get_lang('Next topic');
  168. } elseif ($displayMode == '3') {
  169. // Show the current and next course progress steps
  170. $last_done_advance = $thematic->get_last_done_thematic_advance();
  171. $next_advance_not_done = $thematic->get_next_thematic_advance_not_done();
  172. $thematic_advance_info = $thematic->get_thematic_advance_list($last_done_advance);
  173. $thematic_advance_info2 = $thematic->get_thematic_advance_list($next_advance_not_done);
  174. $subTitle1 = get_lang('Current topic');
  175. $subTitle2 = get_lang('Next topic');
  176. $class1 = ' current';
  177. }
  178. if (!empty($thematic_advance_info)) {
  179. $thematic_advance = get_lang('Course progress');
  180. $thematicScore = $thematic->get_total_average_of_thematic_advances().'%';
  181. $thematicUrl = api_get_path(WEB_CODE_PATH).'course_progress/index.php?action=thematic_details&'.api_get_cidreq();
  182. $thematic_info = $thematic->get_thematic_list($thematic_advance_info['thematic_id']);
  183. $thematic_advance_info['start_date'] = api_get_local_time(
  184. $thematic_advance_info['start_date']
  185. );
  186. $thematic_advance_info['start_date'] = api_format_date(
  187. $thematic_advance_info['start_date'],
  188. DATE_TIME_FORMAT_LONG
  189. );
  190. $userInfo = api_get_user_info();
  191. $courseInfo = api_get_course_info();
  192. $titleThematic = $thematic_advance.' : '.$courseInfo['name'].' <b>( '.$thematicScore.' )</b>';
  193. $infoUser = '<div class="thematic-avatar"><img src="'.$userInfo['avatar'].'" class="img-circle img-responsive"></div>';
  194. $infoUser .= '<div class="progress">
  195. <div class="progress-bar progress-bar-primary" role="progressbar" style="width: '.$thematicScore.';">
  196. '.$thematicScore.'
  197. </div>
  198. </div>';
  199. $thematicItemOne = '
  200. <div class="col-md-6 items-progress">
  201. <div class="thematic-cont '.$class1.'">
  202. <div class="topics">'.$subTitle1.'</div>
  203. <h4 class="title-topics">'.Display::returnFontAwesomeIcon('book').strip_tags($thematic_info['title']).'</h4>
  204. <p class="date">'.Display::returnFontAwesomeIcon('calendar-o').$thematic_advance_info['start_date'].'</p>
  205. <div class="views">'.Display::returnFontAwesomeIcon('file-text-o').strip_tags($thematic_advance_info['content']).'</div>
  206. <p class="time">'.Display::returnFontAwesomeIcon('clock-o').get_lang('Duration in hours').' : '.$thematic_advance_info['duration'].' - <a href="'.$thematicUrl.'">'.get_lang('See detail').'</a></p>
  207. </div>
  208. </div>';
  209. if (!empty($thematic_advance_info2)) {
  210. $thematic_info2 = $thematic->get_thematic_list($thematic_advance_info2['thematic_id']);
  211. $thematic_advance_info2['start_date'] = api_get_local_time($thematic_advance_info2['start_date']);
  212. $thematic_advance_info2['start_date'] = api_format_date($thematic_advance_info2['start_date'], DATE_TIME_FORMAT_LONG);
  213. $thematicItemTwo = '
  214. <div class="col-md-6 items-progress">
  215. <div class="thematic-cont">
  216. <div class="topics">'.$subTitle2.'</div>
  217. <h4 class="title-topics">'.Display::returnFontAwesomeIcon('book').$thematic_info2['title'].'</h4>
  218. <p class="date">'.Display::returnFontAwesomeIcon('calendar-o').$thematic_advance_info2['start_date'].'</p>
  219. <div class="views">'.Display::returnFontAwesomeIcon('file-text-o').strip_tags($thematic_advance_info2['content']).'</div>
  220. <p class="time">'.Display::returnFontAwesomeIcon('clock-o').get_lang('Duration in hours').' : '.$thematic_advance_info2['duration'].' - <a href="'.$thematicUrl.'">'.get_lang('See detail').'</a></p>
  221. </div>
  222. </div>';
  223. }
  224. $thematicPanel = '<div class="row">';
  225. $thematicPanel .= '<div class="col-md-2">'.$infoUser.'</div>';
  226. $thematicPanel .= '<div class="col-md-10"><div class="row">'.$thematicItemOne.$thematicItemTwo.'</div></div>';
  227. $thematicPanel .= '</div>';
  228. $thematicPanel .= '<div class="separate">
  229. <a href="'.$thematicUrl.'" class="btn btn-default btn-block">'.get_lang('Show course planning').'</a>
  230. </div>';
  231. $thematicProgress = Display::panelCollapse(
  232. $titleThematic,
  233. $thematicPanel,
  234. 'thematic',
  235. null,
  236. 'accordion-thematic',
  237. 'collapse-thematic',
  238. false
  239. );
  240. }
  241. }
  242. $thematicSection = '';
  243. if (!empty($thematic_advance_info)) {
  244. $thematicSection = $thematic_description_html;
  245. $thematicSection .= $thematicProgress;
  246. }
  247. $toolbar = [];
  248. /*if (api_is_allowed_to_edit() && empty($session_id)) {
  249. $tool = [
  250. 'name' => get_lang('Customize icons'),
  251. 'url' => api_get_path(WEB_CODE_PATH).'course_info/tools.php?'.api_get_cidreq(),
  252. 'icon' => 'fas fa-cog',
  253. ];
  254. $toolbar[] = $tool;
  255. }*/
  256. if ($intro_dispCommand) {
  257. if (empty($intro_content)) {
  258. // Displays "Add intro" commands
  259. if (!empty($courseId)) {
  260. $tool = [
  261. 'name' => addslashes(get_lang('Add Intro')),
  262. 'url' => api_get_self().'?'.api_get_cidreq().$blogParam.'&intro_cmdAdd=1',
  263. 'icon' => 'fas fa-pencil-alt',
  264. ];
  265. $toolbar[] = $tool;
  266. } else {
  267. $tool = [
  268. 'name' => get_lang('Add Intro'),
  269. 'url' => api_get_self().'?intro_cmdAdd=1',
  270. 'icon' => 'fas fa-pencil-alt',
  271. ];
  272. $toolbar[] = $tool;
  273. }
  274. } else {
  275. // Displays "edit intro && delete intro" commands
  276. if (!empty($courseId)) {
  277. $tool = [
  278. 'name' => get_lang('Edit'),
  279. 'url' => api_get_self().'?'.api_get_cidreq().$blogParam.'&intro_cmdEdit=1',
  280. 'icon' => 'fas fa-pencil-alt',
  281. ];
  282. $toolbar[] = $tool;
  283. $tool = [
  284. 'name' => addslashes(api_htmlentities(get_lang('Please confirm your choice'))),
  285. 'url' => api_get_self()."?".api_get_cidreq().$blogParam."&intro_cmdDel=1",
  286. 'icon' => 'fas fa-trash-alt',
  287. 'class' => 'delete-swal',
  288. ];
  289. $toolbar[] = $tool;
  290. } else {
  291. $tool = [
  292. 'name' => get_lang('Edit'),
  293. 'url' => api_get_self().'?intro_cmdEdit=1',
  294. 'icon' => 'fas fa-pencil-alt',
  295. ];
  296. $toolbar[] = $tool;
  297. $tool = [
  298. 'name' => addslashes(api_htmlentities(get_lang('Please confirm your choice'))),
  299. 'url' => api_get_self()."?".api_get_cidreq()."&intro_cmdDel=1",
  300. 'icon' => 'fas fa-trash-alt',
  301. 'class' => 'delete-swal',
  302. ];
  303. $toolbar[] = $tool;
  304. }
  305. // Fix for chrome XSS filter for videos in iframes - BT#7930
  306. $browser = api_get_navigator();
  307. if (strpos($introduction_section, '<iframe') !== false && $browser['name'] == 'Chrome') {
  308. header('X-XSS-Protection: 0');
  309. }
  310. }
  311. }
  312. $nameSection = get_lang('Add an introduction to the course');
  313. $helpSection = get_lang('This course is already created! Now you can add a presentation or welcome text to your course in this section, ideal for your students.');
  314. if ($moduleId != 'course_homepage') {
  315. $nameSection = get_lang('Add an introduction to the tool');
  316. $helpSection = get_lang('Add Custom Tools Intro');
  317. }
  318. $textContent = [];
  319. if ($intro_dispDefault) {
  320. if (!empty($intro_content)) {
  321. $textContent = [
  322. 'id' => 'introduction-tool',
  323. 'name' => $nameSection,
  324. 'help' => $helpSection,
  325. 'text' => $intro_content,
  326. ];
  327. } else {
  328. if (api_is_allowed_to_edit()) {
  329. $textContent = [
  330. 'id' => 'introduction-course',
  331. 'name' => $nameSection,
  332. 'help' => $helpSection,
  333. 'text' => $intro_content,
  334. ];
  335. }
  336. }
  337. }
  338. $browser = api_get_navigator();
  339. if (strpos($introduction_section, '<iframe') !== false && $browser['name'] == 'Chrome') {
  340. header("X-XSS-Protection: 0");
  341. }
  342. $data = null;
  343. $tpl = new Template(null);
  344. $tpl->assign('thematic', $thematicSection);
  345. $tpl->assign('intro', $textContent);
  346. $tpl->assign('toolbar', $toolbar);
  347. $introduction_section .= $tpl->fetch($tpl->get_template('auth/introduction_section.html.twig'));