-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.php
232 lines (202 loc) · 6.9 KB
/
lib.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?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/>.
/**
* Library of interface functions and constants.
*
* @package mod_jupyter
* @copyright KIB3 StuPro SS2022 Development Team of the University of Stuttgart
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_jupyter\gradeservice;
/**
* Return if the plugin supports $feature.
*
* @param string $feature Constant representing the feature.
* @return true | null True if the feature is supported, null otherwise.
*/
function jupyter_supports($feature) {
switch ($feature) {
case FEATURE_MOD_INTRO:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
default:
return null;
}
}
/**
* Saves a new instance of the mod_jupyter into the database.
*
* Given an object containing all the necessary data, (defined by the form
* in mod_form.php) this function will create a new instance and return the id
* number of the instance.
*
* @param stdClass $data An object from the form.
* @return int The id of the newly inserted record.
*/
function jupyter_add_instance(stdClass $data): int {
global $DB;
$data->timecreated = time();
$data->timemodified = $data->timecreated;
$cmid = $data->coursemodule;
if (!$data->autograded) {
$data->notebook_ready = true;
}
$data->id = $DB->insert_record('jupyter', $data);
$DB->set_field('course_modules', 'instance', $data->id, ['id' => $cmid]);
jupyter_set_mainfile($data);
return $data->id;
}
/**
* Updates an instance of the mod_jupyter in the database.
*
* Given an object containing all the necessary data (defined in mod_form.php),
* this function will update an existing instance with new data.
*
* @param stdClass $data An object from the form in mod_form.php.
* @return bool True if successful, false otherwise.
*/
function jupyter_update_instance(stdClass $data) {
global $DB;
$data->timemodified = time();
$data->id = $data->instance;
jupyter_set_mainfile($data);
return $DB->update_record('jupyter', $data);
}
/**
* Removes an instance of the mod_jupyter from the database.
*
* @param int $id Id of the module instance.
* @return bool True if successful, false on failure.
*/
function jupyter_delete_instance(int $id) {
global $DB;
$jupyter = $DB->get_record('jupyter', ['id' => $id]);
if (!$jupyter) {
return false;
}
try {
if ($jupyter->autograded) {
gradeservice::delete_assignment($jupyter);
}
} catch (Exception $e) {
debugging(json_encode($e, JSON_PRETTY_PRINT));
debugging("Error while deleting autograder instance. "
. "You may need to delete this grade instance manually. Instance to Delete: "
. json_encode($jupyter, JSON_PRETTY_PRINT));
}
$DB->delete_records('jupyter', ['id' => $id]);
$DB->delete_records('jupyter_grades', ['jupyter' => $id]);
$DB->delete_records('jupyter_questions', ['jupyter' => $id]);
$DB->delete_records('jupyter_questions_points', ['jupyter' => $id]);
return true;
}
/**
* Saves draft files as the activity package.
*
* @param stdClass $data an object from the form
*/
function jupyter_set_mainfile(stdClass $data): void {
global $DB;
$cmid = $data->coursemodule;
$context = context_module::instance($cmid);
if (!empty($data->packagefile)) {
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_jupyter', 'package');
file_save_draft_area_files(
$data->packagefile,
$context->id,
'mod_jupyter',
'package',
0,
['subdirs' => 0, 'maxfiles' => 1]
);
$files = $fs->get_area_files($context->id, 'mod_jupyter', 'package', 0, 'id', false);
$file = reset($files);
$data->notebook_filename = $file->get_filename();
$DB->update_record('jupyter', $data);
}
}
/**
* Creates or updates grade item for the given mod_jupyter instance.
*
* Needed by {@see grade_update_mod_grades()}.
*
* @param stdClass $jupyter Instance object with extra cmidnumber and modname property.
* @param bool $reset Reset grades in the gradebook.
* @return void.
*/
function jupyter_grade_item_update($jupyter, $reset = false) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
$item = [];
$item['itemname'] = clean_param($jupyter->name, PARAM_NOTAGS);
$item['gradetype'] = GRADE_TYPE_VALUE;
if ($jupyter->grade > 0) {
$item['gradetype'] = GRADE_TYPE_VALUE;
$item['grademax'] = $jupyter->grade;
$item['grademin'] = 0;
} else if ($jupyter->grade < 0) {
$item['gradetype'] = GRADE_TYPE_SCALE;
$item['scaleid'] = -$jupyter->grade;
} else {
$item['gradetype'] = GRADE_TYPE_NONE;
}
if ($reset) {
$item['reset'] = true;
}
grade_update('/mod/jupyter', $jupyter->course, 'mod', 'jupyter', $jupyter->id, 0, null, $item);
}
/**
* Delete grade item for given mod_jupyter instance.
*
* @param stdClass $jupyter Instance object.
* @return grade_item.
*/
function jupyter_grade_item_delete($jupyter) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
return grade_update(
'/mod/jupyter',
$jupyter->course,
'mod',
'mod_jupyter',
$jupyter->id,
0,
null,
['deleted' => 1]
);
}
/**
* Update mod_jupyter grades in the gradebook.
*
* Needed by {@see grade_update_mod_grades()}.
*
* @param stdClass $jupyter Instance object with extra cmidnumber and modname property.
* @param int $userid Update grade of specific user only, 0 means all participants.
* @param bool $nullifnone If a single user is specified and $nullifnone is true a grade item with a null rawgrade will be inserted
*/
function jupyter_update_grades($jupyter, $userid = 0, $nullifnone = true) {
global $CFG, $DB;
require_once($CFG->libdir . '/gradelib.php');
// Populate array of grade objects indexed by userid.
$grades = $DB->get_records('jupyter_grades', ['userid' => $userid]);
grade_update('/mod/jupyter', $jupyter->course, 'mod', 'jupyter', $jupyter->id, 0, $grades);
}