File:  [LON-CAPA] / loncom / interface / loncoursedata.pm
Revision 1.23: download - view: text, annotated - select for diffs
Wed Aug 28 22:02:47 2002 UTC (21 years, 9 months ago) by stredwic
Branches: MAIN
CVS tags: HEAD
Fixed some single quote problems in my documentation

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: loncoursedata.pm,v 1.23 2002/08/28 22:02:47 stredwic Exp $
    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: 
   38: Set of functions that download and process student and course information.
   39: 
   40: =head1 PACKAGES USED
   41: 
   42:  Apache::Constants qw(:common :http)
   43:  Apache::lonnet()
   44:  Apache::lonhtmlcommon
   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();
   55: use Apache::lonhtmlcommon;
   56: use HTML::TokeParser;
   57: use GDBM_File;
   58: 
   59: =pod
   60: 
   61: =head1 DOWNLOAD INFORMATION
   62: 
   63: This section contains all the functions that get data from other servers 
   64: and/or itself.
   65: 
   66: =cut
   67: 
   68: # ----- DOWNLOAD INFORMATION -------------------------------------------
   69: 
   70: =pod
   71: 
   72: =item &DownloadClasslist()
   73: 
   74: Collects lastname, generation, middlename, firstname, PID, and section for each
   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.
   84: 
   85: =over 4
   86: 
   87: Input: $courseID, $lastDownloadTime, $c
   88: 
   89: $courseID:  The id of the course
   90: 
   91: $lastDownloadTime:  This is the date stamp for when this information was
   92: last gathered.  If it is set to Not downloaded, it will gather the data
   93: again, though it currently does not remove the old data.
   94: 
   95: $c: The connection class that can determine if the browser has aborted.  It
   96: is used to short circuit this function so that it does not continue to 
   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,
  106: middlename, and PID : Key is $name.studentInformation
  107: 
  108: -A hash pointer to each students section data : Key is $name.section
  109: 
  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: 
  114: =back
  115: 
  116: =cut
  117: 
  118: sub DownloadClasslist {
  119:     my ($courseID, $lastDownloadTime, $c)=@_;
  120:     my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
  121:     my %classlist;
  122: 
  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
  128:     # Not downloaded, otherwise it is only downloaded if the file
  129:     # has been updated and has a more recent date stamp
  130:     if($lastDownloadTime ne 'Not downloaded' &&
  131:        $lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
  132:         # Data is not gathered so return UpToDate as true.  This
  133:         # will be interpreted in ProcessClasslist
  134:         $classlist{'lastDownloadTime'}=time;
  135:         $classlist{'UpToDate'} = 'true';
  136:         return \%classlist;
  137:     }
  138: 
  139:     %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
  140:     foreach(keys (%classlist)) {
  141:         if(/^(con_lost|error|no_such_host)/i) {
  142:             return \%classlist;
  143:         }
  144:     }
  145: 
  146:     foreach my $name (keys(%classlist)) {
  147:         if(defined($c) && ($c->aborted())) {
  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);
  169:         $classlist{$name.':sections'}=\%section;
  170:     }
  171: 
  172:     $classlist{'UpToDate'} = 'false';
  173:     $classlist{'lastDownloadTime'}=time;
  174: 
  175:     return \%classlist;
  176: }
  177: 
  178: =pod
  179: 
  180: =item &DownloadCourseInformation()
  181: 
  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
  186: checks the timestamp of the students course database file and only downloads
  187: if it has been modified since the last download.
  188: 
  189: =over 4
  190: 
  191: Input: $namedata, $courseID, $lastDownloadTime, $WhatIWant
  192: 
  193: $namedata: student name:domain
  194: 
  195: $courseID:  The id of the course
  196: 
  197: $lastDownloadTime:  This is the date stamp for when this information was
  198: last gathered.  If it is set to Not downloaded, it will gather the data
  199: again, though it currently does not remove the old data.
  200: 
  201: $WhatIWant:  Regular expression used to get selected data with dump
  202: 
  203: Output: \%courseData
  204: 
  205: \%courseData:  A hash pointer to the raw data from the students course
  206: database.
  207: 
  208: =back
  209: 
  210: =cut
  211: 
  212: sub DownloadCourseInformation {
  213:     my ($namedata,$courseID,$lastDownloadTime,$WhatIWant)=@_;
  214:     my %courseData;
  215:     my ($name,$domain) = split(/\:/,$namedata);
  216: 
  217:     my $modifiedTime = &Apache::lonnet::GetFileTimestamp($domain, $name,
  218:                                       $courseID.'.db', 
  219:                                       $Apache::lonnet::perlvar{'lonUsersDir'});
  220: 
  221:     if($lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
  222:         # Data is not gathered so return UpToDate as true.  This
  223:         # will be interpreted in ProcessClasslist
  224:         $courseData{$namedata.':lastDownloadTime'}=time;
  225:         $courseData{$namedata.':UpToDate'} = 'true';
  226:         return \%courseData;
  227:     }
  228: 
  229:     # Download course data
  230:     if(!defined($WhatIWant)) {
  231:         # set the regular expression to everything by setting it to period
  232:         $WhatIWant = '.';
  233:     }
  234:     %courseData=&Apache::lonnet::dump($courseID, $domain, $name, $WhatIWant);
  235:     $courseData{'UpToDate'} = 'false';
  236:     $courseData{'lastDownloadTime'}=time;
  237: 
  238:     my %newData;
  239:     foreach (keys(%courseData)) {
  240:         # need to have the keys to be prepended with the name:domain of the
  241:         # student to reduce data collision later.
  242:         $newData{$namedata.':'.$_} = $courseData{$_};
  243:     }
  244: 
  245:     return \%newData;
  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
  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 
  257: with stopping downloading then can not tie hash to database later.
  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 {
  290:     my ($cache,$c)=@_;
  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:             }
  299: 	    if(tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
  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: 
  312:     # Initialize state machine.  Set information pointing to top level map.
  313:     my (@sequences, @currentResource, @finishResource);
  314:     my ($currentSequence, $currentResourceID, $lastResourceID);
  315: 
  316:     $currentResourceID=$hash{'ids_/res/'.$ENV{'request.course.uri'}};
  317:     push(@currentResource, $currentResourceID);
  318:     $lastResourceID=-1;
  319:     $currentSequence=-1;
  320:     my $topLevelSequenceNumber = $currentSequence;
  321: 
  322:     my %sequenceRecord;
  323:     while(1) {
  324:         if($c->aborted()) {
  325:             last;
  326:         }
  327: 	# HANDLE NEW SEQUENCE!
  328: 	#if page || sequence
  329: 	if(defined($hash{'map_pc_'.$hash{'src_'.$currentResourceID}}) &&
  330:            !defined($sequenceRecord{$currentResourceID})) {
  331:             $sequenceRecord{$currentResourceID}++;
  332: 	    push(@sequences, $currentSequence);
  333: 	    push(@currentResource, $currentResourceID);
  334: 	    push(@finishResource, $lastResourceID);
  335: 
  336: 	    $currentSequence=$hash{'map_pc_'.$hash{'src_'.$currentResourceID}};
  337: 
  338:             # Mark sequence as containing problems.  If it doesn't, then
  339:             # it will be removed when processing for this sequence is
  340:             # complete.  This allows the problems in a sequence
  341:             # to be outputed before problems in the subsequences
  342:             if(!defined($cache->{'orderedSequences'})) {
  343:                 $cache->{'orderedSequences'}=$currentSequence;
  344:             } else {
  345:                 $cache->{'orderedSequences'}.=':'.$currentSequence;
  346:             }
  347: 
  348: 	    $lastResourceID=$hash{'map_finish_'.
  349: 				  $hash{'src_'.$currentResourceID}};
  350: 	    $currentResourceID=$hash{'map_start_'.
  351: 				     $hash{'src_'.$currentResourceID}};
  352: 
  353: 	    if(!($currentResourceID) || !($lastResourceID)) {
  354: 		$currentSequence=pop(@sequences);
  355: 		$currentResourceID=pop(@currentResource);
  356: 		$lastResourceID=pop(@finishResource);
  357: 		if($currentSequence eq $topLevelSequenceNumber) {
  358: 		    last;
  359: 		}
  360: 	    }
  361:             next;
  362: 	}
  363: 
  364: 	# Handle gradable resources: exams, problems, etc
  365: 	$currentResourceID=~/(\d+)\.(\d+)/;
  366:         my $partA=$1;
  367:         my $partB=$2;
  368: 	if($hash{'src_'.$currentResourceID}=~
  369: 	   /\.(problem|exam|quiz|assess|survey|form)$/ &&
  370: 	   $partA eq $currentSequence && 
  371:            !defined($sequenceRecord{$currentSequence.':'.
  372:                                     $currentResourceID})) {
  373:             $sequenceRecord{$currentSequence.':'.$currentResourceID}++;
  374: 	    my $Problem = &Apache::lonnet::symbclean(
  375: 			  &Apache::lonnet::declutter($hash{'map_id_'.$partA}).
  376: 			  '___'.$partB.'___'.
  377: 			  &Apache::lonnet::declutter($hash{'src_'.
  378: 							 $currentResourceID}));
  379: 
  380: 	    $cache->{$currentResourceID.':problem'}=$Problem;
  381: 	    if(!defined($cache->{$currentSequence.':problems'})) {
  382: 		$cache->{$currentSequence.':problems'}=$currentResourceID;
  383: 	    } else {
  384: 		$cache->{$currentSequence.':problems'}.=
  385: 		    ':'.$currentResourceID;
  386: 	    }
  387: 
  388: 	    my $meta=$hash{'src_'.$currentResourceID};
  389: #            $cache->{$currentResourceID.':title'}=
  390: #                &Apache::lonnet::metdata($meta,'title');
  391:             $cache->{$currentResourceID.':title'}=
  392:                 $hash{'title_'.$currentResourceID};
  393:             $cache->{$currentResourceID.':source'}=
  394:                 $hash{'src_'.$currentResourceID};
  395: 
  396:             # Get Parts for problem
  397:             my %beenHere;
  398:             foreach (split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
  399:                 if(/^\w+response_\d+.*/) {
  400:                     my (undef, $partId, $responseId) = split(/_/,$_);
  401:                     if($beenHere{'p:'.$partId} ==  0) {
  402:                         $beenHere{'p:'.$partId}++;
  403:                         if(!defined($cache->{$currentSequence.':'.
  404:                                             $currentResourceID.':parts'})) {
  405:                             $cache->{$currentSequence.':'.$currentResourceID.
  406:                                      ':parts'}=$partId;
  407:                         } else {
  408:                             $cache->{$currentSequence.':'.$currentResourceID.
  409:                                      ':parts'}.=':'.$partId;
  410:                         }
  411:                     }
  412:                     if($beenHere{'r:'.$partId.':'.$responseId} == 0) {
  413:                         $beenHere{'r:'.$partId.':'.$responseId}++;
  414:                         if(!defined($cache->{$currentSequence.':'.
  415:                                              $currentResourceID.':'.$partId.
  416:                                              ':responseIDs'})) {
  417:                             $cache->{$currentSequence.':'.$currentResourceID.
  418:                                      ':'.$partId.':responseIDs'}=$responseId;
  419:                         } else {
  420:                             $cache->{$currentSequence.':'.$currentResourceID.
  421:                                      ':'.$partId.':responseIDs'}.=':'.
  422:                                                                   $responseId;
  423:                         }
  424:                     }
  425:                     if(/^optionresponse/ && 
  426:                        $beenHere{'o:'.$partId.':'.$currentResourceID} == 0) {
  427:                         $beenHere{'o:'.$partId.$currentResourceID}++;
  428:                         if(defined($cache->{'OptionResponses'})) {
  429:                             $cache->{'OptionResponses'}.= ':::'.
  430:                                 $currentSequence.':'.$currentResourceID.':'.
  431:                                 $partId.':'.$responseId;
  432:                         } else {
  433:                             $cache->{'OptionResponses'}= $currentSequence.':'.
  434:                                 $currentResourceID.':'.
  435:                                 $partId.':'.$responseId;
  436:                         }
  437:                     }
  438:                 }
  439:             }
  440:         }
  441: 
  442: 	# if resource == finish resource, then it is the end of a sequence/page
  443: 	if($currentResourceID eq $lastResourceID) {
  444: 	    # pop off last resource of sequence
  445: 	    $currentResourceID=pop(@currentResource);
  446: 	    $lastResourceID=pop(@finishResource);
  447: 
  448: 	    if(defined($cache->{$currentSequence.':problems'})) {
  449: 		# Capture sequence information here
  450: 		$cache->{$currentSequence.':title'}=
  451: 		    $hash{'title_'.$currentResourceID};
  452:                 $cache->{$currentSequence.':source'}=
  453:                     $hash{'src_'.$currentResourceID};
  454: 
  455:                 my $totalProblems=0;
  456:                 foreach my $currentProblem (split(/\:/,
  457:                                                $cache->{$currentSequence.
  458:                                                ':problems'})) {
  459:                     foreach (split(/\:/,$cache->{$currentSequence.':'.
  460:                                                    $currentProblem.
  461:                                                    ':parts'})) {
  462:                         $totalProblems++;
  463:                     }
  464:                 }
  465: 		my @titleLength=split(//,$cache->{$currentSequence.
  466:                                                     ':title'});
  467:                 # $extra is 3 for problems correct and 3 for space
  468:                 # between problems correct and problem output
  469:                 my $extra = 6;
  470: 		if(($totalProblems + $extra) > (scalar @titleLength)) {
  471: 		    $cache->{$currentSequence.':columnWidth'}=
  472:                         $totalProblems + $extra;
  473: 		} else {
  474: 		    $cache->{$currentSequence.':columnWidth'}=
  475:                         (scalar @titleLength);
  476: 		}
  477: 	    } else {
  478:                 # Remove sequence from list, if it contains no problems to
  479:                 # display.
  480:                 $cache->{'orderedSequences'}=~s/$currentSequence//;
  481:                 $cache->{'orderedSequences'}=~s/::/:/g;
  482:                 $cache->{'orderedSequences'}=~s/^:|:$//g;
  483:             }
  484: 
  485: 	    $currentSequence=pop(@sequences);
  486: 	    if($currentSequence eq $topLevelSequenceNumber) {
  487: 		last;
  488: 	    }
  489:         }
  490: 
  491: 	# MOVE!!!
  492: 	# move to next resource
  493: 	unless(defined($hash{'to_'.$currentResourceID})) {
  494: 	    # big problem, need to handle.  Next is probably wrong
  495:             my $errorMessage = 'Big problem in ';
  496:             $errorMessage .= 'loncoursedata::ProcessTopLevelMap.';
  497:             $errorMessage .= '  bighash to_$currentResourceID not defined!';
  498:             &Apache::lonnet::logthis($errorMessage);
  499: 	    last;
  500: 	}
  501: 	my @nextResources=();
  502: 	foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
  503:             if(!defined($sequenceRecord{$currentSequence.':'.
  504:                                         $hash{'goesto_'.$_}})) {
  505:                 push(@nextResources, $hash{'goesto_'.$_});
  506:             }
  507: 	}
  508: 	push(@currentResource, @nextResources);
  509: 	# Set the next resource to be processed
  510: 	$currentResourceID=pop(@currentResource);
  511:     }
  512: 
  513:     unless (untie(%hash)) {
  514:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  515:                                  "Could not untie coursemap $fn (browse)".
  516:                                  ".</font>"); 
  517:     }
  518: 
  519:     return 'OK';
  520: }
  521: 
  522: =pod
  523: 
  524: =item &ProcessClasslist()
  525: 
  526: Taking the class list dumped from &DownloadClasslist(), all the 
  527: students and their non-class information is processed using the 
  528: &ProcessStudentInformation() function.  A date stamp is also recorded for
  529: when the data was processed.
  530: 
  531: Takes data downloaded for a student and breaks it up into managable pieces and 
  532: stored in cache data.  The username, domain, class related date, PID, 
  533: full name, and section are all processed here.
  534: 
  535: =over 4
  536: 
  537: Input: $cache, $classlist, $courseID, $ChartDB, $c
  538: 
  539: $cache: A hash pointer to store the data
  540: 
  541: $classlist:  The hash of data collected about a student from 
  542: &DownloadClasslist().  The hash contains a list of students, a pointer 
  543: to a hash of student information for each student, and each students section 
  544: number.
  545: 
  546: $courseID:  The course ID
  547: 
  548: $ChartDB:  The name of the cache database file.
  549: 
  550: $c:  The connection class used to determine if an abort has been sent to the 
  551: browser
  552: 
  553: Output: @names
  554: 
  555: @names:  An array of students whose information has been processed, and are to 
  556: be considered in an arbitrary order.
  557: 
  558: =back
  559: 
  560: =cut
  561: 
  562: sub ProcessClasslist {
  563:     my ($cache,$classlist,$courseID,$c)=@_;
  564:     my @names=();
  565: 
  566:     $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
  567:     if($classlist->{'UpToDate'} eq 'true') {
  568:         return split(/:::/,$cache->{'NamesOfStudents'});;
  569:     }
  570: 
  571:     foreach my $name (keys(%$classlist)) {
  572:         if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
  573:            $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
  574:             next;
  575:         }
  576:         if($c->aborted()) {
  577:             return ();
  578:         }
  579:         my $studentInformation = $classlist->{$name.':studentInformation'},
  580:         my $sectionData = $classlist->{$name.':sections'},
  581:         my $date = $classlist->{$name},
  582:         my ($studentName,$studentDomain) = split(/\:/,$name);
  583: 
  584:         $cache->{$name.':username'}=$studentName;
  585:         $cache->{$name.':domain'}=$studentDomain;
  586:         # Initialize timestamp for student
  587:         if(!defined($cache->{$name.':lastDownloadTime'})) {
  588:             $cache->{$name.':lastDownloadTime'}='Not downloaded';
  589:             $cache->{$name.':updateTime'}=' Not updated';
  590:         }
  591: 
  592:         my $error = 0;
  593:         foreach(keys(%$studentInformation)) {
  594:             if(/^(con_lost|error|no_such_host)/i) {
  595:                 $cache->{$name.':error'}=
  596:                     'Could not download student environment data.';
  597:                 $cache->{$name.':fullname'}='';
  598:                 $cache->{$name.':id'}='';
  599:                 $error = 1;
  600:             }
  601:         }
  602:         next if($error);
  603:         push(@names,$name);
  604:         $cache->{$name.':fullname'}=&ProcessFullName(
  605:                                           $studentInformation->{'lastname'},
  606:                                           $studentInformation->{'generation'},
  607:                                           $studentInformation->{'firstname'},
  608:                                           $studentInformation->{'middlename'});
  609:         $cache->{$name.':id'}=$studentInformation->{'id'};
  610: 
  611:         my ($end, $start)=split(':',$date);
  612:         $courseID=~s/\_/\//g;
  613:         $courseID=~s/^(\w)/\/$1/;
  614: 
  615:         my $sec='';
  616:         foreach my $key (keys (%$sectionData)) {
  617:             my $value = $sectionData->{$key};
  618:             if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
  619:                 my $tempsection=$1;
  620:                 if($key eq $courseID.'_st') {
  621:                     $tempsection='';
  622:                 }
  623:                 my ($dummy,$roleend,$rolestart)=split(/\_/,$value);
  624:                 if($roleend eq $end && $rolestart eq $start) {
  625:                     $sec = $tempsection;
  626:                     last;
  627:                 }
  628:             }
  629:         }
  630: 
  631:         my $status='Expired';
  632:         if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
  633:             $status='Active';
  634:         }
  635:         $cache->{$name.':Status'}=$status;
  636:         $cache->{$name.':section'}=$sec;
  637: 
  638:         if($sec eq '' || !defined($sec) || $sec eq ' ') {
  639:             $sec = 'none';
  640:         }
  641:         if(defined($cache->{'sectionList'})) {
  642:             if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
  643:                 $cache->{'sectionList'} .= ':'.$sec;
  644:             }
  645:         } else {
  646:             $cache->{'sectionList'} = $sec;
  647:         }
  648:     }
  649: 
  650:     $cache->{'ClasslistTimestamp'}=time;
  651:     $cache->{'NamesOfStudents'}=join(':::',@names);
  652: 
  653:     return @names;
  654: }
  655: 
  656: =pod
  657: 
  658: =item &ProcessStudentData()
  659: 
  660: Takes the course data downloaded for a student in 
  661: &DownloadCourseInformation() and breaks it up into key value pairs
  662: to be stored in the cached data.  The keys are comprised of the 
  663: $username:$domain:$keyFromCourseDatabase.  The student username:domain is
  664: stored away signifying that the students information has been downloaded and 
  665: can be reused from cached data.
  666: 
  667: =over 4
  668: 
  669: Input: $cache, $courseData, $name
  670: 
  671: $cache: A hash pointer to store data
  672: 
  673: $courseData:  A hash pointer that points to the course data downloaded for a 
  674: student.
  675: 
  676: $name:  username:domain
  677: 
  678: Output: None
  679: 
  680: *NOTE:  There is no output, but an error message is stored away in the cache 
  681: data.  This is checked in &FormatStudentData().  The key username:domain:error 
  682: will only exist if an error occured.  The error is an error from 
  683: &DownloadCourseInformation().
  684: 
  685: =back
  686: 
  687: =cut
  688: 
  689: sub ProcessStudentData {
  690:     my ($cache,$courseData,$name)=@_;
  691: 
  692:     if(!&CheckDateStampError($courseData, $cache, $name)) {
  693:         return;
  694:     }
  695: 
  696:     # user name:domain was prepended earlier in DownloadCourseInformation
  697:     foreach (keys %$courseData) {
  698:         $cache->{$_}=$courseData->{$_};
  699:     }
  700: 
  701:     return;
  702: }
  703: 
  704: =pod
  705: 
  706: =item &ExtractStudentData()
  707: 
  708: HISTORY: This function originally existed in every statistics module,
  709: and performed different tasks, the had some overlap.  Due to the need
  710: for the data from the different modules, they were combined into
  711: a single function.
  712: 
  713: This function now extracts all the necessary course data for a student
  714: from what was downloaded from their homeserver.  There is some extra
  715: time overhead compared to the ProcessStudentInformation function, but
  716: it would have had to occurred at some point anyways.  This is now
  717: typically called while downloading the data it will process.  It is
  718: the brother function to ProcessStudentInformation.
  719: 
  720: =over 4
  721: 
  722: Input: $input, $output, $data, $name
  723: 
  724: $input: A hash that contains the input data to be processed
  725: 
  726: $output: A hash to contain the processed data
  727: 
  728: $data: A hash containing the information on what is to be
  729: processed and how (basically).
  730: 
  731: $name:  username:domain
  732: 
  733: The input is slightly different here, but is quite simple.
  734: It is currently used where the $input, $output, and $data
  735: can and are often the same hashes, but they do not need
  736: to be.
  737: 
  738: Output: None
  739: 
  740: *NOTE:  There is no output, but an error message is stored away in the cache 
  741: data.  This is checked in &FormatStudentData().  The key username:domain:error 
  742: will only exist if an error occured.  The error is an error from 
  743: &DownloadCourseInformation().
  744: 
  745: =back
  746: 
  747: =cut
  748: 
  749: sub ExtractStudentData {
  750:     my ($input, $output, $data, $name)=@_;
  751: 
  752:     if(!&CheckDateStampError($input, $data, $name)) {
  753:         return;
  754:     }
  755: 
  756:     my ($username,$domain)=split(':',$name);
  757: 
  758:     my $Version;
  759:     my $problemsCorrect = 0;
  760:     my $totalProblems   = 0;
  761:     my $problemsSolved  = 0;
  762:     my $numberOfParts   = 0;
  763:     my $totalAwarded    = 0;
  764:     foreach my $sequence (split(':', $data->{'orderedSequences'})) {
  765:         foreach my $problemID (split(':', $data->{$sequence.':problems'})) {
  766:             my $problem = $data->{$problemID.':problem'};
  767:             my $LatestVersion = $input->{$name.':version:'.$problem};
  768: 
  769:             # Output dashes for all the parts of this problem if there
  770:             # is no version information about the current problem.
  771:             if(!$LatestVersion) {
  772:                 foreach my $part (split(/\:/,$data->{$sequence.':'.
  773:                                                       $problemID.
  774:                                                       ':parts'})) {
  775:                     $output->{$name.':'.$problemID.':'.$part.':tries'} = 0;
  776:                     $output->{$name.':'.$problemID.':'.$part.':awarded'} = 0;
  777:                     $output->{$name.':'.$problemID.':'.$part.':code'} = ' ';
  778:                     $totalProblems++;
  779:                 }
  780:                 $output->{$name.':'.$problemID.':NoVersion'} = 'true';
  781:                 next;
  782:             }
  783: 
  784:             my %partData=undef;
  785:             # Initialize part data, display skips correctly
  786:             # Skip refers to when a student made no submissions on that
  787:             # part/problem.
  788:             foreach my $part (split(/\:/,$data->{$sequence.':'.
  789:                                                  $problemID.
  790:                                                  ':parts'})) {
  791:                 $partData{$part.':tries'}=0;
  792:                 $partData{$part.':code'}=' ';
  793:                 $partData{$part.':awarded'}=0;
  794:                 $partData{$part.':timestamp'}=0;
  795:                 foreach my $response (split(':', $data->{$sequence.':'.
  796:                                                          $problemID.':'.
  797:                                                          $part.':responseIDs'})) {
  798:                     $partData{$part.':'.$response.':submission'}='';
  799:                 }
  800:             }
  801: 
  802:             # Looping through all the versions of each part, starting with the
  803:             # oldest version.  Basically, it gets the most recent 
  804:             # set of grade data for each part.
  805:             my @submissions = ();
  806: 	    for(my $Version=1; $Version<=$LatestVersion; $Version++) {
  807:                 foreach my $part (split(/\:/,$data->{$sequence.':'.
  808:                                                      $problemID.
  809:                                                      ':parts'})) {
  810: 
  811:                     if(!defined($input->{"$name:$Version:$problem".
  812:                                          ":resource.$part.solved"})) {
  813:                         # No grade for this submission, so skip
  814:                         next;
  815:                     }
  816: 
  817:                     my $tries=0;
  818:                     my $code=' ';
  819:                     my $awarded=0;
  820: 
  821:                     $tries = $input->{$name.':'.$Version.':'.$problem.
  822:                                       ':resource.'.$part.'.tries'};
  823:                     $awarded = $input->{$name.':'.$Version.':'.$problem.
  824:                                         ':resource.'.$part.'.awarded'};
  825: 
  826:                     $partData{$part.':awarded'}=($awarded) ? $awarded : 0;
  827:                     $partData{$part.':tries'}=($tries) ? $tries : 0;
  828: 
  829:                     $partData{$part.':timestamp'}=$input->{$name.':'.$Version.':'.
  830:                                                            $problem.
  831:                                                            ':timestamp'};
  832:                     if(!$input->{$name.':'.$Version.':'.$problem.':resource.'.$part.
  833:                                  '.previous'}) {
  834:                         foreach my $response (split(':',
  835:                                                    $data->{$sequence.':'.
  836:                                                            $problemID.':'.
  837:                                                            $part.':responseIDs'})) {
  838:                             @submissions=($input->{$name.':'.$Version.':'.
  839:                                                    $problem.
  840:                                                    ':resource.'.$part.'.'.
  841:                                                    $response.'.submission'},
  842:                                           @submissions);
  843:                         }
  844:                     }
  845: 
  846:                     my $val = $input->{$name.':'.$Version.':'.$problem.
  847:                                        ':resource.'.$part.'.solved'};
  848:                     if    ($val eq 'correct_by_student')   {$code = '*';} 
  849:                     elsif ($val eq 'correct_by_override')  {$code = '+';}
  850:                     elsif ($val eq 'incorrect_attempted')  {$code = '.';} 
  851:                     elsif ($val eq 'incorrect_by_override'){$code = '-';}
  852:                     elsif ($val eq 'excused')              {$code = 'x';}
  853:                     elsif ($val eq 'ungraded_attempted')   {$code = '#';}
  854:                     else                                   {$code = ' ';}
  855:                     $partData{$part.':code'}=$code;
  856:                 }
  857:             }
  858: 
  859:             foreach my $part (split(/\:/,$data->{$sequence.':'.$problemID.
  860:                                                  ':parts'})) {
  861:                 $output->{$name.':'.$problemID.':'.$part.':wrong'} = 
  862:                     $partData{$part.':tries'};
  863: 
  864:                 if($partData{$part.':code'} eq '*') {
  865:                     $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
  866:                     $problemsCorrect++;
  867:                 } elsif($partData{$part.':code'} eq '+') {
  868:                     $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
  869:                     $problemsCorrect++;
  870:                 }
  871: 
  872:                 $output->{$name.':'.$problemID.':'.$part.':tries'} = 
  873:                     $partData{$part.':tries'};
  874:                 $output->{$name.':'.$problemID.':'.$part.':code'} =
  875:                     $partData{$part.':code'};
  876:                 $output->{$name.':'.$problemID.':'.$part.':awarded'} =
  877:                     $partData{$part.':awarded'};
  878:                 $totalAwarded += $partData{$part.':awarded'};
  879:                 $output->{$name.':'.$problemID.':'.$part.':timestamp'} =
  880:                     $partData{$part.':timestamp'};
  881:                 foreach my $response (split(':', $data->{$sequence.':'.
  882:                                                          $problemID.':'.
  883:                                                          $part.':responseIDs'})) {
  884:                     $output->{$name.':'.$problemID.':'.$part.':'.$response.
  885:                               ':submission'}=join(':::',@submissions);
  886:                 }
  887: 
  888:                 if($partData{$part.':code'} ne 'x') {
  889:                     $totalProblems++;
  890:                 }
  891:             }
  892:         }
  893: 
  894:         $output->{$name.':'.$sequence.':problemsCorrect'} = $problemsCorrect;
  895:         $problemsSolved += $problemsCorrect;
  896: 	$problemsCorrect=0;
  897:     }
  898: 
  899:     $output->{$name.':problemsSolved'} = $problemsSolved;
  900:     $output->{$name.':totalProblems'} = $totalProblems;
  901:     $output->{$name.':totalAwarded'} = $totalAwarded;
  902: 
  903:     return;
  904: }
  905: 
  906: sub LoadDiscussion {
  907:     my ($courseID)=@_;
  908:     my %Discuss=();
  909:     my %contrib=&Apache::lonnet::dump(
  910:                 $courseID,
  911:                 $ENV{'course.'.$courseID.'.domain'},
  912:                 $ENV{'course.'.$courseID.'.num'});
  913: 				 
  914:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
  915: 
  916:     foreach my $temp(keys %contrib) {
  917: 	if ($temp=~/^version/) {
  918: 	    my $ver=$contrib{$temp};
  919: 	    my ($dummy,$prb)=split(':',$temp);
  920: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
  921: 		my $name=$contrib{"$idx:$prb:sendername"};
  922: 		$Discuss{"$name:$prb"}=$idx;	
  923: 	    }
  924: 	}
  925:     }       
  926: 
  927:     return \%Discuss;
  928: }
  929: 
  930: # ----- END PROCESSING FUNCTIONS ---------------------------------------
  931: 
  932: =pod
  933: 
  934: =head1 HELPER FUNCTIONS
  935: 
  936: These are just a couple of functions do various odd and end 
  937: jobs.  There was also a couple of bulk functions added.  These are
  938: &DownloadStudentCourseData(), &DownloadStudentCourseDataSeparate(), and
  939: &CheckForResidualDownload().  These functions now act as the interface
  940: for downloading student course data.  The statistical modules should
  941: no longer make the calls to dump and download and process etc.  They
  942: make calls to these bulk functions to get their data.
  943: 
  944: =cut
  945: 
  946: # ----- HELPER FUNCTIONS -----------------------------------------------
  947: 
  948: sub CheckDateStampError {
  949:     my ($courseData, $cache, $name)=@_;
  950:     if($courseData->{$name.':UpToDate'} eq 'true') {
  951:         $cache->{$name.':lastDownloadTime'} = 
  952:             $courseData->{$name.':lastDownloadTime'};
  953:         if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
  954:             $cache->{$name.':updateTime'} = ' Not updated';
  955:         } else {
  956:             $cache->{$name.':updateTime'}=
  957:                 localtime($courseData->{$name.':lastDownloadTime'});
  958:         }
  959:         return 0;
  960:     }
  961: 
  962:     $cache->{$name.':lastDownloadTime'}=$courseData->{$name.':lastDownloadTime'};
  963:     if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
  964:         $cache->{$name.':updateTime'} = ' Not updated';
  965:     } else {
  966:         $cache->{$name.':updateTime'}=
  967:             localtime($courseData->{$name.':lastDownloadTime'});
  968:     }
  969: 
  970:     if(defined($courseData->{$name.':error'})) {
  971:         $cache->{$name.':error'}=$courseData->{$name.':error'};
  972:         return 0;
  973:     }
  974: 
  975:     return 1;
  976: }
  977: 
  978: =pod
  979: 
  980: =item &ProcessFullName()
  981: 
  982: Takes lastname, generation, firstname, and middlename (or some partial
  983: set of this data) and returns the full name version as a string.  Format
  984: is Lastname generation, firstname middlename or a subset of this.
  985: 
  986: =cut
  987: 
  988: sub ProcessFullName {
  989:     my ($lastname, $generation, $firstname, $middlename)=@_;
  990:     my $Str = '';
  991: 
  992:     if($lastname ne '') {
  993: 	$Str .= $lastname.' ';
  994: 	if($generation ne '') {
  995: 	    $Str .= $generation;
  996: 	} else {
  997: 	    chop($Str);
  998: 	}
  999: 	$Str .= ', ';
 1000: 	if($firstname ne '') {
 1001: 	    $Str .= $firstname.' ';
 1002: 	}
 1003: 	if($middlename ne '') {
 1004: 	    $Str .= $middlename;
 1005: 	} else {
 1006: 	    chop($Str);
 1007: 	    if($firstname eq '') {
 1008: 		chop($Str);
 1009: 	    }
 1010: 	}
 1011:     } else {
 1012: 	if($firstname ne '') {
 1013: 	    $Str .= $firstname.' ';
 1014: 	}
 1015: 	if($middlename ne '') {
 1016: 	    $Str .= $middlename.' ';
 1017: 	}
 1018: 	if($generation ne '') {
 1019: 	    $Str .= $generation;
 1020: 	} else {
 1021: 	    chop($Str);
 1022: 	}
 1023:     }
 1024: 
 1025:     return $Str;
 1026: }
 1027: 
 1028: =pod
 1029: 
 1030: =item &TestCacheData()
 1031: 
 1032: Determine if the cache database can be accessed with a tie.  It waits up to
 1033: ten seconds before returning failure.  This function exists to help with
 1034: the problems with stopping the data download.  When an abort occurs and the
 1035: user quickly presses a form button and httpd child is created.  This
 1036: child needs to wait for the other to finish (hopefully within ten seconds).
 1037: 
 1038: =over 4
 1039: 
 1040: Input: $ChartDB
 1041: 
 1042: $ChartDB: The name of the cache database to be opened
 1043: 
 1044: Output: -1, 0, 1
 1045: 
 1046: -1: Could not tie database
 1047:  0: Use cached data
 1048:  1: New cache database created, use that.
 1049: 
 1050: =back
 1051: 
 1052: =cut
 1053: 
 1054: sub TestCacheData {
 1055:     my ($ChartDB,$isRecalculate,$totalDelay)=@_;
 1056:     my $isCached=-1;
 1057:     my %testData;
 1058:     my $tieTries=0;
 1059: 
 1060:     if(!defined($totalDelay)) {
 1061:         $totalDelay = 10;
 1062:     }
 1063: 
 1064:     if ((-e "$ChartDB") && (!$isRecalculate)) {
 1065: 	$isCached = 1;
 1066:     } else {
 1067: 	$isCached = 0;
 1068:     }
 1069: 
 1070:     while($tieTries < $totalDelay) {
 1071:         my $result=0;
 1072:         if($isCached) {
 1073:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
 1074:         } else {
 1075:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
 1076:         }
 1077:         if($result) {
 1078:             last;
 1079:         }
 1080:         $tieTries++;
 1081:         sleep 1;
 1082:     }
 1083:     if($tieTries >= $totalDelay) {
 1084:         return -1;
 1085:     }
 1086: 
 1087:     untie(%testData);
 1088: 
 1089:     return $isCached;
 1090: }
 1091: 
 1092: sub DownloadStudentCourseData {
 1093:     my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
 1094: 
 1095:     my $title = 'LON-CAPA Statistics';
 1096:     my $heading = 'Download and Process Course Data';
 1097:     my $studentCount = scalar(@$students);
 1098:     my %cache;
 1099: 
 1100: 
 1101:     my $WhatIWant;
 1102:     $WhatIWant = '(^version:|';
 1103:     $WhatIWant .= '^\d+:.+?:(resource\.\d+\.';
 1104:     $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
 1105:     $WhatIWant .= '|timestamp)';
 1106:     $WhatIWant .= ')';
 1107: #    $WhatIWant = '.';
 1108: 
 1109:     if($status eq 'true') {
 1110:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
 1111:     }
 1112: 
 1113:     my $displayString;
 1114:     my $count=0;
 1115:     foreach (@$students) {
 1116:         if($c->aborted()) { return 'Aborted'; }
 1117: 
 1118:         if($status eq 'true') {
 1119:             $count++;
 1120:             my $displayString = $count.'/'.$studentCount.': '.$_;
 1121:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
 1122:         }
 1123: 
 1124:         my $downloadTime='Not downloaded';
 1125:         if($checkDate eq 'true'  && 
 1126:            tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
 1127:             $downloadTime = $cache{$_.':lastDownloadTime'};
 1128:             untie(%cache);
 1129:         }
 1130: 
 1131:         if($c->aborted()) { return 'Aborted'; }
 1132: 
 1133:         if($downloadTime eq 'Not downloaded') {
 1134:             my $courseData = 
 1135:                 &DownloadCourseInformation($_, $courseID, $downloadTime, 
 1136:                                            $WhatIWant);
 1137:             if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
 1138:                 foreach my $key (keys(%$courseData)) {
 1139:                     if($key =~ /^(con_lost|error|no_such_host)/i) {
 1140:                         $courseData->{$_.':error'} = 'No course data for '.$_;
 1141:                         last;
 1142:                     }
 1143:                 }
 1144:                 if($extract eq 'true') {
 1145:                     &ExtractStudentData($courseData, \%cache, \%cache, $_);
 1146:                 } else {
 1147:                     &ProcessStudentData(\%cache, $courseData, $_);
 1148:                 }
 1149:                 untie(%cache);
 1150:             } else {
 1151:                 next;
 1152:             }
 1153:         }
 1154:     }
 1155:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
 1156: 
 1157:     return 'OK';
 1158: }
 1159: 
 1160: sub DownloadStudentCourseDataSeparate {
 1161:     my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
 1162:     my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
 1163:     my $title = 'LON-CAPA Statistics';
 1164:     my $heading = 'Download Course Data';
 1165: 
 1166:     my $WhatIWant;
 1167:     $WhatIWant = '(^version:|';
 1168:     $WhatIWant .= '^\d+:.+?:(resource\.\d+\.';
 1169:     $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
 1170:     $WhatIWant .= '|timestamp)';
 1171:     $WhatIWant .= ')';
 1172: 
 1173:     &CheckForResidualDownload($courseID, $cacheDB, $students, $c);
 1174: 
 1175:     my %cache;
 1176: 
 1177:     my $studentCount = scalar(@$students);
 1178:     if($status eq 'true') {
 1179:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
 1180:     }
 1181:     my $count=0;
 1182:     my $displayString='';
 1183:     foreach (@$students) {
 1184:         if($c->aborted()) {
 1185:             return 'Aborted';
 1186:         }
 1187: 
 1188:         if($status eq 'true') {
 1189:             $count++;
 1190:             $displayString = $count.'/'.$studentCount.': '.$_;
 1191:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
 1192:         }
 1193: 
 1194:         my $downloadTime='Not downloaded';
 1195:         if($checkDate eq 'true'  && 
 1196:            tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
 1197:             $downloadTime = $cache{$_.':lastDownloadTime'};
 1198:             untie(%cache);
 1199:         }
 1200: 
 1201:         if($c->aborted()) {
 1202:             return 'Aborted';
 1203:         }
 1204: 
 1205:         if($downloadTime eq 'Not downloaded') {
 1206:             my $error = 0;
 1207:             my $courseData = 
 1208:                 &DownloadCourseInformation($_, $courseID, $downloadTime,
 1209:                                            $WhatIWant);
 1210:             my %downloadData;
 1211:             unless(tie(%downloadData,'GDBM_File',$residualFile,
 1212:                        &GDBM_WRCREAT(),0640)) {
 1213:                 return 'Failed to tie temporary download hash.';
 1214:             }
 1215:             foreach my $key (keys(%$courseData)) {
 1216:                 $downloadData{$key} = $courseData->{$key};
 1217:                 if($key =~ /^(con_lost|error|no_such_host)/i) {
 1218:                     $error = 1;
 1219:                     last;
 1220:                 }
 1221:             }
 1222:             if($error) {
 1223:                 foreach my $deleteKey (keys(%$courseData)) {
 1224:                     delete $downloadData{$deleteKey};
 1225:                 }
 1226:                 $downloadData{$_.':error'} = 'No course data for '.$_;
 1227:             }
 1228:             untie(%downloadData);
 1229:         }
 1230:     }
 1231:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
 1232: 
 1233:     return &CheckForResidualDownload($cacheDB, 'true', 'true', 
 1234:                                      $courseID, $r, $c);
 1235: }
 1236: 
 1237: sub CheckForResidualDownload {
 1238:     my ($cacheDB,$extract,$status,$courseID,$r,$c)=@_;
 1239: 
 1240:     my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
 1241:     if(!-e $residualFile) {
 1242:         return 'OK';
 1243:     }
 1244: 
 1245:     my %downloadData;
 1246:     my %cache;
 1247:     unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_READER(),0640)) {
 1248:         return 'Can not tie database for check for residual download: tempDB';
 1249:     }
 1250:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
 1251:         untie(%downloadData);
 1252:         return 'Can not tie database for check for residual download: cacheDB';
 1253:     }
 1254: 
 1255:     my @students=();
 1256:     my %checkStudent;
 1257:     my $key;
 1258:     while(($key, undef) = each %downloadData) {
 1259:         my @temp = split(':', $key);
 1260:         my $student = $temp[0].':'.$temp[1];
 1261:         if(!defined($checkStudent{$student})) {
 1262:             $checkStudent{$student}++;
 1263:             push(@students, $student);
 1264:         }
 1265:     }
 1266: 
 1267:     my $heading = 'Process Course Data';
 1268:     my $title = 'LON-CAPA Statistics';
 1269:     my $studentCount = scalar(@students);
 1270:     if($status eq 'true') {
 1271:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
 1272:     }
 1273: 
 1274:     my $count=1;
 1275:     foreach my $name (@students) {
 1276:         last if($c->aborted());
 1277: 
 1278:         if($status eq 'true') {
 1279:             my $displayString = $count.'/'.$studentCount.': '.$name;
 1280:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
 1281:         }
 1282: 
 1283:         if($extract eq 'true') {
 1284:             &ExtractStudentData(\%downloadData, \%cache, \%cache, $name);
 1285:         } else {
 1286:             &ProcessStudentData(\%cache, \%downloadData, $name);
 1287:         }
 1288:         $count++;
 1289:     }
 1290: 
 1291:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
 1292: 
 1293:     untie(%cache);
 1294:     untie(%downloadData);
 1295: 
 1296:     if(!$c->aborted()) {
 1297:         my @files = ($residualFile);
 1298:         unlink(@files);
 1299:     }
 1300: 
 1301:     return 'OK';
 1302: }
 1303: 
 1304: # ----- END HELPER FUNCTIONS --------------------------------------------
 1305: 
 1306: 1;
 1307: __END__

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