Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
contacts
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 record
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 for_category
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
17namespace local_atrisk\local;
18
19/**
20 * Contacts with flagged students, and the list a category page shows.
21 *
22 * @package    local_atrisk
23 * @copyright  2026 DevLMS <contact@devlms.com>
24 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26class contacts {
27    /**
28     * Record that the current user contacted a student about a course.
29     *
30     * @param int $userid the student
31     * @param int $courseid
32     * @param string $note what was said or agreed
33     * @return int the new record id
34     */
35    public static function record(int $userid, int $courseid, string $note): int {
36        global $DB, $USER;
37        $id = $DB->insert_record('local_atrisk_contact', [
38            'userid' => $userid,
39            'courseid' => $courseid,
40            'contactedby' => $USER->id,
41            'note' => trim($note),
42            'timecreated' => time(),
43        ]);
44        \local_atrisk\event\student_contacted::create([
45            'context' => \context_course::instance($courseid),
46            'objectid' => $id,
47            'relateduserid' => $userid,
48        ])->trigger();
49        return $id;
50    }
51
52    /**
53     * Flagged students in courses of a category and its subcategories, with the latest contact.
54     *
55     * @param int $categoryid
56     * @return array of rows: flag fields, user name fields, coursename, lastnote, lastcontactedby, lastcontacted
57     */
58    public static function for_category(int $categoryid): array {
59        global $DB;
60        $cat = \core_course_category::get($categoryid, MUST_EXIST, true);
61        $path = $DB->get_field('course_categories', 'path', ['id' => $cat->id]);
62        $names = \core_user\fields::for_name()->get_sql('u', false, '', '', false);
63        $params = ['catid' => $cat->id, 'path' => $DB->sql_like_escape($path) . '/%'] + $names->params;
64        $sql = "SELECT f.id, f.userid, f.courseid, f.inactive, f.missed, f.grade,
65                       c.fullname AS coursename, c.category, {$names->selects},
66                       lc.note AS lastnote, lc.contactedby AS lastcontactedby, lc.timecreated AS lastcontacted
67                  FROM {local_atrisk_flag} f
68                  JOIN {course} c ON c.id = f.courseid
69                  JOIN {course_categories} cc ON cc.id = c.category
70                  JOIN {user} u ON u.id = f.userid
71             LEFT JOIN (SELECT userid, courseid, MAX(id) AS maxid
72                          FROM {local_atrisk_contact}
73                      GROUP BY userid, courseid) latest ON latest.userid = f.userid AND latest.courseid = f.courseid
74             LEFT JOIN {local_atrisk_contact} lc ON lc.id = latest.maxid
75                 WHERE (cc.id = :catid OR " . $DB->sql_like('cc.path', ':path') . ")
76              ORDER BY lc.timecreated IS NOT NULL, f.missed DESC, f.inactive DESC, u.lastname, u.firstname, c.fullname";
77        return $DB->get_records_sql($sql, $params);
78    }
79}