210a211,212
>   $default_tid = variable_get('faq_ask_default_terms',array());
> 
219c221
<       $default_tid = 0;
---
>       $default_tid = variable_get('faq_ask_default_terms',array());
233d234
<     $default_tid = $tid;
246,255c247,265
<   if (!$expert_categorize) {
<     $form['category'] = array(
<       '#title' => t('Category'),
<       '#type' => 'select',
<       '#options' => $cat_list,
<       '#default_value' => $default_tid,
<       '#required' => TRUE,
<       '#weight' => -3,
<       '#description' => t('Please select the correct category for your question.'),
<       );
---
>   $default_tid = array_keys($default_tid);
>   $default_tid = $default_tid[0];
>   $form['category'] = array(
>     //'#title' => t('Category'),
>     '#type' => 'select',
>     '#options' => $cat_list,
>     '#default_value' => $default_tid,
>     '#required' => TRUE,
>     '#multiple' => TRUE,
>     '#weight' => -3,
>     //'#description' => t('Please select the correct category for your question.'),
>     );
>   if ($expert_categorize) {
>     if (array_key_exists('#attributes',$form['category'])) {
>       $form['category']['#attributes']['style'] = 'display:none';
>     }
>     else {
>       $form['category']['#attributes'] = array('style'=>'display:none');
>     }
258,261c268,269
<     $form['category'] = array(
<       '#type' => 'value',
<       '#value' => -1,
<       );
---
>     $form['category']['#title'] = t('Category');
>     $form['category']['#description'] = t('Please select the correct category for your question.');
263d270
< 
335,337d341
<     else {
<       $term = taxonomy_get_term($category);
<     }
343c347
<       'taxonomy' => $category >= 0 ? array($category => $term) : NULL,
---
>       'taxonomy' => NULL,
351a356,366
>       if ($category != 0 AND is_array($category)) {
>         $node['taxonomy'] = array();
>         foreach ($category as $tid=>$val) {
>           $t = taxonomy_get_term($tid);
>           $node['taxonomy'][$tid] = $t;
>         }
>       }
>       else {
>         $node['taxonomy'] = array($category=>taxonomy_get_term($category));
>       }
>     $term = $node['taxonomy'];
369c384
<         'category' => $category >= 0 ? filter_xss($term->name) : -1,
---
>         'category' => -1,
372a388,399
>       if ($category >= 0) {
>         if (is_array($term)) {
>           $term_names = array();
>           foreach ($term as $t) {
>             $term_names[] = $t->name;
>           }
>           $params['category'] = filter_xss(implode (', ',$term_names));
>         }
>         else {
>           $params['category'] = filter_xss($term->name);
>         }
>       }
374c401
<       $result = db_query('SELECT uid FROM {faq_expert} WHERE tid=%d', $category);
---
>       $result = db_query('SELECT DISTINCT(users_roles.uid) FROM (users_roles JOIN faq_expert ON users_roles.rid=faq_expert.rid) WHERE faq_expert.tid=%d', $category);
383a411,422
>           $term_names = array();
>           if (is_array($term)) {
>             foreach ($term as $t) {
>               $term_names[] = $t->name;
>             }
>             $term_names = implode (', ',$term_names);
>           }
>           else {
>             $term_names = $term->name;
>           }
>           drupal_set_message($term_names);
>           drupal_set_message(filter_xss($term_names));
385c424
<             array('@to' => $account->mail, '@cat' => filter_xss($term->name)), WATCHDOG_ERROR);
---
>             array('@to' => $account->mail, '@cat' => filter_xss($term_names)), WATCHDOG_ERROR);
429c468,474
<     drupal_goto('faq'. ($category > 0 ? '/'. $category : NULL));
---
>     /*//This isn't working right now, and since I hate it anyway, I commented it out instead of fixing it.  I have no remorse.
>     if (is_array($category)) {
>       drupal_goto('faq'. ($category > 0 ? '/'.implode('+',$category) : NULL));
>     }
>     else {
>       drupal_goto('faq'. ($category > 0 ? '/'.$category : NULL));
>     }//*/
457c502,504
<         $body = t('The following question has been posted.', NULL, $language->language);
---
> 	//Updated by JB on 03/19/09:
>         $body[] = t('The following question has been posted.', NULL, $language->language);
>         //$body = t('The following question has been posted.', NULL, $language->language);
460c507,509
<         $body = t('The following question has been posted in the "!cat" category.', array('!cat' => filter_xss($term->name)));
---
>       	//Updated by JB on 03/19/09:
>         $body[] = t('The following question has been posted in the "!cat" category.', array('!cat' => filter_xss($term->name)));
>         //$body = t('The following question has been posted in the "!cat" category.', array('!cat' => filter_xss($term->name)));
526a576,588
>   $cat_sql = db_rewrite_sql('SELECT DISTINCT(t.name), e.tid FROM {term_data} t JOIN {faq_expert} e USING (tid)', 't', 'tid');
>   $cat_list = array_flip(db_result_array(db_query($cat_sql)));
>   $form['options']['faq_ask_default_terms'] = array(
>       '#title' => t('Default categories'),
>       '#type' => 'select',
>       '#multiple'=>TRUE,
>       '#options' => $cat_list,
>       '#default_value' => variable_get('faq_ask_default_terms',array()),
>       '#required' => FALSE,
>       '#weight' => -3,
>       '#description' => t('Please select the correct category for your question.'),
>   );  
> 
528c590
<     0 => t('Asker retains ownerhsip'),
---
>     0 => t('Asker retains ownership'),
643,646c705
<   $role_list = db_result_array(db_query("SELECT r.rid, r.name FROM {role} r JOIN {permission} p USING (rid) WHERE p.perm LIKE '%%answer question%%'"));
<   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)), 'error');
<   }
---
>   $role_list = db_result_array(db_query("SELECT role.rid, role.name FROM role JOIN permission ON role.rid=permission.rid WHERE permission.perm LIKE '%%answer question%%'"));
674,710d732
<   // 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");
<     }
<     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);
<     }
<     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>',
<       );
<   }
< 
<   $more_experts_than_terms = count($faq_expert_names) > count($faq_terms);
714c736
<   $only_one_expert = (count($faq_expert_names) == 1);
---
>   $only_one_expert_role = (count($role_list) == 1);
717,768c739,758
<   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>';
---
>   // Experts go across the top; terms go down the left.
>   $top = NULL;
>   if ($only_one_expert_role) {
>     $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>', $role_list) .'</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 ($role_list as $rid => $name) {
>       $box_name = 'expert_'. $rid .'_'. $tid;
>       $form['experts'][$box_name] = array(
>         '#type' => 'checkbox',
>         '#default_value' => $only_one_expert_role,
>         '#prefix' => $top . $left .'<td align="center">',
>         '#suffix' => '</td>',
>         );
>       $top = NULL;
>       $left = NULL;
770c760
<     $form['experts'][$box_name]['#suffix'] .= '</table>';
---
>   $form['experts'][$box_name]['#suffix'] .= '</tr>';
771a762
>   $form['experts'][$box_name]['#suffix'] .= '</table>';
777c768
<     $box_name = 'expert_'. $expert['uid'] .'_'. $expert['tid'];
---
>     $box_name = 'expert_'. $expert['rid'] .'_'. $expert['tid'];
789c780
<   if ($only_one_expert) {
---
>   if ($only_one_expert_role) {
791c782
<     $form['experts']['faq_ask_default_expert'] = array(
---
>     $form['experts']['faq_ask_default_expert_role'] = array(
793c784
<       '#value' => 1,
---
>       '#value' => 'all',
797c788
<     $form['experts']['faq_ask_default_expert'] = array(
---
>     $form['experts']['faq_ask_default_expert_role'] = array(
799,801c790,792
<       '#options' => $faq_expert_names,
<       '#multiple' => FALSE,
<       '#title' => t('Default expert'),
---
>       '#options' => $role_list + array('all'=>'all','none'=>'none'),
>       '#multiple' => TRUE,
>       '#title' => t('Default expert role'),
803c794
<       '#default_value' => variable_get('faq_ask_default_expert', 1),
---
>       '#default_value' => variable_get('faq_ask_default_expert_role', 'all'),
836c827
<   variable_set('faq_ask_default_expert', $form_state['values']['faq_ask_default_expert']);
---
>   variable_set('faq_ask_default_expert_role', $form_state['values']['faq_ask_default_expert_role']);
841a833,835
>   //Added for new default terms:
>   variable_set('faq_ask_default_terms',$form_state['values']['faq_ask_default_terms']);
> 
844c838,839
<   $values = array('('. $form_state['values']['faq_ask_default_expert'] .', 0)');
---
>   //$values = array('('. $form_state['values']['faq_ask_default_expert_role'] .', 0)');
>   $values = array();
847c842
<       list($junk, $uid, $tid) = explode('_', $name);
---
>       list($junk, $rid, $tid) = explode('_', $name);
849c844
<         $values[] = '('. $uid .', '. $tid .')';
---
>         $values[] = '('. $rid .', '. $tid .')';
858c853
<     db_query("INSERT INTO {faq_expert} (uid, tid) VALUES %s", $vals);
---
>     db_query("INSERT INTO {faq_expert} (rid, tid) VALUES %s", $vals);
861c856
<   drupal_set_message(t('Configuration has been updated.'), 'status');
---
>   //drupal_set_message(t('Configuration has been updated.'), 'status');
885a881
>   ////THERE HAS GOT TO BE A BETTER WAY TO DO THIS!!!:
911c907
<   $default_expert = variable_get('faq_ask_default_expert', 1);
---
>   $default_expert = variable_get('faq_ask_default_expert_role', 'all');
923,925c919,928
<             $insert = db_query("INSERT INTO {faq_expert} (uid, tid) VALUES (%d, %d)", $default_expert, $array['tid']);
<             if ($insert === FALSE) {
<               drupal_set_message(t('Attempt to assign expert failed.'), 'error');
---
>             //This part needed to change in order to handle our new "all" and "none" selector choices.
>             if ($default_expert == 'all' || (is_array($default_expert) && in_array('all',$default_expert)) ){
>               $default_expert = array();
>               $res = db_query("SELECT DISTINCT(rid) FROM permission WHERE permission.perm LIKE '%%answer question%%'");
>               while ($new = db_fetch_array($res) ) {
>                 $default_expert[] = $new['rid'];
>               }
>             }
>             elseif ($default_expert == 'none') {
>               $default_expert = array();
927,928c930,939
<             else {
<               drupal_set_message(t('Assigned expert @expert to @name (@tid).', array('@expert' => $default_expert, '@name' => $array['name'], '@tid' => $array['tid'])), 'status');
---
>             foreach ($default_expert as $de) {
>               if ($de && $de !== 'none') {
>                 $insert = db_query("INSERT INTO {faq_expert} (rid, tid) VALUES (%d, %d)", $de, $array['tid']);
>                 if ($insert === FALSE) {
>                   drupal_set_message(t('Attempt to assign expert failed.'), 'error');
>                 }
>                 else {
>                   drupal_set_message(t('Assigned expert @expert to @name (@tid).', array('@expert' => $default_expert, '@name' => $array['name'], '@tid' => $array['tid'])), 'status');
>                 }
>               }
1152c1163
<       $extra_msg = '<p class="faq_ask_expert_advice">'. variable_get('faq_ask_expert_advice', _faq_ask_advice_default('expert')) .'</p>';
---
>       $extra_msg = '<p class="faq_ask_expert_advice">'. variable_get('faq_ask_expert_advice', _faq_ask_advice_default('expert')) .' '.l('See more','faq_ask/more').'</p>';
1155c1166,1171
<     $terms = db_result_array(db_query('SELECT tid FROM {faq_expert} WHERE uid=%d', $user->uid));
---
>     if ($user->uid == 1) {
>       $terms = db_result_array(db_query("SELECT faq_expert.tid FROM (users_roles JOIN permission ON permission.rid=users_roles.rid AND permission.perm LIKE '%%answer question%%') JOIN faq_expert ON faq_expert.rid=users_roles.rid"));
>     }
>     else {
>       $terms = db_result_array(db_query("SELECT faq_expert.tid FROM (users_roles JOIN permission ON permission.rid=users_roles.rid AND users_roles.uid=%d AND permission.perm LIKE '%%answer question%%') JOIN faq_expert ON faq_expert.rid=users_roles.rid",$user->uid));
>     }
1167c1183
<     $result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n LEFT JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND (tn.tid IN (". db_placeholders($terms) .") OR tn.tid IS NULL) ORDER BY ". $order, $terms);
---
>     $result = db_query("SELECT DISTINCT n.title, n.nid, tn.tid FROM {node} n LEFT JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND (tn.tid IN (". db_placeholders($terms) .") OR tn.tid IS NULL) GROUP BY n.nid ORDER BY ". $order, $terms);
1170c1186
<     $result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 ORDER BY ". $order, $terms);
---
>     $result = db_query("SELECT DISTINCT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 GROUP BY n.nid ORDER BY ". $order, $terms);
1176c1192
<     $result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND n.uid=%d ORDER BY ". $order, $user->uid);
---
>     $result = db_query("SELECT DISTINCT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND n.uid=%d GROUP BY n.nid ORDER BY ". $order, $user->uid);
1181d1196
< 
1187d1201
< 
1230d1243
< 
