ajax_controller.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Course description's ajax controller class definition
  5. * @package chamilo.course_description
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace CourseDescription;
  11. use \Display;
  12. use \Template;
  13. use \FormValidator;
  14. use \Security;
  15. use \Uri;
  16. use Header;
  17. /**
  18. * Ajax controller. Dispatch request and perform required action.
  19. *
  20. * - delete category/link
  21. *
  22. * Usage:
  23. *
  24. * $controller = AjaxController::instance();
  25. * $controller->run();
  26. *
  27. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  28. * @license /license.txt
  29. */
  30. class AjaxController extends \Controller
  31. {
  32. const ACTION_DELETE = 'delete';
  33. const ACTION_DELETE_BY_COURSE = 'delete_by_course';
  34. /**
  35. * Return the instance of the controller.
  36. *
  37. * @return \CourseDescription\AjaxController
  38. */
  39. public static function instance()
  40. {
  41. static $result = null;
  42. if (empty($result)) {
  43. $result = new self();
  44. }
  45. return $result;
  46. }
  47. protected function __construct()
  48. {
  49. }
  50. /**
  51. * Prepare the environment. Set up breadcrumps and raise tracking event.
  52. */
  53. protected function prolog()
  54. {
  55. event_access_tool(TOOL_COURSE_DESCRIPTION);
  56. }
  57. public function is_allowed_to_edit()
  58. {
  59. if (Request::is_student_view()) {
  60. return false;
  61. }
  62. $session_id = Request::get_session_id();
  63. if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  64. return false;
  65. }
  66. if (!api_is_allowed_to_edit(false, true, true)) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. public function authorize()
  72. {
  73. $authorize = api_protect_course_script();
  74. if (!$authorize) {
  75. return false;
  76. }
  77. $c_id = Request::get_c_id();
  78. if (empty($c_id)) {
  79. return false;
  80. }
  81. if (Request::is_student_view()) {
  82. return false;
  83. }
  84. if (!$this->is_allowed_to_edit()) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. *
  91. */
  92. public function delete()
  93. {
  94. if (!$this->is_allowed_to_edit()) {
  95. $this->forbidden();
  96. return;
  97. }
  98. $description = (object) array();
  99. $description->c_id = Request::get_c_id();
  100. $description->id = Request::get_id();
  101. $success = CourseDescription::repository()->remove($description);
  102. $this->response($success);
  103. }
  104. /**
  105. *
  106. */
  107. public function delete_by_course()
  108. {
  109. if (!$this->is_allowed_to_edit()) {
  110. $this->forbidden();
  111. return;
  112. }
  113. $course = (object) array();
  114. $course->c_id = Request::get_c_id();
  115. $course->session_id = Request::get_session_id();
  116. $success = CourseDescription::repository()->remove_by_course($course);
  117. $this->response($success);
  118. }
  119. function forbidden()
  120. {
  121. $this->response(false, get_lang('YouAreNotAuthorized'));
  122. }
  123. public function unknown()
  124. {
  125. $this->response(false, get_lang('UnknownAction'));
  126. }
  127. /**
  128. * Action exists but implementation is missing.
  129. */
  130. public function missing()
  131. {
  132. $this->response(false, get_lang('NoImplementation'));
  133. }
  134. /**
  135. * Display a standard json responce.
  136. *
  137. * @param bool $success
  138. * @param string $message
  139. * @param object $data
  140. */
  141. public function response($success = false, $message = '', $data = null)
  142. {
  143. $message = trim($message);
  144. $response = (object) array();
  145. $response->success = $success;
  146. if ($message) {
  147. $response->message = Display::return_message($message, $success ? 'normal' : 'error');
  148. } else {
  149. $response->message = '';
  150. }
  151. $response->data = $data;
  152. $this->render_json($response);
  153. }
  154. }