function quiz_questions_form_submit in Quiz 6.5
Same name and namespace in other branches
- 8.4 quiz.admin.inc \quiz_questions_form_submit()
- 5.2 quiz.module \quiz_questions_form_submit()
- 5 quiz.module \quiz_questions_form_submit()
- 6.6 quiz.admin.inc \quiz_questions_form_submit()
- 6.2 quiz.admin.inc \quiz_questions_form_submit()
- 6.3 quiz.admin.inc \quiz_questions_form_submit()
- 6.4 quiz.admin.inc \quiz_questions_form_submit()
- 7.6 quiz.admin.inc \quiz_questions_form_submit()
- 7 quiz.admin.inc \quiz_questions_form_submit()
- 7.4 quiz.admin.inc \quiz_questions_form_submit()
- 7.5 quiz.admin.inc \quiz_questions_form_submit()
Submit function for quiz_questions.
Updates from the "manage questions" tab.
Parameters
$form_id: A string containing the form id.
$values: Array containing the form values.
1 string reference to 'quiz_questions_form_submit'
- quiz_questions_form in ./
quiz.admin.inc - Handles "manage questions" tab.
File
- ./
quiz.admin.inc, line 350 - Administrator interface for Quiz module.
Code
function quiz_questions_form_submit($form, &$form_state) {
// This is ugly and should be fixed.
$quiz = node_load(arg(1));
$new_revision = $form_state['values']['new_revision'];
$num_random = $form_state['values']['number_of_random_questions'];
$term_id = isset($form_state['values']['random_term_id']) ? $form_state['values']['random_term_id'] : 0;
$removed = explode(',', $form_state['values']['remove_from_quiz']);
// Get quiz questions that will always be on the quiz:
$questions = array();
foreach ($form_state['values']['weights'] as $id => $weight) {
if (in_array($id, $removed)) {
// Skip items that should be removed.
continue;
}
list($state, $nid) = explode('-', $id, 2);
$nid = (int) $nid;
$question['nid'] = $nid;
$question['state'] = $state == 'always' ? QUESTION_ALWAYS : QUESTION_RANDOM;
$question['weight'] = $weight;
// Add item as an object in the questions array.
$questions[] = (object) $question;
}
// Save questions.
quiz_set_questions($quiz, $questions, (bool) $new_revision);
// If using random questions and no term ID is specified, make sure we have enough.
if (empty($term_id)) {
$assigned_random = 0;
foreach ($questions as $question) {
if ($question->state == QUESTION_RANDOM) {
++$assigned_random;
}
}
// Adjust number of random questions downward to match number of selected questions..
if ($num_random > $assigned_random) {
$num_random = $assigned_random;
drupal_set_message(t('The number of random questions for this @quiz have been lowered to %anum to match the number of questions you assigned.', array(
'@quiz' => QUIZ_NAME,
'%anum' => $assigned_random,
), 'warning'));
}
}
else {
// Warn user if not enough questions available with this term_id.
$available_random = count(_quiz_get_random_taxonomy_question_ids($term_id, $num_random));
if ($num_random > $available_random) {
$num_random = $available_random;
drupal_set_message(t('There are currently not enough questions assigned to this term (@random). Please lower the number of random quetions or assign more questions to this taxonomy term before taking this @quiz.', array(
'@random' => $available_random,
'@quiz' => QUIZ_NAME,
)), 'error');
}
}
// Update the quiz properties.
$sql = "UPDATE {quiz_node_properties} SET number_of_random_questions = %d, tid = %d WHERE vid = %d AND nid = %d";
if (db_query($sql, $num_random, $term_id, $quiz->vid, $quiz->nid) == FALSE) {
drupal_set_message(t('There was an error updating the @quiz.', array(
'@quiz' => QUIZ_NAME,
)), 'error');
}
else {
drupal_set_message(t('Questions updated successfully.'));
}
}