mirror of
https://github.com/moodle/moodle.git
synced 2025-04-23 09:23:09 +02:00
MDL-71797 quiz: Improve the checks when fetching user's attempts
This commit is contained in:
parent
fec5a40ddb
commit
af4fb733ee
mod/quiz
@ -413,10 +413,21 @@ class mod_quiz_external extends external_api {
|
||||
require_capability('mod/quiz:viewreports', $context);
|
||||
}
|
||||
|
||||
// Update quiz with override information.
|
||||
$quiz = quiz_update_effective_access($quiz, $params['userid']);
|
||||
$attempts = quiz_get_user_attempts($quiz->id, $user->id, $params['status'], $params['includepreviews']);
|
||||
|
||||
$attemptresponse = [];
|
||||
foreach ($attempts as $attempt) {
|
||||
$reviewoptions = quiz_get_review_options($quiz, $attempt, $context);
|
||||
if (!has_capability('mod/quiz:viewreports', $context) &&
|
||||
($reviewoptions->marks < question_display_options::MARK_AND_MAX || $attempt->state != quiz_attempt::FINISHED)) {
|
||||
// Blank the mark if the teacher does not allow it.
|
||||
$attempt->sumgrades = null;
|
||||
}
|
||||
$attemptresponse[] = $attempt;
|
||||
}
|
||||
$result = array();
|
||||
$result['attempts'] = $attempts;
|
||||
$result['attempts'] = $attemptresponse;
|
||||
$result['warnings'] = $warnings;
|
||||
return $result;
|
||||
}
|
||||
|
@ -110,16 +110,18 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
|
||||
* @param boolean $finishattempt whether to finish the new attempt
|
||||
* @param string $behaviour the quiz preferredbehaviour, defaults to 'deferredfeedback'.
|
||||
* @param boolean $includeqattachments whether to include a question that supports attachments, defaults to false.
|
||||
* @param array $extraoptions extra options for Quiz.
|
||||
* @return array array containing the quiz, context and the attempt
|
||||
*/
|
||||
private function create_quiz_with_questions($startattempt = false, $finishattempt = false, $behaviour = 'deferredfeedback',
|
||||
$includeqattachments = false) {
|
||||
$includeqattachments = false, $extraoptions = []) {
|
||||
|
||||
// Create a new quiz with attempts.
|
||||
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
||||
$data = array('course' => $this->course->id,
|
||||
'sumgrades' => 2,
|
||||
'preferredbehaviour' => $behaviour);
|
||||
$data = array_merge($data, $extraoptions);
|
||||
$quiz = $quizgenerator->create_instance($data);
|
||||
$context = context_module::instance($quiz->cmid);
|
||||
|
||||
@ -395,6 +397,8 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
|
||||
$this->assertEquals($quiz->id, $result['attempts'][0]['quiz']);
|
||||
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
||||
$this->assertEquals(1, $result['attempts'][0]['attempt']);
|
||||
$this->assertArrayHasKey('sumgrades', $result['attempts'][0]);
|
||||
$this->assertEquals(1.0, $result['attempts'][0]['sumgrades']);
|
||||
|
||||
// Test filters. Only finished.
|
||||
$result = mod_quiz_external::get_user_attempts($quiz->id, 0, 'finished', false);
|
||||
@ -460,6 +464,42 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_user_attempts with marks hidden
|
||||
*/
|
||||
public function test_get_user_attempts_with_marks_hidden() {
|
||||
// Create quiz with one attempt finished and hide the mark.
|
||||
list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(
|
||||
true, true, 'deferredfeedback', false,
|
||||
['marksduring' => 0, 'marksimmediately' => 0, 'marksopen' => 0, 'marksclosed' => 0]);
|
||||
|
||||
// Student cannot see the grades.
|
||||
$this->setUser($this->student);
|
||||
$result = mod_quiz_external::get_user_attempts($quiz->id);
|
||||
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
||||
|
||||
$this->assertCount(1, $result['attempts']);
|
||||
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
||||
$this->assertEquals($quiz->id, $result['attempts'][0]['quiz']);
|
||||
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
||||
$this->assertEquals(1, $result['attempts'][0]['attempt']);
|
||||
$this->assertArrayHasKey('sumgrades', $result['attempts'][0]);
|
||||
$this->assertEquals(null, $result['attempts'][0]['sumgrades']);
|
||||
|
||||
// Test manager can see user grades.
|
||||
$this->setUser($this->teacher);
|
||||
$result = mod_quiz_external::get_user_attempts($quiz->id, $this->student->id);
|
||||
$result = external_api::clean_returnvalue(mod_quiz_external::get_user_attempts_returns(), $result);
|
||||
|
||||
$this->assertCount(1, $result['attempts']);
|
||||
$this->assertEquals($attempt->id, $result['attempts'][0]['id']);
|
||||
$this->assertEquals($quiz->id, $result['attempts'][0]['quiz']);
|
||||
$this->assertEquals($this->student->id, $result['attempts'][0]['userid']);
|
||||
$this->assertEquals(1, $result['attempts'][0]['attempt']);
|
||||
$this->assertArrayHasKey('sumgrades', $result['attempts'][0]);
|
||||
$this->assertEquals(1.0, $result['attempts'][0]['sumgrades']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_user_best_grade
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user