ajax_controller.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Notebook;
  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 one note
  13. * - delete all notes in a course/session
  14. * - returns a note from its id
  15. *
  16. * Usage:
  17. *
  18. * $controller = AjaxController::instance();
  19. * $controller->run();
  20. *
  21. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  22. * @license /license.txt
  23. */
  24. class AjaxController extends \AjaxController
  25. {
  26. const ACTION_REMOVE = 'remove';
  27. const ACTION_REMOVE_BY_COURSE = 'remove_by_course';
  28. const ACTION_FIND_BY_ID = 'find_by_id';
  29. /**
  30. * Return the instance of the controller.
  31. *
  32. * @return \Notebook\AjaxController
  33. */
  34. public static function instance()
  35. {
  36. static $result = null;
  37. if (empty($result)) {
  38. $result = new self(Access::instance());
  39. }
  40. return $result;
  41. }
  42. /**
  43. * Prepare the environment. Set up breadcrumps and raise tracking event.
  44. */
  45. protected function prolog()
  46. {
  47. event_access_tool(TOOL_NOTEBOOK);
  48. }
  49. public function is_allowed_to_edit()
  50. {
  51. return $this->access()->can_edit();
  52. }
  53. /**
  54. * Remove/delete a Notebook entry
  55. */
  56. public function remove()
  57. {
  58. if (!$this->is_allowed_to_edit()) {
  59. $this->forbidden();
  60. return;
  61. }
  62. $item = Request::get_item_key();
  63. $success = Notebook::repository()->remove($item);
  64. $message = $success ? '' : get_lang('Error');
  65. $this->response($success, $message);
  66. }
  67. /**
  68. * Remove/delete all notebook entries belonging to a course.
  69. */
  70. public function remove_by_course()
  71. {
  72. if (!$this->is_allowed_to_edit()) {
  73. $this->forbidden();
  74. return;
  75. }
  76. $course = Request::get_course_key();
  77. $success = Notebook::repository()->remove_by_course($course);
  78. $message = $success ? '' : get_lang('Error');
  79. $this->response($success, $message);
  80. }
  81. public function find_by_id()
  82. {
  83. $c_id = Request::get_c_id();
  84. $id = Request::get_id();
  85. $item = Notebook::repository()->find_one_by_id($c_id, $id);
  86. $data = (object) array();
  87. if ($item) {
  88. $data->title = $item->title;
  89. $data->description = $item->description;
  90. }
  91. $this->response($success, '', $data);
  92. }
  93. }