Skip to content

Commit c7af871

Browse files
devindjdevin
devin
authored andcommittedAug 8, 2016
Issue #2487756 by captainack, djdevin: Quiz creators cannot view results for own quizzes
1 parent 91cb79c commit c7af871

File tree

5 files changed

+100
-27
lines changed

5 files changed

+100
-27
lines changed
 

‎includes/QuizResultAnswerController.class.inc

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ class QuizResultAnswerController extends EntityAPIController {
6565
$class = 'q-waiting';
6666
}
6767

68-
if ($instance->canReview('score') || quiz_access_to_score()) {
68+
$quiz_result = quiz_result_load($entity->result_id);
69+
70+
if ($instance->canReview('score') || quiz_access_to_score($quiz_result)) {
6971
$out['score']['#title'] = '';
7072
$out['score']['#markup'] = theme('quiz_question_score', array('score' => $score, 'max_score' => $instance->getMaxScore(), 'class' => $class));
7173
}
@@ -79,7 +81,6 @@ class QuizResultAnswerController extends EntityAPIController {
7981
if ($instance->canReview('question_feedback')) {
8082
if ($properties = entity_load('quiz_question', FALSE, array('nid' => $instance->quizQuestion->node->nid, 'vid' => $instance->quizQuestion->node->vid))) {
8183
$quiz_question = reset($properties);
82-
$quiz_result = quiz_result_load($entity->result_id);
8384
$account = user_load($quiz_result->uid);
8485
$token_types = array(
8586
'global' => NULL,

‎question_types/quiz_question/quiz_question.core.inc

+4-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,10 @@ abstract class QuizQuestionResponse {
718718
'#type' => 'value',
719719
'#value' => $this->display_number,
720720
);
721-
if (quiz_access_to_score()) {
721+
722+
$quiz_result = quiz_result_load($this->result_id);
723+
724+
if (quiz_access_to_score($quiz_result)) {
722725

723726
if ($submit = $this->getReportFormSubmit()) {
724727
$form['score'] = $this->getReportFormScore();

‎quiz.info

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ files[] = includes/QuizResultMetadataController.class.inc
2929
files[] = includes/views/handlers/quiz_views_handler_filter_quiz_question.inc
3030
files[] = includes/views/handlers/quiz_views_handler_filter_quiz_question_type.inc
3131
files[] = includes/views/handlers/quiz_views_handler_field_quiz_question_result_answer.inc
32+
files[] = tests/QuizAccessTestCase.test
3233
files[] = tests/QuizCreationTestCase.test
3334
files[] = tests/QuizFeedbackTestCase.test
3435
files[] = tests/QuizGradingTestCase.test

‎quiz.module

+7-24
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ function quiz_access_my_result($result_id) {
240240
if ($quiz_result = quiz_result_load($result_id)) {
241241
$node = node_load($quiz_result->nid, $quiz_result->vid);
242242

243-
if (quiz_access_to_score($node->uid) || ($quiz_result->time_end > 0 && $quiz_result->uid == $user->uid)) {
243+
if (quiz_access_to_score($quiz_result) || ($quiz_result->time_end > 0 && $quiz_result->uid == $user->uid)) {
244244
return TRUE;
245245
}
246246
}
@@ -254,18 +254,17 @@ function quiz_access_my_result($result_id) {
254254
* @param $quiz_creator
255255
* uid of the quiz creator.
256256
*/
257-
function quiz_access_to_score($quiz_creator = NULL) {
257+
function quiz_access_to_score(QuizResult $quiz_result) {
258258
global $user;
259-
if ($quiz_creator == NULL && ($quiz = quiz_get_quiz_from_menu())) {
260-
$quiz_creator = $quiz->uid;
261-
}
262-
if (user_access('score any quiz')) {
259+
$quiz = node_load($quiz_result->nid);
260+
261+
if (user_access('score own quiz') && $quiz->uid == $user->uid) {
263262
return TRUE;
264263
}
265-
if (user_access('score own quiz') && $user->uid == $quiz_creator) {
264+
if (user_access('score any quiz')) {
266265
return TRUE;
267266
}
268-
if (user_access('score taken quiz answer')) {
267+
if (user_access('score taken quiz answer') && $quiz_result->uid == $user->uid) {
269268
return TRUE;
270269
}
271270
}
@@ -3067,22 +3066,6 @@ function _quiz_get_quiz_name() {
30673066
return t(variable_get('quiz_name', 'Quiz'));
30683067
}
30693068

3070-
/**
3071-
* Retrieves the quiz node from the menu router.
3072-
*
3073-
* @return
3074-
* Quiz node, if found, or FALSE if quiz node can't be retrieved from the menu
3075-
* router.
3076-
*/
3077-
function quiz_get_quiz_from_menu() {
3078-
if ($to_return = menu_get_object('quiz_type_access', 4)) {
3079-
return $to_return;
3080-
}
3081-
//TODO: FIX it. This seems to return NULL in feedback page.
3082-
$node = menu_get_object();
3083-
return (is_object($node) && $node->type == 'quiz') ? $node : FALSE;
3084-
}
3085-
30863069
/**
30873070
* Finds out if a quiz has been answered or not.
30883071
*

‎tests/QuizAccessTestCase.test

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Unit tests for the quiz question Module.
6+
*/
7+
8+
/**
9+
* Test aspects of quiz access and permissions.
10+
*/
11+
class QuizAccessTestCase extends QuizTestCase {
12+
13+
function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
14+
$modules[] = 'short_answer';
15+
parent::setUp($modules, $admin_permissions, $user_permissions);
16+
}
17+
18+
public static function getInfo() {
19+
return array(
20+
'name' => t('Quiz access'),
21+
'description' => t('Unit test for Quiz access.'),
22+
'group' => t('Quiz'),
23+
);
24+
}
25+
26+
/**
27+
* Test quiz authors being able to score results for own quiz.
28+
*/
29+
public function testQuizOwnerResultEdit() {
30+
$grader = $this->drupalCreateUser(array('score own quiz', 'view results for own quiz'));
31+
32+
$question_node = $this->drupalCreateNode(array(
33+
'type' => 'short_answer',
34+
'title' => 'SA 1 title',
35+
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
36+
'correct_answer' => 'blue',
37+
'body' => array(LANGUAGE_NONE => array(array('value' => 'SA 1 body text'))),
38+
));
39+
$quiz_node = $this->drupalCreateQuiz(array('uid' => $grader->uid));
40+
$this->linkQuestionToQuiz($question_node, $quiz_node);
41+
42+
$this->drupalLogin($this->user);
43+
$this->drupalGet("node/$quiz_node->nid/take");
44+
$this->drupalPost(NULL, array(
45+
"question[$question_node->nid][answer]" => 'bluish',
46+
), t('Finish'));
47+
48+
// Score.
49+
$this->drupalLogin($grader);
50+
$this->drupalGet("node/$quiz_node->nid/quiz/results/1/view");
51+
$this->drupalPost(NULL, array(
52+
'question[0][score]' => 5,
53+
), t('Save score'));
54+
}
55+
56+
/**
57+
* Test quiz takers being able to grade their own results.
58+
*/
59+
public function testQuizTakerAnswerScore() {
60+
$question_node = $this->drupalCreateNode(array(
61+
'type' => 'short_answer',
62+
'title' => 'SA 1 title',
63+
'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
64+
'correct_answer' => 'blue',
65+
'correct_answer' => 1,
66+
'body' => array(LANGUAGE_NONE => array(array('value' => 'SA 1 body text'))),
67+
));
68+
$quiz_node = $this->linkQuestionToQuiz($question_node);
69+
70+
71+
$grader = $this->drupalCreateUser(array('score taken quiz answer', 'view own quiz results'));
72+
$this->drupalLogin($grader);
73+
$this->drupalGet("node/$quiz_node->nid/take");
74+
$this->drupalPost(NULL, array(
75+
"question[$question_node->nid][answer]" => 'bluish',
76+
), t('Finish'));
77+
78+
// Score.
79+
$this->drupalGet("node/$quiz_node->nid/quiz/results/1/view");
80+
$this->drupalPost(NULL, array(
81+
'question[0][score]' => 5,
82+
), t('Save score'));
83+
}
84+
85+
}

0 commit comments

Comments
 (0)
Please sign in to comment.