Annotation of loncom/interface/loncoursedata.pm, revision 1.40

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: # (Publication Handler
                      3: #
1.40    ! minaeibi    4: # $Id: loncoursedata.pm,v 1.39 2002/10/22 16:34:24 minaeibi Exp $
1.1       stredwic    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
                     34: loncoursedata
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
1.22      stredwic   38: Set of functions that download and process student and course information.
1.1       stredwic   39: 
                     40: =head1 PACKAGES USED
                     41: 
                     42:  Apache::Constants qw(:common :http)
                     43:  Apache::lonnet()
1.22      stredwic   44:  Apache::lonhtmlcommon
1.1       stredwic   45:  HTML::TokeParser
                     46:  GDBM_File
                     47: 
                     48: =cut
                     49: 
                     50: package Apache::loncoursedata;
                     51: 
                     52: use strict;
                     53: use Apache::Constants qw(:common :http);
                     54: use Apache::lonnet();
1.13      stredwic   55: use Apache::lonhtmlcommon;
1.1       stredwic   56: use HTML::TokeParser;
                     57: use GDBM_File;
                     58: 
                     59: =pod
                     60: 
                     61: =head1 DOWNLOAD INFORMATION
                     62: 
1.22      stredwic   63: This section contains all the functions that get data from other servers 
                     64: and/or itself.
1.1       stredwic   65: 
                     66: =cut
                     67: 
                     68: # ----- DOWNLOAD INFORMATION -------------------------------------------
                     69: 
                     70: =pod
                     71: 
1.3       stredwic   72: =item &DownloadClasslist()
1.1       stredwic   73: 
                     74: Collects lastname, generation, middlename, firstname, PID, and section for each
1.22      stredwic   75: student from their environment database.  The section data is also download, though
                     76: it is in a rough format, and is processed later.  The list of students is built from
                     77: collecting a classlist for the course that is to be displayed.  Once the classlist
                     78: has been downloaded, its date stamp is recorded.  Unless the datestamp for the
                     79: class database is reset or is modified, this data will not be downloaded again.  
                     80: Also, there was talk about putting the fullname and section
                     81: and perhaps other pieces of data into the classlist file.  This would
                     82: reduce the number of different file accesses and reduce the amount of 
                     83: processing on this side.
1.1       stredwic   84: 
                     85: =over 4
                     86: 
1.21      matthew    87: Input: $courseID, $lastDownloadTime, $c
1.1       stredwic   88: 
                     89: $courseID:  The id of the course
                     90: 
1.22      stredwic   91: $lastDownloadTime:  This is the date stamp for when this information was
1.23      stredwic   92: last gathered.  If it is set to Not downloaded, it will gather the data
1.22      stredwic   93: again, though it currently does not remove the old data.
1.21      matthew    94: 
1.1       stredwic   95: $c: The connection class that can determine if the browser has aborted.  It
1.21      matthew    96: is used to short circuit this function so that it does not continue to 
1.1       stredwic   97: get information when there is no need.
                     98: 
                     99: Output: \%classlist
                    100: 
                    101: \%classlist: A pointer to a hash containing the following data:
                    102: 
                    103: -A list of student name:domain (as keys) (known below as $name)
                    104: 
                    105: -A hash pointer for each student containing lastname, generation, firstname,
1.23      stredwic  106: middlename, and PID : Key is $name.studentInformation
1.1       stredwic  107: 
                    108: -A hash pointer to each students section data : Key is $name.section
                    109: 
1.22      stredwic  110: -If there was an error in dump, it will be returned in the hash.  See
                    111: the error codes for dump in lonnet.  Also, an error key will be 
                    112: generated if an abort occurs.
                    113: 
1.1       stredwic  114: =back
                    115: 
                    116: =cut
                    117: 
1.3       stredwic  118: sub DownloadClasslist {
                    119:     my ($courseID, $lastDownloadTime, $c)=@_;
1.1       stredwic  120:     my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
1.3       stredwic  121:     my %classlist;
1.1       stredwic  122: 
1.22      stredwic  123:     my $modifiedTime = &Apache::lonnet::GetFileTimestamp($courseDomain, $courseNumber,
                    124:                                                          'classlist.db', 
                    125:                                                          $Apache::lonnet::perlvar{'lonUsersDir'});
                    126: 
                    127:     # Always download the information if lastDownloadTime is set to
1.23      stredwic  128:     # Not downloaded, otherwise it is only downloaded if the file
1.22      stredwic  129:     # has been updated and has a more recent date stamp
1.7       stredwic  130:     if($lastDownloadTime ne 'Not downloaded' &&
                    131:        $lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
1.22      stredwic  132:         # Data is not gathered so return UpToDate as true.  This
                    133:         # will be interpreted in ProcessClasslist
1.7       stredwic  134:         $classlist{'lastDownloadTime'}=time;
                    135:         $classlist{'UpToDate'} = 'true';
                    136:         return \%classlist;
                    137:     }
1.3       stredwic  138: 
                    139:     %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
1.20      stredwic  140:     foreach(keys (%classlist)) {
                    141:         if(/^(con_lost|error|no_such_host)/i) {
1.33      albertel  142: 	    return;
1.20      stredwic  143:         }
1.1       stredwic  144:     }
                    145: 
                    146:     foreach my $name (keys(%classlist)) {
1.22      stredwic  147:         if(defined($c) && ($c->aborted())) {
1.1       stredwic  148:             $classlist{'error'}='aborted';
                    149:             return \%classlist;
                    150:         }
                    151: 
                    152:         my ($studentName,$studentDomain) = split(/\:/,$name);
                    153:         # Download student environment data, specifically the full name and id.
                    154:         my %studentInformation=&Apache::lonnet::get('environment',
                    155:                                                     ['lastname','generation',
                    156:                                                      'firstname','middlename',
                    157:                                                      'id'],
                    158:                                                     $studentDomain,
                    159:                                                     $studentName);
                    160:         $classlist{$name.':studentInformation'}=\%studentInformation;
                    161: 
                    162:         if($c->aborted()) {
                    163:             $classlist{'error'}='aborted';
                    164:             return \%classlist;
                    165:         }
                    166: 
                    167:         #Section
                    168:         my %section=&Apache::lonnet::dump('roles',$studentDomain,$studentName);
1.3       stredwic  169:         $classlist{$name.':sections'}=\%section;
1.1       stredwic  170:     }
                    171: 
1.3       stredwic  172:     $classlist{'UpToDate'} = 'false';
                    173:     $classlist{'lastDownloadTime'}=time;
                    174: 
1.1       stredwic  175:     return \%classlist;
                    176: }
                    177: 
                    178: =pod
                    179: 
1.4       stredwic  180: =item &DownloadCourseInformation()
1.1       stredwic  181: 
1.22      stredwic  182: Dump of all the course information for a single student.  The data can be
                    183: pruned by making use of dumps regular expression arguement.  This function
                    184: also takes a regular expression which it passes straight through to dump.  
                    185: The data is no escaped, because it is done elsewhere.  It also
1.3       stredwic  186: checks the timestamp of the students course database file and only downloads
                    187: if it has been modified since the last download.
1.1       stredwic  188: 
                    189: =over 4
                    190: 
1.22      stredwic  191: Input: $namedata, $courseID, $lastDownloadTime, $WhatIWant
1.1       stredwic  192: 
1.22      stredwic  193: $namedata: student name:domain
1.1       stredwic  194: 
                    195: $courseID:  The id of the course
                    196: 
1.22      stredwic  197: $lastDownloadTime:  This is the date stamp for when this information was
1.23      stredwic  198: last gathered.  If it is set to Not downloaded, it will gather the data
1.22      stredwic  199: again, though it currently does not remove the old data.
                    200: 
                    201: $WhatIWant:  Regular expression used to get selected data with dump
                    202: 
1.1       stredwic  203: Output: \%courseData
                    204: 
1.23      stredwic  205: \%courseData:  A hash pointer to the raw data from the students course
1.1       stredwic  206: database.
                    207: 
                    208: =back
                    209: 
                    210: =cut
                    211: 
1.4       stredwic  212: sub DownloadCourseInformation {
1.12      stredwic  213:     my ($namedata,$courseID,$lastDownloadTime,$WhatIWant)=@_;
1.3       stredwic  214:     my %courseData;
1.4       stredwic  215:     my ($name,$domain) = split(/\:/,$namedata);
1.1       stredwic  216: 
1.22      stredwic  217:     my $modifiedTime = &Apache::lonnet::GetFileTimestamp($domain, $name,
1.7       stredwic  218:                                       $courseID.'.db', 
                    219:                                       $Apache::lonnet::perlvar{'lonUsersDir'});
                    220: 
1.13      stredwic  221:     if($lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
1.22      stredwic  222:         # Data is not gathered so return UpToDate as true.  This
                    223:         # will be interpreted in ProcessClasslist
1.13      stredwic  224:         $courseData{$namedata.':lastDownloadTime'}=time;
                    225:         $courseData{$namedata.':UpToDate'} = 'true';
1.7       stredwic  226:         return \%courseData;
                    227:     }
1.3       stredwic  228: 
1.4       stredwic  229:     # Download course data
1.12      stredwic  230:     if(!defined($WhatIWant)) {
1.22      stredwic  231:         # set the regular expression to everything by setting it to period
1.12      stredwic  232:         $WhatIWant = '.';
                    233:     }
                    234:     %courseData=&Apache::lonnet::dump($courseID, $domain, $name, $WhatIWant);
1.3       stredwic  235:     $courseData{'UpToDate'} = 'false';
                    236:     $courseData{'lastDownloadTime'}=time;
1.13      stredwic  237: 
                    238:     my %newData;
                    239:     foreach (keys(%courseData)) {
1.22      stredwic  240:         # need to have the keys to be prepended with the name:domain of the
                    241:         # student to reduce data collision later.
1.13      stredwic  242:         $newData{$namedata.':'.$_} = $courseData{$_};
                    243:     }
                    244: 
                    245:     return \%newData;
1.1       stredwic  246: }
                    247: 
                    248: # ----- END DOWNLOAD INFORMATION ---------------------------------------
                    249: 
                    250: =pod
                    251: 
                    252: =head1 PROCESSING FUNCTIONS
                    253: 
                    254: These functions process all the data for all the students.  Also, they
1.22      stredwic  255: are the functions that access the cache database for writing the majority of
                    256: the time.  The downloading and caching were separated to reduce problems 
1.23      stredwic  257: with stopping downloading then can not tie hash to database later.
1.1       stredwic  258: 
                    259: =cut
                    260: 
                    261: # ----- PROCESSING FUNCTIONS ---------------------------------------
                    262: 
                    263: =pod
                    264: 
                    265: =item &ProcessTopResourceMap()
                    266: 
                    267: Trace through the "big hash" created in rat/lonuserstate.pm::loadmap.  
                    268: Basically, this function organizes a subset of the data and stores it in
                    269: cached data.  The data stored is the problems, sequences, sequence titles,
                    270: parts of problems, and their ordering.  Column width information is also 
                    271: partially handled here on a per sequence basis.
                    272: 
                    273: =over 4
                    274: 
                    275: Input: $cache, $c
                    276: 
                    277: $cache:  A pointer to a hash to store the information
                    278: 
                    279: $c:  The connection class used to determine if an abort has been sent to the 
                    280: browser
                    281: 
                    282: Output: A string that contains an error message or "OK" if everything went 
                    283: smoothly.
                    284: 
                    285: =back
                    286: 
                    287: =cut
                    288: 
                    289: sub ProcessTopResourceMap {
1.11      stredwic  290:     my ($cache,$c)=@_;
1.1       stredwic  291:     my %hash;
                    292:     my $fn=$ENV{'request.course.fn'};
                    293:     if(-e "$fn.db") {
                    294: 	my $tieTries=0;
                    295: 	while($tieTries < 3) {
                    296:             if($c->aborted()) {
                    297:                 return;
                    298:             }
1.10      stredwic  299: 	    if(tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
1.1       stredwic  300: 		last;
                    301: 	    }
                    302: 	    $tieTries++;
                    303: 	    sleep 1;
                    304: 	}
                    305: 	if($tieTries >= 3) {
                    306:             return 'Coursemap undefined.';
                    307:         }
                    308:     } else {
                    309:         return 'Can not open Coursemap.';
                    310:     }
                    311: 
1.28      stredwic  312:     my $oldkeys;
1.40    ! minaeibi  313:     delete $cache->{'OptionResponses'};
1.28      stredwic  314:     if(defined($cache->{'ResourceKeys'})) {
                    315:         $oldkeys = $cache->{'ResourceKeys'};
                    316:         foreach (split(':::', $cache->{'ResourceKeys'})) {
                    317:             delete $cache->{$_};
                    318:         }
                    319:         delete $cache->{'ResourceKeys'};
                    320:     }
                    321: 
1.1       stredwic  322:     # Initialize state machine.  Set information pointing to top level map.
                    323:     my (@sequences, @currentResource, @finishResource);
                    324:     my ($currentSequence, $currentResourceID, $lastResourceID);
                    325: 
1.31      www       326:     $currentResourceID=$hash{'ids_'.
                    327:       &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1.1       stredwic  328:     push(@currentResource, $currentResourceID);
                    329:     $lastResourceID=-1;
                    330:     $currentSequence=-1;
                    331:     my $topLevelSequenceNumber = $currentSequence;
                    332: 
1.11      stredwic  333:     my %sequenceRecord;
1.28      stredwic  334:     my %allkeys;
1.1       stredwic  335:     while(1) {
                    336:         if($c->aborted()) {
                    337:             last;
                    338:         }
                    339: 	# HANDLE NEW SEQUENCE!
                    340: 	#if page || sequence
1.11      stredwic  341: 	if(defined($hash{'map_pc_'.$hash{'src_'.$currentResourceID}}) &&
                    342:            !defined($sequenceRecord{$currentResourceID})) {
                    343:             $sequenceRecord{$currentResourceID}++;
1.1       stredwic  344: 	    push(@sequences, $currentSequence);
                    345: 	    push(@currentResource, $currentResourceID);
                    346: 	    push(@finishResource, $lastResourceID);
                    347: 
                    348: 	    $currentSequence=$hash{'map_pc_'.$hash{'src_'.$currentResourceID}};
                    349: 
                    350:             # Mark sequence as containing problems.  If it doesn't, then
                    351:             # it will be removed when processing for this sequence is
                    352:             # complete.  This allows the problems in a sequence
                    353:             # to be outputed before problems in the subsequences
                    354:             if(!defined($cache->{'orderedSequences'})) {
                    355:                 $cache->{'orderedSequences'}=$currentSequence;
                    356:             } else {
                    357:                 $cache->{'orderedSequences'}.=':'.$currentSequence;
                    358:             }
1.28      stredwic  359:             $allkeys{'orderedSequences'}++;
1.1       stredwic  360: 
                    361: 	    $lastResourceID=$hash{'map_finish_'.
                    362: 				  $hash{'src_'.$currentResourceID}};
                    363: 	    $currentResourceID=$hash{'map_start_'.
                    364: 				     $hash{'src_'.$currentResourceID}};
                    365: 
                    366: 	    if(!($currentResourceID) || !($lastResourceID)) {
                    367: 		$currentSequence=pop(@sequences);
                    368: 		$currentResourceID=pop(@currentResource);
                    369: 		$lastResourceID=pop(@finishResource);
                    370: 		if($currentSequence eq $topLevelSequenceNumber) {
                    371: 		    last;
                    372: 		}
                    373: 	    }
1.12      stredwic  374:             next;
1.1       stredwic  375: 	}
                    376: 
                    377: 	# Handle gradable resources: exams, problems, etc
                    378: 	$currentResourceID=~/(\d+)\.(\d+)/;
                    379:         my $partA=$1;
                    380:         my $partB=$2;
                    381: 	if($hash{'src_'.$currentResourceID}=~
                    382: 	   /\.(problem|exam|quiz|assess|survey|form)$/ &&
1.11      stredwic  383: 	   $partA eq $currentSequence && 
                    384:            !defined($sequenceRecord{$currentSequence.':'.
                    385:                                     $currentResourceID})) {
                    386:             $sequenceRecord{$currentSequence.':'.$currentResourceID}++;
1.1       stredwic  387: 	    my $Problem = &Apache::lonnet::symbclean(
                    388: 			  &Apache::lonnet::declutter($hash{'map_id_'.$partA}).
                    389: 			  '___'.$partB.'___'.
                    390: 			  &Apache::lonnet::declutter($hash{'src_'.
                    391: 							 $currentResourceID}));
                    392: 
                    393: 	    $cache->{$currentResourceID.':problem'}=$Problem;
1.28      stredwic  394:             $allkeys{$currentResourceID.':problem'}++;
1.1       stredwic  395: 	    if(!defined($cache->{$currentSequence.':problems'})) {
                    396: 		$cache->{$currentSequence.':problems'}=$currentResourceID;
                    397: 	    } else {
                    398: 		$cache->{$currentSequence.':problems'}.=
                    399: 		    ':'.$currentResourceID;
                    400: 	    }
1.28      stredwic  401:             $allkeys{$currentSequence.':problems'}++;
1.1       stredwic  402: 
1.2       stredwic  403: 	    my $meta=$hash{'src_'.$currentResourceID};
                    404: #            $cache->{$currentResourceID.':title'}=
                    405: #                &Apache::lonnet::metdata($meta,'title');
                    406:             $cache->{$currentResourceID.':title'}=
                    407:                 $hash{'title_'.$currentResourceID};
1.28      stredwic  408:             $allkeys{$currentResourceID.':title'}++;
1.9       minaeibi  409:             $cache->{$currentResourceID.':source'}=
                    410:                 $hash{'src_'.$currentResourceID};
1.28      stredwic  411:             $allkeys{$currentResourceID.':source'}++;
1.2       stredwic  412: 
1.1       stredwic  413:             # Get Parts for problem
1.8       stredwic  414:             my %beenHere;
                    415:             foreach (split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
                    416:                 if(/^\w+response_\d+.*/) {
                    417:                     my (undef, $partId, $responseId) = split(/_/,$_);
                    418:                     if($beenHere{'p:'.$partId} ==  0) {
                    419:                         $beenHere{'p:'.$partId}++;
                    420:                         if(!defined($cache->{$currentSequence.':'.
                    421:                                             $currentResourceID.':parts'})) {
                    422:                             $cache->{$currentSequence.':'.$currentResourceID.
                    423:                                      ':parts'}=$partId;
                    424:                         } else {
                    425:                             $cache->{$currentSequence.':'.$currentResourceID.
                    426:                                      ':parts'}.=':'.$partId;
                    427:                         }
1.28      stredwic  428:                         $allkeys{$currentSequence.':'.$currentResourceID.
                    429:                                   ':parts'}++;
1.8       stredwic  430:                     }
                    431:                     if($beenHere{'r:'.$partId.':'.$responseId} == 0) {
                    432:                         $beenHere{'r:'.$partId.':'.$responseId}++;
                    433:                         if(!defined($cache->{$currentSequence.':'.
                    434:                                              $currentResourceID.':'.$partId.
                    435:                                              ':responseIDs'})) {
                    436:                             $cache->{$currentSequence.':'.$currentResourceID.
                    437:                                      ':'.$partId.':responseIDs'}=$responseId;
                    438:                         } else {
                    439:                             $cache->{$currentSequence.':'.$currentResourceID.
                    440:                                      ':'.$partId.':responseIDs'}.=':'.
                    441:                                                                   $responseId;
                    442:                         }
1.28      stredwic  443:                         $allkeys{$currentSequence.':'.$currentResourceID.':'.
                    444:                                      $partId.':responseIDs'}++;
1.1       stredwic  445:                     }
1.8       stredwic  446:                     if(/^optionresponse/ && 
                    447:                        $beenHere{'o:'.$partId.':'.$currentResourceID} == 0) {
                    448:                         $beenHere{'o:'.$partId.$currentResourceID}++;
                    449:                         if(defined($cache->{'OptionResponses'})) {
                    450:                             $cache->{'OptionResponses'}.= ':::'.
1.16      stredwic  451:                                 $currentSequence.':'.$currentResourceID.':'.
                    452:                                 $partId.':'.$responseId;
                    453:                         } else {
                    454:                             $cache->{'OptionResponses'}= $currentSequence.':'.
1.8       stredwic  455:                                 $currentResourceID.':'.
                    456:                                 $partId.':'.$responseId;
1.2       stredwic  457:                         }
1.28      stredwic  458:                         $allkeys{'OptionResponses'}++;
1.2       stredwic  459:                     }
                    460:                 }
1.8       stredwic  461:             }
                    462:         }
1.1       stredwic  463: 
                    464: 	# if resource == finish resource, then it is the end of a sequence/page
                    465: 	if($currentResourceID eq $lastResourceID) {
                    466: 	    # pop off last resource of sequence
                    467: 	    $currentResourceID=pop(@currentResource);
                    468: 	    $lastResourceID=pop(@finishResource);
                    469: 
                    470: 	    if(defined($cache->{$currentSequence.':problems'})) {
                    471: 		# Capture sequence information here
                    472: 		$cache->{$currentSequence.':title'}=
                    473: 		    $hash{'title_'.$currentResourceID};
1.28      stredwic  474:                 $allkeys{$currentSequence.':title'}++;
1.2       stredwic  475:                 $cache->{$currentSequence.':source'}=
                    476:                     $hash{'src_'.$currentResourceID};
1.28      stredwic  477:                 $allkeys{$currentSequence.':source'}++;
1.1       stredwic  478: 
                    479:                 my $totalProblems=0;
                    480:                 foreach my $currentProblem (split(/\:/,
                    481:                                                $cache->{$currentSequence.
                    482:                                                ':problems'})) {
                    483:                     foreach (split(/\:/,$cache->{$currentSequence.':'.
                    484:                                                    $currentProblem.
                    485:                                                    ':parts'})) {
                    486:                         $totalProblems++;
                    487:                     }
                    488:                 }
                    489: 		my @titleLength=split(//,$cache->{$currentSequence.
                    490:                                                     ':title'});
1.39      minaeibi  491:                 # $extra is 5 for problems correct and 3 for space
1.1       stredwic  492:                 # between problems correct and problem output
1.39      minaeibi  493:                 my $extra = 8;
1.1       stredwic  494: 		if(($totalProblems + $extra) > (scalar @titleLength)) {
                    495: 		    $cache->{$currentSequence.':columnWidth'}=
                    496:                         $totalProblems + $extra;
                    497: 		} else {
                    498: 		    $cache->{$currentSequence.':columnWidth'}=
                    499:                         (scalar @titleLength);
                    500: 		}
1.28      stredwic  501:                 $allkeys{$currentSequence.':columnWidth'}++;
1.1       stredwic  502: 	    } else {
                    503:                 # Remove sequence from list, if it contains no problems to
                    504:                 # display.
                    505:                 $cache->{'orderedSequences'}=~s/$currentSequence//;
                    506:                 $cache->{'orderedSequences'}=~s/::/:/g;
                    507:                 $cache->{'orderedSequences'}=~s/^:|:$//g;
                    508:             }
                    509: 
                    510: 	    $currentSequence=pop(@sequences);
                    511: 	    if($currentSequence eq $topLevelSequenceNumber) {
                    512: 		last;
                    513: 	    }
1.11      stredwic  514:         }
1.1       stredwic  515: 
                    516: 	# MOVE!!!
                    517: 	# move to next resource
                    518: 	unless(defined($hash{'to_'.$currentResourceID})) {
                    519: 	    # big problem, need to handle.  Next is probably wrong
1.11      stredwic  520:             my $errorMessage = 'Big problem in ';
                    521:             $errorMessage .= 'loncoursedata::ProcessTopLevelMap.';
                    522:             $errorMessage .= '  bighash to_$currentResourceID not defined!';
                    523:             &Apache::lonnet::logthis($errorMessage);
1.1       stredwic  524: 	    last;
                    525: 	}
                    526: 	my @nextResources=();
                    527: 	foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
1.11      stredwic  528:             if(!defined($sequenceRecord{$currentSequence.':'.
                    529:                                         $hash{'goesto_'.$_}})) {
                    530:                 push(@nextResources, $hash{'goesto_'.$_});
                    531:             }
1.1       stredwic  532: 	}
                    533: 	push(@currentResource, @nextResources);
                    534: 	# Set the next resource to be processed
                    535: 	$currentResourceID=pop(@currentResource);
                    536:     }
                    537: 
1.28      stredwic  538:     my @theKeys = keys(%allkeys);
                    539:     my $newkeys = join(':::', @theKeys);
                    540:     $cache->{'ResourceKeys'} = join(':::', $newkeys);
                    541:     if($newkeys ne $oldkeys) {
                    542:         $cache->{'ResourceUpdated'} = 'true';
                    543:     } else {
                    544:         $cache->{'ResourceUpdated'} = 'false';
                    545:     }
                    546: 
1.1       stredwic  547:     unless (untie(%hash)) {
                    548:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    549:                                  "Could not untie coursemap $fn (browse)".
                    550:                                  ".</font>"); 
                    551:     }
                    552: 
                    553:     return 'OK';
                    554: }
                    555: 
                    556: =pod
                    557: 
1.3       stredwic  558: =item &ProcessClasslist()
1.1       stredwic  559: 
1.3       stredwic  560: Taking the class list dumped from &DownloadClasslist(), all the 
1.1       stredwic  561: students and their non-class information is processed using the 
                    562: &ProcessStudentInformation() function.  A date stamp is also recorded for
                    563: when the data was processed.
                    564: 
1.3       stredwic  565: Takes data downloaded for a student and breaks it up into managable pieces and 
                    566: stored in cache data.  The username, domain, class related date, PID, 
                    567: full name, and section are all processed here.
                    568: 
1.1       stredwic  569: =over 4
                    570: 
                    571: Input: $cache, $classlist, $courseID, $ChartDB, $c
                    572: 
                    573: $cache: A hash pointer to store the data
                    574: 
                    575: $classlist:  The hash of data collected about a student from 
1.3       stredwic  576: &DownloadClasslist().  The hash contains a list of students, a pointer 
1.23      stredwic  577: to a hash of student information for each student, and each students section 
1.1       stredwic  578: number.
                    579: 
                    580: $courseID:  The course ID
                    581: 
                    582: $ChartDB:  The name of the cache database file.
                    583: 
                    584: $c:  The connection class used to determine if an abort has been sent to the 
                    585: browser
                    586: 
                    587: Output: @names
                    588: 
                    589: @names:  An array of students whose information has been processed, and are to 
1.32      matthew   590: be considered in an arbitrary order.  The entries in @names are of the form
                    591: username:domain.
                    592: 
                    593: The values in $cache are as follows:
                    594: 
                    595:  *NOTE: for the following $name implies username:domain
                    596:  $name.':error'                  only defined if an error occured.  Value
                    597:                                  contains the error message
                    598:  $name.':lastDownloadTime'       unconverted time of the last update of a
                    599:                                  student\'s course data
                    600:  $name.'updateTime'              coverted time of the last update of a 
                    601:                                  student\'s course data
                    602:  $name.':username'               username of a student
                    603:  $name.':domain'                 domain of a student
                    604:  $name.':fullname'               full name of a student
                    605:  $name.':id'                     PID of a student
                    606:  $name.':Status'                 active/expired status of a student
                    607:  $name.':section'                section of a student
1.1       stredwic  608: 
                    609: =back
                    610: 
                    611: =cut
                    612: 
1.3       stredwic  613: sub ProcessClasslist {
                    614:     my ($cache,$classlist,$courseID,$c)=@_;
1.1       stredwic  615:     my @names=();
                    616: 
1.3       stredwic  617:     $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
                    618:     if($classlist->{'UpToDate'} eq 'true') {
                    619:         return split(/:::/,$cache->{'NamesOfStudents'});;
                    620:     }
                    621: 
1.1       stredwic  622:     foreach my $name (keys(%$classlist)) {
                    623:         if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
1.3       stredwic  624:            $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
1.1       stredwic  625:             next;
                    626:         }
                    627:         if($c->aborted()) {
1.3       stredwic  628:             return ();
1.1       stredwic  629:         }
1.32      matthew   630:         my $studentInformation = $classlist->{$name.':studentInformation'};
                    631:         my $date = $classlist->{$name};
1.3       stredwic  632:         my ($studentName,$studentDomain) = split(/\:/,$name);
                    633: 
                    634:         $cache->{$name.':username'}=$studentName;
                    635:         $cache->{$name.':domain'}=$studentDomain;
1.10      stredwic  636:         # Initialize timestamp for student
1.3       stredwic  637:         if(!defined($cache->{$name.':lastDownloadTime'})) {
                    638:             $cache->{$name.':lastDownloadTime'}='Not downloaded';
1.6       stredwic  639:             $cache->{$name.':updateTime'}=' Not updated';
1.3       stredwic  640:         }
                    641: 
1.20      stredwic  642:         my $error = 0;
                    643:         foreach(keys(%$studentInformation)) {
                    644:             if(/^(con_lost|error|no_such_host)/i) {
                    645:                 $cache->{$name.':error'}=
                    646:                     'Could not download student environment data.';
                    647:                 $cache->{$name.':fullname'}='';
                    648:                 $cache->{$name.':id'}='';
                    649:                 $error = 1;
                    650:             }
                    651:         }
                    652:         next if($error);
                    653:         push(@names,$name);
                    654:         $cache->{$name.':fullname'}=&ProcessFullName(
1.3       stredwic  655:                                           $studentInformation->{'lastname'},
                    656:                                           $studentInformation->{'generation'},
                    657:                                           $studentInformation->{'firstname'},
                    658:                                           $studentInformation->{'middlename'});
1.20      stredwic  659:         $cache->{$name.':id'}=$studentInformation->{'id'};
1.3       stredwic  660: 
                    661:         my ($end, $start)=split(':',$date);
                    662:         $courseID=~s/\_/\//g;
                    663:         $courseID=~s/^(\w)/\/$1/;
                    664: 
                    665:         my $sec='';
1.32      matthew   666:         my $sectionData = $classlist->{$name.':sections'};
1.3       stredwic  667:         foreach my $key (keys (%$sectionData)) {
                    668:             my $value = $sectionData->{$key};
                    669:             if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
                    670:                 my $tempsection=$1;
                    671:                 if($key eq $courseID.'_st') {
                    672:                     $tempsection='';
                    673:                 }
1.32      matthew   674:                 my (undef,$roleend,$rolestart)=split(/\_/,$value);
1.3       stredwic  675:                 if($roleend eq $end && $rolestart eq $start) {
                    676:                     $sec = $tempsection;
                    677:                     last;
                    678:                 }
                    679:             }
                    680:         }
                    681: 
                    682:         my $status='Expired';
                    683:         if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
                    684:             $status='Active';
                    685:         }
                    686:         $cache->{$name.':Status'}=$status;
                    687:         $cache->{$name.':section'}=$sec;
1.7       stredwic  688: 
                    689:         if($sec eq '' || !defined($sec) || $sec eq ' ') {
                    690:             $sec = 'none';
                    691:         }
                    692:         if(defined($cache->{'sectionList'})) {
                    693:             if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
                    694:                 $cache->{'sectionList'} .= ':'.$sec;
                    695:             }
                    696:         } else {
                    697:             $cache->{'sectionList'} = $sec;
                    698:         }
1.1       stredwic  699:     }
                    700: 
1.3       stredwic  701:     $cache->{'ClasslistTimestamp'}=time;
                    702:     $cache->{'NamesOfStudents'}=join(':::',@names);
1.1       stredwic  703: 
                    704:     return @names;
                    705: }
                    706: 
                    707: =pod
                    708: 
                    709: =item &ProcessStudentData()
                    710: 
                    711: Takes the course data downloaded for a student in 
1.4       stredwic  712: &DownloadCourseInformation() and breaks it up into key value pairs
1.1       stredwic  713: to be stored in the cached data.  The keys are comprised of the 
                    714: $username:$domain:$keyFromCourseDatabase.  The student username:domain is
1.23      stredwic  715: stored away signifying that the students information has been downloaded and 
1.1       stredwic  716: can be reused from cached data.
                    717: 
                    718: =over 4
                    719: 
                    720: Input: $cache, $courseData, $name
                    721: 
                    722: $cache: A hash pointer to store data
                    723: 
                    724: $courseData:  A hash pointer that points to the course data downloaded for a 
                    725: student.
                    726: 
                    727: $name:  username:domain
                    728: 
                    729: Output: None
                    730: 
                    731: *NOTE:  There is no output, but an error message is stored away in the cache 
                    732: data.  This is checked in &FormatStudentData().  The key username:domain:error 
                    733: will only exist if an error occured.  The error is an error from 
1.4       stredwic  734: &DownloadCourseInformation().
1.1       stredwic  735: 
                    736: =back
                    737: 
                    738: =cut
                    739: 
                    740: sub ProcessStudentData {
                    741:     my ($cache,$courseData,$name)=@_;
                    742: 
1.13      stredwic  743:     if(!&CheckDateStampError($courseData, $cache, $name)) {
                    744:         return;
                    745:     }
                    746: 
1.28      stredwic  747:     # This little delete thing, should not be here.  Move some other
                    748:     # time though.
1.26      stredwic  749:     if(defined($cache->{$name.':keys'})) {
                    750: 	foreach (split(':::', $cache->{$name.':keys'})) {
                    751: 	    delete $cache->{$name.':'.$_};
                    752: 	}
1.28      stredwic  753:         delete $cache->{$name.':keys'};
1.26      stredwic  754:     }
                    755: 
                    756:     my %courseKeys;
1.22      stredwic  757:     # user name:domain was prepended earlier in DownloadCourseInformation
1.13      stredwic  758:     foreach (keys %$courseData) {
1.29      albertel  759: 	my $currentKey = $_;
                    760: 	$currentKey =~ s/^$name//;
1.26      stredwic  761: 	$courseKeys{$currentKey}++;
1.13      stredwic  762:         $cache->{$_}=$courseData->{$_};
                    763:     }
                    764: 
1.26      stredwic  765:     $cache->{$name.':keys'} = join(':::', keys(%courseKeys));
                    766: 
1.13      stredwic  767:     return;
                    768: }
                    769: 
1.22      stredwic  770: =pod
                    771: 
                    772: =item &ExtractStudentData()
                    773: 
                    774: HISTORY: This function originally existed in every statistics module,
                    775: and performed different tasks, the had some overlap.  Due to the need
                    776: for the data from the different modules, they were combined into
                    777: a single function.
                    778: 
                    779: This function now extracts all the necessary course data for a student
                    780: from what was downloaded from their homeserver.  There is some extra
                    781: time overhead compared to the ProcessStudentInformation function, but
                    782: it would have had to occurred at some point anyways.  This is now
                    783: typically called while downloading the data it will process.  It is
                    784: the brother function to ProcessStudentInformation.
                    785: 
                    786: =over 4
                    787: 
                    788: Input: $input, $output, $data, $name
                    789: 
                    790: $input: A hash that contains the input data to be processed
                    791: 
                    792: $output: A hash to contain the processed data
                    793: 
                    794: $data: A hash containing the information on what is to be
                    795: processed and how (basically).
                    796: 
                    797: $name:  username:domain
                    798: 
                    799: The input is slightly different here, but is quite simple.
                    800: It is currently used where the $input, $output, and $data
                    801: can and are often the same hashes, but they do not need
                    802: to be.
                    803: 
                    804: Output: None
                    805: 
                    806: *NOTE:  There is no output, but an error message is stored away in the cache 
                    807: data.  This is checked in &FormatStudentData().  The key username:domain:error 
                    808: will only exist if an error occured.  The error is an error from 
                    809: &DownloadCourseInformation().
                    810: 
                    811: =back
                    812: 
                    813: =cut
                    814: 
1.13      stredwic  815: sub ExtractStudentData {
                    816:     my ($input, $output, $data, $name)=@_;
                    817: 
                    818:     if(!&CheckDateStampError($input, $data, $name)) {
1.3       stredwic  819:         return;
                    820:     }
                    821: 
1.28      stredwic  822:     # This little delete thing, should not be here.  Move some other
                    823:     # time though.
1.26      stredwic  824:     my %allkeys;
                    825:     if(defined($output->{$name.':keys'})) {
                    826: 	foreach (split(':::', $output->{$name.':keys'})) {
                    827: 	    delete $output->{$name.':'.$_};
                    828: 	}
1.28      stredwic  829:         delete $output->{$name.':keys'};
1.26      stredwic  830:     }
                    831: 
1.13      stredwic  832:     my ($username,$domain)=split(':',$name);
                    833: 
                    834:     my $Version;
                    835:     my $problemsCorrect = 0;
                    836:     my $totalProblems   = 0;
                    837:     my $problemsSolved  = 0;
                    838:     my $numberOfParts   = 0;
1.14      stredwic  839:     my $totalAwarded    = 0;
1.13      stredwic  840:     foreach my $sequence (split(':', $data->{'orderedSequences'})) {
                    841:         foreach my $problemID (split(':', $data->{$sequence.':problems'})) {
                    842:             my $problem = $data->{$problemID.':problem'};
                    843:             my $LatestVersion = $input->{$name.':version:'.$problem};
                    844: 
                    845:             # Output dashes for all the parts of this problem if there
                    846:             # is no version information about the current problem.
1.27      stredwic  847:             $output->{$name.':'.$problemID.':NoVersion'} = 'false';
                    848:             $allkeys{$name.':'.$problemID.':NoVersion'}++;
1.13      stredwic  849:             if(!$LatestVersion) {
                    850:                 foreach my $part (split(/\:/,$data->{$sequence.':'.
                    851:                                                       $problemID.
                    852:                                                       ':parts'})) {
1.15      stredwic  853:                     $output->{$name.':'.$problemID.':'.$part.':tries'} = 0;
                    854:                     $output->{$name.':'.$problemID.':'.$part.':awarded'} = 0;
                    855:                     $output->{$name.':'.$problemID.':'.$part.':code'} = ' ';
1.26      stredwic  856: 		    $allkeys{$name.':'.$problemID.':'.$part.':tries'}++;
                    857: 		    $allkeys{$name.':'.$problemID.':'.$part.':awarded'}++;
                    858: 		    $allkeys{$name.':'.$problemID.':'.$part.':code'}++;
1.13      stredwic  859:                     $totalProblems++;
                    860:                 }
                    861:                 $output->{$name.':'.$problemID.':NoVersion'} = 'true';
                    862:                 next;
                    863:             }
                    864: 
                    865:             my %partData=undef;
                    866:             # Initialize part data, display skips correctly
                    867:             # Skip refers to when a student made no submissions on that
                    868:             # part/problem.
                    869:             foreach my $part (split(/\:/,$data->{$sequence.':'.
                    870:                                                  $problemID.
                    871:                                                  ':parts'})) {
                    872:                 $partData{$part.':tries'}=0;
                    873:                 $partData{$part.':code'}=' ';
                    874:                 $partData{$part.':awarded'}=0;
                    875:                 $partData{$part.':timestamp'}=0;
                    876:                 foreach my $response (split(':', $data->{$sequence.':'.
                    877:                                                          $problemID.':'.
                    878:                                                          $part.':responseIDs'})) {
                    879:                     $partData{$part.':'.$response.':submission'}='';
                    880:                 }
                    881:             }
                    882: 
                    883:             # Looping through all the versions of each part, starting with the
                    884:             # oldest version.  Basically, it gets the most recent 
                    885:             # set of grade data for each part.
                    886:             my @submissions = ();
                    887: 	    for(my $Version=1; $Version<=$LatestVersion; $Version++) {
                    888:                 foreach my $part (split(/\:/,$data->{$sequence.':'.
                    889:                                                      $problemID.
                    890:                                                      ':parts'})) {
                    891: 
                    892:                     if(!defined($input->{"$name:$Version:$problem".
                    893:                                          ":resource.$part.solved"})) {
                    894:                         # No grade for this submission, so skip
                    895:                         next;
                    896:                     }
                    897: 
                    898:                     my $tries=0;
                    899:                     my $code=' ';
                    900:                     my $awarded=0;
                    901: 
                    902:                     $tries = $input->{$name.':'.$Version.':'.$problem.
                    903:                                       ':resource.'.$part.'.tries'};
                    904:                     $awarded = $input->{$name.':'.$Version.':'.$problem.
                    905:                                         ':resource.'.$part.'.awarded'};
                    906: 
                    907:                     $partData{$part.':awarded'}=($awarded) ? $awarded : 0;
                    908:                     $partData{$part.':tries'}=($tries) ? $tries : 0;
                    909: 
                    910:                     $partData{$part.':timestamp'}=$input->{$name.':'.$Version.':'.
                    911:                                                            $problem.
                    912:                                                            ':timestamp'};
                    913:                     if(!$input->{$name.':'.$Version.':'.$problem.':resource.'.$part.
                    914:                                  '.previous'}) {
                    915:                         foreach my $response (split(':',
                    916:                                                    $data->{$sequence.':'.
                    917:                                                            $problemID.':'.
                    918:                                                            $part.':responseIDs'})) {
                    919:                             @submissions=($input->{$name.':'.$Version.':'.
                    920:                                                    $problem.
                    921:                                                    ':resource.'.$part.'.'.
                    922:                                                    $response.'.submission'},
                    923:                                           @submissions);
                    924:                         }
                    925:                     }
                    926: 
                    927:                     my $val = $input->{$name.':'.$Version.':'.$problem.
                    928:                                        ':resource.'.$part.'.solved'};
                    929:                     if    ($val eq 'correct_by_student')   {$code = '*';} 
                    930:                     elsif ($val eq 'correct_by_override')  {$code = '+';}
                    931:                     elsif ($val eq 'incorrect_attempted')  {$code = '.';} 
                    932:                     elsif ($val eq 'incorrect_by_override'){$code = '-';}
                    933:                     elsif ($val eq 'excused')              {$code = 'x';}
                    934:                     elsif ($val eq 'ungraded_attempted')   {$code = '#';}
                    935:                     else                                   {$code = ' ';}
                    936:                     $partData{$part.':code'}=$code;
                    937:                 }
                    938:             }
                    939: 
                    940:             foreach my $part (split(/\:/,$data->{$sequence.':'.$problemID.
                    941:                                                  ':parts'})) {
                    942:                 $output->{$name.':'.$problemID.':'.$part.':wrong'} = 
                    943:                     $partData{$part.':tries'};
1.26      stredwic  944: 		$allkeys{$name.':'.$problemID.':'.$part.':wrong'}++;
1.13      stredwic  945: 
                    946:                 if($partData{$part.':code'} eq '*') {
                    947:                     $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
                    948:                     $problemsCorrect++;
                    949:                 } elsif($partData{$part.':code'} eq '+') {
                    950:                     $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
                    951:                     $problemsCorrect++;
                    952:                 }
                    953: 
                    954:                 $output->{$name.':'.$problemID.':'.$part.':tries'} = 
                    955:                     $partData{$part.':tries'};
                    956:                 $output->{$name.':'.$problemID.':'.$part.':code'} =
                    957:                     $partData{$part.':code'};
                    958:                 $output->{$name.':'.$problemID.':'.$part.':awarded'} =
                    959:                     $partData{$part.':awarded'};
1.26      stredwic  960: 		$allkeys{$name.':'.$problemID.':'.$part.':tries'}++;
                    961: 		$allkeys{$name.':'.$problemID.':'.$part.':code'}++;
                    962: 		$allkeys{$name.':'.$problemID.':'.$part.':awarded'}++;
                    963: 
1.14      stredwic  964:                 $totalAwarded += $partData{$part.':awarded'};
1.13      stredwic  965:                 $output->{$name.':'.$problemID.':'.$part.':timestamp'} =
                    966:                     $partData{$part.':timestamp'};
1.26      stredwic  967: 		$allkeys{$name.':'.$problemID.':'.$part.':timestamp'}++;
                    968: 
1.13      stredwic  969:                 foreach my $response (split(':', $data->{$sequence.':'.
                    970:                                                          $problemID.':'.
                    971:                                                          $part.':responseIDs'})) {
                    972:                     $output->{$name.':'.$problemID.':'.$part.':'.$response.
                    973:                               ':submission'}=join(':::',@submissions);
1.26      stredwic  974: 		    $allkeys{$name.':'.$problemID.':'.$part.':'.$response.
                    975: 			     ':submission'}++;
1.13      stredwic  976:                 }
1.3       stredwic  977: 
1.13      stredwic  978:                 if($partData{$part.':code'} ne 'x') {
                    979:                     $totalProblems++;
                    980:                 }
                    981:             }
1.1       stredwic  982:         }
1.13      stredwic  983: 
                    984:         $output->{$name.':'.$sequence.':problemsCorrect'} = $problemsCorrect;
1.26      stredwic  985: 	$allkeys{$name.':'.$sequence.':problemsCorrect'}++;
1.13      stredwic  986:         $problemsSolved += $problemsCorrect;
                    987: 	$problemsCorrect=0;
1.3       stredwic  988:     }
                    989: 
1.13      stredwic  990:     $output->{$name.':problemsSolved'} = $problemsSolved;
                    991:     $output->{$name.':totalProblems'} = $totalProblems;
1.14      stredwic  992:     $output->{$name.':totalAwarded'} = $totalAwarded;
1.26      stredwic  993:     $allkeys{$name.':problemsSolved'}++;
                    994:     $allkeys{$name.':totalProblems'}++;
                    995:     $allkeys{$name.':totalAwarded'}++;
                    996: 
                    997:     $output->{$name.':keys'} = join(':::', keys(%allkeys));
1.1       stredwic  998: 
                    999:     return;
1.4       stredwic 1000: }
                   1001: 
                   1002: sub LoadDiscussion {
1.13      stredwic 1003:     my ($courseID)=@_;
1.5       minaeibi 1004:     my %Discuss=();
                   1005:     my %contrib=&Apache::lonnet::dump(
                   1006:                 $courseID,
                   1007:                 $ENV{'course.'.$courseID.'.domain'},
                   1008:                 $ENV{'course.'.$courseID.'.num'});
                   1009: 				 
                   1010:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
                   1011: 
1.4       stredwic 1012:     foreach my $temp(keys %contrib) {
                   1013: 	if ($temp=~/^version/) {
                   1014: 	    my $ver=$contrib{$temp};
                   1015: 	    my ($dummy,$prb)=split(':',$temp);
                   1016: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
                   1017: 		my $name=$contrib{"$idx:$prb:sendername"};
1.5       minaeibi 1018: 		$Discuss{"$name:$prb"}=$idx;	
1.4       stredwic 1019: 	    }
                   1020: 	}
                   1021:     }       
1.5       minaeibi 1022: 
                   1023:     return \%Discuss;
1.1       stredwic 1024: }
                   1025: 
                   1026: # ----- END PROCESSING FUNCTIONS ---------------------------------------
                   1027: 
                   1028: =pod
                   1029: 
                   1030: =head1 HELPER FUNCTIONS
                   1031: 
                   1032: These are just a couple of functions do various odd and end 
1.22      stredwic 1033: jobs.  There was also a couple of bulk functions added.  These are
                   1034: &DownloadStudentCourseData(), &DownloadStudentCourseDataSeparate(), and
                   1035: &CheckForResidualDownload().  These functions now act as the interface
                   1036: for downloading student course data.  The statistical modules should
                   1037: no longer make the calls to dump and download and process etc.  They
                   1038: make calls to these bulk functions to get their data.
1.1       stredwic 1039: 
                   1040: =cut
                   1041: 
                   1042: # ----- HELPER FUNCTIONS -----------------------------------------------
                   1043: 
1.13      stredwic 1044: sub CheckDateStampError {
                   1045:     my ($courseData, $cache, $name)=@_;
                   1046:     if($courseData->{$name.':UpToDate'} eq 'true') {
                   1047:         $cache->{$name.':lastDownloadTime'} = 
                   1048:             $courseData->{$name.':lastDownloadTime'};
                   1049:         if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
                   1050:             $cache->{$name.':updateTime'} = ' Not updated';
                   1051:         } else {
                   1052:             $cache->{$name.':updateTime'}=
                   1053:                 localtime($courseData->{$name.':lastDownloadTime'});
                   1054:         }
                   1055:         return 0;
                   1056:     }
                   1057: 
                   1058:     $cache->{$name.':lastDownloadTime'}=$courseData->{$name.':lastDownloadTime'};
                   1059:     if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
                   1060:         $cache->{$name.':updateTime'} = ' Not updated';
                   1061:     } else {
                   1062:         $cache->{$name.':updateTime'}=
                   1063:             localtime($courseData->{$name.':lastDownloadTime'});
                   1064:     }
                   1065: 
                   1066:     if(defined($courseData->{$name.':error'})) {
                   1067:         $cache->{$name.':error'}=$courseData->{$name.':error'};
                   1068:         return 0;
                   1069:     }
                   1070: 
                   1071:     return 1;
                   1072: }
                   1073: 
1.1       stredwic 1074: =pod
                   1075: 
                   1076: =item &ProcessFullName()
                   1077: 
                   1078: Takes lastname, generation, firstname, and middlename (or some partial
                   1079: set of this data) and returns the full name version as a string.  Format
                   1080: is Lastname generation, firstname middlename or a subset of this.
                   1081: 
                   1082: =cut
                   1083: 
                   1084: sub ProcessFullName {
                   1085:     my ($lastname, $generation, $firstname, $middlename)=@_;
                   1086:     my $Str = '';
                   1087: 
1.34      matthew  1088:     # Strip whitespace preceeding & following name components.
                   1089:     $lastname   =~ s/(\s+$|^\s+)//g;
                   1090:     $generation =~ s/(\s+$|^\s+)//g;
                   1091:     $firstname  =~ s/(\s+$|^\s+)//g;
                   1092:     $middlename =~ s/(\s+$|^\s+)//g;
                   1093: 
1.1       stredwic 1094:     if($lastname ne '') {
1.34      matthew  1095: 	$Str .= $lastname;
                   1096: 	$Str .= ' '.$generation if ($generation ne '');
                   1097: 	$Str .= ',';
                   1098:         $Str .= ' '.$firstname  if ($firstname ne '');
                   1099:         $Str .= ' '.$middlename if ($middlename ne '');
1.1       stredwic 1100:     } else {
1.34      matthew  1101:         $Str .= $firstname      if ($firstname ne '');
                   1102:         $Str .= ' '.$middlename if ($middlename ne '');
                   1103:         $Str .= ' '.$generation if ($generation ne '');
1.1       stredwic 1104:     }
                   1105: 
                   1106:     return $Str;
                   1107: }
                   1108: 
                   1109: =pod
                   1110: 
                   1111: =item &TestCacheData()
                   1112: 
                   1113: Determine if the cache database can be accessed with a tie.  It waits up to
                   1114: ten seconds before returning failure.  This function exists to help with
                   1115: the problems with stopping the data download.  When an abort occurs and the
                   1116: user quickly presses a form button and httpd child is created.  This
                   1117: child needs to wait for the other to finish (hopefully within ten seconds).
                   1118: 
                   1119: =over 4
                   1120: 
                   1121: Input: $ChartDB
                   1122: 
                   1123: $ChartDB: The name of the cache database to be opened
                   1124: 
                   1125: Output: -1, 0, 1
                   1126: 
1.23      stredwic 1127: -1: Could not tie database
1.1       stredwic 1128:  0: Use cached data
                   1129:  1: New cache database created, use that.
                   1130: 
                   1131: =back
                   1132: 
                   1133: =cut
                   1134: 
                   1135: sub TestCacheData {
                   1136:     my ($ChartDB,$isRecalculate,$totalDelay)=@_;
                   1137:     my $isCached=-1;
                   1138:     my %testData;
                   1139:     my $tieTries=0;
                   1140: 
                   1141:     if(!defined($totalDelay)) {
                   1142:         $totalDelay = 10;
                   1143:     }
                   1144: 
                   1145:     if ((-e "$ChartDB") && (!$isRecalculate)) {
                   1146: 	$isCached = 1;
                   1147:     } else {
                   1148: 	$isCached = 0;
                   1149:     }
                   1150: 
                   1151:     while($tieTries < $totalDelay) {
                   1152:         my $result=0;
                   1153:         if($isCached) {
1.10      stredwic 1154:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
1.1       stredwic 1155:         } else {
1.10      stredwic 1156:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
1.1       stredwic 1157:         }
                   1158:         if($result) {
                   1159:             last;
                   1160:         }
                   1161:         $tieTries++;
                   1162:         sleep 1;
                   1163:     }
                   1164:     if($tieTries >= $totalDelay) {
                   1165:         return -1;
                   1166:     }
                   1167: 
                   1168:     untie(%testData);
                   1169: 
                   1170:     return $isCached;
                   1171: }
1.2       stredwic 1172: 
1.13      stredwic 1173: sub DownloadStudentCourseData {
                   1174:     my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
                   1175: 
                   1176:     my $title = 'LON-CAPA Statistics';
                   1177:     my $heading = 'Download and Process Course Data';
                   1178:     my $studentCount = scalar(@$students);
1.18      stredwic 1179: 
1.13      stredwic 1180:     my $WhatIWant;
1.20      stredwic 1181:     $WhatIWant = '(^version:|';
1.18      stredwic 1182:     $WhatIWant .= '^\d+:.+?:(resource\.\d+\.';
1.13      stredwic 1183:     $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
                   1184:     $WhatIWant .= '|timestamp)';
                   1185:     $WhatIWant .= ')';
1.20      stredwic 1186: #    $WhatIWant = '.';
1.13      stredwic 1187: 
                   1188:     if($status eq 'true') {
                   1189:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
                   1190:     }
1.17      stredwic 1191: 
                   1192:     my $displayString;
                   1193:     my $count=0;
1.13      stredwic 1194:     foreach (@$students) {
1.24      stredwic 1195:         my %cache;
                   1196: 
1.13      stredwic 1197:         if($c->aborted()) { return 'Aborted'; }
                   1198: 
                   1199:         if($status eq 'true') {
1.17      stredwic 1200:             $count++;
1.13      stredwic 1201:             my $displayString = $count.'/'.$studentCount.': '.$_;
                   1202:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
                   1203:         }
                   1204: 
                   1205:         my $downloadTime='Not downloaded';
1.28      stredwic 1206:         my $needUpdate = 'false';
1.13      stredwic 1207:         if($checkDate eq 'true'  && 
                   1208:            tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
                   1209:             $downloadTime = $cache{$_.':lastDownloadTime'};
1.28      stredwic 1210:             $needUpdate = $cache{'ResourceUpdated'};
1.13      stredwic 1211:             untie(%cache);
                   1212:         }
                   1213: 
                   1214:         if($c->aborted()) { return 'Aborted'; }
                   1215: 
1.28      stredwic 1216:         if($needUpdate eq 'true') {
                   1217:             $downloadTime = 'Not downloaded';
                   1218: 	}
1.24      stredwic 1219: 	my $courseData = 
                   1220: 	    &DownloadCourseInformation($_, $courseID, $downloadTime, 
                   1221: 				       $WhatIWant);
                   1222: 	if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
                   1223: 	    foreach my $key (keys(%$courseData)) {
                   1224: 		if($key =~ /^(con_lost|error|no_such_host)/i) {
                   1225: 		    $courseData->{$_.':error'} = 'No course data for '.$_;
                   1226: 		    last;
                   1227: 		}
                   1228: 	    }
                   1229: 	    if($extract eq 'true') {
                   1230: 		&ExtractStudentData($courseData, \%cache, \%cache, $_);
                   1231: 	    } else {
                   1232: 		&ProcessStudentData(\%cache, $courseData, $_);
                   1233: 	    }
                   1234: 	    untie(%cache);
                   1235: 	} else {
                   1236: 	    next;
                   1237: 	}
1.13      stredwic 1238:     }
                   1239:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
                   1240: 
                   1241:     return 'OK';
                   1242: }
                   1243: 
                   1244: sub DownloadStudentCourseDataSeparate {
                   1245:     my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
                   1246:     my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
                   1247:     my $title = 'LON-CAPA Statistics';
                   1248:     my $heading = 'Download Course Data';
                   1249: 
                   1250:     my $WhatIWant;
1.20      stredwic 1251:     $WhatIWant = '(^version:|';
1.18      stredwic 1252:     $WhatIWant .= '^\d+:.+?:(resource\.\d+\.';
1.13      stredwic 1253:     $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
                   1254:     $WhatIWant .= '|timestamp)';
                   1255:     $WhatIWant .= ')';
                   1256: 
1.30      stredwic 1257:     &CheckForResidualDownload($cacheDB, 'true', 'true', $courseID, $r, $c);
1.13      stredwic 1258: 
                   1259:     my $studentCount = scalar(@$students);
                   1260:     if($status eq 'true') {
                   1261:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
                   1262:     }
1.17      stredwic 1263:     my $count=0;
                   1264:     my $displayString='';
1.13      stredwic 1265:     foreach (@$students) {
                   1266:         if($c->aborted()) {
                   1267:             return 'Aborted';
                   1268:         }
                   1269: 
                   1270:         if($status eq 'true') {
1.17      stredwic 1271:             $count++;
                   1272:             $displayString = $count.'/'.$studentCount.': '.$_;
1.13      stredwic 1273:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
                   1274:         }
                   1275: 
1.24      stredwic 1276:         my %cache;
1.13      stredwic 1277:         my $downloadTime='Not downloaded';
1.28      stredwic 1278:         my $needUpdate = 'false';
1.13      stredwic 1279:         if($checkDate eq 'true'  && 
                   1280:            tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
                   1281:             $downloadTime = $cache{$_.':lastDownloadTime'};
1.28      stredwic 1282:             $needUpdate = $cache{'ResourceUpdated'};
1.13      stredwic 1283:             untie(%cache);
                   1284:         }
                   1285: 
                   1286:         if($c->aborted()) {
                   1287:             return 'Aborted';
                   1288:         }
                   1289: 
1.28      stredwic 1290:         if($needUpdate eq 'true') {
                   1291:             $downloadTime = 'Not downloaded';
                   1292: 	}
                   1293: 
                   1294:         my $error = 0;
                   1295:         my $courseData = 
                   1296:             &DownloadCourseInformation($_, $courseID, $downloadTime,
                   1297:                                        $WhatIWant);
                   1298:         my %downloadData;
                   1299:         unless(tie(%downloadData,'GDBM_File',$residualFile,
                   1300:                    &GDBM_WRCREAT(),0640)) {
                   1301:             return 'Failed to tie temporary download hash.';
                   1302:         }
                   1303:         foreach my $key (keys(%$courseData)) {
                   1304:             $downloadData{$key} = $courseData->{$key};
                   1305:             if($key =~ /^(con_lost|error|no_such_host)/i) {
                   1306:                 $error = 1;
                   1307:                 last;
1.18      stredwic 1308:             }
1.28      stredwic 1309:         }
                   1310:         if($error) {
                   1311:             foreach my $deleteKey (keys(%$courseData)) {
                   1312:                 delete $downloadData{$deleteKey};
1.13      stredwic 1313:             }
1.28      stredwic 1314:             $downloadData{$_.':error'} = 'No course data for '.$_;
                   1315:         }
                   1316:         untie(%downloadData);
1.13      stredwic 1317:     }
                   1318:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
                   1319: 
                   1320:     return &CheckForResidualDownload($cacheDB, 'true', 'true', 
                   1321:                                      $courseID, $r, $c);
                   1322: }
                   1323: 
                   1324: sub CheckForResidualDownload {
                   1325:     my ($cacheDB,$extract,$status,$courseID,$r,$c)=@_;
                   1326: 
                   1327:     my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
                   1328:     if(!-e $residualFile) {
1.18      stredwic 1329:         return 'OK';
1.13      stredwic 1330:     }
                   1331: 
                   1332:     my %downloadData;
                   1333:     my %cache;
1.17      stredwic 1334:     unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_READER(),0640)) {
                   1335:         return 'Can not tie database for check for residual download: tempDB';
                   1336:     }
                   1337:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
                   1338:         untie(%downloadData);
                   1339:         return 'Can not tie database for check for residual download: cacheDB';
1.13      stredwic 1340:     }
                   1341: 
                   1342:     my @students=();
                   1343:     my %checkStudent;
1.18      stredwic 1344:     my $key;
                   1345:     while(($key, undef) = each %downloadData) {
                   1346:         my @temp = split(':', $key);
1.13      stredwic 1347:         my $student = $temp[0].':'.$temp[1];
                   1348:         if(!defined($checkStudent{$student})) {
                   1349:             $checkStudent{$student}++;
                   1350:             push(@students, $student);
                   1351:         }
                   1352:     }
                   1353: 
                   1354:     my $heading = 'Process Course Data';
                   1355:     my $title = 'LON-CAPA Statistics';
                   1356:     my $studentCount = scalar(@students);
                   1357:     if($status eq 'true') {
                   1358:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
                   1359:     }
                   1360: 
1.20      stredwic 1361:     my $count=1;
1.13      stredwic 1362:     foreach my $name (@students) {
                   1363:         last if($c->aborted());
                   1364: 
                   1365:         if($status eq 'true') {
1.19      stredwic 1366:             my $displayString = $count.'/'.$studentCount.': '.$name;
1.13      stredwic 1367:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
                   1368:         }
                   1369: 
                   1370:         if($extract eq 'true') {
                   1371:             &ExtractStudentData(\%downloadData, \%cache, \%cache, $name);
                   1372:         } else {
                   1373:             &ProcessStudentData(\%cache, \%downloadData, $name);
                   1374:         }
                   1375:         $count++;
                   1376:     }
                   1377: 
                   1378:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
                   1379: 
                   1380:     untie(%cache);
                   1381:     untie(%downloadData);
                   1382: 
                   1383:     if(!$c->aborted()) {
                   1384:         my @files = ($residualFile);
                   1385:         unlink(@files);
                   1386:     }
                   1387: 
                   1388:     return 'OK';
1.3       stredwic 1389: }
1.1       stredwic 1390: 
1.35      matthew  1391: ################################################
                   1392: ################################################
                   1393: 
                   1394: =pod
                   1395: 
                   1396: =item &get_classlist();
                   1397: 
                   1398: Retrieve the classist of a given class or of the current class.  Student
                   1399: information is returned from the classlist.db file and, if needed,
                   1400: from the students environment.
                   1401: 
                   1402: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
                   1403: and course number, respectively).  Any omitted arguments will be taken 
                   1404: from the current environment ($ENV{'request.course.id'},
                   1405: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
                   1406: 
                   1407: Returns a reference to a hash which contains:
                   1408:  keys    '$sname:$sdom'
                   1409:  values  [$end,$start,$id,$section,$fullname]
                   1410: 
                   1411: =cut
                   1412: 
                   1413: ################################################
                   1414: ################################################
                   1415: 
                   1416: sub get_classlist {
                   1417:     my ($cid,$cdom,$cnum) = @_;
                   1418:     $cid = $cid || $ENV{'request.course.id'};
                   1419:     $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
                   1420:     $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
                   1421:    my $now = time;
                   1422:     #
                   1423:     my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
                   1424:     while (my ($student,$info) = each(%classlist)) {
                   1425:         return undef if ($student =~ /^(con_lost|error|no_such_host)/i);
                   1426:         my ($sname,$sdom) = split(/:/,$student);
                   1427:         my @Values = split(/:/,$info);
                   1428:         my ($end,$start,$id,$section,$fullname);
                   1429:         if (@Values > 2) {
                   1430:             ($end,$start,$id,$section,$fullname) = @Values;
                   1431:         } else { # We have to get the data ourselves
                   1432:             ($end,$start) = @Values;
1.37      matthew  1433:             $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35      matthew  1434:             my %info=&Apache::lonnet::get('environment',
                   1435:                                           ['firstname','middlename',
                   1436:                                            'lastname','generation','id'],
                   1437:                                           $sdom, $sname);
                   1438:             my ($tmp) = keys(%info);
                   1439:             if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   1440:                 $fullname = 'not available';
                   1441:                 $id = 'not available';
1.38      matthew  1442:                 &Apache::lonnet::logthis('unable to retrieve environment '.
                   1443:                                          'for '.$sname.':'.$sdom);
1.35      matthew  1444:             } else {
                   1445:                 $fullname = &ProcessFullName(@info{qw/lastname generation 
                   1446:                                                        firstname middlename/});
                   1447:                 $id = $info{'id'};
                   1448:             }
1.36      matthew  1449:             # Update the classlist with this students information
                   1450:             if ($fullname ne 'not available') {
                   1451:                 my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
                   1452:                 my $reply=&Apache::lonnet::cput('classlist',
                   1453:                                                 {$student => $enrolldata},
                   1454:                                                 $cdom,$cnum);
                   1455:                 if ($reply !~ /^(ok|delayed)/) {
                   1456:                     &Apache::lonnet::logthis('Unable to update classlist for '.
                   1457:                                              'student '.$sname.':'.$sdom.
                   1458:                                              ' error:'.$reply);
                   1459:                 }
                   1460:             }
1.35      matthew  1461:         }
                   1462:         my $status='Expired';
                   1463:         if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
                   1464:             $status='Active';
                   1465:         }
                   1466:         $classlist{$student} = 
                   1467:             [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
                   1468:     }
                   1469:     if (wantarray()) {
                   1470:         return (\%classlist,['domain','username','end','start','id',
                   1471:                              'section','fullname','status']);
                   1472:     } else {
                   1473:         return \%classlist;
                   1474:     }
                   1475: }
                   1476: 
1.1       stredwic 1477: # ----- END HELPER FUNCTIONS --------------------------------------------
                   1478: 
                   1479: 1;
                   1480: __END__
1.36      matthew  1481: 
1.35      matthew  1482: 

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