course_description_controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  73. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  74. if (1) {
  75. $title = $_POST['title'];
  76. $content = $_POST['contentDescription'];
  77. $description_type = $_POST['description_type'];
  78. $id = $_POST['id'];
  79. if (empty($id)) {
  80. // If the ID was not provided, find the first matching description item given the item type
  81. $description = $course_description->get_data_by_description_type(
  82. $description_type
  83. );
  84. if (count($description) > 0) {
  85. $id = $description['id'];
  86. }
  87. // If no corresponding description is found, edit a new one
  88. }
  89. $progress = isset($_POST['progress']) ? $_POST['progress'] : '';
  90. $course_description->set_description_type($description_type);
  91. $course_description->set_title($title);
  92. $course_description->set_content($content);
  93. $course_description->set_progress($progress);
  94. $thematic_advance = $course_description->get_data_by_id($id);
  95. if (!empty($thematic_advance)) {
  96. $course_description->set_id($id);
  97. $course_description->update();
  98. } else {
  99. $course_description->insert();
  100. }
  101. Display::addFlash(
  102. Display::return_message(
  103. get_lang('CourseDescriptionUpdated')
  104. )
  105. );
  106. }
  107. $this->listing(false);
  108. } else {
  109. $data['error'] = 1;
  110. $data['default_description_titles'] = $course_description->get_default_description_title();
  111. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  112. $data['default_description_icon'] = $course_description->get_default_description_icon();
  113. $data['question'] = $course_description->get_default_question();
  114. $data['information'] = $course_description->get_default_information();
  115. $data['description_title'] = $_POST['title'];
  116. $data['description_content'] = $_POST['contentDescription'];
  117. $data['description_type'] = $_POST['description_type'];
  118. $data['progress'] = $_POST['progress'];
  119. $data['descriptions'] = $course_description->get_data_by_id($_POST['id']);
  120. // render to the view
  121. $this->view->set_data($data);
  122. $this->view->set_layout('layout');
  123. $this->view->set_template('edit');
  124. $this->view->render();
  125. }
  126. } else {
  127. $data['default_description_titles'] = $course_description->get_default_description_title();
  128. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  129. $data['default_description_icon'] = $course_description->get_default_description_icon();
  130. $data['question'] = $course_description->get_default_question();
  131. $data['information'] = $course_description->get_default_information();
  132. $data['description_type'] = $description_type;
  133. if (empty($id)) {
  134. // If the ID was not provided, find the first matching description item given the item type
  135. $description = $course_description->get_data_by_description_type($description_type);
  136. if (count($description) > 0) {
  137. $id = $description['id'];
  138. }
  139. // If no corresponding description is found, edit a new one
  140. }
  141. if (!empty($id)) {
  142. if (isset($_GET['id_session'])) {
  143. $session_id = intval($_GET['id_session']);
  144. }
  145. $course_description_data = $course_description->get_data_by_id(
  146. $id,
  147. null,
  148. $session_id
  149. );
  150. $data['description_type'] = $course_description_data['description_type'];
  151. $data['description_title'] = $course_description_data['description_title'];
  152. $data['description_content'] = $course_description_data['description_content'];
  153. $data['progress'] = $course_description_data['progress'];
  154. $data['descriptions'] = $course_description->get_data_by_description_type(
  155. $description_type,
  156. null,
  157. $session_id
  158. );
  159. }
  160. // render to the view
  161. $this->view->set_data($data);
  162. $this->view->set_layout('layout');
  163. $this->view->set_template('edit');
  164. $this->view->render();
  165. }
  166. }
  167. /**
  168. * It's used for adding a course description,
  169. * render to listing or add view
  170. */
  171. public function add()
  172. {
  173. $course_description = new CourseDescription();
  174. $session_id = api_get_session_id();
  175. $course_description->set_session_id($session_id);
  176. $data = array();
  177. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  178. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  179. if (1) {
  180. $title = $_POST['title'];
  181. $content = $_POST['contentDescription'];
  182. $description_type = $_POST['description_type'];
  183. if ($description_type >= ADD_BLOCK) {
  184. $course_description->set_description_type($description_type);
  185. $course_description->set_title($title);
  186. $course_description->set_content($content);
  187. $course_description->insert(api_get_course_int_id());
  188. }
  189. Display::addFlash(
  190. Display::return_message(
  191. get_lang('CourseDescriptionUpdated')
  192. )
  193. );
  194. }
  195. $this->listing(false);
  196. } else {
  197. $data['error'] = 1;
  198. $data['default_description_titles'] = $course_description->get_default_description_title();
  199. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  200. $data['default_description_icon'] = $course_description->get_default_description_icon();
  201. $data['question'] = $course_description->get_default_question();
  202. $data['information'] = $course_description->get_default_information();
  203. $data['description_title'] = $_POST['title'];
  204. $data['description_content'] = $_POST['contentDescription'];
  205. $data['description_type'] = $_POST['description_type'];
  206. $this->view->set_data($data);
  207. $this->view->set_layout('layout');
  208. $this->view->set_template('add');
  209. $this->view->render();
  210. }
  211. } else {
  212. $data['default_description_titles'] = $course_description->get_default_description_title();
  213. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  214. $data['default_description_icon'] = $course_description->get_default_description_icon();
  215. $data['question'] = $course_description->get_default_question();
  216. $data['information'] = $course_description->get_default_information();
  217. $data['description_type'] = $course_description->get_max_description_type();
  218. // render to the view
  219. $this->view->set_data($data);
  220. $this->view->set_layout('layout');
  221. $this->view->set_template('add');
  222. $this->view->render();
  223. }
  224. }
  225. /**
  226. * It's used for destroy a course description,
  227. * render to listing view
  228. * @param int $id description type
  229. */
  230. public function destroy($id)
  231. {
  232. $course_description = new CourseDescription();
  233. $session_id = api_get_session_id();
  234. $course_description->set_session_id($session_id);
  235. if (!empty($id)) {
  236. $course_description->set_id($id);
  237. $course_description->delete();
  238. Display::addFlash(
  239. Display::return_message(get_lang('CourseDescriptionDeleted'))
  240. );
  241. }
  242. $this->listing(false);
  243. }
  244. }