thematic_controller.php 22 KB

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