commit c97eb749142e70f01e566b0b5e609b4bdbf5ebdb Author: fago Date: Wed Oct 5 17:06:27 2011 +0200 init diff --git a/quiz.module b/quiz.module index 9f98894..d06c6ff 100644 --- a/quiz.module +++ b/quiz.module @@ -2042,6 +2042,9 @@ function quiz_take_quiz($quiz) { // Remove session variables, save $rid $rid = $_SESSION['quiz_' . $quiz->nid]['result_id']; unset($_SESSION['quiz_' . $quiz->nid]); + if (module_exists('rules')) { + rules_invoke_event('quiz_end', $quiz); + } // NOTE: End actions might redirect the user somewhere. Code below this line might not get executed... quiz_end_actions($quiz, $rid, $score); } diff --git a/quiz.rules.inc b/quiz.rules.inc new file mode 100644 index 0000000..4c7f7a6 --- /dev/null +++ b/quiz.rules.inc @@ -0,0 +1,102 @@ + t('User has completed a quiz'), + 'group' => t('Quiz'), + 'variables' => array( + 'node' => array( + 'type' => 'node', + 'bundle' => 'quiz', + 'label' => t('Quiz node'), + ), + ), + 'access callback' => 'quiz_rules_integration_access', + ); + return $items; +} + +/** + * Implements hook_rules_condition_info(). + */ +function quiz_rules_condition_info() { + $items['quiz_is_passed'] = array( + 'label' => t('User has passed a quiz'), + 'parameter' => array( + 'node' => array( + 'type' => 'node', + 'bundles' => array('quiz'), + 'label' => t('Quiz node'), + ), + 'account' => array( + 'type' => 'user', + 'label' => t('User'), + ), + ), + 'base' => 'quiz_rules_is_passed', + 'group' => t('Quiz'), + 'access callback' => 'quiz_rules_integration_access', + ); + return $items; +} + +/** + * Condition implementation: User has passed a quiz. + */ +function quiz_rules_is_passed($quiz, $account) { + return quiz_is_passed($account->uid, $quiz->nid, $quiz->vid); +} + +/** +* Implements hook_rules_action_info(). +*/ +function quiz_rules_action_info() { + $items['quiz_clear_user_result'] = array( + 'label' => t("Clear a user's quiz results"), + 'parameter' => array( + 'account' => array( + 'type' => 'user', + 'label' => t('User'), + 'description' => t('The user for whose the results should be deleted.') + ), + 'node' => array( + 'type' => 'node', + 'bundles' => array('quiz'), + 'label' => t('Quiz node'), + ), + ), + 'base' => 'quiz_rules_clear_user_result', + 'group' => t('Quiz'), + 'access callback' => 'quiz_rules_integration_access', + ); + return $items; +} + +/** + * Action implementation: Clear a user's quiz results. + */ +function quiz_rules_clear_user_result($account, $quiz) { + db_delete('quiz_node_results') + ->condition('uid', $account->uid) + ->condition('nid', $quiz->nid) + ->execute(); +} + +/** + * Quiz rules integration access callback. + */ +function quiz_rules_integration_access($type, $name) { + if ($type == 'event' || $type == 'condition') { + return entity_access('view', 'node') && user_access('view any quiz results'); + } + return user_access('administer quiz configuration'); +} +