Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
exports_page
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 export_for_template
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
1 / 1
9
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_finalgrades\output;
18
19use local_finalgrades\local\exporter;
20use renderer_base;
21use stdClass;
22
23/**
24 * The registrar's page: what waits to be sent, per course, and the past export files.
25 *
26 * @package    local_finalgrades
27 * @copyright  2026 DevLMS <contact@devlms.com>
28 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30class exports_page implements \renderable, \templatable {
31    /** How many past exports to list. */
32    public const LIMIT = 50;
33
34    /**
35     * Data for the template.
36     *
37     * @param renderer_base $output
38     * @return stdClass
39     */
40    public function export_for_template(renderer_base $output): stdClass {
41        global $DB;
42        $pending = exporter::lines(exporter::pending());
43        $bycourse = [];
44        foreach ($pending as $row) {
45            $bycourse[$row->courseid] ??= ['new' => 0, 'change' => 0, 'withdrawn' => 0];
46            $bycourse[$row->courseid][$row->action]++;
47        }
48        $courses = $DB->get_records_list('course', 'id', array_keys($bycourse), 'fullname', 'id, fullname, idnumber');
49        $pendingrows = [];
50        foreach ($courses as $course) {
51            $pendingrows[] = [
52                'course' => format_string($course->fullname),
53                'idnumber' => $course->idnumber,
54                'url' => (new \moodle_url('/local/finalgrades/index.php', ['id' => $course->id]))->out(false),
55            ] + $bycourse[$course->id];
56        }
57
58        $exports = [];
59        $records = $DB->get_records('local_finalgrades_export', null, 'id DESC', '*', 0, self::LIMIT);
60        $makers = $DB->get_records_list(
61            'user',
62            'id',
63            array_filter(array_column($records, 'createdby')),
64            '',
65            'id, ' . implode(', ', \core_user\fields::get_name_fields())
66        );
67        $system = \context_system::instance();
68        foreach ($records as $export) {
69            $exports[] = [
70                'filename' => $export->filename,
71                'rowcount' => (int) $export->rowcount,
72                'time' => userdate($export->timecreated, get_string('strftimedatetimeshort', 'langconfig')),
73                'by' => isset($makers[$export->createdby])
74                    ? fullname($makers[$export->createdby]) : get_string('bytask', 'local_finalgrades'),
75                'url' => \moodle_url::make_pluginfile_url(
76                    $system->id,
77                    'local_finalgrades',
78                    exporter::FILEAREA,
79                    $export->id,
80                    '/',
81                    $export->filename,
82                    true
83                )->out(false),
84            ];
85        }
86
87        $task = \core\task\manager::get_scheduled_task(\local_finalgrades\task\export_grades::class);
88        $next = $task && !$task->get_disabled() ? $task->get_next_run_time() : 0;
89        $folder = (string) get_config('local_finalgrades', 'dropfolder');
90        return (object) [
91            'pendingcount' => count($pending),
92            'pending' => $pendingrows,
93            'haspending' => (bool) $pendingrows,
94            'exports' => $exports,
95            'hasexports' => (bool) $exports,
96            'pendinglabel' => get_string('pendingcount', 'local_finalgrades', count($pending)),
97            'nextrun' => $next
98                ? get_string('nextrun', 'local_finalgrades', userdate($next, get_string('strftimedatetimeshort', 'langconfig')))
99                : '',
100            'requireapproval' => (bool) get_config('local_finalgrades', 'requireapproval'),
101            'dropfolder' => $folder === '' ? '' : get_string('dropfolderis', 'local_finalgrades', $folder),
102            'actionurl' => (new \moodle_url('/local/finalgrades/exports.php'))->out(false),
103            'sesskey' => sesskey(),
104        ];
105    }
106}