thematic_controller.php 24 KB

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