course_description_controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if ($history) {
  35. $course_description_data = $course_description->get_description_history(THEMATIC_ADVANCE);
  36. $data['history'] = true;
  37. } else {
  38. $course_description_data = $course_description->get_description_data();
  39. }
  40. $data['descriptions'] = $course_description_data['descriptions'];
  41. $data['default_description_titles'] = $course_description->get_default_description_title();
  42. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  43. $data['default_description_icon'] = $course_description->get_default_description_icon();
  44. $data['messages'] = $messages;
  45. // render to the view
  46. $this->view->set_data($data);
  47. $this->view->set_layout('layout');
  48. $this->view->set_template('listing');
  49. $this->view->render();
  50. }
  51. /**
  52. * It's used for editing a course description,
  53. * render to listing or edit view
  54. * @param int description type
  55. */
  56. public function edit($description_type) {
  57. $course_description = new CourseDescription();
  58. $session_id = api_get_session_id();
  59. $course_description->set_session_id($session_id);
  60. $data = array();
  61. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  62. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  63. $check = Security::check_token();
  64. if ($check) {
  65. $title = $_POST['title'];
  66. if (api_get_setting('wcag_anysurfer_public_pages')=='true') {
  67. $content = WCAG_Rendering::prepareXHTML();
  68. } else {
  69. $content = $_POST['contentDescription'];
  70. }
  71. $description_type = $_POST['description_type'];
  72. $progress = $_POST['progress'];
  73. $course_description->set_description_type($description_type);
  74. $course_description->set_title($title);
  75. $course_description->set_content($content);
  76. $course_description->set_progress($progress);
  77. if ($description_type >= ADD_BLOCK) {
  78. $thematic_advance = $course_description->get_data_by_description_type($description_type);
  79. if (!empty($thematic_advance)) {
  80. $affected_rows = $course_description->update();
  81. } else {
  82. $affected_rows = $course_description->insert();
  83. }
  84. } else {
  85. $thematic_advance = $course_description->get_data_by_description_type($description_type);
  86. if (!empty($thematic_advance)) {
  87. if ($description_type == THEMATIC_ADVANCE) {
  88. // if is thematic advance type save in history
  89. $course_description->set_title($thematic_advance['description_title']);
  90. $course_description->set_content($thematic_advance['description_content']);
  91. $course_description->set_progress($thematic_advance['progress']);
  92. $course_description->insert_stats($description_type);
  93. }
  94. $course_description->set_title($title);
  95. $course_description->set_content($content);
  96. $course_description->set_progress($progress);
  97. $affected_rows = $course_description->update();
  98. } else {
  99. $affected_rows = $course_description->insert();
  100. }
  101. }
  102. Security::clear_token();
  103. }
  104. if ($affected_rows) {
  105. $message['edit'] = true;
  106. }
  107. $this->listing(false,$message);
  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_description_type($_POST['description_type']);
  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. if (!empty($description_type)) {
  128. if (isset($_GET['id_session'])) {
  129. $session_id = intval($_GET['id_session']);
  130. }
  131. $course_description_data = $course_description->get_data_by_description_type($description_type, null, $session_id);
  132. $data['default_description_titles'] = $course_description->get_default_description_title();
  133. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  134. $data['default_description_icon'] = $course_description->get_default_description_icon();
  135. $data['question'] = $course_description->get_default_question();
  136. $data['information'] = $course_description->get_default_information();
  137. $data['description_title'] = $course_description_data['description_title'];
  138. $data['description_content'] = $course_description_data['description_content'];
  139. $data['description_type'] = $description_type;
  140. $data['progress'] = $course_description_data['progress'];
  141. $data['descriptions'] = $course_description->get_data_by_description_type($description_type, null, $session_id);
  142. }
  143. // render to the view
  144. $this->view->set_data($data);
  145. $this->view->set_layout('layout');
  146. $this->view->set_template('edit');
  147. $this->view->render();
  148. }
  149. }
  150. /**
  151. * It's used for adding a course description,
  152. * render to listing or add view
  153. */
  154. public function add() {
  155. $course_description = new CourseDescription();
  156. $session_id = api_get_session_id();
  157. $course_description->set_session_id($session_id);
  158. $data = array();
  159. if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST") {
  160. if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) {
  161. $check = Security::check_token();
  162. if ($check) {
  163. $title = $_POST['title'];
  164. if (api_get_setting('wcag_anysurfer_public_pages')=='true') {
  165. $content = WCAG_Rendering::prepareXHTML();
  166. } else {
  167. $content = $_POST['contentDescription'];
  168. }
  169. $description_type = $_POST['description_type'];
  170. if ($description_type >= ADD_BLOCK) {
  171. $course_description->set_description_type($description_type);
  172. $course_description->set_title($title);
  173. $course_description->set_content($content);
  174. $affected_rows = $course_description->insert(api_get_course_int_id());
  175. }
  176. Security::clear_token();
  177. }
  178. if ($affected_rows) {
  179. $message['add'] = true;
  180. }
  181. $this->listing(false,$message);
  182. } else {
  183. $data['error'] = 1;
  184. $data['default_description_titles'] = $course_description->get_default_description_title();
  185. $data['default_description_title_editable'] = $course_description->get_default_description_title_editable();
  186. $data['default_description_icon'] = $course_description->get_default_description_icon();
  187. $data['question'] = $course_description->get_default_question();
  188. $data['information'] = $course_description->get_default_information();
  189. $data['description_title'] = $_POST['title'];
  190. $data['description_content'] = $_POST['contentDescription'];
  191. $data['description_type'] = $_POST['description_type'];
  192. $this->view->set_data($data);
  193. $this->view->set_layout('layout');
  194. $this->view->set_template('add');
  195. $this->view->render();
  196. }
  197. } else {
  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_type'] = $course_description->get_max_description_type();
  204. // render to the view
  205. $this->view->set_data($data);
  206. $this->view->set_layout('layout');
  207. $this->view->set_template('add');
  208. $this->view->render();
  209. }
  210. }
  211. /**
  212. * It's used for destroy a course description,
  213. * render to listing view
  214. * @param int description type
  215. */
  216. public function destroy($description_type) {
  217. $course_description = new CourseDescription();
  218. $session_id = api_get_session_id();
  219. $course_description->set_session_id($session_id);
  220. if (!empty($description_type)) {
  221. $course_description->set_description_type($description_type);
  222. $affected_rows = $course_description->delete();
  223. }
  224. if ($affected_rows) {
  225. $message['destroy'] = true;
  226. }
  227. $this->listing(false,$message);
  228. }
  229. }
  230. ?>