thematic_controller.php 24 KB

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