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

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: # (Publication Handler
                      3: #
1.7     ! stredwic    4: # $Id: loncoursedata.pm,v 1.6 2002/07/25 21:23:51 stredwic Exp $
1.1       stredwic    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
                     34: loncoursedata
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     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: 
1.3       stredwic   73: =item &DownloadClasslist()
1.1       stredwic   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: 
1.3       stredwic  104: sub DownloadClasslist {
                    105:     my ($courseID, $lastDownloadTime, $c)=@_;
1.1       stredwic  106:     my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
1.3       stredwic  107:     my %classlist;
1.1       stredwic  108: 
1.7     ! stredwic  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:     }
1.3       stredwic  119: 
                    120:     %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
1.1       stredwic  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);
1.3       stredwic  149:         $classlist{$name.':sections'}=\%section;
1.1       stredwic  150:     }
                    151: 
1.3       stredwic  152:     $classlist{'UpToDate'} = 'false';
                    153:     $classlist{'lastDownloadTime'}=time;
                    154: 
1.1       stredwic  155:     return \%classlist;
                    156: }
                    157: 
                    158: =pod
                    159: 
1.4       stredwic  160: =item &DownloadCourseInformation()
1.1       stredwic  161: 
                    162: Dump of all the course information for a single student.  There is no
1.3       stredwic  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.
1.1       stredwic  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: 
1.4       stredwic  184: sub DownloadCourseInformation {
                    185:     my ($namedata,$courseID,$lastDownloadTime)=@_;
1.3       stredwic  186:     my %courseData;
1.4       stredwic  187:     my ($name,$domain) = split(/\:/,$namedata);
1.1       stredwic  188: 
1.7     ! stredwic  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:     }
1.3       stredwic  198: 
1.4       stredwic  199:     # Download course data
                    200:     %courseData=&Apache::lonnet::dump($courseID, $domain, $name);
1.3       stredwic  201:     $courseData{'UpToDate'} = 'false';
                    202:     $courseData{'lastDownloadTime'}=time;
1.1       stredwic  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)=@_;
                    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: 
1.2       stredwic  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: 
1.1       stredwic  346:             # Get Parts for problem
                    347: 	    foreach (split(/\,/,&Apache::lonnet::metadata($meta,'keys'))) {
                    348: 		if($_=~/^stores\_(\d+)\_tries$/) {
                    349: 		    my $Part=&Apache::lonnet::metadata($meta,$_.'.part');
                    350:                     if(!defined($cache->{$currentSequence.':'.
                    351:                                           $currentResourceID.':parts'})) {
                    352:                         $cache->{$currentSequence.':'.$currentResourceID.
                    353:                                    ':parts'}=$Part;
                    354:                     } else {
                    355:                         $cache->{$currentSequence.':'.$currentResourceID.
                    356:                                    ':parts'}.=':'.$Part;
                    357:                     }
1.2       stredwic  358:                     foreach (split(/\,/,
                    359:                              &Apache::lonnet::metadata($meta,'packages'))) {
                    360:                         if($_=~/^optionresponse\_($Part)\_(\w+)$/) {
                    361:                             if(defined($cache->{'OptionResponses'})) {
                    362:                                 $cache->{'OptionResponses'}.= ':::'.
                    363:                                     $hash{'src_'.$currentResourceID}.'::'.
                    364:                                     $hash{'title_'.$currentResourceID}.'::'.
                    365:                                     $Part.'::'.$Problem;
                    366:                             } else {
                    367:                                 $cache->{'OptionResponses'}=
                    368:                                     $hash{'src_'.$currentResourceID}.'::'.
                    369:                                     $hash{'title_'.$currentResourceID}.'::'.
                    370:                                     $Part.'::'.$Problem;
                    371:                             }
                    372:                         }
                    373:                     }
                    374:                 }
1.1       stredwic  375: 	    }
                    376: 	}
                    377: 
                    378: 	# if resource == finish resource, then it is the end of a sequence/page
                    379: 	if($currentResourceID eq $lastResourceID) {
                    380: 	    # pop off last resource of sequence
                    381: 	    $currentResourceID=pop(@currentResource);
                    382: 	    $lastResourceID=pop(@finishResource);
                    383: 
                    384: 	    if(defined($cache->{$currentSequence.':problems'})) {
                    385: 		# Capture sequence information here
                    386: 		$cache->{$currentSequence.':title'}=
                    387: 		    $hash{'title_'.$currentResourceID};
1.2       stredwic  388:                 $cache->{$currentSequence.':source'}=
                    389:                     $hash{'src_'.$currentResourceID};
1.1       stredwic  390: 
                    391:                 my $totalProblems=0;
                    392:                 foreach my $currentProblem (split(/\:/,
                    393:                                                $cache->{$currentSequence.
                    394:                                                ':problems'})) {
                    395:                     foreach (split(/\:/,$cache->{$currentSequence.':'.
                    396:                                                    $currentProblem.
                    397:                                                    ':parts'})) {
                    398:                         $totalProblems++;
                    399:                     }
                    400:                 }
                    401: 		my @titleLength=split(//,$cache->{$currentSequence.
                    402:                                                     ':title'});
                    403:                 # $extra is 3 for problems correct and 3 for space
                    404:                 # between problems correct and problem output
                    405:                 my $extra = 6;
                    406: 		if(($totalProblems + $extra) > (scalar @titleLength)) {
                    407: 		    $cache->{$currentSequence.':columnWidth'}=
                    408:                         $totalProblems + $extra;
                    409: 		} else {
                    410: 		    $cache->{$currentSequence.':columnWidth'}=
                    411:                         (scalar @titleLength);
                    412: 		}
                    413: 	    } else {
                    414:                 # Remove sequence from list, if it contains no problems to
                    415:                 # display.
                    416:                 $cache->{'orderedSequences'}=~s/$currentSequence//;
                    417:                 $cache->{'orderedSequences'}=~s/::/:/g;
                    418:                 $cache->{'orderedSequences'}=~s/^:|:$//g;
                    419:             }
                    420: 
                    421: 	    $currentSequence=pop(@sequences);
                    422: 	    if($currentSequence eq $topLevelSequenceNumber) {
                    423: 		last;
                    424: 	    }
                    425: 	}
                    426: 
                    427: 	# MOVE!!!
                    428: 	# move to next resource
                    429: 	unless(defined($hash{'to_'.$currentResourceID})) {
                    430: 	    # big problem, need to handle.  Next is probably wrong
                    431: 	    last;
                    432: 	}
                    433: 	my @nextResources=();
                    434: 	foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
                    435: 	    push(@nextResources, $hash{'goesto_'.$_});
                    436: 	}
                    437: 	push(@currentResource, @nextResources);
                    438: 	# Set the next resource to be processed
                    439: 	$currentResourceID=pop(@currentResource);
                    440:     }
                    441: 
                    442:     unless (untie(%hash)) {
                    443:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    444:                                  "Could not untie coursemap $fn (browse)".
                    445:                                  ".</font>"); 
                    446:     }
                    447: 
                    448:     return 'OK';
                    449: }
                    450: 
                    451: =pod
                    452: 
1.3       stredwic  453: =item &ProcessClasslist()
1.1       stredwic  454: 
1.3       stredwic  455: Taking the class list dumped from &DownloadClasslist(), all the 
1.1       stredwic  456: students and their non-class information is processed using the 
                    457: &ProcessStudentInformation() function.  A date stamp is also recorded for
                    458: when the data was processed.
                    459: 
1.3       stredwic  460: Takes data downloaded for a student and breaks it up into managable pieces and 
                    461: stored in cache data.  The username, domain, class related date, PID, 
                    462: full name, and section are all processed here.
                    463: 
                    464: 
1.1       stredwic  465: =over 4
                    466: 
                    467: Input: $cache, $classlist, $courseID, $ChartDB, $c
                    468: 
                    469: $cache: A hash pointer to store the data
                    470: 
                    471: $classlist:  The hash of data collected about a student from 
1.3       stredwic  472: &DownloadClasslist().  The hash contains a list of students, a pointer 
1.1       stredwic  473: to a hash of student information for each student, and each student's section 
                    474: number.
                    475: 
                    476: $courseID:  The course ID
                    477: 
                    478: $ChartDB:  The name of the cache database file.
                    479: 
                    480: $c:  The connection class used to determine if an abort has been sent to the 
                    481: browser
                    482: 
                    483: Output: @names
                    484: 
                    485: @names:  An array of students whose information has been processed, and are to 
                    486: be considered in an arbitrary order.
                    487: 
                    488: =back
                    489: 
                    490: =cut
                    491: 
1.3       stredwic  492: sub ProcessClasslist {
                    493:     my ($cache,$classlist,$courseID,$c)=@_;
1.1       stredwic  494:     my @names=();
                    495: 
1.3       stredwic  496:     $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
                    497:     if($classlist->{'UpToDate'} eq 'true') {
                    498:         return split(/:::/,$cache->{'NamesOfStudents'});;
                    499:     }
                    500: 
1.1       stredwic  501:     foreach my $name (keys(%$classlist)) {
                    502:         if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
1.3       stredwic  503:            $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
1.1       stredwic  504:             next;
                    505:         }
                    506:         if($c->aborted()) {
1.3       stredwic  507:             return ();
1.1       stredwic  508:         }
                    509:         push(@names,$name);
1.3       stredwic  510:         my $studentInformation = $classlist->{$name.':studentInformation'},
                    511:         my $sectionData = $classlist->{$name.':sections'},
                    512:         my $date = $classlist->{$name},
                    513:         my ($studentName,$studentDomain) = split(/\:/,$name);
                    514: 
                    515:         $cache->{$name.':username'}=$studentName;
                    516:         $cache->{$name.':domain'}=$studentDomain;
                    517:         if(!defined($cache->{$name.':lastDownloadTime'})) {
                    518:             $cache->{$name.':lastDownloadTime'}='Not downloaded';
1.6       stredwic  519:             $cache->{$name.':updateTime'}=' Not updated';
1.3       stredwic  520:         }
                    521: 
                    522:         my ($checkForError)=keys(%$studentInformation);
                    523:         if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
                    524:             $cache->{$name.':error'}=
                    525:                 'Could not download student environment data.';
                    526:             $cache->{$name.':fullname'}='';
                    527:             $cache->{$name.':id'}='';
                    528:         } else {
                    529:             $cache->{$name.':fullname'}=&ProcessFullName(
                    530:                                           $studentInformation->{'lastname'},
                    531:                                           $studentInformation->{'generation'},
                    532:                                           $studentInformation->{'firstname'},
                    533:                                           $studentInformation->{'middlename'});
                    534:             $cache->{$name.':id'}=$studentInformation->{'id'};
                    535:         }
                    536: 
                    537:         my ($end, $start)=split(':',$date);
                    538:         $courseID=~s/\_/\//g;
                    539:         $courseID=~s/^(\w)/\/$1/;
                    540: 
                    541:         my $sec='';
                    542:         foreach my $key (keys (%$sectionData)) {
                    543:             my $value = $sectionData->{$key};
                    544:             if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
                    545:                 my $tempsection=$1;
                    546:                 if($key eq $courseID.'_st') {
                    547:                     $tempsection='';
                    548:                 }
                    549:                 my ($dummy,$roleend,$rolestart)=split(/\_/,$value);
                    550:                 if($roleend eq $end && $rolestart eq $start) {
                    551:                     $sec = $tempsection;
                    552:                     last;
                    553:                 }
                    554:             }
                    555:         }
                    556: 
                    557:         my $status='Expired';
                    558:         if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
                    559:             $status='Active';
                    560:         }
                    561:         $cache->{$name.':Status'}=$status;
                    562:         $cache->{$name.':section'}=$sec;
1.7     ! stredwic  563: 
        !           564:         if($sec eq '' || !defined($sec) || $sec eq ' ') {
        !           565:             $sec = 'none';
        !           566:         }
        !           567:         if(defined($cache->{'sectionList'})) {
        !           568:             if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
        !           569:                 $cache->{'sectionList'} .= ':'.$sec;
        !           570:             }
        !           571:         } else {
        !           572:             $cache->{'sectionList'} = $sec;
        !           573:         }
1.1       stredwic  574:     }
                    575: 
1.3       stredwic  576:     $cache->{'ClasslistTimestamp'}=time;
                    577:     $cache->{'NamesOfStudents'}=join(':::',@names);
1.1       stredwic  578: 
                    579:     return @names;
                    580: }
                    581: 
                    582: =pod
                    583: 
                    584: =item &ProcessStudentData()
                    585: 
                    586: Takes the course data downloaded for a student in 
1.4       stredwic  587: &DownloadCourseInformation() and breaks it up into key value pairs
1.1       stredwic  588: to be stored in the cached data.  The keys are comprised of the 
                    589: $username:$domain:$keyFromCourseDatabase.  The student username:domain is
                    590: stored away signifying that the student's information has been downloaded and 
                    591: can be reused from cached data.
                    592: 
                    593: =over 4
                    594: 
                    595: Input: $cache, $courseData, $name
                    596: 
                    597: $cache: A hash pointer to store data
                    598: 
                    599: $courseData:  A hash pointer that points to the course data downloaded for a 
                    600: student.
                    601: 
                    602: $name:  username:domain
                    603: 
                    604: Output: None
                    605: 
                    606: *NOTE:  There is no output, but an error message is stored away in the cache 
                    607: data.  This is checked in &FormatStudentData().  The key username:domain:error 
                    608: will only exist if an error occured.  The error is an error from 
1.4       stredwic  609: &DownloadCourseInformation().
1.1       stredwic  610: 
                    611: =back
                    612: 
                    613: =cut
                    614: 
                    615: sub ProcessStudentData {
                    616:     my ($cache,$courseData,$name)=@_;
                    617: 
1.3       stredwic  618:     if($courseData->{'UpToDate'} eq 'true') {
                    619:         $cache->{$name.':lastDownloadTime'}=$courseData->{'lastDownloadTime'};
1.6       stredwic  620:         if($courseData->{'lastDownloadTime'} eq 'Not downloaded') {
                    621:             $cache->{$name.':updateTime'} = ' Not updated';
                    622:         } else {
                    623:             $cache->{$name.':updateTime'}=
                    624:                 localtime($courseData->{'lastDownloadTime'});
                    625:         }
1.3       stredwic  626:         return;
                    627:     }
                    628: 
                    629:     my @courseKeys = keys(%$courseData);
                    630: 
                    631:     foreach (@courseKeys) {
                    632:         if(/^(con_lost|error|no_such_host)/i) {
                    633:             $cache->{$name.':error'}='Could not download course data.';
                    634:             return;
1.1       stredwic  635:         }
1.3       stredwic  636:     }
                    637: 
                    638:     $cache->{$name.':lastDownloadTime'}=$courseData->{'lastDownloadTime'};
1.6       stredwic  639:     if($courseData->{'lastDownloadTime'} eq 'Not downloaded') {
                    640:         $cache->{$name.':updateTime'} = ' Not updated';
                    641:     } else {
                    642:         $cache->{$name.':updateTime'}=
                    643:             localtime($courseData->{'lastDownloadTime'});
                    644:     }
1.3       stredwic  645:     foreach (@courseKeys) {
                    646:         $cache->{$name.':'.$_}=$courseData->{$_};
1.1       stredwic  647:     }
                    648: 
                    649:     return;
1.4       stredwic  650: }
                    651: 
                    652: sub LoadDiscussion {
1.5       minaeibi  653:     my ( $courseID)=@_;
                    654:     my %Discuss=();
                    655:     my %contrib=&Apache::lonnet::dump(
                    656:                 $courseID,
                    657:                 $ENV{'course.'.$courseID.'.domain'},
                    658:                 $ENV{'course.'.$courseID.'.num'});
                    659: 				 
                    660:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
                    661: 
1.4       stredwic  662:     foreach my $temp(keys %contrib) {
                    663: 	if ($temp=~/^version/) {
                    664: 	    my $ver=$contrib{$temp};
                    665: 	    my ($dummy,$prb)=split(':',$temp);
                    666: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
                    667: 		my $name=$contrib{"$idx:$prb:sendername"};
1.5       minaeibi  668: 		$Discuss{"$name:$prb"}=$idx;	
1.4       stredwic  669: 	    }
                    670: 	}
                    671:     }       
1.5       minaeibi  672: 
                    673:     return \%Discuss;
1.1       stredwic  674: }
                    675: 
                    676: # ----- END PROCESSING FUNCTIONS ---------------------------------------
                    677: 
                    678: =pod
                    679: 
                    680: =head1 HELPER FUNCTIONS
                    681: 
                    682: These are just a couple of functions do various odd and end 
                    683: jobs.
                    684: 
                    685: =cut
                    686: 
                    687: # ----- HELPER FUNCTIONS -----------------------------------------------
                    688: 
                    689: =pod
                    690: 
                    691: =item &ProcessFullName()
                    692: 
                    693: Takes lastname, generation, firstname, and middlename (or some partial
                    694: set of this data) and returns the full name version as a string.  Format
                    695: is Lastname generation, firstname middlename or a subset of this.
                    696: 
                    697: =cut
                    698: 
                    699: sub ProcessFullName {
                    700:     my ($lastname, $generation, $firstname, $middlename)=@_;
                    701:     my $Str = '';
                    702: 
                    703:     if($lastname ne '') {
                    704: 	$Str .= $lastname.' ';
                    705: 	if($generation ne '') {
                    706: 	    $Str .= $generation;
                    707: 	} else {
                    708: 	    chop($Str);
                    709: 	}
                    710: 	$Str .= ', ';
                    711: 	if($firstname ne '') {
                    712: 	    $Str .= $firstname.' ';
                    713: 	}
                    714: 	if($middlename ne '') {
                    715: 	    $Str .= $middlename;
                    716: 	} else {
                    717: 	    chop($Str);
                    718: 	    if($firstname eq '') {
                    719: 		chop($Str);
                    720: 	    }
                    721: 	}
                    722:     } else {
                    723: 	if($firstname ne '') {
                    724: 	    $Str .= $firstname.' ';
                    725: 	}
                    726: 	if($middlename ne '') {
                    727: 	    $Str .= $middlename.' ';
                    728: 	}
                    729: 	if($generation ne '') {
                    730: 	    $Str .= $generation;
                    731: 	} else {
                    732: 	    chop($Str);
                    733: 	}
                    734:     }
                    735: 
                    736:     return $Str;
                    737: }
                    738: 
                    739: =pod
                    740: 
                    741: =item &TestCacheData()
                    742: 
                    743: Determine if the cache database can be accessed with a tie.  It waits up to
                    744: ten seconds before returning failure.  This function exists to help with
                    745: the problems with stopping the data download.  When an abort occurs and the
                    746: user quickly presses a form button and httpd child is created.  This
                    747: child needs to wait for the other to finish (hopefully within ten seconds).
                    748: 
                    749: =over 4
                    750: 
                    751: Input: $ChartDB
                    752: 
                    753: $ChartDB: The name of the cache database to be opened
                    754: 
                    755: Output: -1, 0, 1
                    756: 
                    757: -1: Couldn't tie database
                    758:  0: Use cached data
                    759:  1: New cache database created, use that.
                    760: 
                    761: =back
                    762: 
                    763: =cut
                    764: 
                    765: sub TestCacheData {
                    766:     my ($ChartDB,$isRecalculate,$totalDelay)=@_;
                    767:     my $isCached=-1;
                    768:     my %testData;
                    769:     my $tieTries=0;
                    770: 
                    771:     if(!defined($totalDelay)) {
                    772:         $totalDelay = 10;
                    773:     }
                    774: 
                    775:     if ((-e "$ChartDB") && (!$isRecalculate)) {
                    776: 	$isCached = 1;
                    777:     } else {
                    778: 	$isCached = 0;
                    779:     }
                    780: 
                    781:     while($tieTries < $totalDelay) {
                    782:         my $result=0;
                    783:         if($isCached) {
                    784:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER,0640);
                    785:         } else {
                    786:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB,0640);
                    787:         }
                    788:         if($result) {
                    789:             last;
                    790:         }
                    791:         $tieTries++;
                    792:         sleep 1;
                    793:     }
                    794:     if($tieTries >= $totalDelay) {
                    795:         return -1;
                    796:     }
                    797: 
                    798:     untie(%testData);
                    799: 
                    800:     return $isCached;
                    801: }
1.2       stredwic  802: 
1.3       stredwic  803: sub GetFileTimestamp {
                    804:     my ($studentDomain,$studentName,$filename,$root)=@_;
                    805:     $studentDomain=~s/\W//g;
                    806:     $studentName=~s/\W//g;
                    807:     my $subdir=$studentName.'__';
                    808:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                    809:     my $proname="$studentDomain/$subdir/$studentName";
                    810:     $proname .= '/'.$filename;
                    811:     my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
                    812:                                        $root);
                    813:     my $fileStat = $dir[0];
                    814:     my @stats = split('&', $fileStat);
                    815:     if(@stats) {
                    816:         return $stats[9];
                    817:     } else {
                    818:         return -1;
                    819:     }
                    820: }
1.1       stredwic  821: 
                    822: # ----- END HELPER FUNCTIONS --------------------------------------------
                    823: 
                    824: 1;
                    825: __END__

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