ajax_controller.class.php 2.6 KB

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