notifier   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 159
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A htmlmsg_instance_updated() 0 26 1
A notify_instance_updated() 0 31 2
A htmlmsg_recording_ready() 0 5 1
A notify_recording_ready() 0 6 1
A enqueue_notifications() 0 22 4
A send_notification() 0 4 1
A receivers() 0 7 1
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
 * The mod_bigbluebuttonbn locallib/notifier.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2017 - present, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
24
 */
25
26
namespace mod_bigbluebuttonbn\locallib;
27
28
use html_writer;
29
use mod_bigbluebuttonbn\plugin;
30
31
defined('MOODLE_INTERNAL') || die();
32
33
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
34
35
/**
36
 * Helper class for sending notifications.
37
 *
38
 * @copyright 2010 onwards, Blindside Networks Inc
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class notifier
42
{
43
    /**
44
     * Prepares html message body for instance updated notification.
45
     *
46
     * @param object $msg
47
     * @return string
48
     */
49
    public static function htmlmsg_instance_updated($msg) {
50
        $messagetext = '<p>'.get_string('pluginname', 'bigbluebuttonbn').
51
            ' <b>'.$msg->activity_url.'</b> '.
52
            get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>'."\n";
53
        $messagetext .= '<p>'.get_string('email_body_notification_meeting_details', 'bigbluebuttonbn').':'."\n";
54
        $messagetext .= '<table border="0" style="margin: 5px 0 0 20px"><tbody>'."\n";
55
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
56
            get_string('email_body_notification_meeting_title', 'bigbluebuttonbn').': </td><td>'."\n";
57
        $messagetext .= $msg->activity_title.'</td></tr>'."\n";
58
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
59
            get_string('email_body_notification_meeting_description', 'bigbluebuttonbn').': </td><td>'."\n";
60
        $messagetext .= $msg->activity_description.'</td></tr>'."\n";
61
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
62
            get_string('email_body_notification_meeting_start_date', 'bigbluebuttonbn').': </td><td>'."\n";
63
        $messagetext .= $msg->activity_openingtime.'</td></tr>'."\n";
64
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
65
            get_string('email_body_notification_meeting_end_date', 'bigbluebuttonbn').': </td><td>'."\n";
66
        $messagetext .= $msg->activity_closingtime.'</td></tr>'."\n";
67
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.$msg->action.' '.
68
            get_string('email_body_notification_meeting_by', 'bigbluebuttonbn').': </td><td>'."\n";
69
        $messagetext .= $msg->activity_owner.'</td></tr></tbody></table></p>'."\n";
70
        $messagetext .= '<p><hr/><br/>'.get_string('email_footer_sent_by', 'bigbluebuttonbn').' '.
71
            $msg->user_name.' ';
72
        $messagetext .= get_string('email_footer_sent_from', 'bigbluebuttonbn').' '.$msg->course_name.'.</p>';
73
        return $messagetext;
74
    }
75
76
    /**
77
     * Starts the notification process.
78
     *
79
     * @param object $bigbluebuttonbn
80
     * @param string $action
81
     * @return void
82
     */
83
    public static function notify_instance_updated($bigbluebuttonbn, $action) {
84
        global $USER;
85
        $coursemodinfo = \course_modinfo::instance($bigbluebuttonbn->course);
86
        $course = $coursemodinfo->get_course($bigbluebuttonbn->course);
87
        $sender = $USER;
88
        // Prepare message.
89
        $msg = (object) array();
90
        // Build the message_body.
91
        $msg->action = $action;
92
        $msg->activity_url = html_writer::link(
93
            plugin::necurl('/mod/bigbluebuttonbn/view.php', ['id' => $bigbluebuttonbn->coursemodule]),
94
            format_string($bigbluebuttonbn->name)
95
        );
96
        $msg->activity_title = format_string($bigbluebuttonbn->name);
97
        // Add the meeting details to the message_body.
98
        $msg->action = ucfirst($action);
99
        $msg->activity_description = '';
100
        if (!empty($bigbluebuttonbn->intro)) {
101
            $msg->activity_description = format_string(trim($bigbluebuttonbn->intro));
102
        }
103
        $msg->activity_openingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->openingtime);
104
        $msg->activity_closingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->closingtime);
105
        $msg->activity_owner = fullname($sender);
106
107
        $msg->user_name = fullname($sender);
108
        $msg->user_email = $sender->email;
109
        $msg->course_name = $course->fullname;
110
111
        // Send notification to all users enrolled.
112
        self::enqueue_notifications($bigbluebuttonbn, $sender, self::htmlmsg_instance_updated($msg));
113
    }
114
115
    /**
116
     * Prepares html message body for recording ready notification.
117
     *
118
     * @param object $bigbluebuttonbn
119
     *
120
     * @return void
121
     */
122
    public static function htmlmsg_recording_ready($bigbluebuttonbn) {
123
        return '<p>'.get_string('email_body_recording_ready_for', 'bigbluebuttonbn').
124
            ' &quot;' . $bigbluebuttonbn->name . '&quot; '.
125
            get_string('email_body_recording_ready_is_ready', 'bigbluebuttonbn').'.</p>';
126
    }
127
128
    /**
129
     * Helper function triggers a send notification when the recording is ready.
130
     *
131
     * @param object $bigbluebuttonbn
132
     *
133
     * @return void
134
     */
135
    public static function notify_recording_ready($bigbluebuttonbn) {
136
        // Instead of get_admin, the firs user enrolled with editing privileges may be used as the sender.
137
        $sender = get_admin();
138
        $htmlmsg = self::htmlmsg_recording_ready($bigbluebuttonbn);
139
        self::enqueue_notifications($bigbluebuttonbn, $sender, $htmlmsg);
140
    }
141
142
    /**
143
     * Enqueue notifications to be sent to all users in a context where the instance belongs.
144
     *
145
     * @param object $bigbluebuttonbn
146
     * @param object $sender
147
     * @param string $htmlmsg
148
     * @return void
149
     */
150
    public static function enqueue_notifications($bigbluebuttonbn, $sender, $htmlmsg) {
151
        foreach (self::receivers($bigbluebuttonbn->course) as $receiver) {
152
            if ($sender->id != $receiver->id) {
153
                // Enqueue a task for sending a notification.
154
                try {
155
                    // Create the instance of completion_update_state task.
156
                    $task = new \mod_bigbluebuttonbn\task\send_notification();
157
                    // Add custom data.
158
                    $data = array(
159
                        'sender' => $sender,
160
                        'receiver' => $receiver,
161
                        'htmlmsg' => $htmlmsg
162
                    );
163
                    $task->set_custom_data($data);
164
                    // Enqueue it.
165
                    \core\task\manager::queue_adhoc_task($task);
166
                } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class mod_bigbluebuttonbn\locallib\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
167
                    mtrace("Error while enqueuing completion_uopdate_state task. " . (string) $e);
168
                }
169
            }
170
        }
171
    }
172
173
    /**
174
     * Sends notification to a user.
175
     *
176
     * @param object $sender
177
     * @param object $receiver
178
     * @param object $htmlmsg
179
     * @return void
180
     */
181
    public static function send_notification($sender, $receiver, $htmlmsg) {
182
        // Send the message.
183
        message_post_message($sender, $receiver, $htmlmsg, FORMAT_HTML);
184
    }
185
186
    /**
187
     * Define users to be notified.
188
     *
189
     * @param object $courseid
190
     * @return array
191
     */
192
    public static function receivers($courseid) {
193
        $context = \context_course::instance($courseid);
194
        $users = array();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
        // Potential users should be active users only.
196
        $users = get_enrolled_users($context, 'mod/bigbluebuttonbn:view', 0, 'u.*', null, 0, 0, true);
197
        return $users;
198
    }
199
}
200