course_description_controller.php 9.7 KB

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