[LON-CAPA-cvs] cvs: loncom /metadata_database searchcat.pl /metadata_database/LONCAPA lonmetadata.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Wed, 25 Jul 2007 23:17:49 -0000


This is a MIME encoded message

--raeburn1185405469
Content-Type: text/plain

raeburn		Wed Jul 25 19:17:49 2007 EDT

  Modified files:              
    /loncom/metadata_database	searchcat.pl 
    /loncom/metadata_database/LONCAPA	lonmetadata.pm 
  Log:
  searchcat.pl 
  - Additional MySQL table to build: allusers
  - &descend_tree() arguments changed. $dom added as first arg, and reference to %allusers hash added as last arg.
  - %allusers keys are usernames for all users in the domain which are not courses.
  - user information put into allusers MySQL table.
  
  LONCAPA/lonmetadata.pm
  - description of allusers table
  - &update_metadata(), &lookup_metadata() and &delete_metadata() modified to allow more flexibility in the WHERE condition in the SQL query (no longer forced to be url = ).
  - &process_allusers_data() added to add/modify the contents of the allusers table.      
  
  
--raeburn1185405469
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070725191749.txt"

Index: loncom/metadata_database/searchcat.pl
diff -u loncom/metadata_database/searchcat.pl:1.76 loncom/metadata_database/searchcat.pl:1.77
--- loncom/metadata_database/searchcat.pl:1.76	Wed Apr 11 18:44:18 2007
+++ loncom/metadata_database/searchcat.pl	Wed Jul 25 19:17:43 2007
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # searchcat.pl "Search Catalog" batch script
 #
-# $Id: searchcat.pl,v 1.76 2007/04/11 22:44:18 albertel Exp $
+# $Id: searchcat.pl,v 1.77 2007/07/25 23:17:43 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -123,6 +123,7 @@
                  'portfolio'   => 'portfolio_metadata',
                  'access'      => 'portfolio_access',
                  'addedfields' => 'portfolio_addedfields',
+                 'allusers'     => 'allusers',
                );
 
 my %newnames;
@@ -172,7 +173,7 @@
 }
 
 #
-# Create the new metadata and portfolio tables
+# Create the new metadata, portfolio and allusers tables
 foreach my $key (keys(%newnames)) {
     if ($newnames{$key} ne '') { 
         my $request =
@@ -221,15 +222,18 @@
               no_chdir   => 1,
              }, join('/',($Apache::lonnet::perlvar{'lonDocRoot'},'res',$dom,$user)) );
     }
-    # Search for public portfolio files
-    my %portusers;
+    # Search for all users and public portfolio files
+    my (%allusers,%portusers);
     if ($oneuser) {
         %portusers = (
                         $oneuser => '',
                        );
+        %allusers = (
+                        $oneuser => '',
+                       );
     } else {
         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
-        &descend_tree($dir,0,\%portusers);
+        &descend_tree($dom,$dir,0,\%portusers,\%allusers);
     }
     foreach my $uname (keys(%portusers)) {
         my $urlstart = '/uploaded/'.$dom.'/'.$uname;
@@ -257,6 +261,20 @@
             &portfolio_logging(%portmetalog);
         }
     }
+    # Update allusers
+    foreach my $uname (keys(%allusers)) {
+        my %userdata = 
+            &Apache::lonnet::get('environment',['firstname','lastname',
+                'middlename','generation','id','permanentemail'],$dom,$uname);
+        $userdata{'username'} = $uname;
+        $userdata{'domain'} = $dom;
+        my %alluserslog = 
+            &LONCAPA::lonmetadata::process_allusers_data($dbh,$simulate,
+                \%newnames,$uname,$dom,\%userdata);
+        foreach my $item (keys(%alluserslog)) {
+            &log(0,$alluserslog{$item});
+        }
+    }
 }
 
 #
@@ -317,7 +335,7 @@
 }
 
 sub descend_tree {
-    my ($dir,$depth,$alldomusers) = @_;
+    my ($dom,$dir,$depth,$allportusers,$alldomusers) = @_;
     if (-d $dir) {
         opendir(DIR,$dir);
         my @contents = grep(!/^\./,readdir(DIR));
@@ -325,10 +343,12 @@
         $depth ++;
         foreach my $item (@contents) {
             if ($depth < 4) {
-                &descend_tree($dir.'/'.$item,$depth,$alldomusers);
+                &descend_tree($dom,$dir.'/'.$item,$depth,$allportusers,$alldomusers);
             } else {
                 if (-e $dir.'/'.$item.'/file_permissions.db') {
-                 
+                     $$allportusers{$item} = '';
+                }
+                if (!&Apache::lonnet::is_course($dom,$item)) { 
                     $$alldomusers{$item} = '';
                 }
             }       
Index: loncom/metadata_database/LONCAPA/lonmetadata.pm
diff -u loncom/metadata_database/LONCAPA/lonmetadata.pm:1.22 loncom/metadata_database/LONCAPA/lonmetadata.pm:1.23
--- loncom/metadata_database/LONCAPA/lonmetadata.pm:1.22	Fri Jun 15 19:02:09 2007
+++ loncom/metadata_database/LONCAPA/lonmetadata.pm	Wed Jul 25 19:17:49 2007
@@ -1,6 +1,6 @@
 # The LearningOnline Network with CAPA
 #
-# $Id: lonmetadata.pm,v 1.22 2007/06/15 23:02:09 albertel Exp $
+# $Id: lonmetadata.pm,v 1.23 2007/07/25 23:17:49 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -32,7 +32,7 @@
 use DBI;
 use HTML::TokeParser;
 use vars qw($Metadata_Table_Description $Portfolio_metadata_table_description 
-$Portfolio_access_table_description $Fulltext_indicies $Portfolio_metadata_indices $Portfolio_access_indices $Portfolio_addedfields_table_description $Portfolio_addedfields_indices);
+$Portfolio_access_table_description $Fulltext_indicies $Portfolio_metadata_indices $Portfolio_access_indices $Portfolio_addedfields_table_description $Portfolio_addedfields_indices $Allusers_table_description $Allusers_indices);
 
 ######################################################################
 ######################################################################
@@ -247,6 +247,26 @@
 ######################################################################
 ######################################################################
 
+$Allusers_table_description =
+    [
+     { name => 'username',   type=>'TEXT', restrictions => 'NOT NULL' },
+     { name => 'domain', type=>'TEXT', restrictions => 'NOT NULL' },
+     { name => 'lastname', type=>'TEXT',},
+     { name => 'firstname', type=>'TEXT'},
+     { name => 'middlename', type=>'TEXT'},
+     { name => 'generation', type=>'TEXT'},
+     { name => 'permanentemail', type=>'TEXT'},
+     { name => 'id', type=>'TEXT'},
+   ];
+
+$Allusers_indices = [qw/
+    username
+    domain
+    lastname
+    firstname/];
+
+######################################################################
+######################################################################
 
 =pod
 
@@ -268,12 +288,14 @@
         portfolio_metadata    => $Portfolio_metadata_table_description,
         portfolio_access      => $Portfolio_access_table_description,
         portfolio_addedfields => $Portfolio_addedfields_table_description, 
+        allusers              => $Allusers_table_description,
     );
     my %index_description = (
         metadata              => $Fulltext_indicies,
         portfolio_metadata    => $Portfolio_metadata_indices,
         portfolio_access      => $Portfolio_access_indices,
         portfolio_addedfields => $Portfolio_addedfields_indices,
+        allusers              => $Allusers_indices,
     );
     if ($tabletype eq 'portfolio_search') {
         my @portfolio_search_table = @{$table_description{portfolio_metadata}};
@@ -466,7 +488,7 @@
 
 =pod
 
-=item lookup_metadata()
+=item ()
 
 Inputs: database handle ($dbh) and a hash or hash reference containing 
 metadata which will be used for a search.
@@ -516,7 +538,8 @@
 
 Inputs: $dbh, the database handler.
 $tablename, the name of the metadata table to remove from. default: 'metadata'
-$url, the url of the resource to remove from the metadata database.
+$delitem, the resource to remove from the metadata database, in the form: 
+          url = quoted url 
 
 Returns: undef on success, dbh errorstr on failure.
 
@@ -525,14 +548,17 @@
 ######################################################################
 ######################################################################
 sub delete_metadata {
-    my ($dbh,$tablename,$url) = @_;
+    my ($dbh,$tablename,$delitem) = @_;
     $tablename = 'metadata' if (! defined($tablename));
-    my $error;
-    my $delete_command = 'DELETE FROM '.$tablename.' WHERE url='.
-        $dbh->quote($url);
-    $dbh->do($delete_command);
-    if ($dbh->err) {
-        $error = $dbh->errstr();
+    my ($error,$delete_command);
+    if ($delitem eq '') {
+        $error = 'deletion aborted - no resource specified';    
+    } else {
+        $delete_command = 'DELETE FROM '.$tablename.' WHERE '.$delitem;
+        $dbh->do($delete_command);
+        if ($dbh->err) {
+            $error = $dbh->errstr();
+        }
     }
     return $error;
 }
@@ -553,7 +579,10 @@
 $dbh, database handle
 $newmetadata, hash reference containing the new metadata
 $tablename, metadata table name.  Defaults to 'metadata'.
-$tabletype, type of table (metadata, portfolio_metadata, portfolio_access)  
+$tabletype, type of table (metadata, portfolio_metadata, portfolio_access, 
+                           allusers)
+$conditions, optional hash of conditions to use in SQL queries; 
+             default used if none provided.
 
 Returns:
 $error on failure.  undef on success.
@@ -563,20 +592,32 @@
 ######################################################################
 ######################################################################
 sub update_metadata {
-    my ($dbh,$tablename,$tabletype,$newmetadata)=@_;
-    my $error;
+    my ($dbh,$tablename,$tabletype,$newmetadata,$conditions)=@_;
+    my ($error,$condition);
     $tablename = 'metadata' if (! defined($tablename));
     $tabletype = 'metadata' if (! defined($tabletype));
-    if (! exists($newmetadata->{'url'})) {
-        $error = 'Unable to update: no url specified';
+    if (ref($conditions) eq 'HASH') {
+        my @items;
+        foreach my $key (keys(%{$conditions})) {
+            if (! exists($newmetadata->{$key})) {
+                $error .= "Unable to update: no $key specified";
+            } else {
+                push(@items,"$key = ".$dbh->quote($newmetadata->{$key}));
+            }
+        }
+        $condition = join(' AND ',@items); 
+    } else {
+        if (! exists($newmetadata->{'url'})) {
+            $error = 'Unable to update: no url specified';
+        } else {
+            $condition = 'url = '.$dbh->quote($newmetadata->{'url'});
+        }
     }
     return $error if (defined($error));
     # 
     # Retrieve current values
     my $row;
-    ($error,$row) = &lookup_metadata($dbh,
-                                   ' url='.$dbh->quote($newmetadata->{'url'}),
-                                     undef,$tablename);
+    ($error,$row) = &lookup_metadata($dbh,$condition,undef,$tablename);
     return $error if ($error);
     my %metadata = &LONCAPA::lonmetadata::metadata_col_to_hash($tabletype,@{$row->[0]});
     #
@@ -586,7 +627,7 @@
     }
     #
     # Delete old data (deleting a nonexistant record does not produce an error.
-    $error = &delete_metadata($dbh,$tablename,$newmetadata->{'url'});
+    $error = &delete_metadata($dbh,$tablename,$condition);
     return $error if (defined($error));
     #
     # Store updated metadata
@@ -1013,7 +1054,13 @@
     my %loghash;
     if ($caller eq 'update') {
         # Delete old data (no error if deleting non-existent record).
-        my $error=&delete_metadata($dbh,$newnames->{'access'},$url);
+        my $error;
+        if ($url eq '') {
+            $error = 'No url specified'; 
+        } else {
+            my $delitem = 'url = '.$dbh->quote($url);
+            $error=&delete_metadata($dbh,$newnames->{'access'},$delitem);
+        }
         if (defined($error)) {
             $loghash{'access'}{'err'} = "MySQL Error Delete: ".$error;
             return %loghash;
@@ -1055,13 +1102,19 @@
     my %loghash;
     if ($caller eq 'update') {
         # Delete old data (no error if deleting non-existent record).
-        my $error=&delete_metadata($dbh,$newnames->{'portfolio'},$url);
+        my ($error,$delitem);
+        if ($url eq '') {
+            $error = 'No url specified';
+        } else {
+            $delitem = 'url = '.$dbh->quote($url);
+            $error=&delete_metadata($dbh,$newnames->{'portfolio'},$delitem);
+        }
         if (defined($error)) {
             $loghash{'metadata'}{'err'} = "MySQL Error delete metadata: ".
                                                $error;
             return %loghash;
         }
-        $error=&delete_metadata($dbh,$newnames->{'addedfields'},$url);
+        $error=&delete_metadata($dbh,$newnames->{'addedfields'},$delitem);
         if (defined($error)) {
             $loghash{'addedfields'}{'err'}="MySQL Error delete addedfields: ".$error;
         }
@@ -1121,6 +1174,44 @@
     return %loghash;
 }
 
+sub process_allusers_data {
+    my ($dbh,$simulate,$newnames,$uname,$udom,$userdata,$caller) = @_;
+    my %loghash;
+    if ($caller eq 'update') {
+        # Delete old data (no error if deleting non-existent record).
+        my ($error,$delitem);
+        if ($udom eq '' || $uname eq '' ) {
+            $error = 'No domain and/or username specified';
+        } else {
+            $delitem = 'domain = '.$dbh->quote($udom).' AND username = '.
+                       $dbh->quote($uname);
+            $error=&delete_metadata($dbh,$newnames->{'allusers'},$delitem);
+        }
+        if (defined($error)) {
+            $loghash{'err'} = 'MySQL Error in allusers delete: '.$error;
+            return %loghash;
+        }
+    }
+    if (!$simulate) {
+        if ($udom ne '' && $uname ne '') {
+            my ($count,$err) = &store_metadata($dbh,$newnames->{'allusers'},
+                                               'allusers',$userdata);
+            if ($err) {
+                $loghash{'err'} = 'MySQL Error in allusers insert: '.$err;
+            }
+            if ($count < 1) {
+                $loghash{'count'} = 
+                    'Unable to insert record into MySQL allusers database for '.
+                    $uname.' in '.$udom;
+            }
+        } else {
+            $loghash{'err'} = 
+                'MySQL Error allusrs insert: missing username and/or domain';
+        }
+    }
+    return %loghash;
+}
+
 ######################################################################
 ######################################################################
 

--raeburn1185405469--