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

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

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