thematic_controller.php 24 KB

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