Completed
Pull Request — master (#164)
by Jesus
02:08
created

notifier::send_notification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
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::send_notifications($bigbluebuttonbn, $sender, self::htmlmsg_instance_updated($msg));
0 ignored issues
show
Bug introduced by
The method send_notifications() does not exist on mod_bigbluebuttonbn\locallib\notifier. Did you maybe mean send_notification()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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
        $sender = get_admin();
131
        $htmlmsg = self::htmlmsg_recording_ready($bigbluebuttonbn);
132
        self::send_notifications($bigbluebuttonbn, $sender, $htmlmsg);
0 ignored issues
show
Bug introduced by
The method send_notifications() does not exist on mod_bigbluebuttonbn\locallib\notifier. Did you maybe mean send_notification()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
133
    }
134
135
    /**
136
     * Enqueue notifications to be sent to all users in a context where the instance belongs.
137
     *
138
     * @param object $bigbluebuttonbn
139
     * @param object $sender
140
     * @param string $htmlmsg
141
     * @return void
142
     */
143
    public static function enqueue_notifications($bigbluebuttonbn, $sender, $htmlmsg) {
144
        foreach (self::receivers($bigbluebuttonbn->course) as $receiver) {
145
            if ($sender->id != $receiver->id) {
146
                // Enqueue a task for sending a notification.
147
                try {
148
                    // Create the instance of completion_update_state task.
149
                    $task = new \mod_bigbluebuttonbn\task\send_notification();
150
                    // Add custom data.
151
                    $data = array(
152
                        'sender' => $sender,
153
                        'receiver' => $receiver,
154
                        'htmlmsg' => $htmlmsg
155
                    );
156
                    $task->set_custom_data($data);
157
                    // CONTRIB-7457: Task should be executed by a user, maybe Teacher as Student won't have rights for everriding.
158
                    // $ task -> set_userid ( $ user -> id );.
159
                    // Queue it.
160
                    \core\task\manager::queue_adhoc_task($task);
161
                } 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...
162
                    mtrace("Error while enqueuing completion_uopdate_state task. " . (string)$e);
163
                }
164
            }
165
        }
166
    }
167
168
    /**
169
     * Sends notification to a user.
170
     *
171
     * @param object $sender
172
     * @param object $receiver
173
     * @param object $htmlmsg
174
     * @return void
175
     */
176
    public static function send_notification($sender, $receiver, $htmlmsg) {
177
        // Send the message.
178
        message_post_message($sender, $receiver, $htmlmsg, FORMAT_HTML);
179
    }
180
181
    /**
182
     * Define users to be notified.
183
     *
184
     * @param object $courseid
185
     * @return array
186
     */
187
    public static function receivers($courseid) {
188
        $context = \context_course::instance($courseid);
189
        $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...
190
        // See if there are any users in the lesson.
191
        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...
192
        $users = get_enrolled_users($context, 'mod/bigbluebuttonbn:view', 0, 'u.*', $sort);
193
        return $users;
194
    }
195
}
196