thematic_controller.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Thematic Controller script.
  6. * Prepares the common background variables to give to the scripts corresponding to
  7. * the requested action.
  8. *
  9. * This file contains class used like controller for thematic,
  10. * it should be included inside a dispatcher file (e.g: index.php)
  11. *
  12. * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC !
  13. * DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
  14. *
  15. * @author Christian Fasanando <christian1827@gmail.com>
  16. * @author Julio Montoya <gugli100@gmail.com> token support improving UI
  17. *
  18. * @package chamilo.course_progress
  19. */
  20. class ThematicController
  21. {
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct()
  26. {
  27. $this->toolname = 'course_progress';
  28. $this->view = new View($this->toolname);
  29. }
  30. /**
  31. * This method is used for thematic control (update, insert or listing).
  32. *
  33. * @param string $action
  34. * render to thematic.php
  35. */
  36. public function thematic($action)
  37. {
  38. $thematic = new Thematic();
  39. $data = [];
  40. $check = Security::check_token('request');
  41. $thematic_id = isset($_REQUEST['thematic_id']) ? intval($_REQUEST['thematic_id']) : null;
  42. $displayHeader = !empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header' ? false : true;
  43. $courseId = api_get_course_int_id();
  44. if ($check) {
  45. switch ($action) {
  46. case 'thematic_add':
  47. case 'thematic_edit':
  48. // insert or update a thematic
  49. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  50. if (trim($_POST['title']) !== '') {
  51. if (api_is_allowed_to_edit(null, true)) {
  52. $id = isset($_POST['thematic_id']) ? $_POST['thematic_id'] : null;
  53. $title = trim($_POST['title']);
  54. $content = trim($_POST['content']);
  55. $session_id = api_get_session_id();
  56. $thematic->set_thematic_attributes($id, $title, $content, $session_id);
  57. $last_id = $thematic->thematic_save();
  58. if ($_POST['action'] == 'thematic_add') {
  59. $action = 'thematic_details';
  60. $thematic_id = null;
  61. if ($last_id) {
  62. $data['last_id'] = $last_id;
  63. }
  64. } else {
  65. $action = 'thematic_details';
  66. $thematic_id = null;
  67. }
  68. Display::addFlash(Display::return_message(get_lang('Updated')));
  69. }
  70. } else {
  71. $error = true;
  72. $data['error'] = $error;
  73. $data['action'] = $_POST['action'];
  74. $data['thematic_id'] = $_POST['thematic_id'];
  75. // render to the view
  76. $this->view->set_data($data);
  77. $this->view->set_layout('layout');
  78. $this->view->set_template('thematic');
  79. $this->view->render();
  80. }
  81. }
  82. break;
  83. case 'thematic_copy':
  84. // Copy a thematic to a session
  85. $thematic->copy($thematic_id);
  86. $thematic_id = null;
  87. $action = 'thematic_details';
  88. break;
  89. case 'thematic_delete_select':
  90. // Delete many thematics
  91. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  92. if (api_is_allowed_to_edit(null, true)) {
  93. $thematic_ids = $_POST['id'];
  94. $thematic->delete($thematic_ids);
  95. Display::addFlash(Display::return_message(get_lang('Deleted')));
  96. }
  97. $action = 'thematic_details';
  98. }
  99. break;
  100. case 'thematic_delete':
  101. // Delete a thematic
  102. if (isset($thematic_id)) {
  103. if (api_is_allowed_to_edit(null, true)) {
  104. $thematic->delete($thematic_id);
  105. Display::addFlash(Display::return_message(get_lang('Deleted')));
  106. }
  107. $thematic_id = null;
  108. $action = 'thematic_details';
  109. }
  110. break;
  111. case 'thematic_import_select':
  112. break;
  113. case 'thematic_import':
  114. $csv_import_array = Import::csv_reader($_FILES['file']['tmp_name'], false);
  115. if (isset($_POST['replace']) && $_POST['replace']) {
  116. // Remove current thematic.
  117. $list = $thematic->get_thematic_list();
  118. foreach ($list as $i) {
  119. $thematic->delete($i);
  120. }
  121. }
  122. // Import the progress.
  123. $current_thematic = null;
  124. foreach ($csv_import_array as $key => $item) {
  125. if (!$key) {
  126. continue;
  127. }
  128. switch ($item[0]) {
  129. case 'title':
  130. $thematic->set_thematic_attributes(
  131. null,
  132. $item[1],
  133. $item[2],
  134. api_get_session_id()
  135. );
  136. $current_thematic = $thematic->thematic_save();
  137. $description_type = 1;
  138. break;
  139. case 'plan':
  140. $thematic->set_thematic_plan_attributes(
  141. $current_thematic,
  142. $item[1],
  143. $item[2],
  144. $description_type
  145. );
  146. $thematic->thematic_plan_save();
  147. $description_type++;
  148. break;
  149. case 'progress':
  150. $thematic->set_thematic_advance_attributes(
  151. null,
  152. $current_thematic,
  153. 0,
  154. $item[3],
  155. $item[1],
  156. $item[2]
  157. );
  158. $thematic->thematic_advance_save();
  159. break;
  160. }
  161. }
  162. $action = 'thematic_details';
  163. break;
  164. case 'thematic_export':
  165. $list = $thematic->get_thematic_list();
  166. $csv = [];
  167. $csv[] = ['type', 'data1', 'data2', 'data3'];
  168. foreach ($list as $theme) {
  169. $csv[] = ['title', strip_tags($theme['title']), strip_tags($theme['content'])];
  170. $data = $thematic->get_thematic_plan_data($theme['id']);
  171. if (!empty($data)) {
  172. foreach ($data as $plan) {
  173. if (empty($plan['description'])) {
  174. continue;
  175. }
  176. $csv[] = [
  177. 'plan',
  178. strip_tags($plan['title']),
  179. strip_tags($plan['description']),
  180. ];
  181. }
  182. }
  183. $data = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
  184. if (!empty($data)) {
  185. foreach ($data as $advance) {
  186. $csv[] = [
  187. 'progress',
  188. strip_tags($advance['start_date']),
  189. strip_tags($advance['duration']),
  190. strip_tags($advance['content']),
  191. ];
  192. }
  193. }
  194. }
  195. Export::arrayToCsv($csv);
  196. exit;
  197. // Don't continue building a normal page.
  198. return;
  199. case 'export_documents':
  200. case 'thematic_export_pdf':
  201. $pdfOrientation = api_get_configuration_value('thematic_pdf_orientation');
  202. $list = $thematic->get_thematic_list();
  203. $item = [];
  204. $listFinish = [];
  205. foreach ($list as $theme) {
  206. $dataPlan = $thematic->get_thematic_plan_data($theme['id']);
  207. if (!empty($dataPlan)) {
  208. foreach ($dataPlan as $plan) {
  209. if (empty($plan['description'])) {
  210. continue;
  211. }
  212. $item[] = [
  213. 'title' => $plan['title'],
  214. 'description' => $plan['description'],
  215. ];
  216. }
  217. $theme['thematic_plan'] = $item;
  218. }
  219. $dataAdvance = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
  220. if (!empty($dataAdvance)) {
  221. $theme['thematic_advance'] = $dataAdvance;
  222. }
  223. $listFinish[] = $theme;
  224. }
  225. $view = new Template('', false, false, false, true, false, false);
  226. $view->assign('data', $listFinish);
  227. $template = $view->get_template('course_progress/pdf_general_thematic.tpl');
  228. $format = $pdfOrientation !== 'portrait' ? 'A4-L' : 'A4-P';
  229. $orientation = $pdfOrientation !== 'portrait' ? 'L' : 'P';
  230. $fileName = get_lang('Thematic').'-'.api_get_local_time();
  231. $title = get_lang('Thematic');
  232. $signatures = ['Drh', 'Teacher', 'Date'];
  233. if ($action === 'export_documents') {
  234. $pdf = new PDF(
  235. $format,
  236. $orientation,
  237. [
  238. 'filename' => $fileName,
  239. 'pdf_title' => $fileName,
  240. 'add_signatures' => $signatures,
  241. ]
  242. );
  243. $pdf->exportFromHtmlToDocumentsArea($view->fetch($template), $fileName, $courseId);
  244. header('Location: '.api_get_self().'?'.api_get_cidreq());
  245. exit;
  246. }
  247. Export::export_html_to_pdf(
  248. $view->fetch($template),
  249. [
  250. 'filename' => $fileName,
  251. 'pdf_title' => $title,
  252. 'add_signatures' => $signatures,
  253. 'format' => $format,
  254. 'orientation' => $orientation,
  255. ]
  256. );
  257. break;
  258. case 'export_single_documents':
  259. case 'export_single_thematic':
  260. $theme = $thematic->get_thematic_list($thematic_id);
  261. $plans = $thematic->get_thematic_plan_data($theme['id']);
  262. $plans = array_filter(
  263. $plans,
  264. function ($plan) {
  265. return !empty($plan['description']);
  266. }
  267. );
  268. $advances = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
  269. $view = new Template('', false, false, false, true, false, false);
  270. $view->assign('theme', $theme);
  271. $view->assign('plans', $plans);
  272. $view->assign('advances', $advances);
  273. $template = $view->get_template('course_progress/pdf_single_thematic.tpl');
  274. $pdfOrientation = api_get_configuration_value('thematic_pdf_orientation');
  275. $format = $pdfOrientation !== 'portrait' ? 'A4-L' : 'A4-P';
  276. $orientation = $pdfOrientation !== 'portrait' ? 'L' : 'P';
  277. $title = get_lang('Thematic').'-'.$theme['title'];
  278. $fileName = $title.'-'.api_get_local_time();
  279. $signatures = ['Drh', 'Teacher', 'Date'];
  280. if ($action === 'export_single_documents') {
  281. $pdf = new PDF(
  282. $format,
  283. $orientation,
  284. [
  285. 'filename' => $fileName,
  286. 'pdf_title' => $fileName,
  287. 'add_signatures' => $signatures,
  288. ]
  289. );
  290. $pdf->exportFromHtmlToDocumentsArea(
  291. $view->fetch($template),
  292. $fileName,
  293. $courseId
  294. );
  295. header('Location: '.api_get_self().'?'.api_get_cidreq());
  296. exit;
  297. }
  298. Export::export_html_to_pdf(
  299. $view->fetch($template),
  300. [
  301. 'filename' => $fileName,
  302. 'pdf_title' => $title,
  303. 'add_signatures' => $signatures,
  304. 'format' => $format,
  305. 'orientation' => $orientation,
  306. ]
  307. );
  308. break;
  309. case 'moveup':
  310. $thematic->move_thematic('up', $thematic_id);
  311. $action = 'thematic_details';
  312. $thematic_id = null;
  313. break;
  314. case 'movedown':
  315. $thematic->move_thematic('down', $thematic_id);
  316. $action = 'thematic_details';
  317. $thematic_id = null;
  318. break;
  319. }
  320. Security::clear_token();
  321. } else {
  322. $action = 'thematic_details';
  323. $thematic_id = null;
  324. }
  325. if (isset($thematic_id)) {
  326. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  327. $data['thematic_id'] = $thematic_id;
  328. }
  329. if ($action == 'thematic_details') {
  330. if (isset($thematic_id)) {
  331. $thematic_data_result = $thematic->get_thematic_list($thematic_id);
  332. if (!empty($thematic_data_result)) {
  333. $thematic_data[$thematic_id] = $thematic_data_result;
  334. }
  335. $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
  336. } else {
  337. $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
  338. $data['max_thematic_item'] = $thematic->get_max_thematic_item();
  339. $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
  340. $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
  341. }
  342. // Second column
  343. $thematic_plan_data = $thematic->get_thematic_plan_data();
  344. // Third column
  345. $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true);
  346. $data['thematic_plan_div'] = $thematic->get_thematic_plan_array($thematic_plan_data);
  347. $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
  348. $data['thematic_plan_data'] = $thematic_plan_data;
  349. $data['thematic_advance_data'] = $thematic_advance_data;
  350. $data['thematic_data'] = $thematic_data;
  351. }
  352. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  353. $data['action'] = $action;
  354. $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
  355. // render to the view
  356. $this->view->set_data($data);
  357. $this->view->set_layout($layoutName);
  358. $this->view->set_template('thematic');
  359. $this->view->render();
  360. }
  361. /**
  362. * This method is used for thematic plan control (update, insert or listing).
  363. *
  364. * @param string $action
  365. * render to thematic_plan.php
  366. */
  367. public function thematic_plan($action)
  368. {
  369. $thematic = new Thematic();
  370. $data = [];
  371. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  372. if (isset($_POST['action']) &&
  373. ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')
  374. ) {
  375. if (isset($_POST['title'])) {
  376. $token = Session::read('thematic_plan_token');
  377. if ($_POST['thematic_plan_token'] == $token) {
  378. if (api_is_allowed_to_edit(null, true)) {
  379. $title_list = $_REQUEST['title'];
  380. $description_list = $_REQUEST['description'];
  381. $description_type = $_REQUEST['description_type'];
  382. for ($i = 1; $i < count($title_list) + 1; $i++) {
  383. $thematic->set_thematic_plan_attributes(
  384. $_REQUEST['thematic_id'],
  385. $title_list[$i],
  386. $description_list[$i],
  387. $description_type[$i]
  388. );
  389. $thematic->thematic_plan_save();
  390. }
  391. $saveRedirect = api_get_path(WEB_PATH).'main/course_progress/index.php?';
  392. $saveRedirect .= api_get_cidreq().'&';
  393. if (isset($_REQUEST['add_item'])) {
  394. $thematic->set_thematic_plan_attributes(
  395. $_REQUEST['thematic_id'],
  396. '',
  397. '',
  398. $i
  399. );
  400. $thematic->thematic_plan_save();
  401. $saveRedirect .= http_build_query([
  402. 'action' => 'thematic_plan_list',
  403. 'thematic_id' => $_REQUEST['thematic_id'],
  404. ]);
  405. } else {
  406. $saveRedirect .= 'thematic_plan_save_message=ok';
  407. Session::erase('thematic_plan_token');
  408. $data['message'] = 'ok';
  409. }
  410. header("Location: $saveRedirect");
  411. exit;
  412. }
  413. $data['action'] = 'thematic_plan_list';
  414. }
  415. } else {
  416. $error = true;
  417. $action = $_POST['action'];
  418. $data['error'] = $error;
  419. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data(
  420. $_POST['thematic_id'],
  421. $_POST['description_type']
  422. );
  423. $data['thematic_id'] = $_POST['thematic_id'];
  424. $data['description_type'] = $_POST['description_type'];
  425. $data['action'] = $action;
  426. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  427. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  428. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  429. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  430. // render to the view
  431. $this->view->set_data($data);
  432. $this->view->set_layout('layout');
  433. $this->view->set_template('thematic_plan');
  434. $this->view->render();
  435. }
  436. }
  437. }
  438. $thematic_id = intval($_GET['thematic_id']);
  439. if ($action == 'thematic_plan_list') {
  440. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  441. }
  442. $description_type = isset($_GET['description_type']) ? intval($_GET['description_type']) : null;
  443. if (!empty($thematic_id) && !empty($description_type)) {
  444. if ($action === 'thematic_plan_delete') {
  445. if (api_is_allowed_to_edit(null, true)) {
  446. $thematic->thematic_plan_destroy(
  447. $thematic_id,
  448. $description_type
  449. );
  450. }
  451. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  452. $action = 'thematic_plan_list';
  453. } else {
  454. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  455. }
  456. $data['thematic_id'] = $thematic_id;
  457. $data['description_type'] = $description_type;
  458. } elseif (!empty($thematic_id) && $action === 'thematic_plan_list') {
  459. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  460. $data['thematic_id'] = $thematic_id;
  461. }
  462. $data['thematic_id'] = $thematic_id;
  463. $data['action'] = $action;
  464. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  465. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  466. $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  467. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  468. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  469. // render to the view
  470. $this->view->set_data($data);
  471. $this->view->set_layout('layout');
  472. $this->view->set_template('thematic_plan');
  473. $this->view->render();
  474. exit;
  475. }
  476. /**
  477. * This method is used for thematic advance control (update, insert or listing)
  478. * render to thematic_advance.php.
  479. *
  480. * @param string $action
  481. */
  482. public function thematic_advance($action)
  483. {
  484. $thematic = new Thematic();
  485. $attendance = new Attendance();
  486. $data = [];
  487. $displayHeader = !empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header' ? false : true;
  488. // get data for attendance input select
  489. $attendance_list = $attendance->get_attendances_list();
  490. $attendance_select = [];
  491. $attendance_select[0] = get_lang('SelectAnAttendance');
  492. foreach ($attendance_list as $attendance_id => $attendance_data) {
  493. $attendance_select[$attendance_id] = $attendance_data['name'];
  494. }
  495. $thematic_id = intval($_REQUEST['thematic_id']);
  496. $thematic_advance_id = isset($_REQUEST['thematic_advance_id']) ? (int) $_REQUEST['thematic_advance_id'] : null;
  497. $thematic_advance_data = [];
  498. switch ($action) {
  499. case 'thematic_advance_delete':
  500. if (!empty($thematic_advance_id)) {
  501. if (api_is_allowed_to_edit(null, true)) {
  502. $thematic->thematic_advance_destroy($thematic_advance_id);
  503. }
  504. Display::addFlash(Display::return_message(get_lang('Deleted')));
  505. header('Location: index.php');
  506. exit;
  507. }
  508. break;
  509. case 'thematic_advance_list':
  510. if (!api_is_allowed_to_edit(null, true)) {
  511. echo '';
  512. exit;
  513. }
  514. $data['action'] = $_REQUEST['action'];
  515. $data['thematic_id'] = $_REQUEST['thematic_id'];
  516. $data['attendance_select'] = $attendance_select;
  517. if (isset($_REQUEST['thematic_advance_id'])) {
  518. $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
  519. $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
  520. $data['thematic_advance_data'] = $thematic_advance_data;
  521. }
  522. break;
  523. default:
  524. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  525. break;
  526. }
  527. // get calendar select by attendance id
  528. $calendar_select = [];
  529. if (!empty($thematic_advance_data)) {
  530. if (!empty($thematic_advance_data['attendance_id'])) {
  531. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  532. if (!empty($attendance_calendar)) {
  533. foreach ($attendance_calendar as $calendar) {
  534. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  535. }
  536. }
  537. }
  538. }
  539. $data['action'] = $action;
  540. $data['thematic_id'] = $thematic_id;
  541. $data['thematic_advance_id'] = $thematic_advance_id;
  542. $data['attendance_select'] = $attendance_select;
  543. $data['thematic_advance_data'] = $thematic_advance_data;
  544. $data['calendar_select'] = $calendar_select;
  545. $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
  546. // render to the view
  547. $this->view->set_data($data);
  548. $this->view->set_layout($layoutName);
  549. $this->view->set_template('thematic_advance');
  550. $this->view->render();
  551. }
  552. }