thematic_controller.php 26 KB

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