function makemeeting_answers_form in Make Meeting Scheduler 7.2
Form callback: enables users to answer a makemeeting poll
1 string reference to 'makemeeting_answers_form'
- makemeeting_forms in ./
makemeeting.module - Implements hook_forms().
File
- ./
makemeeting.form.inc, line 6
Code
function makemeeting_answers_form($form, &$form_state, $item, $instance, $answer = NULL) {
global $user;
$form = [];
$form['#item'] = $item;
$form['#theme'] = 'makemeeting_answers';
// Ensure form validation and submission logic is included
form_load_include($form_state, 'inc', 'makemeeting', 'makemeeting.form');
// Force the id of the form as it might get overriden in AJAX-loaded forms
$form['#id'] = 'makemeeting-answers-form';
// Pass entity-related values in the form
foreach ([
'field_name',
'entity_type',
'deleted',
'entity_id',
'language',
'delta',
] as $info) {
$form[$info] = [
'#type' => 'value',
'#value' => $instance[$info],
];
}
// Pass answer being edited
if ($answer) {
$form['answer_edited'] = [
'#type' => 'value',
'#value' => $answer,
];
}
// Include current destination for ajax calls
if (!isset($form_state['ajax_destination'])) {
$form_state['ajax_destination'] = drupal_get_destination();
}
// Include the name of the current user
$form['name'] = [
'#type' => 'textfield',
'#size' => 22,
];
if (!user_is_logged_in()) {
// This is an HTML5 attribute
$form['name']['#attributes'] = [
'placeholder' => t('Your name (required)'),
];
$form['name']['#required'] = TRUE;
}
else {
$form['name']['#default_value'] = format_username($user);
$form['name']['#disabled'] = TRUE;
}
if (!empty($answer)) {
if ($answer->uid > 0) {
$account = user_load($answer->uid);
$form['name']['#default_value'] = format_username($account);
}
else {
$form['name']['#default_value'] = $answer->name;
}
}
// If the form is limited, fetch already submitted answers
$answers = [];
if ($item['limit'] > 0) {
$select = db_select('makemeeting_answers', 'ma')
->fields('ma', [
'value',
]);
foreach ([
'field_name',
'entity_type',
'deleted',
'entity_id',
'language',
'delta',
] as $info) {
$select
->condition($info, $instance[$info]);
}
// Filter out answer being edited
if ($answer) {
$select
->condition('answer_id', $answer->answer_id, '!=');
}
$results = $select
->execute();
// And add each answer to our results array for futher use
foreach ($results as $result) {
$_answer = unserialize($result->value);
if (is_array($_answer)) {
foreach ($_answer as $key => $value) {
if ($value) {
$answers[$key] = empty($answers[$key]) ? 1 : $answers[$key] + 1;
}
}
}
elseif (is_string($_answer) && $_answer) {
$answers[$_answer] = empty($answers[$_answer]) ? 1 : $answers[$_answer] + 1;
}
}
}
// Possible answers
$form['answers'] = [];
foreach ($item['choices'] as $choice) {
$chdate = _makemeeting_date_timestamp($choice['chdate']);
$count = 0;
foreach ($choice['chsuggestions'] as $id => $text) {
// Add a form element only if there's a suggestion label
// or it is the first suggestion for this date
if ($text || !$text && !$count) {
_makemeeting_answer_element($form, $item, $id, $chdate, $text, $answers, $answer);
}
$count++;
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
if (!empty($answer)) {
// Modify form submit to include ajax behavior
$form['submit']['#ajax'] = [
'callback' => 'makemeeting_answer_js',
'wrapper' => 'makemeeting-answers-form',
'effect' => 'fade',
];
}
return $form;
}