thematic_controller.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 'moveup':
  103. $thematic->move_thematic('up', $thematic_id);
  104. $action = 'thematic_details';
  105. $thematic_id = null;
  106. break;
  107. case 'movedown':
  108. $thematic->move_thematic('down', $thematic_id);
  109. $action = 'thematic_details';
  110. $thematic_id = null;
  111. break;
  112. }
  113. Security::clear_token();
  114. } else {
  115. $action = 'thematic_details';
  116. $thematic_id = null;
  117. }
  118. if (isset($thematic_id)) {
  119. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  120. $data['thematic_id'] = $thematic_id;
  121. }
  122. if ($action == 'thematic_details') {
  123. if (isset($thematic_id)) {
  124. $thematic_data_result = $thematic->get_thematic_list($thematic_id);
  125. if (!empty($thematic_data_result)) {
  126. $thematic_data[$thematic_id] = $thematic_data_result;
  127. }
  128. $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
  129. } else {
  130. $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
  131. $data['max_thematic_item'] = $thematic->get_max_thematic_item();
  132. $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
  133. $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
  134. }
  135. //Second column
  136. $thematic_plan_data = $thematic->get_thematic_plan_data();
  137. //Third column
  138. $thematic_advance_data = $thematic->get_thematic_advance_list(null, null, true);
  139. $data['thematic_plan_div'] = $thematic->get_thematic_plan_div($thematic_plan_data);
  140. $data['thematic_advance_div'] = $thematic->get_thematic_advance_div($thematic_advance_data);
  141. $data['thematic_plan_data'] = $thematic_plan_data;
  142. $data['thematic_advance_data'] = $thematic_advance_data;
  143. $data['thematic_data'] = $thematic_data;
  144. }
  145. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  146. $data['action'] = $action;
  147. // render to the view
  148. $this->view->set_data($data);
  149. $this->view->set_layout('layout');
  150. $this->view->set_template('thematic');
  151. $this->view->render();
  152. }
  153. /**
  154. * This method is used for thematic plan control (update, insert or listing)
  155. * @param string Action
  156. * render to thematic_plan.php
  157. */
  158. public function thematic_plan($action) {
  159. $thematic= new Thematic();
  160. $data = array();
  161. $error = false;
  162. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  163. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')) {
  164. if (trim($_POST['title']) !== '') {
  165. if ($_POST['thematic_plan_token'] == $_SESSION['thematic_plan_token']) {
  166. if (api_is_allowed_to_edit(null, true)) {
  167. $thematic_id = $_POST['thematic_id'];
  168. $title = $_POST['title'];
  169. $description = $_POST['description'];
  170. $description_type = $_POST['description_type'];
  171. $thematic->set_thematic_plan_attributes($thematic_id, $title, $description, $description_type);
  172. $affected_rows = $thematic->thematic_plan_save();
  173. unset($_SESSION['thematic_plan_token']);
  174. $data['message'] = 'ok';
  175. }
  176. $data['action'] = 'thematic_plan_list';
  177. }
  178. } else {
  179. $error = true;
  180. $action = $_POST['action'];
  181. $data['error'] = $error;
  182. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($_POST['thematic_id'], $_POST['description_type']);
  183. $data['thematic_id'] = $_POST['thematic_id'];
  184. $data['description_type'] = $_POST['description_type'];
  185. $data['action'] = $action;
  186. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  187. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  188. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  189. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  190. // render to the view
  191. $this->view->set_data($data);
  192. $this->view->set_layout('layout');
  193. $this->view->set_template('thematic_plan');
  194. $this->view->render();
  195. }
  196. }
  197. }
  198. if ($action == 'thematic_plan_list') {
  199. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  200. }
  201. $thematic_id = intval($_GET['thematic_id']);
  202. $description_type = intval($_GET['description_type']);
  203. if (!empty($thematic_id) && !empty($description_type)) {
  204. if ($action == 'thematic_plan_delete') {
  205. if (api_is_allowed_to_edit(null, true)) {
  206. $affected_rows = $thematic->thematic_plan_destroy($thematic_id, $description_type);
  207. }
  208. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  209. $action = 'thematic_plan_list';
  210. } else {
  211. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  212. }
  213. $data['thematic_id'] = $thematic_id;
  214. $data['description_type'] = $description_type;
  215. } else if (!empty($thematic_id) && $action == 'thematic_plan_list') {
  216. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  217. $data['thematic_id'] = $thematic_id;
  218. }
  219. $data['thematic_id'] = $thematic_id;
  220. $data['action'] = $action;
  221. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  222. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  223. $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  224. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  225. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  226. //render to the view
  227. $this->view->set_data($data);
  228. $this->view->set_layout('layout_no_header');
  229. $this->view->set_template('thematic_plan');
  230. $this->view->render();
  231. exit;
  232. }
  233. /**
  234. * This method is used for thematic advance control (update, insert or listing)
  235. * @param string Action
  236. * render to thematic_advance.php
  237. */
  238. public function thematic_advance($action) {
  239. $thematic= new Thematic();
  240. $attendance = new Attendance();
  241. $data = array();
  242. // get data for attendance input select
  243. $attendance_list = $attendance->get_attendances_list();
  244. $attendance_select = array();
  245. $attendance_select[0] = get_lang('SelectAnAttendance');
  246. foreach ($attendance_list as $attendance_id => $attendance_data) {
  247. $attendance_select[$attendance_id] = $attendance_data['name'];
  248. }
  249. $thematic_id = intval($_GET['thematic_id']);
  250. $thematic_advance_id = intval($_GET['thematic_advance_id']);
  251. $thematic_advance_data = array();
  252. if (!empty($thematic_advance_id)) {
  253. if ($action == 'thematic_advance_delete') {
  254. if (api_is_allowed_to_edit(null, true)) {
  255. $affected_rows = $thematic->thematic_advance_destroy($thematic_advance_id);
  256. }
  257. $action = 'thematic_list';
  258. header('Location: index.php');
  259. exit;
  260. } else {
  261. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  262. }
  263. }
  264. // get calendar select by attendance id
  265. $calendar_select = array();
  266. if (!empty($thematic_advance_data)) {
  267. if (!empty($thematic_advance_data['attendance_id'])) {
  268. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  269. if (!empty($attendance_calendar)) {
  270. foreach ($attendance_calendar as $calendar) {
  271. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  272. }
  273. }
  274. }
  275. }
  276. $data['action'] = $action;
  277. $data['thematic_id'] = $thematic_id;
  278. $data['thematic_advance_id'] = $thematic_advance_id;
  279. $data['attendance_select'] = $attendance_select;
  280. $data['thematic_advance_data'] = $thematic_advance_data;
  281. $data['calendar_select'] = $calendar_select;
  282. // render to the view
  283. $this->view->set_data($data);
  284. $this->view->set_layout('layout_no_header');
  285. $this->view->set_template('thematic_advance');
  286. $this->view->render();
  287. }
  288. }