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. /**
  20. * Constructor
  21. */
  22. public function __construct()
  23. {
  24. $this->toolname = 'course_progress';
  25. $this->view = new View($this->toolname);
  26. }
  27. /**
  28. * This method is used for thematic control (update, insert or listing)
  29. * @param string Action
  30. * render to thematic.php
  31. */
  32. public function thematic($action)
  33. {
  34. $thematic = new Thematic();
  35. $data = array();
  36. $error = false;
  37. $msg_add = false;
  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 = $_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, api_get_course_id(), 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. $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
  236. // render to the view
  237. $this->view->set_data($data);
  238. $this->view->set_layout($layoutName);
  239. $this->view->set_template('thematic');
  240. $this->view->render();
  241. }
  242. /**
  243. * This method is used for thematic plan control (update, insert or listing)
  244. * @param string Action
  245. * render to thematic_plan.php
  246. */
  247. public function thematic_plan($action)
  248. {
  249. $thematic = new Thematic();
  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['desc'];
  259. $description_list = $_REQUEST['description'];
  260. $description_type = $_REQUEST['description_type'];
  261. for ($i = 1; $i < count($title_list) + 1; $i++) {
  262. $thematic->set_thematic_plan_attributes($_REQUEST['thematic_id'], $title_list[$i], $description_list[$i], $description_type[$i]);
  263. $affected_rows = $thematic->thematic_plan_save();
  264. }
  265. unset($_SESSION['thematic_plan_token']);
  266. $data['message'] = 'ok';
  267. }
  268. $data['action'] = 'thematic_plan_list';
  269. }
  270. } else {
  271. $error = true;
  272. $action = $_POST['action'];
  273. $data['error'] = $error;
  274. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($_POST['thematic_id'], $_POST['description_type']);
  275. $data['thematic_id'] = $_POST['thematic_id'];
  276. $data['description_type'] = $_POST['description_type'];
  277. $data['action'] = $action;
  278. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  279. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  280. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  281. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  282. // render to the view
  283. $this->view->set_data($data);
  284. $this->view->set_layout('layout');
  285. $this->view->set_template('thematic_plan');
  286. $this->view->render();
  287. }
  288. }
  289. }
  290. $thematic_id = intval($_GET['thematic_id']);
  291. if ($action == 'thematic_plan_list') {
  292. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  293. }
  294. $description_type = intval($_GET['description_type']);
  295. if (!empty($thematic_id) && !empty($description_type)) {
  296. if ($action == 'thematic_plan_delete') {
  297. if (api_is_allowed_to_edit(null, true)) {
  298. $affected_rows = $thematic->thematic_plan_destroy($thematic_id, $description_type);
  299. }
  300. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  301. $action = 'thematic_plan_list';
  302. } else {
  303. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  304. }
  305. $data['thematic_id'] = $thematic_id;
  306. $data['description_type'] = $description_type;
  307. } else if (!empty($thematic_id) && $action == 'thematic_plan_list') {
  308. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  309. $data['thematic_id'] = $thematic_id;
  310. }
  311. $data['thematic_id'] = $thematic_id;
  312. $data['action'] = $action;
  313. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  314. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  315. $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  316. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  317. $data['thematic_data'] = $thematic->get_thematic_list($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. exit;
  324. }
  325. /**
  326. * This method is used for thematic advance control (update, insert or listing)
  327. * @param string Action
  328. * render to thematic_advance.php
  329. */
  330. public function thematic_advance($action)
  331. {
  332. $thematic = new Thematic();
  333. $attendance = new Attendance();
  334. $data = array();
  335. $displayHeader = (!empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header') ? false : true;
  336. // get data for attendance input select
  337. $attendance_list = $attendance->get_attendances_list();
  338. $attendance_select = array();
  339. $attendance_select[0] = get_lang('SelectAnAttendance');
  340. foreach ($attendance_list as $attendance_id => $attendance_data) {
  341. $attendance_select[$attendance_id] = $attendance_data['name'];
  342. }
  343. $thematic_id = intval($_REQUEST['thematic_id']);
  344. $thematic_advance_id = intval($_REQUEST['thematic_advance_id']);
  345. $thematic_advance_data = array();
  346. switch ($action) {
  347. case 'thematic_advance_delete':
  348. if (!empty($thematic_advance_id)) {
  349. if (api_is_allowed_to_edit(null, true)) {
  350. $affected_rows = $thematic->thematic_advance_destroy($thematic_advance_id);
  351. }
  352. $action = 'thematic_list';
  353. header('Location: index.php');
  354. exit;
  355. }
  356. break;
  357. case 'thematic_advance_list':
  358. if (!api_is_allowed_to_edit(null, true)) {
  359. echo '';
  360. exit;
  361. }
  362. if (($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) || (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours']))) {
  363. if ($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) {
  364. $start_date_error = true;
  365. $data['start_date_error'] = $start_date_error;
  366. }
  367. if (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours'])) {
  368. $duration_error = true;
  369. $data['duration_error'] = $duration_error;
  370. }
  371. $data['action'] = $_REQUEST['action'];
  372. $data['thematic_id'] = $_REQUEST['thematic_id'];
  373. $data['attendance_select'] = $attendance_select;
  374. if (isset($_REQUEST['thematic_advance_id'])) {
  375. $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
  376. $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
  377. $data['thematic_advance_data'] = $thematic_advance_data;
  378. }
  379. } else {
  380. if ($_REQUEST['thematic_advance_token'] == $_SESSION['thematic_advance_token'] && api_is_allowed_to_edit(null, true)) {
  381. $thematic_advance_id = $_REQUEST['thematic_advance_id'];
  382. $thematic_id = $_REQUEST['thematic_id'];
  383. $content = $_REQUEST['content'];
  384. $duration = $_REQUEST['duration_in_hours'];
  385. if (isset($_REQUEST['start_date_type']) && $_REQUEST['start_date_type'] == 2) {
  386. $start_date = $thematic->build_datetime_from_array($_REQUEST['custom_start_date']);
  387. $attendance_id = 0;
  388. } else {
  389. $start_date = $_REQUEST['start_date_by_attendance'];
  390. $attendance_id = $_REQUEST['attendance_select'];
  391. }
  392. $thematic->set_thematic_advance_attributes($thematic_advance_id, $thematic_id, $attendance_id, $content, $start_date, $duration);
  393. $affected_rows = $thematic->thematic_advance_save();
  394. if ($affected_rows) {
  395. // get last done thematic advance before move thematic list
  396. $last_done_thematic_advance = $thematic->get_last_done_thematic_advance();
  397. // update done advances with de current thematic list
  398. if (!empty($last_done_thematic_advance)) {
  399. $update_done_advances = $thematic->update_done_thematic_advances($last_done_thematic_advance);
  400. }
  401. }
  402. }
  403. }
  404. break;
  405. default:
  406. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  407. break;
  408. }
  409. // get calendar select by attendance id
  410. $calendar_select = array();
  411. if (!empty($thematic_advance_data)) {
  412. if (!empty($thematic_advance_data['attendance_id'])) {
  413. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  414. if (!empty($attendance_calendar)) {
  415. foreach ($attendance_calendar as $calendar) {
  416. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  417. }
  418. }
  419. }
  420. }
  421. $data['action'] = $action;
  422. $data['thematic_id'] = $thematic_id;
  423. $data['thematic_advance_id'] = $thematic_advance_id;
  424. $data['attendance_select'] = $attendance_select;
  425. $data['thematic_advance_data'] = $thematic_advance_data;
  426. $data['calendar_select'] = $calendar_select;
  427. $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
  428. // render to the view
  429. $this->view->set_data($data);
  430. $this->view->set_layout($layoutName);
  431. $this->view->set_template('thematic_advance');
  432. $this->view->render();
  433. }
  434. }