thematic_controller.php 26 KB

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