File:  [LON-CAPA] / loncom / interface / loncoursedata.pm
Revision 1.10: download - view: text, annotated - select for diffs
Thu Aug 1 20:49:06 2002 UTC (21 years, 11 months ago) by stredwic
Branches: MAIN
CVS tags: HEAD
First, added the parenthesis thing to the GDBM stuff.  Fixed the interface
problem statistics so that the buttons work correctly.  How the data
is interpretted is not finished.

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: loncoursedata.pm,v 1.10 2002/08/01 20:49:06 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 information.
   39: 
   40: =head1 PACKAGES USED
   41: 
   42:  Apache::Constants qw(:common :http)
   43:  Apache::lonnet()
   44:  HTML::TokeParser
   45:  GDBM_File
   46: 
   47: =cut
   48: 
   49: package Apache::loncoursedata;
   50: 
   51: use strict;
   52: use Apache::Constants qw(:common :http);
   53: use Apache::lonnet();
   54: use HTML::TokeParser;
   55: use GDBM_File;
   56: 
   57: =pod
   58: 
   59: =head1 DOWNLOAD INFORMATION
   60: 
   61: This section contains all the files that get data from other servers 
   62: and/or itself.  There is one function that has a call to get remote
   63: information but isn't included here which is ProcessTopLevelMap.  The
   64: usage was small enough to be ignored, but that portion may be moved
   65: here in the future.
   66: 
   67: =cut
   68: 
   69: # ----- DOWNLOAD INFORMATION -------------------------------------------
   70: 
   71: =pod
   72: 
   73: =item &DownloadClasslist()
   74: 
   75: Collects lastname, generation, middlename, firstname, PID, and section for each
   76: student from their environment database.  The list of students is built from
   77: collecting a classlist for the course that is to be displayed.
   78: 
   79: =over 4
   80: 
   81: Input: $courseID, $c
   82: 
   83: $courseID:  The id of the course
   84: 
   85: $c: The connection class that can determine if the browser has aborted.  It
   86: is used to short circuit this function so that it doesn't continue to 
   87: get information when there is no need.
   88: 
   89: Output: \%classlist
   90: 
   91: \%classlist: A pointer to a hash containing the following data:
   92: 
   93: -A list of student name:domain (as keys) (known below as $name)
   94: 
   95: -A hash pointer for each student containing lastname, generation, firstname,
   96: middlename, and PID : Key is $name.'studentInformation'
   97: 
   98: -A hash pointer to each students section data : Key is $name.section
   99: 
  100: =back
  101: 
  102: =cut
  103: 
  104: sub DownloadClasslist {
  105:     my ($courseID, $lastDownloadTime, $c)=@_;
  106:     my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
  107:     my %classlist;
  108: 
  109:     my $modifiedTime = &GetFileTimestamp($courseDomain, $courseNumber,
  110:                                      'classlist.db', 
  111:                                      $Apache::lonnet::perlvar{'lonUsersDir'});
  112: 
  113:     if($lastDownloadTime ne 'Not downloaded' &&
  114:        $lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
  115:         $classlist{'lastDownloadTime'}=time;
  116:         $classlist{'UpToDate'} = 'true';
  117:         return \%classlist;
  118:     }
  119: 
  120:     %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
  121:     my ($checkForError)=keys (%classlist);
  122:     if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
  123:         return \%classlist;
  124:     }
  125: 
  126:     foreach my $name (keys(%classlist)) {
  127:         if($c->aborted()) {
  128:             $classlist{'error'}='aborted';
  129:             return \%classlist;
  130:         }
  131: 
  132:         my ($studentName,$studentDomain) = split(/\:/,$name);
  133:         # Download student environment data, specifically the full name and id.
  134:         my %studentInformation=&Apache::lonnet::get('environment',
  135:                                                     ['lastname','generation',
  136:                                                      'firstname','middlename',
  137:                                                      'id'],
  138:                                                     $studentDomain,
  139:                                                     $studentName);
  140:         $classlist{$name.':studentInformation'}=\%studentInformation;
  141: 
  142:         if($c->aborted()) {
  143:             $classlist{'error'}='aborted';
  144:             return \%classlist;
  145:         }
  146: 
  147:         #Section
  148:         my %section=&Apache::lonnet::dump('roles',$studentDomain,$studentName);
  149:         $classlist{$name.':sections'}=\%section;
  150:     }
  151: 
  152:     $classlist{'UpToDate'} = 'false';
  153:     $classlist{'lastDownloadTime'}=time;
  154: 
  155:     return \%classlist;
  156: }
  157: 
  158: =pod
  159: 
  160: =item &DownloadCourseInformation()
  161: 
  162: Dump of all the course information for a single student.  There is no
  163: pruning of data, it is all stored in a hash and returned.  It also
  164: checks the timestamp of the students course database file and only downloads
  165: if it has been modified since the last download.
  166: 
  167: =over 4
  168: 
  169: Input: $name, $courseID
  170: 
  171: $name: student name:domain
  172: 
  173: $courseID:  The id of the course
  174: 
  175: Output: \%courseData
  176: 
  177: \%courseData:  A hash pointer to the raw data from the student's course
  178: database.
  179: 
  180: =back
  181: 
  182: =cut
  183: 
  184: sub DownloadCourseInformation {
  185:     my ($namedata,$courseID,$lastDownloadTime)=@_;
  186:     my %courseData;
  187:     my ($name,$domain) = split(/\:/,$namedata);
  188: 
  189:     my $modifiedTime = &GetFileTimestamp($domain, $name,
  190:                                       $courseID.'.db', 
  191:                                       $Apache::lonnet::perlvar{'lonUsersDir'});
  192: 
  193:     if($lastDownloadTime >= $modifiedTime) {
  194:         $courseData{'lastDownloadTime'}=time;
  195:         $courseData{'UpToDate'} = 'true';
  196:         return \%courseData;
  197:     }
  198: 
  199:     # Download course data
  200:     %courseData=&Apache::lonnet::dump($courseID, $domain, $name);
  201:     $courseData{'UpToDate'} = 'false';
  202:     $courseData{'lastDownloadTime'}=time;
  203:     return \%courseData;
  204: }
  205: 
  206: # ----- END DOWNLOAD INFORMATION ---------------------------------------
  207: 
  208: =pod
  209: 
  210: =head1 PROCESSING FUNCTIONS
  211: 
  212: These functions process all the data for all the students.  Also, they
  213: are the only functions that access the cache database for writing.  Thus
  214: they are the only functions that cache data.  The downloading and caching
  215: were separated to reduce problems with stopping downloading then can't
  216: tie hash to database later.
  217: 
  218: =cut
  219: 
  220: # ----- PROCESSING FUNCTIONS ---------------------------------------
  221: 
  222: =pod
  223: 
  224: =item &ProcessTopResourceMap()
  225: 
  226: Trace through the "big hash" created in rat/lonuserstate.pm::loadmap.  
  227: Basically, this function organizes a subset of the data and stores it in
  228: cached data.  The data stored is the problems, sequences, sequence titles,
  229: parts of problems, and their ordering.  Column width information is also 
  230: partially handled here on a per sequence basis.
  231: 
  232: =over 4
  233: 
  234: Input: $cache, $c
  235: 
  236: $cache:  A pointer to a hash to store the information
  237: 
  238: $c:  The connection class used to determine if an abort has been sent to the 
  239: browser
  240: 
  241: Output: A string that contains an error message or "OK" if everything went 
  242: smoothly.
  243: 
  244: =back
  245: 
  246: =cut
  247: 
  248: sub ProcessTopResourceMap {
  249:     my ($cache,$c,$r)=@_;
  250:     my %hash;
  251:     my $fn=$ENV{'request.course.fn'};
  252:     if(-e "$fn.db") {
  253: 	my $tieTries=0;
  254: 	while($tieTries < 3) {
  255:             if($c->aborted()) {
  256:                 return;
  257:             }
  258: 	    if(tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
  259: 		last;
  260: 	    }
  261: 	    $tieTries++;
  262: 	    sleep 1;
  263: 	}
  264: 	if($tieTries >= 3) {
  265:             return 'Coursemap undefined.';
  266:         }
  267:     } else {
  268:         return 'Can not open Coursemap.';
  269:     }
  270: 
  271:     # Initialize state machine.  Set information pointing to top level map.
  272:     my (@sequences, @currentResource, @finishResource);
  273:     my ($currentSequence, $currentResourceID, $lastResourceID);
  274: 
  275:     $currentResourceID=$hash{'ids_/res/'.$ENV{'request.course.uri'}};
  276:     push(@currentResource, $currentResourceID);
  277:     $lastResourceID=-1;
  278:     $currentSequence=-1;
  279:     my $topLevelSequenceNumber = $currentSequence;
  280: 
  281:     while(1) {
  282:         if($c->aborted()) {
  283:             last;
  284:         }
  285: 	# HANDLE NEW SEQUENCE!
  286: 	#if page || sequence
  287: 	if(defined($hash{'map_pc_'.$hash{'src_'.$currentResourceID}})) {
  288: 	    push(@sequences, $currentSequence);
  289: 	    push(@currentResource, $currentResourceID);
  290: 	    push(@finishResource, $lastResourceID);
  291: 
  292: 	    $currentSequence=$hash{'map_pc_'.$hash{'src_'.$currentResourceID}};
  293: 
  294:             # Mark sequence as containing problems.  If it doesn't, then
  295:             # it will be removed when processing for this sequence is
  296:             # complete.  This allows the problems in a sequence
  297:             # to be outputed before problems in the subsequences
  298:             if(!defined($cache->{'orderedSequences'})) {
  299:                 $cache->{'orderedSequences'}=$currentSequence;
  300:             } else {
  301:                 $cache->{'orderedSequences'}.=':'.$currentSequence;
  302:             }
  303: 
  304: 	    $lastResourceID=$hash{'map_finish_'.
  305: 				  $hash{'src_'.$currentResourceID}};
  306: 	    $currentResourceID=$hash{'map_start_'.
  307: 				     $hash{'src_'.$currentResourceID}};
  308: 
  309: 	    if(!($currentResourceID) || !($lastResourceID)) {
  310: 		$currentSequence=pop(@sequences);
  311: 		$currentResourceID=pop(@currentResource);
  312: 		$lastResourceID=pop(@finishResource);
  313: 		if($currentSequence eq $topLevelSequenceNumber) {
  314: 		    last;
  315: 		}
  316: 	    }
  317: 	}
  318: 
  319: 	# Handle gradable resources: exams, problems, etc
  320: 	$currentResourceID=~/(\d+)\.(\d+)/;
  321:         my $partA=$1;
  322:         my $partB=$2;
  323: 	if($hash{'src_'.$currentResourceID}=~
  324: 	   /\.(problem|exam|quiz|assess|survey|form)$/ &&
  325: 	   $partA eq $currentSequence) {
  326: 	    my $Problem = &Apache::lonnet::symbclean(
  327: 			  &Apache::lonnet::declutter($hash{'map_id_'.$partA}).
  328: 			  '___'.$partB.'___'.
  329: 			  &Apache::lonnet::declutter($hash{'src_'.
  330: 							 $currentResourceID}));
  331: 
  332: 	    $cache->{$currentResourceID.':problem'}=$Problem;
  333: 	    if(!defined($cache->{$currentSequence.':problems'})) {
  334: 		$cache->{$currentSequence.':problems'}=$currentResourceID;
  335: 	    } else {
  336: 		$cache->{$currentSequence.':problems'}.=
  337: 		    ':'.$currentResourceID;
  338: 	    }
  339: 
  340: 	    my $meta=$hash{'src_'.$currentResourceID};
  341: #            $cache->{$currentResourceID.':title'}=
  342: #                &Apache::lonnet::metdata($meta,'title');
  343:             $cache->{$currentResourceID.':title'}=
  344:                 $hash{'title_'.$currentResourceID};
  345:             $cache->{$currentResourceID.':source'}=
  346:                 $hash{'src_'.$currentResourceID};
  347: 
  348:             # Get Parts for problem
  349:             my %beenHere;
  350:             foreach (split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
  351:                 if(/^\w+response_\d+.*/) {
  352:                     my (undef, $partId, $responseId) = split(/_/,$_);
  353:                     if($beenHere{'p:'.$partId} ==  0) {
  354:                         $beenHere{'p:'.$partId}++;
  355:                         if(!defined($cache->{$currentSequence.':'.
  356:                                             $currentResourceID.':parts'})) {
  357:                             $cache->{$currentSequence.':'.$currentResourceID.
  358:                                      ':parts'}=$partId;
  359:                         } else {
  360:                             $cache->{$currentSequence.':'.$currentResourceID.
  361:                                      ':parts'}.=':'.$partId;
  362:                         }
  363:                     }
  364:                     if($beenHere{'r:'.$partId.':'.$responseId} == 0) {
  365:                         $beenHere{'r:'.$partId.':'.$responseId}++;
  366:                         if(!defined($cache->{$currentSequence.':'.
  367:                                              $currentResourceID.':'.$partId.
  368:                                              ':responseIDs'})) {
  369:                             $cache->{$currentSequence.':'.$currentResourceID.
  370:                                      ':'.$partId.':responseIDs'}=$responseId;
  371:                         } else {
  372:                             $cache->{$currentSequence.':'.$currentResourceID.
  373:                                      ':'.$partId.':responseIDs'}.=':'.
  374:                                                                   $responseId;
  375:                         }
  376:                     }
  377:                     if(/^optionresponse/ && 
  378:                        $beenHere{'o:'.$partId.':'.$currentResourceID} == 0) {
  379:                         $beenHere{'o:'.$partId.$currentResourceID}++;
  380:                         if(defined($cache->{'OptionResponses'})) {
  381:                             $cache->{'OptionResponses'}.= ':::'.
  382:                                 $currentResourceID.':'.
  383:                                 $partId.':'.$responseId;
  384:                         } else {
  385:                             $cache->{'OptionResponses'}= $currentResourceID.
  386:                                 ':'.$partId.':'.$responseId;
  387:                         }
  388:                     }
  389:                 }
  390:             }
  391:         }
  392: 
  393: 	# if resource == finish resource, then it is the end of a sequence/page
  394: 	if($currentResourceID eq $lastResourceID) {
  395: 	    # pop off last resource of sequence
  396: 	    $currentResourceID=pop(@currentResource);
  397: 	    $lastResourceID=pop(@finishResource);
  398: 
  399: 	    if(defined($cache->{$currentSequence.':problems'})) {
  400: 		# Capture sequence information here
  401: 		$cache->{$currentSequence.':title'}=
  402: 		    $hash{'title_'.$currentResourceID};
  403:                 $cache->{$currentSequence.':source'}=
  404:                     $hash{'src_'.$currentResourceID};
  405: 
  406:                 my $totalProblems=0;
  407:                 foreach my $currentProblem (split(/\:/,
  408:                                                $cache->{$currentSequence.
  409:                                                ':problems'})) {
  410:                     foreach (split(/\:/,$cache->{$currentSequence.':'.
  411:                                                    $currentProblem.
  412:                                                    ':parts'})) {
  413:                         $totalProblems++;
  414:                     }
  415:                 }
  416: 		my @titleLength=split(//,$cache->{$currentSequence.
  417:                                                     ':title'});
  418:                 # $extra is 3 for problems correct and 3 for space
  419:                 # between problems correct and problem output
  420:                 my $extra = 6;
  421: 		if(($totalProblems + $extra) > (scalar @titleLength)) {
  422: 		    $cache->{$currentSequence.':columnWidth'}=
  423:                         $totalProblems + $extra;
  424: 		} else {
  425: 		    $cache->{$currentSequence.':columnWidth'}=
  426:                         (scalar @titleLength);
  427: 		}
  428: 	    } else {
  429:                 # Remove sequence from list, if it contains no problems to
  430:                 # display.
  431:                 $cache->{'orderedSequences'}=~s/$currentSequence//;
  432:                 $cache->{'orderedSequences'}=~s/::/:/g;
  433:                 $cache->{'orderedSequences'}=~s/^:|:$//g;
  434:             }
  435: 
  436: 	    $currentSequence=pop(@sequences);
  437: 	    if($currentSequence eq $topLevelSequenceNumber) {
  438: 		last;
  439: 	    }
  440: 	}
  441: 
  442: 	# MOVE!!!
  443: 	# move to next resource
  444: 	unless(defined($hash{'to_'.$currentResourceID})) {
  445: 	    # big problem, need to handle.  Next is probably wrong
  446: 	    last;
  447: 	}
  448: 	my @nextResources=();
  449: 	foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
  450: 	    push(@nextResources, $hash{'goesto_'.$_});
  451: 	}
  452: 	push(@currentResource, @nextResources);
  453: 	# Set the next resource to be processed
  454: 	$currentResourceID=pop(@currentResource);
  455:     }
  456: 
  457:     unless (untie(%hash)) {
  458:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  459:                                  "Could not untie coursemap $fn (browse)".
  460:                                  ".</font>"); 
  461:     }
  462: 
  463:     return 'OK';
  464: }
  465: 
  466: =pod
  467: 
  468: =item &ProcessClasslist()
  469: 
  470: Taking the class list dumped from &DownloadClasslist(), all the 
  471: students and their non-class information is processed using the 
  472: &ProcessStudentInformation() function.  A date stamp is also recorded for
  473: when the data was processed.
  474: 
  475: Takes data downloaded for a student and breaks it up into managable pieces and 
  476: stored in cache data.  The username, domain, class related date, PID, 
  477: full name, and section are all processed here.
  478: 
  479: 
  480: =over 4
  481: 
  482: Input: $cache, $classlist, $courseID, $ChartDB, $c
  483: 
  484: $cache: A hash pointer to store the data
  485: 
  486: $classlist:  The hash of data collected about a student from 
  487: &DownloadClasslist().  The hash contains a list of students, a pointer 
  488: to a hash of student information for each student, and each student's section 
  489: number.
  490: 
  491: $courseID:  The course ID
  492: 
  493: $ChartDB:  The name of the cache database file.
  494: 
  495: $c:  The connection class used to determine if an abort has been sent to the 
  496: browser
  497: 
  498: Output: @names
  499: 
  500: @names:  An array of students whose information has been processed, and are to 
  501: be considered in an arbitrary order.
  502: 
  503: =back
  504: 
  505: =cut
  506: 
  507: sub ProcessClasslist {
  508:     my ($cache,$classlist,$courseID,$c)=@_;
  509:     my @names=();
  510: 
  511:     $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
  512:     if($classlist->{'UpToDate'} eq 'true') {
  513:         return split(/:::/,$cache->{'NamesOfStudents'});;
  514:     }
  515: 
  516:     foreach my $name (keys(%$classlist)) {
  517:         if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
  518:            $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
  519:             next;
  520:         }
  521:         if($c->aborted()) {
  522:             return ();
  523:         }
  524:         push(@names,$name);
  525:         my $studentInformation = $classlist->{$name.':studentInformation'},
  526:         my $sectionData = $classlist->{$name.':sections'},
  527:         my $date = $classlist->{$name},
  528:         my ($studentName,$studentDomain) = split(/\:/,$name);
  529: 
  530:         $cache->{$name.':username'}=$studentName;
  531:         $cache->{$name.':domain'}=$studentDomain;
  532:         # Initialize timestamp for student
  533:         if(!defined($cache->{$name.':lastDownloadTime'})) {
  534:             $cache->{$name.':lastDownloadTime'}='Not downloaded';
  535:             $cache->{$name.':updateTime'}=' Not updated';
  536:         }
  537: 
  538:         my ($checkForError)=keys(%$studentInformation);
  539:         if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
  540:             $cache->{$name.':error'}=
  541:                 'Could not download student environment data.';
  542:             $cache->{$name.':fullname'}='';
  543:             $cache->{$name.':id'}='';
  544:         } else {
  545:             $cache->{$name.':fullname'}=&ProcessFullName(
  546:                                           $studentInformation->{'lastname'},
  547:                                           $studentInformation->{'generation'},
  548:                                           $studentInformation->{'firstname'},
  549:                                           $studentInformation->{'middlename'});
  550:             $cache->{$name.':id'}=$studentInformation->{'id'};
  551:         }
  552: 
  553:         my ($end, $start)=split(':',$date);
  554:         $courseID=~s/\_/\//g;
  555:         $courseID=~s/^(\w)/\/$1/;
  556: 
  557:         my $sec='';
  558:         foreach my $key (keys (%$sectionData)) {
  559:             my $value = $sectionData->{$key};
  560:             if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
  561:                 my $tempsection=$1;
  562:                 if($key eq $courseID.'_st') {
  563:                     $tempsection='';
  564:                 }
  565:                 my ($dummy,$roleend,$rolestart)=split(/\_/,$value);
  566:                 if($roleend eq $end && $rolestart eq $start) {
  567:                     $sec = $tempsection;
  568:                     last;
  569:                 }
  570:             }
  571:         }
  572: 
  573:         my $status='Expired';
  574:         if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
  575:             $status='Active';
  576:         }
  577:         $cache->{$name.':Status'}=$status;
  578:         $cache->{$name.':section'}=$sec;
  579: 
  580:         if($sec eq '' || !defined($sec) || $sec eq ' ') {
  581:             $sec = 'none';
  582:         }
  583:         if(defined($cache->{'sectionList'})) {
  584:             if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
  585:                 $cache->{'sectionList'} .= ':'.$sec;
  586:             }
  587:         } else {
  588:             $cache->{'sectionList'} = $sec;
  589:         }
  590:     }
  591: 
  592:     $cache->{'ClasslistTimestamp'}=time;
  593:     $cache->{'NamesOfStudents'}=join(':::',@names);
  594: 
  595:     return @names;
  596: }
  597: 
  598: =pod
  599: 
  600: =item &ProcessStudentData()
  601: 
  602: Takes the course data downloaded for a student in 
  603: &DownloadCourseInformation() and breaks it up into key value pairs
  604: to be stored in the cached data.  The keys are comprised of the 
  605: $username:$domain:$keyFromCourseDatabase.  The student username:domain is
  606: stored away signifying that the student's information has been downloaded and 
  607: can be reused from cached data.
  608: 
  609: =over 4
  610: 
  611: Input: $cache, $courseData, $name
  612: 
  613: $cache: A hash pointer to store data
  614: 
  615: $courseData:  A hash pointer that points to the course data downloaded for a 
  616: student.
  617: 
  618: $name:  username:domain
  619: 
  620: Output: None
  621: 
  622: *NOTE:  There is no output, but an error message is stored away in the cache 
  623: data.  This is checked in &FormatStudentData().  The key username:domain:error 
  624: will only exist if an error occured.  The error is an error from 
  625: &DownloadCourseInformation().
  626: 
  627: =back
  628: 
  629: =cut
  630: 
  631: sub ProcessStudentData {
  632:     my ($cache,$courseData,$name)=@_;
  633: 
  634:     if($courseData->{'UpToDate'} eq 'true') {
  635:         $cache->{$name.':lastDownloadTime'}=$courseData->{'lastDownloadTime'};
  636:         if($courseData->{'lastDownloadTime'} eq 'Not downloaded') {
  637:             $cache->{$name.':updateTime'} = ' Not updated';
  638:         } else {
  639:             $cache->{$name.':updateTime'}=
  640:                 localtime($courseData->{'lastDownloadTime'});
  641:         }
  642:         return;
  643:     }
  644: 
  645:     my @courseKeys = keys(%$courseData);
  646: 
  647:     foreach (@courseKeys) {
  648:         if(/^(con_lost|error|no_such_host)/i) {
  649:             $cache->{$name.':error'}='Could not download course data.';
  650:             return;
  651:         }
  652:     }
  653: 
  654:     $cache->{$name.':lastDownloadTime'}=$courseData->{'lastDownloadTime'};
  655:     if($courseData->{'lastDownloadTime'} eq 'Not downloaded') {
  656:         $cache->{$name.':updateTime'} = ' Not updated';
  657:     } else {
  658:         $cache->{$name.':updateTime'}=
  659:             localtime($courseData->{'lastDownloadTime'});
  660:     }
  661:     foreach (@courseKeys) {
  662:         $cache->{$name.':'.$_}=$courseData->{$_};
  663:     }
  664: 
  665:     return;
  666: }
  667: 
  668: sub LoadDiscussion {
  669:     my ( $courseID)=@_;
  670:     my %Discuss=();
  671:     my %contrib=&Apache::lonnet::dump(
  672:                 $courseID,
  673:                 $ENV{'course.'.$courseID.'.domain'},
  674:                 $ENV{'course.'.$courseID.'.num'});
  675: 				 
  676:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
  677: 
  678:     foreach my $temp(keys %contrib) {
  679: 	if ($temp=~/^version/) {
  680: 	    my $ver=$contrib{$temp};
  681: 	    my ($dummy,$prb)=split(':',$temp);
  682: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
  683: 		my $name=$contrib{"$idx:$prb:sendername"};
  684: 		$Discuss{"$name:$prb"}=$idx;	
  685: 	    }
  686: 	}
  687:     }       
  688: 
  689:     return \%Discuss;
  690: }
  691: 
  692: # ----- END PROCESSING FUNCTIONS ---------------------------------------
  693: 
  694: =pod
  695: 
  696: =head1 HELPER FUNCTIONS
  697: 
  698: These are just a couple of functions do various odd and end 
  699: jobs.
  700: 
  701: =cut
  702: 
  703: # ----- HELPER FUNCTIONS -----------------------------------------------
  704: 
  705: =pod
  706: 
  707: =item &ProcessFullName()
  708: 
  709: Takes lastname, generation, firstname, and middlename (or some partial
  710: set of this data) and returns the full name version as a string.  Format
  711: is Lastname generation, firstname middlename or a subset of this.
  712: 
  713: =cut
  714: 
  715: sub ProcessFullName {
  716:     my ($lastname, $generation, $firstname, $middlename)=@_;
  717:     my $Str = '';
  718: 
  719:     if($lastname ne '') {
  720: 	$Str .= $lastname.' ';
  721: 	if($generation ne '') {
  722: 	    $Str .= $generation;
  723: 	} else {
  724: 	    chop($Str);
  725: 	}
  726: 	$Str .= ', ';
  727: 	if($firstname ne '') {
  728: 	    $Str .= $firstname.' ';
  729: 	}
  730: 	if($middlename ne '') {
  731: 	    $Str .= $middlename;
  732: 	} else {
  733: 	    chop($Str);
  734: 	    if($firstname eq '') {
  735: 		chop($Str);
  736: 	    }
  737: 	}
  738:     } else {
  739: 	if($firstname ne '') {
  740: 	    $Str .= $firstname.' ';
  741: 	}
  742: 	if($middlename ne '') {
  743: 	    $Str .= $middlename.' ';
  744: 	}
  745: 	if($generation ne '') {
  746: 	    $Str .= $generation;
  747: 	} else {
  748: 	    chop($Str);
  749: 	}
  750:     }
  751: 
  752:     return $Str;
  753: }
  754: 
  755: =pod
  756: 
  757: =item &TestCacheData()
  758: 
  759: Determine if the cache database can be accessed with a tie.  It waits up to
  760: ten seconds before returning failure.  This function exists to help with
  761: the problems with stopping the data download.  When an abort occurs and the
  762: user quickly presses a form button and httpd child is created.  This
  763: child needs to wait for the other to finish (hopefully within ten seconds).
  764: 
  765: =over 4
  766: 
  767: Input: $ChartDB
  768: 
  769: $ChartDB: The name of the cache database to be opened
  770: 
  771: Output: -1, 0, 1
  772: 
  773: -1: Couldn't tie database
  774:  0: Use cached data
  775:  1: New cache database created, use that.
  776: 
  777: =back
  778: 
  779: =cut
  780: 
  781: sub TestCacheData {
  782:     my ($ChartDB,$isRecalculate,$totalDelay)=@_;
  783:     my $isCached=-1;
  784:     my %testData;
  785:     my $tieTries=0;
  786: 
  787:     if(!defined($totalDelay)) {
  788:         $totalDelay = 10;
  789:     }
  790: 
  791:     if ((-e "$ChartDB") && (!$isRecalculate)) {
  792: 	$isCached = 1;
  793:     } else {
  794: 	$isCached = 0;
  795:     }
  796: 
  797:     while($tieTries < $totalDelay) {
  798:         my $result=0;
  799:         if($isCached) {
  800:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
  801:         } else {
  802:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
  803:         }
  804:         if($result) {
  805:             last;
  806:         }
  807:         $tieTries++;
  808:         sleep 1;
  809:     }
  810:     if($tieTries >= $totalDelay) {
  811:         return -1;
  812:     }
  813: 
  814:     untie(%testData);
  815: 
  816:     return $isCached;
  817: }
  818: 
  819: sub GetFileTimestamp {
  820:     my ($studentDomain,$studentName,$filename,$root)=@_;
  821:     $studentDomain=~s/\W//g;
  822:     $studentName=~s/\W//g;
  823:     my $subdir=$studentName.'__';
  824:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
  825:     my $proname="$studentDomain/$subdir/$studentName";
  826:     $proname .= '/'.$filename;
  827:     my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
  828:                                        $root);
  829:     my $fileStat = $dir[0];
  830:     my @stats = split('&', $fileStat);
  831:     if(@stats) {
  832:         return $stats[9];
  833:     } else {
  834:         return -1;
  835:     }
  836: }
  837: 
  838: # ----- END HELPER FUNCTIONS --------------------------------------------
  839: 
  840: 1;
  841: __END__

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