You are here

function og_quiz_get_user_results in OG Quiz 7

Page callback for viewing user results. Filter by courses the viewer is actually a member of.

Parameters

int $uid:

Return value

string

See also

quiz_get_user_results().

1 string reference to 'og_quiz_get_user_results'
og_quiz_menu_alter in ./og_quiz.module
Implements hook_menu_alter().

File

./og_quiz.module, line 446
Module hooks and custom logic.

Code

function og_quiz_get_user_results($uid) {
  module_load_include('inc', 'quiz', 'quiz.pages');
  global $user;

  // In some edge-cases, we get the user object instead of the uid.
  if (is_object($uid) && isset($uid->uid)) {
    $uid = $uid->uid;
  }

  // This is a verbatim copy of quiz_get_user_results().
  $results = array();
  $dbresult = db_query('SELECT DISTINCT(n.nid), n.title, qnp.pass_rate, qnrs.result_id, qnrs.time_start, qnrs.time_end, qnrs.score, qnrs.is_evaluated
    FROM {node} n
    INNER JOIN {quiz_node_properties} qnp ON n.nid = qnp.nid
    INNER JOIN {quiz_node_results} qnrs ON qnrs.vid = qnp.vid
    INNER JOIN {users} u ON u.uid = qnrs.uid
    WHERE n.type = :type
      AND u.uid = :uid
    ORDER BY qnrs.result_id DESC', array(
    ':type' => 'quiz',
    ':uid' => $uid,
  ));

  // Create an array out of the results.
  foreach ($dbresult as $result) {
    $result = (array) $result;
    $access = TRUE;

    // Start filtering based on access levels.
    if (!user_access('view any quiz results')) {
      $quiz = node_load($result['nid']);
      if (!(user_access('view results for own quiz') && $user->uid == $quiz->uid)) {

        // Check group permissions.
        if (!og_quiz_ogs_access($quiz, 'view any quiz results')) {
          if (!(og_quiz_ogs_access($quiz, 'view results for own quiz') && $user->uid == $quiz->uid)) {

            // Is it the user herself ?
            if (!(user_access('view own quiz results') && $user->uid == $uid)) {
              if (!(og_quiz_ogs_access($quiz, 'view own quiz results') && $user->uid == $uid)) {
                $access = FALSE;
              }
            }
          }
        }
      }
    }

    // Filter by groups the user can see
    if ($access) {
      $results[$result['result_id']] = $result;
    }
  }
  return theme('quiz_get_user_results', array(
    'results' => $results,
  ));
}