message/output/popup/amd/src/notification_repository.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Retrieves notifications from the server.
  17. *
  18. * @module message_popup/notification_repository
  19. * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['core/ajax', 'core/notification'], function(Ajax, Notification) {
  23. /**
  24. * Retrieve a list of notifications from the server.
  25. *
  26. * @param {object} args The request arguments
  27. * @return {object} jQuery promise
  28. */
  29. var query = function(args) {
  30. if (typeof args.limit === 'undefined') {
  31. args.limit = 20;
  32. }
  33. if (typeof args.offset === 'undefined') {
  34. args.offset = 0;
  35. }
  36. var request = {
  37. methodname: 'message_popup_get_popup_notifications',
  38. args: args
  39. };
  40. var promise = Ajax.call([request])[0];
  41. promise.fail(Notification.exception);
  42. return promise;
  43. };
  44. /**
  45. * Get the number of unread notifications from the server.
  46. *
  47. * @param {object} args The request arguments
  48. * @return {object} jQuery promise
  49. */
  50. var countUnread = function(args) {
  51. var request = {
  52. methodname: 'message_popup_get_unread_popup_notification_count',
  53. args: args
  54. };
  55. var promise = Ajax.call([request])[0];
  56. promise.fail(Notification.exception);
  57. return promise;
  58. };
  59. /**
  60. * Mark all notifications for the given user as read.
  61. *
  62. * @param {object} args The request arguments:
  63. * @return {object} jQuery promise
  64. */
  65. var markAllAsRead = function(args) {
  66. var request = {
  67. methodname: 'core_message_mark_all_notifications_as_read',
  68. args: args
  69. };
  70. var promise = Ajax.call([request])[0];
  71. promise.fail(Notification.exception);
  72. return promise;
  73. };
  74. /**
  75. * Mark a specific notification as read.
  76. *
  77. * @param {int} id The notification id
  78. * @param {int} timeread The read timestamp (optional)
  79. * @return {object} jQuery promise
  80. */
  81. var markAsRead = function(id, timeread) {
  82. var args = {
  83. notificationid: id,
  84. };
  85. if (timeread) {
  86. args.timeread = timeread;
  87. }
  88. var request = {
  89. methodname: 'core_message_mark_notification_read',
  90. args: args
  91. };
  92. var promise = Ajax.call([request])[0];
  93. promise.fail(Notification.exception);
  94. return promise;
  95. };
  96. return {
  97. query: query,
  98. countUnread: countUnread,
  99. markAllAsRead: markAllAsRead,
  100. markAsRead: markAsRead,
  101. };
  102. });