class QuizResultController in Quiz 7.6
Same name and namespace in other branches
- 7.5 includes/QuizResultController.class.inc \QuizResultController
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
- class \QuizResultController
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
Expanded class hierarchy of QuizResultController
1 string reference to 'QuizResultController'
- quiz_entity_info in ./
quiz.module - Implements hook_entity_info().
File
- includes/
QuizResultController.class.inc, line 3
View source
class QuizResultController extends EntityAPIController {
function delete($ids, \DatabaseTransaction $transaction = NULL) {
foreach ($ids as $id) {
// Fire the question types' implementations of question attempts deletion.
$sql = 'SELECT result_id, question_nid, question_vid FROM {quiz_node_results_answers}
WHERE result_id = (:result_id)';
$result = db_query($sql, array(
':result_id' => $id,
));
foreach ($result as $record) {
quiz_question_delete_result($id, $record->question_nid, $record->question_vid);
}
// Delete Quiz's records of any attempts at a question.
db_delete('quiz_node_results_answers')
->condition('result_id', $id)
->execute();
}
parent::delete($ids, $transaction);
}
function load($ids = array(), $conditions = array()) {
$quiz_results = parent::load($ids, $conditions);
foreach ($quiz_results as $quiz_result) {
$quiz_node_relationships = entity_load('quiz_question_relationship', FALSE, array(
'parent_nid' => $quiz_result->nid,
'parent_vid' => $quiz_result->vid,
));
$relationships = array();
foreach ($quiz_node_relationships as $quiz_node_relationship) {
$relationships[$quiz_node_relationship->child_nid] = $quiz_node_relationship;
}
$quiz_result_answers = entity_load('quiz_result_answer', FALSE, array(
'result_id' => $quiz_result->result_id,
));
if ($quiz_result_answers) {
foreach ($quiz_result_answers as $quiz_result_answer) {
$wrap = entity_metadata_wrapper('quiz_result_answer', $quiz_result_answer);
$type = $wrap->question_nid
->value()->type;
if (!empty($relationships[$quiz_result_answer->question_nid])) {
$relationship = $relationships[$quiz_result_answer->question_nid];
}
$quiz_result->layout[$quiz_result_answer->number] = array(
'display_number' => $quiz_result_answer->number,
'nid' => $quiz_result_answer->question_nid,
'vid' => $quiz_result_answer->question_vid,
'tid' => $quiz_result_answer->tid,
'number' => $quiz_result_answer->number,
'type' => $type,
'qnr_id' => isset($relationship) ? $relationship->qnr_id : NULL,
'qnr_pid' => isset($relationship) ? $relationship->qnr_pid : NULL,
);
}
ksort($quiz_result->layout, SORT_NUMERIC);
}
else {
$quiz_result->layout = array();
}
}
return $quiz_results;
}
/**
* Save the Quiz result and do any post-processing to the result.
*
* @param type $entity
* @param \DatabaseTransaction $transaction
* @return boolean
*/
function save($entity, \DatabaseTransaction $transaction = NULL) {
if (empty($entity->time_start)) {
$entity->time_start = REQUEST_TIME;
}
$new = !empty($entity->is_new);
// Save the Quiz result.
parent::save($entity, $transaction);
// Post process the result.
if ($new) {
$quiz = node_load($entity->nid, $entity->vid);
// Call @deprecated hook_quiz_begin().
module_invoke_all('quiz_begin', $quiz, $entity->result_id);
// Create question list.
$questions = quiz_build_question_list($quiz);
if ($questions === FALSE) {
drupal_set_message(t('Not enough random questions were found. Please add more questions before trying to take this @quiz.', array(
'@quiz' => QUIZ_NAME,
)), 'error');
return FALSE;
}
if (count($questions) == 0) {
drupal_set_message(t('No questions were found. Please !assign_questions before trying to take this @quiz.', array(
'@quiz' => QUIZ_NAME,
'!assign_questions' => l(t('assign questions'), 'node/' . $quiz->nid . '/quiz/questions'),
)), 'error');
return FALSE;
}
$i = 0;
foreach ($questions as $question) {
$quiz_result_answer = entity_create('quiz_result_answer', array(
'result_id' => $entity->result_id,
'question_nid' => $question['nid'],
'question_vid' => $question['vid'],
'tid' => !empty($question['tid']) ? $question['tid'] : NULL,
'number' => ++$i,
));
entity_save('quiz_result_answer', $quiz_result_answer);
}
if (!empty($entity->build_on_last)) {
$build_on_last = $entity->build_on_last;
// @kludge we need to reload to get the layout loaded on.
$entity = quiz_result_load($entity->result_id);
$entity->build_on_last = $build_on_last;
// Build on the last attempt the user took. If this quiz has build on last
// attempt set, we need to search for a previous attempt with the same
// version of the current quiz.
$quiz_result_old = self::findOldResult($entity);
// Now clone the answers on top of the new result.
quiz_clone_quiz_result($quiz_result_old, $entity);
}
}
}
/**
* Find a result that is not the same as the passed result.
*
* Note: the Quiz result does not have an actually exist - in that case, it
* will return the first completed result found.
*/
public static function findOldResult($quiz_result) {
$efq = new EntityFieldQuery();
$result = $efq
->entityCondition('entity_type', 'quiz_result')
->propertyCondition('uid', $quiz_result->uid)
->propertyCondition('nid', $quiz_result->nid)
->propertyCondition('vid', $quiz_result->vid)
->propertyCondition('result_id', isset($quiz_result->result_id) ? $quiz_result->result_id : 0, '!=')
->propertyCondition('time_start', 0, '>')
->propertyOrderBy('time_start', 'DESC')
->range(0, 1)
->execute();
if (!empty($result['quiz_result'])) {
$old_quiz_result = reset($result['quiz_result']);
return quiz_result_load($old_quiz_result->result_id);
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalDefaultEntityController:: |
protected | property | Whether this entity type should use the static cache. | |
DrupalDefaultEntityController:: |
protected | property | Static cache of entities, keyed by entity ID. | |
DrupalDefaultEntityController:: |
protected | property | Array of information about the entity. | |
DrupalDefaultEntityController:: |
protected | property | Entity type for this controller instance. | |
DrupalDefaultEntityController:: |
protected | property | Additional arguments to pass to hook_TYPE_load(). | |
DrupalDefaultEntityController:: |
protected | property | Name of the entity's ID field in the entity database table. | |
DrupalDefaultEntityController:: |
protected | property | Name of entity's revision database table field, if it supports revisions. | |
DrupalDefaultEntityController:: |
protected | property | The table that stores revisions, if the entity supports revisions. | |
DrupalDefaultEntityController:: |
protected | function | Attaches data to entities upon loading. | 4 |
DrupalDefaultEntityController:: |
protected | function | Gets entities from the static cache. | 1 |
DrupalDefaultEntityController:: |
protected | function | Stores entities in the static entity cache. | |
DrupalDefaultEntityController:: |
protected | function | Ensures integer entity IDs are valid. | |
DrupalDefaultEntityController:: |
protected | function | Callback for array_filter that removes non-integer IDs. | |
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
protected | function |
Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function | Builds and executes the query for loading. | |
EntityAPIController:: |
protected | function | Renders a single entity property. | |
EntityAPIController:: |
public | function |
Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
protected | function | Saves an entity revision. | |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Overridden. Overrides DrupalDefaultEntityController:: |
1 |
QuizResultController:: |
function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |
||
QuizResultController:: |
public static | function | Find a result that is not the same as the passed result. | |
QuizResultController:: |
function |
Overridden. Overrides EntityAPIController:: |
||
QuizResultController:: |
function |
Save the Quiz result and do any post-processing to the result. Overrides EntityAPIController:: |