--- loncom/metadata_database/LONCAPA/lonmetadata.pm 2007/01/02 12:53:27 1.16 +++ loncom/metadata_database/LONCAPA/lonmetadata.pm 2009/04/29 15:21:00 1.27 @@ -1,6 +1,6 @@ # The LearningOnline Network with CAPA # -# $Id: lonmetadata.pm,v 1.16 2007/01/02 12:53:27 raeburn Exp $ +# $Id: lonmetadata.pm,v 1.27 2009/04/29 15:21:00 bisitz Exp $ # # Copyright Michigan State University Board of Trustees # @@ -32,7 +32,7 @@ use strict; 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); ###################################################################### ###################################################################### @@ -184,8 +184,6 @@ $Portfolio_metadata_table_description = { name => 'domain', type=>'TEXT'}, { name => 'groupname', type=>'TEXT'}, { name => 'courserestricted', type=>'TEXT'}, - { name => 'addedfieldnames', type=>'TEXT'}, - { name => 'addedfieldvalues', type=>'TEXT'}, #-------------------------------------------------- { name => 'dependencies', type=>'TEXT'}, { name => 'modifyinguser', type=>'TEXT'}, @@ -249,6 +247,26 @@ $Portfolio_addedfields_indices = [qw/ ###################################################################### ###################################################################### +$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 @@ -270,12 +288,14 @@ sub describe_metadata_storage { 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}}; @@ -518,7 +538,8 @@ Removes a single metadata record, based 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. @@ -527,14 +548,17 @@ Returns: undef on success, dbh errorstr ###################################################################### ###################################################################### 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; } @@ -555,7 +579,10 @@ Inputs: $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. @@ -565,20 +592,32 @@ $error on failure. undef on success. ###################################################################### ###################################################################### 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]}); # @@ -588,7 +627,7 @@ sub update_metadata { } # # 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 @@ -797,23 +836,17 @@ sub process_dynamic_metadata { # Get the statistical data - Use a weighted average foreach my $type (qw/avetries difficulty disc/) { my $studentcount; + my %course_counted; my $sum; my @Values; my @Students; # - # Old data - foreach my $coursedata (values(%{$resdata->{'statistics'}}), - values(%{$resdata->{'stats'}})) { - if (ref($coursedata) eq 'HASH' && exists($coursedata->{$type})) { - $studentcount += $coursedata->{'stdno'}; - $sum += ($coursedata->{$type}*$coursedata->{'stdno'}); - push(@Values,$coursedata->{$type}); - push(@Students,$coursedata->{'stdno'}); - } - } + # New data if (exists($resdata->{'stats'})) { foreach my $identifier (sort(keys(%{$resdata->{'stats'}}))) { my $coursedata = $resdata->{'stats'}->{$identifier}; + next if (lc($coursedata->{$type}) eq 'nan'); + $course_counted{$coursedata->{'course'}}++; $studentcount += $coursedata->{'stdno'}; $sum += $coursedata->{$type}*$coursedata->{'stdno'}; push(@Values,$coursedata->{$type}); @@ -821,7 +854,18 @@ sub process_dynamic_metadata { } } # - # New data + # Old data + foreach my $course (keys(%{$resdata->{'statistics'}})) { + next if (exists($course_counted{$course})); + my $coursedata = $resdata->{'statistics'}{$course}; + if (ref($coursedata) eq 'HASH' && exists($coursedata->{$type})) { + next if (lc($coursedata->{$type}) eq 'nan'); + $studentcount += $coursedata->{'stdno'}; + $sum += ($coursedata->{$type}*$coursedata->{'stdno'}); + push(@Values,$coursedata->{$type}); + push(@Students,$coursedata->{'stdno'}); + } + } if (defined($studentcount) && $studentcount>0) { $data{$type} = $sum/$studentcount; $data{$type.'_list'} = join(',',@Values); @@ -830,12 +874,7 @@ sub process_dynamic_metadata { # # Find out the number of students who have completed the resource... my $stdno; - foreach my $coursedata (values(%{$resdata->{'statistics'}}), - values(%{$resdata->{'stats'}})) { - if (ref($coursedata) eq 'HASH' && exists($coursedata->{'stdno'})) { - $stdno += $coursedata->{'stdno'}; - } - } + my %course_counted; if (exists($resdata->{'stats'})) { # # For the number of students, take the maximum found for the class @@ -848,6 +887,7 @@ sub process_dynamic_metadata { } if ($current_course ne $coursedata->{'course'}) { $stdno += $coursemax; + $course_counted{$coursedata->{'course'}}++; $coursemax = 0; $current_course = $coursedata->{'course'}; } @@ -857,6 +897,14 @@ sub process_dynamic_metadata { } $stdno += $coursemax; # pick up the final course in the list } + # check for old data that has not been run since the format was changed + foreach my $course (keys(%{$resdata->{'statistics'}})) { + next if (exists($course_counted{$course})); + my $coursedata = $resdata->{'statistics'}{$course}; + if (ref($coursedata) eq 'HASH' && exists($coursedata->{'stdno'})) { + $stdno += $coursedata->{'stdno'}; + } + } $data{'stdno'}=$stdno; # # Get the context data @@ -887,16 +935,20 @@ sub process_dynamic_metadata { } # # put together comments - my $comments = '
'; + my $comments = ''; foreach my $evaluator (keys(%{$resdata->{'evaluation'}->{'comments'}})){ $comments .= '

'. - ''.$evaluator.':'. + ''.$evaluator.': '. $resdata->{'evaluation'}->{'comments'}->{$evaluator}. '

'; } - $comments .= '
'; - $data{'comments'} = $comments; + if ($comments) { + $comments = '
' + .$comments + .'
'; + $data{'comments'} = $comments; + } # if (exists($resdata->{'stats'})) { $data{'stats'} = $resdata->{'stats'}; @@ -993,20 +1045,10 @@ sub portfolio_metadata { } } } # End of ($token->[0] eq 'S') - } - if (keys(%addedfields) > 0) { - foreach my $key (sort keys(%addedfields)) { - $metacache{'addedfieldnames'} .= $key.','; - $metacache{'addedfieldvalues'} .= $addedfields{$key}.'&&&'; - } - $metacache{'addedfieldnames'} =~ s/,$//; - $metacache{'addedfieldvalues'} =~ s/\&\&\&$//; - if ($metacache{'keys'}) { - $metacache{'keys'}.=',addedfieldnames'; - } else { - $metacache{'keys'}='addedfieldnames'; - } - $metacache{'keys'}.=',addedfieldvalues'; + + if (!exists($metacache{'domain'})) { + $metacache{'domain'} = $dom; + } } return (\%metacache,$metacache{'courserestricted'},\%addedfields); } @@ -1016,7 +1058,13 @@ sub process_portfolio_access_data { 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; @@ -1058,13 +1106,19 @@ sub process_portfolio_metadata { 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; } @@ -1124,17 +1178,53 @@ sub process_portfolio_metadata { 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 '. + 'COLLATE latin1_general_cs = '.$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; +} + ###################################################################### ###################################################################### -## Utilities originally in searchcat.pl. Moved to be more widely available. - sub getfile { my $file = shift(); if (! -e $file ) { return undef; } - my $fh=IO::File->new($file); + open(my $fh,"<$file"); my $contents = ''; while (<$fh>) { $contents .= $_;