thematic_controller.php 23 KB

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