function security_questions_user_answer_form in Security Questions 7
Same name and namespace in other branches
- 6 security_questions.module \security_questions_user_answer_form()
Main form for answering questions.
The reason for the variables defaulting to NULL is due to the different ways we will be using the form. Sometimes it will be a stand alone form, other times it will be added to an existing form.
3 string references to 'security_questions_user_answer_form'
- security_questions_form_user_login_alter in ./
security_questions.module - Implements hook_form_FORM_ID_alter() for user_login().
- security_questions_form_user_register_form_alter in ./
security_questions.module - Implements hook_form_FORM_ID_alter() for user_register().
- security_questions_list_user in ./
security_questions.module - Security Questions List page for user.
File
- ./
security_questions.module, line 478 - Main module file for security_questions.
Code
function security_questions_user_answer_form($form, &$form_state, $account = NULL, $required = NULL, $form_id = NULL) {
// If we are using this form in an existing form, get the required number
// of questions from the build info.
if (!empty($form_state['build_info'])) {
$required = $form_state['build_info']['args'][1];
}
// Get a random question for this user.
if ($account) {
$random_question = security_questions_get_random_question($account);
// Get a list of the questions that the user has already answered.
$answered = db_query('SELECT q.security_question
FROM {security_questions} q, {security_questions_answers} a
WHERE a.uid = :uid AND q.security_question_id = a.security_question_id', array(
':uid' => $account->uid,
))
->fetchCol();
}
// Store number of required questions for this context in the form state
// to pass it to validation and submit handlers.
$form['security_questions'] = array(
'#type' => 'fieldset',
'#title' => t('Security Questions'),
'#collapsed' => FALSE,
'#collapsible' => FALSE,
);
// If there is an account available, list the questions they have answered
// and provide a form to answer unanswered required questions.
if ($account) {
if ($answered) {
$form['security_questions']['help'] = array(
'#type' => 'item',
'#markup' => t("You don't have the required number of security questions answered. Please answer the following question(s)."),
);
// Output questions in a table so that the user can easily see which
// questions they have already answered.
foreach ($answered as $row) {
$rows[] = array(
check_plain($row),
);
}
$table = array(
'header' => array(
t('Previously answered questions'),
),
'rows' => $rows,
);
$output = theme('table', $table);
$form['security_questions']['answered'] = array(
'#type' => 'item',
'#markup' => $output,
);
}
else {
$form['security_questions']['help'] = array(
'#type' => 'item',
'#markup' => t('You have not answered any security questions. Please answer the following questions. They will be used to verify your identity in the future.'),
);
}
}
// Get a list of questions that the user can answer. If we are allowing user
// supplied questions, we need to make sure that we include them.
if ($account) {
$questions = db_query('SELECT security_question AS sc, security_question_id AS qid
FROM {security_questions} WHERE uid = :uid OR admin = :admin', array(
':uid' => $account->uid,
':admin' => 1,
));
}
else {
$questions = db_query('SELECT security_question AS sc, security_question_id AS qid
FROM {security_questions} WHERE admin = :admin', array(
':admin' => 1,
));
}
$options = array();
// No need to check_plain on the options, as they will be checked during
// form_select_options.
while ($q = $questions
->fetchObject()) {
$options[$q->qid] = $q->sc;
}
// If we are allowing user defined questions, add an option for "other."
if (variable_get('security_questions_user_questions')) {
$options['other'] = t('-- Other - Enter your own question');
}
// Set counter to start at the number of questions required.
$i = 1;
while ($i <= $required) {
$form['security_questions']['security_question_id_' . $i] = array(
'#type' => 'select',
'#title' => t('Question @i', array(
'@i' => $i,
)),
'#description' => t('The security question to which you want to answer'),
'#required' => TRUE,
'#options' => $options,
);
// If we are allowing user defined questions allow for user's questions.
if (variable_get('security_questions_user_questions')) {
$form['security_questions']['security_question_user_question_' . $i] = array(
'#type' => 'textfield',
'#title' => t('Question @i', array(
'@i' => $i,
)),
'#description' => t('Enter your own question'),
'#required' => FALSE,
);
// If there are no predefined questions to select, hide the selector and
// make the text field required.
if (count($options) === 1) {
$form['security_questions']['security_question_id_' . $i]['#access'] = FALSE;
$form['security_questions']['security_question_id_' . $i]['#default_value'] = 'other';
$form['security_questions']['security_question_user_question_' . $i]['#required'] = TRUE;
}
else {
$form['security_questions']['security_question_user_question_' . $i]['#states'] = array(
'visible' => array(
':input[name="security_question_id_' . $i . '"]' => array(
'value' => 'other',
),
),
);
}
}
$form['security_questions']['security_question_user_answer_' . $i] = array(
'#type' => 'textfield',
'#title' => t('Answer @i', array(
'@i' => $i,
)),
'#description' => t('Your answer to the selected security question'),
'#required' => TRUE,
);
$i++;
}
// If this form is being used on the list page for the user,
// add a submit button.
if ($form_id == 'list_page') {
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit answers'),
);
}
return $form;
}