You are here

function quiz_access_results in Quiz 7.5

Same name and namespace in other branches
  1. 8.4 quiz.module \quiz_access_results()
  2. 6.4 quiz.module \quiz_access_results()
  3. 7.6 quiz.module \quiz_access_results()
  4. 7 quiz.module \quiz_access_results()
  5. 7.4 quiz.module \quiz_access_results()

Helper function to determine if a user has access to the different results pages.

Parameters

$quiz: The quiz node.

$result_id: The result id of a result we are trying to access.

Return value

bool|NULL TRUE if user has permission.

1 call to quiz_access_results()
quiz_result_access in ./quiz.module
Entity access for quiz results.
1 string reference to 'quiz_access_results'
quiz_menu in ./quiz.module
Implements hook_menu().

File

./quiz.module, line 186
quiz.module Main file for the Quiz module.

Code

function quiz_access_results($quiz, $quiz_result = NULL) {
  global $user;
  if ($quiz->type !== 'quiz') {
    return FALSE;
  }

  // If rid is set we must make sure the result belongs to the quiz we are
  // viewing results for.
  if (!empty($quiz_result)) {
    $res = db_query('SELECT qnr.nid, qnr.uid FROM {quiz_node_results} qnr WHERE result_id = :result_id', array(
      ':result_id' => $quiz_result->result_id,
    ))
      ->fetch();
    if ($res && $res->nid != $quiz->nid) {
      return FALSE;
    }
  }
  if (user_access('score any quiz')) {
    return TRUE;
  }
  if (user_access('view any quiz results')) {
    return TRUE;
  }
  if (user_access('view results for own quiz') && $user->uid == $quiz->uid) {
    return TRUE;
  }
  if (user_access('score taken quiz answer')) {

    // Check if the taken user is seeing his result.
    if (!empty($quiz_result) && $res && $res->uid == $user->uid) {
      return TRUE;
    }
  }
}