thematic_controller.php 23 KB

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