hi,

i'm trying to write a feeds importer module plugin for quiz but therefore need some help regarding the starting-point for quiz.

could someone point out

  • which function actually adds the quiz functionalities to a node
  • which function adds the question functionalities to a node

my idea

  1. create new node
  2. [set general node settings]
  3. save node
  4. quiz_function_that_adds_settings_to_node( node, quiz_settings )

thanks in advance!

greets

Comments

attisan’s picture

no-one? really? I could really use a helping hand. (in fact - "a pointing finger" would suffice).

greets

joshf’s picture

Issue summary: View changes

It's basically just like any other node. Here's how I was able to accomplish this with a multichoice question:

global $user;

$quiz = new stdClass();
$quiz->title = 'Loyalty Test';
$quiz->type = 'quiz';
$quiz->language = LANGUAGE_NONE;
$quiz->uid = $user->uid; 
$quiz->status = 1;

node_object_prepare($quiz);
$quiz = node_submit($quiz);
node_save($quiz);

$question = new stdClass();
$question->title = 'User is a fan';
$question->type = 'multichoice';
$question->body = array(LANGUAGE_NONE => array(array('value' => 'Do you totally love this website or what?')));
$question->choice_multi = 0;
$question->choice_random = 0;
$question->choice_boolean = 0;
$question->language = LANGUAGE_NONE;
$question->uid = $user->uid; 
$question->status = 1;
$question->alternatives = array(
  array(
    'answer' => array(
      'value' => 'Yes',
    ),
    // All these values are necessary to avoid integrity constraint errors.
    'answer_format' => 'plain_text',
    'feedback_if_chosen_format' => 'plain_text',
    'feedback_if_not_chosen_format' => 'plain_text',
    'score_if_chosen' => 1,
  ),
  array(
    'answer' => array(
      'value' => 'No',
    ),
    'answer_format' => 'plain_text',
    'feedback_if_chosen_format' => 'plain_text',
    'feedback_if_not_chosen_format' => 'plain_text',
    'score_if_chosen' => 0,
  ),
);

node_object_prepare($question);
$question = node_submit($question);
node_save($question);

// This is how you add a question to a quiz.
module_load_include('inc', 'multichoice', 'multichoice.classes');
$question_wrapper = new MultichoiceQuestion($question);
$question_wrapper->saveRelationships($quiz->nid, $quiz->vid);
quiz_update_max_score_properties(array($quiz->vid));

OliverColeman’s picture

I guess the API has changed as the arguments supplied to the saveRelationships function in the previous example are missing (at least from 7.x-4.0-beta3). I ended up doing the below (before node_save() on the question node).

    $question_node->add_directly = array(
      'latest' => array(
        $quiz_node->nid. '-'. $quiz_node->vid => $quiz_node->nid. '-'. $quiz_node->vid
      )
    );
djdevin’s picture

Status: Active » Closed (fixed)

Thanks, and for 5.x it is

$question = node_load($question_nid);
 _quiz_question_get_instance($question)->saveRelationships($quiz_nid, $quiz_vid);

See also quiz_add_question_to_quiz() as well which is the Views Bulk Operation for adding questions to quizzes.

webservant316’s picture

#3 above, the Quiz version 4 solution, depends upon the logic of the function saveRelationships() in quiz_question.core.inc. However, if the quiz node properties specify to randomize the questions, yet there are only mandatory questions attached then saveRelationships() fails to attached the questions properly. Agreed it is curious to set the Quiz to randomize and then only attach mandatory questions. However, this situation is handled properly when using the UI to add questions. However, if programmatically adding questions with these settings then #3 does not work.

There may be a better way , but I had to create the function below for guaranteed attachment of all Questions to the Quizzes in my use case...

function mymodule_quiz_question_relationship($quiz, $question, $relationship) {
if(db_insert('quiz_node_relationship')->fields(array(
'parent_nid' => $quiz->nid,
'parent_vid' => $quiz->vid,
'child_nid'	 => $question->nid,
'child_vid'	 => $question->vid,
'max_score' => $relationship['max_score'],
'weight' => $relationship['weight'],
'question_status' => $relationship['question_status'],
'auto_update_max_score'	=> $relationship['auto_update_max_score'],
))->execute()===FALSE) { return FALSE; }
return TRUE;
}
nasrin_109’s picture

for new version of quiz module we must edit code like below :

$question->alternatives = array(
array(
'answer' => array(
'value' => 'Yes',
'format' => 'plain_text',
),

// All these values are necessary to avoid integrity constraint errors.
// 'answer_format' => 'plain_text',
....

fishfree’s picture

How to do this in 6.0.0-alpha7 version? Thanks!