function faq_ask_settings_form in FAQ_Ask 7
Same name and namespace in other branches
- 6.2 faq_ask.module \faq_ask_settings_form()
- 6 faq_ask.module \faq_ask_settings_form()
Implements hook_form().
This form allows the users to select the expert roles and to which categories the users in those roles are assigned. Note, the expert/category table attempts to use the least horizontal space, so it can "flip" based on whether there are more categories or experts.
Parameters
array $form_state:
1 string reference to 'faq_ask_settings_form'
- faq_ask_menu in ./
faq_ask.module - Implements hook_menu().
File
- ./
faq_ask.module, line 1128 - This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.
Code
function faq_ask_settings_form($form, &$form_state) {
// Set a basic message that will be unset once we pass the error checking.
$form['error'] = array(
'#value' => t('Errors were found, please correct them before proceeding.'),
'#weight' => -10,
);
$faq_use_categories = variable_get('faq_use_categories', FALSE);
if (!$faq_use_categories) {
drupal_set_message(t('The Faq_Ask module requires that FAQ "Categorize questions."') . ' ' . t('Please go to the <a href="@url">settings page</a> to configure this module.', array(
'@url' => url('admin/config/content/faq/categories'),
)), 'error');
return $form;
}
// Get the list of vocabularies that apply to FAQ s.
$vocabs = taxonomy_get_vocabularies('faq');
if (count($vocabs) == 0) {
drupal_set_message(t('The Faq_Ask module requires that at least one vocabulary apply to the "faq" content type. Please go to the Taxonomy <a href="@taxo_uri">configuration page</a> to do this.', array(
'@taxo_uri' => url('admin/structure/taxonomy'),
)), 'error');
return $form;
}
// Get the admin's name.
//$admin = ucwords(db_result(db_query('SELECT name FROM {users} WHERE uid=1')));
$query1 = db_select('users', 'u');
$query1
->addField('u', 'name');
$query1
->condition('u.uid', '1');
$admin = ucwords($query1
->execute()
->fetchField());
// ---------------------------------------------
// Get the Simplenews newsletters if they exists
$sn_newsletters = array(
'0' => t('No newsletter'),
);
if (module_exists('simplenews')) {
if (!function_exists('simplenews_get_newsletters')) {
drupal_set_message(t('The Simplenews integration is not compatible with this version of Simplenews. Please download a later version.'), 'error');
}
else {
$list = simplenews_get_newsletters(variable_get('simplenews_vid', ''));
foreach ($list as $key => $object) {
$list[$key] = $object->name;
}
$sn_newsletters += $list;
}
}
// ---------------------------------------------
// Get the MailChimp newsletters if they exists
// mailchimp_subscribe_user
$mc_newsletters = array(
'0' => t('No newsletter'),
);
if (module_exists('mailchimp_lists')) {
if (!function_exists('mailchimp_lists_get_available_lists')) {
drupal_set_message(t('The MailChimp integration is not compatible with this version of MailChimp. Please download a later version.'), 'error');
}
else {
$mc_lists = mailchimp_get_lists();
// dpm($mc_lists);
foreach ($mc_lists as $key => $object) {
$mc_lists[$object['id']] = $object['name'];
}
$mc_newsletters += $mc_lists;
}
}
$form['notification'] = array(
'#type' => 'fieldset',
'#title' => t('Notifications'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['notification']['faq_ask_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify experts'),
'#description' => t('If this box is checked, the expert(s) for the question will be notified via email that a question awaits them. If you do not choose this option, the "Unanswered Questions" block will be the only way they will know they have questions to answer.'),
'#default_value' => variable_get('faq_ask_notify', 0),
);
$form['notification']['notify_asker'] = array(
'#type' => 'fieldset',
'#title' => T('Asker notification'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['notification']['notify_asker']['faq_ask_asker_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify askers'),
'#description' => t('If this box is checked, the asker creating the question will be notified via email that their question is answered.'),
'#default_value' => variable_get('faq_ask_notify_asker', 0),
);
$form['notification']['notify_asker']['faq_ask_asker_notify_cron'] = array(
'#type' => 'checkbox',
'#title' => t('Use cron for asker notification'),
'#description' => t('If this box is checked, the asker notifications will be sendt via cron.'),
'#default_value' => variable_get('faq_ask_notify_by_cron', TRUE),
// '#disabled' => !variable_get('faq_ask_notify_asker', 0),
'#states' => array(
'visible' => array(
':input[name="faq_ask_asker_notify"]' => array(
'checked' => TRUE,
),
),
),
);
$form['notification']['notify_asker']['simplenews'] = array(
'#type' => 'fieldset',
'#title' => t('Simplenews newsletter integration'),
'#collapsible' => TRUE,
'#collapsed' => !module_exists('simplenews'),
);
// If the Simplenews module is loaded we can add functionality to add anonymous askers to a newsletter
$form['notification']['notify_asker']['simplenews']['faq_ask_notify_asker_simplenews'] = array(
'#type' => 'select',
'#title' => t('Add anonymous asker to newsletter'),
'#default_value' => variable_get('faq_ask_notify_asker_simplenews_tid', '0'),
'#options' => $sn_newsletters,
'#description' => module_exists('simplenews') ? t('Select a newsletter you want anonymous askers to be assigned to.') : t('This functionality needs the <a href="http://drupal.org/project/simplenews">Simplenews module</a> to be activated.'),
'#disabled' => !module_exists('simplenews'),
'#states' => array(
'visible' => array(
':input[name="faq_ask_asker_notify"]' => array(
'checked' => TRUE,
),
),
),
);
$form['notification']['notify_asker']['simplenews']['faq_ask_notify_asker_simplenews_confirm'] = array(
'#type' => 'checkbox',
'#title' => t('Confirm subscription to newsletter'),
'#description' => t('If this box is checked, the asker creating the question will be asked to confirm the subscription of the newsletter.'),
'#default_value' => variable_get('faq_ask_notify_asker_simplenews_confirm', 1),
'#disabled' => !module_exists('simplenews'),
'#states' => array(
'visible' => array(
':input[name="faq_ask_asker_notify"]' => array(
'checked' => TRUE,
),
),
),
);
$form['notification']['notify_asker']['mailchimp'] = array(
'#type' => 'fieldset',
'#title' => t('MailChimp newsletter integration'),
'#collapsible' => TRUE,
'#collapsed' => !module_exists('mailchimp_lists'),
);
//dpm($mc_newsletters);
//dpm(variable_get('faq_ask_notify_asker_mailchimp_lid', '0'));
// If the MailChimp module is loaded we can add functionality to add anonymous askers to a newsletter
$form['notification']['notify_asker']['mailchimp']['faq_ask_notify_asker_mailchimp'] = array(
'#type' => 'select',
'#title' => t('Add anonymous asker to newsletter'),
'#default_value' => variable_get('faq_ask_notify_asker_mailchimp_lid', '0'),
'#options' => $mc_newsletters,
'#description' => module_exists('mailchimp_lists') ? t('Select a newsletter you want anonymous askers to be assigned to.') : t('This functionality needs the <a href="http://drupal.org/project/mailchimp_lists">MailChimp module</a> to be activated.'),
'#disabled' => !module_exists('mailchimp_lists'),
'#states' => array(
'visible' => array(
':input[name="faq_ask_asker_notify"]' => array(
'checked' => TRUE,
),
),
),
);
$form['notification']['notify_asker']['mailchimp']['faq_ask_notify_asker_mailchimp_confirm'] = array(
'#type' => 'checkbox',
'#title' => t('Confirm subscription to newsletter'),
'#description' => t('If this box is checked, the asker creating the question will be asked to confirm the subscription of the newsletter.'),
'#default_value' => variable_get('faq_ask_notify_asker_simplenews_confirm', 1),
'#disabled' => !module_exists('mailchimp_lists'),
'#states' => array(
'visible' => array(
':input[name="faq_ask_asker_notify"]' => array(
'checked' => TRUE,
),
),
),
);
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['options']['faq_ask_categorize'] = array(
'#type' => 'checkbox',
'#title' => t('Only expert can categorize'),
'#description' => t('If this box is checked, only an expert answering a question can add a category.'),
'#default_value' => variable_get('faq_ask_categorize', FALSE),
'#weight' => 1,
);
/*
// Issue #1482014 by Matthew Slater (matslats): Assumes vocab called tags
$options = array();
foreach (field_info_instances('node', 'faq') as $fieldname => $instance) {
//could narrow it down to just taxonomy fields
//and even derive the vocab name instead of the fieldname if that's more useful.
$options[$fieldname] = $instance['label'];
}
$form['options']['faq_category_field'] = array(
'#title' => 'Description field',
'#description' => t("Which field API field in the FAQ node bundle should be used for 'categories'"),
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('faq_category_field', 0),
'#weight' => 2,
);
*/
$give_options = array(
0 => t('Asker retains ownerhsip'),
1 => t('Anonymous questions reassigned to expert'),
2 => t('All questions reassigned to expert'),
);
$form['options']['faq_ask_expert_own'] = array(
'#type' => 'radios',
'#options' => $give_options,
'#title' => t('Give ownership to the expert'),
'#description' => t('This determines if questions will be reassigned to the expert when answered.'),
'#default_value' => variable_get('faq_ask_expert_own', 0),
'#weight' => 3,
);
$form['options']['faq_ask_unanswered'] = array(
'#type' => 'textarea',
'#title' => t('Default unanswered body text'),
'#cols' => 60,
'#rows' => 1,
'#description' => t('This text will be inserted into the body of questions when they are asked. This helps make editing easier'),
'#default_value' => variable_get('faq_ask_unanswered', t('Not answered yet.')),
'#weight' => 4,
);
$form['options']['faq_ask_expert_advice'] = array(
'#type' => 'textarea',
'#title' => t('Answer advice for the expert'),
'#cols' => 60,
'#rows' => 1,
'#description' => t('This text will be shown at the bottom of the "Unanswered questions" block.'),
'#default_value' => variable_get('faq_ask_expert_advice', _faq_ask_advice_default()),
'#weight' => 4,
);
$form['options']['advice']['faq_ask_admin_advice'] = array(
'#type' => 'textarea',
'#title' => t('Advice for an administrator/editor'),
'#cols' => 60,
'#rows' => 1,
'#default_value' => variable_get('faq_ask_admin_advice', _faq_ask_advice_default('admin')),
'#weight' => 5,
);
$form['options']['advice']['faq_ask_asker_advice'] = array(
'#type' => 'textarea',
'#title' => t('Advice for an asker'),
'#cols' => 60,
'#rows' => 1,
'#default_value' => variable_get('faq_ask_asker_advice', _faq_ask_advice_default('asker')),
'#weight' => 6,
);
$help_default = variable_get('faq_ask_help_text', _faq_ask_help_default());
$form['options']['faq_ask_help_text'] = array(
'#type' => 'textarea',
'#title' => t('Help text for the asker'),
'#cols' => 60,
'#rows' => drupal_strlen($help_default) / 60,
'#description' => t('This text will be shown at the top of the "Ask a Question" page.'),
'#default_value' => $help_default,
'#weight' => 7,
);
$form['experts'] = array(
'#type' => 'fieldset',
'#title' => t('Experts'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// Use the list of vocabularies from above.
if (count($vocabs) == 1) {
// Single vocabulary, don't bother with a selection box, just set it.
$vid = key($vocabs);
$def_vid = array(
$vid => $vid,
);
variable_set('faq_ask_vocabularies', array(
$vid => $vid,
));
$vobj = $vocabs[$vid];
$free = $vobj->name;
}
else {
// Multiple vocabs available.
$voc_list = array();
// Clear vocabulary list
$def_vid = array();
// Clear default selected list
foreach ($vocabs as $vid => $vobj) {
$voc_list[$vid] = $vobj->name;
// Create selection list
if ($vobj->name == 'FAQ') {
$def_vid[$vid] = $vid;
// Create default selected list
}
}
if (empty($def_vid)) {
// If no default selected vocabs, then default select all of them
$def_vid = array_keys($voc_list);
}
// variable_get('efaq_ask_vocabularies', 0)?
/* Issue #161406 by phazer: Categories not included in the FAQ list are showing up on the Expert Grid
* Changed default vids to reflect an array rather than a separate vocab.
* Also changed the vocab list terms retrieved are based upon
*/
$form['experts']['faq_ask_vocabularies'] = array(
'#type' => 'select',
'#options' => $voc_list,
'#title' => t('Use these vocabularies'),
'#multiple' => TRUE,
'#default_value' => variable_get('faq_ask_vocabularies', $def_vid),
'#description' => t('Only the terms from the selected vocabularies will be included in the list below.') . ' ' . t("Simply adding the 'FAQ' content type to a vocabulary will not make it eligible for experts; you must return to here to add it.") . '<br/><big>' . t('If you select different vocabularies, you must save the configuration BEFORE selecting users below.') . '</big>',
'#weight' => 8,
);
}
// End multiple vocabs.
// Changed query and loop because it failed if 'answer' was the first perm in list.
// This should be faster any way.
$query1 = db_select('role', 'r');
$query1
->join('role_permission', 'p', 'r.rid = p.rid');
$query1
->condition('p.permission', '%%answer question%%', 'LIKE');
$query1
->fields('r', array(
'rid',
'name',
));
$role_object_list = $query1
->execute()
->fetchAllAssoc('rid');
$role_list = array();
foreach ($role_object_list as $role_object) {
$role_list[$role_object->rid] = $role_object->name;
}
if (empty($role_list)) {
drupal_set_message(t('No roles with "answer question" permission were found; only @admin is currently eligible to be an expert. You may want to go to the <a href="@access">Permissions page</a> to update your permissions.', array(
'@access' => url('admin/user/permissions'),
'@admin' => $admin,
), array(
'langcode' => 'en',
)), 'error');
}
// Get all terms associated with FAQ.
$vocabs_array = variable_get('faq_ask_vocabularies', $def_vid);
$result = db_select('taxonomy_term_data', 'td')
->condition('td.vid', $vocabs_array, 'IN')
->fields('td', array(
'tid',
'name',
'description',
))
->orderBy('td.weight')
->orderBy('td.name')
->execute()
->fetchAllAssoc('tid');
$faq_terms = array();
foreach ($result as $term) {
// Show term hierarchy?
$term_name = check_plain($term->name);
if (substr($term->description, 0, 9) == 'suggested') {
$faq_terms[$term->tid] = $term_name . '<br/>--<small>' . strip_tags($term->description) . '</small>';
}
else {
$faq_terms[$term->tid] = $term_name;
}
}
if (count($faq_terms) == 0) {
drupal_set_message(t('No vocabularies or terms were found for the "faq" content type . Please go to the <a href="@access">Categories page</a> to update your vocabulary.', array(
'@access' => url('admin/structure/taxonomy'),
)), 'error');
return $form;
}
// Get all users associated with the roles.
$faq_expert_names = array();
// User/1 typically is not assigned roles, but should be in the list.
$faq_expert_names[1] = $admin;
$rids = variable_get('faq_expert_role', array());
if (!empty($rids)) {
if (in_array(DRUPAL_AUTHENTICATED_RID, $rids)) {
// Authenticated users may be experts, so get all active users.
// No other roles matter.
//$result = db_query("SELECT u.uid, u.name FROM {users} u WHERE status=1");
$result = db_select('users', 'u')
->condition('status', 1)
->fields('u', array(
'uid',
'name',
))
->execute()
->fetchAllKeyed();
}
else {
// Only specific roles may be experts.
//$result = db_query('SELECT DISTINCT(u.uid), u.name FROM {users_roles} ur JOIN {users} u USING (uid) WHERE ur.rid IN (' . db_placeholders($rids) . ')', $rids);
$query = db_select('users_roles', 'ur');
$query
->join('users', 'u', 'ur.uid = u.uid');
$query = $query
->condition('ur.rid', $rids, 'IN')
->fields('u', array(
'uid',
'name',
))
->distinct();
$result = $query
->execute()
->fetchAllKeyed();
}
foreach ($result as $uid => $name) {
if ($uid != 1) {
$faq_expert_names[$uid] = ucwords($name);
}
}
//while ($user = db_fetch_array($result)) {
// if ($user['uid'] != 1) {
// $faq_expert_names[$user['uid']] = ucwords($user['name']);
// }
//}
// Put them in alphabetical order.
asort($faq_expert_names);
}
if (!empty($role_list)) {
$form['experts']['faq_expert_role'] = array(
'#type' => 'select',
'#title' => t('Expert Roles'),
'#options' => $role_list,
'#multiple' => TRUE,
'#default_value' => variable_get('faq_expert_role', '2'),
'#description' => t('User 1 (@admin) will always be in the list, regardless of roles.', array(
'@admin' => $admin,
)) . '<br/><big>' . t('If you select different roles, you must save the configuration BEFORE selecting users below.') . '</big>',
'#weight' => 9,
);
}
$more_experts_than_terms = count($faq_expert_names) > count($faq_terms);
// If there is only one eligible expert, we might as well preset all categories.
$expert_msg = NULL;
$only_one_expert = count($faq_expert_names) == 1;
$count = 0;
if ($more_experts_than_terms) {
// Experts go down the left; terms go across the top.
$top = NULL;
if ($only_one_expert) {
$top .= '<p>' . t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') . '</p>';
}
$top .= '<table id="faq_experts"><tr><th> </th><th>' . implode('</th><th>', $faq_terms) . '</th></tr>';
if ($only_one_expert) {
$top .= '<tr><td colspan="100">' . t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') . '</td></tr>';
}
foreach ($faq_expert_names as $uid => $name) {
++$count;
$class = $count & 1 ? 'odd' : 'even';
$left = '<tr class="' . $class . '"><td><strong>' . $name . '</strong></td>';
foreach ($faq_terms as $tid => $term_name) {
$box_name = 'expert_' . $uid . '_' . $tid;
$form['experts'][$box_name] = array(
'#type' => 'checkbox',
'#default_value' => $only_one_expert,
'#prefix' => $top . $left . '<td align="center">',
'#suffix' => '</td>',
);
$top = NULL;
$left = NULL;
}
$form['experts'][$box_name]['#suffix'] .= '</tr>';
}
$form['experts'][$box_name]['#suffix'] .= '</table>';
}
else {
// Experts go across the top; terms go down the left.
$top = NULL;
if ($only_one_expert) {
$top .= '<p>' . t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') . '</p>';
}
$top .= '<table id="faq_experts"><tr><th> </th><th>' . implode('</th><th>', $faq_expert_names) . '</th></tr>';
foreach ($faq_terms as $tid => $term_name) {
++$count;
$class = $count & 1 ? 'odd' : 'even';
$left = '<tr class="' . $class . '"><td><strong>' . $term_name . '</strong></td>';
foreach ($faq_expert_names as $uid => $name) {
$box_name = 'expert_' . $uid . '_' . $tid;
$form['experts'][$box_name] = array(
'#type' => 'checkbox',
'#default_value' => $only_one_expert,
'#prefix' => $top . $left . '<td align="center">',
'#suffix' => '</td>',
);
$top = NULL;
$left = NULL;
}
$form['experts'][$box_name]['#suffix'] .= '</tr>';
}
$form['experts'][$box_name]['#suffix'] .= '</table>';
}
$form['experts'][$box_name]['#suffix'] .= t('Those who will be answering questions will need both "answer question" and "edit faq" permissions.');
//$result = db_query("SELECT * FROM {faq_expert}");
$result = db_select('faq_expert', 'fe')
->fields('fe', array(
'uid',
'tid',
))
->execute()
->fetchAll();
foreach ($result as $expert) {
$box_name = 'expert_' . $expert->uid . '_' . $expert->tid;
if (isset($form['experts'][$box_name])) {
// Might not be present any more.
$form['experts'][$box_name]['#default_value'] = TRUE;
}
else {
// Expert 0 means default expert; overlook it.
if ($expert->tid != 0) {
drupal_set_message(t("@name doesn't exist. If you have just changed your role selections this may be okay.", array(
'@name' => $box_name,
)), 'warning');
}
}
}
//while ($expert = db_fetch_array($result)) {
// $box_name = 'expert_' . $expert['uid'] . '_' . $expert['tid'];
// if (isset($form['experts'][$box_name])) { // Might not be present any more.
// $form['experts'][$box_name]['#default_value'] = TRUE;
// }
// else {
// // Expert 0 means default expert; overlook it.
// if ($expert['tid'] != 0) {
// drupal_set_message(t("!name doesn't exist. If you have just changed your role selections this may be okay.", array('!name' => $box_name)), 'warning');
// }
// }
//}
if ($only_one_expert) {
// Create a form value to set default expert to admin.
$form['experts']['faq_ask_default_expert'] = array(
'#type' => 'value',
'#value' => 1,
);
}
else {
$form['experts']['faq_ask_default_expert'] = array(
'#type' => 'select',
'#options' => $faq_expert_names,
'#multiple' => FALSE,
'#title' => t('Default expert'),
'#description' => t('The selected user will be assigned as the expert for all terms that are added to the selected vocabularies until you return to this page and update it.'),
'#default_value' => variable_get('faq_ask_default_expert', 1),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
'#submit' => array(
'faq_ask_settings_form_submit',
),
);
// Get rid of error element.
unset($form['error']);
return $form;
}