Repository navigation
Expand file tree
/
Copy pathcourse.php
More file actions
167 lines (149 loc) · 6.49 KB
/
Copy pathcourse.php
File metadata and controls
167 lines (149 loc) · 6.49 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* A course's credential groups: which Hyperstack groups issue when a learner completes the course.
*
* @package local_hyperstack
* @copyright 2026 Hyperstack Inc.
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_hyperstack\api\api_exception;
use local_hyperstack\api\client;
use local_hyperstack\form\mapping_form;
use local_hyperstack\issuer;
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/completionlib.php');
$id = required_param('id', PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$course = get_course($id);
require_login($course);
$context = context_course::instance($course->id);
require_capability('local/hyperstack:manage', $context);
$url = new moodle_url('/local/hyperstack/course.php', ['id' => $course->id]);
$PAGE->set_url($url);
$PAGE->set_pagelayout('incourse');
$PAGE->set_title(get_string('coursenav', 'local_hyperstack') . moodle_page::TITLE_SEPARATOR . $course->shortname);
$PAGE->set_heading($course->fullname);
if ($action === 'pastcompleters' && confirm_sesskey()) {
$queued = issuer::queue_past_completers($course->id);
redirect($url, get_string('past:queued', 'local_hyperstack', $queued), null, \core\output\notification::NOTIFY_SUCCESS);
}
$configured = client::is_configured();
$apierror = null;
$form = null;
$mapped = $DB->get_records('local_hyperstack_map', ['courseid' => $course->id], 'grouptitle', 'groupkey, id, grouptitle');
if ($configured) {
try {
$groups = (new client())->groups();
} catch (api_exception $e) {
$groups = null;
$apierror = $e->get_reason();
}
if ($groups !== null) {
// Groups that are not archived, plus any mapped group, even archived or gone from Hyperstack.
$choices = [];
$titles = [];
foreach ($groups as $group) {
$key = clean_param($group['group_id'] ?? '', PARAM_ALPHANUMEXT);
if ($key === '' || (!empty($group['archived']) && !isset($mapped[$key]))) {
continue;
}
$label = format_string($group['title'] ?? $key);
if (!empty($group['group_code'])) {
$label .= ' (' . format_string($group['group_code']) . ')';
}
$choices[] = ['key' => $key, 'label' => $label, 'mapped' => isset($mapped[$key])];
$titles[$key] = clean_param($group['title'] ?? $key, PARAM_TEXT);
}
foreach ($mapped as $key => $map) {
if (!isset($titles[$key])) {
$label = get_string('mapping:gone', 'local_hyperstack', format_string($map->grouptitle));
$choices[] = ['key' => $key, 'label' => $label, 'mapped' => true];
$titles[$key] = $map->grouptitle;
}
}
$form = new mapping_form($url, ['choices' => $choices]);
if ($data = $form->get_data()) {
$ticked = $form->ticked($data);
$now = time();
foreach ($ticked as $key) {
if (!isset($mapped[$key])) {
$DB->insert_record('local_hyperstack_map', (object) [
'courseid' => $course->id,
'groupkey' => $key,
'grouptitle' => core_text::substr($titles[$key], 0, 255),
'timecreated' => $now,
'timemodified' => $now,
]);
}
}
foreach ($mapped as $key => $map) {
if (!in_array($key, $ticked)) {
$DB->delete_records('local_hyperstack_map', ['id' => $map->id]);
}
}
redirect($url, get_string('mapping:saved', 'local_hyperstack'), null, \core\output\notification::NOTIFY_SUCCESS);
}
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('coursenav', 'local_hyperstack'));
if (!$configured) {
$message = get_string('error:notconnected', 'local_hyperstack');
if (has_capability('moodle/site:config', context_system::instance())) {
$settingsurl = new moodle_url('/admin/settings.php', ['section' => 'local_hyperstack']);
$message .= ' ' . html_writer::link($settingsurl, get_string('settings:open', 'local_hyperstack'));
}
echo $OUTPUT->notification($message, 'warning');
echo $OUTPUT->footer();
die();
}
// Course completion triggers issuing; without it nothing is ever sent.
$completion = new completion_info($course);
if (!$completion->is_enabled()) {
echo $OUTPUT->notification(get_string('warn:completiondisabled', 'local_hyperstack'), 'warning');
} else if (!$completion->has_criteria()) {
$criteriaurl = new moodle_url('/course/completion.php', ['id' => $course->id]);
echo $OUTPUT->notification(get_string('warn:nocriteria', 'local_hyperstack', $criteriaurl->out()), 'warning');
}
if ($apierror !== null) {
echo $OUTPUT->notification(get_string('error:groups', 'local_hyperstack', s($apierror)), 'error');
}
if ($form) {
$form->display();
}
if ($mapped) {
$completed = $DB->count_records_select(
'course_completions',
'course = :course AND timecompleted IS NOT NULL',
['course' => $course->id]
);
echo $OUTPUT->heading(get_string('past:title', 'local_hyperstack'), 3);
echo html_writer::tag('p', get_string('past:help', 'local_hyperstack', $completed));
$button = new single_button(
new moodle_url($url, ['action' => 'pastcompleters']),
get_string('past:button', 'local_hyperstack'),
'post'
);
$button->disabled = $completed == 0;
echo html_writer::div($OUTPUT->render($button), 'mb-4');
}
echo $OUTPUT->heading(get_string('report:title', 'local_hyperstack'), 3);
echo html_writer::tag('p', html_writer::link(
new moodle_url('/local/hyperstack/report.php', ['id' => $course->id]),
get_string('report:open', 'local_hyperstack')
));
echo $OUTPUT->footer();