Skip to content

Commit 459c38e

Browse files
committed
MBS-8820: User specific creation and archive download
1 parent 9c80c96 commit 459c38e

File tree

6 files changed

+314
-32
lines changed

6 files changed

+314
-32
lines changed

classes/Report.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,25 @@ public function has_access(string $wstoken): bool {
131131
/**
132132
* Get all attempts for all users inside this quiz, excluding previews
133133
*
134+
* @param int $userid - If set, only the attempts of the given user are included.
134135
* @return array Array of all attempt IDs together with the userid that were
135136
* made inside this quiz. Indexed by attemptid.
136137
*
137138
* @throws \dml_exception
138139
*/
139-
public function get_attempts(): array {
140+
public function get_attempts($userid = 0): array {
140141
global $DB;
141142

142-
return $DB->get_records_sql(
143-
"SELECT id AS attemptid, userid " .
144-
"FROM {quiz_attempts} " .
145-
"WHERE preview = 0 AND quiz = :quizid",
146-
[
147-
"quizid" => $this->quiz->id,
148-
]
149-
);
143+
$conditions = [
144+
'quiz' => $this->quiz->id,
145+
'preview' => 0,
146+
];
147+
148+
if(!empty($userid)) {
149+
$conditions['userid'] = $userid;
150+
}
151+
152+
return $DB->get_records('quiz_attempts', $conditions, '', 'id AS attemptid, userid');
150153
}
151154

152155
/**

classes/output/job_overview_table.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ class job_overview_table extends \table_sql {
4848
* @param int $courseid ID of the course
4949
* @param int $cmid ID of the course module
5050
* @param int $quizid ID of the quiz
51+
* @param int $userid - If set, the table is limited to the archives created by the user itself.
5152
*
5253
* @throws \coding_exception
5354
*/
54-
public function __construct(string $uniqueid, int $courseid, int $cmid, int $quizid) {
55+
public function __construct(string $uniqueid, int $courseid, int $cmid, int $quizid, int $userid = 0) {
5556
parent::__construct($uniqueid);
5657
$this->define_columns([
5758
'timecreated',
@@ -71,19 +72,22 @@ public function __construct(string $uniqueid, int $courseid, int $cmid, int $qui
7172
'',
7273
]);
7374

74-
$this->set_sql(
75-
'j.jobid, j.userid, j.timecreated, j.timemodified, j.status, j.statusextras, j.retentiontime, j.artifactfilechecksum, '.
76-
'f.pathnamehash, f.filesize, u.username',
77-
'{'.ArchiveJob::JOB_TABLE_NAME.'} j '.
78-
'JOIN {user} u ON j.userid = u.id '.
79-
'LEFT JOIN {files} f ON j.artifactfileid = f.id',
80-
'j.courseid = :courseid AND j.cmid = :cmid AND j.quizid = :quizid',
81-
[
82-
'courseid' => $courseid,
83-
'cmid' => $cmid,
84-
'quizid' => $quizid,
85-
]
86-
);
75+
$conditions = [
76+
'courseid' => $courseid,
77+
'cmid' => $cmid,
78+
'quizid' => $quizid,
79+
];
80+
81+
$fields = 'j.jobid, j.userid, j.timecreated, j.timemodified, j.status, j.statusextras, j.retentiontime, j.artifactfilechecksum, f.pathnamehash, f.filesize, u.username';
82+
$sql = '{' . ArchiveJob::JOB_TABLE_NAME . '} AS j JOIN {user} AS u ON j.userid = u.id LEFT JOIN {files} AS f ON j.artifactfileid = f.id';
83+
$where = 'j.courseid = :courseid AND j.cmid = :cmid AND j.quizid = :quizid';
84+
85+
if (!empty($userid)) {
86+
$conditions['userid'] = $userid;
87+
$where .= ' AND u.id = :userid';
88+
}
89+
90+
$this->set_sql($fields, $sql, $where, $conditions);
8791

8892
$this->sortable(true, 'timecreated', SORT_DESC);
8993
$this->no_sorting('jobid');

db/access.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,15 @@
6565
'contextlevel' => CONTEXT_SYSTEM,
6666
'archetypes' => [],
6767
],
68+
// Capability to use the webservice. Required for the webservice user.
69+
'mod/quiz_archiver:getownarchive' => [
70+
'riskbitmask' => (RISK_PERSONAL),
71+
'captype' => 'read',
72+
'contextlevel' => CONTEXT_SYSTEM,
73+
'archetypes' => [
74+
'student' => CAP_ALLOW,
75+
'editingteacher' => CAP_ALLOW,
76+
'manager' => CAP_ALLOW,
77+
],
78+
],
6879
];

lib.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@
4848
function quiz_archiver_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
4949
// Check permissions.
5050
require_login($course, false, $cm);
51-
require_capability('mod/quiz:grade', $context);
52-
require_capability('quiz/grading:viewstudentnames', $context);
53-
require_capability('quiz/grading:viewidnumber', $context);
51+
52+
if (!((
53+
has_capability('mod/quiz:grade', $context)
54+
&& has_capability('quiz/grading:viewstudentnames', $context)
55+
&& has_capability('quiz/grading:viewidnumber', $context)
56+
) ||
57+
has_capability('mod/quiz_archiver:getownarchive', $context)
58+
)) {
59+
throw new moodle_exception("You have not the capability to download the archive file.");
60+
}
5461

5562
// Validate course.
5663
if ($args[1] !== $course->id) {

0 commit comments

Comments
 (0)