ajax_controller.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Link;
  3. use \Model\Course;
  4. use \CourseDescription;
  5. use \CourseDescriptionRoutes;
  6. use \Display;
  7. use \Template;
  8. use \FormValidator;
  9. use \Security;
  10. use \Uri;
  11. use Header;
  12. /**
  13. * Ajax controller. Dispatch request and perform required action.
  14. *
  15. * - delete category/link
  16. * - hide/show link
  17. * - sort links/categories
  18. *
  19. * Usage:
  20. *
  21. * $controller = AjaxController::instance();
  22. * $controller->run();
  23. *
  24. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  25. * @license /license.txt
  26. */
  27. class AjaxController extends \Controller
  28. {
  29. const ACTION_DELETE_CATEGORY = 'delete_category';
  30. const ACTION_HIDE_LINK = 'hide_link';
  31. const ACTION_SHOW_LINK = 'show_link';
  32. const ACTION_DELETE_LINK = 'delete_link';
  33. const ACTION_DELETE_BY_COURSE = 'delete_by_course';
  34. const ACTION_SORT_CATEGORIES = 'sort_categories';
  35. const ACTION_SORT_LINKS = 'sort_links';
  36. const ACTION_VALIDATE_LINK = 'validate_link';
  37. /**
  38. * Return the instance of the controller.
  39. *
  40. * @return \Link\AjaxController
  41. */
  42. public static function instance()
  43. {
  44. static $result = null;
  45. if (empty($result)) {
  46. $result = new self();
  47. }
  48. return $result;
  49. }
  50. protected function __construct()
  51. {
  52. }
  53. /**
  54. * Prepare the environment. Set up breadcrumps and raise tracking event.
  55. */
  56. protected function prolog()
  57. {
  58. event_access_tool(TOOL_LINK);
  59. }
  60. public function authorize()
  61. {
  62. $authorize = api_protect_course_script();
  63. if (!$authorize) {
  64. return false;
  65. }
  66. $c_id = Request::get_c_id();
  67. if (empty($c_id)) {
  68. return false;
  69. }
  70. if (Request::is_student_view()) {
  71. return false;
  72. }
  73. if (!$this->is_allowed_to_edit()) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. public function is_allowed_to_edit()
  79. {
  80. $session_id = Request::get_session_id();
  81. if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
  82. return false;
  83. }
  84. // if (!Security::check_token('get')) {
  85. // return false;
  86. // }
  87. if (!api_is_allowed_to_edit(false, true, true)) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. *
  94. */
  95. public function hide_link()
  96. {
  97. if (!$this->is_allowed_to_edit()) {
  98. $this->forbidden();
  99. return;
  100. }
  101. $c_id = Request::get_c_id();
  102. $id = Request::get_id();
  103. $success = LinkRepository::instance()->make_invisible($c_id, $id);
  104. $this->response($success);
  105. }
  106. /**
  107. *
  108. */
  109. public function show_link()
  110. {
  111. if (!$this->is_allowed_to_edit()) {
  112. $this->forbidden();
  113. return;
  114. }
  115. $c_id = Request::get_c_id();
  116. $id = Request::get_id();
  117. $success = LinkRepository::instance()->make_visible($c_id, $id);
  118. $this->response($success);
  119. }
  120. /**
  121. *
  122. */
  123. public function delete_link()
  124. {
  125. if (!$this->is_allowed_to_edit()) {
  126. $this->forbidden();
  127. return;
  128. }
  129. $link = (object) array();
  130. $link->c_id = Request::get_c_id();
  131. $link->id = Request::get_id();
  132. $success = LinkRepository::instance()->remove($link);
  133. $this->response($success);
  134. }
  135. /**
  136. *
  137. */
  138. public function delete_category()
  139. {
  140. if (!$this->is_allowed_to_edit()) {
  141. $this->forbidden();
  142. return;
  143. }
  144. $category = (object) array();
  145. $category->c_id = Request::get_c_id();
  146. $category->id = Request::get_id();
  147. $success = LinkCategoryRepository::instance()->remove($category);
  148. $this->response($success);
  149. }
  150. /**
  151. *
  152. */
  153. public function delete_by_course()
  154. {
  155. if (!$this->is_allowed_to_edit()) {
  156. $this->forbidden();
  157. return;
  158. }
  159. $c_id = Request::get_c_id();
  160. $session_id = Request::get_session_id();
  161. $success_link = LinkRepository::instance()->remove_by_course($c_id, $session_id);
  162. $success_cat = LinkCategoryRepository::instance()->remove_by_course($c_id, $session_id);
  163. $this->response($success_link && $success_cat);
  164. }
  165. public function sort_categories()
  166. {
  167. if (!$this->is_allowed_to_edit()) {
  168. $this->forbidden();
  169. return;
  170. }
  171. $c_id = Request::get_c_id();
  172. $ids = Request::get_ids();
  173. if (empty($ids)) {
  174. return;
  175. }
  176. $repo = LinkCategoryRepository::instance();
  177. $success = $repo->order($c_id, $ids);
  178. $this->response($success);
  179. }
  180. public function sort_links()
  181. {
  182. if (!$this->is_allowed_to_edit()) {
  183. $this->forbidden();
  184. return;
  185. }
  186. $c_id = Request::get_c_id();
  187. $ids = Request::get_ids();
  188. if (empty($ids)) {
  189. return;
  190. }
  191. $repo = LinkRepository::instance();
  192. $success = $repo->order($c_id, $ids);
  193. $this->response($success);
  194. }
  195. public function validate_link()
  196. {
  197. $c_id = Request::get_c_id();
  198. $id = Request::get_id();
  199. $repo = LinkRepository::instance();
  200. $link = $repo->find_one_by_id($c_id, $id);
  201. $success = $link ? $link->validate() : false;
  202. $this->response($success);
  203. }
  204. function forbidden()
  205. {
  206. $this->response(false, get_lang('YouAreNotAuthorized'));
  207. }
  208. public function unknown()
  209. {
  210. $this->response(false, get_lang('UnknownAction'));
  211. }
  212. /**
  213. * Action exists but implementation is missing.
  214. */
  215. public function missing()
  216. {
  217. $this->response(false, get_lang('NoImplementation'));
  218. }
  219. /**
  220. * Display a standard json responce.
  221. *
  222. * @param bool $success
  223. * @param string $message
  224. * @param object $data
  225. */
  226. public function response($success = false, $message = '', $data = null)
  227. {
  228. $message = trim($message);
  229. $response = (object) array();
  230. $response->success = $success;
  231. if ($message) {
  232. $response->message = Display::return_message($message, $success ? 'normal' : 'error');
  233. } else {
  234. $response->message = '';
  235. }
  236. $response->data = $data;
  237. $this->render_json($response);
  238. }
  239. }