--- loncom/metadata_database/LONCAPA/lonmetadata.pm 2004/04/16 21:43:56 1.8 +++ loncom/metadata_database/LONCAPA/lonmetadata.pm 2005/11/29 19:56:42 1.13 @@ -1,6 +1,6 @@ # The LearningOnline Network with CAPA # -# $Id: lonmetadata.pm,v 1.8 2004/04/16 21:43:56 matthew Exp $ +# $Id: lonmetadata.pm,v 1.13 2005/11/29 19:56:42 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -75,6 +75,7 @@ creationdate DATETIME, lastrevisiondate DATETIME, owner TEXT, copyright TEXT, +domain TEXT FULLTEXT idx_title (title), FULLTEXT idx_author (author), @@ -111,6 +112,7 @@ my @Metadata_Table_Description = { name => 'lastrevisiondate', type=>'DATETIME'}, { name => 'owner', type=>'TEXT'}, { name => 'copyright', type=>'TEXT'}, + { name => 'domain', type=>'TEXT'}, #-------------------------------------------------- { name => 'dependencies', type=>'TEXT'}, { name => 'modifyinguser', type=>'TEXT'}, @@ -133,6 +135,8 @@ my @Metadata_Table_Description = { name => 'avetries_list', type=>'TEXT'}, { name => 'difficulty', type=>'FLOAT'}, { name => 'difficulty_list',type=>'TEXT'}, + { name => 'disc', type=>'FLOAT'}, + { name => 'disc_list', type=>'TEXT'}, { name => 'clear', type=>'FLOAT'}, { name => 'technical', type=>'FLOAT'}, { name => 'correct', type=>'FLOAT'}, @@ -283,8 +287,7 @@ sub create_statement_handler { sub clear_sth { $sth=undef; $sth_table=undef;} sub store_metadata { - my $dbh = shift(); - my $tablename = shift(); + my ($dbh,$tablename,@Metadata)=@_; my $errors = ''; if (! defined($sth) || ( defined($tablename) && ($sth_table ne $tablename)) || @@ -292,15 +295,19 @@ sub store_metadata { &create_statement_handler($dbh,$tablename); } my $successcount = 0; - while (my $mdata = shift()) { + foreach my $mdata (@Metadata) { next if (ref($mdata) ne "HASH"); my @MData; foreach my $field (@Metadata_Table_Description) { - if (exists($mdata->{$field->{'name'}})) { - if ($mdata->{$field->{'name'}} eq 'nan') { + my $fname = $field->{'name'}; + if (exists($mdata->{$fname}) && + defined($mdata->{$fname}) && + $mdata->{$fname} ne '') { + if ($mdata->{$fname} eq 'nan' || + $mdata->{$fname} eq '') { push(@MData,'NULL'); } else { - push(@MData,$mdata->{$field->{'name'}}); + push(@MData,$mdata->{$fname}); } } else { push(@MData,undef); @@ -312,6 +319,7 @@ sub store_metadata { } else { $errors = join(',',$errors,$sth->errstr); } + $errors =~ s/^,//; } if (wantarray()) { return ($successcount,$errors); @@ -340,10 +348,11 @@ The array reference is the same one retu ###################################################################### ###################################################################### sub lookup_metadata { - my ($dbh,$condition,$fetchparameter) = @_; + my ($dbh,$condition,$fetchparameter,$tablename) = @_; + $tablename = 'metadata' if (! defined($tablename)); my $error; my $returnvalue=[]; - my $request = 'SELECT * FROM metadata'; + my $request = 'SELECT * FROM '.$tablename; if (defined($condition)) { $request .= ' WHERE '.$condition; } @@ -372,13 +381,86 @@ sub lookup_metadata { =item delete_metadata() -Not implemented yet +Removes a single metadata record, based on its url. + +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. + +Returns: undef on success, dbh errorstr on failure. =cut ###################################################################### ###################################################################### -sub delete_metadata {} +sub delete_metadata { + my ($dbh,$tablename,$url) = @_; + $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(); + } + return $error; +} + +###################################################################### +###################################################################### + +=pod + +=item update_metadata + +Updates metadata record in mysql database. It does not matter if the record +currently exists. Fields not present in the new metadata will be taken +from the current record, if it exists. To delete an entry for a key, set +it to "" or undef. + +Inputs: +$dbh, database handle +$newmetadata, hash reference containing the new metadata +$tablename, metadata table name. Defaults to 'metadata'. + +Returns: +$error on failure. undef on success. + +=cut + +###################################################################### +###################################################################### +sub update_metadata { + my ($dbh,$tablename,$newmetadata)=@_; + my $error; + $tablename = 'metadata' if (! defined($tablename)); + if (! exists($newmetadata->{'url'})) { + $error = 'Unable to update: no url specified'; + } + return $error if (defined($error)); + # + # Retrieve current values + my $row; + ($error,$row) = &lookup_metadata($dbh, + ' url='.$dbh->quote($newmetadata->{'url'}), + undef,$tablename); + return $error if ($error); + my %metadata = &LONCAPA::lonmetadata::metadata_col_to_hash(@{$row->[0]}); + # + # Update metadata values + while (my ($key,$value) = each(%$newmetadata)) { + $metadata{$key} = $value; + } + # + # Delete old data (deleting a nonexistant record does not produce an error. + $error = &delete_metadata($dbh,$tablename,$newmetadata->{'url'}); + return $error if (defined($error)); + # + # Store updated metadata + my $success; + ($success,$error) = &store_metadata($dbh,$tablename,\%metadata); + return $error; +} ###################################################################### ###################################################################### @@ -401,6 +483,15 @@ sub metadata_col_to_hash { my %hash=(); for (my $i=0; $i<=$#Metadata_Table_Description;$i++) { $hash{$Metadata_Table_Description[$i]->{'name'}}=$cols[$i]; + unless ($hash{$Metadata_Table_Description[$i]->{'name'}}) { + if ($Metadata_Table_Description[$i]->{'type'} eq 'TEXT') { + $hash{$Metadata_Table_Description[$i]->{'name'}}=''; + } elsif ($Metadata_Table_Description[$i]->{'type'} eq 'DATETIME') { + $hash{$Metadata_Table_Description[$i]->{'name'}}='0000-00-00 00:00:00'; + } else { + $hash{$Metadata_Table_Description[$i]->{'name'}}=0; + } + } } return %hash; } @@ -429,6 +520,7 @@ The nohist_resevaldata.db file has the f $username@$dom___$resource___depth $username@$dom___$resource___technical $username@$dom___$resource___helpful + $username@$dom___$resource___correct Course Context Data ------------------------------------------ @@ -498,7 +590,7 @@ sub process_reseval_data { # # Statistics: $source is course id $DynamicData{$file}->{'statistics'}->{$source}->{$type}=$value; - } elsif ($type =~ /^(clear|comments|depth|technical|helpful)$/){ + } elsif ($type =~ /^(clear|comments|depth|technical|helpful|correct)$/){ # # Evaluation $source is username, check if they evaluated it # more than once. If so, pad the entry with a space. @@ -673,6 +765,9 @@ sub process_dynamic_metadata { if (exists($resdata->{'stats'})) { $data{'stats'} = $resdata->{'stats'}; } + if (exists($DynamicData->{'domain'})) { + $data{'domain'} = $DynamicData->{'domain'}; + } # return %data; } @@ -717,9 +812,6 @@ sub escape { return $str; } - - - 1; __END__;