Completed
Branch master (50b31b)
by Jesus
02:00
created

notifier::enqueue_notifications()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 3
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
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
defined('MOODLE_INTERNAL') || die();
29
30
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
31
32
/**
33
 * Helper class for sending notifications.
34
 *
35
 * @copyright 2010 onwards, Blindside Networks Inc
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class notifier
39
{
40
    /**
41
     * Prepares html message body for instance updated notification.
42
     *
43
     * @param object $msg
44
     * @return string
45
     */
46
    public static function htmlmsg_instance_updated($msg) {
47
        $messagetext = '<p>'.$msg->activity_type.' "'.$msg->activity_title.'" '.
48
            get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>'."\n";
49
        $messagetext .= '<p><b>'.$msg->activity_title.'</b> '.
50
            get_string('email_body_notification_meeting_details', 'bigbluebuttonbn').':'."\n";
51
        $messagetext .= '<table border="0" style="margin: 5px 0 0 20px"><tbody>'."\n";
52
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
53
            get_string('email_body_notification_meeting_title', 'bigbluebuttonbn').': </td><td>'."\n";
54
        $messagetext .= $msg->activity_title.'</td></tr>'."\n";
55
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
56
            get_string('email_body_notification_meeting_description', 'bigbluebuttonbn').': </td><td>'."\n";
57
        $messagetext .= $msg->activity_description.'</td></tr>'."\n";
58
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
59
            get_string('email_body_notification_meeting_start_date', 'bigbluebuttonbn').': </td><td>'."\n";
60
        $messagetext .= $msg->activity_openingtime.'</td></tr>'."\n";
61
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
62
            get_string('email_body_notification_meeting_end_date', 'bigbluebuttonbn').': </td><td>'."\n";
63
        $messagetext .= $msg->activity_closingtime.'</td></tr>'."\n";
64
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.$msg->action.' '.
65
            get_string('email_body_notification_meeting_by', 'bigbluebuttonbn').': </td><td>'."\n";
66
        $messagetext .= $msg->activity_owner.'</td></tr></tbody></table></p>'."\n";
67
        $messagetext .= '<p><hr/><br/>'.get_string('email_footer_sent_by', 'bigbluebuttonbn').' '.
68
            $msg->user_name.'('.$msg->user_email.') ';
69
        $messagetext .= get_string('email_footer_sent_from', 'bigbluebuttonbn').' '.$msg->course_name.'.</p>';
70
        return $messagetext;
71
    }
72
73
    /**
74
     * Starts the notification process.
75
     *
76
     * @param object $bigbluebuttonbn
77
     * @param string $action
78
     * @return void
79
     */
80
    public static function notify_instance_updated($bigbluebuttonbn, $action) {
81
        global $USER;
82
        $coursemodinfo = \course_modinfo::instance($bigbluebuttonbn->course);
83
        $course = $coursemodinfo->get_course($bigbluebuttonbn->course);
84
        $sender = $USER;
85
        // Prepare message.
86
        $msg = (object) array();
87
        // Build the message_body.
88
        $msg->action = $action;
89
        $msg->activity_type = '';
90
        $msg->activity_title = $bigbluebuttonbn->name;
91
        // Add the meeting details to the message_body.
92
        $msg->action = ucfirst($action);
93
        $msg->activity_description = '';
94
        if (!empty($bigbluebuttonbn->intro)) {
95
            $msg->activity_description = trim($bigbluebuttonbn->intro);
96
        }
97
        $msg->activity_openingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->openingtime);
98
        $msg->activity_closingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->closingtime);
99
        $msg->activity_owner = fullname($sender);
100
101
        $msg->user_name = fullname($sender);
102
        $msg->user_email = $sender->email;
103
        $msg->course_name = $course->fullname;
104
105
        // Send notification to all users enrolled.
106
        self::enqueue_notifications($bigbluebuttonbn, $sender, self::htmlmsg_instance_updated($msg));
107
    }
108
109
    /**
110
     * Prepares html message body for recording ready notification.
111
     *
112
     * @param object $bigbluebuttonbn
113
     *
114
     * @return void
115
     */
116
    public static function htmlmsg_recording_ready($bigbluebuttonbn) {
117
        return '<p>'.get_string('email_body_recording_ready_for', 'bigbluebuttonbn').
118
            ' &quot;' . $bigbluebuttonbn->name . '&quot; '.
119
            get_string('email_body_recording_ready_is_ready', 'bigbluebuttonbn').'.</p>';
120
    }
121
122
    /**
123
     * Helper function triggers a send notification when the recording is ready.
124
     *
125
     * @param object $bigbluebuttonbn
126
     *
127
     * @return void
128
     */
129
    public static function notify_recording_ready($bigbluebuttonbn) {
130
        // Instead of get_admin, the firs user enrolled with editing privileges may be used as the sender.
131
        $sender = get_admin();
132
        $htmlmsg = self::htmlmsg_recording_ready($bigbluebuttonbn);
133
        self::enqueue_notifications($bigbluebuttonbn, $sender, $htmlmsg);
134
    }
135
136
    /**
137
     * Enqueue notifications to be sent to all users in a context where the instance belongs.
138
     *
139
     * @param object $bigbluebuttonbn
140
     * @param object $sender
141
     * @param string $htmlmsg
142
     * @return void
143
     */
144
    public static function enqueue_notifications($bigbluebuttonbn, $sender, $htmlmsg) {
145
        foreach (self::receivers($bigbluebuttonbn->course) as $receiver) {
146
            if ($sender->id != $receiver->id) {
147
                // Enqueue a task for sending a notification.
148
                try {
149
                    // Create the instance of completion_update_state task.
150
                    $task = new \mod_bigbluebuttonbn\task\send_notification();
151
                    // Add custom data.
152
                    $data = array(
153
                        'sender' => $sender,
154
                        'receiver' => $receiver,
155
                        'htmlmsg' => $htmlmsg
156
                    );
157
                    $task->set_custom_data($data);
158
                    // Enqueue it.
159
                    \core\task\manager::queue_adhoc_task($task);
160
                } 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...
161
                    mtrace("Error while enqueuing completion_uopdate_state task. " . (string) $e);
162
                }
163
            }
164
        }
165
    }
166
167
    /**
168
     * Sends notification to a user.
169
     *
170
     * @param object $sender
171
     * @param object $receiver
172
     * @param object $htmlmsg
173
     * @return void
174
     */
175
    public static function send_notification($sender, $receiver, $htmlmsg) {
176
        // Send the message.
177
        message_post_message($sender, $receiver, $htmlmsg, FORMAT_HTML);
178
    }
179
180
    /**
181
     * Define users to be notified.
182
     *
183
     * @param object $courseid
184
     * @return array
185
     */
186
    public static function receivers($courseid) {
187
        $context = \context_course::instance($courseid);
188
        $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...
189
        // See if there are any users in the lesson.
190
        list($sort, $params) = users_order_by_sql('u');
0 ignored issues
show
Unused Code introduced by
The assignment to $params is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
191
        $users = get_enrolled_users($context, 'mod/bigbluebuttonbn:view', 0, 'u.*', $sort);
192
        return $users;
193
    }
194
}
195