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

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: # (Publication Handler
                      3: #
1.8     ! stredwic    4: # $Id: loncoursedata.pm,v 1.7 2002/07/26 16:22:09 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.8     ! stredwic  340:             $cache->{$currentResourceID.':source'} = 
        !           341:                 $hash{'src_'.$currentResourceID};
1.2       stredwic  342: 	    my $meta=$hash{'src_'.$currentResourceID};
                    343: #            $cache->{$currentResourceID.':title'}=
                    344: #                &Apache::lonnet::metdata($meta,'title');
                    345:             $cache->{$currentResourceID.':title'}=
                    346:                 $hash{'title_'.$currentResourceID};
                    347: 
1.1       stredwic  348:             # Get Parts for problem
1.8     ! stredwic  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:                         }
1.1       stredwic  376:                     }
1.8     ! stredwic  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;
1.2       stredwic  387:                         }
                    388:                     }
                    389:                 }
1.8     ! stredwic  390:             }
        !           391:         }
1.1       stredwic  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};
1.2       stredwic  403:                 $cache->{$currentSequence.':source'}=
                    404:                     $hash{'src_'.$currentResourceID};
1.1       stredwic  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: 
1.3       stredwic  468: =item &ProcessClasslist()
1.1       stredwic  469: 
1.3       stredwic  470: Taking the class list dumped from &DownloadClasslist(), all the 
1.1       stredwic  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: 
1.3       stredwic  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: 
1.1       stredwic  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 
1.3       stredwic  487: &DownloadClasslist().  The hash contains a list of students, a pointer 
1.1       stredwic  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: 
1.3       stredwic  507: sub ProcessClasslist {
                    508:     my ($cache,$classlist,$courseID,$c)=@_;
1.1       stredwic  509:     my @names=();
                    510: 
1.3       stredwic  511:     $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
                    512:     if($classlist->{'UpToDate'} eq 'true') {
                    513:         return split(/:::/,$cache->{'NamesOfStudents'});;
                    514:     }
                    515: 
1.1       stredwic  516:     foreach my $name (keys(%$classlist)) {
                    517:         if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
1.3       stredwic  518:            $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
1.1       stredwic  519:             next;
                    520:         }
                    521:         if($c->aborted()) {
1.3       stredwic  522:             return ();
1.1       stredwic  523:         }
                    524:         push(@names,$name);
1.3       stredwic  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:         if(!defined($cache->{$name.':lastDownloadTime'})) {
                    533:             $cache->{$name.':lastDownloadTime'}='Not downloaded';
1.6       stredwic  534:             $cache->{$name.':updateTime'}=' Not updated';
1.3       stredwic  535:         }
                    536: 
                    537:         my ($checkForError)=keys(%$studentInformation);
                    538:         if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
                    539:             $cache->{$name.':error'}=
                    540:                 'Could not download student environment data.';
                    541:             $cache->{$name.':fullname'}='';
                    542:             $cache->{$name.':id'}='';
                    543:         } else {
                    544:             $cache->{$name.':fullname'}=&ProcessFullName(
                    545:                                           $studentInformation->{'lastname'},
                    546:                                           $studentInformation->{'generation'},
                    547:                                           $studentInformation->{'firstname'},
                    548:                                           $studentInformation->{'middlename'});
                    549:             $cache->{$name.':id'}=$studentInformation->{'id'};
                    550:         }
                    551: 
                    552:         my ($end, $start)=split(':',$date);
                    553:         $courseID=~s/\_/\//g;
                    554:         $courseID=~s/^(\w)/\/$1/;
                    555: 
                    556:         my $sec='';
                    557:         foreach my $key (keys (%$sectionData)) {
                    558:             my $value = $sectionData->{$key};
                    559:             if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
                    560:                 my $tempsection=$1;
                    561:                 if($key eq $courseID.'_st') {
                    562:                     $tempsection='';
                    563:                 }
                    564:                 my ($dummy,$roleend,$rolestart)=split(/\_/,$value);
                    565:                 if($roleend eq $end && $rolestart eq $start) {
                    566:                     $sec = $tempsection;
                    567:                     last;
                    568:                 }
                    569:             }
                    570:         }
                    571: 
                    572:         my $status='Expired';
                    573:         if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
                    574:             $status='Active';
                    575:         }
                    576:         $cache->{$name.':Status'}=$status;
                    577:         $cache->{$name.':section'}=$sec;
1.7       stredwic  578: 
                    579:         if($sec eq '' || !defined($sec) || $sec eq ' ') {
                    580:             $sec = 'none';
                    581:         }
                    582:         if(defined($cache->{'sectionList'})) {
                    583:             if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
                    584:                 $cache->{'sectionList'} .= ':'.$sec;
                    585:             }
                    586:         } else {
                    587:             $cache->{'sectionList'} = $sec;
                    588:         }
1.1       stredwic  589:     }
                    590: 
1.3       stredwic  591:     $cache->{'ClasslistTimestamp'}=time;
                    592:     $cache->{'NamesOfStudents'}=join(':::',@names);
1.1       stredwic  593: 
                    594:     return @names;
                    595: }
                    596: 
                    597: =pod
                    598: 
                    599: =item &ProcessStudentData()
                    600: 
                    601: Takes the course data downloaded for a student in 
1.4       stredwic  602: &DownloadCourseInformation() and breaks it up into key value pairs
1.1       stredwic  603: to be stored in the cached data.  The keys are comprised of the 
                    604: $username:$domain:$keyFromCourseDatabase.  The student username:domain is
                    605: stored away signifying that the student's information has been downloaded and 
                    606: can be reused from cached data.
                    607: 
                    608: =over 4
                    609: 
                    610: Input: $cache, $courseData, $name
                    611: 
                    612: $cache: A hash pointer to store data
                    613: 
                    614: $courseData:  A hash pointer that points to the course data downloaded for a 
                    615: student.
                    616: 
                    617: $name:  username:domain
                    618: 
                    619: Output: None
                    620: 
                    621: *NOTE:  There is no output, but an error message is stored away in the cache 
                    622: data.  This is checked in &FormatStudentData().  The key username:domain:error 
                    623: will only exist if an error occured.  The error is an error from 
1.4       stredwic  624: &DownloadCourseInformation().
1.1       stredwic  625: 
                    626: =back
                    627: 
                    628: =cut
                    629: 
                    630: sub ProcessStudentData {
                    631:     my ($cache,$courseData,$name)=@_;
                    632: 
1.3       stredwic  633:     if($courseData->{'UpToDate'} eq 'true') {
                    634:         $cache->{$name.':lastDownloadTime'}=$courseData->{'lastDownloadTime'};
1.6       stredwic  635:         if($courseData->{'lastDownloadTime'} eq 'Not downloaded') {
                    636:             $cache->{$name.':updateTime'} = ' Not updated';
                    637:         } else {
                    638:             $cache->{$name.':updateTime'}=
                    639:                 localtime($courseData->{'lastDownloadTime'});
                    640:         }
1.3       stredwic  641:         return;
                    642:     }
                    643: 
                    644:     my @courseKeys = keys(%$courseData);
                    645: 
                    646:     foreach (@courseKeys) {
                    647:         if(/^(con_lost|error|no_such_host)/i) {
                    648:             $cache->{$name.':error'}='Could not download course data.';
                    649:             return;
1.1       stredwic  650:         }
1.3       stredwic  651:     }
                    652: 
                    653:     $cache->{$name.':lastDownloadTime'}=$courseData->{'lastDownloadTime'};
1.6       stredwic  654:     if($courseData->{'lastDownloadTime'} eq 'Not downloaded') {
                    655:         $cache->{$name.':updateTime'} = ' Not updated';
                    656:     } else {
                    657:         $cache->{$name.':updateTime'}=
                    658:             localtime($courseData->{'lastDownloadTime'});
                    659:     }
1.3       stredwic  660:     foreach (@courseKeys) {
                    661:         $cache->{$name.':'.$_}=$courseData->{$_};
1.1       stredwic  662:     }
                    663: 
                    664:     return;
1.4       stredwic  665: }
                    666: 
                    667: sub LoadDiscussion {
1.5       minaeibi  668:     my ( $courseID)=@_;
                    669:     my %Discuss=();
                    670:     my %contrib=&Apache::lonnet::dump(
                    671:                 $courseID,
                    672:                 $ENV{'course.'.$courseID.'.domain'},
                    673:                 $ENV{'course.'.$courseID.'.num'});
                    674: 				 
                    675:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
                    676: 
1.4       stredwic  677:     foreach my $temp(keys %contrib) {
                    678: 	if ($temp=~/^version/) {
                    679: 	    my $ver=$contrib{$temp};
                    680: 	    my ($dummy,$prb)=split(':',$temp);
                    681: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
                    682: 		my $name=$contrib{"$idx:$prb:sendername"};
1.5       minaeibi  683: 		$Discuss{"$name:$prb"}=$idx;	
1.4       stredwic  684: 	    }
                    685: 	}
                    686:     }       
1.5       minaeibi  687: 
                    688:     return \%Discuss;
1.1       stredwic  689: }
                    690: 
                    691: # ----- END PROCESSING FUNCTIONS ---------------------------------------
                    692: 
                    693: =pod
                    694: 
                    695: =head1 HELPER FUNCTIONS
                    696: 
                    697: These are just a couple of functions do various odd and end 
                    698: jobs.
                    699: 
                    700: =cut
                    701: 
                    702: # ----- HELPER FUNCTIONS -----------------------------------------------
                    703: 
                    704: =pod
                    705: 
                    706: =item &ProcessFullName()
                    707: 
                    708: Takes lastname, generation, firstname, and middlename (or some partial
                    709: set of this data) and returns the full name version as a string.  Format
                    710: is Lastname generation, firstname middlename or a subset of this.
                    711: 
                    712: =cut
                    713: 
                    714: sub ProcessFullName {
                    715:     my ($lastname, $generation, $firstname, $middlename)=@_;
                    716:     my $Str = '';
                    717: 
                    718:     if($lastname ne '') {
                    719: 	$Str .= $lastname.' ';
                    720: 	if($generation ne '') {
                    721: 	    $Str .= $generation;
                    722: 	} else {
                    723: 	    chop($Str);
                    724: 	}
                    725: 	$Str .= ', ';
                    726: 	if($firstname ne '') {
                    727: 	    $Str .= $firstname.' ';
                    728: 	}
                    729: 	if($middlename ne '') {
                    730: 	    $Str .= $middlename;
                    731: 	} else {
                    732: 	    chop($Str);
                    733: 	    if($firstname eq '') {
                    734: 		chop($Str);
                    735: 	    }
                    736: 	}
                    737:     } else {
                    738: 	if($firstname ne '') {
                    739: 	    $Str .= $firstname.' ';
                    740: 	}
                    741: 	if($middlename ne '') {
                    742: 	    $Str .= $middlename.' ';
                    743: 	}
                    744: 	if($generation ne '') {
                    745: 	    $Str .= $generation;
                    746: 	} else {
                    747: 	    chop($Str);
                    748: 	}
                    749:     }
                    750: 
                    751:     return $Str;
                    752: }
                    753: 
                    754: =pod
                    755: 
                    756: =item &TestCacheData()
                    757: 
                    758: Determine if the cache database can be accessed with a tie.  It waits up to
                    759: ten seconds before returning failure.  This function exists to help with
                    760: the problems with stopping the data download.  When an abort occurs and the
                    761: user quickly presses a form button and httpd child is created.  This
                    762: child needs to wait for the other to finish (hopefully within ten seconds).
                    763: 
                    764: =over 4
                    765: 
                    766: Input: $ChartDB
                    767: 
                    768: $ChartDB: The name of the cache database to be opened
                    769: 
                    770: Output: -1, 0, 1
                    771: 
                    772: -1: Couldn't tie database
                    773:  0: Use cached data
                    774:  1: New cache database created, use that.
                    775: 
                    776: =back
                    777: 
                    778: =cut
                    779: 
                    780: sub TestCacheData {
                    781:     my ($ChartDB,$isRecalculate,$totalDelay)=@_;
                    782:     my $isCached=-1;
                    783:     my %testData;
                    784:     my $tieTries=0;
                    785: 
                    786:     if(!defined($totalDelay)) {
                    787:         $totalDelay = 10;
                    788:     }
                    789: 
                    790:     if ((-e "$ChartDB") && (!$isRecalculate)) {
                    791: 	$isCached = 1;
                    792:     } else {
                    793: 	$isCached = 0;
                    794:     }
                    795: 
                    796:     while($tieTries < $totalDelay) {
                    797:         my $result=0;
                    798:         if($isCached) {
                    799:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER,0640);
                    800:         } else {
                    801:             $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB,0640);
                    802:         }
                    803:         if($result) {
                    804:             last;
                    805:         }
                    806:         $tieTries++;
                    807:         sleep 1;
                    808:     }
                    809:     if($tieTries >= $totalDelay) {
                    810:         return -1;
                    811:     }
                    812: 
                    813:     untie(%testData);
                    814: 
                    815:     return $isCached;
                    816: }
1.2       stredwic  817: 
1.3       stredwic  818: sub GetFileTimestamp {
                    819:     my ($studentDomain,$studentName,$filename,$root)=@_;
                    820:     $studentDomain=~s/\W//g;
                    821:     $studentName=~s/\W//g;
                    822:     my $subdir=$studentName.'__';
                    823:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                    824:     my $proname="$studentDomain/$subdir/$studentName";
                    825:     $proname .= '/'.$filename;
                    826:     my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
                    827:                                        $root);
                    828:     my $fileStat = $dir[0];
                    829:     my @stats = split('&', $fileStat);
                    830:     if(@stats) {
                    831:         return $stats[9];
                    832:     } else {
                    833:         return -1;
                    834:     }
                    835: }
1.1       stredwic  836: 
                    837: # ----- END HELPER FUNCTIONS --------------------------------------------
                    838: 
                    839: 1;
                    840: __END__

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