Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_message\external;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/message/lib.php');
23
 
24
use core_external\external_api;
25
use core_external\external_function_parameters;
26
use core_external\external_value;
27
use context_system;
28
use core_user;
29
use moodle_exception;
30
 
31
/**
32
 * External service to get number of unread notifications
33
 *
34
 * @package   core_message
35
 * @category  external
36
 * @copyright 2021 Dani Palou <dani@moodle.com>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @since     Moodle 4.0
39
 */
40
class get_unread_notification_count extends external_api {
41
    /**
42
     * Returns description of method parameters
43
     *
44
     * @return external_function_parameters
45
     */
46
    public static function execute_parameters(): external_function_parameters {
47
        return new external_function_parameters([
48
            'useridto' => new external_value(PARAM_INT, 'user id who received the notification, 0 for any user', VALUE_REQUIRED),
49
        ]);
50
    }
51
 
52
    /**
53
     * Get number of unread notifications.
54
     *
55
     * @param int $useridto the user id who received the notification, 0 for any user
56
     * @return int number of unread notifications
57
     * @throws \moodle_exception
58
     */
59
    public static function execute(int $useridto): int {
60
        global $USER, $DB;
61
 
62
        $params = self::validate_parameters(
63
            self::execute_parameters(),
64
            ['useridto' => $useridto],
65
        );
66
 
67
        $context = context_system::instance();
68
        self::validate_context($context);
69
 
70
        $useridto = $params['useridto'];
71
 
72
        if (!empty($useridto)) {
73
            if (core_user::is_real_user($useridto)) {
74
                $userto = core_user::get_user($useridto, '*', MUST_EXIST);
75
            } else {
76
                throw new moodle_exception('invaliduser');
77
            }
78
        }
79
 
80
        // Check if the current user is the sender/receiver or just a privileged user.
81
        if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
82
            throw new moodle_exception('accessdenied', 'admin');
83
        }
84
 
85
        return $DB->count_records_sql(
86
            "SELECT COUNT(n.id)
87
               FROM {notifications} n
88
          LEFT JOIN {user} u ON (u.id = n.useridfrom AND u.deleted = 0)
89
              WHERE n.useridto = ?
90
                    AND n.timeread IS NULL",
91
            [$useridto],
92
        );
93
    }
94
 
95
    /**
96
     * Describe the return structure of the external service.
97
     *
98
     * @return external_value
99
     */
100
    public static function execute_returns(): external_value {
101
        return new external_value(PARAM_INT, 'The count of unread notifications.');
102
    }
103
}