? DIFF
? faq_ask-421804.patch
Index: faq_ask.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/faq_ask/faq_ask.install,v
retrieving revision 1.11
diff -u -p -r1.11 faq_ask.install
--- faq_ask.install	24 Nov 2008 20:13:23 -0000	1.11
+++ faq_ask.install	15 Apr 2009 17:12:16 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: faq_ask.install,v 1.11 2008/11/24 20:13:23 nancyw Exp $
+// $Id: faq_ask.install,v 1.4.2.7 2008/11/24 20:13:07 nancyw Exp $
 
 /**
  * @file
@@ -14,44 +14,24 @@ function faq_ask_schema() {
   $schema['faq_expert'] = array(
     'description' => t('FAQ expert to term mapping.'),
     'fields' => array(
-      'uid' => array(
-        'description' => t('User identifier for the expert.'),
+      'rid' => array(
+        'description' => t('Role identifier for the expert role.'),
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         ),
       'tid' => array(
-        'description' => t('Taxonomy identifier of the terms for the expert.'),
+        'description' => t('Taxonomy identifier of the terms for the expert role.'),
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         ),
       ),
-    'primary key' => array('uid', 'tid'),
+    'primary key' => array('rid', 'tid'),
     'indexes' => array(
-      'tid' => array('tid', 'uid'),
+      'tid' => array('tid', 'rid'),
       ),
     );
-
-  $schema['faq_questions'] = array(
-    'fields' => array(
-      'nid' => array('type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'disp-width' => '10'),
-      'vid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'disp-width' => '10'),
-      'question' => array(
-        'type' => 'text',
-        'not null' => TRUE),
-      ),
-    'primary key' => array('nid', 'vid'),
-);
   return $schema;
 }
 
@@ -59,41 +39,23 @@ function faq_ask_schema() {
  * Implementation of hook_install().
  */
 function faq_ask_install() {
-  // Note: I did not specify defaults here because an insert should fail if a value is not supplied.
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $result = db_query("CREATE TABLE IF NOT EXISTS {faq_expert} (
-        uid INT(10) UNSIGNED NOT NULL,
-        tid INT(10) UNSIGNED NOT NULL,
-        PRIMARY KEY (uid, tid),
-        INDEX tid (tid, uid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-      break;
-
-    case 'pgsql':
-      $result = db_query("CREATE TABLE {faq_expert} (
-        uid integer NOT NULL,
-        tid integer NOT NULL,
-        PRIMARY KEY (uid, tid)
-      )");
-      db_query("CREATE INDEX {faq_expert}_tid ON {faq_expert} (tid, uid)");
-      break;
-  }
+  $result = drupal_install_schema('faq_ask');
 
-  if ($result) {
+  if (count($result) > 0) {
     drupal_set_message(t('faq_ask module installed.'));
   }
-  else { drupal_set_message(t('faq_ask table creation failed. Please "uninstall" the module and retry.')); }
+  else {
+    drupal_set_message(t('faq_ask table creation failed. Please "uninstall" the module and retry.'));
+  }
 }
 
 /**
  * Implementation of hook_update_N().
  */
-function faq_ask_update_5100() {
+function faq_ask_update_6100() {
   $ret = array();
 
-  $ret[] = update_sql('INSERT INTO {faq_expert} (uid, tid) VALUES ('. variable_get('faq_ask_default_expert', 1) .', 0)'); 
+ // $ret[] = update_sql('INSERT INTO {faq_expert} (uid, tid) VALUES ('. variable_get('faq_ask_default_expert', 1) .', 0)'); 
 
   return $ret;
 }
@@ -102,7 +64,7 @@ function faq_ask_update_5100() {
  * Implementation of hook_uninstall().
  */
 function faq_ask_uninstall() {
-  db_query('DROP TABLE {faq_expert}');
+  drupal_uninstall_schema('faq_ask');
 
   variable_del('faq_expert_role');
   variable_del('faq_ask_vocabularies');
@@ -115,6 +77,6 @@ function faq_ask_uninstall() {
   variable_del('faq_ask_help_text');
   variable_del('faq_ask_categorize');
   variable_del('faq_ask_expert_own');
-
+  
   drupal_set_message(t('faq_ask module uninstalled.'));
 }
Index: faq_ask.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/faq_ask/faq_ask.module,v
retrieving revision 1.43
diff -u -p -r1.43 faq_ask.module
--- faq_ask.module	28 Feb 2009 19:48:41 -0000	1.43
+++ faq_ask.module	15 Apr 2009 17:12:17 -0000
@@ -362,7 +362,7 @@ function faq_ask_form_submit($form_id, $
     // Are we notifying the expert(s)?
     if (variable_get('faq_ask_notify', FALSE)) {
       // Find out who the experts are.
-      $result = db_query("SELECT u.mail FROM {faq_expert} e JOIN {users} u USING (uid) WHERE e.tid=%d", $category);
+      $result = db_query('SELECT DISTINCT(users.mail) FROM (users_roles JOIN faq_expert ON users_roles.rid=faq_expert.rid) RIGHT JOIN users ON users_roles.uid=users.uid WHERE faq_expert.tid=%s', $category);
       $to_addresses = array();
       while ($expert = db_fetch_array($result)) {
         $to_addresses[] = $expert['mail'];
@@ -384,6 +384,7 @@ function faq_ask_form_submit($form_id, $
       else {
         $body = t('The following question has been posted in the "!cat" category.', array('!cat' => filter_xss($term->name)));
       }
+
       $body .= "\n<br/><br/>". $form_values['title'];
       // The URLs in the following are contructed as absolute addresses.
       $body .= "\n<br/><br/>". t('In order to answer it you will first need to <a href="!url">login</a> to the site.', array('!url' => url('user', NULL, NULL, TRUE)));
@@ -603,7 +604,7 @@ function faq_ask_settings_form($op = NUL
       '#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>',
+        .'<br/><big>'. t('If you select different vocabularies, you must save the configuration BEFORE selecting user roles below.') .'</big>',
       );
   } // End multiple vocabs.
 
@@ -649,69 +650,32 @@ function faq_ask_settings_form($op = NUL
     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/content/taxonomy'))), 'warning');
   }
 
-  // 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);
+  $more_expert_roles_than_terms = count($role_list) > 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);
-  
+  $only_one_expert_role = (count($role_list) == 1);
+
   $count = 0;
-  if ($more_experts_than_terms) {
+  if ($more_expert_roles_than_terms) {
     // Experts go down the left; terms go across the top.
     $top = NULL;
-    if ($only_one_expert) {
+    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>', $faq_terms) .'</th></tr>';
-    if ($only_one_expert) {
+    if ($only_one_expert_role) {
       $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) {
+    foreach ($role_list as $rid => $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;
+        $box_name = 'expert_'. $rid .'_'. $tid;
         $form['experts'][$box_name] = array(
           '#type' => 'checkbox',
-          '#default_value' => $only_one_expert,
+          '#default_value' => $only_one_expert_role,
           '#prefix' => $top . $left .'<td align="center">',
           '#suffix' => '</td>',
           );
@@ -725,19 +689,19 @@ function faq_ask_settings_form($op = NUL
   else {
     // Experts go across the top; terms go down the left.
     $top = NULL;
-    if ($only_one_expert) {
+    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>', $faq_expert_names) .'</th></tr>';
+    $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 ($faq_expert_names as $uid => $name) {
-        $box_name = 'expert_'. $uid .'_'. $tid;
+      foreach ($role_list as $rid => $name) {
+        $box_name = 'expert_'. $rid .'_'. $tid;
         $form['experts'][$box_name] = array(
           '#type' => 'checkbox',
-          '#default_value' => $only_one_expert,
+          '#default_value' => $only_one_expert_role,
           '#prefix' => $top . $left .'<td align="center">',
           '#suffix' => '</td>',
           );
@@ -753,7 +717,7 @@ function faq_ask_settings_form($op = NUL
 
   $result = db_query("SELECT * FROM {faq_expert}");
   while ($expert = db_fetch_array($result)) {
-    $box_name = 'expert_'. $expert['uid'] .'_'. $expert['tid'];
+    $box_name = 'expert_'. $expert['rid'] .'_'. $expert['tid'];
     if (isset($form['experts'][$box_name])) { // Might not be present any more.
       $form['experts'][$box_name]['#default_value'] = TRUE;
     }
@@ -765,21 +729,21 @@ function faq_ask_settings_form($op = NUL
     }
   }
 
-  if ($only_one_expert) {
+  if ($only_one_expert_role) {
     // Create a form value to set default expert to admin.
-    $form['experts']['faq_ask_default_expert'] = array(
+    $form['experts']['faq_ask_default_expert_role'] = array(
       '#type' => 'value',
-      '#value' => 1,
+      '#value' => 'all',
       );
   }
   else {
-    $form['experts']['faq_ask_default_expert'] = array(
+    $form['experts']['faq_ask_default_expert_role'] = array(
       '#type' => 'select',
-      '#options' => $faq_expert_names,
+      '#options' => $role_list + array('all'=>'all','none'=>'none'),
       '#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),
+      '#title' => t('Default expert role'),
+      '#description' => t('The selected user role 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_role', 'all'),
       );
   } 
 
@@ -815,12 +779,12 @@ function faq_ask_settings_form_submit($f
 
   // Get all the selected expert/category options.
   // First, we'll include the default expert for tid=0.
-  $values = array('('. $form_values['faq_ask_default_expert'] .', 0)');
+  $values = array();
   foreach ($form_values as $name => $value) {
     if (substr($name, 0, 7) == 'expert_') {
-      list($junk, $uid, $tid) = explode('_', $name);
+      list($junk, $rid, $tid) = explode('_', $name);
       if ($value) {
-        $values[] = '('. $uid .', '. $tid .')';
+        $values[] = '('. $rid .', '. $tid .')';
       }
     }
   }
@@ -829,7 +793,7 @@ function faq_ask_settings_form_submit($f
   // Delete the current values and save the new ones.
   if (!empty($values)) {
     db_query('TRUNCATE {faq_expert}');
-    db_query("INSERT INTO {faq_expert} (uid, tid) VALUES %s", $vals);
+    db_query("INSERT INTO {faq_expert} (rid, tid) VALUES %s", $vals);
   }
 
   drupal_set_message(t('Configuration has been updated.'), 'notice');
@@ -882,7 +846,7 @@ function faq_ask_reassign(&$node) {
  */
 function faq_ask_taxonomy($op, $type, $array = NULL) {
   global $user;
-  $default_expert = variable_get('faq_ask_default_expert', 1);
+  $default_expert = variable_get('faq_ask_default_expert_role', 'all');
   $my_vocs = variable_get('faq_ask_vocabularies', array());
   $vid = $array['vid'];
   // See if it's one of our vocabularies.
@@ -894,12 +858,37 @@ function faq_ask_taxonomy($op, $type, $a
         case 'term':
           // term: set default expert.
           if ($our_vocab) {
-            $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');
+            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();
+            }
+            $tid = $array['tid'];
+            $tname = $array['name'];
+            if (!$tdesc = $array['description']) {
+              $array['description'] = t('Suggested by @name (!uid) on @date',
+                array('@name' => $user->name, '!uid' => $user->uid,  '@date' => format_date(time(), 'small'))
+                );
+              $tdesc = $array['description'];
+              // Update the description.
+              drupal_write_record('term_data', $array, 'tid');
             }
-            else {
-              drupal_set_message(t('Assigned expert @expert to @name (@tid).', array('@expert' => $default_expert, '@name' => $array['name'], '@tid' => $array['tid'])), 'notice');
+            drupal_set_message("inserting $tid ($tname) into $vid => '$tdesc'.");
+            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');
+                }
+              }
             }
           }
           break;
@@ -1126,7 +1115,16 @@ function _faq_ask_list_unanswered($limit
       $extra_msg = '<p class="faq_ask_expert_advice">'. variable_get('faq_ask_expert_advice', _faq_ask_advice_default('expert')) .'</p>';
     }
     // Get the expert's terms.
-    $terms = db_result_array(db_query('SELECT tid FROM {faq_expert} WHERE uid=%d', $user->uid));
+    $terms = array();
+    if ($user->uid == 1) {
+      $t_res = 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 {
+      $t_res = 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);
+    }
+    while ($row = db_fetch_array($t_res)) {
+      $terms[$row['tid']] = $row['tid'];
+    }
 
     // Check if this expert has any categories.
     if (count($terms) == 0) {
@@ -1138,10 +1136,10 @@ function _faq_ask_list_unanswered($limit
       }
     }
     // Join the term_data table to select based on tid.
-    $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);
   }
   elseif ($can_edit) {
-    $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);
     if ($limit < 1000) {
       $extra_msg = '<p class="faq_ask_expert_advice">'. variable_get('faq_ask_admin_advice', _faq_ask_advice_default('admin')) .'</p>';
     }
@@ -1213,4 +1211,4 @@ function faq_ask_list_more() {
   drupal_set_title(t('All Unanswered Questions'));
   $output = '<br/>';
   return _faq_ask_list_unanswered(9999999);
-}
\ No newline at end of file
+}
