Annotation of loncom/interface/lonnavmaps.pm, revision 1.51

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # Navigate Maps Handler
1.1       www         3: #
1.51    ! bowersj2    4: # $Id: lonnavmaps.pm,v 1.50 2002/09/09 17:20:14 www Exp $
1.20      albertel    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: #
1.2       www        28: # (Page Handler
1.1       www        29: #
1.2       www        30: # (TeX Content Handler
1.1       www        31: #
1.2       www        32: # 05/29/00,05/30 Gerd Kortemeyer)
                     33: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
                     34: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer)
1.1       www        35: #
1.17      www        36: # 3/1/1,6/1,17/1,29/1,30/1,2/8,9/21,9/24,9/25 Gerd Kortemeyer
1.21      www        37: # YEAR=2002
                     38: # 1/1 Gerd Kortemeyer
                     39: #
1.2       www        40: 
1.1       www        41: package Apache::lonnavmaps;
                     42: 
                     43: use strict;
1.2       www        44: use Apache::Constants qw(:common :http);
                     45: use Apache::lonnet();
1.18      albertel   46: use Apache::loncommon();
1.2       www        47: use HTML::TokeParser;
                     48: use GDBM_File;
                     49: 
                     50: # -------------------------------------------------------------- Module Globals
                     51: my %hash;
                     52: my @rows;
                     53: 
1.10      www        54: #
                     55: # These cache hashes need to be independent of user, resource and course
                     56: # (user and course can/should be in the keys)
                     57: #
                     58: 
                     59: my %courserdatas;
                     60: my %userrdatas;
                     61: 
                     62: #
                     63: # These global hashes are dependent on user, course and resource, 
                     64: # and need to be initialized every time when a sheet is calculated
                     65: #
                     66: my %courseopt;
                     67: my %useropt;
                     68: my %parmhash;
                     69: 
1.47      bowersj2   70: # This parameter keeps track of whether obtaining the user's information
                     71: # failed, which the colorizer in astatus can use
                     72: my $networkFailedFlag = 0;
                     73: 
1.2       www        74: # ------------------------------------------------------------------ Euclid gcd
                     75: 
                     76: sub euclid {
                     77:     my ($e,$f)=@_;
                     78:     my $a; my $b; my $r;
                     79:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
                     80:     while ($r!=0) {
1.51    ! bowersj2   81:         $a=$b; $b=$r;
1.2       www        82:         $r=$a%$b;
                     83:     }
                     84:     return $b;
                     85: }
                     86: 
1.10      www        87: # --------------------------------------------------------------------- Parmval
                     88: 
                     89: # -------------------------------------------- Figure out a cascading parameter
                     90: #
                     91: # For this function to work
                     92: #
                     93: # * parmhash needs to be tied
                     94: # * courseopt and useropt need to be initialized for this user and course
                     95: #
                     96: 
                     97: sub parmval {
                     98:     my ($what,$symb)=@_;
                     99:     my $cid=$ENV{'request.course.id'};
                    100:     my $csec=$ENV{'request.course.sec'};
                    101:     my $uname=$ENV{'user.name'};
                    102:     my $udom=$ENV{'user.domain'};
                    103: 
                    104:     unless ($symb) { return ''; }
                    105:     my $result='';
                    106: 
                    107:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                    108: 
                    109: # ----------------------------------------------------- Cascading lookup scheme
1.24      albertel  110:     my $rwhat=$what;
                    111:     $what=~s/^parameter\_//;
                    112:     $what=~s/\_/\./;
                    113: 
                    114:     my $symbparm=$symb.'.'.$what;
                    115:     my $mapparm=$mapname.'___(all).'.$what;
                    116:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
                    117: 
                    118:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
                    119:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
                    120:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
                    121: 
                    122:     my $courselevel= $usercourseprefix.'.'.$what;
                    123:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
                    124:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
                    125: 
                    126: # ---------------------------------------------------------- first, check user
                    127:     if ($uname) {
1.51    ! bowersj2  128:         if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
        !           129:         if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
        !           130:         if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
1.24      albertel  131:     }
1.10      www       132: 
1.24      albertel  133: # ------------------------------------------------------- second, check course
                    134:     if ($csec) {
1.51    ! bowersj2  135:         if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
1.25      albertel  136:         if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }
1.10      www       137:         if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
1.24      albertel  138:     }
1.10      www       139: 
1.24      albertel  140:     if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
                    141:     if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
                    142:     if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
1.10      www       143: 
1.24      albertel  144: # ----------------------------------------------------- third, check map parms
1.10      www       145: 
1.24      albertel  146:     my $thisparm=$parmhash{$symbparm};
                    147:     if ($thisparm) { return $thisparm; }
1.10      www       148: 
1.24      albertel  149: # ----------------------------------------------------- fourth , check default
1.10      www       150: 
1.25      albertel  151:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
                    152:     if ($default) { return $default}
                    153: 
                    154: # --------------------------------------------------- fifth , cascade up parts
                    155: 
                    156:     my ($space,@qualifier)=split(/\./,$rwhat);
                    157:     my $qualifier=join('.',@qualifier);
                    158:     unless ($space eq '0') {
1.51    ! bowersj2  159:         my ($part,$id)=split(/\_/,$space);
        !           160:         if ($id) {
        !           161:             my $partgeneral=&parmval($part.".$qualifier",$symb);
        !           162:             if ($partgeneral) { return $partgeneral; }
        !           163:         } else {
        !           164:             my $resourcegeneral=&parmval("0.$qualifier",$symb);
        !           165:             if ($resourcegeneral) { return $resourcegeneral; }
        !           166:         }
1.25      albertel  167:     }
                    168:     return '';
1.10      www       169: }
                    170: 
                    171: 
                    172: 
1.9       www       173: # ------------------------------------------------------------- Find out status
1.25      albertel  174: # return codes
                    175: # tcode (timecode)
                    176: # 1: will open later
                    177: # 2: is open and not past due yet
                    178: # 3: is past due date
                    179: # 4: due in the next 24 hours
                    180: #
                    181: # code (curent solved status)
                    182: # 1: not attempted
                    183: # 2: attempted but wrong, or incorrect by instructor
                    184: # 3: solved or correct by instructor
1.29      albertel  185: # 4: partially correct (one or more parts correct)
                    186: # "excused" needs to be supported, but is not yet.
1.9       www       187: sub astatus {
                    188:     my $rid=shift;
1.29      albertel  189:     my $code=0;
1.9       www       190:     my $ctext='';
                    191:     $rid=~/(\d+)\.(\d+)/;
1.10      www       192:     my $symb=&Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.
1.51    ! bowersj2  193:         &Apache::lonnet::declutter($hash{'src_'.$rid});
1.15      www       194:     my %duedate=();
                    195:     my %opendate=();
                    196:     my %answerdate=();
1.25      albertel  197:     # need to always check part 0's open/due/answer status
1.51    ! bowersj2  198:     foreach (sort(split(/\,/,&Apache::lonnet::metadata($hash{'src_'.$rid},'allpo\ssiblekeys')))) {
1.15      www       199:         if ($_=~/^parameter\_(.*)\_opendate$/) {
1.51    ! bowersj2  200:             my $part=$1;
1.15      www       201:             $duedate{$part}=&parmval($part.'.duedate',$symb);
                    202:             $opendate{$part}=&parmval($part.'.opendate',$symb);
                    203:             $answerdate{$part}=&parmval($part.'.answerdate',$symb);
1.49      www       204:             if (&parmval($part.'.opendate.type',$symb) eq 'date_interval') {
1.51    ! bowersj2  205:                 $opendate{$part}=$duedate{$part}-$opendate{$part};
1.49      www       206:             }
1.50      www       207:             if (&parmval($part.'.answerdate.type',$symb) eq 'date_interval') {
1.49      www       208:                 $answerdate{$part}=$duedate{$part}+$answerdate{$part};
                    209:             }
1.15      www       210:         }
1.25      albertel  211:     }
1.11      www       212:     my $now=time;
                    213:     my $tcode=0;
1.16      www       214: 
                    215:     my %returnhash=&Apache::lonnet::restore($symb);
                    216: 
1.25      albertel  217:     foreach (sort(keys(%opendate))) {
1.51    ! bowersj2  218:         my $duedate=$duedate{$_};
        !           219:         my $opendate=$opendate{$_};
        !           220:         my $answerdate=$answerdate{$_};
        !           221:         my $preface='';
        !           222:         unless ($_ eq '0') { $preface=' Part: '.$_.' '; }
        !           223:         if ($opendate) {
        !           224:             if ($now<$duedate || (!$duedate)) {
        !           225:                 unless ($tcode==4) { $tcode=2; }
        !           226:                 if ($duedate) {
        !           227:                     $ctext.=$preface.'Due: '.localtime($duedate);
        !           228:                 } else {
        !           229:                     $ctext.=$preface.'No Due Date';
        !           230:                 }
        !           231:                 if ($now<$opendate) {
        !           232:                     unless ($tcode) { $tcode=1; }
        !           233:                     $ctext.=$preface.'Open: '.localtime($opendate);
        !           234:                 }
        !           235:                 if ($duedate && $duedate-$now<86400) {
        !           236:                     $tcode=4;
        !           237:                     $ctext.=$preface.'Due: '.localtime($duedate);
        !           238:                 }
        !           239:             } else {
        !           240:                 unless (($tcode==4) || ($tcode eq 2)) { $tcode=3; }
        !           241:                 if ($now<$answerdate) {
        !           242:                     $ctext.='Answer: '.localtime($duedate);
        !           243:                 }
        !           244:             }
        !           245:         } else {
        !           246:             unless (($tcode==2) || ($tcode==4)) { $tcode=1; }
        !           247:         }
        !           248:         
        !           249:         my $status=$returnhash{'resource.'.$_.'.solved'};
        !           250:         
        !           251:         if ($status eq 'correct_by_student') {
        !           252:             if ($code==0||$code==3) { $code=3; } else { $code=4; }
        !           253:             $ctext.=' solved';
        !           254:         } elsif ($status eq 'correct_by_override') {
        !           255:             if ($code==0||$code==3) { $code=3; } else { $code=4; }
        !           256:             $ctext.=' override';
        !           257:         } elsif ($status eq 'incorrect_attempted') {
        !           258:             if ($code!=4 && $code!=3) { $code=2; }
        !           259:             if ($code==3) { $code=4; }
        !           260:             $ctext.=' ('.
        !           261:                 ($returnhash{'resource.'.$_.'.tries'}?
        !           262:                  $returnhash{'resource.'.$_.'.tries'}:'0');
        !           263:             my $numtries = &parmval($_.'.maxtries',$symb);
        !           264:             if ($numtries) { $ctext.='/'.$numtries.' tries'; }
        !           265:             $ctext.=')';
        !           266:         } elsif ($status eq 'incorrect_by_override') {
        !           267:             if ($code!=4 && $code!=3) { $code=2; }
        !           268:             if ($code==3) { $code=4; }
        !           269:             $ctext.=' override';
        !           270:         } elsif ($status eq 'excused') {
        !           271:             if ($code==0||$code==3) { $code=3; } else { $code=4; }
        !           272:             $ctext.=' excused';
        !           273:         } else {
        !           274:             if ($code==0) { $code=1; }
        !           275:         }
1.25      albertel  276:     }
1.16      www       277: 
1.11      www       278:     return 'p'.$code.$tcode.'"'.$ctext.'"';
1.9       www       279: }
                    280: 
1.2       www       281: 
1.31      albertel  282: sub addresource {
1.32      albertel  283:     my ($resource,$sofar,$rid,$showtypes,$indent,$linkid)=@_;
1.31      albertel  284:     if ($showtypes eq 'problems') {
1.51    ! bowersj2  285:         if ($resource!~/\.(problem|exam|quiz|assess|survey|form)$/) {
        !           286:             return;
        !           287:         }
1.31      albertel  288:     }
                    289:     my $brepriv=&Apache::lonnet::allowed('bre',$resource);
                    290:     if ($hash{'src_'.$rid}) {
1.51    ! bowersj2  291:         if (($brepriv eq '2') || ($brepriv eq 'F')) {
        !           292:             my $pprefix='';
        !           293:             if ($resource=~/\.(problem|exam|quiz|assess|survey|form)$/) {
        !           294:                 $pprefix=&astatus($rid);
        !           295:             }
        !           296:             $$sofar++;
        !           297:             if ($indent) { $pprefix='i'.$indent.','.$pprefix; }
        !           298:             if ($linkid) { $pprefix='l'.$linkid.','.$pprefix; }
        !           299:             if (defined($rows[$$sofar])) {
        !           300:                 $rows[$$sofar].='&'.$pprefix.$rid;
        !           301:             } else {
        !           302:                 $rows[$$sofar]=$pprefix.$rid;
        !           303:             }
        !           304:         }
1.31      albertel  305:     }
                    306: }
                    307: 
                    308: sub followlinks () {
1.32      albertel  309:     my ($rid,$sofar,$beenhere,$further,$showtypes,$indent,$linkid)=@_;
1.31      albertel  310:     my $mincond=1;
                    311:     my $next='';
                    312:     foreach (split(/\,/,$hash{'to_'.$rid})) {
1.51    ! bowersj2  313:         my $thiscond=
        !           314:             &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
        !           315:         if ($thiscond>=$mincond) {
        !           316:             if ($next) {
        !           317:                 $next.=','.$_.':'.$thiscond;
        !           318:             } else {
        !           319:                 $next=$_.':'.$thiscond;
        !           320:             }
        !           321:             if ($thiscond>$mincond) { $mincond=$thiscond; }
        !           322:         }
1.31      albertel  323:     }
1.32      albertel  324:     my $col=0;
                    325:     &Apache::lonxml::debug("following links -$next-");
1.31      albertel  326:     foreach (split(/\,/,$next)) {
1.51    ! bowersj2  327:         my ($nextlinkid,$condval)=split(/\:/,$_);
        !           328:         if ($condval>=$mincond) {
        !           329:             my $now=&tracetable($sofar,$hash{'goesto_'.$nextlinkid},
        !           330:                                 $beenhere,$showtypes,$indent,$linkid);
        !           331:             if ($now>$further) {                
        !           332:                 if ($col>0) {
        !           333:                     my $string;
        !           334:                     for(my $i=0;$i<$col;$i++) { $string.='&'; }
        !           335:                     for(my $i=$further+1;$now-1>$i;$i++) {
        !           336:                         $rows[$i]=$string.$rows[$i];
        !           337:                     }
        !           338:                 }
        !           339:                 $further=$now;
        !           340:             }
        !           341:         }
        !           342:         $col++;
1.31      albertel  343:     }
                    344:     return $further;
                    345: }
                    346: # ------------------------------------------------------------ Build page table
                    347: 
                    348: sub tracetable {
1.32      albertel  349:     my ($sofar,$rid,$beenhere,$showtypes,$indent,$linkid)=@_;
                    350:     my $newshowtypes=$showtypes;
1.31      albertel  351:     my $further=$sofar;
1.44      www       352: # $Apache::lonxml::debug=1;
1.32      albertel  353:     &Apache::lonxml::debug("$rid ; $linkid ; $sofar ; $beenhere ; ".$hash{'src_'.$rid});
                    354:     if ($beenhere=~/\&$rid\&/) { return $further; }
                    355:     $beenhere.=$rid.'&';
1.31      albertel  356: 
                    357:     if (defined($hash{'is_map_'.$rid})) {
1.51    ! bowersj2  358:         $sofar++;
        !           359:         my $tprefix='';
        !           360:         if ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}}
        !           361:             eq 'sequence') {
        !           362:             $tprefix='h';
        !           363:         } elsif ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}}
        !           364:                  eq 'page') {
        !           365:             $tprefix='j';
        !           366:             if ($indent) { $tprefix='i'.$indent.','.$tprefix; }
        !           367:             if ($linkid) { $tprefix='l'.$linkid.','.$tprefix; }
        !           368:             $newshowtypes='problems';
        !           369:             $indent++;
        !           370:             #if in a .page continue to link the encompising .page
        !           371:             if (!$linkid) { $linkid=$rid; }
        !           372:         }
        !           373:         if (defined($rows[$sofar])) {
        !           374:             $rows[$sofar].='&'.$tprefix.$rid;
        !           375:         } else {
        !           376:             $rows[$sofar]=$tprefix.$rid;
        !           377:         }
        !           378:         if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
        !           379:             (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
        !           380:             my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
        !           381:             $sofar=&tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
        !           382:                                '&'.$frid.'&',$newshowtypes,$indent,$linkid);
        !           383:             &addresource($hash{'src_'.$frid},\$sofar,$frid,$newshowtypes,
        !           384:                          $indent,$linkid);
        !           385:             if ($tprefix =~ /j$/) { $indent--; $linkid=''; }
        !           386:         }
1.31      albertel  387:     } else {
1.51    ! bowersj2  388:         &addresource($hash{'src_'.$rid},\$sofar,$rid,$showtypes,
        !           389:                      $indent,$linkid);
1.31      albertel  390:     }
                    391: 
                    392:     if (defined($hash{'to_'.$rid})) {
1.51    ! bowersj2  393:         $further=&followlinks($rid,$sofar,$beenhere,$further,$showtypes,
        !           394:                               $indent,$linkid);
1.2       www       395:     }
1.31      albertel  396: 
1.2       www       397:     return $further;
                    398: }
                    399: 
                    400: # ================================================================ Main Handler
1.1       www       401: 
                    402: sub handler {
1.28      albertel  403:     my $r=shift;
1.2       www       404: 
1.51    ! bowersj2  405:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
        !           406: 
        !           407:     if ($ENV{'form.jtest'} ne "1")
        !           408:     {
        !           409:         return new_handle($r);
        !           410:     }
1.2       www       411: 
                    412: # ------------------------------------------- Set document type for header only
                    413: 
1.28      albertel  414:     if ($r->header_only) {
1.51    ! bowersj2  415:         if ($ENV{'browser.mathml'}) {
        !           416:             $r->content_type('text/xml');
        !           417:         } else {
        !           418:             $r->content_type('text/html');
        !           419:         }
        !           420:         $r->send_http_header;
        !           421:         return OK;
1.28      albertel  422:     }
                    423:     my $requrl=$r->uri;
                    424:     my $hashtied;
1.2       www       425: # ----------------------------------------------------------------- Tie db file
1.28      albertel  426:     my $fn;
                    427:     if ($ENV{'request.course.fn'}) {
1.51    ! bowersj2  428:         $fn=$ENV{'request.course.fn'};
        !           429:         if (-e "$fn.db") {
        !           430:             if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) &&
        !           431:                 (tie(%parmhash,'GDBM_File',
        !           432:                      $ENV{'request.course.fn'}.'_parms.db',
        !           433:                      &GDBM_READER(),0640))) {
        !           434:                 $hashtied=1;
        !           435:             }
        !           436:         }
1.28      albertel  437:     }
                    438:     if (!$hashtied) {
1.51    ! bowersj2  439:         $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
        !           440:         return HTTP_NOT_ACCEPTABLE; 
1.28      albertel  441:     }
                    442: 
1.2       www       443: # ------------------------------------------------------------------- Hash tied
1.28      albertel  444: 
                    445:     if ($ENV{'browser.mathml'}) {
1.51    ! bowersj2  446:         $r->content_type('text/xml');
1.28      albertel  447:     } else {
1.51    ! bowersj2  448:         $r->content_type('text/html');
1.28      albertel  449:     }
                    450:     &Apache::loncommon::no_cache($r);
                    451:     $r->send_http_header;
                    452: 
1.42      www       453:     my $firstres=$hash{'map_start_'.
                    454:            &Apache::lonnet::clutter($ENV{'request.course.uri'})};
                    455:     my $lastres=$hash{'map_finish_'.
                    456:            &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1.28      albertel  457:     if (!(($firstres) && ($lastres))) {
1.51    ! bowersj2  458:         $r->print('<html><body>Coursemap undefined.</body></html>');
1.28      albertel  459:     } else {
                    460: 
1.2       www       461: # ----------------------------------------------------------------- Render page
1.10      www       462: # -------------------------------------------------------------- Set parameters
                    463: 
                    464: 
                    465: # ---------------------------- initialize coursedata and userdata for this user
1.51    ! bowersj2  466:         undef %courseopt;
        !           467:         undef %useropt;
1.10      www       468: 
1.51    ! bowersj2  469:         my $uname=$ENV{'user.name'};
        !           470:         my $udom=$ENV{'user.domain'};
        !           471:         my $uhome=$ENV{'user.home'};
        !           472:         my $cid=$ENV{'request.course.id'};
        !           473:         my $chome=$ENV{'course.'.$cid.'.home'};
        !           474:         my ($cdom,$cnum)=split(/\_/,$cid);
        !           475: 
        !           476:         my $userprefix=$uname.'_'.$udom.'_';
        !           477:         
        !           478:         unless ($uhome eq 'no_host') { 
1.48      bowersj2  479: # ------------------------------------------------- Get coursedata (if present)
1.51    ! bowersj2  480:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
        !           481:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
        !           482:                                                  ':resourcedata',$chome);
        !           483:                 if ($reply!~/^error\:/) {
        !           484:                     $courserdatas{$cid}=$reply;
        !           485:                     $courserdatas{$cid.'.last_cache'}=time;
        !           486:                 }
        !           487:                 # check to see if network failed
        !           488:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
        !           489:                 {
        !           490:                     $networkFailedFlag = 1;
        !           491:                 }
        !           492:             }
        !           493:             foreach (split(/\&/,$courserdatas{$cid})) {
        !           494:                 my ($name,$value)=split(/\=/,$_);
        !           495:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
        !           496:                     &Apache::lonnet::unescape($value);
        !           497:             }
1.10      www       498: # --------------------------------------------------- Get userdata (if present)
1.51    ! bowersj2  499:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
        !           500:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
        !           501:                 if ($reply!~/^error\:/) {
        !           502:                     $userrdatas{$uname.'___'.$udom}=$reply;
        !           503:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
        !           504:                 }
        !           505:             }
        !           506:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
        !           507:                 my ($name,$value)=split(/\=/,$_);
        !           508:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
        !           509:                     &Apache::lonnet::unescape($value);
        !           510:             }
        !           511:         }
1.28      albertel  512: 
1.51    ! bowersj2  513:         @rows=();
1.28      albertel  514: 
1.51    ! bowersj2  515:         &tracetable(0,$firstres,'&','',0);
1.2       www       516: 
                    517: # ------------------------------------------------------------------ Page parms
                    518: 
1.51    ! bowersj2  519:         my $j;
        !           520:         my $i;
        !           521:         my $lcm=1;
        !           522:         my $contents=0;
1.2       www       523: 
                    524: # ---------------------------------------------- Go through table to get layout
                    525: 
1.51    ! bowersj2  526:         for ($i=0;$i<=$#rows;$i++) {
        !           527:             if ($rows[$i]) {
        !           528:                 &Apache::lonxml::debug("Row $i is:".$rows[$i]);
        !           529:                 $contents++;
        !           530:                 my @colcont=split(/\&/,$rows[$i]);
        !           531:                 $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
        !           532:             } 
        !           533:         }
1.5       www       534: 
1.2       www       535: 
1.51    ! bowersj2  536:         unless ($contents) {
        !           537:             $r->print('<html><body>Empty Map.</body></html>');
        !           538:         } else {
1.10      www       539: 
1.2       www       540: # ------------------------------------------------------------------ Build page
                    541: 
1.51    ! bowersj2  542:             my $currenturl=$ENV{'form.postdata'};
        !           543:             $currenturl=~s/^http\:\/\///;
        !           544:             $currenturl=~s/^[^\/]+//;
1.13      www       545: 
1.2       www       546: # ---------------------------------------------------------------- Send headers
                    547: 
1.51    ! bowersj2  548:             my $date=localtime;
        !           549:             my $now=time;
1.21      www       550: # ----------------------------------------- Get email status and discussiontime
                    551: 
1.51    ! bowersj2  552:             my %emailstatus=&Apache::lonnet::dump('email_status');
        !           553:             my $logouttime=$emailstatus{'logout'};
        !           554:             my $courseleave=$emailstatus{'logout_'.$ENV{'request.course.id'}};
        !           555:             my $lastcheck=($courseleave>$logouttime?$courseleave:$logouttime);
        !           556: 
        !           557:             my %discussiontimes=&Apache::lonnet::dump('discussiontimes',
        !           558:                                                           $cdom,$cnum);
        !           559: 
        !           560:             my %feedback=();
        !           561:             my %error=();
        !           562:             foreach my $msgid (split(/\&/,&Apache::lonnet::reply('keys:'.
        !           563:                                                                  $ENV{'user.domain'}.':'.
        !           564:                                                                  $ENV{'user.name'}.':nohist_email',
        !           565:                                                                  $ENV{'user.home'}))) {
        !           566:                 $msgid=&Apache::lonnet::unescape($msgid);
        !           567:                 my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
        !           568:                 if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
        !           569:                     my ($what,$url)=($1,$2);
        !           570:                     my %status=
        !           571:                         &Apache::lonnet::get('email_status',[$msgid]);
        !           572:                     if ($status{$msgid}=~/^error\:/) { 
        !           573:                         $status{$msgid}=''; 
        !           574:                     }
        !           575:                         
        !           576:                     if (($status{$msgid} eq 'new') || 
        !           577:                         (!$status{$msgid})) { 
        !           578:                         if ($what eq 'Error') {
        !           579:                             $error{$url}.=','.$msgid; 
        !           580:                         } else {
        !           581:                             $feedback{$url}.=','.$msgid;
        !           582:                         }
        !           583:                     }
        !           584:                 }
        !           585:             }
1.22      www       586: # ----------------------------------------------------------- Start Page Output
1.43      www       587:             my $bodytagadd='';
1.51    ! bowersj2  588:             $r->print(
1.43      www       589:                    '<html><head><title>Navigate Course Map</title></head>');
1.51    ! bowersj2  590:             if (($currenturl=~/^\/res/) &&
        !           591:                 ($currenturl!~/^\/res\/adm/)) {
        !           592:                 $bodytagadd='onLoad="window.location.hash='."'curloc'".'"';
        !           593:             }
        !           594:             $r->print(&Apache::loncommon::bodytag('Navigate Course Map','',
1.43      www       595:                                                   $bodytagadd));
1.51    ! bowersj2  596:             $r->print('<script>window.focus();</script>');
        !           597:             my $desc=$ENV{'course.'.$ENV{'request.course.id'}.'.description'};
        !           598:             if (defined($desc)) { $r->print("<h2>$desc</h2>\n"); }
        !           599:             $r->print("<h3>$date</h3>\n");
        !           600:             $r->rflush();
        !           601:             $r->print('<img src="/adm/lonMisc/chat.gif"> New discussion since '.
        !           602:                       localtime($lastcheck).
        !           603:                       '<br><img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'); 
        !           604:             if (($currenturl=~/^\/res/) &&
        !           605:                 ($currenturl!~/^\/res\/adm/)) {
        !           606:                 $r->print('<a href="#curloc">Current Location</a><p>');
        !           607:             }
        !           608: 
        !           609:             # Handle a network error
        !           610: 
        !           611:             if ($networkFailedFlag)
        !           612:             {
        !           613:                 $r->print('<H2>LON-CAPA network failure.</H2>'."\n");
        !           614:                 $r->print("<p>LON-CAPA's network is having difficulties, some problem" .
        !           615:                           " information, such as due dates, will not be available.");
        !           616:             }
1.13      www       617: # ----------------------------------------------------- The little content list
1.51    ! bowersj2  618:             for ($i=0;$i<=$#rows;$i++) {
        !           619:                 if ($rows[$i]) {
        !           620:                     my @colcont=split(/\&/,$rows[$i]);
        !           621:                     my $avespan=$lcm/($#colcont+1);
        !           622:                     for ($j=0;$j<=$#colcont;$j++) {
        !           623:                         my $rid=$colcont[$j];
        !           624:                         if ($rid=~/^h(.+)/) {
        !           625:                             $rid=$1;
        !           626:                             $r->print('&nbsp;&nbsp;&nbsp;<a href="#'.
        !           627:                                       $rid.'">'.$hash{'title_'.$rid}.
        !           628:                                       '</a><br>');
        !           629:                         }
        !           630:                     }
        !           631:                 }
        !           632:             }
1.2       www       633: # ----------------------------------------------------------------- Start table
1.51    ! bowersj2  634:             $r->print('<hr><table cols="'.$lcm.'" border="0">');
        !           635:             for ($i=0;$i<=$#rows;$i++) {
        !           636:                 if ($rows[$i]) {
        !           637:                     $r->print("\n<tr>");
        !           638:                     my @colcont=split(/\&/,$rows[$i]);
        !           639:                     my $avespan=$lcm/($#colcont+1);
        !           640: 
        !           641:                     # for each item I wish to print on this row...
        !           642:                     for ($j=0;$j<=$#colcont;$j++) {
        !           643:                         my $indent;my $indentstr;
        !           644:                         my $linkid;
        !           645:                         my $rid=$colcont[$j];
1.36      www       646:                         $rid=~/(\d+)\.(\d+)$/;
1.51    ! bowersj2  647:                         my $src=
        !           648:                            &Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
        !           649:                         my $symb=
        !           650:           &Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.$src;
        !           651:                         my $add='<td>';
        !           652:                         my $adde='</td>';
        !           653:                         my $hwk='<font color="#223322">';
        !           654:                         my $hwke='</font>';
        !           655:                         if ($rid=~/^l(\d+\.\d+),(.+)/) { $linkid=$1; $rid=$2; }
        !           656:                         if ($rid=~/^i(\d+),(.+)/) { $indent=$1; $rid=$2; }
        !           657:                         if ($rid=~/^h(.+)/) {
        !           658:                             $rid=$1;
        !           659:                             $add='<th bgcolor="#AAFF55"><a name="'.$rid.'">';
        !           660:                             $adde='</th>';
1.36      www       661:                             if (($ENV{'user.adv'}) && 
1.51    ! bowersj2  662:                                 ($parmhash{$symb.'.0.parameter_randompick'})) {
1.36      www       663:                                $adde=' (randomly select '.
1.51    ! bowersj2  664:                                    $parmhash{$symb.'.0.parameter_randompick'}.
1.36      www       665:                                    ')</th>';
                    666:                             }
1.51    ! bowersj2  667:                         }
        !           668:                         if ($rid=~/^j(.+)/) { $rid=$1; }
        !           669:                         if ($rid=~/^p(\d)(\d)\"([\w\: \(\)\/\,]*)\"(.+)/) {
        !           670:                             # sub astatus describes what code/tcode mean
        !           671:                             my $code=$1;
        !           672:                             my $tcode=$2;
        !           673:                             my $ctext=$3;
        !           674:                             $rid=$4;
        !           675:                             
        !           676:                             # will open later
        !           677:                             if ($tcode eq '1') { 
        !           678:                                 $add='<td bgcolor="#AAAAAA">';
        !           679:                             }
        !           680: 
        !           681:                             # solved/correct
        !           682:                             if ($code eq '3') {
        !           683:                                 $add='<td bgcolor="#AAFFAA">';
        !           684:                             } elsif ($code eq '4') { # partially correct
        !           685:                                 $add='<td bgcolor="#E0FFAA">';
        !           686:                             } else {
        !           687:                                 # not attempted
        !           688:                                 
        !           689:                                 # we end up here on network failure, so pick a neutral
        !           690:                                 # color if the network failed instead of bright red.
        !           691:                                 if ( $networkFailedFlag )
        !           692:                                 {
        !           693:                                     $add = '<td bgcolor="#AAAAAA">';
        !           694:                                 }
        !           695:                                 else
        !           696:                                 {
        !           697:                                     $add='<td bgcolor="#FFAAAA">';
        !           698:                                 }
        !           699: 
        !           700:                                 if ($tcode eq '2') { # open, not past due
        !           701:                                     $add='<td bgcolor="#FFFFAA">';
        !           702:                                 }
        !           703: 
        !           704:                                 if ($tcode eq '4') { # due in next 24 hours
        !           705:                                     $add='<td bgcolor="#FFFF33">';
        !           706:                                     $adde='</td>';
        !           707:                                 }
        !           708:                             }
        !           709:                             $hwk='<font color="#888811"><b>';
        !           710:                             $hwke='</b></font>';
        !           711:                             if ($code eq '1') {
        !           712:                                 $hwke='</b> ('.$ctext.')</font>';
        !           713:                             }
        !           714:                             if ($code eq '2' || $code eq '4') {
        !           715:                                 $hwk='<font color="#992222"><b>';
        !           716:                                 $hwke='</b> ('.$ctext.')</font>';
        !           717:                             }
        !           718:                             if ($code eq '3') {
        !           719:                                 $hwk='<font color="#229922"><b>';
        !           720:                                 $hwke='</b> ('.$ctext.')</font>';
        !           721:                             }
        !           722:                             if ($networkFailedFlag) 
        !           723:                             {
        !           724:                                 $hwke='</b> (status unavailable)</font>';
        !           725:                             }
        !           726:                         }
        !           727:                         if ($rid && $hash{'src_'.$rid} eq $currenturl) {
        !           728:                             $add=$add.'<a name="curloc"></a>'.
        !           729:                                 '<font color=red size=+2><b>&gt; </b></font>';
        !           730:                             $adde=
        !           731:                                 '<font color=red size=+2><b> &lt;</b></font>'.$adde;
        !           732:                         }
        !           733:                         if ($discussiontimes{$symb}>$lastcheck) {
        !           734:                             $adde=
        !           735:                                 '<img border=0 src="/adm/lonMisc/chat.gif">'.
        !           736:                                     $adde;
        !           737:                         }
        !           738:                         if ($error{$src}) {
        !           739:                             foreach (split(/\,/,$error{$src})) {
        !           740:                                 if ($_) {
        !           741:                                     $adde=
        !           742:                                         '&nbsp;<a href="/adm/email?display='.
        !           743:                                             &Apache::lonnet::escape($_).
        !           744:                                                 '"><img src="/adm/lonMisc/bomb.gif" border=0></a>'
        !           745:                                                     .$adde;
        !           746:                                 }
        !           747:                             }
        !           748:                         }
        !           749:                         if ($feedback{$src}) {
        !           750:                             foreach (split(/\,/,$feedback{$src})) {
        !           751:                                 if ($_) {
        !           752:                                     $adde=
        !           753:                                         '&nbsp;<a href="/adm/email?display='.
        !           754:                                             &Apache::lonnet::escape($_).
        !           755:                                                 '"><img src="/adm/lonMisc/feedback.gif" border=0></a>'
        !           756:                                                     .$adde;
        !           757:                                 }
        !           758:                             }
        !           759:                         }
        !           760:                         if ($indent) {
        !           761:                             my $is="&nbsp;&nbsp;";
        !           762:                             for(my $i=-1;$i<$indent;$i++) { $indentstr.=$is; }
        !           763:                         }
        !           764:                         if (!$linkid) { $linkid=$rid; }
1.38      www       765:                         if ($hash{'randomout_'.$rid}) {
                    766:                             $adde=' <i>(hidden)</i>'.$adde;
                    767:                         }
1.51    ! bowersj2  768:                         $r->print($add.$indentstr);
        !           769:                         if ($rid) {
        !           770:                             $r->print('<a href="'.$hash{'src_'.$linkid}.
1.39      www       771:                                       (($hash{'src_'.$linkid}=~/\?/)?'&':'?').
                    772:                                       'symb='.&Apache::lonnet::escape($symb)
1.35      www       773:                                        .'">'.
1.51    ! bowersj2  774:                                       $hwk.$hash{'title_'.$rid}.$hwke.'</a>');
        !           775:                         }
        !           776:                         $r->print($adde);
        !           777:                     }
        !           778:                     $r->print('</tr>');
        !           779:                 }
        !           780:             }
        !           781:             $r->print("\n</table>");
        !           782:             $r->print('</body></html>');
1.2       www       783: # -------------------------------------------------------------------- End page
1.51    ! bowersj2  784:         }                  
1.2       www       785: # ------------------------------------------------------------- End render page
1.28      albertel  786:     }
1.2       www       787: # ------------------------------------------------------------------ Untie hash
1.28      albertel  788:     unless (untie(%hash)) {
1.51    ! bowersj2  789:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
        !           790:                                  "Could not untie coursemap $fn (browse).</font>"); 
1.28      albertel  791:     }
                    792:     unless (untie(%parmhash)) {
1.51    ! bowersj2  793:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
        !           794:                                  "Could not untie parmhash (browse).</font>"); 
1.28      albertel  795:     }
                    796:     return OK;
1.2       www       797: }
1.1       www       798: 
1.51    ! bowersj2  799: sub new_handle {
        !           800:     my $r = shift;
        !           801: 
        !           802:     # Handle header-only request
        !           803:     if ($r->header_only) {
        !           804:         if ($ENV{'browser.mathml'}) {
        !           805:             $r->content_type('text/xml');
        !           806:         } else {
        !           807:             $r->content_type('text/html');
        !           808:         }
        !           809:         $r->send_http_header;
        !           810:         return OK;
        !           811:     }
        !           812: 
        !           813:     # Send header, don't cache this page
        !           814:     if ($ENV{'browser.mathml'}) {
        !           815:         $r->content_type('text/xml');
        !           816:     } else {
        !           817:         $r->content_type('text/html');
        !           818:     }
        !           819:     &Apache::loncommon::no_cache($r);
        !           820:     $r->send_http_header;
        !           821: 
        !           822:     # Header
        !           823:     $r->print(&Apache::loncommon::bodytag('Navigate Course Map','',
        !           824:                                           ''));
        !           825:     # Initialize the nav map
        !           826:     my $navmap = Apache::lonnavmaps::navmap->new(
        !           827:                         $ENV{"request.course.fn"}.".db",
        !           828:                         $ENV{"request.course.fn"}."_parms.db", 1);
        !           829: 
        !           830:     # Grab a resource object so we have access to the constants; this
        !           831:     # is technically not proper, but should be harmless
        !           832:     my $res = $navmap->firstResource();
        !           833: 
        !           834:     # Defines a status->color mapping, null string means don't color
        !           835:     my %colormap = 
        !           836:     ( $res->NETWORK_FAILURE    => '',
        !           837:       $res->CORRECT            => '#BBFFBB',
        !           838:       $res->EXCUSED            => '#BBBBFF',
        !           839:       $res->PAST_DUE           => '#FFAA00',
        !           840:       $res->ANSWER_OPEN        => '#FF00AA',
        !           841:       $res->OPEN_LATER         => '',
        !           842:       $res->TRIES_LEFT         => '#FFFF00',
        !           843:       $res->INCORRECT          => '#FFAA00',
        !           844:       $res->OPEN               => '#FFFF88' );
        !           845: 
        !           846:     if (!defined($navmap)) {
        !           847:         my $requrl = $r->uri;
        !           848:         $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
        !           849:         return HTTP_NOT_ACCEPTABLE;
        !           850:     }
        !           851: 
        !           852:     my %filterHash;
        !           853:     # Figure out what we're not displaying
        !           854:     foreach (split(/\,/, $ENV{"form.filter"})) {
        !           855:         if ($_) {
        !           856:             $filterHash{$_} = "1";
        !           857:         }
        !           858:     }
        !           859: 
        !           860:     # Begin the HTML table
        !           861:     # two columns: the title/icon on the left, the info on the right
        !           862:     $r->print('<table cellspacing="3" cellpadding="0" bgcolor="#FFFFFF">' ."\n");
        !           863: 
        !           864:     my $mapIterator = $navmap->getIterator(undef, undef, \%filterHash);
        !           865:     my $curRes = $mapIterator->next();
        !           866:     my $indentLevel = -1;
        !           867:     my $indentString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        !           868: 
        !           869:     my $isNewBranch = 0;
        !           870: 
        !           871:     while ($curRes != $mapIterator->END_NAV_MAP) {
        !           872:         if ($curRes == $mapIterator->BEGIN_MAP() ||
        !           873: 	    $curRes == $mapIterator->BEGIN_BRANCH()) {
        !           874:             $indentLevel++;
        !           875:         }
        !           876:         if ($curRes == $mapIterator->END_MAP() ||
        !           877: 	    $curRes == $mapIterator->END_BRANCH()) {
        !           878:             $indentLevel--;
        !           879:         }
        !           880: 
        !           881: 	if ($curRes == $mapIterator->BEGIN_BRANCH()) {
        !           882: 	    $r->print("Begin branch<br/>");
        !           883: 	}
        !           884:         if ($curRes == $mapIterator->END_BRANCH()) {
        !           885:             $r->print("End branch<br/>");
        !           886:         }
        !           887: 
        !           888:         if (ref($curRes) && $curRes->src()) {
        !           889: 
        !           890:             my @parts;
        !           891:             if ($curRes->is_problem()) {
        !           892:                 @parts = @{$curRes->parts()};
        !           893:                 if (scalar(@parts) > 1) { shift(@parts); }
        !           894:             } else {
        !           895:                 @parts[0] = "0"; # this is to get past foreach loop below
        !           896:                  # you can consider a non-problem resource as a resource
        !           897:                  # with only one part without loss
        !           898:             }
        !           899: 
        !           900:             foreach my $part (@parts) {
        !           901:                 
        !           902:                 # For each thing we're displaying...
        !           903: 
        !           904:                 my $stack = $mapIterator->getStack();
        !           905:                 my $src = getLinkForResource($stack);
        !           906: 
        !           907:                 my $srcHasQuestion = $src =~ /\?/;
        !           908:                 my $link = $src.
        !           909:                     ($srcHasQuestion?'&':'?') .
        !           910:                     'symb='.&Apache::lonnet::escape($curRes->symb()).
        !           911:                     '"';
        !           912:                 my $title = $curRes->title();
        !           913:                 my $partLabel = "";
        !           914: 
        !           915:                 # links to open and close the folders
        !           916:                 my $linkopen = "<a href=\"$link\">";
        !           917:                 my $linkclose = "</a>";
        !           918: 
        !           919:                 my $icon = "html.gif";
        !           920:                 if ($curRes->is_problem()) { 
        !           921:                     $icon = "problem.gif"; 
        !           922:                 }
        !           923: 
        !           924:                 # Display the correct icon, link to open or shut map
        !           925:                 if ($curRes->is_map()) { 
        !           926:                     my $mapId = $curRes->map_pc();
        !           927:                     my $nowOpen = !defined($filterHash{$mapId});
        !           928:                     $icon = $nowOpen ?
        !           929:                         "folder_opened.gif" : "folder_closed.gif";
        !           930:                     $linkopen = "<a href=\"/adm/navmaps?jtest=1&filter=";
        !           931:                     $linkopen .= $nowOpen ? 
        !           932:                         addToFilter(\%filterHash, $mapId) :
        !           933:                         removeFromFilter(\%filterHash, $mapId);
        !           934:                     $linkopen .= "\">";
        !           935:                     $linkclose = "</a>";
        !           936:                     
        !           937:                 }
        !           938:                 
        !           939:                 my $colorizer = "";
        !           940:                 if ($curRes->is_problem()) {
        !           941:                     my $status = $curRes->status($part);
        !           942:                     my $color = $colormap{$status};
        !           943:                     if ($color ne "") {
        !           944:                         $colorizer = "bgcolor=\"$color\"";
        !           945:                     }
        !           946:                 }
        !           947: 
        !           948:                 $r->print("  <tr><td align=\"left\" valign=\"top\">\n");
        !           949: 
        !           950:                 # print indentation
        !           951:                 for (my $i = 0; $i < $indentLevel; $i++) {
        !           952:                     $r->print($indentString);
        !           953:                 }
        !           954: 
        !           955:                 $r->print("  ${linkopen}<img border=\"0\" src=\"" .
        !           956:                           "/adm/lonIcons/$icon\" alt=\"\">${linkclose}\n");
        !           957: 
        !           958:                 if ($curRes->is_problem() && $part != "0") { $partLabel = " (Part $part)"; }
        !           959: 
        !           960:                 $r->print("  <a href=\"$link\">$title$partLabel</a></td>".
        !           961:                           "<td $colorizer align=\"right\">\n");
        !           962:                 
        !           963:                 if ($curRes->kind() eq "res" and 
        !           964:                     $curRes->is_problem() ) {
        !           965:                     $r->print (" Due: " . localtime($curRes->duedate()));
        !           966:                 }
        !           967:             }
        !           968:         }
        !           969:         $curRes = $mapIterator->next();
        !           970:     }
        !           971: 
        !           972:     $r->print("</table></body></html>");
        !           973: 
        !           974:     return OK;
        !           975: }
        !           976: 
        !           977: # Convenience functions: Returns a string that adds or subtracts
        !           978: # the second argument from the first hash, appropriate for the 
        !           979: # query string that determines which folders to recurse on
        !           980: sub addToFilter {
        !           981:     my $hashIn = shift;
        !           982:     my $addition = shift;
        !           983:     my %hash = %$hashIn;
        !           984:     $hash{$addition} = 1;
        !           985: 
        !           986:     return join (",", keys(%hash));
        !           987: }
        !           988: 
        !           989: sub removeFromFilter {
        !           990:     my $hashIn = shift;
        !           991:     my $subtraction = shift;
        !           992:     my %hash = %$hashIn;
        !           993: 
        !           994:     delete $hash{$subtraction};
        !           995:     return join(",", keys(%hash));
        !           996: }
        !           997: 
        !           998: # Convenience function: Given a stack returned from getStack on the iterator,
        !           999: # return the correct src() value.
        !          1000: # Later, this should add an anchor when we start putting anchors in pages.
        !          1001: sub getLinkForResource {
        !          1002:     my $stack = shift;
        !          1003:     my $res;
        !          1004: 
        !          1005:     # Check to see if there are any pages in the stack
        !          1006:     foreach $res (@$stack) {
        !          1007:         if (defined($res) && $res->is_page()) {
        !          1008:             return $res->src();
        !          1009:         }
        !          1010:     }
        !          1011: 
        !          1012:     # Failing that, return the src of the last resource that is defined
        !          1013:     # (when we first recurse on a map, it puts an undefined resource
        !          1014:     # on the bottom because $self->{HERE} isn't defined yet, and we
        !          1015:     # want the src for the map anyhow)
        !          1016:     foreach (@$stack) {
        !          1017:         if (defined($_)) { $res = $_; }
        !          1018:     }
        !          1019: 
        !          1020:     return $res->src();
        !          1021: }
        !          1022: 
        !          1023: 1;
        !          1024: 
        !          1025: package Apache::lonnavmaps::navmap;
        !          1026: 
        !          1027: # LEFT:
        !          1028: # * Actual handler code (multi-part)
        !          1029: # * Branches (aieee!) (and conditionals)
        !          1030: 
        !          1031: =pod
        !          1032: 
        !          1033: lonnavmaps provides functions and objects for dealing with the compiled course hashes generated when a user enters the course, and also provides the Apache handler for the "Navigation Map" button.
        !          1034: 
        !          1035: =head1 navmap object: Encapsulating the compiled nav map
        !          1036: 
        !          1037: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
        !          1038: 
        !          1039: Most notably it provides a way to navigate the map sensibly and a flexible iterator that makes it easy to write various renderers based on nav maps.
        !          1040: 
        !          1041: You must obtain resource objects through the navmap object.
        !          1042: 
        !          1043: =head2 Methods
        !          1044: 
        !          1045: =over 4
        !          1046: 
        !          1047: =item * B<new>(filename, parmHashFile, genCourseAndUserOptions): Binds a new navmap object to the compiled course representation and parmHashFile. genCourseAndUserOptions is a flag saying whether the course options and user options hash should be generated. This is for when you are using the parameters of the resources that require them; see documentation in resource object documentation. Returns the navmap object if this is successful, or B<undef> if not. You must check for undef; errors will occur when you try to use the other methods otherwise.
        !          1048: 
        !          1049: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
        !          1050: 
        !          1051: =cut
        !          1052: 
        !          1053: use strict;
        !          1054: use GDBM_File;
        !          1055: 
        !          1056: sub new {
        !          1057:     # magic invocation to create a class instance
        !          1058:     my $proto = shift;
        !          1059:     my $class = ref($proto) || $proto;
        !          1060:     my $self = {};
        !          1061: 
        !          1062:     $self->{NAV_HASH_FILE} = shift;
        !          1063:     $self->{PARM_HASH_FILE} = shift;
        !          1064:     $self->{GENERATE_COURSE_USER_OPT} = shift;
        !          1065: 
        !          1066:     # Resource cache stores navmapresource's as we reference them. We generate
        !          1067:     # them on-demand so we don't pay for creating resources unless we use them.
        !          1068:     $self->{RESOURCE_CACHE} = {};
        !          1069: 
        !          1070:     # Network failure flag, if we accessed the course or user opt and
        !          1071:     # failed
        !          1072:     $self->{NETWORK_FAILURE} = 0;
        !          1073: 
        !          1074:     # tie the nav hash
        !          1075:     my %navmaphash;
        !          1076:     if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
        !          1077:               &GDBM_READER(), 0640))) {
        !          1078:         return undef;
        !          1079:     }
        !          1080:     $self->{NAV_HASH} = \%navmaphash;
        !          1081:     
        !          1082:     my %parmhash;
        !          1083:     if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
        !          1084:               &GDBM_READER(), 0640)))
        !          1085:     {
        !          1086:         return undef;
        !          1087:     }
        !          1088:     $self->{PARM_HASH} = \%parmhash;
        !          1089:     $self->{HASH_TIED} = 1;
        !          1090: 
        !          1091:     # If the course opt hash and the user opt hash should be generated,
        !          1092:     # generate them
        !          1093:     if ($self->{GENERATE_COURSE_USER_OPT}) {
        !          1094:         my $uname=$ENV{'user.name'};
        !          1095:         my $udom=$ENV{'user.domain'};
        !          1096:         my $uhome=$ENV{'user.home'};
        !          1097:         my $cid=$ENV{'request.course.id'};
        !          1098:         my $chome=$ENV{'course.'.$cid.'.home'};
        !          1099:         my ($cdom,$cnum)=split(/\_/,$cid);
        !          1100:         
        !          1101:         my $userprefix=$uname.'_'.$udom.'_';
        !          1102:         
        !          1103:         my %courserdatas; my %useropt; my %courseopt;
        !          1104:         unless ($uhome eq 'no_host') { 
        !          1105: # ------------------------------------------------- Get coursedata (if present)
        !          1106:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
        !          1107:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
        !          1108:                                                  ':resourcedata',$chome);
        !          1109:                 if ($reply!~/^error\:/) {
        !          1110:                     $courserdatas{$cid}=$reply;
        !          1111:                     $courserdatas{$cid.'.last_cache'}=time;
        !          1112:                 }
        !          1113:                 # check to see if network failed
        !          1114:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
        !          1115:                 {
        !          1116:                     $self->{NETWORK_FAILURE} = 1;
        !          1117:                 }
        !          1118:             }
        !          1119:             foreach (split(/\&/,$courserdatas{$cid})) {
        !          1120:                 my ($name,$value)=split(/\=/,$_);
        !          1121:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
        !          1122:                     &Apache::lonnet::unescape($value);
        !          1123:             }
        !          1124: # --------------------------------------------------- Get userdata (if present)
        !          1125:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
        !          1126:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
        !          1127:                 if ($reply!~/^error\:/) {
        !          1128:                     $userrdatas{$uname.'___'.$udom}=$reply;
        !          1129:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
        !          1130:                 }
        !          1131:                 # check to see if network failed
        !          1132:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
        !          1133:                 {
        !          1134:                     $self->{NETWORK_FAILURE} = 1;
        !          1135:                 }
        !          1136:             }
        !          1137:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
        !          1138:                 my ($name,$value)=split(/\=/,$_);
        !          1139:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
        !          1140:                     &Apache::lonnet::unescape($value);
        !          1141:             }
        !          1142:         $self->{COURSE_OPT} = \%courseopt;
        !          1143:         $self->{USER_OPT} = \%useropt;
        !          1144:         }
        !          1145:     }   
        !          1146: 
        !          1147:     bless($self);
        !          1148: 
        !          1149:     return $self;
        !          1150: }
        !          1151: 
        !          1152: sub getIterator {
        !          1153:     my $self = shift;
        !          1154:     my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
        !          1155:                                                      shift, shift);
        !          1156:     return $iterator;
        !          1157: }
        !          1158: 
        !          1159: # unties the hash when done
        !          1160: sub untieHashes {
        !          1161:     my $self = shift;
        !          1162:     untie $self->{NAV_HASH} if ($self->{HASH_TIED});
        !          1163:     untie $self->{PARM_HASH} if ($self->{HASH_TIED});
        !          1164:     $self->{HASH_TIED} = 0;
        !          1165: }
        !          1166: 
        !          1167: # when the object is destroyed, be sure to untie all the hashes we tied.
        !          1168: sub DESTROY {
        !          1169:     my $self = shift;
        !          1170:     $self->untieHashes();
        !          1171: }
        !          1172: 
        !          1173: =pod
        !          1174: 
        !          1175: =item * B<getById>(id): Based on the ID of the resource (1.1, 3.2, etc.), get a resource object for that resource. This method, or other methods that use it (as in the resource object) is the only proper way to obtain a resource object.
        !          1176: 
        !          1177: =cut
        !          1178: 
        !          1179: # The strategy here is to cache the resource objects, and only construct them
        !          1180: # as we use them. The real point is to prevent reading any more from the tied
        !          1181: # hash then we have to, which should hopefully alleviate speed problems.
        !          1182: # Caching is just an incidental detail I throw in because it makes sense.
        !          1183: 
        !          1184: sub getById {
        !          1185:     my $self = shift;
        !          1186:     my $id = shift;
        !          1187: 
        !          1188:     if (defined ($self->{RESOURCE_CACHE}->{$id}))
        !          1189:     {
        !          1190:         return $self->{RESOURCE_CACHE}->{$id};
        !          1191:     }
        !          1192: 
        !          1193:     # resource handles inserting itself into cache.
        !          1194:     return Apache::lonnavmaps::resource->new($self, $id);
        !          1195: }
        !          1196: 
        !          1197: =pod
        !          1198: 
        !          1199: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
        !          1200: 
        !          1201: =cut
        !          1202: 
        !          1203: sub firstResource {
        !          1204:     my $self = shift;
        !          1205:     my $firstResource = $self->{NAV_HASH}->{'map_start_' .
        !          1206:                      &Apache::lonnet::clutter($ENV{'request.course.uri'})};
        !          1207:     return $self->getById($firstResource);
        !          1208: }
        !          1209: 
        !          1210: =pod
        !          1211: 
        !          1212: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
        !          1213: 
        !          1214: =cut
        !          1215: 
        !          1216: sub finishResource {
        !          1217:     my $self = shift;
        !          1218:     my $firstResource = $self->{NAV_HASH}->{'map_finish_' .
        !          1219:                      &Apache::lonnet::clutter($ENV{'request.course.uri'})};
        !          1220:     return $self->getById($firstResource);
        !          1221: }
        !          1222: 
        !          1223: # -------------------------------------------- Figure out a cascading parameter
        !          1224: #
        !          1225: # For this function to work
        !          1226: #
        !          1227: # * parmhash needs to be tied
        !          1228: # * courseopt and useropt need to be initialized for this user and course
        !          1229: #
        !          1230: 
        !          1231: sub parmval {
        !          1232:     my $self = shift;
        !          1233:     my ($what,$symb)=@_;
        !          1234:     my $cid=$ENV{'request.course.id'};
        !          1235:     my $csec=$ENV{'request.course.sec'};
        !          1236:     my $uname=$ENV{'user.name'};
        !          1237:     my $udom=$ENV{'user.domain'};
        !          1238: 
        !          1239:     unless ($symb) { return ''; }
        !          1240:     my $result='';
        !          1241: 
        !          1242:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
        !          1243: 
        !          1244: # ----------------------------------------------------- Cascading lookup scheme
        !          1245:     my $rwhat=$what;
        !          1246:     $what=~s/^parameter\_//;
        !          1247:     $what=~s/\_/\./;
        !          1248: 
        !          1249:     my $symbparm=$symb.'.'.$what;
        !          1250:     my $mapparm=$mapname.'___(all).'.$what;
        !          1251:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
        !          1252: 
        !          1253:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
        !          1254:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
        !          1255:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
        !          1256: 
        !          1257:     my $courselevel= $usercourseprefix.'.'.$what;
        !          1258:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
        !          1259:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
        !          1260: 
        !          1261:     my $useropt = $self->{USER_OPT};
        !          1262:     my $courseopt = $self->{COURSE_OPT};
        !          1263:     my $parmhash = $self->{PARM_HASH};
        !          1264: 
        !          1265: # ---------------------------------------------------------- first, check user
        !          1266:     if ($uname and defined($useropt)) {
        !          1267:         if ($$useropt{$courselevelr}) { return $$useropt{$courselevelr}; }
        !          1268:         if ($$useropt{$courselevelm}) { return $$useropt{$courselevelm}; }
        !          1269:         if ($$useropt{$courselevel}) { return $$useropt{$courselevel}; }
        !          1270:     }
        !          1271: 
        !          1272: # ------------------------------------------------------- second, check course
        !          1273:     if ($csec and defined($courseopt)) {
        !          1274:         if ($$courseopt{$seclevelr}) { return $$courseopt{$seclevelr}; }
        !          1275:         if ($$courseopt{$seclevelm}) { return $$courseopt{$seclevelm}; }
        !          1276:         if ($$courseopt{$seclevel}) { return $$courseopt{$seclevel}; }
        !          1277:     }
        !          1278: 
        !          1279:     if (defined($courseopt)) {
        !          1280:         if ($$courseopt{$courselevelr}) { return $$courseopt{$courselevelr}; }
        !          1281:         if ($$courseopt{$courselevelm}) { return $$courseopt{$courselevelm}; }
        !          1282:         if ($$courseopt{$courselevel}) { return $$courseopt{$courselevel}; }
        !          1283:     }
        !          1284: 
        !          1285: # ----------------------------------------------------- third, check map parms
        !          1286: 
        !          1287:     my $thisparm=$$parmhash{$symbparm};
        !          1288:     if ($thisparm) { return $thisparm; }
        !          1289: 
        !          1290: # ----------------------------------------------------- fourth , check default
        !          1291: 
        !          1292:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
        !          1293:     if ($default) { return $default}
        !          1294: 
        !          1295: # --------------------------------------------------- fifth , cascade up parts
        !          1296: 
        !          1297:     my ($space,@qualifier)=split(/\./,$rwhat);
        !          1298:     my $qualifier=join('.',@qualifier);
        !          1299:     unless ($space eq '0') {
        !          1300:         my ($part,$id)=split(/\_/,$space);
        !          1301:         if ($id) {
        !          1302:             my $partgeneral=$self->parmval($part.".$qualifier",$symb);
        !          1303:             if ($partgeneral) { return $partgeneral; }
        !          1304:         } else {
        !          1305:             my $resourcegeneral=$self->parmval("0.$qualifier",$symb);
        !          1306:             if ($resourcegeneral) { return $resourcegeneral; }
        !          1307:         }
        !          1308:     }
        !          1309:     return '';
        !          1310: }
        !          1311: 
        !          1312: 
        !          1313: 1;
        !          1314: 
        !          1315: package Apache::lonnavmaps::iterator;
        !          1316: 
        !          1317: # This package must precede "navmap" because "navmap" uses it. In
        !          1318: # order to keep the documentation order straight, the iterator is documented
        !          1319: # after the navmap object.
        !          1320: 
        !          1321: =pod
        !          1322: 
        !          1323: =back
        !          1324: 
        !          1325: =head1 navmap Iterator
        !          1326: 
        !          1327: An I<iterator> encapsulates the logic required to traverse a data structure. navmap uses an iterator to traverse the course map according to the criteria you wish to use.
        !          1328: 
        !          1329: To obtain an iterator, call the B<getIterator>() function of a B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator directly.) This will return a reference to the iterator:
        !          1330: 
        !          1331: C<my $resourceIterator = $navmap-E<gt>getIterator();>
        !          1332: 
        !          1333: To get the next thing from the iterator, call B<next>:
        !          1334: 
        !          1335: C<my $nextThing = $resourceIterator-E<gt>next()>
        !          1336: 
        !          1337: getIterator behaves as follows:
        !          1338: 
        !          1339: =over 4
        !          1340: 
        !          1341: =item B<getIterator>(nav_map, firstResource, finishResource, filterHash): All parameters are optional. firstResource is a resource reference corresponding to where the iterator should start. It defaults to navmap->firstResource() for the corresponding nav map. finishResource corresponds to where you want the iterator to end, defaulting to navmap->finishResource(). It is your responsibility to ensure that the iterator will actually get there. filterHash is a hash used as a set containing strings representing the resource IDs, defaulting to empty. Condition is a 1 or 0 that sets what to do with the filter hash: If a 1, then only resource that exist IN the filterHash will be recursed on. If it is a 0, only resources NOT in the filterHash will be recursed on. Defaults to 0.
        !          1342: 
        !          1343: Thus, by default, all resources will be shown. Change the condition to a 1 without changing the hash, and only the top level of the map will be shown. Changing the condition to 1 and including some values in the hash will allow you to selectively examine parts of the navmap, while leaving it on 0 and adding things to the hash will allow you to selectively ignore parts of the nav map. See the handler code for examples.
        !          1344: 
        !          1345: The iterator will return either a reference to a resource object, or a token representing something in the map, such as the beginning of a new branch. The possible tokens are:
        !          1346: 
        !          1347: =over 4
        !          1348: 
        !          1349: =item * some list of tokens here
        !          1350: 
        !          1351: =back
        !          1352: 
        !          1353: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_NAV_MAP. (Perl will automatically optimize these into constants.
        !          1354: 
        !          1355: =cut
        !          1356: 
        !          1357: # Here are the tokens for the iterator:
        !          1358: 
        !          1359: sub END_NAV_MAP { return 0; }  # Represents end of entire nav map
        !          1360: sub BEGIN_MAP { return 1; }    # begining of a new map
        !          1361: sub END_MAP { return 2; }      # end of the map
        !          1362: sub BEGIN_BRANCH { return 3; } # beginning of a branch
        !          1363: sub END_BRANCH { return 4; }   # end of a branch
        !          1364: 
        !          1365: # Params: nav map, start resource, end resource, filter, condition, 
        !          1366: # already seen hash ref
        !          1367: 
        !          1368: sub new {
        !          1369:     # magic invocation to create a class instance
        !          1370:     my $proto = shift;
        !          1371:     my $class = ref($proto) || $proto;
        !          1372:     my $self = {};
        !          1373: 
        !          1374:     $self->{NAV_MAP} = shift;
        !          1375:     return undef unless ($self->{NAV_MAP});
        !          1376: 
        !          1377:     # Handle the parameters
        !          1378:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
        !          1379:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
        !          1380: 
        !          1381:     # If the given resources are just the ID of the resource, get the
        !          1382:     # objects
        !          1383:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
        !          1384:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
        !          1385:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
        !          1386:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
        !          1387: 
        !          1388:     $self->{FILTER} = shift;
        !          1389: 
        !          1390:     # A hash, used as a set, of resource already seen
        !          1391:     $self->{ALREADY_SEEN} = shift;
        !          1392:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
        !          1393: 
        !          1394:     # Flag: Have we started yet? If not, the first action is to return BEGIN_MAP.
        !          1395:     $self->{STARTED} = 0;
        !          1396: 
        !          1397:     # Should we continue calling the recursive iterator, if any?
        !          1398:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
        !          1399:     # The recursive iterator, if any
        !          1400:     $self->{RECURSIVE_ITERATOR} = undef;
        !          1401:     # Are we recursing on a map, or a branch?
        !          1402:     $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
        !          1403:     # And the count of how deep it is, so that this iterator can keep track of
        !          1404:     # when to pick back up again.
        !          1405:     $self->{RECURSIVE_DEPTH} = 0;
        !          1406: 
        !          1407:     # For keeping track of our branches, we maintain our own stack
        !          1408:     $self->{BRANCH_STACK} = [];
        !          1409:     # If the size shrinks, we exhausted a branch
        !          1410:     $self->{BRANCH_STACK_SIZE} = 0;
        !          1411:     $self->{BRANCH_DEPTH} = 0;
        !          1412: 
        !          1413:     # For returning two things in a forced sequence
        !          1414:     $self->{FORCE_NEXT} = undef;
        !          1415: 
        !          1416:     # Start with the first resource
        !          1417:     push @{$self->{BRANCH_STACK}}, $self->{FIRST_RESOURCE};
        !          1418:     $self->{BRANCH_STACK_SIZE} = 1;
        !          1419: 
        !          1420:     bless($self);
        !          1421:     return $self;
        !          1422: }
        !          1423: 
        !          1424: # Note... this function is *touchy*. I strongly recommend tracing
        !          1425: # through it with the debugger a few times on a non-trivial map before
        !          1426: # modifying it. Order is *everything*.
        !          1427: sub next {
        !          1428:     my $self = shift;
        !          1429:     
        !          1430:     # Iterator logic goes here
        !          1431: 
        !          1432:     # Is this return value pre-determined?
        !          1433:     if (defined($self->{FORCE_NEXT})) {
        !          1434:         my $tmp = $self->{FORCE_NEXT};
        !          1435:         $self->{FORCE_NEXT} = undef;
        !          1436:         return $tmp;
        !          1437:     }
        !          1438: 
        !          1439:     # Are we using a recursive iterator? If so, pull from that and
        !          1440:     # watch the depth; we want to resume our level at the correct time.
        !          1441:     if ($self->{RECURSIVE_ITERATOR_FLAG})
        !          1442:     {
        !          1443:         # grab the next from the recursive iterator
        !          1444:         my $next = $self->{RECURSIVE_ITERATOR}->next();
        !          1445:         
        !          1446:         # is it a begin or end map? Update depth if so
        !          1447:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
        !          1448:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
        !          1449: 
        !          1450:         # Are we back at depth 0? If so, stop recursing.
        !          1451:         if ($self->{RECURSIVE_DEPTH} == 0) {
        !          1452:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
        !          1453:         }
        !          1454:         
        !          1455:         return $next;
        !          1456:     }
        !          1457: 
        !          1458:     # Is there a current resource to grab? If not, then return
        !          1459:     # END_BRANCH and END_MAP in succession.
        !          1460:     if (scalar(@{$self->{BRANCH_STACK}}) == 0) {
        !          1461:         if ($self->{BRANCH_DEPTH} > 0) {
        !          1462:             $self->{FORCE_NEXT} = $self->END_MAP();
        !          1463: 	    $self->{BRANCH_DEPTH}--;
        !          1464:             return $self->END_BRANCH();
        !          1465:         } else {
        !          1466:             return $self->END_MAP();
        !          1467:         }
        !          1468:     }
        !          1469: 
        !          1470:     # Have we not yet begun? If not, return BEGIN_MAP and 
        !          1471:     # remember that we've started.
        !          1472:     if ( !$self->{STARTED} ) {
        !          1473:         $self->{STARTED} = 1;
        !          1474:         return $self->BEGIN_MAP;
        !          1475:     }
        !          1476: 
        !          1477:     # Did the branch stack shrink since last run? If so,
        !          1478:     # we exhausted a branch last time, therefore, we're about
        !          1479:     # to start a new one. (We know because we already checked to see
        !          1480:     # if the stack was empty.)
        !          1481:     if ( scalar (@{$self->{BRANCH_STACK}}) < $self->{BRANCH_STACK_SIZE}) {
        !          1482: 	$self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
        !          1483: 	$self->{BRANCH_DEPTH}++;
        !          1484: 	return $self->BEGIN_BRANCH();
        !          1485:     }
        !          1486: 
        !          1487:     # Get the next resource in the branch
        !          1488:     $self->{HERE} = pop @{$self->{BRANCH_STACK}};
        !          1489:     # We always return it after this point and never before
        !          1490:     # (proof: look at just the return statements), so we
        !          1491:     # remember that we've seen this.
        !          1492:     $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
        !          1493:     
        !          1494:     # Are we at the utter end? If so, return the END_NAV_MAP marker.
        !          1495:     if ($self->{HERE} == $self->{NAV_MAP}->finishResource() ) {
        !          1496: 	$self->{FORCE_NEXT} = $self->END_NAV_MAP;
        !          1497:         return $self->{HERE};
        !          1498:     }
        !          1499:     
        !          1500:     # Remember the size for comparision next time.
        !          1501:     $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
        !          1502: 
        !          1503:     # Get the next possible resources
        !          1504:     my $nextUnfiltered = $self->{HERE}->getNext();
        !          1505:     my $next = [];
        !          1506: 
        !          1507:     # filter the next possibilities to remove things we've 
        !          1508:     # already seen
        !          1509:     foreach (@$nextUnfiltered) {
        !          1510: 	if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
        !          1511: 	    push @$next, $_;
        !          1512: 	}
        !          1513:     }
        !          1514: 
        !          1515:     # Handle branch cases:
        !          1516:     # Nothing is available next: BRANCH_END
        !          1517:     # 1 thing next: standard non-branch
        !          1518:     # 2+ things next: have some branches
        !          1519:     my $nextCount = scalar(@$next);
        !          1520:     if ($nextCount == 0) {
        !          1521: 	# Return this and on the next run, close the branch up if we're 
        !          1522: 	# in a branch
        !          1523: 	if ($self->{BRANCH_DEPTH} > 0 ) {
        !          1524: 	    $self->{FORCE_NEXT} = $self->END_BRANCH();
        !          1525: 	    $self->{BRANCH_DEPTH}--;
        !          1526: 	}
        !          1527:         return $self->{HERE};
        !          1528:     }
        !          1529:     
        !          1530:     while (@$next) {
        !          1531:         # copy the next possibilities over to the branch stack
        !          1532:         # in the right order
        !          1533: 	push @{$self->{BRANCH_STACK}}, shift @$next;
        !          1534:     }
        !          1535: 
        !          1536:     if ($nextCount >= 2) {
        !          1537: 	$self->{FORCE_NEXT} = $self->BEGIN_BRANCH();
        !          1538: 	$self->{BRANCH_DEPTH}++;
        !          1539:         return $self->{HERE}; 
        !          1540:     }
        !          1541: 
        !          1542:     # If this is a map and we want to recurse down it... (not filtered out)
        !          1543:     if ($self->{HERE}->is_map() && 
        !          1544:          !defined($self->{FILTER}->{$self->{HERE}->map_pc()})) { 
        !          1545:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
        !          1546:         my $firstResource = $self->{HERE}->map_start();
        !          1547:         my $finishResource = $self->{HERE}->map_finish();
        !          1548: 
        !          1549:         # Odd perl syntax here; $self->new allows one to create a new iterator
        !          1550:         # can't figure out how to ref this package directly correctly
        !          1551:         # isn't MAIN::new, __PACKAGE__::new or Apache::lonnavmaps::iterator->new
        !          1552:         $self->{RECURSIVE_ITERATOR} =
        !          1553:           Apache::lonnavmaps::iterator->new ($self->{NAV_MAP}, $firstResource, 
        !          1554:                      $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN});
        !          1555: 	# prime the new iterator with the first resource
        !          1556: 	#push @{$self->{RECURSIVE_ITERATOR}->{BRANCH_STACK}}, $firstResource;
        !          1557:     }
        !          1558: 
        !          1559:     return $self->{HERE};
        !          1560: }
        !          1561: 
        !          1562: =pod
        !          1563: 
        !          1564: The other method available on the iterator is B<getStack>, which returns an array populated with the current 'stack' of maps, as references to the resource objects. Example: This is useful when making the navigation map, as we need to check whether we are under a page map to see if we need to link directly to the resource, or to the page. The first elements in the array will correspond to the top of the stack (most inclusive map).
        !          1565: 
        !          1566: =cut
        !          1567: 
        !          1568: sub getStack {
        !          1569:     my $self=shift;
        !          1570: 
        !          1571:     my @stack;
        !          1572: 
        !          1573:     $self->populateStack(\@stack);
        !          1574: 
        !          1575:     return \@stack;
        !          1576: }
        !          1577: 
        !          1578: # Private method: Calls the iterators recursively to populate the stack.
        !          1579: sub populateStack {
        !          1580:     my $self=shift;
        !          1581:     my $stack = shift;
        !          1582: 
        !          1583:     push @$stack, $self->{HERE};
        !          1584: 
        !          1585:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
        !          1586:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
        !          1587:     }
        !          1588: }
        !          1589: 
1.1       www      1590: 1;
1.2       www      1591: 
1.51    ! bowersj2 1592: package Apache::lonnavmaps::resource;
        !          1593: 
        !          1594: use Apache::lonnet;
        !          1595: 
        !          1596: =pod
        !          1597: 
        !          1598: =head1 Object: resource
        !          1599: 
        !          1600: A resource object encapsulates a resource in a resource map, allowing easy manipulation of the resource, querying the properties of the resource (including user properties), and represents a reference that can be used as the canonical representation of the resource by lonnavmap clients like renderers.
        !          1601: 
        !          1602: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
        !          1603: 
        !          1604: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
        !          1605: 
        !          1606: =head2 Public Members
        !          1607: 
        !          1608: resource objects have a hash called DATA ($resourceRef->{DATA}) that you can store whatever you want in. This allows you to easily do two-pass algorithms without worrying about managing your own resource->data hash.
        !          1609: 
        !          1610: =head2 Methods
        !          1611: 
        !          1612: =over 4
        !          1613: 
        !          1614: =item * B<new>($navmapRef, $idString): The first arg is a reference to the parent navmap object. The second is the idString of the resource itself. 
        !          1615: 
        !          1616: =cut
        !          1617: 
        !          1618: sub new {
        !          1619:     # magic invocation to create a class instance
        !          1620:     my $proto = shift;
        !          1621:     my $class = ref($proto) || $proto;
        !          1622:     my $self = {};
        !          1623: 
        !          1624:     $self->{NAV_MAP} = shift;
        !          1625:     $self->{ID} = shift;
        !          1626: 
        !          1627:     # Store this new resource in the parent nav map's cache.
        !          1628:     $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
        !          1629: 
        !          1630:     # A hash that can be used by two-pass algorithms to store data
        !          1631:     # about this resource in. Not used by the resource object
        !          1632:     # directly.
        !          1633:     $self->{DATA} = {};
        !          1634:    
        !          1635:     bless($self);
        !          1636:     
        !          1637:     return $self;
        !          1638: }
        !          1639: 
        !          1640: =pod
        !          1641: 
        !          1642: =item * B<is_map>: Returns true if this is a map, false otherwise.
        !          1643: 
        !          1644: =item * B<title>: Returns title.
        !          1645: 
        !          1646: =item * B<type>: Returns the type of the resource, "start", "normal", or "finish". 
        !          1647: 
        !          1648: =item * B<problem>: Returns true if the resource is a problem type, false otherwise. (Looks at the extension on the src field.)
        !          1649: 
        !          1650: =back
        !          1651: 
        !          1652: =cut
        !          1653: 
        !          1654: # private function: simplify the NAV_HASH lookups we keep doing
        !          1655: # pass the name, and to automatically append my ID, pass a true val on the
        !          1656: # second param
        !          1657: sub navHash {
        !          1658:     my $self = shift;
        !          1659:     my $param = shift;
        !          1660:     my $id = shift;
        !          1661:     return $self->{NAV_MAP}->{NAV_HASH}->{$param . ($id?$self->{ID}:"")};
        !          1662: }
        !          1663: 
        !          1664: # These info functions can be used directly, as they don't return
        !          1665: # resource information.
        !          1666: sub title { my $self=shift; return $self->navHash("title_", 1); }
        !          1667: sub type { my $self=shift; return $self->navHash("type_", 1); }
        !          1668: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
        !          1669: # "to" can return a comma seperated list for branches
        !          1670: sub to { my $self=shift; return $self->navHash("to_", 1); }
        !          1671: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
        !          1672: sub src { 
        !          1673:     my $self=shift;
        !          1674:     return $self->navHash("src_", 1);
        !          1675: }
        !          1676: sub symb {
        !          1677:     my $self=shift;
        !          1678:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
        !          1679:     my $symbSrc = &Apache::lonnet::declutter($self->src());
        !          1680:     return &Apache::lonnet::declutter(
        !          1681:          $self->navHash('map_id_'.$first)) 
        !          1682:         . '___' . $second . '___' . $symbSrc;
        !          1683: }
        !          1684: sub is_problem {
        !          1685:     my $self=shift;
        !          1686:     my $src = $self->src();
        !          1687:     return ($src =~ /problem$/);
        !          1688: }
        !          1689: sub is_html {
        !          1690:     my $self=shift;
        !          1691:     my $src = $self->src();
        !          1692:     return ($src =~ /html$/);
        !          1693: }
        !          1694: sub is_sequence {
        !          1695:     my $self=shift;
        !          1696:     my $src = $self->src();
        !          1697:     return ($src =~ /sequence$/);
        !          1698: }
        !          1699: sub is_page {
        !          1700:     my $self=shift;
        !          1701:     my $src = $self->src();
        !          1702:     return ($src =~ /page$/);
        !          1703: }
        !          1704: 
        !          1705: 
        !          1706: 
        !          1707: 
        !          1708: # Move this into POD: In order to use these correctly, courseopt
        !          1709: # and useropt need to be generated
        !          1710: sub parmval {
        !          1711:     my $self = shift;
        !          1712:     my $what = shift;
        !          1713:     my $part = shift || "0";
        !          1714:     return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
        !          1715: }
        !          1716: 
        !          1717: #####
        !          1718: # Map Queries
        !          1719: #####
        !          1720: 
        !          1721: # These methods relate to whether or not the resource is a map, and the 
        !          1722: # attributes of that map.
        !          1723: 
        !          1724: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
        !          1725: sub map_finish {
        !          1726:     my $self = shift;
        !          1727:     my $src = $self->src();
        !          1728:     my $res = $self->navHash("map_finish_$src", 0);
        !          1729:     $res = $self->{NAV_MAP}->getById($res);
        !          1730:     return $res;
        !          1731: }
        !          1732: sub map_start {
        !          1733:     my $self = shift;
        !          1734:     my $src = $self->src();
        !          1735:     my $res = $self->navHash("map_start_$src", 0);
        !          1736:     $res = $self->{NAV_MAP}->getById($res);
        !          1737:     return $res;
        !          1738: }
        !          1739: sub map_pc {
        !          1740:     my $self = shift;
        !          1741:     my $src = $self->src();
        !          1742:     return $self->navHash("map_pc_$src", 0);
        !          1743: }
        !          1744: sub map_type {
        !          1745:     my $self = shift;
        !          1746:     my $pc = $self->map_pc();
        !          1747:     return $self->navHash("map_type_$pc", 0);
        !          1748: }
        !          1749: 
        !          1750: 
        !          1751: 
        !          1752: #####
        !          1753: # Property queries
        !          1754: #####
        !          1755: 
        !          1756: # These functions will be responsible for returning the CORRECT
        !          1757: # VALUE for the parameter, no matter what. So while they may look
        !          1758: # like direct calls to parmval, they can be more then that.
        !          1759: # So, for instance, the duedate function should use the "duedatetype"
        !          1760: # information, rather then the resource object user.
        !          1761: 
        !          1762: =pod
        !          1763: 
        !          1764: =head2 Resource Parameters
        !          1765: 
        !          1766: In order to use the resource parameters correctly, the nav map must have been instantiated with genCourseAndUserOptions set to true, so the courseopt and useropt is read correctly. Then, you can call these functions to get the relevant parameters for the resource. Each function defaults to "part 0", but can be directed to another part by passing the part as the second parameter.
        !          1767: 
        !          1768: These methods are responsible for getting the parameter correct, not merely reflecting the contents of the GDBM hashes. As we move towards dates relative to other dates, these methods should be updated to reflect that. (Then, anybody using these methods won't have to update their code.)
        !          1769: 
        !          1770: =over 4
        !          1771: 
        !          1772: =item * B<acc>: Get the Client IP/Name Access Control information.
        !          1773: 
        !          1774: =item * B<answerdate>: Get the answer-reveal date for the problem.
        !          1775: 
        !          1776: =item * B<duedate>: Get the due date for the problem.
        !          1777: 
        !          1778: =item * B<tries>: Get the number of tries the student has used on the problem.
        !          1779: 
        !          1780: =item * B<maxtries>: Get the number of max tries allowed.
        !          1781: 
        !          1782: =item * B<opendate>: Get the open date for the problem.
        !          1783: 
        !          1784: =item * B<sig>: Get the significant figures setting.
        !          1785: 
        !          1786: =item * B<tol>: Get the tolerance for the problem.
        !          1787: 
        !          1788: =item * B<type>: Get the question type for the problem.
        !          1789: 
        !          1790: =item * B<weight>: Get the weight for the problem.
        !          1791: 
        !          1792: =back
        !          1793: 
        !          1794: =cut
        !          1795: 
        !          1796: sub acc {
        !          1797:     (my $self, my $part) = @_;
        !          1798:     return $self->parmval("acc", $part);
        !          1799: }
        !          1800: sub answerdate {
        !          1801:     (my $self, my $part) = @_;
        !          1802:     # Handle intervals
        !          1803:     if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
        !          1804:         return $self->duedate($part) + 
        !          1805:             $self->parmval("answerdate", $part);
        !          1806:     }
        !          1807:     return $self->parmval("answerdate", $part);
        !          1808: }
        !          1809: sub duedate {
        !          1810:     (my $self, my $part) = @_;
        !          1811:     return $self->parmval("duedate", $part);
        !          1812: }
        !          1813: sub maxtries {
        !          1814:     (my $self, my $part) = @_;
        !          1815:     return $self->parmval("maxtries", $part);
        !          1816: }
        !          1817: sub opendate {
        !          1818:     (my $self, my $part) = @_;
        !          1819:     if ($self->parmval("opendate.type", $part) eq 'date_interval') {
        !          1820:         return $self->duedate($part) -
        !          1821:             $self->parmval("opendate", $part);
        !          1822:     }
        !          1823:     return $self->parmval("opendate");
        !          1824: }
        !          1825: sub sig {
        !          1826:     (my $self, my $part) = @_;
        !          1827:     return $self->parmval("sig", $part);
        !          1828: }
        !          1829: sub tol {
        !          1830:     (my $self, my $part) = @_;
        !          1831:     return $self->parmval("tol", $part);
        !          1832: }
        !          1833: sub type {
        !          1834:     (my $self, my $part) = @_;
        !          1835:     return $self->parmval("type", $part);
        !          1836: }
        !          1837: sub weight {
        !          1838:     (my $self, my $part) = @_;
        !          1839:     return $self->parmval("weight", $part);
        !          1840: }
        !          1841: sub tries {
        !          1842:     my $self = shift;
        !          1843:     my $part = shift;
        !          1844:     $part = '0' if (!defined($part));
        !          1845: 
        !          1846:     # Make sure return hash is loaded, should error check
        !          1847:     $self->getReturnHash();
        !          1848:     
        !          1849:     my $tries = $self->{RETURN_HASH}->{'resource.'.$part.'.tries'};
        !          1850:     if (!defined($tries)) {return '0';}
        !          1851:     return $tries;
        !          1852: }
        !          1853: 
        !          1854: # Multiple things need this
        !          1855: sub getReturnHash {
        !          1856:     my $self = shift;
        !          1857:     
        !          1858:     if (!defined($self->{RETURN_HASH})) {
        !          1859:         my %tmpHash = &Apache::lonnet::restore($self->symb());
        !          1860:         $self->{RETURN_HASH} = \%tmpHash;
        !          1861:     }
        !          1862: }       
        !          1863: 
        !          1864: ######
        !          1865: # Status queries
        !          1866: ######
        !          1867: 
        !          1868: # These methods query the status of problems.
        !          1869: 
        !          1870: # If we need to count parts, this function determines the number of
        !          1871: # parts from the metadata. When called, it returns a reference to a list
        !          1872: # of strings corresponding to the parts. (Thus, using it in a scalar context
        !          1873: # tells you how many parts you have in the problem:
        !          1874: # $partcount = scalar($resource->countParts());
        !          1875: # Don't use $self->{PARTS} directly because you don't know if it's been
        !          1876: # computed yet.
        !          1877: 
        !          1878: =pod
        !          1879: 
        !          1880: =head2 Resource misc
        !          1881: 
        !          1882: Misc. functions for the resource.
        !          1883: 
        !          1884: =over 4
        !          1885: 
        !          1886: =item * B<parts>(): Returns a list reference containing sorted strings corresponding to each part of the problem. To count the number of parts, use the list in a scalar context, and subtract one if greater then two. (One part problems have a part 0. Multi-parts have a part 0, plus a part for each part. You may or may not wish to filter out part 0.)
        !          1887: 
        !          1888: =item * B<countParts>(): Returns the number of parts of the problem a student can answer. Thus, for single part problems, returns 1. For multipart, it returns the number of parts in the problem, not including psuedo-part 0.
        !          1889: 
        !          1890: =back
        !          1891: 
        !          1892: =cut
        !          1893: 
        !          1894: sub parts {
        !          1895:     my $self = shift;
        !          1896: 
        !          1897:     $self->extractParts();
        !          1898:     return $self->{PARTS};
        !          1899: }
        !          1900: 
        !          1901: sub countParts {
        !          1902:     my $self = shift;
        !          1903:     
        !          1904:     my $parts = $self->parts();
        !          1905:     
        !          1906:     if (scalar(@{$parts}) == 1) { return 1;}
        !          1907: 
        !          1908:     return scalar(@{$parts}) - 1;
        !          1909: }
        !          1910: 
        !          1911: # Private function: Extracts the parts information and saves it
        !          1912: sub extractParts { 
        !          1913:     my $self = shift;
        !          1914:     
        !          1915:     return if ($self->{PARTS});
        !          1916: 
        !          1917:     $self->{PARTS} = [];
        !          1918: 
        !          1919:     # Retrieve part count
        !          1920:     foreach (split(/\,/,&Apache::lonnet::metadata($self->src(), 
        !          1921:                                                   'allpossiblekeys'))) {
        !          1922:         if ($_ =~ /^parameter\_(.*)\_opendate$/) {
        !          1923:             push @{$self->{PARTS}}, $1;
        !          1924:         }
        !          1925:     }
        !          1926:     
        !          1927:     # Is this possible to do in one line? - Jeremy
        !          1928:     my @sortedParts = sort @{$self->{PARTS}};
        !          1929:     $self->{PARTS} = \@sortedParts;
        !          1930: 
        !          1931:     return;
        !          1932: }
        !          1933: 
        !          1934: =pod
        !          1935: 
        !          1936: =head2 Resource Status
        !          1937: 
        !          1938: Problem resources have status information, reflecting their various dates and completion statuses. You can obtain this information and import symbolic constants to represent the status.
        !          1939: 
        !          1940: There are two aspects to the status: the date-related information and the completion information.
        !          1941: 
        !          1942: Idiomatic usage of these two methods would probably look something like
        !          1943: 
        !          1944:  foreach ($resource->parts()) {
        !          1945:     my $dateStatus = $resource->getDateStatus($_);
        !          1946:     my $completionStatus = $resource->getCompletionStatus($_);
        !          1947: 
        !          1948:     ... use it here ...
        !          1949:  }
        !          1950: 
        !          1951: =over 4
        !          1952: 
        !          1953: =item * B<getDateStatus>($part): ($part defaults to 0). A convenience function that returns a symbolic constant telling you about the date status of the part, with the following possible results:
        !          1954: 
        !          1955: =back
        !          1956: 
        !          1957: B<Date Codes>
        !          1958: 
        !          1959: =over 4
        !          1960: 
        !          1961: =item * B<OPEN_LATER>: The problem will be opened later.
        !          1962: 
        !          1963: =item * B<OPEN>: Open and not yet due.
        !          1964: 
        !          1965: =item * B<PAST_DUE>: The due date has passed, but the answer date has not yet arrived.
        !          1966: 
        !          1967: =item * B<ANSWER_OPEN>: The answer date is here.
        !          1968: 
        !          1969: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
        !          1970: 
        !          1971: =back
        !          1972: 
        !          1973: =cut
        !          1974: 
        !          1975: # Apparently the compiler optimizes these into constants automatically
        !          1976: sub OPEN_LATER      { return 0; }
        !          1977: sub OPEN            { return 1; }
        !          1978: sub PAST_DUE        { return 2; }
        !          1979: sub ANSWER_OPEN     { return 3; }
        !          1980: sub NETWORK_FAILURE { return 100; }
        !          1981: 
        !          1982: sub getDateStatus {
        !          1983:     my $self = shift;
        !          1984:     my $part = shift;
        !          1985:     $part = "0" if (!defined($part));
        !          1986:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
        !          1987: 
        !          1988:     my $now = time();
        !          1989: 
        !          1990:     my $o = $now - $self->opendate($part);
        !          1991:     my $d = $now - $self->duedate($part);
        !          1992:     my $a = $now - $self->answerdate($part);
        !          1993: 
        !          1994:     if ($o < 0) {return $self->OPEN_LATER};
        !          1995:     if ($d < 0) {return $self->OPEN};
        !          1996:     if ($a < 0) {return $self->PAST_DUE};
        !          1997:     return $self->ANSWER_OPEN;
        !          1998: }
        !          1999: 
        !          2000: =pod
        !          2001: 
        !          2002: B<>
        !          2003: 
        !          2004: =over 4
        !          2005: 
        !          2006: =item * B<getCompletionStatus>($part): ($part defaults to 0.) A convenience function that returns a symbolic constant telling you about the completion status of the part, with the following possible results:
        !          2007: 
        !          2008: =back 
        !          2009: 
        !          2010: B<Completion Codes>
        !          2011: 
        !          2012: =over 4
        !          2013: 
        !          2014: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
        !          2015: 
        !          2016: =item * B<INCORRECT>: Attempted, but wrong by student.
        !          2017: 
        !          2018: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
        !          2019: 
        !          2020: =item * B<CORRECT>: Correct or correct by instructor.
        !          2021: 
        !          2022: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
        !          2023: 
        !          2024: =item * B<EXCUSED>: Excused. Not yet implemented.
        !          2025: 
        !          2026: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
        !          2027: 
        !          2028: =back
        !          2029: 
        !          2030: =cut
        !          2031: 
        !          2032: sub NOT_ATTEMPTED         { return 0; }
        !          2033: sub INCORRECT             { return 1; }
        !          2034: sub INCORRECT_BY_OVERRIDE { return 2; }
        !          2035: sub CORRECT               { return 3; }
        !          2036: sub CORRECT_BY_OVERRIDE   { return 4; }
        !          2037: sub EXCUSED               { return 5; }
        !          2038: 
        !          2039: sub getCompletionStatus {
        !          2040:     my $self = shift;
        !          2041:     my $part = shift;
        !          2042:     $part = "0" if (!defined($part));
        !          2043:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
        !          2044: 
        !          2045:     # Make sure return hash exists
        !          2046:     $self->getReturnHash();
        !          2047:     
        !          2048:     my $status = $self->{RETURN_HASH}->{'resource.'.$part.'.solved'};
        !          2049: 
        !          2050:     # Left as seperate if statements in case we ever do more with this
        !          2051:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
        !          2052:     if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
        !          2053:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
        !          2054:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
        !          2055:     if ($status eq 'excused') {return $self->EXCUSED; }
        !          2056:     return $self->NOT_ATTEMPTED;
        !          2057: }
        !          2058: 
        !          2059: =pod
        !          2060: 
        !          2061: B<Composite Status>
        !          2062: 
        !          2063: Along with directly returning the date or completion status, the resource object includes a convenience function B<status>() that will combine the two status tidbits into one composite status that can represent the status of the resource as a whole. The precise logic is documented in the comments of the status method. The following results may be returned, all available as methods on the resource object ($res->NETWORK_FAILURE()):
        !          2064: 
        !          2065: =over 4
        !          2066: 
        !          2067: =item * NETWORK_FAILURE: The network has failed and the information is not available.
        !          2068: 
        !          2069: =item * CORRECT: For any reason at all, the part is considered correct.
        !          2070: 
        !          2071: =item * EXCUSED: For any reason at all, the problem is excused.
        !          2072: 
        !          2073: =item * PAST_DUE: The problem is past due, and not considered correct.
        !          2074: 
        !          2075: =item * ANSWER_OPEN: The problem is past due, not correct, and the answer is now available.
        !          2076: 
        !          2077: =item * OPEN_LATER: The problem is not yet open.
        !          2078: 
        !          2079: =item * TRIES_LEFT: The problem is open, has been tried, is not correct, but there are tries left.
        !          2080: 
        !          2081: =item * INCORRECT: The problem is open, and all tries have been used without getting the correct answer.
        !          2082: 
        !          2083: =item * OPEN: The item is open and not yet tried.
        !          2084: 
        !          2085: =back
        !          2086: 
        !          2087: =cut
        !          2088: 
        !          2089: sub TRIES_LEFT { return 10; }
        !          2090: 
        !          2091: sub status {
        !          2092:     my $self = shift;
        !          2093:     my $part = shift;
        !          2094:     if (!defined($part)) { $part = "0"; }
        !          2095:     my $completionStatus = $self->getCompletionStatus($part);
        !          2096:     my $dateStatus = $self->getDateStatus($part);
        !          2097: 
        !          2098:     # What we have is a two-dimensional matrix with 4 entries on one
        !          2099:     # dimension and 5 entries on the other, which we want to colorize,
        !          2100:     # plus network failure.
        !          2101: 
        !          2102:     # Don't colorize on network failure.
        !          2103:     if ($completionStatus == NETWORK_FAILURE()) { return $self->NETWORK_FAILURE(); }
        !          2104: 
        !          2105:     # There are a few whole rows we can dispose of:
        !          2106:     # If the problem is CORRECT, color it green no matter what
        !          2107:     if ($completionStatus == CORRECT() ||
        !          2108:         $completionStatus == CORRECT_BY_OVERRIDE() ) {
        !          2109:         return $self->CORRECT(); # Return a nice green.
        !          2110:     }
        !          2111: 
        !          2112:     # If it's EXCUSED, then return something no matter what
        !          2113:     if ($completionStatus == EXCUSED()) {
        !          2114:         return $self->EXCUSED(); # return a nice blue
        !          2115:     }
        !          2116: 
        !          2117:     # Now we're down to a 3 (incorrect, incorrect_override, not_attempted)
        !          2118:     # by 4 matrix (date status).
        !          2119: 
        !          2120:     # If it's Past Due and we didn't bail earlier because it's correct,
        !          2121:     # color it orange. (Red is sort inappropriate; too drastic a color
        !          2122:     # for something the student can't fix.
        !          2123:     if ($dateStatus == PAST_DUE()) {
        !          2124:         return $self->PAST_DUE(); # return orange
        !          2125:     }
        !          2126: 
        !          2127:     if ($dateStatus == ANSWER_OPEN()) {
        !          2128:         return $self->ANSWER_OPEN();
        !          2129:     }
        !          2130: 
        !          2131:     # Now: (incorrect, incorrect_override, not_attempted) x 
        !          2132:     # (open_later), (open)
        !          2133:     
        !          2134:     # If it's open later, then don't colorize
        !          2135:     if ($dateStatus == OPEN_LATER()) {
        !          2136:         return $self->OPEN_LATER();
        !          2137:     }
        !          2138: 
        !          2139:     # If it's WRONG...
        !          2140:     if ($completionStatus == INCORRECT() || $completionStatus == INCORRECT_BY_OVERRIDE()) {
        !          2141:         # and there are TRIES LEFT:
        !          2142:         if ($self->tries() < $self->maxtries()) {
        !          2143:             return $self->TRIES_LEFT(); # return red: The student can fix this
        !          2144:         }
        !          2145:         return $self->INCORRECT(); # otherwise, return orange; student can't fix this
        !          2146:     }
        !          2147: 
        !          2148:     # Otherwise, it's untried and open
        !          2149:     return $self->OPEN(); # Light yellow
        !          2150: }
        !          2151: 
        !          2152: =pod
        !          2153: 
        !          2154: =head2 Resource/Nav Map Navigation
        !          2155: 
        !          2156: =over 4
        !          2157: 
        !          2158: =item * B<getNext>(): Gets the next resource in the navmap after this one.
        !          2159: 
        !          2160: =cut
        !          2161: 
        !          2162: # For the simple single-link case, to get from a resource to the next
        !          2163: # resource, you need to look up the "to_" link in the nav hash, then
        !          2164: # follow that with the "goesto_" link.
        !          2165: 
        !          2166: sub getNext {
        !          2167:     my $self = shift;
        !          2168:     my $alreadySeenHash = shift;
        !          2169:     my @branches;
        !          2170:     my $to = $self->to();
        !          2171:     foreach my $branch ( split(/\,/, $to) ) {
        !          2172:         my $choice = $self->{NAV_MAP}->getById($branch);
        !          2173:         my $next = $choice->goesto();
        !          2174:         $next = $self->{NAV_MAP}->getById($next);
        !          2175: 
        !          2176:         # Don't remember it if we've already seen it
        !          2177:         if (!defined($alreadySeenHash) ||
        !          2178:             !defined($alreadySeenHash->{$next->{ID}})) {
        !          2179:                 push @branches, $next;
        !          2180:             }
        !          2181:     }
        !          2182:     return \@branches;
        !          2183: }
        !          2184: 
        !          2185: =pod
1.2       www      2186: 
1.51    ! bowersj2 2187: =back
1.2       www      2188: 
1.51    ! bowersj2 2189: =cut
1.2       www      2190: 
1.51    ! bowersj2 2191: 1;
1.2       www      2192: 
1.51    ! bowersj2 2193: __END__
1.2       www      2194: 
                   2195: 

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