|
| 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. |
| 19 | + * |
| 20 | + * @package assignsubmission_maharaws |
| 21 | + * @author 2025 Sarah Cotton <sarah.cotton@catalyst-au.net> |
| 22 | + * @copyright Catalyst IT, 2025 |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 24 | + * */ |
| 25 | + |
| 26 | +namespace assignsubmission_maharaws; |
| 27 | + |
| 28 | +use stdClass; |
| 29 | + |
| 30 | +/** |
| 31 | + * Helper class. |
| 32 | + */ |
| 33 | +class helper { |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the Mahara group mapping record. |
| 37 | + * |
| 38 | + * @param int $moodlegroup The Moodle group id. |
| 39 | + * @return mixed a fieldset object containing the first matching record or false. |
| 40 | + */ |
| 41 | + public static function get_mahara_group(int $moodlegroup): mixed { |
| 42 | + global $DB; |
| 43 | + return $DB->get_record('assignsubmission_maharawsgroup', ['moodlegroup' => $moodlegroup]); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Create Mahara groups against the configured Mahara institution. |
| 48 | + * |
| 49 | + * @param stdClass $data The form data. |
| 50 | + */ |
| 51 | + public static function create_mahara_groups(stdClass $data): void { |
| 52 | + global $DB, $USER; |
| 53 | + |
| 54 | + $groupids = $data->assignsubmission_maharaws_groups; |
| 55 | + if ($groupids) { |
| 56 | + $creategroups = []; |
| 57 | + $creategroupserrors = []; |
| 58 | + foreach ($groupids as $groupid) { |
| 59 | + $group = groups_get_group($groupid); |
| 60 | + |
| 61 | + // Has the selected group already been created/mapped to a Mahara group? |
| 62 | + $maharagroup = self::get_mahara_group($group->id); |
| 63 | + // If not, schedule an adhoc task to try and create the group. |
| 64 | + if (!$maharagroup) { |
| 65 | + try { |
| 66 | + $task = \assignsubmission_maharaws\task\create_group::instance($group->courseid, $group->id, $USER->id); |
| 67 | + \core\task\manager::queue_adhoc_task($task, true); |
| 68 | + $creategroups[] = $group->name; |
| 69 | + |
| 70 | + if (!$DB->get_record('assignsubmission_maharawsgroup', ['moodlegroup' => $group->id])) { |
| 71 | + // Store Moodle group id and institution. |
| 72 | + $obj = new stdClass(); |
| 73 | + $obj->moodlegroup = $group->id; |
| 74 | + $obj->institution = $data->institution; |
| 75 | + $DB->insert_record('assignsubmission_maharawsgroup', $obj); |
| 76 | + } |
| 77 | + } catch (\Exception $e) { |
| 78 | + $creategroupserrors[] = $e->getMessage(); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (!empty($creategroups)) { |
| 84 | + // Let the user know the groups have been queued for creation. |
| 85 | + \core\notification::add(get_string('groups:createdgroups', 'assignsubmission_maharaws', |
| 86 | + implode(', ', $creategroups)), \core\output\notification::NOTIFY_SUCCESS); |
| 87 | + } |
| 88 | + |
| 89 | + if (!empty($creategroupserrors)) { |
| 90 | + // Let the user know of any errors. |
| 91 | + \core\notification::add(get_string('groups:createdgroups:errors', 'assignsubmission_maharaws', |
| 92 | + implode(', ', $creategroupserrors)), \core\output\notification::NOTIFY_ERROR); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Send error notification for sync job failures. |
| 99 | + * |
| 100 | + * @param int $userid User to send the notification to. |
| 101 | + * @param stdClass $course Course the notification is associated with. |
| 102 | + * @param string $messagebody The message. |
| 103 | + */ |
| 104 | + public static function send_notification(int $userid, stdClass $course, string $taskname, string $messagebody) { |
| 105 | + global $DB; |
| 106 | + |
| 107 | + $message = new \core\message\message(); |
| 108 | + $message->component = 'assignsubmission_maharaws'; |
| 109 | + $message->name = $taskname; |
| 110 | + $message->userfrom = \core_user::get_noreply_user(); |
| 111 | + $message->userto = $userid; |
| 112 | + $message->subject = get_string('messagesubject:'. $taskname, 'assignsubmission_maharaws', $course->fullname); |
| 113 | + $message->fullmessage = $messagebody; |
| 114 | + $message->fullmessageformat = FORMAT_MARKDOWN; |
| 115 | + $message->fullmessagehtml = $messagebody; |
| 116 | + $message->smallmessage = $messagebody; |
| 117 | + $message->notification = 1; |
| 118 | + $message->contexturl = (new \moodle_url('/course/view.php', ['id' => $course->id]))->out(false); |
| 119 | + $message->contexturlname = 'Course ' . $course->fullname; |
| 120 | + |
| 121 | + // Extra content for specific processor. |
| 122 | + $content = [ |
| 123 | + '*' => [ |
| 124 | + 'footer' => '<p>Link to course: ' . $message->contexturl . '</p>', |
| 125 | + ], |
| 126 | + ]; |
| 127 | + $message->set_additional_content('email', $content); |
| 128 | + message_send($message); |
| 129 | + |
| 130 | + // Additionally send the notification to admins. |
| 131 | + $admins = get_config('assignsubmission_maharaws', 'errornotifications'); |
| 132 | + if ($admins !== '') { |
| 133 | + $admins = explode(',', $admins); |
| 134 | + foreach ($admins as $admin) { |
| 135 | + $userid = $DB->get_field('user', 'id', ['username' => trim($admin)]); |
| 136 | + $message->userto = $userid; |
| 137 | + message_send($message); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments