function quiz_questions_form_validate in Quiz 7.5
Same name and namespace in other branches
- 8.4 quiz.admin.inc \quiz_questions_form_validate()
- 6.6 quiz.admin.inc \quiz_questions_form_validate()
- 6.3 quiz.admin.inc \quiz_questions_form_validate()
- 6.4 quiz.admin.inc \quiz_questions_form_validate()
- 6.5 quiz.admin.inc \quiz_questions_form_validate()
- 7.6 quiz.admin.inc \quiz_questions_form_validate()
- 7 quiz.admin.inc \quiz_questions_form_validate()
- 7.4 quiz.admin.inc \quiz_questions_form_validate()
Validate that the supplied questions are real.
File
- ./
quiz.admin.inc, line 986 - Administrator interface for Quiz module.
Code
function quiz_questions_form_validate($form, $form_state) {
if ($form_state['build_info']['args'][0]->changed > $form_state['values']['timestamp']) {
form_set_error('changed', t('This content has been modified by another user, changes cannot be saved.'));
}
$already_checked = array();
$weight_map = isset($form_state['values']['weights']) ? $form_state['values']['weights'] : array();
// Make sure the number of random questions is a positive number.
if (isset($form_state['values']['num_random_questions']) && !_quiz_is_int($form_state['values']['num_random_questions'], 0)) {
form_set_error('num_random_questions', 'The number of random questions needs to be a positive number');
}
// Make sure the max score for random questions is a positive number.
if (isset($form_state['values']['max_score_for_random']) && !_quiz_is_int($form_state['values']['max_score_for_random'], 0)) {
form_set_error('max_score_for_random', 'The max score for random questions needs to be a positive number');
}
if (empty($weight_map)) {
form_set_error('none', 'No questions were included.');
return;
}
$question_types = array_keys(quiz_get_question_types());
foreach ($weight_map as $id => $weight) {
list($nid, $vid) = explode('-', $id, 2);
// If a node isn't one of the questionstypes we remove it from the question
// list.
$has_questions = (bool) db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('type', $question_types, 'IN')
->condition('n.nid', $nid)
->execute()
->fetchField();
if (!$has_questions) {
form_set_error('none', 'One of the supplied questions was invalid. It has been removed from the quiz.');
unset($form_state['values']['weights'][$id]);
}
elseif (in_array($nid, $already_checked)) {
form_set_error('none', 'A duplicate question has been removed. You can only ask a question once per quiz.');
unset($form_state['values']['weights'][$id]);
}
else {
$already_checked[] = $nid;
}
}
// We make sure max score is a positive number.
$max_scores = $form_state['values']['max_scores'];
foreach ($max_scores as $id => $max_score) {
if (!_quiz_is_int($max_score, 0)) {
form_set_error("max_scores][{$id}", t('Max score needs to be a positive number'));
}
}
}