--- modulesold\search.module	Thu Sep 22 22:25:11 2005
+++ modules\search.module	Tue Nov 29 08:43:36 2005
@@ -1,5 +1,5 @@
 <?php
-// $Id: search.module,v 1.1.1.1 2005/09/22 21:25:11 Marand Exp $
+// $Id: search.module,v 1.1.1.1 2005/09/22 21:25:09 Marand Exp $
 
 /**
  * @file
@@ -72,18 +72,35 @@ function search_perm() {
 /**
  * Implementation of hook_block().
  */
-function search_block($op = 'list', $delta = 0) {
+function search_block($op = 'list', $delta = 0) 
+  {
   global $user;
-  if ($op == 'list') {
+  if ($op == 'list') 
+    {
     $blocks[0]['info'] = t('Search form');
+    $blocks[1]['info'] = t('Recent searches');
     return $blocks;
+    }
+  else if ($op == 'view')
+    {
+    switch ($delta)
+      {
+      case 0: 
+        if (user_access('search content') && (arg(0) != 'search')) 
+          {
+          $block['content'] = search_form('', '', null, '');
+          $block['subject'] = t('Search');
+          return $block;
+          }
+        else 
+          return NULL;
+      case 1:
+        $block['content'] = _search_recent();
+        $block['subject'] = t('Recent searches');
+        return $block;
+      }
+    }
   }
-  else if ($op == 'view' && user_access('search content') && arg(0) != 'search') {
-    $block['content'] = search_form('', '', null, '');
-    $block['subject'] = t('Search');
-    return $block;
-  }
-}
 
 /**
  * Implementation of hook_menu().
@@ -852,5 +869,32 @@ function theme_search_item($item, $type)
   return $output;
 }
 
-
+/**
+ * Ancillary function for search_block: builds a block of recent search terms
+ * @return string themed block
+ *
+ */
+function _search_recent()
+  {
+  $sq = "
+  SELECT
+    distinct message query
+  FROM
+    {watchdog}
+  WHERE
+    (type = 'search') AND (message like '%(content).')
+  ORDER BY timestamp DESC
+  " ;
+  $q = db_query_range($sq, 0, 5);
+  $ar = array();
+  while ($o = db_fetch_object($q))
+    {  
+    $term = substr(substr($o->query, 12),0,-16);
+    $ar[] = l($term, 'search/node/' . $term);
+    }
+  $output = theme_item_list($ar, NULL);
+  unset ($ar);
+  return $output ;
+  }
+  
 ?>
