thematic_controller.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like controller for thematic, it should be included inside a dispatcher file (e.g: index.php)
  5. *
  6. * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC ! DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
  7. *
  8. * @author Christian Fasanando <christian1827@gmail.com>
  9. * @author Julio Montoya <gugli100@gmail.com> token support improving UI
  10. * @package chamilo.course_progress
  11. */
  12. /**
  13. * Thematic Controller script. Prepares the common background variables to give to the scripts corresponding to
  14. * the requested action
  15. * @package chamilo.course_progress
  16. */
  17. class ThematicController {
  18. /**
  19. * Constructor
  20. */
  21. public function __construct() {
  22. $this->toolname = 'course_progress';
  23. $this->view = new View($this->toolname);
  24. }
  25. /**
  26. * This method is used for thematic control (update, insert or listing)
  27. * @param string Action
  28. * render to thematic.php
  29. */
  30. public function thematic($action) {
  31. $thematic = new Thematic();
  32. $data = array();
  33. $error = false;
  34. $check = Security::check_token('request');
  35. $thematic_id = isset($_REQUEST['thematic_id']) ? intval($_REQUEST['thematic_id']) : null;
  36. if ($check) {
  37. switch ($action) {
  38. case 'thematic_add':
  39. case 'thematic_edit':
  40. // insert or update a thematic
  41. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  42. if (trim($_POST['title']) !== '') {
  43. if (api_is_allowed_to_edit(null, true)) {
  44. $id = $_POST['thematic_id'];
  45. $title = $_POST['title'];
  46. $content = $_POST['content'];
  47. $session_id = api_get_session_id();
  48. $thematic->set_thematic_attributes($id, $title, $content, $session_id);
  49. $last_id = $thematic->thematic_save();
  50. if ($_POST['action'] == 'thematic_add') {
  51. $action = 'thematic_details';
  52. $thematic_id = null;
  53. if ($last_id) {
  54. $data['last_id'] = $last_id;
  55. }
  56. } else {
  57. $action = 'thematic_details';
  58. $thematic_id = null;
  59. }
  60. }
  61. } else {
  62. $error = true;
  63. $data['error'] = $error;
  64. $data['action'] = $_POST['action'];
  65. $data['thematic_id'] = $_POST['thematic_id'];
  66. // render to the view
  67. $this->view->set_data($data);
  68. $this->view->set_layout('layout');
  69. $this->view->set_template('thematic');
  70. $this->view->render();
  71. }
  72. }
  73. break;
  74. case 'thematic_copy':
  75. //Copy a thematic to a session
  76. $thematic->copy($thematic_id);
  77. $thematic_id = null;
  78. $action = 'thematic_details';
  79. break;
  80. case 'thematic_delete_select':
  81. //Delete many thematics
  82. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  83. if (api_is_allowed_to_edit(null, true)) {
  84. $thematic_ids = $_POST['id'];
  85. $affected_rows = $thematic->thematic_destroy($thematic_ids);
  86. }
  87. $action = 'thematic_details';
  88. }
  89. break;
  90. case 'thematic_delete':
  91. // Delete a thematic
  92. if (isset($thematic_id)) {
  93. if (api_is_allowed_to_edit(null, true)) {
  94. $affected_rows = $thematic->thematic_destroy($thematic_id);
  95. }
  96. $thematic_id = null;
  97. $action = 'thematic_details';
  98. }
  99. break;
  100. case 'thematic_import_select':
  101. break;
  102. case 'thematic_import':
  103. $csv_import_array = Import::csv_to_array($_FILES['file']['tmp_name'], 'horizontal');
  104. if (isset($_POST['replace']) && $_POST['replace']) {
  105. // Remove current thematic.
  106. $list = $thematic->get_thematic_list();
  107. foreach ($list as $i) {
  108. $thematic->thematic_destroy($i);
  109. }
  110. }
  111. // Import the progress.
  112. $current_thematic = null;
  113. foreach ($csv_import_array as $data) {
  114. foreach ($data as $key => $item) {
  115. if ($key == 'title') {
  116. $thematic->set_thematic_attributes(null, $item[0], $item[1], api_get_session_id());
  117. $current_thematic = $thematic->thematic_save();
  118. $description_type = 0;
  119. }
  120. if ($key == 'plan') {
  121. $thematic->set_thematic_plan_attributes($current_thematic, $item[0], $item[1], $description_type);
  122. $thematic->thematic_plan_save();
  123. $description_type++;
  124. }
  125. if ($key == 'progress') {
  126. $thematic->set_thematic_advance_attributes(null, $current_thematic, 0, $item[2], $item[0], $item[1]);
  127. $thematic->thematic_advance_save();
  128. }
  129. }
  130. }
  131. $action = 'thematic_details';
  132. break;
  133. case 'thematic_export':
  134. $list = $thematic->get_thematic_list();
  135. $csv = array();
  136. foreach ($list as $theme) {
  137. $csv[] = array('title', $theme['title'], $theme['content']);
  138. $data = $thematic->get_thematic_plan_data($theme['id']);
  139. if (!empty($data)) {
  140. foreach ($data as $plan) {
  141. $csv[] = array('plan', $plan['title'], $plan['description']);
  142. }
  143. }
  144. $data = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
  145. if (!empty($data)) {
  146. foreach ($data as $advance) {
  147. $csv[] = array('progress', $advance['start_date'], $advance['duration'], $advance['content']);
  148. }
  149. }
  150. }
  151. Export::export_table_csv($csv);
  152. exit;
  153. // Don't continue building a normal page.
  154. return;
  155. case 'thematic_export_pdf':
  156. $list = $thematic->get_thematic_list();
  157. $table = array();
  158. $table[] = array(get_lang('Thematic'), get_lang('ThematicPlan'), get_lang('ThematicAdvance'));
  159. foreach ($list as $theme) {
  160. $data = $thematic->get_thematic_plan_data($theme['id']);
  161. $plan_html = null;
  162. if (!empty($data)) {
  163. foreach ($data as $plan) {
  164. $plan_html .= '<strong>' . $plan['title'] . '</strong><br /> ' . $plan['description'] . '<br />';
  165. }
  166. }
  167. $data = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
  168. $advance_html = null;
  169. if (!empty($data)) {
  170. foreach ($data as $advance) {
  171. $advance_html .= api_convert_and_format_date($advance['start_date'], DATE_FORMAT_LONG) . ' ('.$advance['duration'].' '.get_lang('HourShort').')<br />'.$advance['content'].'<br />';
  172. }
  173. }
  174. $table[] = array($theme['title'], $plan_html, $advance_html);
  175. }
  176. $params = array(
  177. 'filename' => get_lang('Thematic') . '-' . api_get_local_time(),
  178. 'pdf_title' => get_lang('Thematic'),
  179. 'add_signatures' => true,
  180. 'format' => 'A4-L',
  181. 'orientation' => 'L'
  182. );
  183. Export::export_table_pdf($table, $params);
  184. break;
  185. case 'moveup':
  186. $thematic->move_thematic('up', $thematic_id);
  187. $action = 'thematic_details';
  188. $thematic_id = null;
  189. break;
  190. case 'movedown':
  191. $thematic->move_thematic('down', $thematic_id);
  192. $action = 'thematic_details';
  193. $thematic_id = null;
  194. break;
  195. }
  196. Security::clear_token();
  197. } else {
  198. $action = 'thematic_details';
  199. $thematic_id = null;
  200. }
  201. if (isset($thematic_id)) {
  202. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  203. $data['thematic_id'] = $thematic_id;
  204. }
  205. if ($action == 'thematic_details') {
  206. if (isset($thematic_id)) {
  207. $thematic_data_result = $thematic->get_thematic_list($thematic_id);
  208. if (!empty($thematic_data_result)) {
  209. $thematic_data[$thematic_id] = $thematic_data_result;
  210. }
  211. $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
  212. } else {
  213. $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
  214. $data['max_thematic_item'] = $thematic->get_max_thematic_item();
  215. $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
  216. $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
  217. }
  218. //Second column
  219. $thematic_plan_data = $thematic->get_thematic_plan_data();
  220. //Third column
  221. $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true);
  222. $data['thematic_plan_div'] = $thematic->get_thematic_plan_div($thematic_plan_data);
  223. $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
  224. $data['thematic_plan_data'] = $thematic_plan_data;
  225. $data['thematic_advance_data'] = $thematic_advance_data;
  226. $data['thematic_data'] = $thematic_data;
  227. }
  228. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  229. $data['action'] = $action;
  230. // render to the view
  231. $this->view->set_data($data);
  232. $this->view->set_layout('layout');
  233. $this->view->set_template('thematic');
  234. $this->view->render();
  235. }
  236. /**
  237. * This method is used for thematic plan control (update, insert or listing)
  238. * @param string Action
  239. * render to thematic_plan.php
  240. */
  241. public function thematic_plan($action) {
  242. $thematic = new Thematic();
  243. $data = array();
  244. $error = false;
  245. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  246. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')) {
  247. if (isset($_POST['title'])) {
  248. if ($_POST['thematic_plan_token'] == $_SESSION['thematic_plan_token']) {
  249. if (api_is_allowed_to_edit(null, true)) {
  250. $title_list = $_REQUEST['title'];
  251. $description_list = $_REQUEST['description'];
  252. $description_type = $_REQUEST['description_type'];
  253. $delete_list = $_REQUEST['delete'];
  254. foreach ($title_list as $id => $title) {
  255. $thematic_plan_id = $id;
  256. if (!$thematic->get_thematic_data_by_id($id)) {
  257. $thematic_plan_id = null;
  258. }
  259. $thematic->set_thematic_plan_attributes($_REQUEST['thematic_id'], $title, $description_list[$id], $description_type[$id], $thematic_plan_id);
  260. if (isset($delete_list[$id]) && !empty($delete_list[$id])) {
  261. $thematic->thematic_plan_delete();
  262. } else {
  263. $affected_rows = $thematic->thematic_plan_save();
  264. }
  265. }
  266. unset($_SESSION['thematic_plan_token']);
  267. $data['message'] = 'ok';
  268. }
  269. $data['action'] = 'thematic_plan_list';
  270. }
  271. } else {
  272. $error = true;
  273. $action = $_POST['action'];
  274. $data['error'] = $error;
  275. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($_POST['thematic_id'], $_POST['description_type']);
  276. $data['thematic_id'] = $_POST['thematic_id'];
  277. $data['description_type'] = $_POST['description_type'];
  278. $data['action'] = $action;
  279. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  280. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  281. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  282. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  283. // render to the view
  284. $this->view->set_data($data);
  285. $this->view->set_layout('layout');
  286. $this->view->set_template('thematic_plan');
  287. $this->view->render();
  288. }
  289. }
  290. }
  291. $thematic_id = intval($_GET['thematic_id']);
  292. if ($action == 'thematic_plan_list') {
  293. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  294. }
  295. $description_type = intval($_GET['description_type']);
  296. if (!empty($thematic_id) && !empty($description_type)) {
  297. if ($action == 'thematic_plan_delete') {
  298. if (api_is_allowed_to_edit(null, true)) {
  299. $affected_rows = $thematic->thematic_plan_destroy($thematic_id, $description_type);
  300. }
  301. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  302. $action = 'thematic_plan_list';
  303. } else {
  304. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  305. }
  306. $data['thematic_id'] = $thematic_id;
  307. $data['description_type'] = $description_type;
  308. } else if (!empty($thematic_id) && $action == 'thematic_plan_list') {
  309. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  310. $data['thematic_id'] = $thematic_id;
  311. }
  312. $data['thematic_id'] = $thematic_id;
  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['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  317. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  318. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  319. //render to the view
  320. $this->view->set_data($data);
  321. $this->view->set_layout('layout');
  322. $this->view->set_template('thematic_plan');
  323. $this->view->render();
  324. exit;
  325. }
  326. /**
  327. * This method is used for thematic advance control (update, insert or listing)
  328. * @param string Action
  329. * render to thematic_advance.php
  330. */
  331. public function thematic_advance($action) {
  332. $thematic = new Thematic();
  333. $attendance = new Attendance();
  334. $data = array();
  335. // get data for attendance input select
  336. $attendance_list = $attendance->get_attendances_list();
  337. $attendance_select = array();
  338. $attendance_select[0] = get_lang('SelectAnAttendance');
  339. foreach ($attendance_list as $attendance_id => $attendance_data) {
  340. $attendance_select[$attendance_id] = $attendance_data['name'];
  341. }
  342. $thematic_id = intval($_REQUEST['thematic_id']);
  343. $thematic_advance_id = intval($_REQUEST['thematic_advance_id']);
  344. $thematic_advance_data = array();
  345. switch ($action) {
  346. case 'thematic_advance_delete':
  347. if (!empty($thematic_advance_id)) {
  348. if (api_is_allowed_to_edit(null, true)) {
  349. $affected_rows = $thematic->thematic_advance_destroy($thematic_advance_id);
  350. }
  351. $action = 'thematic_list';
  352. header('Location: index.php');
  353. exit;
  354. }
  355. break;
  356. case 'thematic_advance_list':
  357. if (!api_is_allowed_to_edit(null, true)) {
  358. echo '';
  359. exit;
  360. }
  361. if (($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) || (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours']))) {
  362. if ($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) {
  363. $start_date_error = true;
  364. $data['start_date_error'] = $start_date_error;
  365. }
  366. if (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours'])) {
  367. $duration_error = true;
  368. $data['duration_error'] = $duration_error;
  369. }
  370. $data['action'] = $_REQUEST['action'];
  371. $data['thematic_id'] = $_REQUEST['thematic_id'];
  372. $data['attendance_select'] = $attendance_select;
  373. if (isset($_REQUEST['thematic_advance_id'])) {
  374. $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
  375. $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
  376. $data['thematic_advance_data'] = $thematic_advance_data;
  377. }
  378. } else {
  379. if ($_REQUEST['thematic_advance_token'] == $_SESSION['thematic_advance_token'] && api_is_allowed_to_edit(null, true)) {
  380. $thematic_advance_id = $_REQUEST['thematic_advance_id'];
  381. $thematic_id = $_REQUEST['thematic_id'];
  382. $content = $_REQUEST['content'];
  383. $duration = $_REQUEST['duration_in_hours'];
  384. if (isset($_REQUEST['start_date_type']) && $_REQUEST['start_date_type'] == 2) {
  385. $start_date = $thematic->build_datetime_from_array($_REQUEST['custom_start_date']);
  386. $attendance_id = 0;
  387. } else {
  388. $start_date = $_REQUEST['start_date_by_attendance'];
  389. $attendance_id = $_REQUEST['attendance_select'];
  390. }
  391. $thematic->set_thematic_advance_attributes($thematic_advance_id, $thematic_id, $attendance_id, $content, $start_date, $duration);
  392. $affected_rows = $thematic->thematic_advance_save();
  393. if ($affected_rows) {
  394. // get last done thematic advance before move thematic list
  395. $last_done_thematic_advance = $thematic->get_last_done_thematic_advance();
  396. // update done advances with de current thematic list
  397. if (!empty($last_done_thematic_advance)) {
  398. $update_done_advances = $thematic->update_done_thematic_advances($last_done_thematic_advance);
  399. }
  400. }
  401. }
  402. }
  403. break;
  404. default:
  405. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  406. break;
  407. }
  408. // get calendar select by attendance id
  409. $calendar_select = array();
  410. if (!empty($thematic_advance_data)) {
  411. if (!empty($thematic_advance_data['attendance_id'])) {
  412. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  413. if (!empty($attendance_calendar)) {
  414. foreach ($attendance_calendar as $calendar) {
  415. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  416. }
  417. }
  418. }
  419. }
  420. $data['action'] = $action;
  421. $data['thematic_id'] = $thematic_id;
  422. $data['thematic_advance_id'] = $thematic_advance_id;
  423. $data['attendance_select'] = $attendance_select;
  424. $data['thematic_advance_data'] = $thematic_advance_data;
  425. $data['calendar_select'] = $calendar_select;
  426. // render to the view
  427. $this->view->set_data($data);
  428. $this->view->set_layout('layout');
  429. $this->view->set_template('thematic_advance');
  430. $this->view->render();
  431. }
  432. }