course_description_controller.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. * @author Christian Fasanando <christian1827@gmail.com>
  8. * @package chamilo.course_description
  9. */
  10. class CourseDescriptionController
  11. {
  12. private $toolname;
  13. private $view;
  14. /**
  15. * Constructor
  16. */
  17. public function __construct()
  18. {
  19. $this->toolname = 'course_description';
  20. $this->view = new View($this->toolname);
  21. }
  22. /**
  23. * It's used for listing course description,
  24. * render to listing view
  25. * @param boolean true for listing history (optional)
  26. * @param array message for showing by action['edit','add','destroy'] (optional)
  27. */
  28. public function listing($history = false, $messages = array())
  29. {
  30. $course_description = new CourseDescription();
  31. $session_id = api_get_session_id();
  32. $course_description->set_session_id($session_id);
  33. $data = array();
  34. $course_description_data = $course_description->get_description_data();
  35. $data['descriptions'] = isset($course_description_data['descriptions']) ? $course_description_data['descriptions'] : '';
  36. $data['default_description_titles'] = $course_description->get_default_description_title();
  37. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  38. $data['default_description_icon'] = $course_description->get_default_description_icon();
  39. $data['messages'] = $messages;
  40. $browser = api_get_navigator();
  41. if (!is_array($data['descriptions'])) {
  42. $data['descriptions'] = array($data['descriptions']);
  43. }
  44. foreach ($data['descriptions'] as $description) {
  45. if (!empty($description['content'])
  46. && strpos($description['content'], '<iframe') !== false
  47. && $browser['name'] == 'Chrome'
  48. ) {
  49. header("X-XSS-Protection: 0");
  50. }
  51. }
  52. // render to the view
  53. $this->view->set_data($data);
  54. $this->view->set_layout('layout');
  55. $this->view->set_template('listing');
  56. $this->view->render();
  57. }
  58. /**
  59. * It's used for editing a course description,
  60. * render to listing or edit view
  61. * @param int $id description item id
  62. * @param int $description_type description type id
  63. */
  64. public function edit($id, $description_type)
  65. {
  66. $course_description = new CourseDescription();
  67. $session_id = api_get_session_id();
  68. $course_description->set_session_id($session_id);
  69. $data = array();
  70. $data['id'] = $id;
  71. $affected_rows = null;
  72. $message = array();
  73. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  74. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  75. $check = Security::check_token();
  76. if ($check) {
  77. $title = $_POST['title'];
  78. $content = $_POST['contentDescription'];
  79. $description_type = $_POST['description_type'];
  80. $id = $_POST['id'];
  81. if (empty($id)) {
  82. // If the ID was not provided, find the first matching description item given the item type
  83. $description = $course_description->get_data_by_description_type(
  84. $description_type
  85. );
  86. if (count($description) > 0) {
  87. $id = $description['id'];
  88. }
  89. // If no corresponding description is found, edit a new one
  90. }
  91. $progress = isset($_POST['progress']) ? $_POST['progress'] : '';
  92. $course_description->set_description_type($description_type);
  93. $course_description->set_title($title);
  94. $course_description->set_content($content);
  95. $course_description->set_progress($progress);
  96. $thematic_advance = $course_description->get_data_by_id($id);
  97. if (!empty($thematic_advance)) {
  98. $course_description->set_id($id);
  99. $affected_rows = $course_description->update();
  100. } else {
  101. $affected_rows = $course_description->insert();
  102. }
  103. Security::clear_token();
  104. Display::addFlash(
  105. Display::return_message(
  106. get_lang('CourseDescriptionUpdated')
  107. )
  108. );
  109. }
  110. $this->listing(false);
  111. } else {
  112. $data['error'] = 1;
  113. $data['default_description_titles'] = $course_description->get_default_description_title();
  114. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  115. $data['default_description_icon'] = $course_description->get_default_description_icon();
  116. $data['question'] = $course_description->get_default_question();
  117. $data['information'] = $course_description->get_default_information();
  118. $data['description_title'] = $_POST['title'];
  119. $data['description_content'] = $_POST['contentDescription'];
  120. $data['description_type'] = $_POST['description_type'];
  121. $data['progress'] = $_POST['progress'];
  122. $data['descriptions'] = $course_description->get_data_by_id($_POST['id']);
  123. // render to the view
  124. $this->view->set_data($data);
  125. $this->view->set_layout('layout');
  126. $this->view->set_template('edit');
  127. $this->view->render();
  128. }
  129. } else {
  130. $data['default_description_titles'] = $course_description->get_default_description_title();
  131. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  132. $data['default_description_icon'] = $course_description->get_default_description_icon();
  133. $data['question'] = $course_description->get_default_question();
  134. $data['information'] = $course_description->get_default_information();
  135. $data['description_type'] = $description_type;
  136. if (empty($id)) {
  137. // If the ID was not provided, find the first matching description item given the item type
  138. $description = $course_description->get_data_by_description_type($description_type);
  139. if (count($description) > 0) {
  140. $id = $description['id'];
  141. }
  142. // If no corresponding description is found, edit a new one
  143. }
  144. if (!empty($id)) {
  145. if (isset($_GET['id_session'])) {
  146. $session_id = intval($_GET['id_session']);
  147. }
  148. $course_description_data = $course_description->get_data_by_id(
  149. $id,
  150. null,
  151. $session_id
  152. );
  153. $data['description_type'] = $course_description_data['description_type'];
  154. $data['description_title'] = $course_description_data['description_title'];
  155. $data['description_content'] = $course_description_data['description_content'];
  156. $data['progress'] = $course_description_data['progress'];
  157. $data['descriptions'] = $course_description->get_data_by_description_type(
  158. $description_type,
  159. null,
  160. $session_id
  161. );
  162. }
  163. // render to the view
  164. $this->view->set_data($data);
  165. $this->view->set_layout('layout');
  166. $this->view->set_template('edit');
  167. $this->view->render();
  168. }
  169. }
  170. /**
  171. * It's used for adding a course description,
  172. * render to listing or add view
  173. */
  174. public function add()
  175. {
  176. $course_description = new CourseDescription();
  177. $session_id = api_get_session_id();
  178. $course_description->set_session_id($session_id);
  179. $data = array();
  180. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  181. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  182. $check = Security::check_token();
  183. if ($check) {
  184. $title = $_POST['title'];
  185. $content = $_POST['contentDescription'];
  186. $description_type = $_POST['description_type'];
  187. if ($description_type >= ADD_BLOCK) {
  188. $course_description->set_description_type($description_type);
  189. $course_description->set_title($title);
  190. $course_description->set_content($content);
  191. $affected_rows = $course_description->insert(api_get_course_int_id());
  192. }
  193. Security::clear_token();
  194. Display::addFlash(
  195. Display::return_message(
  196. get_lang('CourseDescriptionUpdated')
  197. )
  198. );
  199. }
  200. $this->listing(false);
  201. } else {
  202. $data['error'] = 1;
  203. $data['default_description_titles'] = $course_description->get_default_description_title();
  204. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  205. $data['default_description_icon'] = $course_description->get_default_description_icon();
  206. $data['question'] = $course_description->get_default_question();
  207. $data['information'] = $course_description->get_default_information();
  208. $data['description_title'] = $_POST['title'];
  209. $data['description_content'] = $_POST['contentDescription'];
  210. $data['description_type'] = $_POST['description_type'];
  211. $this->view->set_data($data);
  212. $this->view->set_layout('layout');
  213. $this->view->set_template('add');
  214. $this->view->render();
  215. }
  216. } else {
  217. $data['default_description_titles'] = $course_description->get_default_description_title();
  218. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  219. $data['default_description_icon'] = $course_description->get_default_description_icon();
  220. $data['question'] = $course_description->get_default_question();
  221. $data['information'] = $course_description->get_default_information();
  222. $data['description_type'] = $course_description->get_max_description_type();
  223. // render to the view
  224. $this->view->set_data($data);
  225. $this->view->set_layout('layout');
  226. $this->view->set_template('add');
  227. $this->view->render();
  228. }
  229. }
  230. /**
  231. * It's used for destroy a course description,
  232. * render to listing view
  233. * @param int $id description type
  234. */
  235. public function destroy($id)
  236. {
  237. $course_description = new CourseDescription();
  238. $session_id = api_get_session_id();
  239. $course_description->set_session_id($session_id);
  240. if (!empty($id)) {
  241. $course_description->set_id($id);
  242. $course_description->delete();
  243. Display::addFlash(
  244. Display::return_message(get_lang('CourseDescriptionDeleted'))
  245. );
  246. }
  247. $this->listing(false);
  248. }
  249. }