Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
defined('MOODLE_INTERNAL') || die();
18
 
19
use core_sms\message_status;
20
 
21
require_once($CFG->dirroot . '/message/output/lib.php');
22
require_once($CFG->libdir . '/moodlelib.php');
23
 
24
/**
25
 * Message processor for SMS.
26
 *
27
 * @package    message_sms
28
 * @copyright  2024 Safat Shahin <safat.shahin@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class message_output_sms extends message_output {
32
 
33
    #[\Override]
34
    public function send_message($message): bool {
35
        $userdata = is_object($message->userto) ? $message->userto : get_complete_user_data('id', $message->userto);
36
 
37
        if (empty($userdata) || !$this->is_user_configured($userdata) || !$this->should_send_sms($message)) {
38
            return false;
39
        }
40
 
41
        $manager = \core\di::get(\core_sms\manager::class);
42
        $result = $manager->send(
43
            recipientnumber: $userdata->phone2,
44
            content: $message->fullmessagesms,
45
            component: $message->component,
46
            messagetype: $message->name,
47
            recipientuserid: $userdata->id,
48
        );
49
 
50
        if ($result->status === message_status::GATEWAY_QUEUED ||
51
            $result->status === message_status::GATEWAY_SENT
52
        ) {
53
            return true;
54
        }
55
 
56
        debugging($result->status->description());
57
        return false;
58
    }
59
 
60
    #[\Override]
61
    public function is_user_configured($user = null): bool {
62
        if (empty($user)) {
63
            return false;
64
        }
65
 
66
        // Skip any SMS if user doesn't have a mobile number.
67
        if (empty($user->phone2)) {
68
            mtrace('No mobile number found for userid: ' . $user->id);
69
            return false;
70
        }
71
 
72
        // Skip any messaging of suspended and deleted users.
73
        if (
74
            $user->auth === 'nologin' ||
75
            $user->suspended ||
76
            $user->deleted
77
        ) {
78
            mtrace('The user with userid: ' . $user->id . ' is either deleted or suspended. Can not send SMS.');
79
            return false;
80
        }
81
 
82
        return true;
83
    }
84
 
85
    /**
86
     * Check whether the SMS message data fulfills the requirements.
87
     *
88
     * @param stdclass $messagedata The message data
89
     * @return bool
90
     */
91
    public function should_send_sms(stdclass $messagedata): bool {
92
        // Don't send SMS if it's not a production site and the following config is set.
93
        if (!empty($CFG->nosmsever)) {
94
            mtrace('Can not send SMS while nosmsever is enabled.');
95
            return false;
96
        }
97
        // We don't have a fallback for SMS text. It has to be included.
98
        if (!isset($messagedata->fullmessagesms)) {
99
            mtrace('No SMS string found for the message');
100
            return false;
101
        }
102
        // Check support for SMS from the component.
103
        if (!core_message\helper::supports_sms_notifications($messagedata)) {
104
            return false;
105
        }
106
 
107
        return true;
108
    }
109
 
110
    #[\Override]
111
    public function load_data(&$preferences, $userid) {
112
        return;
113
    }
114
 
115
    #[\Override]
116
    public function config_form($preferences) {
117
        return;
118
    }
119
 
120
    #[\Override]
121
    public function process_form($form, &$preferences) {
122
        return;
123
    }
124
 
125
    #[\Override]
126
    public function get_default_messaging_settings() {
127
        return MESSAGE_DISALLOWED;
128
    }
129
 
130
    #[\Override]
131
    public function can_send_to_any_users() {
132
        return true;
133
    }
134
}