0

In drupal 6, I had a requirement to enable the users to take the quiz on the next day if they failed and from somewhere I got the following code to make that work. They added one more variables, time_end and used strtotime('today')

But now I upgraded to Drupal 7 and code for the same a little bit changed and I couldn't make that work. I am looking forward to getting help to achieve it. I am using quiz quiz-7.x-4.0-rc1

Drupal 6 Original lines

// Check to see if this user is allowed to take the quiz again:
  if ($quiz->takes > 0) {
//   $query = "SELECT COUNT(*) AS takes FROM {quiz_node_results} WHERE uid = %d AND nid = %d";
//   $taken = db_result(db_query($query, $user->uid, $quiz->nid));

Drupal 6 Changed lines (only first two lines. after that code remains the same except drupal_set_message.)

     $query = "SELECT COUNT(*) AS takes FROM {quiz_node_results} WHERE uid = %d AND nid = %d AND time_end > %d";
        $taken = db_result(db_query($query, $user->uid, $quiz->nid, strtotime('today')));

    $allowed_times = format_plural($quiz->takes, '1 time', '@count times');
    $taken_times   = format_plural($taken, '1 time', '@count times');

Drupal 7 Original lines.

  // Check to see if this user is allowed to take the quiz again:
  if ($quiz->takes > 0) {
    $taken = db_query("SELECT COUNT(*) AS takes FROM {quiz_node_results} WHERE uid = :uid AND nid = :nid", array(':uid' => $user->uid, ':nid' => $quiz->nid))->fetchField();
    $allowed_times = format_plural($quiz->takes, '1 time', '@count times');
    $taken_times   = format_plural($taken, '1 time', '@count times');

Full drupal 7 code is available here DrupalContrib

4
  • Cant you make use of rules instead ?? Have you tried installing rules ?? Commented Sep 14, 2016 at 7:10
  • quiz-7.x-4.0-rc1 is not integrated with rules...but quiz 5 does. But I am getting so much of problems while upgrading. Anyway thanks for advise.
    – Nikhil VR
    Commented Sep 14, 2016 at 11:22
  • Please refer drupal.stackexchange.com/questions/212091/… may be this could be of any help Commented Sep 14, 2016 at 12:17
  • show o/p of print_r($quiz); just above if ($quiz->takes > 0) Commented Sep 22, 2016 at 4:33

1 Answer 1

0

I could find a solution. It's actually pretty simple. But I don't know whether this is the right way doing it. Just change $taken like below. Then anybody can retake the test on the next day. Thanks to all for the support.

$taken = db_query("SELECT COUNT(*) AS takes FROM {quiz_node_results} WHERE uid = :uid AND nid = :nid AND time_end > :time_end", array(':uid' => $user->uid, ':nid' => $quiz->nid, ':time_end' => strtotime('today')))->fetchField();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.