You are here

function quiz_question_refresh_latest_quizzes in Quiz 8.4

Same name and namespace in other branches
  1. 6.4 question_types/quiz_question/quiz_question.module \quiz_question_refresh_latest_quizzes()
  2. 7 question_types/quiz_question/quiz_question.module \quiz_question_refresh_latest_quizzes()
  3. 7.4 question_types/quiz_question/quiz_question.module \quiz_question_refresh_latest_quizzes()

Refreshes the quiz_question_latest_quizzes table when a user has modified a new quiz.

The latest quizzes table is used to know what quizzes the user has been using lately.

Parameters

$nid: nid of the last quiz the current user modified

2 calls to quiz_question_refresh_latest_quizzes()
quiz_question_node_insert in question_types/quiz_question/quiz_question.module
Implements hook_node_insert().
quiz_question_node_update in question_types/quiz_question/quiz_question.module
Implements hook_node_update().

File

question_types/quiz_question/quiz_question.module, line 879
Quiz Question module. This module provides the basic facilities for adding quiz question types to a quiz.

Code

function quiz_question_refresh_latest_quizzes($nid) {
  $user = \Drupal::currentUser();

  // Delete entry if it allready exists
  db_delete('quiz_question_latest_quizzes')
    ->condition('uid', $user
    ->id())
    ->condition('quiz_nid', $nid)
    ->execute();

  // Inserts as new entry to get new id. Latest quizzes are ordered by id(descending)
  $id = db_insert('quiz_question_latest_quizzes')
    ->fields(array(
    'uid' => $user
      ->id(),
    'quiz_nid' => $nid,
  ))
    ->execute();

  // If we have to many entries for current user, delete the oldest entries...
  $min_id = db_select('quiz_question_latest_quizzes', 'lq')
    ->fields('lq', array(
    'id',
  ))
    ->condition('uid', $user
    ->id())
    ->orderBy('id', 'DESC')
    ->range(QUIZ_QUESTION_NUM_LATEST - 1, 1)
    ->execute()
    ->fetchField();

  // Delete all table entries older than the nth row, if nth row was found.
  if ($min_id) {
    db_delete('quiz_question_latest_quizzes')
      ->condition('id', $min_id, '<')
      ->condition('uid', $user
      ->id())
      ->execute();
  }
}