Completed
Push — master ( 2cf02e...c7d8b1 )
by Jesus
33:46 queued 27:08
created

notifier::enqueue_notifications()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 20
rs 9.8666
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";
0 ignored issues
show
Bug introduced by
The function get_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            /** @scrutinizer ignore-call */ 
49
            get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>'."\n";
Loading history...
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);
0 ignored issues
show
Bug introduced by
The type course_modinfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
The function fullname was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $msg->activity_owner = /** @scrutinizer ignore-call */ fullname($sender);
Loading history...
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()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        self::/** @scrutinizer ignore-call */ 
107
              send_notifications($bigbluebuttonbn, $sender, self::htmlmsg_instance_updated($msg));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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').
0 ignored issues
show
Bug introduced by
The function get_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        return '<p>'./** @scrutinizer ignore-call */ get_string('email_body_recording_ready_for', 'bigbluebuttonbn').
Loading history...
Bug Best Practice introduced by
The expression return '<p>' . get_strin...luebuttonbn') . '.</p>' returns the type string which is incompatible with the documented return type void.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The function get_admin was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        $sender = /** @scrutinizer ignore-call */ get_admin();
Loading history...
131
        $htmlmsg = self::htmlmsg_recording_ready($bigbluebuttonbn);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $htmlmsg is correct as self::htmlmsg_recording_ready($bigbluebuttonbn) targeting mod_bigbluebuttonbn\loca...mlmsg_recording_ready() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132
        self::send_notifications($bigbluebuttonbn, $sender, $htmlmsg);
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);
0 ignored issues
show
Bug introduced by
The type core\task\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
161
                } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type mod_bigbluebuttonbn\locallib\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
162
                    mtrace("Error while enqueuing completion_uopdate_state task. " . (string)$e);
0 ignored issues
show
Bug introduced by
The function mtrace was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
                    /** @scrutinizer ignore-call */ 
163
                    mtrace("Error while enqueuing completion_uopdate_state task. " . (string)$e);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function message_post_message was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
        /** @scrutinizer ignore-call */ 
179
        message_post_message($sender, $receiver, $htmlmsg, FORMAT_HTML);
Loading history...
Bug introduced by
The constant mod_bigbluebuttonbn\locallib\FORMAT_HTML was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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);
0 ignored issues
show
Bug introduced by
The type context_course was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
189
        $users = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
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
Bug introduced by
The function users_order_by_sql was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
        list($sort, $params) = /** @scrutinizer ignore-call */ users_order_by_sql('u');
Loading history...
192
        $users = get_enrolled_users($context, 'mod/bigbluebuttonbn:view', 0, 'u.*', $sort);
0 ignored issues
show
Bug introduced by
The function get_enrolled_users was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
        $users = /** @scrutinizer ignore-call */ get_enrolled_users($context, 'mod/bigbluebuttonbn:view', 0, 'u.*', $sort);
Loading history...
193
        return $users;
194
    }
195
}
196