function security_questions_user_answer_form in Security Questions 6
Same name and namespace in other branches
- 7 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_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 440 - Main module file for security_questions.
Code
function security_questions_user_answer_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_fetch_array(db_query("SELECT q.security_question\n\t FROM {security_questions} q, {security_questions_answers} a\n WHERE a.uid = %d AND q.security_question_id = a.security_question_id", $account->uid));
}
// 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',
'#value' => 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['header'], $table['rows']);
$form['security_questions']['answered'] = array(
'#type' => 'item',
'#value' => $output,
);
}
else {
$form['security_questions']['help'] = array(
'#type' => 'item',
'#value' => 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 = %d OR admin = 1', $account->uid);
}
else {
$questions = db_query('SELECT security_question AS sc, security_question_id AS qid
FROM {security_questions} WHERE admin = 1');
}
$options = array();
// No need to check_plain on the options, as they will be checked during
// form_select_options.
while ($q = db_fetch_object($questions)) {
$options[$q->qid] = $q->sc;
}
// If we are allowing user defined questions, add an option for "other."
if (variable_get('security_questions_user_questions', FALSE)) {
$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.
// We need to include and depend on ctools dependent functions for this.
// This has been replaced by the #states attributes in D7.
ctools_include('dependent');
if (variable_get('security_questions_user_questions', FALSE)) {
$form['security_questions']['security_question_user_question_' . $i] = array(
'#type' => 'textfield',
'#title' => t('Question %i', array(
'%i' => $i,
)),
'#description' => t('Enter your own question'),
);
}
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]['#dependency'] = array(
'edit-security-question-id-' . $i => array(
'other',
),
);
$form['security_questions']['security_question_user_question_' . $i]['#process'] = array(
'ctools_dependent_process',
);
}
$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,
);
if (count($options) === 1) {
$form['security_questions']['security_question_user_answer_' . $i]['#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['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit answers'),
);
}
return $form;
}