course_description_controller.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class CourseDescriptionController
  5. * This file contains class used like controller, it should be included inside a dispatcher file (e.g: index.php)
  6. * @author Christian Fasanando <christian1827@gmail.com>
  7. * @package chamilo.course_description
  8. */
  9. class CourseDescriptionController
  10. {
  11. private $toolname;
  12. private $view;
  13. /**
  14. * Constructor
  15. */
  16. public function __construct()
  17. {
  18. $this->toolname = 'course_description';
  19. $this->view = new View($this->toolname);
  20. }
  21. /**
  22. * It's used for listing course description,
  23. * render to listing view
  24. * @param boolean true for listing history (optional)
  25. * @param array message for showing by action['edit','add','destroy'] (optional)
  26. */
  27. public function listing($history=false, $messages=array())
  28. {
  29. $course_description = new CourseDescription();
  30. $session_id = api_get_session_id();
  31. $course_description->set_session_id($session_id);
  32. $data = array();
  33. $course_description_data = $course_description->get_description_data();
  34. $data['descriptions'] = $course_description_data['descriptions'];
  35. $data['default_description_titles'] = $course_description->get_default_description_title();
  36. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  37. $data['default_description_icon'] = $course_description->get_default_description_icon();
  38. $data['messages'] = $messages;
  39. $browser = api_get_navigator();
  40. if (!is_array($data['descriptions'])) {
  41. $data['descriptions'] = array($data['descriptions']);
  42. }
  43. foreach ($data['descriptions'] as $description) {
  44. if (strpos($description, '<iframe') !== false && $browser['name'] == 'Chrome') {
  45. header("X-XSS-Protection: 0");
  46. }
  47. }
  48. // render to the view
  49. $this->view->set_data($data);
  50. $this->view->set_layout('layout');
  51. $this->view->set_template('listing');
  52. $this->view->render();
  53. }
  54. /**
  55. * It's used for editing a course description,
  56. * render to listing or edit view
  57. * @param int description type
  58. */
  59. public function edit($id, $description_type)
  60. {
  61. $course_description = new CourseDescription();
  62. $session_id = api_get_session_id();
  63. $course_description->set_session_id($session_id);
  64. $data = array();
  65. $data['id'] = $id;
  66. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  67. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  68. $check = Security::check_token();
  69. if ($check) {
  70. $title = $_POST['title'];
  71. $content = $_POST['contentDescription'];
  72. $description_type = $_POST['description_type'];
  73. $id = $_POST['id'];
  74. $progress = $_POST['progress'];
  75. $course_description->set_description_type($description_type);
  76. $course_description->set_title($title);
  77. $course_description->set_content($content);
  78. $course_description->set_progress($progress);
  79. $thematic_advance = $course_description->get_data_by_id($id);
  80. if (!empty($thematic_advance)) {
  81. $course_description->set_id($id);
  82. $affected_rows = $course_description->update();
  83. } else {
  84. $affected_rows = $course_description->insert();
  85. }
  86. Security::clear_token();
  87. }
  88. if ($affected_rows) {
  89. $message['edit'] = true;
  90. }
  91. $this->listing(false, $message);
  92. } else {
  93. $data['error'] = 1;
  94. $data['default_description_titles'] = $course_description->get_default_description_title();
  95. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  96. $data['default_description_icon'] = $course_description->get_default_description_icon();
  97. $data['question'] = $course_description->get_default_question();
  98. $data['information'] = $course_description->get_default_information();
  99. $data['description_title'] = $_POST['title'];
  100. $data['description_content'] = $_POST['contentDescription'];
  101. $data['description_type'] = $_POST['description_type'];
  102. $data['progress'] = $_POST['progress'];
  103. $data['descriptions'] = $course_description->get_data_by_id($_POST['id']);
  104. // render to the view
  105. $this->view->set_data($data);
  106. $this->view->set_layout('layout');
  107. $this->view->set_template('edit');
  108. $this->view->render();
  109. }
  110. } else {
  111. $data['default_description_titles'] = $course_description->get_default_description_title();
  112. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  113. $data['default_description_icon'] = $course_description->get_default_description_icon();
  114. $data['question'] = $course_description->get_default_question();
  115. $data['information'] = $course_description->get_default_information();
  116. $data['description_type'] = $description_type;
  117. if (!empty($id)) {
  118. if (isset($_GET['id_session'])) {
  119. $session_id = intval($_GET['id_session']);
  120. }
  121. $course_description_data = $course_description->get_data_by_id($id, null, $session_id);
  122. $data['description_type'] = $course_description_data['description_type'];
  123. $data['description_title'] = $course_description_data['description_title'];
  124. $data['description_content'] = $course_description_data['description_content'];
  125. $data['progress'] = $course_description_data['progress'];
  126. $data['descriptions'] = $course_description->get_data_by_description_type($description_type, null, $session_id);
  127. }
  128. // render to the view
  129. $this->view->set_data($data);
  130. $this->view->set_layout('layout');
  131. $this->view->set_template('edit');
  132. $this->view->render();
  133. }
  134. }
  135. /**
  136. * It's used for adding a course description,
  137. * render to listing or add view
  138. */
  139. public function add()
  140. {
  141. $course_description = new CourseDescription();
  142. $session_id = api_get_session_id();
  143. $course_description->set_session_id($session_id);
  144. $data = array();
  145. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  146. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  147. $check = Security::check_token();
  148. if ($check) {
  149. $title = $_POST['title'];
  150. $content = $_POST['contentDescription'];
  151. $description_type = $_POST['description_type'];
  152. if ($description_type >= ADD_BLOCK) {
  153. $course_description->set_description_type($description_type);
  154. $course_description->set_title($title);
  155. $course_description->set_content($content);
  156. $affected_rows = $course_description->insert(api_get_course_int_id());
  157. }
  158. Security::clear_token();
  159. }
  160. if ($affected_rows) {
  161. $message['add'] = true;
  162. }
  163. $this->listing(false,$message);
  164. } else {
  165. $data['error'] = 1;
  166. $data['default_description_titles'] = $course_description->get_default_description_title();
  167. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  168. $data['default_description_icon'] = $course_description->get_default_description_icon();
  169. $data['question'] = $course_description->get_default_question();
  170. $data['information'] = $course_description->get_default_information();
  171. $data['description_title'] = $_POST['title'];
  172. $data['description_content'] = $_POST['contentDescription'];
  173. $data['description_type'] = $_POST['description_type'];
  174. $this->view->set_data($data);
  175. $this->view->set_layout('layout');
  176. $this->view->set_template('add');
  177. $this->view->render();
  178. }
  179. } else {
  180. $data['default_description_titles'] = $course_description->get_default_description_title();
  181. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  182. $data['default_description_icon'] = $course_description->get_default_description_icon();
  183. $data['question'] = $course_description->get_default_question();
  184. $data['information'] = $course_description->get_default_information();
  185. $data['description_type'] = $course_description->get_max_description_type();
  186. // render to the view
  187. $this->view->set_data($data);
  188. $this->view->set_layout('layout');
  189. $this->view->set_template('add');
  190. $this->view->render();
  191. }
  192. }
  193. /**
  194. * It's used for destroy a course description,
  195. * render to listing view
  196. * @param int description type
  197. */
  198. public function destroy($id)
  199. {
  200. $course_description = new CourseDescription();
  201. $session_id = api_get_session_id();
  202. $course_description->set_session_id($session_id);
  203. if (!empty($id)) {
  204. $course_description->set_id($id);
  205. $affected_rows = $course_description->delete();
  206. }
  207. if ($affected_rows) {
  208. $message['destroy'] = true;
  209. }
  210. $this->listing(false, $message);
  211. }
  212. }