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
/**
18
 * Amanote notification helper functions.
19
 *
20
 * @package     filter_amanote
21
 * @copyright   2023 Amaplex Software
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
require_once(__DIR__ . '/../amanote.php');
28
 
29
/**
30
 * Send a notification to a given user.
31
 *
32
 * @param string $kind  The kind of the notification (see db/messages.php).
33
 * @param int    $courseid  The ID of the course that the notification is related to.
34
 * @param object $userto  The user object representing the recipient of the notification.
35
 * @param string $subject The subject line of the notification.
36
 * @param string $message The body of the message, which will be in HTML format.
37
 * @param string|null $contexturl Optional URL to redirect the user when clicking on the notification.
38
 *
39
 * @return int|bool Returns the ID of the sent message if successful, or false on failure.
40
 */
41
function send_notification($kind, $courseid, $userto, $subject, $message, $contexturl = null) {
42
    // Generate the notification.
43
    $notification = new \core\message\message();
44
    $notification->notification = 1;
45
    $notification->component = 'filter_amanote';
46
    $notification->name = $kind;
47
    $notification->courseid = $courseid;
48
    $notification->userfrom = core_user::get_noreply_user();
49
    $notification->userto = $userto;
50
    $notification->subject = $subject;
51
    $notification->fullmessage = $message;
52
    $notification->fullmessageformat = FORMAT_HTML;
53
    $notification->fullmessagehtml = $message;
54
    $notification->contexturl = $contexturl;
55
 
56
    // Send the notification.
57
    return message_send($notification);
58
}
59
 
60
/**
61
 * Send notifications related to a given annotatable.
62
 *
63
 * @param string $kind  The type of notification ('submission').
64
 * @param string $annotatableid The annotatable id
65
 *
66
 * @return array  An array containing the status of sent notifications.
67
 */
68
function send_annotatable_notifications($kind, $annotatableid) {
69
    global $DB, $USER;
70
 
71
    $notifications = [];
72
    $explodedid = explode('.', $annotatableid);
73
    $courseid = $explodedid[0];
74
    $context = context_course::instance($courseid);
75
 
76
    // Handle submission notification.
77
    if ($kind === 'submission') {
78
        $message = '[Amanote] ' . get_string('submissionnotification', 'filter_amanote', fullname($USER));;
79
 
80
        // Get the list of teachers in the course.
81
        $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
82
        $teachers = get_role_users($teacherrole->id, $context, false, 'u.*');
83
 
84
        // Generate the context URL.
85
        $contexturl = generate_amanote_url($annotatableid, null, null, 'document-analytics/' . $annotatableid . '/notes');
86
 
87
        foreach ($teachers as $teacher) {
88
            array_push($notifications, send_notification($kind, $courseid, $teacher, $message, $message, $contexturl));
89
        }
90
    }
91
 
92
    return $notifications;
93
}