function _quiz_question_get_instance in Quiz 8.4
Same name and namespace in other branches
- 6.6 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 6.3 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 6.4 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 6.5 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 7.6 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 7 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 7.4 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
- 7.5 question_types/quiz_question/quiz_question.module \_quiz_question_get_instance()
Get an instance of a quiz question.
Get information about the class and use it to construct a new object of the appropriate type.
Parameters
$node: Question node
$use_cached: Can we use a cached version of the node?
Return value
The appropriate QuizQuestion extension instance
4 calls to _quiz_question_get_instance()
- quiz_question_answering_form in question_types/
quiz_question/ quiz_question.module - Get the form to show to the quiz taker.
- quiz_question_form_node_form_alter in question_types/
quiz_question/ quiz_question.module - Implements hook_form_BASE_FORM_ID_alter().
- quiz_question_has_been_answered in question_types/
quiz_question/ quiz_question.module - @todo Please document this function.
- quiz_question_quiz_question_score in question_types/
quiz_question/ quiz_question.module - Implements hook_quiz_question_score().
File
- question_types/
quiz_question/ quiz_question.module, line 694 - Quiz Question module. This module provides the basic facilities for adding quiz question types to a quiz.
Code
function _quiz_question_get_instance($node, $use_cached = FALSE) {
// We use static caching to improve performance
static $question_instances = array();
$using_dummy_node = FALSE;
if (is_object($node) && !$node instanceof \stdClass) {
$vid = $node
->getRevisionId() ? $node
->getRevisionId() : 0;
if ($use_cached && isset($question_instances[$vid])) {
// We just return a cached instance of the QuizQuestion
return $question_instances[$vid];
}
$name = $node
->getType();
}
elseif (is_object($node) && $node instanceof \stdClass) {
$vid = $node->vid ? $node->vid : 0;
if ($use_cached && isset($question_instances[$vid])) {
// We just return a cached instance of the QuizQuestion
return $question_instances[$vid];
}
// If $node don't have a type it is a dummy node
if (!isset($node->type) || !($name = $node->type)) {
// To substanitally improve performance(especially on the result page) we avoid node_load()...
$sql = 'SELECT d.type, r.nid, r.vid, r.title, p.max_score
FROM {node_field_revision} r
JOIN {node} n ON r.nid = n.nid
JOIN {node_field_data} d ON d.nid = n.nid
JOIN {quiz_question_properties} p ON r.vid = p.vid
WHERE r.vid = :vid';
$node = db_query($sql, array(
':vid' => $node->vid,
))
->fetch();
// @todo: Do it in proper way.
$node = (array) $node;
$node = entity_create('node', $node);
$name = $node
->getType();
$using_dummy_node = TRUE;
}
}
elseif (is_array($node)) {
$name = $node['type'];
$vid = $node['vid'];
if ($use_cached && isset($question_instances[$vid])) {
// We return a cached instance of the appropriate QuizQuestion
return $question_instances[$vid];
}
}
// No cached instance of QuizQuestion has been returned. We construct a new instance
$info = _quiz_question_get_implementations();
$constructor = $info[$name]['question provider'];
if (empty($constructor)) {
return FALSE;
}
// We create a new instance of QuizQuestion
$to_return = new $constructor($node);
if (!$to_return instanceof Drupal\quiz_question\QuizQuestion) {
// Make sure the constructor is creating an extension of QuizQuestion
drupal_set_message(t('The question-type %name isn\'t a QuizQuestion. It needs to extend the QuizQuestion class.', array(
'%name' => $name,
)), 'error', FALSE);
}
// If we're using a dummy node we have to run getNodeProperties, and populate the node with those properties
if ($using_dummy_node) {
$props = $to_return
->getNodeProperties();
foreach ($props as $key => $value) {
$to_return->node->{$key} = $value;
}
}
// Cache the node
$question_instances[$vid] = $to_return;
return $to_return;
}