thematic_controller.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 'moveup':
  158. $thematic->move_thematic('up', $thematic_id);
  159. $action = 'thematic_details';
  160. $thematic_id = null;
  161. break;
  162. case 'movedown':
  163. $thematic->move_thematic('down', $thematic_id);
  164. $action = 'thematic_details';
  165. $thematic_id = null;
  166. break;
  167. }
  168. Security::clear_token();
  169. } else {
  170. $action = 'thematic_details';
  171. $thematic_id = null;
  172. }
  173. if (isset($thematic_id)) {
  174. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  175. $data['thematic_id'] = $thematic_id;
  176. }
  177. if ($action == 'thematic_details') {
  178. if (isset($thematic_id)) {
  179. $thematic_data_result = $thematic->get_thematic_list($thematic_id);
  180. if (!empty($thematic_data_result)) {
  181. $thematic_data[$thematic_id] = $thematic_data_result;
  182. }
  183. $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
  184. } else {
  185. $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
  186. $data['max_thematic_item'] = $thematic->get_max_thematic_item();
  187. $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
  188. $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
  189. }
  190. //Second column
  191. $thematic_plan_data = $thematic->get_thematic_plan_data();
  192. //Third column
  193. $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true);
  194. $data['thematic_plan_div'] = $thematic->get_thematic_plan_div($thematic_plan_data);
  195. $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
  196. $data['thematic_plan_data'] = $thematic_plan_data;
  197. $data['thematic_advance_data'] = $thematic_advance_data;
  198. $data['thematic_data'] = $thematic_data;
  199. }
  200. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  201. $data['action'] = $action;
  202. // render to the view
  203. $this->view->set_data($data);
  204. $this->view->set_layout('layout');
  205. $this->view->set_template('thematic');
  206. $this->view->render();
  207. }
  208. /**
  209. * This method is used for thematic plan control (update, insert or listing)
  210. * @param string Action
  211. * render to thematic_plan.php
  212. */
  213. public function thematic_plan($action) {
  214. $thematic= new Thematic();
  215. $data = array();
  216. $error = false;
  217. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  218. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')) {
  219. if (isset($_POST['title'])) {
  220. if ($_POST['thematic_plan_token'] == $_SESSION['thematic_plan_token']) {
  221. if (api_is_allowed_to_edit(null, true)) {
  222. $title_list = $_REQUEST['title'];
  223. //$description_list = $_REQUEST['desc'];
  224. $description_list = $_REQUEST['description'];
  225. $description_type = $_REQUEST['description_type'];
  226. for($i=1;$i<count($title_list)+1; $i++) {
  227. $thematic->set_thematic_plan_attributes($_REQUEST['thematic_id'], $title_list[$i], $description_list[$i], $description_type[$i]);
  228. $affected_rows = $thematic->thematic_plan_save();
  229. }
  230. unset($_SESSION['thematic_plan_token']);
  231. $data['message'] = 'ok';
  232. }
  233. $data['action'] = 'thematic_plan_list';
  234. }
  235. } else {
  236. $error = true;
  237. $action = $_POST['action'];
  238. $data['error'] = $error;
  239. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($_POST['thematic_id'], $_POST['description_type']);
  240. $data['thematic_id'] = $_POST['thematic_id'];
  241. $data['description_type'] = $_POST['description_type'];
  242. $data['action'] = $action;
  243. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  244. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  245. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  246. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  247. // render to the view
  248. $this->view->set_data($data);
  249. $this->view->set_layout('layout');
  250. $this->view->set_template('thematic_plan');
  251. $this->view->render();
  252. }
  253. }
  254. }
  255. if ($action == 'thematic_plan_list') {
  256. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  257. }
  258. $thematic_id = intval($_GET['thematic_id']);
  259. $description_type = intval($_GET['description_type']);
  260. if (!empty($thematic_id) && !empty($description_type)) {
  261. if ($action == 'thematic_plan_delete') {
  262. if (api_is_allowed_to_edit(null, true)) {
  263. $affected_rows = $thematic->thematic_plan_destroy($thematic_id, $description_type);
  264. }
  265. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  266. $action = 'thematic_plan_list';
  267. } else {
  268. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  269. }
  270. $data['thematic_id'] = $thematic_id;
  271. $data['description_type'] = $description_type;
  272. } else if (!empty($thematic_id) && $action == 'thematic_plan_list') {
  273. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  274. $data['thematic_id'] = $thematic_id;
  275. }
  276. $data['thematic_id'] = $thematic_id;
  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['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  281. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  282. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  283. //render to the view
  284. $this->view->set_data($data);
  285. $this->view->set_layout('layout');
  286. $this->view->set_template('thematic_plan');
  287. $this->view->render();
  288. exit;
  289. }
  290. /**
  291. * This method is used for thematic advance control (update, insert or listing)
  292. * @param string Action
  293. * render to thematic_advance.php
  294. */
  295. public function thematic_advance($action) {
  296. $thematic= new Thematic();
  297. $attendance = new Attendance();
  298. $data = array();
  299. // get data for attendance input select
  300. $attendance_list = $attendance->get_attendances_list();
  301. $attendance_select = array();
  302. $attendance_select[0] = get_lang('SelectAnAttendance');
  303. foreach ($attendance_list as $attendance_id => $attendance_data) {
  304. $attendance_select[$attendance_id] = $attendance_data['name'];
  305. }
  306. $thematic_id = intval($_REQUEST['thematic_id']);
  307. $thematic_advance_id = intval($_REQUEST['thematic_advance_id']);
  308. $thematic_advance_data = array();
  309. switch ($action) {
  310. case 'thematic_advance_delete':
  311. if (!empty($thematic_advance_id)) {
  312. if (api_is_allowed_to_edit(null, true)) {
  313. $affected_rows = $thematic->thematic_advance_destroy($thematic_advance_id);
  314. }
  315. $action = 'thematic_list';
  316. header('Location: index.php');
  317. exit;
  318. }
  319. break;
  320. case 'thematic_advance_list':
  321. if (!api_is_allowed_to_edit(null, true)) {
  322. echo '';
  323. exit;
  324. }
  325. if (($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) || (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours'])) ) {
  326. if ($_REQUEST['start_date_type'] == 1 && empty($_REQUEST['start_date_by_attendance'])) {
  327. $start_date_error = true;
  328. $data['start_date_error'] = $start_date_error;
  329. }
  330. if (!empty($_REQUEST['duration_in_hours']) && !is_numeric($_REQUEST['duration_in_hours'])) {
  331. $duration_error = true;
  332. $data['duration_error'] = $duration_error;
  333. }
  334. $data['action'] = $_REQUEST['action'];
  335. $data['thematic_id'] = $_REQUEST['thematic_id'];
  336. $data['attendance_select'] = $attendance_select;
  337. if (isset($_REQUEST['thematic_advance_id'])) {
  338. $data['thematic_advance_id'] = $_REQUEST['thematic_advance_id'];
  339. $thematic_advance_data = $thematic->get_thematic_advance_list($_REQUEST['thematic_advance_id']);
  340. $data['thematic_advance_data'] = $thematic_advance_data;
  341. }
  342. } else {
  343. if ($_REQUEST['thematic_advance_token'] == $_SESSION['thematic_advance_token'] && api_is_allowed_to_edit(null, true)) {
  344. $thematic_advance_id = $_REQUEST['thematic_advance_id'];
  345. $thematic_id = $_REQUEST['thematic_id'];
  346. $content = $_REQUEST['content'];
  347. $duration = $_REQUEST['duration_in_hours'];
  348. if (isset($_REQUEST['start_date_type']) && $_REQUEST['start_date_type'] == 2) {
  349. $start_date = $thematic->build_datetime_from_array($_REQUEST['custom_start_date']);
  350. $attendance_id = 0;
  351. } else {
  352. $start_date = $_REQUEST['start_date_by_attendance'];
  353. $attendance_id = $_REQUEST['attendance_select'];
  354. }
  355. $thematic->set_thematic_advance_attributes($thematic_advance_id, $thematic_id, $attendance_id, $content, $start_date, $duration);
  356. $affected_rows = $thematic->thematic_advance_save();
  357. if ($affected_rows) {
  358. // get last done thematic advance before move thematic list
  359. $last_done_thematic_advance = $thematic->get_last_done_thematic_advance();
  360. // update done advances with de current thematic list
  361. if (!empty($last_done_thematic_advance)) {
  362. $update_done_advances = $thematic->update_done_thematic_advances($last_done_thematic_advance);
  363. }
  364. }
  365. }
  366. }
  367. break;
  368. default:
  369. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  370. break;
  371. }
  372. // get calendar select by attendance id
  373. $calendar_select = array();
  374. if (!empty($thematic_advance_data)) {
  375. if (!empty($thematic_advance_data['attendance_id'])) {
  376. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  377. if (!empty($attendance_calendar)) {
  378. foreach ($attendance_calendar as $calendar) {
  379. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  380. }
  381. }
  382. }
  383. }
  384. $data['action'] = $action;
  385. $data['thematic_id'] = $thematic_id;
  386. $data['thematic_advance_id'] = $thematic_advance_id;
  387. $data['attendance_select'] = $attendance_select;
  388. $data['thematic_advance_data'] = $thematic_advance_data;
  389. $data['calendar_select'] = $calendar_select;
  390. // render to the view
  391. $this->view->set_data($data);
  392. $this->view->set_layout('layout');
  393. $this->view->set_template('thematic_advance');
  394. $this->view->render();
  395. }
  396. }