thematic_controller.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. * @package chamilo.course_progress
  10. */
  11. /**
  12. * Thematic Controller script. Prepares the common background variables to give to the scripts corresponding to
  13. * the requested action
  14. * @package chamilo.course_progress
  15. */
  16. class ThematicController
  17. {
  18. /**
  19. * Constructor
  20. */
  21. public function __construct() {
  22. $this->toolname = 'course_progress';
  23. $this->view = new View($this->toolname);
  24. }
  25. /**
  26. * This method is used for thematic control (update, insert or listing)
  27. * @param string Action
  28. * render to thematic.php
  29. */
  30. public function thematic($action) {
  31. $thematic= new Thematic();
  32. $data = array();
  33. $error = false;
  34. $msg_add = false;
  35. // insert or update a thematic
  36. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  37. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_add' || $_POST['action'] == 'thematic_edit')) {
  38. if (trim($_POST['title']) !== '') {
  39. if ($_POST['thematic_token'] == $_SESSION['thematic_token']) {
  40. if (api_is_allowed_to_edit(null, true)) {
  41. $id = $_POST['thematic_id'];
  42. $title = $_POST['title'];
  43. $content = $_POST['content'];
  44. $session_id = api_get_session_id();
  45. $thematic->set_thematic_attributes($id, $title, $content, $session_id);
  46. $last_id = $thematic->thematic_save();
  47. if ($_POST['action'] == 'thematic_add') {
  48. $action = 'thematic_details';
  49. if ($last_id) {
  50. $data['last_id'] = $last_id;
  51. }
  52. } else {
  53. $action = 'thematic_list';
  54. }
  55. }
  56. unset($_SESSION['thematic_token']);
  57. }
  58. } else {
  59. $error = true;
  60. $data['error'] = $error;
  61. $data['action'] = $_POST['action'];
  62. $data['thematic_id'] = $_POST['thematic_id'];
  63. // render to the view
  64. $this->view->set_data($data);
  65. $this->view->set_layout('layout');
  66. $this->view->set_template('thematic');
  67. $this->view->render();
  68. }
  69. }
  70. }
  71. //Copy a thematic to a session
  72. if ($action == 'thematic_copy') {
  73. $thematic->copy($_REQUEST['thematic_id']);
  74. $action = 'thematic_list';
  75. }
  76. // delete many thematics
  77. if ($action == 'thematic_delete_select') {
  78. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  79. if (api_is_allowed_to_edit(null, true)) {
  80. $thematic_ids = $_POST['id'];
  81. $affected_rows = $thematic->thematic_destroy($thematic_ids);
  82. }
  83. $action = 'thematic_list';
  84. }
  85. }
  86. $thematic_id = isset($_GET['thematic_id'])?intval($_GET['thematic_id']):null;
  87. if (isset($thematic_id)) {
  88. // delete a thematic
  89. if ($action == 'thematic_delete') {
  90. if (api_is_allowed_to_edit(null, true)) {
  91. $affected_rows = $thematic->thematic_destroy($thematic_id);
  92. }
  93. $action = 'thematic_list';
  94. }
  95. // move thematic
  96. if ($action == 'moveup') {
  97. $thematic->move_thematic('up', $thematic_id);
  98. $action = 'thematic_list';
  99. } else if ($action == 'movedown') {
  100. $thematic->move_thematic('down', $thematic_id);
  101. $action = 'thematic_list';
  102. }
  103. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  104. $data['thematic_id'] = $thematic_id;
  105. }
  106. if ($action == 'thematic_details') {
  107. if (isset($thematic_id)) {
  108. $thematic_data[$thematic_id] = $thematic->get_thematic_list($thematic_id);
  109. $data['total_average_of_advances'] = $thematic->get_average_of_advances_by_thematic($thematic_id);
  110. } else {
  111. $thematic_data = $thematic->get_thematic_list(null, api_get_course_id(), api_get_session_id());
  112. $data['last_done_thematic_advance'] = $thematic->get_last_done_thematic_advance();
  113. $data['total_average_of_advances'] = $thematic->get_total_average_of_thematic_advances();
  114. }
  115. //Second column
  116. $thematic_plan_data = $thematic->get_thematic_plan_data();
  117. //Third column
  118. $thematic_advance_data = $thematic->get_thematic_advance_list();
  119. $data['thematic_plan_data'] = $thematic_plan_data;
  120. $data['thematic_advance_data'] = $thematic_advance_data;
  121. $data['thematic_data'] = $thematic_data;
  122. }
  123. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  124. $data['action'] = $action;
  125. // render to the view
  126. $this->view->set_data($data);
  127. $this->view->set_layout('layout');
  128. $this->view->set_template('thematic');
  129. $this->view->render();
  130. }
  131. /**
  132. * This method is used for thematic plan control (update, insert or listing)
  133. * @param string Action
  134. * render to thematic_plan.php
  135. */
  136. public function thematic_plan($action) {
  137. $thematic= new Thematic();
  138. $data = array();
  139. $error = false;
  140. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  141. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_plan_add' || $_POST['action'] == 'thematic_plan_edit')) {
  142. if (trim($_POST['title']) !== '') {
  143. if ($_POST['thematic_plan_token'] == $_SESSION['thematic_plan_token']) {
  144. if (api_is_allowed_to_edit(null, true)) {
  145. $thematic_id = $_POST['thematic_id'];
  146. $title = $_POST['title'];
  147. $description = $_POST['description'];
  148. $description_type = $_POST['description_type'];
  149. $thematic->set_thematic_plan_attributes($thematic_id, $title, $description, $description_type);
  150. $affected_rows = $thematic->thematic_plan_save();
  151. unset($_SESSION['thematic_plan_token']);
  152. $data['message'] = 'ok';
  153. }
  154. $data['action'] = 'thematic_plan_list';
  155. }
  156. } else {
  157. $error = true;
  158. $action = $_POST['action'];
  159. $data['error'] = $error;
  160. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($_POST['thematic_id'], $_POST['description_type']);
  161. $data['thematic_id'] = $_POST['thematic_id'];
  162. $data['description_type'] = $_POST['description_type'];
  163. $data['action'] = $action;
  164. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  165. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  166. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  167. $data['next_description_type'] = $thematic->get_next_description_type($_POST['thematic_id']);
  168. // render to the view
  169. $this->view->set_data($data);
  170. $this->view->set_layout('layout');
  171. $this->view->set_template('thematic_plan');
  172. $this->view->render();
  173. }
  174. } else if($_POST['action'] == 'thematic_plan_list') {
  175. $title_list = $_POST['title'];
  176. $description_list = $_POST['description'];
  177. $description_type = $_POST['description_type'];
  178. if (api_is_allowed_to_edit(null, true)) {
  179. for($i=1;$i<count($title_list)+1; $i++) {
  180. //if (!empty($description_list[$i])) {
  181. $thematic->set_thematic_plan_attributes($_POST['thematic_id'], $title_list[$i], $description_list[$i], $description_type[$i]);
  182. $affected_rows = $thematic->thematic_plan_save();
  183. //}
  184. }
  185. $data['message'] = 'ok';
  186. }
  187. }
  188. }
  189. if ($action == 'thematic_plan_list') {
  190. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  191. }
  192. $thematic_id = intval($_GET['thematic_id']);
  193. $description_type = intval($_GET['description_type']);
  194. if (!empty($thematic_id) && !empty($description_type)) {
  195. if ($action == 'thematic_plan_delete') {
  196. if (api_is_allowed_to_edit(null, true)) {
  197. $affected_rows = $thematic->thematic_plan_destroy($thematic_id, $description_type);
  198. }
  199. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  200. $action = 'thematic_plan_list';
  201. } else {
  202. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id, $description_type);
  203. }
  204. $data['thematic_id'] = $thematic_id;
  205. $data['description_type'] = $description_type;
  206. } else if (!empty($thematic_id) && $action == 'thematic_plan_list') {
  207. $data['thematic_plan_data'] = $thematic->get_thematic_plan_data($thematic_id);
  208. $data['thematic_id'] = $thematic_id;
  209. }
  210. $data['thematic_id'] = $thematic_id;
  211. $data['action'] = $action;
  212. $data['default_thematic_plan_title'] = $thematic->get_default_thematic_plan_title();
  213. $data['default_thematic_plan_icon'] = $thematic->get_default_thematic_plan_icon();
  214. $data['next_description_type'] = $thematic->get_next_description_type($thematic_id);
  215. $data['default_thematic_plan_question'] = $thematic->get_default_question();
  216. $data['thematic_data'] = $thematic->get_thematic_list($thematic_id);
  217. // render to the view
  218. $this->view->set_data($data);
  219. $this->view->set_layout('layout');
  220. $this->view->set_template('thematic_plan');
  221. $this->view->render();
  222. exit;
  223. }
  224. /**
  225. * This method is used for thematic advance control (update, insert or listing)
  226. * @param string Action
  227. * render to thematic_advance.php
  228. */
  229. public function thematic_advance($action) {
  230. $thematic= new Thematic();
  231. $attendance = new Attendance();
  232. $data = array();
  233. // get data for attendance input select
  234. $attendance_list = $attendance->get_attendances_list();
  235. $attendance_select = array();
  236. $attendance_select[0] = get_lang('SelectAnAttendance');
  237. foreach ($attendance_list as $attendance_id => $attendance_data) {
  238. $attendance_select[$attendance_id] = $attendance_data['name'];
  239. }
  240. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  241. if (isset($_POST['action']) && ($_POST['action'] == 'thematic_advance_add' || $_POST['action'] == 'thematic_advance_edit')) {
  242. if (($_POST['start_date_type'] == 1 && empty($_POST['start_date_by_attendance'])) || (!empty($_POST['duration_in_hours']) && !is_numeric($_POST['duration_in_hours'])) ) {
  243. if ($_POST['start_date_type'] == 1 && empty($_POST['start_date_by_attendance'])) {
  244. $start_date_error = true;
  245. $data['start_date_error'] = $start_date_error;
  246. }
  247. if (!empty($_POST['duration_in_hours']) && !is_numeric($_POST['duration_in_hours'])) {
  248. $duration_error = true;
  249. $data['duration_error'] = $duration_error;
  250. }
  251. $data['action'] = $_POST['action'];
  252. $data['thematic_id'] = $_POST['thematic_id'];
  253. $data['attendance_select'] = $attendance_select;
  254. if (isset($_POST['thematic_advance_id'])) {
  255. $data['thematic_advance_id'] = $_POST['thematic_advance_id'];
  256. $thematic_advance_data = $thematic->get_thematic_advance_list($_POST['thematic_advance_id']);
  257. $data['thematic_advance_data'] = $thematic_advance_data;
  258. }
  259. // render to the view
  260. $this->view->set_data($data);
  261. $this->view->set_layout('layout');
  262. $this->view->set_template('thematic_advance');
  263. $this->view->render();
  264. } else {
  265. if ($_POST['thematic_advance_token'] == $_SESSION['thematic_advance_token'] && api_is_allowed_to_edit(null, true)) {
  266. $thematic_advance_id = $_POST['thematic_advance_id'];
  267. $thematic_id = $_POST['thematic_id'];
  268. $content = $_POST['content'];
  269. $duration = $_POST['duration_in_hours'];
  270. if (isset($_POST['start_date_type']) && $_POST['start_date_type'] == 2) {
  271. $start_date = $thematic->build_datetime_from_array($_POST['custom_start_date']);
  272. $attendance_id = 0;
  273. } else {
  274. $start_date = $_POST['start_date_by_attendance'];
  275. $attendance_id = $_POST['attendance_select'];
  276. }
  277. $thematic->set_thematic_advance_attributes($thematic_advance_id, $thematic_id, $attendance_id, $content, $start_date, $duration);
  278. $affected_rows = $thematic->thematic_advance_save();
  279. if ($affected_rows) {
  280. // get last done thematic advance before move thematic list
  281. $last_done_thematic_advance = $thematic->get_last_done_thematic_advance();
  282. // update done advances with de current thematic list
  283. if (!empty($last_done_thematic_advance)) {
  284. $update_done_advances = $thematic->update_done_thematic_advances($last_done_thematic_advance);
  285. }
  286. }
  287. unset($_SESSION['thematic_advance_token']);
  288. $action = 'thematic_advance_list';
  289. }
  290. }
  291. }
  292. }
  293. $thematic_id = intval($_GET['thematic_id']);
  294. $thematic_advance_id = intval($_GET['thematic_advance_id']);
  295. $thematic_advance_data = array();
  296. if (!empty($thematic_advance_id)) {
  297. if ($action == 'thematic_advance_delete') {
  298. if (api_is_allowed_to_edit(null, true)) {
  299. $affected_rows = $thematic->thematic_advance_destroy($thematic_advance_id);
  300. }
  301. $action = 'thematic_advance_list';
  302. } else {
  303. $thematic_advance_data = $thematic->get_thematic_advance_list($thematic_advance_id);
  304. }
  305. }
  306. // get calendar select by attendance id
  307. $calendar_select = array();
  308. if (!empty($thematic_advance_data)) {
  309. if (!empty($thematic_advance_data['attendance_id'])) {
  310. $attendance_calendar = $attendance->get_attendance_calendar($thematic_advance_data['attendance_id']);
  311. if (!empty($attendance_calendar)) {
  312. foreach ($attendance_calendar as $calendar) {
  313. $calendar_select[$calendar['date_time']] = $calendar['date_time'];
  314. }
  315. }
  316. }
  317. }
  318. $data['action'] = $action;
  319. $data['thematic_id'] = $thematic_id;
  320. $data['thematic_advance_id'] = $thematic_advance_id;
  321. $data['attendance_select'] = $attendance_select;
  322. $data['thematic_advance_data'] = $thematic_advance_data;
  323. $data['calendar_select'] = $calendar_select;
  324. // render to the view
  325. $this->view->set_data($data);
  326. $this->view->set_layout('layout');
  327. $this->view->set_template('thematic_advance');
  328. $this->view->render();
  329. }
  330. }