See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
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->libdir . '/externallib.php'); 23 require_once($CFG->dirroot . '/message/lib.php'); 24 25 use external_api; 26 use external_function_parameters; 27 use external_value; 28 use context_system; 29 use core_user; 30 use moodle_exception; 31 32 /** 33 * External service to get number of unread notifications 34 * 35 * @package core_message 36 * @category external 37 * @copyright 2021 Dani Palou <dani@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 * @since Moodle 4.0 40 */ 41 class get_unread_notification_count extends external_api { 42 /** 43 * Returns description of method parameters 44 * 45 * @return external_function_parameters 46 */ 47 public static function execute_parameters(): external_function_parameters { 48 return new external_function_parameters([ 49 'useridto' => new external_value(PARAM_INT, 'user id who received the notification, 0 for any user', VALUE_REQUIRED), 50 ]); 51 } 52 53 /** 54 * Get number of unread notifications. 55 * 56 * @param int $useridto the user id who received the notification, 0 for any user 57 * @return int number of unread notifications 58 * @throws \moodle_exception 59 */ 60 public static function execute(int $useridto): int { 61 global $USER, $DB; 62 63 $params = self::validate_parameters( 64 self::execute_parameters(), 65 ['useridto' => $useridto], 66 ); 67 68 $context = context_system::instance(); 69 self::validate_context($context); 70 71 $useridto = $params['useridto']; 72 73 if (!empty($useridto)) { 74 if (core_user::is_real_user($useridto)) { 75 $userto = core_user::get_user($useridto, '*', MUST_EXIST); 76 } else { 77 throw new moodle_exception('invaliduser'); 78 } 79 } 80 81 // Check if the current user is the sender/receiver or just a privileged user. 82 if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { 83 throw new moodle_exception('accessdenied', 'admin'); 84 } 85 86 return $DB->count_records_sql( 87 "SELECT COUNT(n.id) 88 FROM {notifications} n 89 LEFT JOIN {user} u ON (u.id = n.useridfrom AND u.deleted = 0) 90 WHERE n.useridto = ? 91 AND n.timeread IS NULL", 92 [$useridto], 93 ); 94 } 95 96 /** 97 * Describe the return structure of the external service. 98 * 99 * @return external_value 100 */ 101 public static function execute_returns(): external_value { 102 return new external_value(PARAM_INT, 'The count of unread notifications.'); 103 } 104 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body