Completed
Pull Request — master (#143)
by Jesus
02:36
created

notifier   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A notification_process() 0 20 2
A notification_msg_html() 0 23 1
A notification_send() 0 18 3
A users_to_notify() 0 8 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
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
     * Starts the notification process.
41
     *
42
     * @param object $context
43
     * @param object $bigbluebuttonbn
44
     * @param string $action
45
     * @return void
46
     */
47
    public static function notification_process($context, $bigbluebuttonbn, $action) {
48
        global $USER;
49
        // Prepare message.
50
        $msg = (object) array();
51
        // Build the message_body.
52
        $msg->action = $action;
53
        $msg->activity_type = '';
54
        $msg->activity_title = $bigbluebuttonbn->name;
55
        // Add the meeting details to the message_body.
56
        $msg->action = ucfirst($action);
57
        $msg->activity_description = '';
58
        if (!empty($bigbluebuttonbn->intro)) {
59
            $msg->activity_description = trim($bigbluebuttonbn->intro);
60
        }
61
        $msg->activity_openingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->openingtime);
62
        $msg->activity_closingtime = bigbluebuttonbn_format_activity_time($bigbluebuttonbn->closingtime);
63
        $msg->activity_owner = fullname($USER);
64
        // Send notification to all users enrolled.
65
        self::notification_send($context, $USER, $bigbluebuttonbn, self::notification_msg_html($msg));
66
    }
67
68
    /**
69
     * Prepares html message body.
70
     *
71
     * @param object $msg
72
     * @return string
73
     */
74
    public static function notification_msg_html($msg) {
75
        $messagetext = '<p>'.$msg->activity_type.' "'.$msg->activity_title.'" '.
76
            get_string('email_body_notification_meeting_has_been', 'bigbluebuttonbn').' '.$msg->action.'.</p>'."\n";
77
        $messagetext .= '<p><b>'.$msg->activity_title.'</b> '.
78
            get_string('email_body_notification_meeting_details', 'bigbluebuttonbn').':'."\n";
79
        $messagetext .= '<table border="0" style="margin: 5px 0 0 20px"><tbody>'."\n";
80
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
81
            get_string('email_body_notification_meeting_title', 'bigbluebuttonbn').': </td><td>'."\n";
82
        $messagetext .= $msg->activity_title.'</td></tr>'."\n";
83
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
84
            get_string('email_body_notification_meeting_description', 'bigbluebuttonbn').': </td><td>'."\n";
85
        $messagetext .= $msg->activity_description.'</td></tr>'."\n";
86
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
87
            get_string('email_body_notification_meeting_start_date', 'bigbluebuttonbn').': </td><td>'."\n";
88
        $messagetext .= $msg->activity_openingtime.'</td></tr>'."\n";
89
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.
90
            get_string('email_body_notification_meeting_end_date', 'bigbluebuttonbn').': </td><td>'."\n";
91
        $messagetext .= $msg->activity_closingtime.'</td></tr>'."\n";
92
        $messagetext .= '<tr><td style="font-weight:bold;color:#555;">'.$msg->action.' '.
93
            get_string('email_body_notification_meeting_by', 'bigbluebuttonbn').': </td><td>'."\n";
94
        $messagetext .= $msg->activity_owner.'</td></tr></tbody></table></p>'."\n";
95
        return $messagetext;
96
    }
97
98
    /**
99
     * Sends the message.
100
     *
101
     * @param object $context
102
     * @param object $sender
103
     * @param object $bigbluebuttonbn
104
     * @param string $message
105
     * @return void
106
     */
107
    public static function notification_send($context, $sender, $bigbluebuttonbn, $message = '') {
108
        global $DB;
109
        $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
110
        // Complete message.
111
        $msg = (object) array();
112
        $msg->user_name = fullname($sender);
113
        $msg->user_email = $sender->email;
114
        $msg->course_name = $course->fullname;
115
        $message .= '<p><hr/><br/>'.get_string('email_footer_sent_by', 'bigbluebuttonbn').' '.
116
            $msg->user_name.'('.$msg->user_email.') ';
117
        $message .= get_string('email_footer_sent_from', 'bigbluebuttonbn').' '.$msg->course_name.'.</p>';
118
        // Process the message sending.
119
        foreach (self::users_to_notify($context, $instance) as $user) {
0 ignored issues
show
Bug introduced by
The variable $instance does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
120
            if ($user->id != $sender->id) {
121
                message_post_message($sender, $user, $message, FORMAT_HTML);
122
            }
123
        }
124
    }
125
126
    /**
127
     * Define users to be notified.
128
     *
129
     * @param object $context
130
     * @param object $course
131
     * @return array
132
     */
133
    public static function users_to_notify($context, $course) {
134
        $users = (array) get_enrolled_users($context, '', 0, 'u.*', null, 0, 0, true);
135
        $modinfo = get_fast_modinfo($course);
136
        $coursemodule = $modinfo->get_cm($context->instanceid);
137
        $info = new \core_availability\info_module($coursemodule);
138
        $filtered = $info->filter_user_list($users);
139
        return $filtered;
140
    }
141
}
142