| 1 | 
           efrain | 
           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 | 
           /**
  | 
        
        
            | 
            | 
           17 | 
            * Retrieves notifications from the server.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     message_popup/notification_repository
  | 
        
        
            | 
            | 
           20 | 
            * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
  | 
        
        
            | 
            | 
           21 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
           define(['core/ajax', 'core/notification'], function(Ajax, Notification) {
  | 
        
        
            | 
            | 
           24 | 
               /**
  | 
        
        
            | 
            | 
           25 | 
                * Retrieve a list of notifications from the server.
  | 
        
        
            | 
            | 
           26 | 
                *
  | 
        
        
            | 
            | 
           27 | 
                * @param {object} args The request arguments
  | 
        
        
            | 
            | 
           28 | 
                * @return {object} jQuery promise
  | 
        
        
            | 
            | 
           29 | 
                */
  | 
        
        
            | 
            | 
           30 | 
               var query = function(args) {
  | 
        
        
            | 
            | 
           31 | 
                   if (typeof args.limit === 'undefined') {
  | 
        
        
            | 
            | 
           32 | 
                       args.limit = 20;
  | 
        
        
            | 
            | 
           33 | 
                   }
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
                   if (typeof args.offset === 'undefined') {
  | 
        
        
            | 
            | 
           36 | 
                       args.offset = 0;
  | 
        
        
            | 
            | 
           37 | 
                   }
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
                   var request = {
  | 
        
        
            | 
            | 
           40 | 
                       methodname: 'message_popup_get_popup_notifications',
  | 
        
        
            | 
            | 
           41 | 
                       args: args
  | 
        
        
            | 
            | 
           42 | 
                   };
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
                   var promise = Ajax.call([request])[0];
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
                   promise.fail(Notification.exception);
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   return promise;
  | 
        
        
            | 
            | 
           49 | 
               };
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               /**
  | 
        
        
            | 
            | 
           52 | 
                * Get the number of unread notifications from the server.
  | 
        
        
            | 
            | 
           53 | 
                *
  | 
        
        
            | 
            | 
           54 | 
                * @param {object} args The request arguments
  | 
        
        
            | 
            | 
           55 | 
                * @return {object} jQuery promise
  | 
        
        
            | 
            | 
           56 | 
                */
  | 
        
        
            | 
            | 
           57 | 
               var countUnread = function(args) {
  | 
        
        
            | 
            | 
           58 | 
                   var request = {
  | 
        
        
            | 
            | 
           59 | 
                       methodname: 'message_popup_get_unread_popup_notification_count',
  | 
        
        
            | 
            | 
           60 | 
                       args: args
  | 
        
        
            | 
            | 
           61 | 
                   };
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   var promise = Ajax.call([request])[0];
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   promise.fail(Notification.exception);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   return promise;
  | 
        
        
            | 
            | 
           68 | 
               };
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
               /**
  | 
        
        
            | 
            | 
           71 | 
                * Mark all notifications for the given user as read.
  | 
        
        
            | 
            | 
           72 | 
                *
  | 
        
        
            | 
            | 
           73 | 
                * @param {object} args The request arguments:
  | 
        
        
            | 
            | 
           74 | 
                * @return {object} jQuery promise
  | 
        
        
            | 
            | 
           75 | 
                */
  | 
        
        
            | 
            | 
           76 | 
               var markAllAsRead = function(args) {
  | 
        
        
            | 
            | 
           77 | 
                   var request = {
  | 
        
        
            | 
            | 
           78 | 
                       methodname: 'core_message_mark_all_notifications_as_read',
  | 
        
        
            | 
            | 
           79 | 
                       args: args
  | 
        
        
            | 
            | 
           80 | 
                   };
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   var promise = Ajax.call([request])[0];
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   promise.fail(Notification.exception);
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
                   return promise;
  | 
        
        
            | 
            | 
           87 | 
               };
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
               /**
  | 
        
        
            | 
            | 
           90 | 
                * Mark a specific notification as read.
  | 
        
        
            | 
            | 
           91 | 
                *
  | 
        
        
            | 
            | 
           92 | 
                * @param {int} id The notification id
  | 
        
        
            | 
            | 
           93 | 
                * @param {int} timeread The read timestamp (optional)
  | 
        
        
            | 
            | 
           94 | 
                * @return {object} jQuery promise
  | 
        
        
            | 
            | 
           95 | 
                */
  | 
        
        
            | 
            | 
           96 | 
               var markAsRead = function(id, timeread) {
  | 
        
        
            | 
            | 
           97 | 
                   var args = {
  | 
        
        
            | 
            | 
           98 | 
                       notificationid: id,
  | 
        
        
            | 
            | 
           99 | 
                   };
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                   if (timeread) {
  | 
        
        
            | 
            | 
           102 | 
                       args.timeread = timeread;
  | 
        
        
            | 
            | 
           103 | 
                   }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                   var request = {
  | 
        
        
            | 
            | 
           106 | 
                       methodname: 'core_message_mark_notification_read',
  | 
        
        
            | 
            | 
           107 | 
                       args: args
  | 
        
        
            | 
            | 
           108 | 
                   };
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
                   var promise = Ajax.call([request])[0];
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                   promise.fail(Notification.exception);
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
                   return promise;
  | 
        
        
            | 
            | 
           115 | 
               };
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
               return {
  | 
        
        
            | 
            | 
           118 | 
                   query: query,
  | 
        
        
            | 
            | 
           119 | 
                   countUnread: countUnread,
  | 
        
        
            | 
            | 
           120 | 
                   markAllAsRead: markAllAsRead,
  | 
        
        
            | 
            | 
           121 | 
                   markAsRead: markAsRead,
  | 
        
        
            | 
            | 
           122 | 
               };
  | 
        
        
            | 
            | 
           123 | 
           });
  |