course_description_controller.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class CourseDescriptionController
  5. * This file contains class used like controller,
  6. * it should be included inside a dispatcher file (e.g: index.php).
  7. *
  8. * @author Christian Fasanando <christian1827@gmail.com>
  9. *
  10. * @package chamilo.course_description
  11. */
  12. class CourseDescriptionController
  13. {
  14. private $toolname;
  15. private $view;
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct()
  20. {
  21. $this->toolname = 'course_description';
  22. $this->view = new View($this->toolname);
  23. }
  24. /**
  25. * It's used for listing course description,
  26. * render to listing view.
  27. *
  28. * @param bool true for listing history (optional)
  29. * @param array message for showing by action['edit','add','destroy'] (optional)
  30. */
  31. public function listing($history = false, $messages = [])
  32. {
  33. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  34. $course_description = new CourseDescription();
  35. $session_id = api_get_session_id();
  36. $data = [];
  37. $course_description->set_session_id($session_id);
  38. $course_description_data = $course_description->get_description_data();
  39. $data['descriptions'] = isset($course_description_data['descriptions']) ? $course_description_data['descriptions'] : '';
  40. $data['default_description_titles'] = $course_description->get_default_description_title();
  41. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  42. $data['default_description_icon'] = $course_description->get_default_description_icon();
  43. $data['messages'] = $messages;
  44. $browser = api_get_navigator();
  45. api_protect_course_script(true);
  46. if (!is_array($data['descriptions'])) {
  47. $data['descriptions'] = [$data['descriptions']];
  48. }
  49. // Prepare confirmation code for item deletion
  50. global $htmlHeadXtra;
  51. $htmlHeadXtra[] = "<script>
  52. function confirmation(name) {
  53. if (confirm(\" ".trim(get_lang('Are you sure to delete'))." \"+name+\"?\")) {
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. }
  59. </script>";
  60. foreach ($data['descriptions'] as $id => $description) {
  61. if (!empty($description['content'])
  62. && strpos($description['content'], '<iframe') !== false
  63. && $browser['name'] == 'Chrome'
  64. ) {
  65. header("X-XSS-Protection: 0");
  66. }
  67. // Add an escape version for the JS code of delete confirmation
  68. if ($description) {
  69. $data['descriptions'][$id]['title_js'] = addslashes(strip_tags($description['title']));
  70. }
  71. }
  72. $actions = null;
  73. $actionLeft = null;
  74. // display actions menu
  75. if ($is_allowed_to_edit) {
  76. $categories = [];
  77. foreach ($data['default_description_titles'] as $id => $title) {
  78. $categories[$id] = $title;
  79. }
  80. $categories[ADD_BLOCK] = get_lang('Other');
  81. $i = 1;
  82. ksort($categories);
  83. foreach ($categories as $id => $title) {
  84. if ($i == ADD_BLOCK) {
  85. $actionLeft .= '<a href="index.php?'.api_get_cidreq().'&action=add">'.
  86. Display::return_icon(
  87. $data['default_description_icon'][$id],
  88. $title,
  89. '',
  90. ICON_SIZE_MEDIUM
  91. ).
  92. '</a>';
  93. break;
  94. } else {
  95. $actionLeft .= '<a href="index.php?action=edit&'.api_get_cidreq().'&description_type='.$id.'">'.
  96. Display::return_icon(
  97. $data['default_description_icon'][$id],
  98. $title,
  99. '',
  100. ICON_SIZE_MEDIUM
  101. ).
  102. '</a>';
  103. $i++;
  104. }
  105. }
  106. $actions = Display::toolbarAction('toolbar', [0 => $actionLeft]);
  107. }
  108. $tpl = new Template(get_lang('Description'));
  109. $tpl->assign('listing', $data);
  110. $tpl->assign('is_allowed_to_edit', $is_allowed_to_edit);
  111. $tpl->assign('actions', $actions);
  112. $tpl->assign('session_id', $session_id);
  113. $templateName = $tpl->get_template('course_description/index.tpl');
  114. $content = $tpl->fetch($templateName);
  115. $tpl->assign('content', $content);
  116. $tpl->display_one_col_template();
  117. }
  118. /**
  119. * It's used for editing a course description,
  120. * render to listing or edit view.
  121. *
  122. * @param int $id description item id
  123. * @param int $description_type description type id
  124. */
  125. public function edit($id, $description_type)
  126. {
  127. $course_description = new CourseDescription();
  128. $session_id = api_get_session_id();
  129. $course_description->set_session_id($session_id);
  130. $data = [];
  131. $data['id'] = $id;
  132. $affected_rows = null;
  133. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  134. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  135. if (1) {
  136. $title = $_POST['title'];
  137. $content = $_POST['contentDescription'];
  138. $description_type = $_POST['description_type'];
  139. $id = $_POST['id'];
  140. if (empty($id)) {
  141. // If the ID was not provided, find the first matching description item given the item type
  142. $description = $course_description->get_data_by_description_type(
  143. $description_type
  144. );
  145. if (count($description) > 0) {
  146. $id = $description['id'];
  147. }
  148. // If no corresponding description is found, edit a new one
  149. }
  150. $progress = isset($_POST['progress']) ? $_POST['progress'] : '';
  151. $course_description->set_description_type($description_type);
  152. $course_description->set_title($title);
  153. $course_description->set_content($content);
  154. $course_description->set_progress($progress);
  155. $thematic_advance = $course_description->get_data_by_id($id);
  156. if (!empty($thematic_advance)) {
  157. $course_description->set_id($id);
  158. $course_description->update();
  159. } else {
  160. $course_description->insert();
  161. }
  162. Display::addFlash(
  163. Display::return_message(
  164. get_lang('The description has been updated')
  165. )
  166. );
  167. }
  168. $this->listing(false);
  169. } else {
  170. $data['error'] = 1;
  171. $data['default_description_titles'] = $course_description->get_default_description_title();
  172. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  173. $data['default_description_icon'] = $course_description->get_default_description_icon();
  174. $data['question'] = $course_description->get_default_question();
  175. $data['information'] = $course_description->get_default_information();
  176. $data['description_title'] = $_POST['title'];
  177. $data['description_content'] = $_POST['contentDescription'];
  178. $data['description_type'] = $_POST['description_type'];
  179. $data['progress'] = $_POST['progress'];
  180. $data['descriptions'] = $course_description->get_data_by_id($_POST['id']);
  181. // render to the view
  182. $this->view->set_data($data);
  183. $this->view->set_layout('layout');
  184. $this->view->set_template('edit');
  185. $this->view->render();
  186. }
  187. } else {
  188. $data['default_description_titles'] = $course_description->get_default_description_title();
  189. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  190. $data['default_description_icon'] = $course_description->get_default_description_icon();
  191. $data['question'] = $course_description->get_default_question();
  192. $data['information'] = $course_description->get_default_information();
  193. $data['description_type'] = $description_type;
  194. if (empty($id)) {
  195. // If the ID was not provided, find the first matching description item given the item type
  196. $description = $course_description->get_data_by_description_type($description_type);
  197. if (count($description) > 0) {
  198. $id = $description['id'];
  199. }
  200. // If no corresponding description is found, edit a new one
  201. }
  202. if (!empty($id)) {
  203. if (isset($_GET['id_session'])) {
  204. $session_id = intval($_GET['id_session']);
  205. }
  206. $course_description_data = $course_description->get_data_by_id(
  207. $id,
  208. null,
  209. $session_id
  210. );
  211. $data['description_type'] = $course_description_data['description_type'];
  212. $data['description_title'] = $course_description_data['description_title'];
  213. $data['description_content'] = $course_description_data['description_content'];
  214. $data['progress'] = $course_description_data['progress'];
  215. $data['descriptions'] = $course_description->get_data_by_description_type(
  216. $description_type,
  217. null,
  218. $session_id
  219. );
  220. }
  221. // render to the view
  222. $this->view->set_data($data);
  223. $this->view->set_layout('layout');
  224. $this->view->set_template('edit');
  225. $this->view->render();
  226. }
  227. }
  228. /**
  229. * It's used for adding a course description,
  230. * render to listing or add view.
  231. */
  232. public function add()
  233. {
  234. $course_description = new CourseDescription();
  235. $session_id = api_get_session_id();
  236. $course_description->set_session_id($session_id);
  237. $data = [];
  238. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  239. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  240. if (1) {
  241. $title = $_POST['title'];
  242. $content = $_POST['contentDescription'];
  243. $description_type = $_POST['description_type'];
  244. if ($description_type >= ADD_BLOCK) {
  245. $course_description->set_description_type($description_type);
  246. $course_description->set_title($title);
  247. $course_description->set_content($content);
  248. $course_description->insert(api_get_course_int_id());
  249. }
  250. Display::addFlash(
  251. Display::return_message(
  252. get_lang('The description has been updated')
  253. )
  254. );
  255. }
  256. $this->listing(false);
  257. } else {
  258. $data['error'] = 1;
  259. $data['default_description_titles'] = $course_description->get_default_description_title();
  260. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  261. $data['default_description_icon'] = $course_description->get_default_description_icon();
  262. $data['question'] = $course_description->get_default_question();
  263. $data['information'] = $course_description->get_default_information();
  264. $data['description_title'] = $_POST['title'];
  265. $data['description_content'] = $_POST['contentDescription'];
  266. $data['description_type'] = $_POST['description_type'];
  267. $this->view->set_data($data);
  268. $this->view->set_layout('layout');
  269. $this->view->set_template('add');
  270. $this->view->render();
  271. }
  272. } else {
  273. $data['default_description_titles'] = $course_description->get_default_description_title();
  274. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  275. $data['default_description_icon'] = $course_description->get_default_description_icon();
  276. $data['question'] = $course_description->get_default_question();
  277. $data['information'] = $course_description->get_default_information();
  278. $data['description_type'] = $course_description->get_max_description_type();
  279. // render to the view
  280. $this->view->set_data($data);
  281. $this->view->set_layout('layout');
  282. $this->view->set_template('add');
  283. $this->view->render();
  284. }
  285. }
  286. /**
  287. * It's used for destroy a course description,
  288. * render to listing view.
  289. *
  290. * @param int $id description type
  291. */
  292. public function destroy($id)
  293. {
  294. $course_description = new CourseDescription();
  295. $session_id = api_get_session_id();
  296. $course_description->set_session_id($session_id);
  297. if (!empty($id)) {
  298. $course_description->set_id($id);
  299. $course_description->delete();
  300. Display::addFlash(
  301. Display::return_message(get_lang('Description has been deleted'))
  302. );
  303. }
  304. $this->listing(false);
  305. }
  306. }