File:  [LON-CAPA] / loncom / interface / loncoursedata.pm
Revision 1.26: download - view: text, annotated - select for diffs
Fri Aug 30 17:11:43 2002 UTC (21 years, 10 months ago) by stredwic
Branches: MAIN
CVS tags: HEAD
First, was getting a strange warning for doing scalar(split(...)) in
lonpercentage.  Second I think I fixed the updating of symbols in
chart after they have been changed.  I tried it out and it work for
my test case.  We will see if it works for everyone.  The problem
is as I suspected, and because I didn't remove the keys when the
students data was updated it was still using old data.  This
would occur if the data didn't overwrite, but changed supporting
information.

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: loncoursedata.pm,v 1.26 2002/08/30 17:11:43 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:     if(defined($cache->{$name.':keys'})) {
  697: 	foreach (split(':::', $cache->{$name.':keys'})) {
  698: 	    delete $cache->{$name.':'.$_};
  699: 	}
  700:     }
  701: 
  702:     my %courseKeys;
  703:     # user name:domain was prepended earlier in DownloadCourseInformation
  704:     foreach (keys %$courseData) {
  705: 	my $currentKey =~ s/^$name//;
  706: 	$courseKeys{$currentKey}++;
  707:         $cache->{$_}=$courseData->{$_};
  708:     }
  709: 
  710:     $cache->{$name.':keys'} = join(':::', keys(%courseKeys));
  711: 
  712:     return;
  713: }
  714: 
  715: =pod
  716: 
  717: =item &ExtractStudentData()
  718: 
  719: HISTORY: This function originally existed in every statistics module,
  720: and performed different tasks, the had some overlap.  Due to the need
  721: for the data from the different modules, they were combined into
  722: a single function.
  723: 
  724: This function now extracts all the necessary course data for a student
  725: from what was downloaded from their homeserver.  There is some extra
  726: time overhead compared to the ProcessStudentInformation function, but
  727: it would have had to occurred at some point anyways.  This is now
  728: typically called while downloading the data it will process.  It is
  729: the brother function to ProcessStudentInformation.
  730: 
  731: =over 4
  732: 
  733: Input: $input, $output, $data, $name
  734: 
  735: $input: A hash that contains the input data to be processed
  736: 
  737: $output: A hash to contain the processed data
  738: 
  739: $data: A hash containing the information on what is to be
  740: processed and how (basically).
  741: 
  742: $name:  username:domain
  743: 
  744: The input is slightly different here, but is quite simple.
  745: It is currently used where the $input, $output, and $data
  746: can and are often the same hashes, but they do not need
  747: to be.
  748: 
  749: Output: None
  750: 
  751: *NOTE:  There is no output, but an error message is stored away in the cache 
  752: data.  This is checked in &FormatStudentData().  The key username:domain:error 
  753: will only exist if an error occured.  The error is an error from 
  754: &DownloadCourseInformation().
  755: 
  756: =back
  757: 
  758: =cut
  759: 
  760: sub ExtractStudentData {
  761:     my ($input, $output, $data, $name)=@_;
  762: 
  763:     if(!&CheckDateStampError($input, $data, $name)) {
  764:         return;
  765:     }
  766: 
  767:     my %allkeys;
  768:     if(defined($output->{$name.':keys'})) {
  769: 	foreach (split(':::', $output->{$name.':keys'})) {
  770: 	    delete $output->{$name.':'.$_};
  771: 	}
  772:     }
  773: 
  774:     my ($username,$domain)=split(':',$name);
  775: 
  776:     my $Version;
  777:     my $problemsCorrect = 0;
  778:     my $totalProblems   = 0;
  779:     my $problemsSolved  = 0;
  780:     my $numberOfParts   = 0;
  781:     my $totalAwarded    = 0;
  782:     foreach my $sequence (split(':', $data->{'orderedSequences'})) {
  783:         foreach my $problemID (split(':', $data->{$sequence.':problems'})) {
  784:             my $problem = $data->{$problemID.':problem'};
  785:             my $LatestVersion = $input->{$name.':version:'.$problem};
  786: 
  787:             # Output dashes for all the parts of this problem if there
  788:             # is no version information about the current problem.
  789:             if(!$LatestVersion) {
  790:                 foreach my $part (split(/\:/,$data->{$sequence.':'.
  791:                                                       $problemID.
  792:                                                       ':parts'})) {
  793:                     $output->{$name.':'.$problemID.':'.$part.':tries'} = 0;
  794:                     $output->{$name.':'.$problemID.':'.$part.':awarded'} = 0;
  795:                     $output->{$name.':'.$problemID.':'.$part.':code'} = ' ';
  796: 		    $allkeys{$name.':'.$problemID.':'.$part.':tries'}++;
  797: 		    $allkeys{$name.':'.$problemID.':'.$part.':awarded'}++;
  798: 		    $allkeys{$name.':'.$problemID.':'.$part.':code'}++;
  799:                     $totalProblems++;
  800:                 }
  801:                 $output->{$name.':'.$problemID.':NoVersion'} = 'true';
  802: 		$allkeys{$name.':'.$problemID.':NoVersion'}++;
  803:                 next;
  804:             }
  805: 
  806:             my %partData=undef;
  807:             # Initialize part data, display skips correctly
  808:             # Skip refers to when a student made no submissions on that
  809:             # part/problem.
  810:             foreach my $part (split(/\:/,$data->{$sequence.':'.
  811:                                                  $problemID.
  812:                                                  ':parts'})) {
  813:                 $partData{$part.':tries'}=0;
  814:                 $partData{$part.':code'}=' ';
  815:                 $partData{$part.':awarded'}=0;
  816:                 $partData{$part.':timestamp'}=0;
  817:                 foreach my $response (split(':', $data->{$sequence.':'.
  818:                                                          $problemID.':'.
  819:                                                          $part.':responseIDs'})) {
  820:                     $partData{$part.':'.$response.':submission'}='';
  821:                 }
  822:             }
  823: 
  824:             # Looping through all the versions of each part, starting with the
  825:             # oldest version.  Basically, it gets the most recent 
  826:             # set of grade data for each part.
  827:             my @submissions = ();
  828: 	    for(my $Version=1; $Version<=$LatestVersion; $Version++) {
  829:                 foreach my $part (split(/\:/,$data->{$sequence.':'.
  830:                                                      $problemID.
  831:                                                      ':parts'})) {
  832: 
  833:                     if(!defined($input->{"$name:$Version:$problem".
  834:                                          ":resource.$part.solved"})) {
  835:                         # No grade for this submission, so skip
  836:                         next;
  837:                     }
  838: 
  839:                     my $tries=0;
  840:                     my $code=' ';
  841:                     my $awarded=0;
  842: 
  843:                     $tries = $input->{$name.':'.$Version.':'.$problem.
  844:                                       ':resource.'.$part.'.tries'};
  845:                     $awarded = $input->{$name.':'.$Version.':'.$problem.
  846:                                         ':resource.'.$part.'.awarded'};
  847: 
  848:                     $partData{$part.':awarded'}=($awarded) ? $awarded : 0;
  849:                     $partData{$part.':tries'}=($tries) ? $tries : 0;
  850: 
  851:                     $partData{$part.':timestamp'}=$input->{$name.':'.$Version.':'.
  852:                                                            $problem.
  853:                                                            ':timestamp'};
  854:                     if(!$input->{$name.':'.$Version.':'.$problem.':resource.'.$part.
  855:                                  '.previous'}) {
  856:                         foreach my $response (split(':',
  857:                                                    $data->{$sequence.':'.
  858:                                                            $problemID.':'.
  859:                                                            $part.':responseIDs'})) {
  860:                             @submissions=($input->{$name.':'.$Version.':'.
  861:                                                    $problem.
  862:                                                    ':resource.'.$part.'.'.
  863:                                                    $response.'.submission'},
  864:                                           @submissions);
  865:                         }
  866:                     }
  867: 
  868:                     my $val = $input->{$name.':'.$Version.':'.$problem.
  869:                                        ':resource.'.$part.'.solved'};
  870:                     if    ($val eq 'correct_by_student')   {$code = '*';} 
  871:                     elsif ($val eq 'correct_by_override')  {$code = '+';}
  872:                     elsif ($val eq 'incorrect_attempted')  {$code = '.';} 
  873:                     elsif ($val eq 'incorrect_by_override'){$code = '-';}
  874:                     elsif ($val eq 'excused')              {$code = 'x';}
  875:                     elsif ($val eq 'ungraded_attempted')   {$code = '#';}
  876:                     else                                   {$code = ' ';}
  877:                     $partData{$part.':code'}=$code;
  878:                 }
  879:             }
  880: 
  881:             foreach my $part (split(/\:/,$data->{$sequence.':'.$problemID.
  882:                                                  ':parts'})) {
  883:                 $output->{$name.':'.$problemID.':'.$part.':wrong'} = 
  884:                     $partData{$part.':tries'};
  885: 		$allkeys{$name.':'.$problemID.':'.$part.':wrong'}++;
  886: 
  887:                 if($partData{$part.':code'} eq '*') {
  888:                     $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
  889:                     $problemsCorrect++;
  890:                 } elsif($partData{$part.':code'} eq '+') {
  891:                     $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
  892:                     $problemsCorrect++;
  893:                 }
  894: 
  895:                 $output->{$name.':'.$problemID.':'.$part.':tries'} = 
  896:                     $partData{$part.':tries'};
  897:                 $output->{$name.':'.$problemID.':'.$part.':code'} =
  898:                     $partData{$part.':code'};
  899:                 $output->{$name.':'.$problemID.':'.$part.':awarded'} =
  900:                     $partData{$part.':awarded'};
  901: 		$allkeys{$name.':'.$problemID.':'.$part.':tries'}++;
  902: 		$allkeys{$name.':'.$problemID.':'.$part.':code'}++;
  903: 		$allkeys{$name.':'.$problemID.':'.$part.':awarded'}++;
  904: 
  905:                 $totalAwarded += $partData{$part.':awarded'};
  906:                 $output->{$name.':'.$problemID.':'.$part.':timestamp'} =
  907:                     $partData{$part.':timestamp'};
  908: 		$allkeys{$name.':'.$problemID.':'.$part.':timestamp'}++;
  909: 
  910:                 foreach my $response (split(':', $data->{$sequence.':'.
  911:                                                          $problemID.':'.
  912:                                                          $part.':responseIDs'})) {
  913:                     $output->{$name.':'.$problemID.':'.$part.':'.$response.
  914:                               ':submission'}=join(':::',@submissions);
  915: 		    $allkeys{$name.':'.$problemID.':'.$part.':'.$response.
  916: 			     ':submission'}++;
  917:                 }
  918: 
  919:                 if($partData{$part.':code'} ne 'x') {
  920:                     $totalProblems++;
  921:                 }
  922:             }
  923:         }
  924: 
  925:         $output->{$name.':'.$sequence.':problemsCorrect'} = $problemsCorrect;
  926: 	$allkeys{$name.':'.$sequence.':problemsCorrect'}++;
  927:         $problemsSolved += $problemsCorrect;
  928: 	$problemsCorrect=0;
  929:     }
  930: 
  931:     $output->{$name.':problemsSolved'} = $problemsSolved;
  932:     $output->{$name.':totalProblems'} = $totalProblems;
  933:     $output->{$name.':totalAwarded'} = $totalAwarded;
  934:     $allkeys{$name.':problemsSolved'}++;
  935:     $allkeys{$name.':totalProblems'}++;
  936:     $allkeys{$name.':totalAwarded'}++;
  937: 
  938:     $output->{$name.':keys'} = join(':::', keys(%allkeys));
  939: 
  940:     return;
  941: }
  942: 
  943: sub LoadDiscussion {
  944:     my ($courseID)=@_;
  945:     my %Discuss=();
  946:     my %contrib=&Apache::lonnet::dump(
  947:                 $courseID,
  948:                 $ENV{'course.'.$courseID.'.domain'},
  949:                 $ENV{'course.'.$courseID.'.num'});
  950: 				 
  951:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
  952: 
  953:     foreach my $temp(keys %contrib) {
  954: 	if ($temp=~/^version/) {
  955: 	    my $ver=$contrib{$temp};
  956: 	    my ($dummy,$prb)=split(':',$temp);
  957: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
  958: 		my $name=$contrib{"$idx:$prb:sendername"};
  959: 		$Discuss{"$name:$prb"}=$idx;	
  960: 	    }
  961: 	}
  962:     }       
  963: 
  964:     return \%Discuss;
  965: }
  966: 
  967: # ----- END PROCESSING FUNCTIONS ---------------------------------------
  968: 
  969: =pod
  970: 
  971: =head1 HELPER FUNCTIONS
  972: 
  973: These are just a couple of functions do various odd and end 
  974: jobs.  There was also a couple of bulk functions added.  These are
  975: &DownloadStudentCourseData(), &DownloadStudentCourseDataSeparate(), and
  976: &CheckForResidualDownload().  These functions now act as the interface
  977: for downloading student course data.  The statistical modules should
  978: no longer make the calls to dump and download and process etc.  They
  979: make calls to these bulk functions to get their data.
  980: 
  981: =cut
  982: 
  983: # ----- HELPER FUNCTIONS -----------------------------------------------
  984: 
  985: sub CheckDateStampError {
  986:     my ($courseData, $cache, $name)=@_;
  987:     if($courseData->{$name.':UpToDate'} eq 'true') {
  988:         $cache->{$name.':lastDownloadTime'} = 
  989:             $courseData->{$name.':lastDownloadTime'};
  990:         if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
  991:             $cache->{$name.':updateTime'} = ' Not updated';
  992:         } else {
  993:             $cache->{$name.':updateTime'}=
  994:                 localtime($courseData->{$name.':lastDownloadTime'});
  995:         }
  996:         return 0;
  997:     }
  998: 
  999:     $cache->{$name.':lastDownloadTime'}=$courseData->{$name.':lastDownloadTime'};
 1000:     if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
 1001:         $cache->{$name.':updateTime'} = ' Not updated';
 1002:     } else {
 1003:         $cache->{$name.':updateTime'}=
 1004:             localtime($courseData->{$name.':lastDownloadTime'});
 1005:     }
 1006: 
 1007:     if(defined($courseData->{$name.':error'})) {
 1008:         $cache->{$name.':error'}=$courseData->{$name.':error'};
 1009:         return 0;
 1010:     }
 1011: 
 1012:     return 1;
 1013: }
 1014: 
 1015: =pod
 1016: 
 1017: =item &ProcessFullName()
 1018: 
 1019: Takes lastname, generation, firstname, and middlename (or some partial
 1020: set of this data) and returns the full name version as a string.  Format
 1021: is Lastname generation, firstname middlename or a subset of this.
 1022: 
 1023: =cut
 1024: 
 1025: sub ProcessFullName {
 1026:     my ($lastname, $generation, $firstname, $middlename)=@_;
 1027:     my $Str = '';
 1028: 
 1029:     if($lastname ne '') {
 1030: 	$Str .= $lastname.' ';
 1031: 	if($generation ne '') {
 1032: 	    $Str .= $generation;
 1033: 	} else {
 1034: 	    chop($Str);
 1035: 	}
 1036: 	$Str .= ', ';
 1037: 	if($firstname ne '') {
 1038: 	    $Str .= $firstname.' ';
 1039: 	}
 1040: 	if($middlename ne '') {
 1041: 	    $Str .= $middlename;
 1042: 	} else {
 1043: 	    chop($Str);
 1044: 	    if($firstname eq '') {
 1045: 		chop($Str);
 1046: 	    }
 1047: 	}
 1048:     } else {
 1049: 	if($firstname ne '') {
 1050: 	    $Str .= $firstname.' ';
 1051: 	}
 1052: 	if($middlename ne '') {
 1053: 	    $Str .= $middlename.' ';
 1054: 	}
 1055: 	if($generation ne '') {
 1056: 	    $Str .= $generation;
 1057: 	} else {
 1058: 	    chop($Str);
 1059: 	}
 1060:     }
 1061: 
 1062:     return $Str;
 1063: }
 1064: 
 1065: =pod
 1066: 
 1067: =item &TestCacheData()
 1068: 
 1069: Determine if the cache database can be accessed with a tie.  It waits up to
 1070: ten seconds before returning failure.  This function exists to help with
 1071: the problems with stopping the data download.  When an abort occurs and the
 1072: user quickly presses a form button and httpd child is created.  This
 1073: child needs to wait for the other to finish (hopefully within ten seconds).
 1074: 
 1075: =over 4
 1076: 
 1077: Input: $ChartDB
 1078: 
 1079: $ChartDB: The name of the cache database to be opened
 1080: 
 1081: Output: -1, 0, 1
 1082: 
 1083: -1: Could not tie database
 1084:  0: Use cached data
 1085:  1: New cache database created, use that.
 1086: 
 1087: =back
 1088: 
 1089: =cut
 1090: 
 1091: sub TestCacheData {
 1092:     my ($ChartDB,$isRecalculate,$totalDelay)=@_;
 1093:     my $isCached=-1;
 1094:     my %testData;
 1095:     my $tieTries=0;
 1096: 
 1097:     if(!defined($totalDelay)) {
 1098:         $totalDelay = 10;
 1099:     }
 1100: 
 1101:     if ((-e "$ChartDB") && (!$isRecalculate)) {
 1102: 	$isCached = 1;
 1103:     } else {
 1104: 	$isCached = 0;
 1105:     }
 1106: 
 1107:     while($tieTries < $totalDelay) {
 1108:         my $result=0;
 1109:         if($isCached) {
 1110:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
 1111:         } else {
 1112:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
 1113:         }
 1114:         if($result) {
 1115:             last;
 1116:         }
 1117:         $tieTries++;
 1118:         sleep 1;
 1119:     }
 1120:     if($tieTries >= $totalDelay) {
 1121:         return -1;
 1122:     }
 1123: 
 1124:     untie(%testData);
 1125: 
 1126:     return $isCached;
 1127: }
 1128: 
 1129: sub DownloadStudentCourseData {
 1130:     my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
 1131: 
 1132:     my $title = 'LON-CAPA Statistics';
 1133:     my $heading = 'Download and Process Course Data';
 1134:     my $studentCount = scalar(@$students);
 1135: 
 1136:     my $WhatIWant;
 1137:     $WhatIWant = '(^version:|';
 1138:     $WhatIWant .= '^\d+:.+?:(resource\.\d+\.';
 1139:     $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
 1140:     $WhatIWant .= '|timestamp)';
 1141:     $WhatIWant .= ')';
 1142: #    $WhatIWant = '.';
 1143: 
 1144:     if($status eq 'true') {
 1145:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
 1146:     }
 1147: 
 1148:     my $displayString;
 1149:     my $count=0;
 1150:     foreach (@$students) {
 1151:         my %cache;
 1152: 
 1153:         if($c->aborted()) { return 'Aborted'; }
 1154: 
 1155:         if($status eq 'true') {
 1156:             $count++;
 1157:             my $displayString = $count.'/'.$studentCount.': '.$_;
 1158:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
 1159:         }
 1160: 
 1161:         my $downloadTime='Not downloaded';
 1162:         if($checkDate eq 'true'  && 
 1163:            tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
 1164:             $downloadTime = $cache{$_.':lastDownloadTime'};
 1165:             untie(%cache);
 1166:         }
 1167: 
 1168:         if($c->aborted()) { return 'Aborted'; }
 1169: 
 1170:         #if($downloadTime ne 'Not downloaded') {
 1171: 	#    next;
 1172: 	#}
 1173: 	my $courseData = 
 1174: 	    &DownloadCourseInformation($_, $courseID, $downloadTime, 
 1175: 				       $WhatIWant);
 1176: 	if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
 1177: 	    foreach my $key (keys(%$courseData)) {
 1178: 		if($key =~ /^(con_lost|error|no_such_host)/i) {
 1179: 		    $courseData->{$_.':error'} = 'No course data for '.$_;
 1180: 		    last;
 1181: 		}
 1182: 	    }
 1183: 	    if($extract eq 'true') {
 1184: 		&ExtractStudentData($courseData, \%cache, \%cache, $_);
 1185: 	    } else {
 1186: 		&ProcessStudentData(\%cache, $courseData, $_);
 1187: 	    }
 1188: 	    untie(%cache);
 1189: 	} else {
 1190: 	    next;
 1191: 	}
 1192:     }
 1193:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
 1194: 
 1195:     return 'OK';
 1196: }
 1197: 
 1198: sub DownloadStudentCourseDataSeparate {
 1199:     my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
 1200:     my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
 1201:     my $title = 'LON-CAPA Statistics';
 1202:     my $heading = 'Download Course Data';
 1203: 
 1204:     my $WhatIWant;
 1205:     $WhatIWant = '(^version:|';
 1206:     $WhatIWant .= '^\d+:.+?:(resource\.\d+\.';
 1207:     $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
 1208:     $WhatIWant .= '|timestamp)';
 1209:     $WhatIWant .= ')';
 1210: 
 1211:     &CheckForResidualDownload($courseID, $cacheDB, $students, $c);
 1212: 
 1213:     my $studentCount = scalar(@$students);
 1214:     if($status eq 'true') {
 1215:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
 1216:     }
 1217:     my $count=0;
 1218:     my $displayString='';
 1219:     foreach (@$students) {
 1220:         if($c->aborted()) {
 1221:             return 'Aborted';
 1222:         }
 1223: 
 1224:         if($status eq 'true') {
 1225:             $count++;
 1226:             $displayString = $count.'/'.$studentCount.': '.$_;
 1227:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
 1228:         }
 1229: 
 1230:         my %cache;
 1231:         my $downloadTime='Not downloaded';
 1232:         if($checkDate eq 'true'  && 
 1233:            tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
 1234:             $downloadTime = $cache{$_.':lastDownloadTime'};
 1235:             untie(%cache);
 1236:         }
 1237: 
 1238:         if($c->aborted()) {
 1239:             return 'Aborted';
 1240:         }
 1241: 
 1242:         #if($downloadTime eq 'Not downloaded') {
 1243:             my $error = 0;
 1244:             my $courseData = 
 1245:                 &DownloadCourseInformation($_, $courseID, $downloadTime,
 1246:                                            $WhatIWant);
 1247:             my %downloadData;
 1248:             unless(tie(%downloadData,'GDBM_File',$residualFile,
 1249:                        &GDBM_WRCREAT(),0640)) {
 1250:                 return 'Failed to tie temporary download hash.';
 1251:             }
 1252:             foreach my $key (keys(%$courseData)) {
 1253:                 $downloadData{$key} = $courseData->{$key};
 1254:                 if($key =~ /^(con_lost|error|no_such_host)/i) {
 1255:                     $error = 1;
 1256:                     last;
 1257:                 }
 1258:             }
 1259:             if($error) {
 1260:                 foreach my $deleteKey (keys(%$courseData)) {
 1261:                     delete $downloadData{$deleteKey};
 1262:                 }
 1263:                 $downloadData{$_.':error'} = 'No course data for '.$_;
 1264:             }
 1265:             untie(%downloadData);
 1266:         #}
 1267:     }
 1268:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
 1269: 
 1270:     return &CheckForResidualDownload($cacheDB, 'true', 'true', 
 1271:                                      $courseID, $r, $c);
 1272: }
 1273: 
 1274: sub CheckForResidualDownload {
 1275:     my ($cacheDB,$extract,$status,$courseID,$r,$c)=@_;
 1276: 
 1277:     my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
 1278:     if(!-e $residualFile) {
 1279:         return 'OK';
 1280:     }
 1281: 
 1282:     my %downloadData;
 1283:     my %cache;
 1284:     unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_READER(),0640)) {
 1285:         return 'Can not tie database for check for residual download: tempDB';
 1286:     }
 1287:     unless(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
 1288:         untie(%downloadData);
 1289:         return 'Can not tie database for check for residual download: cacheDB';
 1290:     }
 1291: 
 1292:     my @students=();
 1293:     my %checkStudent;
 1294:     my $key;
 1295:     while(($key, undef) = each %downloadData) {
 1296:         my @temp = split(':', $key);
 1297:         my $student = $temp[0].':'.$temp[1];
 1298:         if(!defined($checkStudent{$student})) {
 1299:             $checkStudent{$student}++;
 1300:             push(@students, $student);
 1301:         }
 1302:     }
 1303: 
 1304:     my $heading = 'Process Course Data';
 1305:     my $title = 'LON-CAPA Statistics';
 1306:     my $studentCount = scalar(@students);
 1307:     if($status eq 'true') {
 1308:         &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
 1309:     }
 1310: 
 1311:     my $count=1;
 1312:     foreach my $name (@students) {
 1313:         last if($c->aborted());
 1314: 
 1315:         if($status eq 'true') {
 1316:             my $displayString = $count.'/'.$studentCount.': '.$name;
 1317:             &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
 1318:         }
 1319: 
 1320:         if($extract eq 'true') {
 1321:             &ExtractStudentData(\%downloadData, \%cache, \%cache, $name);
 1322:         } else {
 1323:             &ProcessStudentData(\%cache, \%downloadData, $name);
 1324:         }
 1325:         $count++;
 1326:     }
 1327: 
 1328:     if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
 1329: 
 1330:     untie(%cache);
 1331:     untie(%downloadData);
 1332: 
 1333:     if(!$c->aborted()) {
 1334:         my @files = ($residualFile);
 1335:         unlink(@files);
 1336:     }
 1337: 
 1338:     return 'OK';
 1339: }
 1340: 
 1341: # ----- END HELPER FUNCTIONS --------------------------------------------
 1342: 
 1343: 1;
 1344: __END__

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