|
| 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 | + * Helper class to handle notifications in Opencast Step |
| 19 | + * |
| 20 | + * @package lifecyclestep_opencast |
| 21 | + * @copyright 2023 Farbod Zamani Boroujeni, elan e.V. |
| 22 | + * @author Farbod Zamani Boroujeni <zamani@elan-ev.de> |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 24 | + */ |
| 25 | + |
| 26 | +namespace lifecyclestep_opencast; |
| 27 | + |
| 28 | +/** |
| 29 | + * Helper class to handle notifications in Opencast Step |
| 30 | + */ |
| 31 | +class notification_helper { |
| 32 | + /** @var bool $notificationenabled Whether notifying feature is On. */ |
| 33 | + private bool $notificationenabled = true; |
| 34 | + /** |
| 35 | + * Constructor function. |
| 36 | + * @param bool $notificationenabled A flag to make sure that the settings is enabled. |
| 37 | + */ |
| 38 | + public function __construct(bool $notificationenabled) { |
| 39 | + $this->notificationenabled = $notificationenabled; |
| 40 | + } |
| 41 | + /** |
| 42 | + * Notifies admins when a course is processed. |
| 43 | + * |
| 44 | + * @param stdClass $course the course object. |
| 45 | + * @param string $ocworkflow the workflow. |
| 46 | + */ |
| 47 | + public function notify_course_processed($course, $ocworkflow) { |
| 48 | + if (!$this->notificationenabled) { |
| 49 | + return; |
| 50 | + } |
| 51 | + $coursefullname = get_string('coursefullnameunknown', 'lifecyclestep_opencast'); |
| 52 | + if ($course->fullname) { |
| 53 | + $coursefullname = $course->fullname; |
| 54 | + } |
| 55 | + $a = (object)[ |
| 56 | + 'courseid' => $course->id, |
| 57 | + 'coursefullname' => $coursefullname, |
| 58 | + 'ocworkflow' => $ocworkflow, |
| 59 | + ]; |
| 60 | + |
| 61 | + $subject = get_string('notifycourseprocessed_subj', 'lifecyclestep_opencast'); |
| 62 | + $body = get_string('notifycourseprocessed_body', 'lifecyclestep_opencast', $a); |
| 63 | + |
| 64 | + $admin = get_admin(); |
| 65 | + $this->send_message('error', $admin, $subject, $body); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Notifies admins upon failied workflow start on an event. |
| 70 | + * |
| 71 | + * @param stdClass $course the course object. |
| 72 | + * @param int $ocinstanceid the opencast instance id. |
| 73 | + * @param stdClass $video the video object. |
| 74 | + * @param string $ocworkflow the workflow. |
| 75 | + */ |
| 76 | + public function notify_failed_workflow($course, $ocinstanceid, $video, $ocworkflow) { |
| 77 | + if (!$this->notificationenabled) { |
| 78 | + return; |
| 79 | + } |
| 80 | + $coursefullname = get_string('coursefullnameunknown', 'lifecyclestep_opencast'); |
| 81 | + if ($course->fullname) { |
| 82 | + $coursefullname = $course->fullname; |
| 83 | + } |
| 84 | + $a = (object)[ |
| 85 | + 'courseid' => $course->id, |
| 86 | + 'coursefullname' => $coursefullname, |
| 87 | + 'ocworkflow' => $ocworkflow, |
| 88 | + 'videotitle' => $video->title, |
| 89 | + 'videoidentifier' => $video->identifier, |
| 90 | + 'ocinstanceid' => $ocinstanceid, |
| 91 | + ]; |
| 92 | + |
| 93 | + $subject = get_string('errorfailedworkflow_subj', 'lifecyclestep_opencast'); |
| 94 | + $body = get_string('errorfailedworkflow_body', 'lifecyclestep_opencast', $a); |
| 95 | + |
| 96 | + $admin = get_admin(); |
| 97 | + $this->send_message('error', $admin, $subject, $body); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Notifies admins upon fatal error. |
| 102 | + * It is a complimentary function to use anywhere that fits, by default try catch in cron job is handled by moodle itself. |
| 103 | + * |
| 104 | + * @param stdClass $course the course object. |
| 105 | + * @param int $ocinstanceid the opencast instance id. |
| 106 | + * @param string $ocworkflow the workflow. |
| 107 | + * @param string $error error details. |
| 108 | + */ |
| 109 | + public function notify_error($course, $ocinstanceid, $ocworkflow, $error) { |
| 110 | + if (!$this->notificationenabled) { |
| 111 | + return; |
| 112 | + } |
| 113 | + $coursefullname = get_string('coursefullnameunknown', 'lifecyclestep_opencast'); |
| 114 | + if ($course->fullname) { |
| 115 | + $coursefullname = $course->fullname; |
| 116 | + } |
| 117 | + $a = (object)[ |
| 118 | + 'courseid' => $course->id, |
| 119 | + 'coursefullname' => $coursefullname, |
| 120 | + 'ocworkflow' => $ocworkflow, |
| 121 | + 'error' => $error, |
| 122 | + 'ocinstanceid' => $ocinstanceid, |
| 123 | + ]; |
| 124 | + |
| 125 | + $subject = get_string('errorexception_subj', 'lifecyclestep_opencast'); |
| 126 | + $body = get_string('errorexception_body', 'lifecyclestep_opencast', $a); |
| 127 | + |
| 128 | + $admin = get_admin(); |
| 129 | + $this->send_message('error', $admin, $subject, $body); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Notifies admins upon fatal error. |
| 134 | + * It is a complimentary function to use anywhere that fits, by default try catch in cron job is handled by moodle itself. |
| 135 | + * |
| 136 | + * @param stdClass $course the course object. |
| 137 | + * @param int $ocinstanceid the opencast instance id. |
| 138 | + * @param string $ocworkflow the workflow. |
| 139 | + */ |
| 140 | + public function notify_workflow_not_exists($course, $ocinstanceid, $ocworkflow) { |
| 141 | + if (!$this->notificationenabled) { |
| 142 | + return; |
| 143 | + } |
| 144 | + $coursefullname = get_string('coursefullnameunknown', 'lifecyclestep_opencast'); |
| 145 | + if ($course->fullname) { |
| 146 | + $coursefullname = $course->fullname; |
| 147 | + } |
| 148 | + $a = (object)[ |
| 149 | + 'courseid' => $course->id, |
| 150 | + 'coursefullname' => $coursefullname, |
| 151 | + 'ocworkflow' => $ocworkflow, |
| 152 | + 'ocinstanceid' => $ocinstanceid, |
| 153 | + ]; |
| 154 | + |
| 155 | + $subject = get_string('errorworkflownotexists_subj', 'lifecyclestep_opencast'); |
| 156 | + $body = get_string('errorworkflownotexists_body', 'lifecyclestep_opencast', $a); |
| 157 | + |
| 158 | + $admin = get_admin(); |
| 159 | + $this->send_message('error', $admin, $subject, $body); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sends moodle internal message. |
| 164 | + * |
| 165 | + * @param string $messagetype Message type |
| 166 | + * @param object $touser User to which notification is sent |
| 167 | + * @param string $subject Subject |
| 168 | + * @param string $body Body |
| 169 | + * @param string $format Format |
| 170 | + */ |
| 171 | + private function send_message($messagetype, $touser, $subject, $body, $format = FORMAT_PLAIN) { |
| 172 | + $message = new \core\message\message(); |
| 173 | + $message->courseid = SITEID; |
| 174 | + $message->component = 'block_opencast'; |
| 175 | + $message->name = $messagetype; |
| 176 | + $message->userfrom = \core_user::get_user(\core_user::NOREPLY_USER); |
| 177 | + $message->userto = $touser; |
| 178 | + $message->subject = $subject; |
| 179 | + $message->fullmessage = html_to_text($body); |
| 180 | + $message->fullmessageformat = $format; |
| 181 | + $message->fullmessagehtml = $body; |
| 182 | + $message->smallmessage = ''; |
| 183 | + $message->notification = 1; |
| 184 | + |
| 185 | + message_send($message); |
| 186 | + } |
| 187 | +} |
0 commit comments