thematic_controller.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Thematic Controller script.
  5. * Prepares the common background variables to give to the scripts corresponding to
  6. * the requested action
  7. *
  8. * This file contains class used like controller for thematic,
  9. * it should be included inside a dispatcher file (e.g: index.php)
  10. *
  11. * !!! WARNING !!! : ALL DATES IN THIS MODULE ARE STORED IN UTC !
  12. * DO NOT CONVERT DURING THE TRANSITION FROM CHAMILO 1.8.x TO 2.0
  13. *
  14. * @author Christian Fasanando <christian1827@gmail.com>
  15. * @author Julio Montoya <gugli100@gmail.com> token support improving UI
  16. *
  17. * @package chamilo.course_progress
  18. */
  19. class ThematicController
  20. {
  21. /**
  22. * Constructor
  23. */
  24. public function __construct()
  25. {
  26. $this->toolname = 'course_progress';
  27. $this->view = new View($this->toolname);
  28. }
  29. /**
  30. * This method is used for thematic control (update, insert or listing)
  31. * @param string Action
  32. * render to thematic.php
  33. */
  34. public function thematic($action)
  35. {
  36. $thematic = new Thematic();
  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 = isset($_POST['thematic_id']) ? $_POST['thematic_id'] : null;
  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(
  166. get_lang('Thematic'),
  167. get_lang('ThematicPlan'),
  168. get_lang('ThematicAdvance')
  169. );
  170. foreach ($list as $theme) {
  171. $data = $thematic->get_thematic_plan_data($theme['id']);
  172. $plan_html = null;
  173. if (!empty($data)) {
  174. foreach ($data as $plan) {
  175. $plan_html .= '<strong>' . $plan['title'] . '</strong><br /> ' . $plan['description'] . '<br />';
  176. }
  177. }
  178. $data = $thematic->get_thematic_advance_by_thematic_id($theme['id']);
  179. $advance_html = null;
  180. if (!empty($data)) {
  181. foreach ($data as $advance) {
  182. $advance_html .= api_convert_and_format_date($advance['start_date'], DATE_FORMAT_LONG) . ' ('.$advance['duration'].' '.get_lang('HourShort').')<br />'.$advance['content'].'<br />';
  183. }
  184. }
  185. $table[] = array($theme['title'], $plan_html, $advance_html);
  186. }
  187. $params = array(
  188. 'filename' => get_lang('Thematic') . '-' . api_get_local_time(),
  189. 'pdf_title' => get_lang('Thematic'),
  190. 'add_signatures' => true,
  191. 'format' => 'A4-L',
  192. 'orientation' => 'L'
  193. );
  194. Export::export_table_pdf($table, $params);
  195. break;
  196. case 'moveup':
  197. $thematic->move_thematic('up', $thematic_id);
  198. $action = 'thematic_details';
  199. $thematic_id = null;
  200. break;
  201. case 'movedown':
  202. $thematic->move_thematic('down', $thematic_id);
  203. $action = 'thematic_details';
  204. $thematic_id = null;
  205. break;
  206. }
  207. Security::clear_token();
  208. } else {
  209. $action = 'thematic_details';
  210. $thematic_id = null;
  211. }
  212. if (isset($thematic_id)) {
  213. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  214. $data['thematic_id'] = $thematic_id;
  215. }
  216. if ($action == 'thematic_details') {
  217. if (isset($thematic_id)) {
  218. $thematic_data_result = $thematic->get_thematic_list($thematic_id);
  219. if (!empty($thematic_data_result)) {
  220. $thematic_data[$thematic_id] = $thematic_data_result;
  221. }
  222. $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
  223. } else {
  224. $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
  225. $data['max_thematic_item'] = $thematic->get_max_thematic_item();
  226. $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
  227. $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
  228. }
  229. //Second column
  230. $thematic_plan_data = $thematic->get_thematic_plan_data();
  231. //Third column
  232. $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true);
  233. $data['thematic_plan_div'] = $thematic->get_thematic_plan_div($thematic_plan_data);
  234. $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
  235. $data['thematic_plan_data'] = $thematic_plan_data;
  236. $data['thematic_advance_data'] = $thematic_advance_data;
  237. $data['thematic_data'] = $thematic_data;
  238. }
  239. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  240. $data['action'] = $action;
  241. $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
  242. // render to the view
  243. $this->view->set_data($data);
  244. $this->view->set_layout($layoutName);
  245. $this->view->set_template('thematic');
  246. $this->view->render();
  247. }
  248. /**
  249. * This method is used for thematic plan control (update, insert or listing)
  250. * @param string $action
  251. * render to thematic_plan.php
  252. */
  253. public function thematic_plan($action)
  254. {
  255. $thematic = new Thematic();
  256. $data = array();
  257. $error = false;
  258. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  259. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')) {
  260. if (isset($_POST['title'])) {
  261. if ($_POST['thematic_plan_token'] == $_SESSION['thematic_plan_token']) {
  262. if (api_is_allowed_to_edit(null, true)) {
  263. $title_list = $_REQUEST['title'];
  264. $description_list = $_REQUEST['description'];
  265. $description_type = $_REQUEST['description_type'];
  266. for ($i = 1; $i < count($title_list) + 1; $i++) {
  267. $thematic->set_thematic_plan_attributes(
  268. $_REQUEST['thematic_id'],
  269. $title_list[$i],
  270. $description_list[$i],
  271. $description_type[$i]
  272. );
  273. $thematic->thematic_plan_save();
  274. }
  275. unset($_SESSION['thematic_plan_token']);
  276. $data['message'] = 'ok';
  277. $saveRedirect = api_get_path(WEB_PATH) . 'main/course_progress/index.php?';
  278. $saveRedirect.= api_get_cidreq() . '&';
  279. $saveRedirect.= 'thematic_plan_save_message=ok';
  280. header("Location: $saveRedirect");
  281. exit;
  282. }
  283. $data['action'] = 'thematic_plan_list';
  284. }
  285. } else {
  286. $error = true;
  287. $action = $_POST['action'];
  288. $data['error'] = $error;
  289. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($_POST['thematic_id'], $_POST['description_type']);
  290. $data['thematic_id'] = $_POST['thematic_id'];
  291. $data['description_type'] = $_POST['description_type'];
  292. $data['action'] = $action;
  293. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  294. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  295. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  296. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  297. // render to the view
  298. $this->view->set_data($data);
  299. $this->view->set_layout('layout');
  300. $this->view->set_template('thematic_plan');
  301. $this->view->render();
  302. }
  303. }
  304. }
  305. $thematic_id = intval($_GET['thematic_id']);
  306. if ($action == 'thematic_plan_list') {
  307. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  308. }
  309. $description_type = isset($_GET['description_type']) ? intval($_GET['description_type']) : null;
  310. if (!empty($thematic_id) && !empty($description_type)) {
  311. if ($action == 'thematic_plan_delete') {
  312. if (api_is_allowed_to_edit(null, true)) {
  313. $affected_rows = $thematic->thematic_plan_destroy($thematic_id, $description_type);
  314. }
  315. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  316. $action = 'thematic_plan_list';
  317. } else {
  318. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  319. }
  320. $data['thematic_id'] = $thematic_id;
  321. $data['description_type'] = $description_type;
  322. } else if (!empty($thematic_id) && $action == 'thematic_plan_list') {
  323. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  324. $data['thematic_id'] = $thematic_id;
  325. }
  326. $data['thematic_id'] = $thematic_id;
  327. $data['action'] = $action;
  328. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  329. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  330. $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  331. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  332. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  333. //render to the view
  334. $this->view->set_data($data);
  335. $this->view->set_layout('layout');
  336. $this->view->set_template('thematic_plan');
  337. $this->view->render();
  338. exit;
  339. }
  340. /**
  341. * This method is used for thematic advance control (update, insert or listing)
  342. * render to thematic_advance.php
  343. * @param string $action
  344. *
  345. */
  346. public function thematic_advance($action)
  347. {
  348. $thematic = new Thematic();
  349. $attendance = new Attendance();
  350. $data = array();
  351. $displayHeader = (!empty($_REQUEST['display']) && $_REQUEST['display'] === 'no_header') ? false : true;
  352. // get data for attendance input select
  353. $attendance_list = $attendance->get_attendances_list();
  354. $attendance_select = array();
  355. $attendance_select[0] = get_lang('SelectAnAttendance');
  356. foreach ($attendance_list as $attendance_id => $attendance_data) {
  357. $attendance_select[$attendance_id] = $attendance_data['name'];
  358. }
  359. $thematic_id = intval($_REQUEST['thematic_id']);
  360. $thematic_advance_id = isset($_REQUEST['thematic_advance_id']) ? intval($_REQUEST['thematic_advance_id']) : null;
  361. $thematic_advance_data = array();
  362. switch ($action) {
  363. case 'thematic_advance_delete':
  364. if (!empty($thematic_advance_id)) {
  365. if (api_is_allowed_to_edit(null, true)) {
  366. $thematic->thematic_advance_destroy($thematic_advance_id);
  367. }
  368. header('Location: index.php');
  369. exit;
  370. }
  371. break;
  372. case 'thematic_advance_list':
  373. if (!api_is_allowed_to_edit(null, true)) {
  374. echo '';
  375. exit;
  376. }
  377. if ((isset($_REQUEST['start_date_type']) && $_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) ||
  378. (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours']))
  379. ) {
  380. if ($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) {
  381. $start_date_error = true;
  382. $data['start_date_error'] = $start_date_error;
  383. }
  384. if (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours'])) {
  385. $duration_error = true;
  386. $data['duration_error'] = $duration_error;
  387. }
  388. $data['action'] = $_REQUEST['action'];
  389. $data['thematic_id'] = $_REQUEST['thematic_id'];
  390. $data['attendance_select'] = $attendance_select;
  391. if (isset($_REQUEST['thematic_advance_id'])) {
  392. $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
  393. $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
  394. $data['thematic_advance_data'] = $thematic_advance_data;
  395. }
  396. } else {
  397. if (api_is_allowed_to_edit(null, true)) {
  398. $thematic_advance_id = isset($_REQUEST['thematic_advance_id']) ? $_REQUEST['thematic_advance_id'] : null;
  399. $thematic_id = $_REQUEST['thematic_id'];
  400. $content = isset($_REQUEST['content']) ? $_REQUEST['content'] : null;
  401. $duration = isset($_REQUEST['duration_in_hours']) ? $_REQUEST['duration_in_hours'] : null;
  402. if (isset($_REQUEST['start_date_type']) && $_REQUEST['start_date_type'] == 2) {
  403. $start_date = $_REQUEST['custom_start_date'];
  404. $attendance_id = 0;
  405. } else {
  406. $start_date = isset($_REQUEST['start_date_by_attendance']) ? $_REQUEST['start_date_by_attendance'] : null;
  407. $attendance_id = isset($_REQUEST['attendance_select']) ? $_REQUEST['attendance_select'] : null;
  408. }
  409. $thematic->set_thematic_advance_attributes(
  410. $thematic_advance_id,
  411. $thematic_id,
  412. $attendance_id,
  413. $content,
  414. $start_date,
  415. $duration
  416. );
  417. $affected_rows = $thematic->thematic_advance_save();
  418. if ($affected_rows) {
  419. // get last done thematic advance before move thematic list
  420. $last_done_thematic_advance = $thematic->get_last_done_thematic_advance();
  421. // update done advances with de current thematic list
  422. if (!empty($last_done_thematic_advance)) {
  423. $thematic->update_done_thematic_advances($last_done_thematic_advance);
  424. }
  425. }
  426. }
  427. }
  428. break;
  429. default:
  430. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  431. break;
  432. }
  433. // get calendar select by attendance id
  434. $calendar_select = array();
  435. if (!empty($thematic_advance_data)) {
  436. if (!empty($thematic_advance_data['attendance_id'])) {
  437. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  438. if (!empty($attendance_calendar)) {
  439. foreach ($attendance_calendar as $calendar) {
  440. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  441. }
  442. }
  443. }
  444. }
  445. $data['action'] = $action;
  446. $data['thematic_id'] = $thematic_id;
  447. $data['thematic_advance_id'] = $thematic_advance_id;
  448. $data['attendance_select'] = $attendance_select;
  449. $data['thematic_advance_data'] = $thematic_advance_data;
  450. $data['calendar_select'] = $calendar_select;
  451. $layoutName = $displayHeader ? 'layout' : 'layout_no_header';
  452. // render to the view
  453. $this->view->set_data($data);
  454. $this->view->set_layout($layoutName);
  455. $this->view->set_template('thematic_advance');
  456. $this->view->render();
  457. }
  458. }