thematic_controller.php 23 KB

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