File:  [LON-CAPA] / loncom / metadata_database / LONCAPA / lonmetadata.pm
Revision 1.11: download - view: text, annotated - select for diffs
Thu Jun 17 20:33:21 2004 UTC (19 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
Ignored "correct" evaluation data

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonmetadata.pm,v 1.11 2004/06/17 20:33:21 www Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: ######################################################################
   28: 
   29: package LONCAPA::lonmetadata;
   30: 
   31: use strict;
   32: use DBI;
   33: 
   34: ######################################################################
   35: ######################################################################
   36: 
   37: =pod 
   38: 
   39: =head1 Name
   40: 
   41: lonmetadata
   42: 
   43: =head1 Synopsis
   44: 
   45: lonmetadata holds a description of the metadata table and provides
   46: wrappers for the storage and retrieval of metadata to/from the database.
   47: 
   48: =head1 Description
   49: 
   50: =head1 Methods
   51: 
   52: =over 4
   53: 
   54: =cut
   55: 
   56: ######################################################################
   57: ######################################################################
   58: 
   59: =pod
   60: 
   61: =item Old table creation command
   62: 
   63: CREATE TABLE IF NOT EXISTS metadata 
   64: (title TEXT, 
   65: author TEXT, 
   66: subject TEXT, 
   67: url TEXT, 
   68: keywords TEXT, 
   69: version TEXT, 
   70: notes TEXT, 
   71: abstract TEXT, 
   72: mime TEXT, 
   73: language TEXT, 
   74: creationdate DATETIME, 
   75: lastrevisiondate DATETIME, 
   76: owner TEXT, 
   77: copyright TEXT, 
   78: 
   79: FULLTEXT idx_title (title), 
   80: FULLTEXT idx_author (author), 
   81: FULLTEXT idx_subject (subject), 
   82: FULLTEXT idx_url (url), 
   83: FULLTEXT idx_keywords (keywords), 
   84: FULLTEXT idx_version (version), 
   85: FULLTEXT idx_notes (notes), 
   86: FULLTEXT idx_abstract (abstract), 
   87: FULLTEXT idx_mime (mime), 
   88: FULLTEXT idx_language (language),
   89: FULLTEXT idx_owner (owner), 
   90: FULLTEXT idx_copyright (copyright)) 
   91: 
   92: TYPE=MYISAM;
   93: 
   94: =cut
   95: 
   96: ######################################################################
   97: ######################################################################
   98: my @Metadata_Table_Description = 
   99:     (
  100:      { name => 'title',     type=>'TEXT'},
  101:      { name => 'author',    type=>'TEXT'},
  102:      { name => 'subject',   type=>'TEXT'},
  103:      { name => 'url',       type=>'TEXT', restrictions => 'NOT NULL' },
  104:      { name => 'keywords',  type=>'TEXT'},
  105:      { name => 'version',   type=>'TEXT'},
  106:      { name => 'notes',     type=>'TEXT'},
  107:      { name => 'abstract',  type=>'TEXT'},
  108:      { name => 'mime',      type=>'TEXT'},
  109:      { name => 'language',  type=>'TEXT'},
  110:      { name => 'creationdate',     type=>'DATETIME'},
  111:      { name => 'lastrevisiondate', type=>'DATETIME'},
  112:      { name => 'owner',     type=>'TEXT'},
  113:      { name => 'copyright', type=>'TEXT'}, 
  114:       #--------------------------------------------------
  115:      { name => 'dependencies',   type=>'TEXT'},
  116:      { name => 'modifyinguser',  type=>'TEXT'},
  117:      { name => 'authorspace',    type=>'TEXT'},
  118:      { name => 'lowestgradelevel',  type=>'INT'},
  119:      { name => 'highestgradelevel', type=>'INT'},
  120:      { name => 'standards',      type=>'TEXT'},
  121:      { name => 'count',          type=>'INT'},
  122:      { name => 'course',         type=>'INT'},
  123:      { name => 'course_list',    type=>'TEXT'},
  124:      { name => 'goto',           type=>'INT'},
  125:      { name => 'goto_list',      type=>'TEXT'},
  126:      { name => 'comefrom',       type=>'INT'},
  127:      { name => 'comefrom_list',  type=>'TEXT'},
  128:      { name => 'sequsage',       type=>'INT'},
  129:      { name => 'sequsage_list',  type=>'TEXT'},
  130:      { name => 'stdno',          type=>'INT'},
  131:      { name => 'stdno_list',     type=>'TEXT'},
  132:      { name => 'avetries',       type=>'FLOAT'},
  133:      { name => 'avetries_list',  type=>'TEXT'},
  134:      { name => 'difficulty',     type=>'FLOAT'},
  135:      { name => 'difficulty_list',type=>'TEXT'},
  136:      { name => 'disc',           type=>'FLOAT'},
  137:      { name => 'disc_list',      type=>'TEXT'},
  138:      { name => 'clear',          type=>'FLOAT'},
  139:      { name => 'technical',      type=>'FLOAT'},
  140:      { name => 'correct',        type=>'FLOAT'},
  141:      { name => 'helpful',        type=>'FLOAT'},
  142:      { name => 'depth',          type=>'FLOAT'},
  143:      { name => 'hostname',       type=> 'TEXT'},
  144:      #--------------------------------------------------
  145:      );
  146: 
  147: my @Fulltext_indicies = qw/
  148:     title
  149:     author
  150:     subject
  151:     url
  152:     keywords
  153:     version
  154:     notes
  155:     abstract
  156:     mime
  157:     language
  158:     owner
  159:     copyright/;
  160: 
  161: ######################################################################
  162: ######################################################################
  163: 
  164: =pod
  165: 
  166: =item &describe_metadata_storage
  167: 
  168: Input: None
  169: 
  170: Returns: An array of hash references describing the columns and indicies
  171: of the metadata table(s).
  172: 
  173: =cut
  174: 
  175: ######################################################################
  176: ######################################################################
  177: sub describe_metadata_storage { 
  178:     return (\@Metadata_Table_Description,\@Fulltext_indicies);
  179: }
  180: 
  181: ######################################################################
  182: ######################################################################
  183: 
  184: =pod
  185: 
  186: =item create_metadata_storage()
  187: 
  188: Inputs: table name (optional): the name of the table.  Default is 'metadata'.
  189: 
  190: Returns: A perl string which, when executed by MySQL, will cause the
  191: metadata storage to be initialized.
  192: 
  193: =cut
  194: 
  195: ######################################################################
  196: ######################################################################
  197: sub create_metadata_storage { 
  198:     my ($tablename) = @_;
  199:     $tablename = 'metadata' if (! defined($tablename));
  200:     my $request = "CREATE TABLE IF NOT EXISTS ".$tablename." ";
  201:     #
  202:     # Process the columns  (this code is stolen from lonmysql.pm)
  203:     my @Columns;
  204:     my $col_des; # mysql column description
  205:     foreach my $coldata (@Metadata_Table_Description) {
  206:         my $column = $coldata->{'name'};
  207:         $col_des = '';
  208:         if (lc($coldata->{'type'}) =~ /(enum|set)/) { # 'enum' or 'set'
  209:             $col_des.=$column." ".$coldata->{'type'}."('".
  210:                 join("', '",@{$coldata->{'values'}})."')";
  211:         } else {
  212:             $col_des.=$column." ".$coldata->{'type'};
  213:             if (exists($coldata->{'size'})) {
  214:                 $col_des.="(".$coldata->{'size'}.")";
  215:             }
  216:         }
  217:         # Modifiers
  218:         if (exists($coldata->{'restrictions'})){
  219:             $col_des.=" ".$coldata->{'restrictions'};
  220:         }
  221:         if (exists($coldata->{'default'})) {
  222:             $col_des.=" DEFAULT '".$coldata->{'default'}."'";
  223:         }
  224:         $col_des.=' AUTO_INCREMENT' if (exists($coldata->{'auto_inc'}) &&
  225:                                         ($coldata->{'auto_inc'} eq 'yes'));
  226:         $col_des.=' PRIMARY KEY'    if (exists($coldata->{'primary_key'}) &&
  227:                                         ($coldata->{'primary_key'} eq 'yes'));
  228:     } continue {
  229:         # skip blank items.
  230:         push (@Columns,$col_des) if ($col_des ne '');
  231:     }
  232:     foreach my $colname (@Fulltext_indicies) {
  233:         my $text = 'FULLTEXT idx_'.$colname.' ('.$colname.')';
  234:         push (@Columns,$text);
  235:     }
  236:     $request .= "(".join(", ",@Columns).") TYPE=MyISAM";
  237:     return $request;
  238: }
  239: 
  240: ######################################################################
  241: ######################################################################
  242: 
  243: =pod
  244: 
  245: =item store_metadata()
  246: 
  247: Inputs: database handle ($dbh), a table name, and a hash or hash reference 
  248: containing the metadata for a single resource.
  249: 
  250: Returns: 1 on success, 0 on failure to store.
  251: 
  252: =cut
  253: 
  254: ######################################################################
  255: ######################################################################
  256: {
  257:     ##
  258:     ##  WARNING: The following cleverness may cause trouble in cases where
  259:     ##  the dbi connection is dropped and recreated - a stale statement
  260:     ##  handler may linger around and cause trouble.
  261:     ##
  262:     ##  In most scripts, this will work fine.  If the dbi is going to be
  263:     ##  dropped and (possibly) later recreated, call &clear_sth.  Yes it
  264:     ##  is annoying but $sth appearantly does not have a link back to the 
  265:     ##  $dbh, so we can't check our validity.
  266:     ##
  267:     my $sth = undef;
  268:     my $sth_table = undef;
  269: 
  270: sub create_statement_handler {
  271:     my $dbh = shift();
  272:     my $tablename = shift();
  273:     $tablename = 'metadata' if (! defined($tablename));
  274:     $sth_table = $tablename;
  275:     my $request = 'INSERT INTO '.$tablename.' VALUES(';
  276:     foreach (@Metadata_Table_Description) {
  277:         $request .= '?,';
  278:     }
  279:     chop $request;
  280:     $request.= ')';
  281:     $sth = $dbh->prepare($request);
  282:     return;
  283: }
  284: 
  285: sub clear_sth { $sth=undef; $sth_table=undef;}
  286: 
  287: sub store_metadata {
  288:     my ($dbh,$tablename,@Metadata)=@_;
  289:     my $errors = '';
  290:     if (! defined($sth) || 
  291:         ( defined($tablename) && ($sth_table ne $tablename)) || 
  292:         (! defined($tablename) && $sth_table ne 'metadata')) {
  293:         &create_statement_handler($dbh,$tablename);
  294:     }
  295:     my $successcount = 0;
  296:     foreach my $mdata (@Metadata) {
  297:         next if (ref($mdata) ne "HASH");
  298:         my @MData;
  299:         foreach my $field (@Metadata_Table_Description) {
  300:             my $fname = $field->{'name'};
  301:             if (exists($mdata->{$fname}) && 
  302:                 defined($mdata->{$fname}) &&
  303:                 $mdata->{$fname} ne '') {
  304:                 if ($mdata->{$fname} eq 'nan' ||
  305:                     $mdata->{$fname} eq '') {
  306:                     push(@MData,'NULL');
  307:                 } else {
  308:                     push(@MData,$mdata->{$fname});
  309:                 }
  310:             } else {
  311:                 push(@MData,undef);
  312:             }
  313:         }
  314:         $sth->execute(@MData);
  315:         if (! $sth->err) {
  316:             $successcount++;
  317:         } else {
  318:             $errors = join(',',$errors,$sth->errstr);
  319:         }
  320:         $errors =~ s/^,//;
  321:     }
  322:     if (wantarray()) {
  323:         return ($successcount,$errors);
  324:     } else {
  325:         return $successcount;
  326:     }
  327: }
  328: 
  329: }
  330: 
  331: ######################################################################
  332: ######################################################################
  333: 
  334: =pod
  335: 
  336: =item lookup_metadata()
  337: 
  338: Inputs: database handle ($dbh) and a hash or hash reference containing 
  339: metadata which will be used for a search.
  340: 
  341: Returns: scalar with error string on failure, array reference on success.
  342: The array reference is the same one returned by $sth->fetchall_arrayref().
  343: 
  344: =cut
  345: 
  346: ######################################################################
  347: ######################################################################
  348: sub lookup_metadata {
  349:     my ($dbh,$condition,$fetchparameter,$tablename) = @_;
  350:     $tablename = 'metadata' if (! defined($tablename));
  351:     my $error;
  352:     my $returnvalue=[];
  353:     my $request = 'SELECT * FROM '.$tablename;
  354:     if (defined($condition)) {
  355:         $request .= ' WHERE '.$condition;
  356:     }
  357:     my $sth = $dbh->prepare($request);
  358:     if ($sth->err) {
  359:         $error = $sth->errstr;
  360:     }
  361:     if (! $error) {
  362:         $sth->execute();
  363:         if ($sth->err) {
  364:             $error = $sth->errstr;
  365:         } else {
  366:             $returnvalue = $sth->fetchall_arrayref($fetchparameter);
  367:             if ($sth->err) {
  368:                 $error = $sth->errstr;
  369:             }
  370:         }
  371:     }
  372:     return ($error,$returnvalue);
  373: }
  374: 
  375: ######################################################################
  376: ######################################################################
  377: 
  378: =pod
  379: 
  380: =item delete_metadata()
  381: 
  382: Removes a single metadata record, based on its url.
  383: 
  384: Inputs: $dbh, the database handler.
  385: $tablename, the name of the metadata table to remove from. default: 'metadata'
  386: $url, the url of the resource to remove from the metadata database.
  387: 
  388: Returns: undef on success, dbh errorstr on failure.
  389: 
  390: =cut
  391: 
  392: ######################################################################
  393: ######################################################################
  394: sub delete_metadata {
  395:     my ($dbh,$tablename,$url) = @_;
  396:     $tablename = 'metadata' if (! defined($tablename));
  397:     my $error;
  398:     my $delete_command = 'DELETE FROM '.$tablename.' WHERE url='.
  399:         $dbh->quote($url);
  400:     $dbh->do($delete_command);
  401:     if ($dbh->err) {
  402:         $error = $dbh->errstr();
  403:     }
  404:     return $error;
  405: }
  406: 
  407: ######################################################################
  408: ######################################################################
  409: 
  410: =pod
  411: 
  412: =item update_metadata
  413: 
  414: Updates metadata record in mysql database.  It does not matter if the record
  415: currently exists.  Fields not present in the new metadata will be taken
  416: from the current record, if it exists.  To delete an entry for a key, set 
  417: it to "" or undef.
  418: 
  419: Inputs: 
  420: $dbh, database handle
  421: $newmetadata, hash reference containing the new metadata
  422: $tablename, metadata table name.  Defaults to 'metadata'.
  423: 
  424: Returns:
  425: $error on failure.  undef on success.
  426: 
  427: =cut
  428: 
  429: ######################################################################
  430: ######################################################################
  431: sub update_metadata {
  432:     my ($dbh,$tablename,$newmetadata)=@_;
  433:     my $error;
  434:     $tablename = 'metadata' if (! defined($tablename));
  435:     if (! exists($newmetadata->{'url'})) {
  436:         $error = 'Unable to update: no url specified';
  437:     }
  438:     return $error if (defined($error));
  439:     # 
  440:     # Retrieve current values
  441:     my $row;
  442:     ($error,$row) = &lookup_metadata($dbh,
  443:                                    ' url='.$dbh->quote($newmetadata->{'url'}),
  444:                                      undef,$tablename);
  445:     return $error if ($error);
  446:     my %metadata = &LONCAPA::lonmetadata::metadata_col_to_hash(@{$row->[0]});
  447:     #
  448:     # Update metadata values
  449:     while (my ($key,$value) = each(%$newmetadata)) {
  450:         $metadata{$key} = $value;
  451:     }
  452:     #
  453:     # Delete old data (deleting a nonexistant record does not produce an error.
  454:     $error = &delete_metadata($dbh,$tablename,$newmetadata->{'url'});
  455:     return $error if (defined($error));
  456:     #
  457:     # Store updated metadata
  458:     my $success;
  459:     ($success,$error) = &store_metadata($dbh,$tablename,\%metadata);
  460:     return $error;
  461: }
  462: 
  463: ######################################################################
  464: ######################################################################
  465: 
  466: =pod
  467: 
  468: =item metdata_col_to_hash
  469: 
  470: Input: Array of metadata columns
  471: 
  472: Return: Hash with the metadata columns as keys and the array elements
  473: passed in as values
  474: 
  475: =cut
  476: 
  477: ######################################################################
  478: ######################################################################
  479: sub metadata_col_to_hash {
  480:     my @cols=@_;
  481:     my %hash=();
  482:     for (my $i=0; $i<=$#Metadata_Table_Description;$i++) {
  483:         $hash{$Metadata_Table_Description[$i]->{'name'}}=$cols[$i];
  484:     }
  485:     return %hash;
  486: }
  487: 
  488: ######################################################################
  489: ######################################################################
  490: 
  491: =pod
  492: 
  493: =item nohist_resevaldata.db data structure
  494: 
  495: The nohist_resevaldata.db file has the following possible keys:
  496: 
  497:  Statistics Data (values are integers, perl times, or real numbers)
  498:  ------------------------------------------
  499:  $course___$resource___avetries
  500:  $course___$resource___count
  501:  $course___$resource___difficulty
  502:  $course___$resource___stdno
  503:  $course___$resource___timestamp
  504: 
  505:  Evaluation Data (values are on a 1 to 5 scale)
  506:  ------------------------------------------
  507:  $username@$dom___$resource___clear
  508:  $username@$dom___$resource___comments
  509:  $username@$dom___$resource___depth
  510:  $username@$dom___$resource___technical
  511:  $username@$dom___$resource___helpful
  512:  $username@$dom___$resource___correct
  513: 
  514:  Course Context Data
  515:  ------------------------------------------
  516:  $course___$resource___course       course id
  517:  $course___$resource___comefrom     resource preceeding this resource
  518:  $course___$resource___goto         resource following this resource
  519:  $course___$resource___usage        resource containing this resource
  520: 
  521:  New statistical data storage
  522:  ------------------------------------------
  523:  $course&$sec&$numstud___$resource___stats
  524:     $sec is a string describing the sections: all, 1 2, 1 2 3,...
  525:     Value is a '&' deliminated list of key=value pairs.
  526:     Possible keys are (currently) disc,course,sections,difficulty, 
  527:     stdno, timestamp
  528: 
  529: =cut
  530: 
  531: ######################################################################
  532: ######################################################################
  533: 
  534: =pod
  535: 
  536: =item &process_reseval_data 
  537: 
  538: Process a nohist_resevaldata hash into a more complex data structure.
  539: 
  540: Input: Hash reference containing reseval data
  541: 
  542: Returns: Hash with the following structure:
  543: 
  544: $hash{$url}->{'statistics'}->{$courseid}->{'avetries'}   = $value
  545: $hash{$url}->{'statistics'}->{$courseid}->{'count'}      = $value
  546: $hash{$url}->{'statistics'}->{$courseid}->{'difficulty'} = $value
  547: $hash{$url}->{'statistics'}->{$courseid}->{'stdno'}      = $value
  548: $hash{$url}->{'statistics'}->{$courseid}->{'timestamp'}  = $value
  549: 
  550: $hash{$url}->{'evaluation'}->{$username}->{'clear'}     = $value
  551: $hash{$url}->{'evaluation'}->{$username}->{'comments'}  = $value
  552: $hash{$url}->{'evaluation'}->{$username}->{'depth'}     = $value
  553: $hash{$url}->{'evaluation'}->{$username}->{'technical'} = $value
  554: $hash{$url}->{'evaluation'}->{$username}->{'helpful'}   = $value
  555: 
  556: $hash{$url}->{'course'}    = \@Courses
  557: $hash{$url}->{'comefrom'}  = \@Resources
  558: $hash{$url}->{'goto'}      = \@Resources
  559: $hash{$url}->{'usage'}     = \@Resources
  560: 
  561: $hash{$url}->{'stats'}->{$courseid\_$section}->{$key} = $value
  562: 
  563: =cut
  564: 
  565: ######################################################################
  566: ######################################################################
  567: sub process_reseval_data {
  568:     my ($evaldata) = @_;
  569:     my %DynamicData;
  570:     #
  571:     # Process every stored element
  572:     while (my ($storedkey,$value) = each(%{$evaldata})) {
  573:         my ($source,$file,$type) = split('___',$storedkey);
  574:         $source = &unescape($source);
  575:         $file = &unescape($file);
  576:         $value = &unescape($value);
  577:          "    got ".$file."\n        ".$type." ".$source."\n";
  578:         if ($type =~ /^(avetries|count|difficulty|stdno|timestamp)$/) {
  579:             #
  580:             # Statistics: $source is course id
  581:             $DynamicData{$file}->{'statistics'}->{$source}->{$type}=$value;
  582:         } elsif ($type =~ /^(clear|comments|depth|technical|helpful|correct)$/){
  583:             #
  584:             # Evaluation $source is username, check if they evaluated it
  585:             # more than once.  If so, pad the entry with a space.
  586:             while(exists($DynamicData{$file}->{'evaluation'}->{$type}->{$source})) {
  587:                 $source .= ' ';
  588:             }
  589:             $DynamicData{$file}->{'evaluation'}->{$type}->{$source}=$value;
  590:         } elsif ($type =~ /^(course|comefrom|goto|usage)$/) {
  591:             #
  592:             # Context $source is course id or resource
  593:             push(@{$DynamicData{$file}->{$type}},&unescape($source));
  594:         } elsif ($type eq 'stats') {
  595:             #
  596:             # Statistics storage...
  597:             # $source is $cid\_$sec\_$stdno
  598:             # $value is stat1=value&stat2=value&stat3=value,....
  599:             #
  600:             my ($cid,$sec,$stdno)=split('&',$source);
  601:             my $crssec = $cid.'&'.$sec;
  602:             my @Data = split('&',$value);
  603:             my %Statistics;
  604:             while (my ($key,$value) = split('=',pop(@Data))) {
  605:                 $Statistics{$key} = $value;
  606:             }
  607:             $sec =~ s:("$|^")::g;
  608:             $Statistics{'sections'} = $sec;
  609:             #
  610:             # Only store the data if the number of students is greater
  611:             # than the data already stored
  612:             if (! exists($DynamicData{$file}->{'stats'}->{$crssec}) ||
  613:                 $DynamicData{$file}->{'stats'}->{$crssec}->{'stdno'}<$stdno){
  614:                 $DynamicData{$file}->{'stats'}->{$crssec}=\%Statistics;
  615:             }
  616:         }
  617:     }
  618:     return %DynamicData;
  619: }
  620: 
  621: 
  622: ######################################################################
  623: ######################################################################
  624: 
  625: =pod
  626: 
  627: =item &process_dynamic_metadata
  628: 
  629: Inputs: $url: the url of the item to process
  630: $DynamicData: hash reference for the results of &process_reseval_data
  631: 
  632: Returns: Hash containing the following keys:
  633:     avetries, avetries_list, difficulty, difficulty_list, stdno, stdno_list,
  634:     course, course_list, goto, goto_list, comefrom, comefrom_list,
  635:     usage, clear, technical, correct, helpful, depth, comments
  636: 
  637:     Each of the return keys is associated with either a number or a string
  638:     The *_list items are comma-seperated strings.  'comments' is a string
  639:     containing generically marked-up comments.
  640: 
  641: =cut
  642: 
  643: ######################################################################
  644: ######################################################################
  645: sub process_dynamic_metadata {
  646:     my ($url,$DynamicData) = @_;
  647:     my %data;
  648:     my $resdata = $DynamicData->{$url};
  649:     #
  650:     # Get the statistical data - Use a weighted average
  651:     foreach my $type (qw/avetries difficulty disc/) {
  652:         my $studentcount;
  653:         my $sum;
  654:         my @Values;
  655:         my @Students;
  656:         #
  657:         # Old data
  658:         foreach my $coursedata (values(%{$resdata->{'statistics'}}),
  659:                                 values(%{$resdata->{'stats'}})) {
  660:             if (ref($coursedata) eq 'HASH' && exists($coursedata->{$type})) {
  661:                 $studentcount += $coursedata->{'stdno'};
  662:                 $sum += ($coursedata->{$type}*$coursedata->{'stdno'});
  663:                 push(@Values,$coursedata->{$type});
  664:                 push(@Students,$coursedata->{'stdno'});
  665:             }
  666:         }
  667:         if (exists($resdata->{'stats'})) {
  668:             foreach my $identifier (sort(keys(%{$resdata->{'stats'}}))) {
  669:                 my $coursedata = $resdata->{'stats'}->{$identifier};
  670:                 $studentcount += $coursedata->{'stdno'};
  671:                 $sum += $coursedata->{$type}*$coursedata->{'stdno'};
  672:                 push(@Values,$coursedata->{$type});                
  673:                 push(@Students,$coursedata->{'stdno'});
  674:             }
  675:         }
  676:         #
  677:         # New data
  678:         if (defined($studentcount) && $studentcount>0) {
  679:             $data{$type} = $sum/$studentcount;
  680:             $data{$type.'_list'} = join(',',@Values);
  681:         }
  682:     }
  683:     #
  684:     # Find out the number of students who have completed the resource...
  685:     my $stdno;
  686:     foreach my $coursedata (values(%{$resdata->{'statistics'}}),
  687:                             values(%{$resdata->{'stats'}})) {
  688:         if (ref($coursedata) eq 'HASH' && exists($coursedata->{'stdno'})) {
  689:             $stdno += $coursedata->{'stdno'};
  690:         }
  691:     }
  692:     if (exists($resdata->{'stats'})) {
  693:         #
  694:         # For the number of students, take the maximum found for the class
  695:         my $current_course;
  696:         my $coursemax=0;
  697:         foreach my $identifier (sort(keys(%{$resdata->{'stats'}}))) {
  698:             my $coursedata = $resdata->{'stats'}->{$identifier};
  699:             if (! defined($current_course)) {
  700:                 $current_course = $coursedata->{'course'};
  701:             }
  702:             if ($current_course ne $coursedata->{'course'}) {
  703:                 $stdno += $coursemax;
  704:                 $coursemax = 0;
  705:                 $current_course = $coursedata->{'course'};                
  706:             }
  707:             if ($coursemax < $coursedata->{'stdno'}) {
  708:                 $coursemax = $coursedata->{'stdno'};
  709:             }
  710:         }
  711:         $stdno += $coursemax; # pick up the final course in the list
  712:     }
  713:     $data{'stdno'}=$stdno;
  714:     #
  715:     # Get the context data
  716:     foreach my $type (qw/course goto comefrom/) {
  717:         if (defined($resdata->{$type}) && 
  718:             ref($resdata->{$type}) eq 'ARRAY') {
  719:             $data{$type} = scalar(@{$resdata->{$type}});
  720:             $data{$type.'_list'} = join(',',@{$resdata->{$type}});
  721:         }
  722:     }
  723:     if (defined($resdata->{'usage'}) && 
  724:         ref($resdata->{'usage'}) eq 'ARRAY') {
  725:         $data{'sequsage'} = scalar(@{$resdata->{'usage'}});
  726:         $data{'sequsage_list'} = join(',',@{$resdata->{'usage'}});
  727:     }
  728:     #
  729:     # Get the evaluation data
  730:     foreach my $type (qw/clear technical correct helpful depth/) {
  731:         my $count;
  732:         my $sum;
  733:         foreach my $evaluator (keys(%{$resdata->{'evaluation'}->{$type}})){
  734:             $sum += $resdata->{'evaluation'}->{$type}->{$evaluator};
  735:             $count++;
  736:         }
  737:         if ($count > 0) {
  738:             $data{$type}=$sum/$count;
  739:         }
  740:     }
  741:     #
  742:     # put together comments
  743:     my $comments = '<div class="LCevalcomments">';
  744:     foreach my $evaluator (keys(%{$resdata->{'evaluation'}->{'comments'}})){
  745:         $comments .= 
  746:             '<p>'.
  747:             '<b>'.$evaluator.'</b>:'.
  748:             $resdata->{'evaluation'}->{'comments'}->{$evaluator}.
  749:             '</p>';
  750:     }
  751:     $comments .= '</div>';
  752:     $data{'comments'} = $comments;
  753:     #
  754:     if (exists($resdata->{'stats'})) {
  755:         $data{'stats'} = $resdata->{'stats'};
  756:     }
  757:     #
  758:     return %data;
  759: }
  760: 
  761: sub dynamic_metadata_storage {
  762:     my ($data) = @_;
  763:     my %Store;
  764:     my $courseid = $data->{'course'};
  765:     my $sections = $data->{'sections'};
  766:     my $numstu = $data->{'num_students'};
  767:     my $urlres = $data->{'urlres'};
  768:     my $key = $courseid.'&'.$sections.'&'.$numstu.'___'.$urlres.'___stats';
  769:     $Store{$key} =
  770:         'course='.$courseid.'&'.
  771:         'sections='.$sections.'&'.
  772:         'timestamp='.time.'&'.
  773:         'stdno='.$data->{'num_students'}.'&'.
  774:         'avetries='.$data->{'mean_tries'}.'&'.
  775:         'difficulty='.$data->{'deg_of_diff'};
  776:     if (exists($data->{'deg_of_disc'})) {
  777:         $Store{$key} .= '&'.'disc='.$data->{'deg_of_disc'};
  778:     }
  779:     return %Store;
  780: }
  781: 
  782: ######################################################################
  783: ######################################################################
  784: ##
  785: ## The usual suspects, repeated here to reduce dependency hell
  786: ##
  787: ######################################################################
  788: ######################################################################
  789: sub unescape {
  790:     my $str=shift;
  791:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  792:     return $str;
  793: }
  794: 
  795: sub escape {
  796:     my $str=shift;
  797:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  798:     return $str;
  799: }
  800: 
  801: 1;
  802: 
  803: __END__;
  804: 
  805: =pod
  806: 
  807: =back
  808: 
  809: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>