ajax_controller.class.php 3.8 KB

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