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

1.72      bowersj2    1: 
1.2       www         2: # The LearningOnline Network with CAPA
                      3: # Navigate Maps Handler
1.1       www         4: #
1.88      bowersj2    5: # $Id: lonnavmaps.pm,v 1.81 2002/10/14 18:48:13 bowersj2 Exp $
1.20      albertel    6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
                     28: #
1.2       www        29: # (Page Handler
1.1       www        30: #
1.2       www        31: # (TeX Content Handler
1.1       www        32: #
1.2       www        33: # 05/29/00,05/30 Gerd Kortemeyer)
                     34: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
                     35: # 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        36: #
1.17      www        37: # 3/1/1,6/1,17/1,29/1,30/1,2/8,9/21,9/24,9/25 Gerd Kortemeyer
1.21      www        38: # YEAR=2002
                     39: # 1/1 Gerd Kortemeyer
                     40: #
1.2       www        41: 
1.1       www        42: package Apache::lonnavmaps;
                     43: 
                     44: use strict;
1.2       www        45: use Apache::Constants qw(:common :http);
                     46: use Apache::lonnet();
1.18      albertel   47: use Apache::loncommon();
1.2       www        48: use GDBM_File;
1.73      bowersj2   49: use POSIX qw (floor strftime);
1.2       www        50: 
                     51: # -------------------------------------------------------------- Module Globals
                     52: my %hash;
                     53: my @rows;
                     54: 
1.10      www        55: #
                     56: # These cache hashes need to be independent of user, resource and course
                     57: # (user and course can/should be in the keys)
                     58: #
                     59: 
                     60: my %courserdatas;
                     61: my %userrdatas;
                     62: 
                     63: #
                     64: # These global hashes are dependent on user, course and resource, 
                     65: # and need to be initialized every time when a sheet is calculated
                     66: #
                     67: my %courseopt;
                     68: my %useropt;
                     69: my %parmhash;
                     70: 
1.47      bowersj2   71: # This parameter keeps track of whether obtaining the user's information
                     72: # failed, which the colorizer in astatus can use
                     73: my $networkFailedFlag = 0;
                     74: 
1.2       www        75: # ------------------------------------------------------------------ Euclid gcd
                     76: 
                     77: sub euclid {
                     78:     my ($e,$f)=@_;
                     79:     my $a; my $b; my $r;
                     80:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
                     81:     while ($r!=0) {
1.51      bowersj2   82:         $a=$b; $b=$r;
1.2       www        83:         $r=$a%$b;
                     84:     }
                     85:     return $b;
                     86: }
                     87: 
1.10      www        88: # --------------------------------------------------------------------- Parmval
                     89: 
                     90: # -------------------------------------------- Figure out a cascading parameter
                     91: #
                     92: # For this function to work
                     93: #
                     94: # * parmhash needs to be tied
                     95: # * courseopt and useropt need to be initialized for this user and course
                     96: #
                     97: 
                     98: sub parmval {
                     99:     my ($what,$symb)=@_;
                    100:     my $cid=$ENV{'request.course.id'};
                    101:     my $csec=$ENV{'request.course.sec'};
                    102:     my $uname=$ENV{'user.name'};
                    103:     my $udom=$ENV{'user.domain'};
                    104: 
                    105:     unless ($symb) { return ''; }
                    106:     my $result='';
                    107: 
                    108:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                    109: 
                    110: # ----------------------------------------------------- Cascading lookup scheme
1.24      albertel  111:     my $rwhat=$what;
                    112:     $what=~s/^parameter\_//;
                    113:     $what=~s/\_/\./;
                    114: 
                    115:     my $symbparm=$symb.'.'.$what;
                    116:     my $mapparm=$mapname.'___(all).'.$what;
                    117:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
                    118: 
                    119:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
                    120:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
                    121:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
                    122: 
                    123:     my $courselevel= $usercourseprefix.'.'.$what;
                    124:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
                    125:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
                    126: 
                    127: # ---------------------------------------------------------- first, check user
1.57      albertel  128:     if (defined($uname)) {
                    129:         if (defined($useropt{$courselevelr})) { return $useropt{$courselevelr}; }
                    130:         if (defined($useropt{$courselevelm})) { return $useropt{$courselevelm}; }
                    131:         if (defined($useropt{$courselevel})) { return $useropt{$courselevel}; }
1.24      albertel  132:     }
1.10      www       133: 
1.24      albertel  134: # ------------------------------------------------------- second, check course
1.57      albertel  135:     if (defined($csec)) {
                    136:         if (defined($courseopt{$seclevelr})) { return $courseopt{$seclevelr}; }
                    137:         if (defined($courseopt{$seclevelm})) { return $courseopt{$seclevelm}; }
                    138:         if (defined($courseopt{$seclevel})) { return $courseopt{$seclevel}; }
1.24      albertel  139:     }
1.10      www       140: 
1.57      albertel  141:     if (defined($courseopt{$courselevelr})) { return $courseopt{$courselevelr}; }
                    142:     if (defined($courseopt{$courselevelm})) { return $courseopt{$courselevelm}; }
                    143:     if (defined($courseopt{$courselevel})) { return $courseopt{$courselevel}; }
1.10      www       144: 
1.24      albertel  145: # ----------------------------------------------------- third, check map parms
1.10      www       146: 
1.24      albertel  147:     my $thisparm=$parmhash{$symbparm};
1.57      albertel  148:     if (defined($thisparm)) { return $thisparm; }
1.10      www       149: 
1.24      albertel  150: # ----------------------------------------------------- fourth , check default
1.10      www       151: 
1.25      albertel  152:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1.57      albertel  153:     if (defined($default)) { return $default}
1.25      albertel  154: 
                    155: # --------------------------------------------------- fifth , cascade up parts
                    156: 
                    157:     my ($space,@qualifier)=split(/\./,$rwhat);
                    158:     my $qualifier=join('.',@qualifier);
                    159:     unless ($space eq '0') {
1.51      bowersj2  160:         my ($part,$id)=split(/\_/,$space);
                    161:         if ($id) {
                    162:             my $partgeneral=&parmval($part.".$qualifier",$symb);
1.57      albertel  163:             if (defined($partgeneral)) { return $partgeneral; }
1.51      bowersj2  164:         } else {
                    165:             my $resourcegeneral=&parmval("0.$qualifier",$symb);
1.57      albertel  166:             if (defined($resourcegeneral)) { return $resourcegeneral; }
1.51      bowersj2  167:         }
1.25      albertel  168:     }
                    169:     return '';
1.10      www       170: }
                    171: 
                    172: 
                    173: 
1.9       www       174: # ------------------------------------------------------------- Find out status
1.25      albertel  175: # return codes
                    176: # tcode (timecode)
                    177: # 1: will open later
                    178: # 2: is open and not past due yet
                    179: # 3: is past due date
                    180: # 4: due in the next 24 hours
                    181: #
                    182: # code (curent solved status)
                    183: # 1: not attempted
                    184: # 2: attempted but wrong, or incorrect by instructor
                    185: # 3: solved or correct by instructor
1.29      albertel  186: # 4: partially correct (one or more parts correct)
                    187: # "excused" needs to be supported, but is not yet.
1.9       www       188: sub astatus {
                    189:     my $rid=shift;
1.29      albertel  190:     my $code=0;
1.9       www       191:     my $ctext='';
                    192:     $rid=~/(\d+)\.(\d+)/;
1.10      www       193:     my $symb=&Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.
1.51      bowersj2  194:         &Apache::lonnet::declutter($hash{'src_'.$rid});
1.15      www       195:     my %duedate=();
                    196:     my %opendate=();
                    197:     my %answerdate=();
1.25      albertel  198:     # need to always check part 0's open/due/answer status
1.51      bowersj2  199:     foreach (sort(split(/\,/,&Apache::lonnet::metadata($hash{'src_'.$rid},'allpo\ssiblekeys')))) {
1.15      www       200:         if ($_=~/^parameter\_(.*)\_opendate$/) {
1.51      bowersj2  201:             my $part=$1;
1.15      www       202:             $duedate{$part}=&parmval($part.'.duedate',$symb);
                    203:             $opendate{$part}=&parmval($part.'.opendate',$symb);
                    204:             $answerdate{$part}=&parmval($part.'.answerdate',$symb);
1.49      www       205:             if (&parmval($part.'.opendate.type',$symb) eq 'date_interval') {
1.51      bowersj2  206:                 $opendate{$part}=$duedate{$part}-$opendate{$part};
1.49      www       207:             }
1.50      www       208:             if (&parmval($part.'.answerdate.type',$symb) eq 'date_interval') {
1.49      www       209:                 $answerdate{$part}=$duedate{$part}+$answerdate{$part};
                    210:             }
1.15      www       211:         }
1.25      albertel  212:     }
1.11      www       213:     my $now=time;
                    214:     my $tcode=0;
1.16      www       215: 
                    216:     my %returnhash=&Apache::lonnet::restore($symb);
                    217: 
1.25      albertel  218:     foreach (sort(keys(%opendate))) {
1.51      bowersj2  219:         my $duedate=$duedate{$_};
                    220:         my $opendate=$opendate{$_};
                    221:         my $answerdate=$answerdate{$_};
                    222:         my $preface='';
                    223:         unless ($_ eq '0') { $preface=' Part: '.$_.' '; }
                    224:         if ($opendate) {
                    225:             if ($now<$duedate || (!$duedate)) {
                    226:                 unless ($tcode==4) { $tcode=2; }
                    227:                 if ($duedate) {
                    228:                     $ctext.=$preface.'Due: '.localtime($duedate);
                    229:                 } else {
                    230:                     $ctext.=$preface.'No Due Date';
                    231:                 }
                    232:                 if ($now<$opendate) {
                    233:                     unless ($tcode) { $tcode=1; }
                    234:                     $ctext.=$preface.'Open: '.localtime($opendate);
                    235:                 }
                    236:                 if ($duedate && $duedate-$now<86400) {
                    237:                     $tcode=4;
                    238:                     $ctext.=$preface.'Due: '.localtime($duedate);
                    239:                 }
                    240:             } else {
                    241:                 unless (($tcode==4) || ($tcode eq 2)) { $tcode=3; }
                    242:                 if ($now<$answerdate) {
                    243:                     $ctext.='Answer: '.localtime($duedate);
                    244:                 }
                    245:             }
                    246:         } else {
                    247:             unless (($tcode==2) || ($tcode==4)) { $tcode=1; }
                    248:         }
                    249:         
                    250:         my $status=$returnhash{'resource.'.$_.'.solved'};
                    251:         
                    252:         if ($status eq 'correct_by_student') {
                    253:             if ($code==0||$code==3) { $code=3; } else { $code=4; }
                    254:             $ctext.=' solved';
                    255:         } elsif ($status eq 'correct_by_override') {
                    256:             if ($code==0||$code==3) { $code=3; } else { $code=4; }
                    257:             $ctext.=' override';
                    258:         } elsif ($status eq 'incorrect_attempted') {
                    259:             if ($code!=4 && $code!=3) { $code=2; }
                    260:             if ($code==3) { $code=4; }
                    261:             $ctext.=' ('.
                    262:                 ($returnhash{'resource.'.$_.'.tries'}?
                    263:                  $returnhash{'resource.'.$_.'.tries'}:'0');
                    264:             my $numtries = &parmval($_.'.maxtries',$symb);
                    265:             if ($numtries) { $ctext.='/'.$numtries.' tries'; }
                    266:             $ctext.=')';
                    267:         } elsif ($status eq 'incorrect_by_override') {
                    268:             if ($code!=4 && $code!=3) { $code=2; }
                    269:             if ($code==3) { $code=4; }
                    270:             $ctext.=' override';
                    271:         } elsif ($status eq 'excused') {
                    272:             if ($code==0||$code==3) { $code=3; } else { $code=4; }
                    273:             $ctext.=' excused';
                    274:         } else {
                    275:             if ($code==0) { $code=1; }
                    276:         }
1.25      albertel  277:     }
1.16      www       278: 
1.11      www       279:     return 'p'.$code.$tcode.'"'.$ctext.'"';
1.9       www       280: }
                    281: 
1.2       www       282: 
1.31      albertel  283: sub addresource {
1.32      albertel  284:     my ($resource,$sofar,$rid,$showtypes,$indent,$linkid)=@_;
1.31      albertel  285:     if ($showtypes eq 'problems') {
1.51      bowersj2  286:         if ($resource!~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    287:             return;
                    288:         }
1.31      albertel  289:     }
                    290:     my $brepriv=&Apache::lonnet::allowed('bre',$resource);
                    291:     if ($hash{'src_'.$rid}) {
1.51      bowersj2  292:         if (($brepriv eq '2') || ($brepriv eq 'F')) {
                    293:             my $pprefix='';
                    294:             if ($resource=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    295:                 $pprefix=&astatus($rid);
                    296:             }
                    297:             $$sofar++;
                    298:             if ($indent) { $pprefix='i'.$indent.','.$pprefix; }
                    299:             if ($linkid) { $pprefix='l'.$linkid.','.$pprefix; }
                    300:             if (defined($rows[$$sofar])) {
                    301:                 $rows[$$sofar].='&'.$pprefix.$rid;
                    302:             } else {
                    303:                 $rows[$$sofar]=$pprefix.$rid;
                    304:             }
                    305:         }
1.31      albertel  306:     }
                    307: }
                    308: 
                    309: sub followlinks () {
1.32      albertel  310:     my ($rid,$sofar,$beenhere,$further,$showtypes,$indent,$linkid)=@_;
1.31      albertel  311:     my $mincond=1;
                    312:     my $next='';
                    313:     foreach (split(/\,/,$hash{'to_'.$rid})) {
1.51      bowersj2  314:         my $thiscond=
                    315:             &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
                    316:         if ($thiscond>=$mincond) {
                    317:             if ($next) {
                    318:                 $next.=','.$_.':'.$thiscond;
                    319:             } else {
                    320:                 $next=$_.':'.$thiscond;
                    321:             }
                    322:             if ($thiscond>$mincond) { $mincond=$thiscond; }
                    323:         }
1.31      albertel  324:     }
1.32      albertel  325:     my $col=0;
                    326:     &Apache::lonxml::debug("following links -$next-");
1.31      albertel  327:     foreach (split(/\,/,$next)) {
1.51      bowersj2  328:         my ($nextlinkid,$condval)=split(/\:/,$_);
                    329:         if ($condval>=$mincond) {
                    330:             my $now=&tracetable($sofar,$hash{'goesto_'.$nextlinkid},
                    331:                                 $beenhere,$showtypes,$indent,$linkid);
                    332:             if ($now>$further) {                
                    333:                 if ($col>0) {
                    334:                     my $string;
                    335:                     for(my $i=0;$i<$col;$i++) { $string.='&'; }
                    336:                     for(my $i=$further+1;$now-1>$i;$i++) {
                    337:                         $rows[$i]=$string.$rows[$i];
                    338:                     }
                    339:                 }
                    340:                 $further=$now;
                    341:             }
                    342:         }
                    343:         $col++;
1.31      albertel  344:     }
                    345:     return $further;
                    346: }
                    347: # ------------------------------------------------------------ Build page table
                    348: 
                    349: sub tracetable {
1.32      albertel  350:     my ($sofar,$rid,$beenhere,$showtypes,$indent,$linkid)=@_;
                    351:     my $newshowtypes=$showtypes;
1.31      albertel  352:     my $further=$sofar;
1.44      www       353: # $Apache::lonxml::debug=1;
1.32      albertel  354:     &Apache::lonxml::debug("$rid ; $linkid ; $sofar ; $beenhere ; ".$hash{'src_'.$rid});
                    355:     if ($beenhere=~/\&$rid\&/) { return $further; }
                    356:     $beenhere.=$rid.'&';
1.31      albertel  357: 
                    358:     if (defined($hash{'is_map_'.$rid})) {
1.51      bowersj2  359:         $sofar++;
                    360:         my $tprefix='';
                    361:         if ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}}
                    362:             eq 'sequence') {
                    363:             $tprefix='h';
                    364:         } elsif ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}}
                    365:                  eq 'page') {
                    366:             $tprefix='j';
                    367:             if ($indent) { $tprefix='i'.$indent.','.$tprefix; }
                    368:             if ($linkid) { $tprefix='l'.$linkid.','.$tprefix; }
                    369:             $newshowtypes='problems';
                    370:             $indent++;
                    371:             #if in a .page continue to link the encompising .page
                    372:             if (!$linkid) { $linkid=$rid; }
                    373:         }
                    374:         if (defined($rows[$sofar])) {
                    375:             $rows[$sofar].='&'.$tprefix.$rid;
                    376:         } else {
                    377:             $rows[$sofar]=$tprefix.$rid;
                    378:         }
                    379:         if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
                    380:             (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
                    381:             my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
                    382:             $sofar=&tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
                    383:                                '&'.$frid.'&',$newshowtypes,$indent,$linkid);
                    384:             &addresource($hash{'src_'.$frid},\$sofar,$frid,$newshowtypes,
                    385:                          $indent,$linkid);
                    386:             if ($tprefix =~ /j$/) { $indent--; $linkid=''; }
                    387:         }
1.31      albertel  388:     } else {
1.51      bowersj2  389:         &addresource($hash{'src_'.$rid},\$sofar,$rid,$showtypes,
                    390:                      $indent,$linkid);
1.31      albertel  391:     }
                    392: 
                    393:     if (defined($hash{'to_'.$rid})) {
1.51      bowersj2  394:         $further=&followlinks($rid,$sofar,$beenhere,$further,$showtypes,
                    395:                               $indent,$linkid);
1.2       www       396:     }
1.31      albertel  397: 
1.2       www       398:     return $further;
                    399: }
                    400: 
                    401: # ================================================================ Main Handler
1.1       www       402: 
                    403: sub handler {
1.28      albertel  404:     my $r=shift;
1.2       www       405: 
1.51      bowersj2  406:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                    407: 
                    408:     if ($ENV{'form.jtest'} ne "1")
                    409:     {
                    410:         return new_handle($r);
                    411:     }
1.2       www       412: 
                    413: # ------------------------------------------- Set document type for header only
                    414: 
1.28      albertel  415:     if ($r->header_only) {
1.51      bowersj2  416:         if ($ENV{'browser.mathml'}) {
                    417:             $r->content_type('text/xml');
                    418:         } else {
                    419:             $r->content_type('text/html');
                    420:         }
                    421:         $r->send_http_header;
                    422:         return OK;
1.28      albertel  423:     }
                    424:     my $requrl=$r->uri;
                    425:     my $hashtied;
1.2       www       426: # ----------------------------------------------------------------- Tie db file
1.28      albertel  427:     my $fn;
                    428:     if ($ENV{'request.course.fn'}) {
1.51      bowersj2  429:         $fn=$ENV{'request.course.fn'};
                    430:         if (-e "$fn.db") {
                    431:             if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) &&
                    432:                 (tie(%parmhash,'GDBM_File',
                    433:                      $ENV{'request.course.fn'}.'_parms.db',
                    434:                      &GDBM_READER(),0640))) {
                    435:                 $hashtied=1;
                    436:             }
                    437:         }
1.28      albertel  438:     }
                    439:     if (!$hashtied) {
1.51      bowersj2  440:         $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
                    441:         return HTTP_NOT_ACCEPTABLE; 
1.28      albertel  442:     }
                    443: 
1.2       www       444: # ------------------------------------------------------------------- Hash tied
1.28      albertel  445: 
                    446:     if ($ENV{'browser.mathml'}) {
1.51      bowersj2  447:         $r->content_type('text/xml');
1.28      albertel  448:     } else {
1.51      bowersj2  449:         $r->content_type('text/html');
1.28      albertel  450:     }
                    451:     &Apache::loncommon::no_cache($r);
                    452:     $r->send_http_header;
                    453: 
1.42      www       454:     my $firstres=$hash{'map_start_'.
                    455:            &Apache::lonnet::clutter($ENV{'request.course.uri'})};
                    456:     my $lastres=$hash{'map_finish_'.
                    457:            &Apache::lonnet::clutter($ENV{'request.course.uri'})};
1.28      albertel  458:     if (!(($firstres) && ($lastres))) {
1.51      bowersj2  459:         $r->print('<html><body>Coursemap undefined.</body></html>');
1.28      albertel  460:     } else {
                    461: 
1.2       www       462: # ----------------------------------------------------------------- Render page
1.10      www       463: # -------------------------------------------------------------- Set parameters
                    464: 
                    465: 
                    466: # ---------------------------- initialize coursedata and userdata for this user
1.51      bowersj2  467:         undef %courseopt;
                    468:         undef %useropt;
1.10      www       469: 
1.51      bowersj2  470:         my $uname=$ENV{'user.name'};
                    471:         my $udom=$ENV{'user.domain'};
                    472:         my $uhome=$ENV{'user.home'};
                    473:         my $cid=$ENV{'request.course.id'};
                    474:         my $chome=$ENV{'course.'.$cid.'.home'};
                    475:         my ($cdom,$cnum)=split(/\_/,$cid);
                    476: 
                    477:         my $userprefix=$uname.'_'.$udom.'_';
                    478:         
                    479:         unless ($uhome eq 'no_host') { 
1.48      bowersj2  480: # ------------------------------------------------- Get coursedata (if present)
1.51      bowersj2  481:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
                    482:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                    483:                                                  ':resourcedata',$chome);
                    484:                 if ($reply!~/^error\:/) {
                    485:                     $courserdatas{$cid}=$reply;
                    486:                     $courserdatas{$cid.'.last_cache'}=time;
                    487:                 }
                    488:                 # check to see if network failed
                    489:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                    490:                 {
                    491:                     $networkFailedFlag = 1;
                    492:                 }
                    493:             }
                    494:             foreach (split(/\&/,$courserdatas{$cid})) {
                    495:                 my ($name,$value)=split(/\=/,$_);
                    496:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
                    497:                     &Apache::lonnet::unescape($value);
                    498:             }
1.10      www       499: # --------------------------------------------------- Get userdata (if present)
1.51      bowersj2  500:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
                    501:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
                    502:                 if ($reply!~/^error\:/) {
                    503:                     $userrdatas{$uname.'___'.$udom}=$reply;
                    504:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
                    505:                 }
                    506:             }
                    507:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
                    508:                 my ($name,$value)=split(/\=/,$_);
                    509:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
                    510:                     &Apache::lonnet::unescape($value);
                    511:             }
                    512:         }
1.28      albertel  513: 
1.51      bowersj2  514:         @rows=();
1.28      albertel  515: 
1.51      bowersj2  516:         &tracetable(0,$firstres,'&','',0);
1.2       www       517: 
                    518: # ------------------------------------------------------------------ Page parms
                    519: 
1.51      bowersj2  520:         my $j;
                    521:         my $i;
                    522:         my $lcm=1;
                    523:         my $contents=0;
1.2       www       524: 
                    525: # ---------------------------------------------- Go through table to get layout
                    526: 
1.51      bowersj2  527:         for ($i=0;$i<=$#rows;$i++) {
                    528:             if ($rows[$i]) {
                    529:                 &Apache::lonxml::debug("Row $i is:".$rows[$i]);
                    530:                 $contents++;
                    531:                 my @colcont=split(/\&/,$rows[$i]);
                    532:                 $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
                    533:             } 
                    534:         }
1.5       www       535: 
1.2       www       536: 
1.51      bowersj2  537:         unless ($contents) {
                    538:             $r->print('<html><body>Empty Map.</body></html>');
                    539:         } else {
1.10      www       540: 
1.2       www       541: # ------------------------------------------------------------------ Build page
                    542: 
1.51      bowersj2  543:             my $currenturl=$ENV{'form.postdata'};
                    544:             $currenturl=~s/^http\:\/\///;
                    545:             $currenturl=~s/^[^\/]+//;
1.13      www       546: 
1.2       www       547: # ---------------------------------------------------------------- Send headers
                    548: 
1.51      bowersj2  549:             my $date=localtime;
                    550:             my $now=time;
1.21      www       551: # ----------------------------------------- Get email status and discussiontime
                    552: 
1.51      bowersj2  553:             my %emailstatus=&Apache::lonnet::dump('email_status');
                    554:             my $logouttime=$emailstatus{'logout'};
                    555:             my $courseleave=$emailstatus{'logout_'.$ENV{'request.course.id'}};
                    556:             my $lastcheck=($courseleave>$logouttime?$courseleave:$logouttime);
                    557: 
                    558:             my %discussiontimes=&Apache::lonnet::dump('discussiontimes',
                    559:                                                           $cdom,$cnum);
                    560: 
                    561:             my %feedback=();
                    562:             my %error=();
                    563:             foreach my $msgid (split(/\&/,&Apache::lonnet::reply('keys:'.
                    564:                                                                  $ENV{'user.domain'}.':'.
                    565:                                                                  $ENV{'user.name'}.':nohist_email',
                    566:                                                                  $ENV{'user.home'}))) {
                    567:                 $msgid=&Apache::lonnet::unescape($msgid);
                    568:                 my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
                    569:                 if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
                    570:                     my ($what,$url)=($1,$2);
                    571:                     my %status=
                    572:                         &Apache::lonnet::get('email_status',[$msgid]);
                    573:                     if ($status{$msgid}=~/^error\:/) { 
                    574:                         $status{$msgid}=''; 
                    575:                     }
                    576:                         
                    577:                     if (($status{$msgid} eq 'new') || 
                    578:                         (!$status{$msgid})) { 
                    579:                         if ($what eq 'Error') {
                    580:                             $error{$url}.=','.$msgid; 
                    581:                         } else {
                    582:                             $feedback{$url}.=','.$msgid;
                    583:                         }
                    584:                     }
                    585:                 }
                    586:             }
1.22      www       587: # ----------------------------------------------------------- Start Page Output
1.43      www       588:             my $bodytagadd='';
1.51      bowersj2  589:             $r->print(
1.43      www       590:                    '<html><head><title>Navigate Course Map</title></head>');
1.51      bowersj2  591:             if (($currenturl=~/^\/res/) &&
                    592:                 ($currenturl!~/^\/res\/adm/)) {
                    593:                 $bodytagadd='onLoad="window.location.hash='."'curloc'".'"';
                    594:             }
                    595:             $r->print(&Apache::loncommon::bodytag('Navigate Course Map','',
1.43      www       596:                                                   $bodytagadd));
1.51      bowersj2  597:             $r->print('<script>window.focus();</script>');
                    598:             my $desc=$ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    599:             if (defined($desc)) { $r->print("<h2>$desc</h2>\n"); }
                    600:             $r->print("<h3>$date</h3>\n");
                    601:             $r->rflush();
                    602:             $r->print('<img src="/adm/lonMisc/chat.gif"> New discussion since '.
                    603:                       localtime($lastcheck).
                    604:                       '<br><img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'); 
                    605:             if (($currenturl=~/^\/res/) &&
                    606:                 ($currenturl!~/^\/res\/adm/)) {
                    607:                 $r->print('<a href="#curloc">Current Location</a><p>');
                    608:             }
                    609: 
                    610:             # Handle a network error
                    611: 
                    612:             if ($networkFailedFlag)
                    613:             {
                    614:                 $r->print('<H2>LON-CAPA network failure.</H2>'."\n");
                    615:                 $r->print("<p>LON-CAPA's network is having difficulties, some problem" .
                    616:                           " information, such as due dates, will not be available.");
                    617:             }
1.13      www       618: # ----------------------------------------------------- The little content list
1.51      bowersj2  619:             for ($i=0;$i<=$#rows;$i++) {
                    620:                 if ($rows[$i]) {
                    621:                     my @colcont=split(/\&/,$rows[$i]);
                    622:                     my $avespan=$lcm/($#colcont+1);
                    623:                     for ($j=0;$j<=$#colcont;$j++) {
                    624:                         my $rid=$colcont[$j];
                    625:                         if ($rid=~/^h(.+)/) {
                    626:                             $rid=$1;
                    627:                             $r->print('&nbsp;&nbsp;&nbsp;<a href="#'.
                    628:                                       $rid.'">'.$hash{'title_'.$rid}.
                    629:                                       '</a><br>');
                    630:                         }
                    631:                     }
                    632:                 }
                    633:             }
1.2       www       634: # ----------------------------------------------------------------- Start table
1.51      bowersj2  635:             $r->print('<hr><table cols="'.$lcm.'" border="0">');
                    636:             for ($i=0;$i<=$#rows;$i++) {
                    637:                 if ($rows[$i]) {
                    638:                     $r->print("\n<tr>");
                    639:                     my @colcont=split(/\&/,$rows[$i]);
                    640:                     my $avespan=$lcm/($#colcont+1);
                    641: 
                    642:                     # for each item I wish to print on this row...
                    643:                     for ($j=0;$j<=$#colcont;$j++) {
                    644:                         my $indent;my $indentstr;
                    645:                         my $linkid;
                    646:                         my $rid=$colcont[$j];
1.36      www       647:                         $rid=~/(\d+)\.(\d+)$/;
1.51      bowersj2  648:                         my $src=
                    649:                            &Apache::lonnet::declutter($hash{'src_'.$1.'.'.$2});
                    650:                         my $symb=
                    651:           &Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.$src;
                    652:                         my $add='<td>';
                    653:                         my $adde='</td>';
                    654:                         my $hwk='<font color="#223322">';
                    655:                         my $hwke='</font>';
                    656:                         if ($rid=~/^l(\d+\.\d+),(.+)/) { $linkid=$1; $rid=$2; }
                    657:                         if ($rid=~/^i(\d+),(.+)/) { $indent=$1; $rid=$2; }
                    658:                         if ($rid=~/^h(.+)/) {
                    659:                             $rid=$1;
                    660:                             $add='<th bgcolor="#AAFF55"><a name="'.$rid.'">';
                    661:                             $adde='</th>';
1.36      www       662:                             if (($ENV{'user.adv'}) && 
1.51      bowersj2  663:                                 ($parmhash{$symb.'.0.parameter_randompick'})) {
1.36      www       664:                                $adde=' (randomly select '.
1.51      bowersj2  665:                                    $parmhash{$symb.'.0.parameter_randompick'}.
1.36      www       666:                                    ')</th>';
                    667:                             }
1.51      bowersj2  668:                         }
                    669:                         if ($rid=~/^j(.+)/) { $rid=$1; }
                    670:                         if ($rid=~/^p(\d)(\d)\"([\w\: \(\)\/\,]*)\"(.+)/) {
                    671:                             # sub astatus describes what code/tcode mean
                    672:                             my $code=$1;
                    673:                             my $tcode=$2;
                    674:                             my $ctext=$3;
                    675:                             $rid=$4;
                    676:                             
                    677:                             # will open later
                    678:                             if ($tcode eq '1') { 
                    679:                                 $add='<td bgcolor="#AAAAAA">';
                    680:                             }
                    681: 
                    682:                             # solved/correct
                    683:                             if ($code eq '3') {
                    684:                                 $add='<td bgcolor="#AAFFAA">';
                    685:                             } elsif ($code eq '4') { # partially correct
                    686:                                 $add='<td bgcolor="#E0FFAA">';
                    687:                             } else {
                    688:                                 # not attempted
                    689:                                 
                    690:                                 # we end up here on network failure, so pick a neutral
                    691:                                 # color if the network failed instead of bright red.
                    692:                                 if ( $networkFailedFlag )
                    693:                                 {
                    694:                                     $add = '<td bgcolor="#AAAAAA">';
                    695:                                 }
                    696:                                 else
                    697:                                 {
                    698:                                     $add='<td bgcolor="#FFAAAA">';
                    699:                                 }
                    700: 
                    701:                                 if ($tcode eq '2') { # open, not past due
                    702:                                     $add='<td bgcolor="#FFFFAA">';
                    703:                                 }
                    704: 
                    705:                                 if ($tcode eq '4') { # due in next 24 hours
                    706:                                     $add='<td bgcolor="#FFFF33">';
                    707:                                     $adde='</td>';
                    708:                                 }
                    709:                             }
                    710:                             $hwk='<font color="#888811"><b>';
                    711:                             $hwke='</b></font>';
                    712:                             if ($code eq '1') {
                    713:                                 $hwke='</b> ('.$ctext.')</font>';
                    714:                             }
                    715:                             if ($code eq '2' || $code eq '4') {
                    716:                                 $hwk='<font color="#992222"><b>';
                    717:                                 $hwke='</b> ('.$ctext.')</font>';
                    718:                             }
                    719:                             if ($code eq '3') {
                    720:                                 $hwk='<font color="#229922"><b>';
                    721:                                 $hwke='</b> ('.$ctext.')</font>';
                    722:                             }
                    723:                             if ($networkFailedFlag) 
                    724:                             {
                    725:                                 $hwke='</b> (status unavailable)</font>';
                    726:                             }
                    727:                         }
                    728:                         if ($rid && $hash{'src_'.$rid} eq $currenturl) {
                    729:                             $add=$add.'<a name="curloc"></a>'.
                    730:                                 '<font color=red size=+2><b>&gt; </b></font>';
                    731:                             $adde=
                    732:                                 '<font color=red size=+2><b> &lt;</b></font>'.$adde;
                    733:                         }
                    734:                         if ($discussiontimes{$symb}>$lastcheck) {
                    735:                             $adde=
                    736:                                 '<img border=0 src="/adm/lonMisc/chat.gif">'.
                    737:                                     $adde;
                    738:                         }
                    739:                         if ($error{$src}) {
                    740:                             foreach (split(/\,/,$error{$src})) {
                    741:                                 if ($_) {
                    742:                                     $adde=
                    743:                                         '&nbsp;<a href="/adm/email?display='.
                    744:                                             &Apache::lonnet::escape($_).
                    745:                                                 '"><img src="/adm/lonMisc/bomb.gif" border=0></a>'
                    746:                                                     .$adde;
                    747:                                 }
                    748:                             }
                    749:                         }
                    750:                         if ($feedback{$src}) {
                    751:                             foreach (split(/\,/,$feedback{$src})) {
                    752:                                 if ($_) {
                    753:                                     $adde=
                    754:                                         '&nbsp;<a href="/adm/email?display='.
                    755:                                             &Apache::lonnet::escape($_).
                    756:                                                 '"><img src="/adm/lonMisc/feedback.gif" border=0></a>'
                    757:                                                     .$adde;
                    758:                                 }
                    759:                             }
                    760:                         }
                    761:                         if ($indent) {
                    762:                             my $is="&nbsp;&nbsp;";
                    763:                             for(my $i=-1;$i<$indent;$i++) { $indentstr.=$is; }
                    764:                         }
                    765:                         if (!$linkid) { $linkid=$rid; }
1.38      www       766:                         if ($hash{'randomout_'.$rid}) {
                    767:                             $adde=' <i>(hidden)</i>'.$adde;
                    768:                         }
1.51      bowersj2  769:                         $r->print($add.$indentstr);
                    770:                         if ($rid) {
                    771:                             $r->print('<a href="'.$hash{'src_'.$linkid}.
1.39      www       772:                                       (($hash{'src_'.$linkid}=~/\?/)?'&':'?').
                    773:                                       'symb='.&Apache::lonnet::escape($symb)
1.35      www       774:                                        .'">'.
1.51      bowersj2  775:                                       $hwk.$hash{'title_'.$rid}.$hwke.'</a>');
                    776:                         }
                    777:                         $r->print($adde);
                    778:                     }
                    779:                     $r->print('</tr>');
                    780:                 }
                    781:             }
                    782:             $r->print("\n</table>");
                    783:             $r->print('</body></html>');
1.2       www       784: # -------------------------------------------------------------------- End page
1.51      bowersj2  785:         }                  
1.2       www       786: # ------------------------------------------------------------- End render page
1.28      albertel  787:     }
1.2       www       788: # ------------------------------------------------------------------ Untie hash
1.28      albertel  789:     unless (untie(%hash)) {
1.51      bowersj2  790:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    791:                                  "Could not untie coursemap $fn (browse).</font>"); 
1.28      albertel  792:     }
                    793:     unless (untie(%parmhash)) {
1.51      bowersj2  794:         &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    795:                                  "Could not untie parmhash (browse).</font>"); 
1.28      albertel  796:     }
                    797:     return OK;
1.2       www       798: }
1.1       www       799: 
1.51      bowersj2  800: sub new_handle {
                    801:     my $r = shift;
                    802: 
                    803:     # Handle header-only request
                    804:     if ($r->header_only) {
                    805:         if ($ENV{'browser.mathml'}) {
                    806:             $r->content_type('text/xml');
                    807:         } else {
                    808:             $r->content_type('text/html');
                    809:         }
                    810:         $r->send_http_header;
                    811:         return OK;
                    812:     }
                    813: 
                    814:     # Send header, don't cache this page
                    815:     if ($ENV{'browser.mathml'}) {
                    816:         $r->content_type('text/xml');
                    817:     } else {
                    818:         $r->content_type('text/html');
                    819:     }
                    820:     &Apache::loncommon::no_cache($r);
                    821:     $r->send_http_header;
                    822: 
1.92      bowersj2  823:     # Create the nav map the nav map
1.51      bowersj2  824:     my $navmap = Apache::lonnavmaps::navmap->new(
                    825:                         $ENV{"request.course.fn"}.".db",
1.80      bowersj2  826:                         $ENV{"request.course.fn"}."_parms.db", 1, 1);
1.51      bowersj2  827: 
1.55      bowersj2  828: 
                    829:     if (!defined($navmap)) {
                    830:         my $requrl = $r->uri;
                    831:         $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
                    832:         return HTTP_NOT_ACCEPTABLE;
                    833:     }
1.64      bowersj2  834: 
                    835:     # Header
                    836:     $r->print(&Apache::loncommon::bodytag('Navigate Course Map','',
                    837:                                           ''));
                    838:     $r->print('<script>window.focus();</script>');
                    839:     my $desc=$ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.95      bowersj2  840:     $r->print('<table border="0" cellpadding="2" cellspacing="0">');
1.64      bowersj2  841:     my $date=localtime;
1.95      bowersj2  842:     $r->print('<tr><td align="right" valign="bottom">Key:&nbsp;&nbsp;</td>');
1.65      bowersj2  843:     if ($navmap->{LAST_CHECK}) {
1.95      bowersj2  844:         $r->print('<td align="center" valign="bottom">&nbsp;&nbsp;'.
                    845:                   '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
1.73      bowersj2  846:                   strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
1.95      bowersj2  847:                   '</td><td align="center" valign="bottom">&nbsp;&nbsp;'.
                    848:                   '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
                    849:                   '</td>'); 
                    850:     } else {
                    851:         $r->print('<td align="center" valign="bottom">&nbsp;&nbsp;'.
                    852:                   '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
                    853:                   '&nbsp;&nbsp;<img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
                    854:                   '</td>'); 
                    855:     }
                    856:     $r->print('</tr></table>');
                    857:     my $condition = 0;
                    858:     if ($ENV{'form.condition'}) {
                    859:         $condition = 1;
                    860:     }
                    861: 
                    862:     if ($condition) {
                    863:         $r->print('<a href="navmaps?condition=0&filter=">Close All Folders</a>');
1.65      bowersj2  864:     } else {
1.95      bowersj2  865:         $r->print('<a href="navmaps?condition=1&filter=">Open All Folders</a>');
1.65      bowersj2  866:     }
1.55      bowersj2  867: 
1.95      bowersj2  868:     $r->print('<br>&nbsp;');
                    869:     $r->rflush();
                    870: 
1.92      bowersj2  871:     # Now that we've displayed some stuff to the user, init the navmap
                    872:     $navmap->init();
                    873: 
1.55      bowersj2  874:     # Check that it's defined
                    875:     if (!($navmap->courseMapDefined())) {
                    876:         $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
                    877:                   '</body></html>');
                    878:         return OK;
                    879:     }
                    880: 
1.51      bowersj2  881:     # Grab a resource object so we have access to the constants; this
                    882:     # is technically not proper, but should be harmless
                    883:     my $res = $navmap->firstResource();
                    884: 
                    885:     # Defines a status->color mapping, null string means don't color
                    886:     my %colormap = 
1.54      bowersj2  887:     ( $res->NETWORK_FAILURE        => '',
1.59      bowersj2  888:       $res->CORRECT                => '',
1.54      bowersj2  889:       $res->EXCUSED                => '#BBBBFF',
1.59      bowersj2  890:       $res->PAST_DUE_ANSWER_LATER  => '',
                    891:       $res->PAST_DUE_NO_ANSWER     => '',
                    892:       $res->ANSWER_OPEN            => '#CCFFCC',
1.54      bowersj2  893:       $res->OPEN_LATER             => '',
1.59      bowersj2  894:       $res->TRIES_LEFT             => '',
                    895:       $res->INCORRECT              => '',
                    896:       $res->OPEN                   => '',
1.54      bowersj2  897:       $res->NOTHING_SET            => ''        );
1.59      bowersj2  898:     # And a special case in the nav map; what to do when the assignment
                    899:     # is not yet done and due in less then 24 hours
1.86      bowersj2  900:     my $hurryUpColor = "#FF0000";
1.59      bowersj2  901: 
                    902:     my %statusIconMap = 
1.66      bowersj2  903:         ( $res->NETWORK_FAILURE    => '',
                    904:           $res->NOTHING_SET        => '',
                    905:           $res->CORRECT            => 'navmap.correct.gif',
                    906:           $res->EXCUSED            => 'navmap.correct.gif',
                    907:           $res->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif',
                    908:           $res->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif',
                    909:           $res->ANSWER_OPEN        => 'navmap.wrong.gif',
                    910:           $res->OPEN_LATER         => '',
                    911:           $res->TRIES_LEFT         => 'navmap.open.gif',
                    912:           $res->INCORRECT          => 'navmap.wrong.gif',
1.69      bowersj2  913:           $res->OPEN               => 'navmap.open.gif',
1.85      bowersj2  914:           $res->ATTEMPTED          => 'navmap.open.gif' );
1.69      bowersj2  915: 
                    916:     my %iconAltTags = 
                    917:         ( 'navmap.correct.gif' => 'Correct',
                    918:           'navmap.wrong.gif'   => 'Incorrect',
                    919:           'navmap.open.gif'    => 'Open' );
1.59      bowersj2  920: 
                    921:     my %condenseStatuses =
1.66      bowersj2  922:         ( $res->NETWORK_FAILURE    => 1,
                    923:           $res->NOTHING_SET        => 1,
                    924:           $res->CORRECT            => 1 );
1.51      bowersj2  925: 
                    926:     my %filterHash;
                    927:     # Figure out what we're not displaying
                    928:     foreach (split(/\,/, $ENV{"form.filter"})) {
                    929:         if ($_) {
                    930:             $filterHash{$_} = "1";
                    931:         }
                    932:     }
                    933: 
1.88      bowersj2  934:     # Is this a new-style course? If so, we want to suppress showing the top-level
                    935:     # maps in their own folders, in favor of "inlining" them.
                    936:     my $topResource = $navmap->getById("0.0");
                    937:     my $inlineTopLevelMaps = $topResource->src() =~ m|^/uploaded/.*default\.sequence$|;
                    938: 
1.63      bowersj2  939:     my $currenturl = $ENV{'form.postdata'};
                    940:     $currenturl=~s/^http\:\/\///;
                    941:     $currenturl=~s/^[^\/]+//;
1.75      bowersj2  942:     # alreadyHere allows us to only open the maps necessary to view
                    943:     # the current location once, while at the same time remembering
                    944:     # the current location. Without that check, the user would never
                    945:     # be able to close those maps; the user would close it, and the
                    946:     # currenturl scan would re-open it.
                    947:     my $queryAdd = "postdata=" . &Apache::lonnet::escape($currenturl) .
                    948:         "&alreadyHere=1";
1.63      bowersj2  949: 
1.51      bowersj2  950:     # Begin the HTML table
1.59      bowersj2  951:     # four cols: resource + indent, chat+feedback, icon, text string
1.85      bowersj2  952:     $r->print('<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n");
1.51      bowersj2  953: 
1.74      bowersj2  954:     # This needs to be updated to use symbs from the remote, 
                    955:     # instead of uris. The changes to this and the main rendering
                    956:     # loop should be obvious.
                    957:     # Here's a simple example of the iterator.
1.88      bowersj2  958:     # Preprocess the map: Look for current URL, force inlined maps to display
                    959: 
1.96      bowersj2  960:     # This currently does very little...
                    961:     my $mapEventualIterator = Apache::lonnavmaps::iterator->new($navmap, undef, undef, {},
                    962:                                                                 undef, $condition);
                    963: 
1.88      bowersj2  964:     my $mapIterator = $navmap->getIterator(undef, undef, {}, 1);
                    965:     my $found = 0;
                    966:     my $depth = 1;
1.91      bowersj2  967:     my $currentUrlIndex = 0; # keeps track of when the current resource is found,
                    968:                              # so we can back up a few and put the anchor above the
                    969:                              # current resource
1.88      bowersj2  970:     $mapIterator->next(); # discard the first BEGIN_MAP
                    971:     my $curRes = $mapIterator->next();
1.91      bowersj2  972:     my $counter = 0;
1.88      bowersj2  973:     
                    974:     while ($depth > 0) {
1.96      bowersj2  975:         if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
                    976:         if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1.88      bowersj2  977: 
1.91      bowersj2  978:         if (ref($curRes)) { $counter++; }
                    979: 
1.88      bowersj2  980:         my $mapStack = $mapIterator->getStack();
                    981:         if ($currenturl && !$ENV{'form.alreadyHere'} && ref($curRes) && 
                    982:             $curRes->src() eq $currenturl) {
                    983:             # If this is the correct resource, be sure to 
                    984:             # show it by making sure the containing maps
                    985:             # are open.
1.91      bowersj2  986: 
                    987:             $currentUrlIndex = $counter;
1.88      bowersj2  988:             
                    989:             for my $map (@{$mapStack}) {
                    990:                 if ($condition) {
                    991:                     undef $filterHash{$map->map_pc()};
                    992:                 } else {
                    993:                     $filterHash{$map->map_pc()} = 1;
1.74      bowersj2  994:                 }
                    995:             }
1.88      bowersj2  996:             $ENV{'form.alreadyHere'} = 1;
                    997:         }
                    998:             
                    999:         # Preprocessing: If we're inlining nav maps into the top-level display,
                   1000:         # make sure we show this map!
                   1001:         if ($inlineTopLevelMaps && ref($curRes) && $curRes->is_map && 
                   1002:             scalar(@{$mapStack}) == 1) {
                   1003:             if ($condition) {
                   1004:                 undef $filterHash{$curRes->map_pc()};
                   1005:             } else {
                   1006:                 $filterHash{$curRes->map_pc()} = 1;
                   1007:             }
1.74      bowersj2 1008:         }
1.88      bowersj2 1009: 
                   1010:         $curRes = $mapIterator->next();
1.74      bowersj2 1011:     }
1.88      bowersj2 1012:     
1.53      bowersj2 1013:     undef $res; # so we don't accidentally use it later
1.72      bowersj2 1014:     my $indentLevel = 0;
1.61      bowersj2 1015:     my $indentString = "<img src=\"/adm/lonIcons/whitespace1.gif\" width=\"25\" height=\"1\" alt=\"\" border=\"0\" />";
1.51      bowersj2 1016: 
                   1017:     my $isNewBranch = 0;
1.59      bowersj2 1018:     my $now = time();
                   1019:     my $in24Hours = $now + 24 * 60 * 60;
1.78      bowersj2 1020:     my $displayedHereMarker = 0;
1.88      bowersj2 1021:     
1.72      bowersj2 1022:     # We know the first thing is a BEGIN_MAP (see "$self->{STARTED}"
                   1023:     # code in iterator->next), so ignore the first one
1.89      bowersj2 1024:     $mapIterator = $navmap->getIterator(undef, undef, \%filterHash,
1.74      bowersj2 1025:                                            $condition);
1.72      bowersj2 1026:     $mapIterator->next();
1.89      bowersj2 1027:     $curRes = $mapIterator->next();
1.88      bowersj2 1028:     my $deltadepth = 0;
1.90      bowersj2 1029:     $depth = 1;
1.88      bowersj2 1030: 
                   1031:     my @backgroundColors = ("#FFFFFF", "#F6F6F6");
                   1032:     my $rowNum = 0;
1.51      bowersj2 1033: 
1.91      bowersj2 1034:     $counter = 0;
                   1035: 
1.72      bowersj2 1036:     while ($depth > 0) {
1.88      bowersj2 1037:         # If we're in a new style course, and this is a BEGIN_MAP, END_MAP, or
                   1038:         # map resource and the stack depth is only one, just plain ignore this resource
                   1039:         # entirely. (This has the effect of inlining the resources in that map
                   1040:         # in the nav map.)
                   1041:         if ($inlineTopLevelMaps && scalar(@{$mapIterator->getStack()}) == 1 &&
                   1042:             ref($curRes) && $curRes->is_map()) {
                   1043:             # We let the normal depth stuff occur, but we need to shift everything
                   1044:             # over by one to the left to make it look right.
                   1045:             $deltadepth = -1;
                   1046:             $curRes = $mapIterator->next();
                   1047:             next;
                   1048:         }
                   1049: 
1.51      bowersj2 1050:         if ($curRes == $mapIterator->BEGIN_MAP() ||
1.52      bowersj2 1051:             $curRes == $mapIterator->BEGIN_BRANCH()) {
1.51      bowersj2 1052:             $indentLevel++;
                   1053:         }
                   1054:         if ($curRes == $mapIterator->END_MAP() ||
1.52      bowersj2 1055:             $curRes == $mapIterator->END_BRANCH()) {
1.51      bowersj2 1056:             $indentLevel--;
                   1057:         }
1.52      bowersj2 1058:         if ($curRes == $mapIterator->BEGIN_BRANCH()) {
                   1059:             $isNewBranch = 1;
1.72      bowersj2 1060:         }
1.96      bowersj2 1061:         if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
                   1062:         if ($curRes == $mapIterator->END_MAP()) { $depth--; }
1.51      bowersj2 1063: 
1.91      bowersj2 1064:         if (ref($curRes)) { $counter++; }
                   1065: 
1.88      bowersj2 1066:         if ($depth == 1) { $deltadepth = 0; } # we're done shifting, because we're
                   1067:                                               # out of the inlined map
                   1068: 
1.68      bowersj2 1069:         # Is this resource being blotted out?
                   1070:         if (ref($curRes) && !advancedUser() && $curRes->randomout()) {
                   1071:             $curRes = $mapIterator->next();
                   1072:             next; # and totally ignore this resource
                   1073:         }
                   1074: 
1.51      bowersj2 1075:         if (ref($curRes) && $curRes->src()) {
                   1076: 
1.66      bowersj2 1077:             # Step one: Decide which parts to show
                   1078:             my @parts = @{$curRes->parts()};
                   1079:             my $multipart = scalar(@parts) > 1;
                   1080:             my $condensed = 0;
                   1081:                 
1.51      bowersj2 1082:             if ($curRes->is_problem()) {
1.66      bowersj2 1083:                 # Is it multipart?
1.59      bowersj2 1084: 
1.66      bowersj2 1085:                 if ($multipart) {
                   1086:                     # If it's multipart, see if part 0 is "open"
                   1087:                     # if it is, display all parts, if it isn't,
                   1088:                     # just display first
                   1089:                     if (!$curRes->opendate("0")) {
                   1090:                         @parts = ("0"); # just display the zero-th part
1.68      bowersj2 1091:                         $condensed = 1;
1.66      bowersj2 1092:                     } else {
                   1093:                         # Otherwise, only display part 0 if we want to 
                   1094:                         # attach feedback or email information to it
                   1095:                         if ($curRes->hasDiscussion() || $curRes->getFeedback()) {
                   1096:                             shift @parts;
                   1097:                         } else {
                   1098:                             # If there's discussion or feedback, that counts
                   1099:                             # as a difference, so display the parts.
                   1100:                             
                   1101:                             # Now, we decide whether to condense the
                   1102:                             # parts due to similarity
                   1103:                             my $status = $curRes->status($parts[1]);
                   1104:                             my $due = $curRes->duedate($parts[1]);
                   1105:                             my $open = $curRes->opendate($parts[1]);
                   1106:                             my $statusAllSame = 1;
                   1107:                             my $dueAllSame = 1;
                   1108:                             my $openAllSame = 1;
                   1109:                             for (my $i = 2; $i < scalar(@parts); $i++) {
                   1110:                                 if ($curRes->status($parts[$i]) != $status){
                   1111:                                     $statusAllSame = 0;
                   1112:                                 }
                   1113:                                 if ($curRes->duedate($parts[$i]) != $due ) {
                   1114:                                     $dueAllSame = 0;
                   1115:                                 }
                   1116:                                 if ($curRes->opendate($parts[$i]) != $open) {
                   1117:                                     $openAllSame = 0;
                   1118:                                 }
                   1119:                             }
                   1120:                             
                   1121:                             # $allSame is true if all the statuses were
                   1122:                             # the same. Now, if they are all the same and
                   1123:                             # match one of the statuses to condense, or they
                   1124:                             # are all open with the same due date, or they are
                   1125:                             # all OPEN_LATER with the same open date, display the
                   1126:                             # status of the first non-zero part (to get the 'correct'
                   1127:                             # status right, since 0 is never 'correct' or 'open').
                   1128:                             if (($statusAllSame && defined($condenseStatuses{$status})) ||
                   1129:                                 ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
                   1130:                                 ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
                   1131:                                 @parts = ($parts[1]);
                   1132:                                 $condensed = 1;
                   1133:                             }
                   1134:                         }
                   1135:                     }
                   1136:                 }
1.59      bowersj2 1137: 
1.51      bowersj2 1138:             } else {
1.84      bowersj2 1139:                 $parts[0] = "0"; # this is to get past foreach loop below
1.51      bowersj2 1140:                  # you can consider a non-problem resource as a resource
1.77      bowersj2 1141:                   # with only one part without loss
1.51      bowersj2 1142:             }
                   1143: 
1.83      bowersj2 1144:             # Is it a multipart problem with a single part, now in 
                   1145:             # @parts with "0" filtered out? If so, forget it's a multi-part
                   1146:             # problem and treat it like a single-part problem.
                   1147:             if ( scalar(@parts) == 1 ) {
                   1148:                 $multipart = 0;
                   1149:             }
                   1150: 
1.66      bowersj2 1151:             # Display one part, in event of network error.
                   1152:             # If this is a single part, we can at least show the correct
                   1153:             # status, but if it's multipart, we're lost.
                   1154:             if ($curRes->{RESOURCE_ERROR}) {
                   1155:                 @parts = ("0");
                   1156:             }
                   1157: 
                   1158:             # Step two: Show the parts
1.51      bowersj2 1159:             foreach my $part (@parts) {
1.59      bowersj2 1160: 
1.66      bowersj2 1161:                 my $deltalevel = 0; # for inserting the branch icon
1.68      bowersj2 1162:                 my $nonLinkedText = ""; # unlinked stuff after title
1.51      bowersj2 1163:                 
                   1164:                 # For each thing we're displaying...
                   1165: 
                   1166:                 my $stack = $mapIterator->getStack();
                   1167:                 my $src = getLinkForResource($stack);
                   1168: 
                   1169:                 my $srcHasQuestion = $src =~ /\?/;
                   1170:                 my $link = $src.
                   1171:                     ($srcHasQuestion?'&':'?') .
                   1172:                     'symb='.&Apache::lonnet::escape($curRes->symb()).
                   1173:                     '"';
                   1174:                 my $title = $curRes->title();
1.81      bowersj2 1175:                 if (!$title) {
                   1176:                     $title = $curRes->src();
                   1177:                     $title = substr ($title, rindex($title, "/") + 1);
                   1178:                 }
1.51      bowersj2 1179:                 my $partLabel = "";
1.52      bowersj2 1180:                 my $newBranchText = "";
                   1181: 
                   1182:                 # If this is a new branch, label it so
                   1183:                 # (temporary, this should be an icon w/ alt text)
                   1184:                 if ($isNewBranch) {
1.59      bowersj2 1185:                     $newBranchText = "<img src=\"/adm/lonIcons/branch.gif\" border=\"0\">";
1.52      bowersj2 1186:                     $isNewBranch = 0;
1.66      bowersj2 1187:                     $deltalevel = 1;
1.52      bowersj2 1188:                 }
1.51      bowersj2 1189: 
                   1190:                 # links to open and close the folders
                   1191:                 my $linkopen = "<a href=\"$link\">";
                   1192:                 my $linkclose = "</a>";
                   1193: 
1.61      bowersj2 1194:                 my $icon = "<img src=\"/adm/lonIcons/html.gif\" alt=\"\" border=\"0\" />";
1.51      bowersj2 1195:                 if ($curRes->is_problem()) { 
1.66      bowersj2 1196:                     if ($part eq "0" || $condensed) {
                   1197:                         $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border=\"0\" />'; 
                   1198:                     } else {
                   1199:                         $icon = $indentString;
                   1200:                     }
1.51      bowersj2 1201:                 }
                   1202: 
                   1203:                 # Display the correct icon, link to open or shut map
                   1204:                 if ($curRes->is_map()) { 
                   1205:                     my $mapId = $curRes->map_pc();
1.82      bowersj2 1206:                     my $nowOpen = (!defined($filterHash{$mapId}));
                   1207:                     if ($condition) {$nowOpen = !$nowOpen;}
1.51      bowersj2 1208:                     $icon = $nowOpen ?
1.77      bowersj2 1209:                         "navmap.folder.closed.gif" : "navmap.folder.open.gif";
1.66      bowersj2 1210:                     $icon = "<img src=\"/adm/lonIcons/$icon\" alt=\"\" border=\"0\" />";
1.52      bowersj2 1211:                     $linkopen = "<a href=\"/adm/navmaps?filter=";
1.66      bowersj2 1212:                     $linkopen .= ($nowOpen xor $condition) ? 
1.51      bowersj2 1213:                         addToFilter(\%filterHash, $mapId) :
                   1214:                         removeFromFilter(\%filterHash, $mapId);
1.66      bowersj2 1215:                     $linkopen .= "&condition=$condition&$queryAdd\">";
1.51      bowersj2 1216:                     $linkclose = "</a>";
1.68      bowersj2 1217: 
1.51      bowersj2 1218:                 }
                   1219:                 
                   1220:                 my $colorizer = "";
1.86      bowersj2 1221:                 my $color;
1.51      bowersj2 1222:                 if ($curRes->is_problem()) {
                   1223:                     my $status = $curRes->status($part);
1.86      bowersj2 1224:                     $color = $colormap{$status};
1.74      bowersj2 1225: 
                   1226:                     # Special case in the navmaps: If in less then
                   1227:                     # 24 hours, give it a bit of urgency
1.86      bowersj2 1228:                     if (($status == $curRes->OPEN() || $status == $curRes->ATTEMPTED() ||
                   1229:                          $status == $curRes->TRIES_LEFT())
                   1230:                         && $curRes->duedate() &&
1.76      bowersj2 1231:                         $curRes->duedate() < time()+(24*60*60) && 
                   1232:                         $curRes->duedate() > time()) {
1.74      bowersj2 1233:                         $color = $hurryUpColor;
                   1234:                     }
1.79      bowersj2 1235:                     # Special case: If this is the last try, and there is
1.88      bowersj2 1236:                     # more then one available, and it's not due yet, give a bit of urgency
1.79      bowersj2 1237:                     my $tries = $curRes->tries($part);
                   1238:                     my $maxtries = $curRes->maxtries($part);
                   1239:                     if ($tries && $maxtries && $maxtries > 1 &&
1.88      bowersj2 1240:                         $maxtries - $tries == 1 && $curRes->duedate() &&
                   1241:                         $curRes->duedate() > time()) {
1.79      bowersj2 1242:                         $color = $hurryUpColor;
                   1243:                     }
1.51      bowersj2 1244:                     if ($color ne "") {
                   1245:                         $colorizer = "bgcolor=\"$color\"";
                   1246:                     }
                   1247:                 }
                   1248: 
1.68      bowersj2 1249:                 if ($curRes->randomout()) {
                   1250:                     $nonLinkedText .= ' <i>(hidden)</i> ';
                   1251:                 }
                   1252: 
1.88      bowersj2 1253:                 $rowNum++;
                   1254:                 my $backgroundColor = $backgroundColors[$rowNum % scalar(@backgroundColors)];
                   1255: 
1.91      bowersj2 1256:                 # FIRST COL: The resource indentation, branch icon, name, and anchor
1.88      bowersj2 1257:                 $r->print("  <tr bgcolor=\"$backgroundColor\"><td align=\"left\" valign=\"center\" width=\"60%\">\n");
1.51      bowersj2 1258: 
1.91      bowersj2 1259:                 # anchor for current resource... - 5 is deliberate: If it's that
                   1260:                 # high on the screen, don't bother focusing on it. Also this will
                   1261:                 # print multiple anchors if this is an expanded multi-part problem...
                   1262:                 # who cares?
                   1263:                 if ($counter == $currentUrlIndex - 5) {
                   1264:                     $r->print('<a name="current" />');
                   1265:                 }
                   1266: 
1.51      bowersj2 1267:                 # print indentation
1.88      bowersj2 1268:                 for (my $i = 0; $i < $indentLevel - $deltalevel + $deltadepth; $i++) {
1.51      bowersj2 1269:                     $r->print($indentString);
                   1270:                 }
                   1271: 
1.61      bowersj2 1272:                 $r->print("  ${newBranchText}${linkopen}$icon${linkclose}\n");
1.51      bowersj2 1273: 
1.74      bowersj2 1274:                 my $curMarkerBegin = "";
                   1275:                 my $curMarkerEnd = "";
                   1276: 
                   1277:                 # Is this the current resource?
1.78      bowersj2 1278:                 if ($curRes->src() eq $currenturl && !$displayedHereMarker) {
1.74      bowersj2 1279:                     $curMarkerBegin = '<a name="curloc" /><font color="red" size="+2">&gt; </font>';
                   1280:                     $curMarkerEnd = '<font color="red" size="+2"> &lt;</font>';
1.78      bowersj2 1281:                     $displayedHereMarker = 1;
1.74      bowersj2 1282:                 }
                   1283: 
1.69      bowersj2 1284:                 if ($curRes->is_problem() && $part ne "0" && !$condensed) { 
1.66      bowersj2 1285:                     $partLabel = " (Part $part)"; 
                   1286:                     $title = "";
                   1287:                 }
1.83      bowersj2 1288:                 if ($multipart && $condensed) {
1.68      bowersj2 1289:                     $nonLinkedText .= ' (' . $curRes->countParts() . ' parts)';
1.66      bowersj2 1290:                 }
1.59      bowersj2 1291: 
1.74      bowersj2 1292:                 $r->print("  $curMarkerBegin<a href=\"$link\">$title$partLabel</a> $curMarkerEnd $nonLinkedText");
1.97    ! bowersj2 1293:                 $r->print(" TDV:" . $curRes->{DATA}->{TOP_DOWN_VAL}); # temp
        !          1294:                 $r->print(" BUV:" . $curRes->{DATA}->{BOT_UP_VAL}); # temp
        !          1295:                 $r->print(" DD:" . $curRes->{DATA}->{DISPLAY_DEPTH}); # temp
1.66      bowersj2 1296: 
                   1297:                 if ($curRes->{RESOURCE_ERROR}) {
                   1298:                     $r->print(&Apache::loncommon::help_open_topic ("Navmap_Host_Down",
1.84      bowersj2 1299:                                               '<font size="-1">Host down</font>'));
1.66      bowersj2 1300:                     }
                   1301: 
                   1302:                 my $discussionHTML = ""; my $feedbackHTML = "";
                   1303: 
1.85      bowersj2 1304:                 # SECOND COL: Is there text, feedback, errors??
1.66      bowersj2 1305:                 if ($curRes->hasDiscussion()) {
                   1306:                     $discussionHTML = $linkopen .
                   1307:                         '<img border="0" src="/adm/lonMisc/chat.gif" />' .
                   1308:                         $linkclose;
                   1309:                 }
                   1310: 
                   1311:                 if ($curRes->getFeedback()) {
                   1312:                     my $feedback = $curRes->getFeedback();
                   1313:                     foreach (split(/\,/, $feedback)) {
                   1314:                         if ($_) {
                   1315:                             $feedbackHTML .= '&nbsp;<a href="/adm/email?display='
                   1316:                                 . &Apache::lonnet::escape($_) . '">'
                   1317:                                 . '<img src="/adm/lonMisc/feedback.gif" '
                   1318:                                 . 'border="0" /></a>';
                   1319:                         }
                   1320:                     }
                   1321:                 }
                   1322: 
1.84      bowersj2 1323:                 $r->print("<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML&nbsp;</td>");
1.66      bowersj2 1324: 
                   1325:                 # Is this the first displayed part of a multi-part problem
                   1326:                 # that has not been condensed, so we should suppress these two
                   1327:                 # columns?
                   1328:                 my $firstDisplayed = !$condensed && $multipart && $part eq "0";
                   1329: 
1.69      bowersj2 1330:                 # THIRD COL: Problem status icon
1.66      bowersj2 1331:                 if ($curRes->is_problem() &&
                   1332:                     !$firstDisplayed) {
                   1333:                     my $icon = $statusIconMap{$curRes->status($part)};
1.69      bowersj2 1334:                     my $alt = $iconAltTags{$icon};
1.66      bowersj2 1335:                     if ($icon) {
1.84      bowersj2 1336:                         $r->print("<td width=\"30\" valign=\"center\" width=\"50\" align=\"right\">$linkopen<img width=\"25\" height=\"25\" src=\"/adm/lonIcons/$icon\" border=\"0\" alt=\"$alt\" />$linkclose</td>\n");
1.66      bowersj2 1337:                     } else {
1.84      bowersj2 1338:                         $r->print("<td width=\"30\">&nbsp;</td>\n");
1.66      bowersj2 1339:                     }
                   1340:                 } else { # not problem, no icon
1.84      bowersj2 1341:                     $r->print("<td width=\"30\">&nbsp;</td>\n");
1.66      bowersj2 1342:                 }
                   1343: 
1.69      bowersj2 1344:                 # FOURTH COL: Text description
1.86      bowersj2 1345:                 #$r->print("<td $colorizer align=\"right\" valign=\"center\">\n");
                   1346:                 $r->print("<td align=\"right\" valign=\"center\">\n");
1.51      bowersj2 1347:                 
1.61      bowersj2 1348:                 if ($curRes->kind() eq "res" &&
                   1349:                     $curRes->is_problem() &&
1.66      bowersj2 1350:                     !$firstDisplayed) {
1.86      bowersj2 1351:                     $r->print ("<font color=\"$color\"><b>") if ($color);
1.53      bowersj2 1352:                     $r->print (getDescription($curRes, $part));
1.86      bowersj2 1353:                     $r->print ("</b></font>") if ($color);
1.51      bowersj2 1354:                 }
1.68      bowersj2 1355:                 if ($curRes->is_map() && advancedUser() && $curRes->randompick()) {
                   1356:                     $r->print('(randomly select ' . $curRes->randompick() .')');
                   1357:                 }
1.59      bowersj2 1358: 
1.84      bowersj2 1359:                 $r->print("&nbsp;</td></tr>\n");
1.51      bowersj2 1360:             }
                   1361:         }
                   1362:         $curRes = $mapIterator->next();
                   1363:     }
                   1364: 
                   1365:     $r->print("</table></body></html>");
                   1366: 
1.59      bowersj2 1367:     $navmap->untieHashes();
                   1368: 
1.51      bowersj2 1369:     return OK;
                   1370: }
                   1371: 
                   1372: # Convenience functions: Returns a string that adds or subtracts
                   1373: # the second argument from the first hash, appropriate for the 
                   1374: # query string that determines which folders to recurse on
                   1375: sub addToFilter {
                   1376:     my $hashIn = shift;
                   1377:     my $addition = shift;
                   1378:     my %hash = %$hashIn;
                   1379:     $hash{$addition} = 1;
                   1380: 
                   1381:     return join (",", keys(%hash));
                   1382: }
                   1383: 
                   1384: sub removeFromFilter {
                   1385:     my $hashIn = shift;
                   1386:     my $subtraction = shift;
                   1387:     my %hash = %$hashIn;
                   1388: 
                   1389:     delete $hash{$subtraction};
                   1390:     return join(",", keys(%hash));
                   1391: }
                   1392: 
                   1393: # Convenience function: Given a stack returned from getStack on the iterator,
                   1394: # return the correct src() value.
                   1395: # Later, this should add an anchor when we start putting anchors in pages.
                   1396: sub getLinkForResource {
                   1397:     my $stack = shift;
                   1398:     my $res;
                   1399: 
                   1400:     # Check to see if there are any pages in the stack
                   1401:     foreach $res (@$stack) {
                   1402:         if (defined($res) && $res->is_page()) {
                   1403:             return $res->src();
                   1404:         }
                   1405:     }
                   1406: 
                   1407:     # Failing that, return the src of the last resource that is defined
                   1408:     # (when we first recurse on a map, it puts an undefined resource
                   1409:     # on the bottom because $self->{HERE} isn't defined yet, and we
                   1410:     # want the src for the map anyhow)
                   1411:     foreach (@$stack) {
                   1412:         if (defined($_)) { $res = $_; }
                   1413:     }
                   1414: 
                   1415:     return $res->src();
                   1416: }
                   1417: 
1.53      bowersj2 1418: # Convenience function: This seperates the logic of how to create
                   1419: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
                   1420: # etc.) into a seperate function. It takes a resource object as the
                   1421: # first parameter, and the part number of the resource as the second.
                   1422: # It's basically a big switch statement on the status of the resource.
                   1423: 
                   1424: sub getDescription {
                   1425:     my $res = shift;
                   1426:     my $part = shift;
1.69      bowersj2 1427:     my $status = $res->status($part);
1.53      bowersj2 1428: 
                   1429:     if ($status == $res->NETWORK_FAILURE) { return ""; }
                   1430:     if ($status == $res->NOTHING_SET) {
                   1431:         return "Not currently assigned.";
                   1432:     }
                   1433:     if ($status == $res->OPEN_LATER) {
1.73      bowersj2 1434:         return "Open " . timeToHumanString($res->opendate($part));
1.53      bowersj2 1435:     }
                   1436:     if ($status == $res->OPEN) {
1.79      bowersj2 1437:         if ($res->duedate($part)) {
1.73      bowersj2 1438:             return "Due " . timeToHumanString($res->duedate($part));
1.66      bowersj2 1439:         } else {
                   1440:             return "Open, no due date";
                   1441:         }
1.53      bowersj2 1442:     }
1.54      bowersj2 1443:     if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73      bowersj2 1444:         return "Answer open " . timeToHumanString($res->answerdate($part));
1.54      bowersj2 1445:     }
                   1446:     if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73      bowersj2 1447:         return "Was due " . timeToHumanString($res->duedate($part));
1.53      bowersj2 1448:     }
                   1449:     if ($status == $res->ANSWER_OPEN) {
                   1450:         return "Answer available";
                   1451:     }
1.59      bowersj2 1452:     if ($status == $res->EXCUSED) {
1.66      bowersj2 1453:         return "Excused by instructor";
1.59      bowersj2 1454:     }
1.69      bowersj2 1455:     if ($status == $res->ATTEMPTED) {
                   1456:         return "Not yet graded.";
                   1457:     }
1.59      bowersj2 1458:     if ($status == $res->TRIES_LEFT) {
1.79      bowersj2 1459:         my $tries = $res->tries($part);
                   1460:         my $maxtries = $res->maxtries($part);
                   1461:         my $triesString = "";
                   1462:         if ($tries && $maxtries) {
                   1463:             $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
                   1464:             if ($maxtries > 1 && $maxtries - $tries == 1) {
                   1465:                 $triesString = "<b>$triesString</b>";
                   1466:             }
                   1467:         }
1.66      bowersj2 1468:         if ($res->duedate()) {
1.73      bowersj2 1469:             return "Due " . timeToHumanString($res->duedate($part)) .
1.66      bowersj2 1470:                 " $triesString";
                   1471:         } else {
                   1472:             return "No due date $triesString";
                   1473:         }
1.59      bowersj2 1474:     }
1.53      bowersj2 1475: }
                   1476: 
1.68      bowersj2 1477: sub advancedUser {
                   1478:     return $ENV{'user.adv'};
                   1479: }
                   1480: 
1.73      bowersj2 1481: 
                   1482: # timeToHumanString takes a time number and converts it to a
                   1483: # human-readable representation, meant to be used in the following
                   1484: # manner:
                   1485: # print "Due $timestring"
                   1486: # print "Open $timestring"
                   1487: # print "Answer available $timestring"
                   1488: # Very, very, very, VERY English-only... goodness help a localizer on
                   1489: # this func...
1.53      bowersj2 1490: sub timeToHumanString {
1.58      albertel 1491:     my ($time) = @_;
                   1492:     # zero, '0' and blank are bad times
1.73      bowersj2 1493:     if (!$time) {
                   1494:         return 'never';
                   1495:     }
                   1496: 
                   1497:     my $now = time();
                   1498: 
                   1499:     my @time = localtime($time);
                   1500:     my @now = localtime($now);
                   1501: 
                   1502:     # Positive = future
                   1503:     my $delta = $time - $now;
                   1504: 
                   1505:     my $minute = 60;
                   1506:     my $hour = 60 * $minute;
                   1507:     my $day = 24 * $hour;
                   1508:     my $week = 7 * $day;
                   1509:     my $inPast = 0;
                   1510: 
                   1511:     # Logic in comments:
                   1512:     # Is it now? (extremely unlikely)
                   1513:     if ( $delta == 0 ) {
                   1514:         return "this instant";
                   1515:     }
                   1516: 
                   1517:     if ($delta < 0) {
                   1518:         $inPast = 1;
                   1519:         $delta = -$delta;
                   1520:     }
                   1521: 
                   1522:     # Is it in the future?
                   1523:     if ( $delta > 0 ) {
                   1524:         # Is it less then a minute away?
                   1525:         my $tense = $inPast ? " ago" : "";
                   1526:         my $prefix = $inPast ? "" : "in ";
                   1527:         if ( $delta < $minute ) {
                   1528:             if ($delta == 1) { return "${prefix}1 second$tense"; }
                   1529:             return "$prefix$delta seconds$tense";
                   1530:         }
                   1531: 
                   1532:         # Is it less then an hour away?
                   1533:         if ( $delta < $hour ) {
                   1534:             # If so, use minutes
                   1535:             my $minutes = floor($delta / 60);
                   1536:             if ($minutes == 1) { return "${prefix}1 minute$tense"; }
                   1537:             return "$prefix$minutes minutes$tense";
                   1538:         }
                   1539:         
                   1540:         # Is it less then 24 hours away? If so,
                   1541:         # display hours + minutes
                   1542:         if ( $delta < $hour * 24) {
                   1543:             my $hours = floor($delta / $hour);
                   1544:             my $minutes = floor(($delta % $hour) / $minute);
                   1545:             my $hourString = "$hours hours";
                   1546:             my $minuteString = ", $minutes minutes";
                   1547:             if ($hours == 1) {
                   1548:                 $hourString = "1 hour";
                   1549:             }
                   1550:             if ($minutes == 1) {
                   1551:                 $minuteString = ", 1 minute";
                   1552:             }
                   1553:             if ($minutes == 0) {
                   1554:                 $minuteString = "";
                   1555:             }
                   1556:             return "$prefix$hourString$minuteString$tense";
                   1557:         }
                   1558: 
                   1559:         # Less then 5 days away, display day of the week and
                   1560:         # HH:MM
                   1561:         if ( $delta < $day * 5 ) {
1.85      bowersj2 1562:             my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73      bowersj2 1563:             $timeStr =~ s/12:00 am/midnight/;
                   1564:             $timeStr =~ s/12:00 pm/noon/;
                   1565:             return ($inPast ? "last " : "next ") .
                   1566:                 $timeStr;
                   1567:         }
                   1568:         
                   1569:         # Is it this year?
                   1570:         if ( $time[5] == $now[5]) {
                   1571:             # Return on Month Day, HH:MM meridian
                   1572:             my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
                   1573:             $timeStr =~ s/12:00 am/midnight/;
                   1574:             $timeStr =~ s/12:00 pm/noon/;
                   1575:             return $timeStr;
                   1576:         }
                   1577: 
                   1578:         # Not this year, so show the year
                   1579:         my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
                   1580:         $timeStr =~ s/12:00 am/midnight/;
                   1581:         $timeStr =~ s/12:00 pm/noon/;
                   1582:         return $timeStr;
1.58      albertel 1583:     }
1.53      bowersj2 1584: }
                   1585: 
1.51      bowersj2 1586: 1;
                   1587: 
                   1588: package Apache::lonnavmaps::navmap;
                   1589: 
                   1590: =pod
                   1591: 
                   1592: 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.
                   1593: 
                   1594: =head1 navmap object: Encapsulating the compiled nav map
                   1595: 
                   1596: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
                   1597: 
                   1598: 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.
                   1599: 
                   1600: You must obtain resource objects through the navmap object.
                   1601: 
                   1602: =head2 Methods
                   1603: 
                   1604: =over 4
                   1605: 
1.70      bowersj2 1606: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions, genMailDiscussStatus): Binds a new navmap object to the compiled nav map hash and parm hash given as filenames. 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. genMailDiscussStatus causes the nav map to retreive information about the email and discussion status of resources. 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.
1.51      bowersj2 1607: 
                   1608: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
                   1609: 
                   1610: =cut
                   1611: 
                   1612: use strict;
                   1613: use GDBM_File;
                   1614: 
                   1615: sub new {
                   1616:     # magic invocation to create a class instance
                   1617:     my $proto = shift;
                   1618:     my $class = ref($proto) || $proto;
                   1619:     my $self = {};
                   1620: 
                   1621:     $self->{NAV_HASH_FILE} = shift;
                   1622:     $self->{PARM_HASH_FILE} = shift;
                   1623:     $self->{GENERATE_COURSE_USER_OPT} = shift;
1.55      bowersj2 1624:     $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1.51      bowersj2 1625: 
                   1626:     # Resource cache stores navmapresource's as we reference them. We generate
                   1627:     # them on-demand so we don't pay for creating resources unless we use them.
                   1628:     $self->{RESOURCE_CACHE} = {};
                   1629: 
                   1630:     # Network failure flag, if we accessed the course or user opt and
                   1631:     # failed
                   1632:     $self->{NETWORK_FAILURE} = 0;
                   1633: 
                   1634:     # tie the nav hash
                   1635:     my %navmaphash;
                   1636:     if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
                   1637:               &GDBM_READER(), 0640))) {
                   1638:         return undef;
                   1639:     }
                   1640:     $self->{NAV_HASH} = \%navmaphash;
                   1641:     
                   1642:     my %parmhash;
                   1643:     if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
                   1644:               &GDBM_READER(), 0640)))
                   1645:     {
1.66      bowersj2 1646:         untie $self->{PARM_HASH};
1.51      bowersj2 1647:         return undef;
                   1648:     }
                   1649:     $self->{PARM_HASH} = \%parmhash;
                   1650:     $self->{HASH_TIED} = 1;
                   1651: 
1.92      bowersj2 1652:     bless($self);
                   1653:         
                   1654:     return $self;
                   1655: }
                   1656: 
                   1657: sub init {
1.93      bowersj2 1658:     my $self = shift;
1.92      bowersj2 1659: 
1.51      bowersj2 1660:     # If the course opt hash and the user opt hash should be generated,
                   1661:     # generate them
                   1662:     if ($self->{GENERATE_COURSE_USER_OPT}) {
                   1663:         my $uname=$ENV{'user.name'};
                   1664:         my $udom=$ENV{'user.domain'};
                   1665:         my $uhome=$ENV{'user.home'};
                   1666:         my $cid=$ENV{'request.course.id'};
                   1667:         my $chome=$ENV{'course.'.$cid.'.home'};
                   1668:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1669:         
                   1670:         my $userprefix=$uname.'_'.$udom.'_';
                   1671:         
                   1672:         my %courserdatas; my %useropt; my %courseopt;
                   1673:         unless ($uhome eq 'no_host') { 
                   1674: # ------------------------------------------------- Get coursedata (if present)
                   1675:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
                   1676:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                   1677:                                                  ':resourcedata',$chome);
                   1678:                 if ($reply!~/^error\:/) {
                   1679:                     $courserdatas{$cid}=$reply;
                   1680:                     $courserdatas{$cid.'.last_cache'}=time;
                   1681:                 }
                   1682:                 # check to see if network failed
                   1683:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1684:                 {
                   1685:                     $self->{NETWORK_FAILURE} = 1;
                   1686:                 }
                   1687:             }
                   1688:             foreach (split(/\&/,$courserdatas{$cid})) {
                   1689:                 my ($name,$value)=split(/\=/,$_);
                   1690:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1691:                     &Apache::lonnet::unescape($value);
                   1692:             }
                   1693: # --------------------------------------------------- Get userdata (if present)
                   1694:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
                   1695:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
                   1696:                 if ($reply!~/^error\:/) {
                   1697:                     $userrdatas{$uname.'___'.$udom}=$reply;
                   1698:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
                   1699:                 }
                   1700:                 # check to see if network failed
                   1701:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1702:                 {
                   1703:                     $self->{NETWORK_FAILURE} = 1;
                   1704:                 }
                   1705:             }
                   1706:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
                   1707:                 my ($name,$value)=split(/\=/,$_);
                   1708:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1709:                     &Apache::lonnet::unescape($value);
                   1710:             }
1.55      bowersj2 1711:             $self->{COURSE_OPT} = \%courseopt;
                   1712:             $self->{USER_OPT} = \%useropt;
1.51      bowersj2 1713:         }
                   1714:     }   
                   1715: 
1.55      bowersj2 1716:     if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
                   1717:         my $cid=$ENV{'request.course.id'};
                   1718:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1719:         
                   1720:         my %emailstatus = &Apache::lonnet::dump('email_status');
1.56      bowersj2 1721:         my $logoutTime = $emailstatus{'logout'};
                   1722:         my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1.55      bowersj2 1723:         $self->{LAST_CHECK} = ($courseLeaveTime > $logoutTime ?
                   1724:                                $courseLeaveTime : $logoutTime);
1.56      bowersj2 1725:         my %discussiontime = &Apache::lonnet::dump('discussiontimes', 
1.55      bowersj2 1726:                                                    $cdom, $cnum);
                   1727:         my %feedback=();
                   1728:         my %error=();
                   1729:         my $keys = &Apache::lonnet::reply('keys:'.
                   1730:                                           $ENV{'user.domain'}.':'.
                   1731:                                           $ENV{'user.name'}.':nohist_email',
                   1732:                                           $ENV{'user.home'});
                   1733: 
                   1734:         foreach my $msgid (split(/\&/, $keys)) {
                   1735:             $msgid=&Apache::lonnet::unescape($msgid);
                   1736:             my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
                   1737:             if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
                   1738:                 my ($what,$url)=($1,$2);
                   1739:                 my %status=
                   1740:                     &Apache::lonnet::get('email_status',[$msgid]);
                   1741:                 if ($status{$msgid}=~/^error\:/) { 
                   1742:                     $status{$msgid}=''; 
                   1743:                 }
                   1744:                 
                   1745:                 if (($status{$msgid} eq 'new') || 
                   1746:                     (!$status{$msgid})) { 
                   1747:                     if ($what eq 'Error') {
                   1748:                         $error{$url}.=','.$msgid; 
                   1749:                     } else {
                   1750:                         $feedback{$url}.=','.$msgid;
                   1751:                     }
                   1752:                 }
                   1753:             }
                   1754:         }
                   1755:         
                   1756:         $self->{FEEDBACK} = \%feedback;
                   1757:         $self->{ERROR_MSG} = \%error; # what is this? JB
                   1758:         $self->{DISCUSSION_TIME} = \%discussiontime;
                   1759:         $self->{EMAIL_STATUS} = \%emailstatus;
                   1760:         
                   1761:     }    
1.84      bowersj2 1762: 
                   1763:     $self->{PARM_CACHE} = {};
1.55      bowersj2 1764: }
1.51      bowersj2 1765: 
1.55      bowersj2 1766: # Checks to see if coursemap is defined, matching test in old lonnavmaps
                   1767: sub courseMapDefined {
                   1768:     my $self = shift;
                   1769:     my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
                   1770: 
1.56      bowersj2 1771:     my $firstres = $self->{NAV_HASH}->{"map_start_$uri"};
                   1772:     my $lastres = $self->{NAV_HASH}->{"map_finish_$uri"};
1.55      bowersj2 1773:     return $firstres && $lastres;
1.51      bowersj2 1774: }
                   1775: 
                   1776: sub getIterator {
                   1777:     my $self = shift;
1.94      bowersj2 1778:     my $iterator = Apache::lonnavmaps::DFSiterator->new($self, shift, shift,
1.89      bowersj2 1779:                                                      shift, undef, shift, 
                   1780:                                                      $ENV{'form.direction'});
1.51      bowersj2 1781:     return $iterator;
                   1782: }
                   1783: 
                   1784: # unties the hash when done
                   1785: sub untieHashes {
                   1786:     my $self = shift;
1.60      bowersj2 1787:     untie %{$self->{NAV_HASH}} if ($self->{HASH_TIED});
                   1788:     untie %{$self->{PARM_HASH}} if ($self->{HASH_TIED});
1.51      bowersj2 1789:     $self->{HASH_TIED} = 0;
                   1790: }
                   1791: 
                   1792: # when the object is destroyed, be sure to untie all the hashes we tied.
                   1793: sub DESTROY {
                   1794:     my $self = shift;
                   1795:     $self->untieHashes();
                   1796: }
                   1797: 
1.59      bowersj2 1798: # Private function: Does the given resource (as a symb string) have
                   1799: # current discussion? Returns 0 if chat/mail data not extracted.
                   1800: sub hasDiscussion {
                   1801:     my $self = shift;
                   1802:     my $symb = shift;
                   1803:     if (!defined($self->{DISCUSSION_TIME})) { return 0; }
                   1804: 
                   1805:     return $self->{DISCUSSION_TIME}->{$symb} >
1.66      bowersj2 1806:            $self->{LAST_CHECK};
1.59      bowersj2 1807: }
                   1808: 
                   1809: # Private function: Does the given resource (as a symb string) have
                   1810: # current feedback? Returns the string in the feedback hash, which
                   1811: # will be false if it does not exist.
                   1812: sub getFeedback { 
                   1813:     my $self = shift;
                   1814:     my $symb = shift;
                   1815: 
                   1816:     if (!defined($self->{FEEDBACK})) { return ""; }
                   1817:     
                   1818:     return $self->{FEEDBACK}->{$symb};
                   1819: }
                   1820: 
1.51      bowersj2 1821: =pod
                   1822: 
                   1823: =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.
                   1824: 
                   1825: =cut
                   1826: 
                   1827: # The strategy here is to cache the resource objects, and only construct them
                   1828: # as we use them. The real point is to prevent reading any more from the tied
                   1829: # hash then we have to, which should hopefully alleviate speed problems.
                   1830: # Caching is just an incidental detail I throw in because it makes sense.
                   1831: 
                   1832: sub getById {
                   1833:     my $self = shift;
                   1834:     my $id = shift;
                   1835: 
                   1836:     if (defined ($self->{RESOURCE_CACHE}->{$id}))
                   1837:     {
                   1838:         return $self->{RESOURCE_CACHE}->{$id};
                   1839:     }
                   1840: 
                   1841:     # resource handles inserting itself into cache.
                   1842:     return Apache::lonnavmaps::resource->new($self, $id);
                   1843: }
                   1844: 
                   1845: =pod
                   1846: 
                   1847: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
                   1848: 
                   1849: =cut
                   1850: 
                   1851: sub firstResource {
                   1852:     my $self = shift;
                   1853:     my $firstResource = $self->{NAV_HASH}->{'map_start_' .
                   1854:                      &Apache::lonnet::clutter($ENV{'request.course.uri'})};
                   1855:     return $self->getById($firstResource);
                   1856: }
                   1857: 
                   1858: =pod
                   1859: 
                   1860: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
                   1861: 
                   1862: =cut
                   1863: 
                   1864: sub finishResource {
                   1865:     my $self = shift;
                   1866:     my $firstResource = $self->{NAV_HASH}->{'map_finish_' .
                   1867:                      &Apache::lonnet::clutter($ENV{'request.course.uri'})};
                   1868:     return $self->getById($firstResource);
                   1869: }
                   1870: 
                   1871: # -------------------------------------------- Figure out a cascading parameter
                   1872: #
                   1873: # For this function to work
                   1874: #
                   1875: # * parmhash needs to be tied
                   1876: # * courseopt and useropt need to be initialized for this user and course
                   1877: #
                   1878: 
                   1879: sub parmval {
                   1880:     my $self = shift;
                   1881:     my ($what,$symb)=@_;
1.84      bowersj2 1882:     my $hashkey = $what."|||".$symb;
                   1883: 
                   1884:     if (defined($self->{PARM_CACHE}->{$hashkey})) {
                   1885:         return $self->{PARM_CACHE}->{$hashkey};
                   1886:     }
                   1887: 
                   1888:     my $result = $self->parmval_real($what, $symb);
                   1889:     $self->{PARM_CACHE}->{$hashkey} = $result;
                   1890:     return $result;
                   1891: }
                   1892: 
                   1893: sub parmval_real {
                   1894:     my $self = shift;
                   1895:     my ($what,$symb) = @_;
                   1896: 
1.51      bowersj2 1897:     my $cid=$ENV{'request.course.id'};
                   1898:     my $csec=$ENV{'request.course.sec'};
                   1899:     my $uname=$ENV{'user.name'};
                   1900:     my $udom=$ENV{'user.domain'};
                   1901: 
                   1902:     unless ($symb) { return ''; }
                   1903:     my $result='';
                   1904: 
                   1905:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                   1906: 
                   1907: # ----------------------------------------------------- Cascading lookup scheme
                   1908:     my $rwhat=$what;
                   1909:     $what=~s/^parameter\_//;
                   1910:     $what=~s/\_/\./;
                   1911: 
                   1912:     my $symbparm=$symb.'.'.$what;
                   1913:     my $mapparm=$mapname.'___(all).'.$what;
                   1914:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
                   1915: 
                   1916:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
                   1917:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
                   1918:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
                   1919: 
                   1920:     my $courselevel= $usercourseprefix.'.'.$what;
                   1921:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
                   1922:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
                   1923: 
                   1924:     my $useropt = $self->{USER_OPT};
                   1925:     my $courseopt = $self->{COURSE_OPT};
                   1926:     my $parmhash = $self->{PARM_HASH};
                   1927: 
                   1928: # ---------------------------------------------------------- first, check user
                   1929:     if ($uname and defined($useropt)) {
1.57      albertel 1930:         if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
                   1931:         if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
                   1932:         if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1.51      bowersj2 1933:     }
                   1934: 
                   1935: # ------------------------------------------------------- second, check course
                   1936:     if ($csec and defined($courseopt)) {
1.57      albertel 1937:         if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
                   1938:         if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
                   1939:         if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1.51      bowersj2 1940:     }
                   1941: 
                   1942:     if (defined($courseopt)) {
1.57      albertel 1943:         if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
                   1944:         if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
                   1945:         if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1.51      bowersj2 1946:     }
                   1947: 
                   1948: # ----------------------------------------------------- third, check map parms
                   1949: 
                   1950:     my $thisparm=$$parmhash{$symbparm};
1.57      albertel 1951:     if (defined($thisparm)) { return $thisparm; }
1.51      bowersj2 1952: 
                   1953: # ----------------------------------------------------- fourth , check default
                   1954: 
                   1955:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1.57      albertel 1956:     if (defined($default)) { return $default}
1.51      bowersj2 1957: 
                   1958: # --------------------------------------------------- fifth , cascade up parts
                   1959: 
                   1960:     my ($space,@qualifier)=split(/\./,$rwhat);
                   1961:     my $qualifier=join('.',@qualifier);
                   1962:     unless ($space eq '0') {
                   1963:         my ($part,$id)=split(/\_/,$space);
                   1964:         if ($id) {
                   1965:             my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1.57      albertel 1966:             if (defined($partgeneral)) { return $partgeneral; }
1.51      bowersj2 1967:         } else {
                   1968:             my $resourcegeneral=$self->parmval("0.$qualifier",$symb);
1.57      albertel 1969:             if (defined($resourcegeneral)) { return $resourcegeneral; }
1.51      bowersj2 1970:         }
                   1971:     }
                   1972:     return '';
                   1973: }
                   1974: 
                   1975: 
                   1976: 1;
                   1977: 
                   1978: package Apache::lonnavmaps::iterator;
                   1979: 
                   1980: # This package must precede "navmap" because "navmap" uses it. In
                   1981: # order to keep the documentation order straight, the iterator is documented
                   1982: # after the navmap object.
                   1983: 
                   1984: =pod
                   1985: 
                   1986: =back
                   1987: 
                   1988: =head1 navmap Iterator
                   1989: 
                   1990: 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.
                   1991: 
                   1992: 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:
                   1993: 
                   1994: C<my $resourceIterator = $navmap-E<gt>getIterator();>
                   1995: 
                   1996: To get the next thing from the iterator, call B<next>:
                   1997: 
                   1998: C<my $nextThing = $resourceIterator-E<gt>next()>
                   1999: 
                   2000: getIterator behaves as follows:
                   2001: 
                   2002: =over 4
                   2003: 
1.89      bowersj2 2004: =item B<getIterator>(firstResource, finishResource, filterHash, condition, direction): 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(). 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 0, then only resource that exist IN the filterHash will be recursed on. If it is a 1, only resources NOT in the filterHash will be recursed on. Defaults to 0, which is to say, do not recurse unless explicitly asked to. Direction specifies which direction to recurse, either FORWARD or BACKWARD, with FORWARD being default.
1.51      bowersj2 2005: 
1.70      bowersj2 2006: 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: By default, the condition is 0 and all folders are closed unless explicitly opened. Clicking "Show All Resources" will use a condition of 1 and an empty filterHash, resulting in all resources being shown.
1.51      bowersj2 2007: 
                   2008: 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:
                   2009: 
                   2010: =over 4
                   2011: 
1.70      bowersj2 2012: =item * BEGIN_MAP: A new map is being recursed into. This is returned I<after> the map resource itself is returned.
                   2013: 
                   2014: =item * END_MAP: The map is now done.
                   2015: 
                   2016: =item * BEGIN_BRANCH: A branch is now starting. The next resource returned will be the first in that branch.
                   2017: 
                   2018: =item * END_BRANCH: The branch is now done.
1.51      bowersj2 2019: 
                   2020: =back
                   2021: 
1.70      bowersj2 2022: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_MAP.
                   2023: 
                   2024: =back
1.51      bowersj2 2025: 
                   2026: =cut
                   2027: 
                   2028: # Here are the tokens for the iterator:
                   2029: 
                   2030: sub BEGIN_MAP { return 1; }    # begining of a new map
                   2031: sub END_MAP { return 2; }      # end of the map
                   2032: sub BEGIN_BRANCH { return 3; } # beginning of a branch
                   2033: sub END_BRANCH { return 4; }   # end of a branch
1.89      bowersj2 2034: sub FORWARD { return 1; }      # go forward
                   2035: sub BACKWARD { return 2; }
1.51      bowersj2 2036: 
1.96      bowersj2 2037: sub min {
                   2038:     (my $a, my $b) = @_;
                   2039:     if ($a < $b) { return $a; } else { return $b; }
                   2040: }
                   2041: 
1.94      bowersj2 2042: sub new {
                   2043:     # magic invocation to create a class instance
                   2044:     my $proto = shift;
                   2045:     my $class = ref($proto) || $proto;
                   2046:     my $self = {};
                   2047: 
                   2048:     $self->{NAV_MAP} = shift;
                   2049:     return undef unless ($self->{NAV_MAP});
                   2050: 
                   2051:     # Handle the parameters
                   2052:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   2053:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   2054: 
                   2055:     # If the given resources are just the ID of the resource, get the
                   2056:     # objects
                   2057:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   2058:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   2059:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   2060:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   2061: 
                   2062:     $self->{FILTER} = shift;
                   2063: 
                   2064:     # A hash, used as a set, of resource already seen
                   2065:     $self->{ALREADY_SEEN} = shift;
                   2066:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
                   2067:     $self->{CONDITION} = shift;
                   2068: 
                   2069:     # Now, we need to pre-process the map, by walking forward and backward
                   2070:     # over the parts of the map we're going to look at.
1.96      bowersj2 2071: 
1.97    ! bowersj2 2072:     # The processing steps are exactly the same, except for a few small 
        !          2073:     # changes, so I bundle those up in the following list of two elements:
        !          2074:     # (direction_to_iterate, VAL_name, next_resource_method_to_call,
        !          2075:     # first_resource).
        !          2076:     # This prevents writing nearly-identical code twice.
        !          2077:     my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext', 
        !          2078:                         'FIRST_RESOURCE'],
        !          2079:                        [BACKWARD(), 'BOT_UP_VAL', 'getPrevious', 
        !          2080:                         'FINISH_RESOURCE'] );
        !          2081: 
        !          2082:     foreach my $pass (@iterations) {
        !          2083:         my $direction = $pass->[0];
        !          2084:         my $valName = $pass->[1];
        !          2085:         my $nextResourceMethod = $pass->[2];
        !          2086:         my $firstResourceName = $pass->[3];
        !          2087: 
        !          2088:         my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP}, 
        !          2089:                                                             $self->{FIRST_RESOURCE},
        !          2090:                                                             $self->{FINISH_RESOURCE},
        !          2091:                                                             {}, undef, 0, $direction);
1.96      bowersj2 2092:     
1.97    ! bowersj2 2093:         # prime the recursion
        !          2094:         $self->{$firstResourceName}->{DATA}->{$valName} = 0;
        !          2095:         my $depth = 1;
        !          2096:         $iterator->next();
        !          2097:         my $curRes = $iterator->next();
        !          2098:         while ($depth > 0) {
        !          2099:             if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
        !          2100:             if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96      bowersj2 2101:         
1.97    ! bowersj2 2102:             if (ref($curRes)) {
        !          2103:                 my $resultingVal = $curRes->{DATA}->{$valName};
        !          2104:                 my $nextResources = $curRes->$nextResourceMethod();
        !          2105:                 my $resourceCount = scalar(@{$nextResources});
1.96      bowersj2 2106:             
1.97    ! bowersj2 2107:                 if ($resourceCount == 1) {
        !          2108:                     my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
        !          2109:                     $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
        !          2110:                 }
        !          2111:                 
        !          2112:                 if ($resourceCount > 1) {
        !          2113:                     foreach my $res (@{$nextResources}) {
        !          2114:                         my $current = $res->{DATA}->{$valName} || 999999999;
        !          2115:                         $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
        !          2116:                     }
        !          2117:                 }
        !          2118:             }
        !          2119:             if (ref($curRes) && $curRes->is_map() && $direction == FORWARD()) {
        !          2120:                 my $firstResource = $curRes->map_start();
        !          2121:                 my $finishResource = $curRes->map_finish();
        !          2122:                 my $newIterator = Apache::lonnavmaps::iterator->new($self->{NAV_MAP},
        !          2123:                                                                     $firstResource,
        !          2124:                                                                     $finishResource,
        !          2125:                                                                     $self->{FILTER},
        !          2126:                                                                     $self->{ALREADY_SEEN},
        !          2127:                                                                     $self->{CONDITION});
1.96      bowersj2 2128:             }
                   2129:             
1.97    ! bowersj2 2130:             # Assign the final val
        !          2131:             if (ref($curRes) && $direction == BACKWARD()) {
        !          2132:                 $curRes->{DATA}->{DISPLAY_DEPTH} = min($curRes->{DATA}->{TOP_DOWN_VAL},
        !          2133:                                                        $curRes->{DATA}->{BOT_UP_VAL});
1.96      bowersj2 2134:             }
1.97    ! bowersj2 2135:             $curRes = $iterator->next();
1.96      bowersj2 2136:         }
                   2137:     }
1.94      bowersj2 2138: 
                   2139:     # Now we're ready to start iterating.
                   2140: }
                   2141: 
                   2142: 1;
                   2143: 
                   2144: package Apache::lonnavmaps::DFSiterator;
                   2145: 
                   2146: # UNDOCUMENTED: This is a private library, it should not generally be used
                   2147: # by the outside world. What it does is walk through the nav map in a 
                   2148: # depth-first fashion. This is not appropriate for most uses, but it is
                   2149: # used by the main iterator for pre-processing. It also is able to isolate
                   2150: # much of the complexity of the main iterator, so the main iterator is much
                   2151: # simpler.
1.97    ! bowersj2 2152: # There is no real benefit in merging the main iterator and this one into one class...
        !          2153: # all the logic in DFSiterator would need to be replicated, you gain no performance,
        !          2154: # at best, you just make one massively complicated iterator in place of two 
        !          2155: # somewhat complicated ones. ;-) - Jeremy
1.94      bowersj2 2156: 
                   2157: # Here are the tokens for the iterator, replicated from iterator for convenience:
                   2158: 
                   2159: sub BEGIN_MAP { return 1; }    # begining of a new map
                   2160: sub END_MAP { return 2; }      # end of the map
                   2161: sub BEGIN_BRANCH { return 3; } # beginning of a branch
                   2162: sub END_BRANCH { return 4; }   # end of a branch
                   2163: sub FORWARD { return 1; }      # go forward
                   2164: sub BACKWARD { return 2; }
                   2165: 
1.51      bowersj2 2166: # Params: nav map, start resource, end resource, filter, condition, 
                   2167: # already seen hash ref
                   2168: 
                   2169: sub new {
                   2170:     # magic invocation to create a class instance
                   2171:     my $proto = shift;
                   2172:     my $class = ref($proto) || $proto;
                   2173:     my $self = {};
                   2174: 
                   2175:     $self->{NAV_MAP} = shift;
                   2176:     return undef unless ($self->{NAV_MAP});
                   2177: 
                   2178:     # Handle the parameters
                   2179:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   2180:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   2181: 
                   2182:     # If the given resources are just the ID of the resource, get the
                   2183:     # objects
                   2184:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   2185:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   2186:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   2187:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   2188: 
                   2189:     $self->{FILTER} = shift;
                   2190: 
                   2191:     # A hash, used as a set, of resource already seen
                   2192:     $self->{ALREADY_SEEN} = shift;
                   2193:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63      bowersj2 2194:     $self->{CONDITION} = shift;
1.89      bowersj2 2195:     $self->{DIRECTION} = shift || FORWARD();
1.51      bowersj2 2196: 
                   2197:     # Flag: Have we started yet? If not, the first action is to return BEGIN_MAP.
                   2198:     $self->{STARTED} = 0;
                   2199: 
                   2200:     # Should we continue calling the recursive iterator, if any?
                   2201:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2202:     # The recursive iterator, if any
                   2203:     $self->{RECURSIVE_ITERATOR} = undef;
                   2204:     # Are we recursing on a map, or a branch?
                   2205:     $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
                   2206:     # And the count of how deep it is, so that this iterator can keep track of
                   2207:     # when to pick back up again.
                   2208:     $self->{RECURSIVE_DEPTH} = 0;
                   2209: 
                   2210:     # For keeping track of our branches, we maintain our own stack
                   2211:     $self->{BRANCH_STACK} = [];
                   2212:     # If the size shrinks, we exhausted a branch
                   2213:     $self->{BRANCH_STACK_SIZE} = 0;
                   2214:     $self->{BRANCH_DEPTH} = 0;
                   2215: 
                   2216:     # For returning two things in a forced sequence
                   2217:     $self->{FORCE_NEXT} = undef;
                   2218: 
                   2219:     # Start with the first resource
1.89      bowersj2 2220:     if ($self->{DIRECTION} == FORWARD) {
                   2221:         push @{$self->{BRANCH_STACK}}, $self->{FIRST_RESOURCE};
                   2222:     } else {
1.90      bowersj2 2223:         push @{$self->{BRANCH_STACK}}, $self->{FINISH_RESOURCE};
1.89      bowersj2 2224:     }
1.51      bowersj2 2225:     $self->{BRANCH_STACK_SIZE} = 1;
                   2226: 
                   2227:     bless($self);
                   2228:     return $self;
                   2229: }
                   2230: 
                   2231: # Note... this function is *touchy*. I strongly recommend tracing
                   2232: # through it with the debugger a few times on a non-trivial map before
                   2233: # modifying it. Order is *everything*.
                   2234: sub next {
                   2235:     my $self = shift;
                   2236:     
                   2237:     # Iterator logic goes here
                   2238: 
                   2239:     # Are we using a recursive iterator? If so, pull from that and
                   2240:     # watch the depth; we want to resume our level at the correct time.
                   2241:     if ($self->{RECURSIVE_ITERATOR_FLAG})
                   2242:     {
                   2243:         # grab the next from the recursive iterator
                   2244:         my $next = $self->{RECURSIVE_ITERATOR}->next();
                   2245:         
                   2246:         # is it a begin or end map? Update depth if so
                   2247:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
                   2248:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
                   2249: 
                   2250:         # Are we back at depth 0? If so, stop recursing.
                   2251:         if ($self->{RECURSIVE_DEPTH} == 0) {
                   2252:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2253:         }
                   2254:         
                   2255:         return $next;
                   2256:     }
                   2257: 
1.68      bowersj2 2258:     # Is this return value pre-determined?
                   2259:     if (defined($self->{FORCE_NEXT})) {
                   2260:         my $tmp = $self->{FORCE_NEXT};
                   2261:         $self->{FORCE_NEXT} = undef;
                   2262:         return $tmp;
                   2263:     }
                   2264: 
1.51      bowersj2 2265:     # Is there a current resource to grab? If not, then return
                   2266:     # END_BRANCH and END_MAP in succession.
                   2267:     if (scalar(@{$self->{BRANCH_STACK}}) == 0) {
                   2268:         if ($self->{BRANCH_DEPTH} > 0) {
                   2269:             $self->{FORCE_NEXT} = $self->END_MAP();
1.52      bowersj2 2270:             $self->{BRANCH_DEPTH}--;
1.51      bowersj2 2271:             return $self->END_BRANCH();
                   2272:         } else {
                   2273:             return $self->END_MAP();
                   2274:         }
                   2275:     }
                   2276: 
                   2277:     # Have we not yet begun? If not, return BEGIN_MAP and 
                   2278:     # remember that we've started.
                   2279:     if ( !$self->{STARTED} ) {
                   2280:         $self->{STARTED} = 1;
                   2281:         return $self->BEGIN_MAP;
                   2282:     }
                   2283: 
                   2284:     # Did the branch stack shrink since last run? If so,
                   2285:     # we exhausted a branch last time, therefore, we're about
                   2286:     # to start a new one. (We know because we already checked to see
                   2287:     # if the stack was empty.)
                   2288:     if ( scalar (@{$self->{BRANCH_STACK}}) < $self->{BRANCH_STACK_SIZE}) {
1.52      bowersj2 2289:         $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
                   2290:         $self->{BRANCH_DEPTH}++;
                   2291:         return $self->BEGIN_BRANCH();
1.51      bowersj2 2292:     }
                   2293: 
1.52      bowersj2 2294:     # Remember the size for comparision next time.
                   2295:     $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
                   2296: 
                   2297:     # If the next resource we mean to return is going to need
                   2298:     # a lower branch level, terminate branches until we get 
                   2299:     # there.
                   2300: 
1.51      bowersj2 2301:     # Get the next resource in the branch
                   2302:     $self->{HERE} = pop @{$self->{BRANCH_STACK}};
1.52      bowersj2 2303: 
                   2304:     # Are we at the right depth? If not, close a branch and return
                   2305:     # the current resource onto the branch stack
                   2306:     if (defined($self->{HERE}->{DATA}->{ITERATOR_DEPTH})
                   2307:         && $self->{HERE}->{DATA}->{ITERATOR_DEPTH} <
                   2308:         $self->{BRANCH_DEPTH} ) {
                   2309:         $self->{BRANCH_DEPTH}--;
                   2310:         # return it so we can pick it up eventually
                   2311:         push @{$self->{BRANCH_STACK}}, $self->{HERE};
                   2312:         return $self->END_BRANCH();
                   2313:     }
                   2314: 
1.51      bowersj2 2315:     # We always return it after this point and never before
                   2316:     # (proof: look at just the return statements), so we
                   2317:     # remember that we've seen this.
                   2318:     $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
                   2319:     
                   2320:     # Get the next possible resources
1.90      bowersj2 2321:     my $nextUnfiltered;
1.89      bowersj2 2322:     if ($self->{DIRECTION} == FORWARD()) {
1.90      bowersj2 2323:         $nextUnfiltered = $self->{HERE}->getNext();
1.89      bowersj2 2324:     } else {
1.90      bowersj2 2325:         $nextUnfiltered = $self->{HERE}->getPrevious();
1.89      bowersj2 2326:     }
1.51      bowersj2 2327:     my $next = [];
                   2328: 
                   2329:     # filter the next possibilities to remove things we've 
1.52      bowersj2 2330:     # already seen. Also, remember what branch depth they should
                   2331:     # be displayed at, since there's no other reliable way to tell.
1.51      bowersj2 2332:     foreach (@$nextUnfiltered) {
1.52      bowersj2 2333:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
                   2334:             push @$next, $_;
                   2335:             $_->{DATA}->{ITERATOR_DEPTH} = 
                   2336:                 $self->{BRANCH_DEPTH} + 1;
                   2337:         }
1.51      bowersj2 2338:     }
                   2339: 
                   2340:     # Handle branch cases:
                   2341:     # Nothing is available next: BRANCH_END
                   2342:     # 1 thing next: standard non-branch
                   2343:     # 2+ things next: have some branches
                   2344:     my $nextCount = scalar(@$next);
                   2345:     if ($nextCount == 0) {
1.52      bowersj2 2346:         # Return this and on the next run, close the branch up if we're 
                   2347:         # in a branch
                   2348:         if ($self->{BRANCH_DEPTH} > 0 ) {
                   2349:             $self->{FORCE_NEXT} = $self->END_BRANCH();
                   2350:             $self->{BRANCH_DEPTH}--;
                   2351:         }
1.51      bowersj2 2352:     }
                   2353:     
                   2354:     while (@$next) {
                   2355:         # copy the next possibilities over to the branch stack
                   2356:         # in the right order
1.52      bowersj2 2357:         push @{$self->{BRANCH_STACK}}, shift @$next;
1.51      bowersj2 2358:     }
                   2359: 
                   2360:     if ($nextCount >= 2) {
1.52      bowersj2 2361:         $self->{FORCE_NEXT} = $self->BEGIN_BRANCH();
                   2362:         $self->{BRANCH_DEPTH}++;
1.51      bowersj2 2363:         return $self->{HERE}; 
                   2364:     }
                   2365: 
                   2366:     # If this is a map and we want to recurse down it... (not filtered out)
1.70      bowersj2 2367:     if ($self->{HERE}->is_map() && 
1.63      bowersj2 2368:          (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) { 
1.51      bowersj2 2369:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
                   2370:         my $firstResource = $self->{HERE}->map_start();
                   2371:         my $finishResource = $self->{HERE}->map_finish();
                   2372: 
                   2373:         $self->{RECURSIVE_ITERATOR} =
1.94      bowersj2 2374:           Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource, 
1.63      bowersj2 2375:                      $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91      bowersj2 2376:                                              $self->{CONDITION}, $self->{DIRECTION});
1.51      bowersj2 2377:     }
                   2378: 
                   2379:     return $self->{HERE};
                   2380: }
                   2381: 
                   2382: =pod
                   2383: 
                   2384: 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).
                   2385: 
                   2386: =cut
                   2387: 
                   2388: sub getStack {
                   2389:     my $self=shift;
                   2390: 
                   2391:     my @stack;
                   2392: 
                   2393:     $self->populateStack(\@stack);
                   2394: 
                   2395:     return \@stack;
                   2396: }
                   2397: 
                   2398: # Private method: Calls the iterators recursively to populate the stack.
                   2399: sub populateStack {
                   2400:     my $self=shift;
                   2401:     my $stack = shift;
                   2402: 
1.88      bowersj2 2403:     push @$stack, $self->{HERE} if ($self->{HERE});
1.51      bowersj2 2404: 
                   2405:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
                   2406:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
                   2407:     }
                   2408: }
                   2409: 
1.1       www      2410: 1;
1.2       www      2411: 
1.51      bowersj2 2412: package Apache::lonnavmaps::resource;
                   2413: 
                   2414: use Apache::lonnet;
                   2415: 
                   2416: =pod
                   2417: 
                   2418: =head1 Object: resource
                   2419: 
                   2420: 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.
                   2421: 
                   2422: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
                   2423: 
                   2424: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
                   2425: 
                   2426: =head2 Public Members
                   2427: 
                   2428: 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.
                   2429: 
                   2430: =head2 Methods
                   2431: 
                   2432: =over 4
                   2433: 
1.70      bowersj2 2434: =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. Very rarely, if ever, called directly. Use the nav map->getByID() method.
                   2435: 
                   2436: =back
1.51      bowersj2 2437: 
                   2438: =cut
                   2439: 
                   2440: sub new {
                   2441:     # magic invocation to create a class instance
                   2442:     my $proto = shift;
                   2443:     my $class = ref($proto) || $proto;
                   2444:     my $self = {};
                   2445: 
                   2446:     $self->{NAV_MAP} = shift;
                   2447:     $self->{ID} = shift;
                   2448: 
                   2449:     # Store this new resource in the parent nav map's cache.
                   2450:     $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66      bowersj2 2451:     $self->{RESOURCE_ERROR} = 0;
1.51      bowersj2 2452: 
                   2453:     # A hash that can be used by two-pass algorithms to store data
                   2454:     # about this resource in. Not used by the resource object
                   2455:     # directly.
                   2456:     $self->{DATA} = {};
                   2457:    
                   2458:     bless($self);
                   2459:     
                   2460:     return $self;
                   2461: }
                   2462: 
1.70      bowersj2 2463: # private function: simplify the NAV_HASH lookups we keep doing
                   2464: # pass the name, and to automatically append my ID, pass a true val on the
                   2465: # second param
                   2466: sub navHash {
                   2467:     my $self = shift;
                   2468:     my $param = shift;
                   2469:     my $id = shift;
                   2470:     return $self->{NAV_MAP}->{NAV_HASH}->{$param . ($id?$self->{ID}:"")};
                   2471: }
                   2472: 
1.51      bowersj2 2473: =pod
                   2474: 
1.70      bowersj2 2475: B<Metadata Retreival>
                   2476: 
                   2477: These are methods that help you retrieve metadata about the resource:
                   2478: 
                   2479: =over 4
                   2480: 
                   2481: =item * B<ext>: Returns true if the resource is external.
                   2482: 
                   2483: =item * B<goesto>: Returns the "goesto" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
                   2484: 
                   2485: =item * B<kind>: Returns the kind of the resource from the compiled nav map.
                   2486: 
                   2487: =item * B<randomout>: Returns true if this resource was chosen to NOT be shown to the user by the random map selection feature.
1.51      bowersj2 2488: 
1.70      bowersj2 2489: =item * B<randompick>: Returns true for a map if the randompick feature is being used on the map. (?)
                   2490: 
                   2491: =item * B<src>: Returns the source for the resource.
                   2492: 
                   2493: =item * B<symb>: Returns the symb for the resource.
                   2494: 
                   2495: =item * B<title>: Returns the title of the resource.
                   2496: 
                   2497: =item * B<to>: Returns the "to" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
1.51      bowersj2 2498: 
                   2499: =back
                   2500: 
                   2501: =cut
                   2502: 
                   2503: # These info functions can be used directly, as they don't return
                   2504: # resource information.
1.85      bowersj2 2505: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70      bowersj2 2506: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85      bowersj2 2507: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51      bowersj2 2508: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
                   2509: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68      bowersj2 2510: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
                   2511: sub randompick { 
                   2512:     my $self = shift;
                   2513:     return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
                   2514:                                                '.0.parameter_randompick'};
                   2515: }
1.51      bowersj2 2516: sub src { 
                   2517:     my $self=shift;
                   2518:     return $self->navHash("src_", 1);
                   2519: }
                   2520: sub symb {
                   2521:     my $self=shift;
                   2522:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
                   2523:     my $symbSrc = &Apache::lonnet::declutter($self->src());
                   2524:     return &Apache::lonnet::declutter(
                   2525:          $self->navHash('map_id_'.$first)) 
                   2526:         . '___' . $second . '___' . $symbSrc;
                   2527: }
1.70      bowersj2 2528: sub title { my $self=shift; return $self->navHash("title_", 1); }
                   2529: sub to { my $self=shift; return $self->navHash("to_", 1); }
                   2530: 
                   2531: =pod
                   2532: 
                   2533: B<Predicate Testing the Resource>
                   2534: 
                   2535: These methods are shortcuts to deciding if a given resource has a given property.
                   2536: 
                   2537: =over 4
                   2538: 
                   2539: =item * B<is_map>: Returns true if the resource is a map type.
                   2540: 
                   2541: =item * B<is_problem>: Returns true if the resource is a problem type, false otherwise. (Looks at the extension on the src field; might need more to work correctly.)
                   2542: 
                   2543: =item * B<is_page>: Returns true if the resource is a page.
                   2544: 
                   2545: =item * B<is_problem>: Returns true if the resource is a problem.
                   2546: 
                   2547: =item * B<is_sequence>: Returns true if the resource sequence.
                   2548: 
                   2549: =back
                   2550: 
                   2551: =cut
                   2552: 
                   2553: 
                   2554: sub is_html {
1.51      bowersj2 2555:     my $self=shift;
                   2556:     my $src = $self->src();
1.70      bowersj2 2557:     return ($src =~ /html$/);
1.51      bowersj2 2558: }
1.70      bowersj2 2559: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
                   2560: sub is_page {
1.51      bowersj2 2561:     my $self=shift;
                   2562:     my $src = $self->src();
1.70      bowersj2 2563:     return ($src =~ /page$/);
1.51      bowersj2 2564: }
1.70      bowersj2 2565: sub is_problem {
1.51      bowersj2 2566:     my $self=shift;
                   2567:     my $src = $self->src();
1.70      bowersj2 2568:     return ($src =~ /problem$/);
1.51      bowersj2 2569: }
1.70      bowersj2 2570: sub is_sequence {
1.51      bowersj2 2571:     my $self=shift;
                   2572:     my $src = $self->src();
1.70      bowersj2 2573:     return ($src =~ /sequence$/);
1.51      bowersj2 2574: }
                   2575: 
                   2576: 
                   2577: 
                   2578: 
1.70      bowersj2 2579: 
1.51      bowersj2 2580: # Move this into POD: In order to use these correctly, courseopt
                   2581: # and useropt need to be generated
                   2582: sub parmval {
                   2583:     my $self = shift;
                   2584:     my $what = shift;
                   2585:     my $part = shift || "0";
                   2586:     return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
                   2587: }
                   2588: 
1.70      bowersj2 2589: =pod
                   2590: 
                   2591: B<Map Methods>
                   2592: 
                   2593: These methods are useful for getting information about the map properties of the resource, if the resource is a map (B<is_map>).
                   2594: 
                   2595: =over 4
                   2596: 
                   2597: =item * B<map_finish>: Returns a reference to a resource object corresponding to the finish resource of the map.
                   2598: 
                   2599: =item * B<map_pc>: Returns the pc value of the map, which is the first number that appears in the resource ID of the resources in the map, and is the number that appears around the middle of the symbs of the resources in that map.
                   2600: 
                   2601: =item * B<map_start>: Returns a reference to a resource object corresponding to the start resource of the map.
                   2602: 
                   2603: =item * B<map_type>: Returns a string with the type of the map in it.
                   2604: 
                   2605: =back
1.51      bowersj2 2606: 
1.70      bowersj2 2607: =cut
1.51      bowersj2 2608: 
                   2609: sub map_finish {
                   2610:     my $self = shift;
                   2611:     my $src = $self->src();
                   2612:     my $res = $self->navHash("map_finish_$src", 0);
                   2613:     $res = $self->{NAV_MAP}->getById($res);
                   2614:     return $res;
                   2615: }
1.70      bowersj2 2616: sub map_pc {
                   2617:     my $self = shift;
                   2618:     my $src = $self->src();
                   2619:     return $self->navHash("map_pc_$src", 0);
                   2620: }
1.51      bowersj2 2621: sub map_start {
                   2622:     my $self = shift;
                   2623:     my $src = $self->src();
                   2624:     my $res = $self->navHash("map_start_$src", 0);
                   2625:     $res = $self->{NAV_MAP}->getById($res);
                   2626:     return $res;
                   2627: }
                   2628: sub map_type {
                   2629:     my $self = shift;
                   2630:     my $pc = $self->map_pc();
                   2631:     return $self->navHash("map_type_$pc", 0);
                   2632: }
                   2633: 
                   2634: 
                   2635: 
                   2636: #####
                   2637: # Property queries
                   2638: #####
                   2639: 
                   2640: # These functions will be responsible for returning the CORRECT
                   2641: # VALUE for the parameter, no matter what. So while they may look
                   2642: # like direct calls to parmval, they can be more then that.
                   2643: # So, for instance, the duedate function should use the "duedatetype"
                   2644: # information, rather then the resource object user.
                   2645: 
                   2646: =pod
                   2647: 
                   2648: =head2 Resource Parameters
                   2649: 
1.70      bowersj2 2650: 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 parameter.
1.51      bowersj2 2651: 
                   2652: 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.)
                   2653: 
                   2654: =over 4
                   2655: 
                   2656: =item * B<acc>: Get the Client IP/Name Access Control information.
                   2657: 
                   2658: =item * B<answerdate>: Get the answer-reveal date for the problem.
                   2659: 
                   2660: =item * B<duedate>: Get the due date for the problem.
                   2661: 
                   2662: =item * B<tries>: Get the number of tries the student has used on the problem.
                   2663: 
                   2664: =item * B<maxtries>: Get the number of max tries allowed.
                   2665: 
                   2666: =item * B<opendate>: Get the open date for the problem.
                   2667: 
                   2668: =item * B<sig>: Get the significant figures setting.
                   2669: 
                   2670: =item * B<tol>: Get the tolerance for the problem.
                   2671: 
1.70      bowersj2 2672: =item * B<tries>: Get the number of tries the user has already used on the problem.
                   2673: 
1.51      bowersj2 2674: =item * B<type>: Get the question type for the problem.
                   2675: 
                   2676: =item * B<weight>: Get the weight for the problem.
                   2677: 
                   2678: =back
                   2679: 
                   2680: =cut
                   2681: 
                   2682: sub acc {
                   2683:     (my $self, my $part) = @_;
                   2684:     return $self->parmval("acc", $part);
                   2685: }
                   2686: sub answerdate {
                   2687:     (my $self, my $part) = @_;
                   2688:     # Handle intervals
                   2689:     if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
                   2690:         return $self->duedate($part) + 
                   2691:             $self->parmval("answerdate", $part);
                   2692:     }
                   2693:     return $self->parmval("answerdate", $part);
                   2694: }
                   2695: sub duedate {
                   2696:     (my $self, my $part) = @_;
                   2697:     return $self->parmval("duedate", $part);
                   2698: }
                   2699: sub maxtries {
                   2700:     (my $self, my $part) = @_;
                   2701:     return $self->parmval("maxtries", $part);
                   2702: }
                   2703: sub opendate {
                   2704:     (my $self, my $part) = @_;
                   2705:     if ($self->parmval("opendate.type", $part) eq 'date_interval') {
                   2706:         return $self->duedate($part) -
                   2707:             $self->parmval("opendate", $part);
                   2708:     }
                   2709:     return $self->parmval("opendate");
                   2710: }
                   2711: sub sig {
                   2712:     (my $self, my $part) = @_;
                   2713:     return $self->parmval("sig", $part);
                   2714: }
                   2715: sub tol {
                   2716:     (my $self, my $part) = @_;
                   2717:     return $self->parmval("tol", $part);
                   2718: }
                   2719: sub tries {
                   2720:     my $self = shift;
                   2721:     my $part = shift;
                   2722:     $part = '0' if (!defined($part));
                   2723: 
                   2724:     # Make sure return hash is loaded, should error check
                   2725:     $self->getReturnHash();
                   2726:     
                   2727:     my $tries = $self->{RETURN_HASH}->{'resource.'.$part.'.tries'};
                   2728:     if (!defined($tries)) {return '0';}
                   2729:     return $tries;
                   2730: }
1.70      bowersj2 2731: sub type {
                   2732:     (my $self, my $part) = @_;
                   2733:     return $self->parmval("type", $part);
                   2734: }
                   2735: sub weight {
                   2736:     (my $self, my $part) = @_;
                   2737:     return $self->parmval("weight", $part);
                   2738: }
1.51      bowersj2 2739: 
                   2740: # Multiple things need this
                   2741: sub getReturnHash {
                   2742:     my $self = shift;
                   2743:     
                   2744:     if (!defined($self->{RETURN_HASH})) {
1.84      bowersj2 2745:         my %tmpHash  = &Apache::lonnet::restore($self->symb());
1.51      bowersj2 2746:         $self->{RETURN_HASH} = \%tmpHash;
                   2747:     }
                   2748: }       
                   2749: 
                   2750: ######
                   2751: # Status queries
                   2752: ######
                   2753: 
                   2754: # These methods query the status of problems.
                   2755: 
                   2756: # If we need to count parts, this function determines the number of
                   2757: # parts from the metadata. When called, it returns a reference to a list
                   2758: # of strings corresponding to the parts. (Thus, using it in a scalar context
                   2759: # tells you how many parts you have in the problem:
                   2760: # $partcount = scalar($resource->countParts());
                   2761: # Don't use $self->{PARTS} directly because you don't know if it's been
                   2762: # computed yet.
                   2763: 
                   2764: =pod
                   2765: 
                   2766: =head2 Resource misc
                   2767: 
                   2768: Misc. functions for the resource.
                   2769: 
                   2770: =over 4
                   2771: 
1.59      bowersj2 2772: =item * B<hasDiscussion>: Returns a false value if there has been discussion since the user last logged in, true if there has. Always returns false if the discussion data was not extracted when the nav map was constructed.
                   2773: 
                   2774: =item * B<getFeedback>: Gets the feedback for the resource and returns the raw feedback string for the resource, or the null string if there is no feedback or the email data was not extracted when the nav map was constructed. Usually used like this:
                   2775: 
                   2776:  for (split(/\,/, $res->getFeedback())) {
                   2777:     my $link = &Apache::lonnet::escape($_);
                   2778:     ...
                   2779: 
                   2780: and use the link as appropriate.
                   2781: 
                   2782: =cut
                   2783: 
                   2784: sub hasDiscussion {
                   2785:     my $self = shift;
                   2786:     return $self->{NAV_MAP}->hasDiscussion($self->symb());
                   2787: }
                   2788: 
                   2789: sub getFeedback {
                   2790:     my $self = shift;
1.85      bowersj2 2791:     return $self->{NAV_MAP}->getFeedback($self->src());
1.59      bowersj2 2792: }
                   2793: 
                   2794: =pod
                   2795: 
1.70      bowersj2 2796: =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. Filtering part 0 if you want it is up to you.)
1.51      bowersj2 2797: 
1.70      bowersj2 2798: =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. Thus, B<parts> may return an array with fewer parts in it then countParts might lead you to believe.
1.51      bowersj2 2799: 
                   2800: =back
                   2801: 
                   2802: =cut
                   2803: 
                   2804: sub parts {
                   2805:     my $self = shift;
                   2806: 
1.67      bowersj2 2807:     if ($self->ext) { return ['0']; }
                   2808: 
1.51      bowersj2 2809:     $self->extractParts();
                   2810:     return $self->{PARTS};
                   2811: }
                   2812: 
                   2813: sub countParts {
                   2814:     my $self = shift;
                   2815:     
                   2816:     my $parts = $self->parts();
1.66      bowersj2 2817: 
                   2818:     if ($self->{RESOURCE_ERROR}) {
                   2819:         return 0;
                   2820:     }
                   2821: 
                   2822:     if (scalar(@{$parts}) < 2) { return 1;}
1.51      bowersj2 2823: 
                   2824:     return scalar(@{$parts}) - 1;
                   2825: }
                   2826: 
                   2827: # Private function: Extracts the parts information and saves it
                   2828: sub extractParts { 
                   2829:     my $self = shift;
                   2830:     
                   2831:     return if ($self->{PARTS});
1.67      bowersj2 2832:     return if ($self->ext);
1.51      bowersj2 2833: 
                   2834:     $self->{PARTS} = [];
                   2835: 
1.82      bowersj2 2836:     # Retrieve part count, if this is a problem
                   2837:     if ($self->is_problem()) {
                   2838:         my $metadata = &Apache::lonnet::metadata($self->src(), 'allpossiblekeys');
                   2839:         if (!$metadata) {
                   2840:             $self->{RESOURCE_ERROR} = 1;
                   2841:             $self->{PARTS} = [];
                   2842:             return;
                   2843:         }
                   2844:         
                   2845:         foreach (split(/\,/,$metadata)) {
                   2846:             if ($_ =~ /^parameter\_(.*)\_opendate$/) {
                   2847:                 push @{$self->{PARTS}}, $1;
                   2848:             }
1.51      bowersj2 2849:         }
1.82      bowersj2 2850:         
                   2851:         
                   2852:         # Is this possible to do in one line? - Jeremy
                   2853:         my @sortedParts = sort @{$self->{PARTS}};
                   2854:         $self->{PARTS} = \@sortedParts;
1.51      bowersj2 2855:     }
                   2856: 
                   2857:     return;
                   2858: }
                   2859: 
                   2860: =pod
                   2861: 
                   2862: =head2 Resource Status
                   2863: 
                   2864: 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.
                   2865: 
                   2866: There are two aspects to the status: the date-related information and the completion information.
                   2867: 
                   2868: Idiomatic usage of these two methods would probably look something like
                   2869: 
                   2870:  foreach ($resource->parts()) {
                   2871:     my $dateStatus = $resource->getDateStatus($_);
                   2872:     my $completionStatus = $resource->getCompletionStatus($_);
                   2873: 
1.70      bowersj2 2874:     or
                   2875: 
                   2876:     my $status = $resource->status($_);
                   2877: 
1.51      bowersj2 2878:     ... use it here ...
                   2879:  }
                   2880: 
                   2881: =over 4
                   2882: 
1.70      bowersj2 2883: =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. The possible return values are:
1.51      bowersj2 2884: 
                   2885: =back
                   2886: 
                   2887: B<Date Codes>
                   2888: 
                   2889: =over 4
                   2890: 
                   2891: =item * B<OPEN_LATER>: The problem will be opened later.
                   2892: 
                   2893: =item * B<OPEN>: Open and not yet due.
                   2894: 
1.59      bowersj2 2895: 
1.54      bowersj2 2896: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
                   2897: 
                   2898: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51      bowersj2 2899: 
                   2900: =item * B<ANSWER_OPEN>: The answer date is here.
                   2901: 
                   2902: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
                   2903: 
                   2904: =back
                   2905: 
                   2906: =cut
                   2907: 
                   2908: # Apparently the compiler optimizes these into constants automatically
1.54      bowersj2 2909: sub OPEN_LATER             { return 0; }
                   2910: sub OPEN                   { return 1; }
                   2911: sub PAST_DUE_NO_ANSWER     { return 2; }
                   2912: sub PAST_DUE_ANSWER_LATER  { return 3; }
                   2913: sub ANSWER_OPEN            { return 4; }
                   2914: sub NOTHING_SET            { return 5; } 
                   2915: sub NETWORK_FAILURE        { return 100; }
                   2916: 
                   2917: # getDateStatus gets the date status for a given problem part. 
                   2918: # Because answer date, due date, and open date are fully independent
                   2919: # (i.e., it is perfectly possible to *only* have an answer date), 
                   2920: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
                   2921: # (past, future, none given). This function handles this with a decision
                   2922: # tree. Read the comments to follow the decision tree.
1.51      bowersj2 2923: 
                   2924: sub getDateStatus {
                   2925:     my $self = shift;
                   2926:     my $part = shift;
                   2927:     $part = "0" if (!defined($part));
1.54      bowersj2 2928: 
                   2929:     # Always return network failure if there was one.
1.51      bowersj2 2930:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   2931: 
                   2932:     my $now = time();
                   2933: 
1.53      bowersj2 2934:     my $open = $self->opendate($part);
                   2935:     my $due = $self->duedate($part);
                   2936:     my $answer = $self->answerdate($part);
                   2937: 
                   2938:     if (!$open && !$due && !$answer) {
                   2939:         # no data on the problem at all
                   2940:         # should this be the same as "open later"? think multipart.
                   2941:         return $self->NOTHING_SET;
                   2942:     }
1.59      bowersj2 2943:     if (!$open || $now < $open) {return $self->OPEN_LATER}
                   2944:     if (!$due || $now < $due) {return $self->OPEN}
                   2945:     if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
                   2946:     if ($answer) { return $self->ANSWER_OPEN; }
1.54      bowersj2 2947:     return PAST_DUE_NO_ANSWER;
1.51      bowersj2 2948: }
                   2949: 
                   2950: =pod
                   2951: 
                   2952: B<>
                   2953: 
                   2954: =over 4
                   2955: 
                   2956: =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:
                   2957: 
                   2958: =back 
                   2959: 
                   2960: B<Completion Codes>
                   2961: 
                   2962: =over 4
                   2963: 
                   2964: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
                   2965: 
                   2966: =item * B<INCORRECT>: Attempted, but wrong by student.
                   2967: 
                   2968: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
                   2969: 
                   2970: =item * B<CORRECT>: Correct or correct by instructor.
                   2971: 
                   2972: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
                   2973: 
                   2974: =item * B<EXCUSED>: Excused. Not yet implemented.
                   2975: 
                   2976: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
                   2977: 
1.69      bowersj2 2978: =item * B<ATTEMPTED>: Attempted, and not yet graded.
                   2979: 
1.51      bowersj2 2980: =back
                   2981: 
                   2982: =cut
                   2983: 
1.53      bowersj2 2984: sub NOT_ATTEMPTED         { return 10; }
                   2985: sub INCORRECT             { return 11; }
                   2986: sub INCORRECT_BY_OVERRIDE { return 12; }
                   2987: sub CORRECT               { return 13; }
                   2988: sub CORRECT_BY_OVERRIDE   { return 14; }
                   2989: sub EXCUSED               { return 15; }
1.69      bowersj2 2990: sub ATTEMPTED             { return 16; }
1.51      bowersj2 2991: 
                   2992: sub getCompletionStatus {
                   2993:     my $self = shift;
                   2994:     my $part = shift;
                   2995:     $part = "0" if (!defined($part));
                   2996:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   2997: 
                   2998:     # Make sure return hash exists
                   2999:     $self->getReturnHash();
                   3000:     
                   3001:     my $status = $self->{RETURN_HASH}->{'resource.'.$part.'.solved'};
                   3002: 
                   3003:     # Left as seperate if statements in case we ever do more with this
                   3004:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
                   3005:     if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
                   3006:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
                   3007:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
                   3008:     if ($status eq 'excused') {return $self->EXCUSED; }
1.69      bowersj2 3009:     if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51      bowersj2 3010:     return $self->NOT_ATTEMPTED;
                   3011: }
                   3012: 
                   3013: =pod
                   3014: 
                   3015: B<Composite Status>
                   3016: 
                   3017: 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()):
                   3018: 
                   3019: =over 4
                   3020: 
1.70      bowersj2 3021: =item * B<NETWORK_FAILURE>: The network has failed and the information is not available.
1.51      bowersj2 3022: 
1.70      bowersj2 3023: =item * B<NOTHING_SET>: No dates have been set for this problem (part) at all. (Because only certain parts of a multi-part problem may be assigned, this can not be collapsed into "open later", as we don't know a given part will EVER be opened. For single part, this is the same as "OPEN_LATER".)
1.53      bowersj2 3024: 
1.70      bowersj2 3025: =item * B<CORRECT>: For any reason at all, the part is considered correct.
1.51      bowersj2 3026: 
1.70      bowersj2 3027: =item * B<EXCUSED>: For any reason at all, the problem is excused.
1.51      bowersj2 3028: 
1.70      bowersj2 3029: =item * B<PAST_DUE_NO_ANSWER>: The problem is past due, not considered correct, and no answer date is set.
1.54      bowersj2 3030: 
1.70      bowersj2 3031: =item * B<PAST_DUE_ANSWER_LATER>: The problem is past due, not considered correct, and an answer date in the future is set.
1.51      bowersj2 3032: 
1.70      bowersj2 3033: =item * B<ANSWER_OPEN>: The problem is past due, not correct, and the answer is now available.
1.51      bowersj2 3034: 
1.70      bowersj2 3035: =item * B<OPEN_LATER>: The problem is not yet open.
1.51      bowersj2 3036: 
1.70      bowersj2 3037: =item * B<TRIES_LEFT>: The problem is open, has been tried, is not correct, but there are tries left.
1.51      bowersj2 3038: 
1.70      bowersj2 3039: =item * B<INCORRECT>: The problem is open, and all tries have been used without getting the correct answer.
1.51      bowersj2 3040: 
1.70      bowersj2 3041: =item * B<OPEN>: The item is open and not yet tried.
1.51      bowersj2 3042: 
1.70      bowersj2 3043: =item * B<ATTEMPTED>: The problem has been attempted.
1.69      bowersj2 3044: 
1.51      bowersj2 3045: =back
                   3046: 
                   3047: =cut
                   3048: 
                   3049: sub TRIES_LEFT { return 10; }
                   3050: 
                   3051: sub status {
                   3052:     my $self = shift;
                   3053:     my $part = shift;
                   3054:     if (!defined($part)) { $part = "0"; }
                   3055:     my $completionStatus = $self->getCompletionStatus($part);
                   3056:     my $dateStatus = $self->getDateStatus($part);
                   3057: 
                   3058:     # What we have is a two-dimensional matrix with 4 entries on one
                   3059:     # dimension and 5 entries on the other, which we want to colorize,
1.54      bowersj2 3060:     # plus network failure and "no date data at all".
1.51      bowersj2 3061: 
1.53      bowersj2 3062:     if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51      bowersj2 3063: 
                   3064:     # There are a few whole rows we can dispose of:
1.53      bowersj2 3065:     if ($completionStatus == CORRECT ||
                   3066:         $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69      bowersj2 3067:         return CORRECT; 
                   3068:     }
                   3069: 
                   3070:     if ($completionStatus == ATTEMPTED) {
                   3071:         return ATTEMPTED;
1.53      bowersj2 3072:     }
                   3073: 
                   3074:     # If it's EXCUSED, then return that no matter what
                   3075:     if ($completionStatus == EXCUSED) {
                   3076:         return EXCUSED; 
1.51      bowersj2 3077:     }
                   3078: 
1.53      bowersj2 3079:     if ($dateStatus == NOTHING_SET) {
                   3080:         return NOTHING_SET;
1.51      bowersj2 3081:     }
                   3082: 
1.69      bowersj2 3083:     # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
                   3084:     # by 4 matrix (date statuses).
1.51      bowersj2 3085: 
1.54      bowersj2 3086:     if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59      bowersj2 3087:         $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54      bowersj2 3088:         return $dateStatus; 
1.51      bowersj2 3089:     }
                   3090: 
1.53      bowersj2 3091:     if ($dateStatus == ANSWER_OPEN) {
                   3092:         return ANSWER_OPEN;
1.51      bowersj2 3093:     }
                   3094: 
                   3095:     # Now: (incorrect, incorrect_override, not_attempted) x 
                   3096:     # (open_later), (open)
                   3097:     
1.53      bowersj2 3098:     if ($dateStatus == OPEN_LATER) {
                   3099:         return OPEN_LATER;
1.51      bowersj2 3100:     }
                   3101: 
                   3102:     # If it's WRONG...
1.53      bowersj2 3103:     if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51      bowersj2 3104:         # and there are TRIES LEFT:
1.79      bowersj2 3105:         if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53      bowersj2 3106:             return TRIES_LEFT;
1.51      bowersj2 3107:         }
1.53      bowersj2 3108:         return INCORRECT; # otherwise, return orange; student can't fix this
1.51      bowersj2 3109:     }
                   3110: 
                   3111:     # Otherwise, it's untried and open
1.53      bowersj2 3112:     return OPEN; 
1.51      bowersj2 3113: }
                   3114: 
                   3115: =pod
                   3116: 
                   3117: =head2 Resource/Nav Map Navigation
                   3118: 
                   3119: =over 4
                   3120: 
1.85      bowersj2 3121: =item * B<getNext>($alreadySeenHashRef): Retreive an array of the possible next resources after this one. Always returns an array, even in the one- or zero-element case. The "alreadySeenHashRef" is an optional parameter that can be passed in to the method. If $$alreadySeenHashRef{$res->id()} is true in that hash, getNext will not return it in the list. In other words, you can use it to suppress resources you've already seen in the getNext method directly.
                   3122: 
                   3123: =item * B<getPrevious>($alreadySeenHashRef): Retreive an array of the possible previous resources from this one. Always returns an array, even in the one- or zero-element case. $alreadySeenHashRef is the same as in getNext.
1.51      bowersj2 3124: 
                   3125: =cut
                   3126: 
                   3127: sub getNext {
                   3128:     my $self = shift;
                   3129:     my $alreadySeenHash = shift;
                   3130:     my @branches;
                   3131:     my $to = $self->to();
1.85      bowersj2 3132:     foreach my $branch ( split(/,/, $to) ) {
1.51      bowersj2 3133:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3134:         my $next = $choice->goesto();
                   3135:         $next = $self->{NAV_MAP}->getById($next);
                   3136: 
1.61      bowersj2 3137:         # Don't remember it if we've already seen it or if
1.66      bowersj2 3138:         # the student doesn't have browse priviledges
                   3139:         my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.51      bowersj2 3140:         if (!defined($alreadySeenHash) ||
1.61      bowersj2 3141:             !defined($alreadySeenHash->{$next->{ID}}) ||
1.66      bowersj2 3142:             ($browsePriv ne '2' && $browsePriv ne 'F')) {
1.51      bowersj2 3143:                 push @branches, $next;
                   3144:             }
1.85      bowersj2 3145:     }
                   3146:     return \@branches;
                   3147: }
                   3148: 
                   3149: sub getPrevious {
                   3150:     my $self = shift;
1.87      www      3151:     my $alreadySeenHash = shift;
1.85      bowersj2 3152:     my @branches;
                   3153:     my $from = $self->from();
                   3154:     foreach my $branch ( split /,/, $from) {
                   3155:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3156:         my $prev = $choice->comesfrom();
                   3157:         $prev = $self->{NAV_MAP}->getById($prev);
                   3158: 
                   3159:         # Skip it if we've already seen it or the user doesn't have
                   3160:         # browse privs
                   3161:         my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
                   3162:         if (!defined($alreadySeenHash) ||
1.87      www      3163:             !defined($alreadySeenHash->{$prev->{ID}}) ||
1.85      bowersj2 3164:             ($browsePriv ne '2' && $browsePriv ne 'F')) {
1.87      www      3165:             push @branches, $prev;
1.85      bowersj2 3166:         }
1.51      bowersj2 3167:     }
                   3168:     return \@branches;
                   3169: }
                   3170: 
                   3171: =pod
1.2       www      3172: 
1.51      bowersj2 3173: =back
1.2       www      3174: 
1.51      bowersj2 3175: =cut
1.2       www      3176: 
1.51      bowersj2 3177: 1;
1.2       www      3178: 
1.51      bowersj2 3179: __END__
1.2       www      3180: 
                   3181: 

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