thematic_controller.php 22 KB

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