You are here

function quiz_access_results in Quiz 8.4

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

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

Parameters

$quiz: The quiz node.

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

Return value

boolean TRUE if user has permission.

1 call to quiz_access_results()
quiz_access_my_results in ./quiz.module
Helper function to determine if a user has access to view his quiz results
1 string reference to 'quiz_access_results'
quiz.routing.yml in ./quiz.routing.yml
quiz.routing.yml

File

./quiz.module, line 184
Quiz Module

Code

function quiz_access_results($quiz, $rid = NULL) {
  global $user;
  $res = array();
  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 (isset($rid)) {
    $res = db_query('SELECT qnr.nid, qnr.uid FROM {quiz_node_results} qnr WHERE result_id = :result_id', array(
      ':result_id' => $rid,
    ))
      ->fetch();
    if ($res && $res->nid != $quiz
      ->id()) {
      return FALSE;
    }
  }
  if (\Drupal::currentUser()
    ->hasPermission('view any quiz results')) {
    return TRUE;
  }
  if (\Drupal::currentUser()
    ->hasPermission('view results for own quiz') && $user->uid == $quiz->uid) {
    return TRUE;
  }
  if (\Drupal::currentUser()
    ->hasPermission('score taken quiz answer')) {

    //check if the taken user is seeing his result
    if (isset($rid) && $res && $res->uid == $user->uid) {
      return TRUE;
    }
  }
}