-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
135 lines (115 loc) · 5.37 KB
/
Copy pathindex.php
File metadata and controls
135 lines (115 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* SpaceIt overview / stats page (spec §12, surface 2).
*
* @package local_spaceit
* @copyright 2026 Fynn Scharpey
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
use local_spaceit\fsrs\scheduler;
use local_spaceit\output\overview;
use local_spaceit\repository\review_log_repository;
use local_spaceit\repository\state_repository;
use local_spaceit\service\relevance_service;
use local_spaceit\stats\punctuality;
use local_spaceit\stats\review_history;
require_login();
$context = context_system::instance();
require_capability('local/spaceit:view', $context);
$pageurl = new moodle_url('/local/spaceit/index.php');
$PAGE->set_context($context);
$PAGE->set_url($pageurl);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('overview:title', 'local_spaceit'));
$PAGE->set_heading(get_string('overview:title', 'local_spaceit'));
$states = new state_repository();
// Handle a relevance change.
if (optional_param('action', '', PARAM_ALPHA) === 'setrelevance' && confirm_sesskey()) {
require_capability('local/spaceit:manageown', $context);
$stateid = required_param('stateid', PARAM_INT);
$relevance = required_param('relevance', PARAM_INT);
$record = $states->get_by_id($stateid);
if ($record !== null && (int) $record->userid === (int) $USER->id) {
(new relevance_service())->set_relevance($record, $relevance);
}
redirect($pageurl, get_string('relevance:saved', 'local_spaceit'), null, \core\output\notification::NOTIFY_SUCCESS);
}
// Sorting.
$sort = optional_param('tsort', 'due', PARAM_ALPHA);
if (!in_array($sort, overview::sortable_columns(), true)) {
$sort = 'due';
}
$asc = optional_param('tdir', 'asc', PARAM_ALPHA) !== 'desc';
$now = time();
$scheduler = new scheduler();
$useritems = $states->get_active_for_user((int) $USER->id);
echo $OUTPUT->header();
// History panels: completed reviews per day, and punctuality KPIs per period.
$midnight = usergetmidnight($now);
$allreviews = (new review_log_repository())->get_for_user((int) $USER->id);
// Completed reviews per day across the past year (today is the rightmost bar).
// The bars scroll horizontally so roughly a month shows at once while a full
// year can be scrolled; the Y axis and the data toggle stay fixed to the left.
$historydays = 365;
$pxperday = 34;
$historystart = $midnight - ($historydays - 1) * DAYSECS;
$historycounts = review_history::per_day($allreviews, $historystart, $historydays);
$historychart = new \local_spaceit\output\review_history_chart($historycounts, $historystart, $pxperday);
$chartstrip = $OUTPUT->render_from_template(
'local_spaceit/review_history_chart', $historychart->export_for_template($OUTPUT));
// Open the strip on the most recent days and wire the data toggle.
$PAGE->requires->js_call_amd('local_spaceit/review_chart', 'init');
// Punctuality KPIs (this month / semester / all time).
$punctualityrows = array_map(static function (array $row): array {
return [
'label' => get_string('punctuality:period_' . $row['key'], 'local_spaceit'),
'total' => $row['total'],
'haspunctuality' => $row['haspunctuality'],
'average' => $row['average'],
'worst' => $row['worstdays'] === null
? null
: get_string(
$row['worstdays'] === 1 ? 'punctuality:worstday' : 'punctuality:worstdays',
'local_spaceit',
$row['worstdays']),
];
}, punctuality::summary($allreviews, $now));
$punctualitypanel = $OUTPUT->render_from_template('local_spaceit/punctuality_panel', [
'rows' => $punctualityrows,
'hasany' => $allreviews !== [],
'emptymessage' => get_string('punctuality:empty', 'local_spaceit'),
]);
// Render a panel inside a card with its title as the header.
$panelcard = static function (string $title, string $body): string {
return html_writer::div(
html_writer::div(html_writer::tag('h2', $title, ['class' => 'h6 m-0']), 'card-header') .
html_writer::div($body, 'card-body'),
'card',
);
};
// Full-width chart on top, punctuality KPIs stacked underneath.
echo html_writer::div(
$panelcard(get_string('chart:finishedreviews', 'local_spaceit'), $chartstrip), 'mb-3');
echo html_writer::div(
$panelcard(get_string('punctuality:heading', 'local_spaceit'), $punctualitypanel), 'mb-4');
// Table.
echo html_writer::tag('h2', get_string('overview:itemsheading', 'local_spaceit'), ['class' => 'h5 mb-2']);
$canmanage = has_capability('local/spaceit:manageown', $context);
$table = new overview($useritems, $scheduler, $now, $sort, $asc, $pageurl, $canmanage);
echo $OUTPUT->render_from_template('local_spaceit/overview', $table->export_for_template($OUTPUT));
echo $OUTPUT->footer();