Similar to this issue:
https://www.drupal.org/node/2581979

I am getting an SQL error on `result_id` for short_answers. To me at least the original SQL appears similar to the un-altered long_answer sql by amending the SQL in a similar fashion to the above resolved issue the query works and my result(s) are loaded.

See attached patch.

CommentFileSizeAuthor
fix-short-answer-get-answer-query.patch1.79 KBtenken
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tenken created an issue. See original summary.

djdevin’s picture

Hi,

This function is no longer called in beta3, are you sure you are on that version?

tenken’s picture

This function is no longer called in beta3, are you sure you are on that version?

I'm sorry, I never told you how I was using this function. It appears this function is in the module and non-depercated at this time, nor is it labeled as private with a convention such as _short_answer_get_answer() Here it is in git:
http://cgit.drupalcode.org/quiz/tree/question_types/short_answer/short_a...

I want to send an email after quiz submission that contains within the email body some answers submitted by students. I found the default email options in the quiz admin form lacking to send summitted answers in quizes, whereas say the Webform module can more easily do this but doesn't contain a scoring feature. When I send an email I need to use this function to get the answers. Please correct me if there is a better approach :).

I used hook_quiz_finished simply because it was the simplest means to send a custom email on submission from this SO advice: http://stackoverflow.com/a/24161050/1491507

The below code works for beta3 within my project.

I understand, and my client understands, hook_quiz_finished is deprecated, when Quiz6 comes out we'll deal with making this all work again:

/**
 * implements hook_quiz_finished().
 *
 * hook_quiz_finished is DEPERCATED in Quiz5 but but not yet removed.
 * For Quiz 6 assure this can still function.
 */
function ucen_quiz_finished($quiz, $score, $session_data) {

  // Load this quiz module submissions current result. So we can get the
  // visitors name and student perm for example.
  //
  // Ok so this is complicated because a Quiz can contain
  // many versions of the same quiz if a question is amended
  // over time by the quiz creator. So this code loads the quiz
  // result for the given RESULT_ID and then fetches the answer
  // for the specific question(s) based on their "layout" eg, "order"
  // in the Quiz. Eg, this is more complicated than a
  // Webform Component.
  //
  // So this code looks at questions #1, 2 and 3 as first_name,
  // last_name and student_perm number and saves off those
  // values to a local variable to later include in an email.
  $result_id = $_SESSION['quiz'][$quiz->nid]['result_id'];
  $quiz_result = quiz_result_load($result_id);
  
  // Why these aren't questions #1, #2 and #3 in Layout is
  // beyond me, intro page? so we want numbers: 2, 3 and 4. :P
  $questions = array(
     'first_name' => 2,
     'last_name' => 3,
     'student_perm' => 4);
  $answer_tokens = array();
  foreach ($questions as $title => $question_number) {
    $qinfo = $quiz_result->layout[$question_number];
    // This parameter ordering confusion was confusing between various
    // quiz 'answer' question type methods.
    //$answer = quiz_result_answer_load($result_id, $qinfo['nid'], $qinfo['vid']);
    $answer = short_answer_get_answer($qinfo['nid'], $qinfo['vid'], $result_id);
    $answer_tokens[$title] = $answer['answer'];
  }

  // to e-mail address
  $to = "test@test.com"; 
  // from e-mail address, if left NULL will use the System email address.
  $from = 'noreply@test.edu';

  // Depending on what quiz is submitted by the visitor we may or may not want
  // customize the body or subject of the email.
  $node = $quiz;
  switch ($node->nid) {
    case 206:
      $subject = "Training Submission Results for: " . $quiz->title; // subject of e-mail
      $body = <<<EOFHTML
A training session was completed for:

First Name:   $answer_tokens[first_name]

Last Name:    $answer_tokens[last_name]

Student Perm:</b> $answer_tokens[student_perm]


Score:  Total score of $score[numeric_score] Correct out of $score[possible_score] Possible.
EOFHTML;
      break;
    default:
      $subject = 'test ucen email';
      $body = 'test ucen body';
      break;
  }

  // Having built the Subject and Body of the email as needed for the given
  // quiz we basically call the default Drupal mail function to send the email.
  // This is the same code no matter what quiz is submitted.

  //params is the array passed to hook_mail function
  $params = array(
    'subject' => $subject,
    'body' => $body,
  );
  #dpm($params, 'email params');
  drupal_mail('ucen', 'ucen-quiz-submission', $to,
  language_default(), $params, $from, TRUE);
}

Thank you for your time and this module has shown to be very helpful! In this case we're using it basically to score small "work safety" training materials that have a simple score for student jobs such as cashier, food handing safety, etc and we require new student hires to complete.

So in short I use this function, and it appears broken, possibly relevant xkcd https://xkcd.com/1172/ :D

djdevin’s picture

Sorry :)

If you check out quiz.api.php all the deprecated hooks are listed there (as deprecated). Slowly we have been moving away from those "helper" functions and putting them into the Question API.

Moving forward Quiz 5+ is Entity-aware, so you can use Rules for doing things like sending email on Quiz completion. Or, you can use any of the Entity-defined hooks. There are 5 entity types defined, in quiz.api.php, that you can use instead of the deprecated custom hooks.

i.e. instead of hook_quiz_finished, use hook_quiz_result_update($quiz_result)

Then you can check if $quiz_result->is_evaluated.

To replace short_answer_get_answer() I would do this instead:

$questionResponse = _quiz_question_response_get_instance($result_id, $question_node);
$answer = $questionResponse->getResponse();

Let me know if that helps.

tenken’s picture

ok thank you! yes this is helpful should I re-engineer it in the future. :)

I'm not sure you want to use the supplied patch, my understanding is that the SQL in the current helper function is broken as-is.

djdevin’s picture

Category: Bug report » Support request
Status: Active » Closed (fixed)