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

1.72      bowersj2    1: 
1.2       www         2: # The LearningOnline Network with CAPA
                      3: # Navigate Maps Handler
1.1       www         4: #
1.87    ! www         5: # $Id: lonnavmaps.pm,v 1.86 2002/10/24 19:10:07 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: 
                    823:     # Initialize the nav map
                    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'};
                    840:     if (defined($desc)) { $r->print("<h2>$desc</h2>\n"); }
                    841:     my $date=localtime;
                    842:     $r->print("<h3>$date</h3>\n");
                    843:     $r->rflush();
1.65      bowersj2  844:     if ($navmap->{LAST_CHECK}) {
1.66      bowersj2  845:         $r->print('<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.66      bowersj2  847:                   '<br><img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'); 
1.65      bowersj2  848:     } else {
1.66      bowersj2  849:         $r->print('<img src="/adm/lonMisc/chat.gif"> Discussions'.
                    850:                   '<br><img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'); 
1.65      bowersj2  851:     }
1.55      bowersj2  852: 
                    853:     # Check that it's defined
                    854:     if (!($navmap->courseMapDefined())) {
                    855:         $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
                    856:                   '</body></html>');
                    857:         return OK;
                    858:     }
                    859: 
1.51      bowersj2  860:     # Grab a resource object so we have access to the constants; this
                    861:     # is technically not proper, but should be harmless
                    862:     my $res = $navmap->firstResource();
                    863: 
                    864:     # Defines a status->color mapping, null string means don't color
                    865:     my %colormap = 
1.54      bowersj2  866:     ( $res->NETWORK_FAILURE        => '',
1.59      bowersj2  867:       $res->CORRECT                => '',
1.54      bowersj2  868:       $res->EXCUSED                => '#BBBBFF',
1.59      bowersj2  869:       $res->PAST_DUE_ANSWER_LATER  => '',
                    870:       $res->PAST_DUE_NO_ANSWER     => '',
                    871:       $res->ANSWER_OPEN            => '#CCFFCC',
1.54      bowersj2  872:       $res->OPEN_LATER             => '',
1.59      bowersj2  873:       $res->TRIES_LEFT             => '',
                    874:       $res->INCORRECT              => '',
                    875:       $res->OPEN                   => '',
1.54      bowersj2  876:       $res->NOTHING_SET            => ''        );
1.59      bowersj2  877:     # And a special case in the nav map; what to do when the assignment
                    878:     # is not yet done and due in less then 24 hours
1.86      bowersj2  879:     my $hurryUpColor = "#FF0000";
1.59      bowersj2  880: 
                    881:     my %statusIconMap = 
1.66      bowersj2  882:         ( $res->NETWORK_FAILURE    => '',
                    883:           $res->NOTHING_SET        => '',
                    884:           $res->CORRECT            => 'navmap.correct.gif',
                    885:           $res->EXCUSED            => 'navmap.correct.gif',
                    886:           $res->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif',
                    887:           $res->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif',
                    888:           $res->ANSWER_OPEN        => 'navmap.wrong.gif',
                    889:           $res->OPEN_LATER         => '',
                    890:           $res->TRIES_LEFT         => 'navmap.open.gif',
                    891:           $res->INCORRECT          => 'navmap.wrong.gif',
1.69      bowersj2  892:           $res->OPEN               => 'navmap.open.gif',
1.85      bowersj2  893:           $res->ATTEMPTED          => 'navmap.open.gif' );
1.69      bowersj2  894: 
                    895:     my %iconAltTags = 
                    896:         ( 'navmap.correct.gif' => 'Correct',
                    897:           'navmap.wrong.gif'   => 'Incorrect',
                    898:           'navmap.open.gif'    => 'Open' );
1.59      bowersj2  899: 
                    900:     my %condenseStatuses =
1.66      bowersj2  901:         ( $res->NETWORK_FAILURE    => 1,
                    902:           $res->NOTHING_SET        => 1,
                    903:           $res->CORRECT            => 1 );
1.51      bowersj2  904: 
                    905:     my %filterHash;
                    906:     # Figure out what we're not displaying
                    907:     foreach (split(/\,/, $ENV{"form.filter"})) {
                    908:         if ($_) {
                    909:             $filterHash{$_} = "1";
                    910:         }
                    911:     }
                    912: 
1.63      bowersj2  913:     my $currenturl = $ENV{'form.postdata'};
                    914:     $currenturl=~s/^http\:\/\///;
                    915:     $currenturl=~s/^[^\/]+//;
1.75      bowersj2  916:     # alreadyHere allows us to only open the maps necessary to view
                    917:     # the current location once, while at the same time remembering
                    918:     # the current location. Without that check, the user would never
                    919:     # be able to close those maps; the user would close it, and the
                    920:     # currenturl scan would re-open it.
                    921:     my $queryAdd = "postdata=" . &Apache::lonnet::escape($currenturl) .
                    922:         "&alreadyHere=1";
1.63      bowersj2  923: 
1.66      bowersj2  924:     $r->print('<a href="navmaps?condition=1&filter=">Show All Resources</a><br /><br />');
1.63      bowersj2  925: 
1.51      bowersj2  926:     # Begin the HTML table
1.59      bowersj2  927:     # four cols: resource + indent, chat+feedback, icon, text string
1.85      bowersj2  928:     $r->print('<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n");
1.51      bowersj2  929: 
1.66      bowersj2  930:     my $condition = 0;
                    931:     if ($ENV{'form.condition'}) {
1.68      bowersj2  932:         $condition = 1;
1.66      bowersj2  933:     }
                    934: 
1.74      bowersj2  935:     # This needs to be updated to use symbs from the remote, 
                    936:     # instead of uris. The changes to this and the main rendering
                    937:     # loop should be obvious.
                    938:     # Here's a simple example of the iterator.
                    939:     # If there is a current resource
1.75      bowersj2  940:     if ($currenturl && !$ENV{'form.alreadyHere'}) {
1.74      bowersj2  941:         # Give me every resource...
                    942:         my $mapIterator = $navmap->getIterator(undef, undef, {}, 1);
1.84      bowersj2  943:         my $found = 0;
1.74      bowersj2  944:         my $depth = 1;
                    945:         $mapIterator->next(); # discard the first BEGIN_MAP
                    946:         my $curRes = $mapIterator->next();
                    947:         
                    948:         while ($depth > 0 && !$found) {
                    949:             if (ref($curRes) && $curRes->src() eq $currenturl) {
                    950:                 # If this is the correct resource, be sure to 
                    951:                 # show it by making sure the containing maps
                    952:                 # are open.
                    953: 
                    954:                 my $mapStack = $mapIterator->getStack();
                    955:                 for my $map (@{$mapStack}) {
                    956:                     if ($condition) {
                    957:                         undef $filterHash{$map->map_pc()};
                    958:                     } else {
                    959:                         $filterHash{$map->map_pc()} = 1;
                    960:                     }
                    961:                 }
                    962:                 $found = 1;
                    963:             }
                    964:             $curRes = $mapIterator->next();
                    965:         }
                    966:     }
                    967: 
1.53      bowersj2  968:     undef $res; # so we don't accidentally use it later
1.72      bowersj2  969:     my $indentLevel = 0;
1.61      bowersj2  970:     my $indentString = "<img src=\"/adm/lonIcons/whitespace1.gif\" width=\"25\" height=\"1\" alt=\"\" border=\"0\" />";
1.51      bowersj2  971: 
                    972:     my $isNewBranch = 0;
1.59      bowersj2  973:     my $now = time();
                    974:     my $in24Hours = $now + 24 * 60 * 60;
1.72      bowersj2  975:     my $depth = 1;
1.78      bowersj2  976:     my $displayedHereMarker = 0;
1.72      bowersj2  977: 
                    978:     # We know the first thing is a BEGIN_MAP (see "$self->{STARTED}"
                    979:     # code in iterator->next), so ignore the first one
1.74      bowersj2  980:     my $mapIterator = $navmap->getIterator(undef, undef, \%filterHash,
                    981:                                            $condition);
1.72      bowersj2  982:     $mapIterator->next();
                    983:     my $curRes = $mapIterator->next();
1.51      bowersj2  984: 
1.72      bowersj2  985:     while ($depth > 0) {
1.51      bowersj2  986:         if ($curRes == $mapIterator->BEGIN_MAP() ||
1.52      bowersj2  987:             $curRes == $mapIterator->BEGIN_BRANCH()) {
1.51      bowersj2  988:             $indentLevel++;
                    989:         }
                    990:         if ($curRes == $mapIterator->END_MAP() ||
1.52      bowersj2  991:             $curRes == $mapIterator->END_BRANCH()) {
1.51      bowersj2  992:             $indentLevel--;
                    993:         }
1.52      bowersj2  994:         if ($curRes == $mapIterator->BEGIN_BRANCH()) {
                    995:             $isNewBranch = 1;
1.72      bowersj2  996:         }
                    997:         if ($curRes == $mapIterator->BEGIN_MAP()) {
                    998:             $depth++;
                    999:         }
                   1000:         if ($curRes == $mapIterator->END_MAP()) {
                   1001:             $depth--;
1.51      bowersj2 1002:         }
                   1003: 
1.68      bowersj2 1004:         # Is this resource being blotted out?
                   1005:         if (ref($curRes) && !advancedUser() && $curRes->randomout()) {
                   1006:             $curRes = $mapIterator->next();
                   1007:             next; # and totally ignore this resource
                   1008:         }
                   1009: 
1.51      bowersj2 1010:         if (ref($curRes) && $curRes->src()) {
                   1011: 
1.66      bowersj2 1012:             # Step one: Decide which parts to show
                   1013:             my @parts = @{$curRes->parts()};
                   1014:             my $multipart = scalar(@parts) > 1;
                   1015:             my $condensed = 0;
                   1016:                 
1.51      bowersj2 1017:             if ($curRes->is_problem()) {
1.66      bowersj2 1018:                 # Is it multipart?
1.59      bowersj2 1019: 
1.66      bowersj2 1020:                 if ($multipart) {
                   1021:                     # If it's multipart, see if part 0 is "open"
                   1022:                     # if it is, display all parts, if it isn't,
                   1023:                     # just display first
                   1024:                     if (!$curRes->opendate("0")) {
                   1025:                         @parts = ("0"); # just display the zero-th part
1.68      bowersj2 1026:                         $condensed = 1;
1.66      bowersj2 1027:                     } else {
                   1028:                         # Otherwise, only display part 0 if we want to 
                   1029:                         # attach feedback or email information to it
                   1030:                         if ($curRes->hasDiscussion() || $curRes->getFeedback()) {
                   1031:                             shift @parts;
                   1032:                         } else {
                   1033:                             # If there's discussion or feedback, that counts
                   1034:                             # as a difference, so display the parts.
                   1035:                             
                   1036:                             # Now, we decide whether to condense the
                   1037:                             # parts due to similarity
                   1038:                             my $status = $curRes->status($parts[1]);
                   1039:                             my $due = $curRes->duedate($parts[1]);
                   1040:                             my $open = $curRes->opendate($parts[1]);
                   1041:                             my $statusAllSame = 1;
                   1042:                             my $dueAllSame = 1;
                   1043:                             my $openAllSame = 1;
                   1044:                             for (my $i = 2; $i < scalar(@parts); $i++) {
                   1045:                                 if ($curRes->status($parts[$i]) != $status){
                   1046:                                     $statusAllSame = 0;
                   1047:                                 }
                   1048:                                 if ($curRes->duedate($parts[$i]) != $due ) {
                   1049:                                     $dueAllSame = 0;
                   1050:                                 }
                   1051:                                 if ($curRes->opendate($parts[$i]) != $open) {
                   1052:                                     $openAllSame = 0;
                   1053:                                 }
                   1054:                             }
                   1055:                             
                   1056:                             # $allSame is true if all the statuses were
                   1057:                             # the same. Now, if they are all the same and
                   1058:                             # match one of the statuses to condense, or they
                   1059:                             # are all open with the same due date, or they are
                   1060:                             # all OPEN_LATER with the same open date, display the
                   1061:                             # status of the first non-zero part (to get the 'correct'
                   1062:                             # status right, since 0 is never 'correct' or 'open').
                   1063:                             if (($statusAllSame && defined($condenseStatuses{$status})) ||
                   1064:                                 ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
                   1065:                                 ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
                   1066:                                 @parts = ($parts[1]);
                   1067:                                 $condensed = 1;
                   1068:                             }
                   1069:                         }
                   1070:                     }
                   1071:                 }
1.59      bowersj2 1072: 
1.51      bowersj2 1073:             } else {
1.84      bowersj2 1074:                 $parts[0] = "0"; # this is to get past foreach loop below
1.51      bowersj2 1075:                  # you can consider a non-problem resource as a resource
1.77      bowersj2 1076:                   # with only one part without loss
1.51      bowersj2 1077:             }
                   1078: 
1.83      bowersj2 1079:             # Is it a multipart problem with a single part, now in 
                   1080:             # @parts with "0" filtered out? If so, forget it's a multi-part
                   1081:             # problem and treat it like a single-part problem.
                   1082:             if ( scalar(@parts) == 1 ) {
                   1083:                 $multipart = 0;
                   1084:             }
                   1085: 
1.66      bowersj2 1086:             # Display one part, in event of network error.
                   1087:             # If this is a single part, we can at least show the correct
                   1088:             # status, but if it's multipart, we're lost.
                   1089:             if ($curRes->{RESOURCE_ERROR}) {
                   1090:                 @parts = ("0");
                   1091:             }
                   1092: 
                   1093:             # Step two: Show the parts
1.51      bowersj2 1094:             foreach my $part (@parts) {
1.59      bowersj2 1095: 
1.66      bowersj2 1096:                 my $deltalevel = 0; # for inserting the branch icon
1.68      bowersj2 1097:                 my $nonLinkedText = ""; # unlinked stuff after title
1.51      bowersj2 1098:                 
                   1099:                 # For each thing we're displaying...
                   1100: 
                   1101:                 my $stack = $mapIterator->getStack();
                   1102:                 my $src = getLinkForResource($stack);
                   1103: 
                   1104:                 my $srcHasQuestion = $src =~ /\?/;
                   1105:                 my $link = $src.
                   1106:                     ($srcHasQuestion?'&':'?') .
                   1107:                     'symb='.&Apache::lonnet::escape($curRes->symb()).
                   1108:                     '"';
                   1109:                 my $title = $curRes->title();
1.81      bowersj2 1110:                 if (!$title) {
                   1111:                     $title = $curRes->src();
                   1112:                     $title = substr ($title, rindex($title, "/") + 1);
                   1113:                 }
1.51      bowersj2 1114:                 my $partLabel = "";
1.52      bowersj2 1115:                 my $newBranchText = "";
                   1116: 
                   1117:                 # If this is a new branch, label it so
                   1118:                 # (temporary, this should be an icon w/ alt text)
                   1119:                 if ($isNewBranch) {
1.59      bowersj2 1120:                     $newBranchText = "<img src=\"/adm/lonIcons/branch.gif\" border=\"0\">";
1.52      bowersj2 1121:                     $isNewBranch = 0;
1.66      bowersj2 1122:                     $deltalevel = 1;
1.52      bowersj2 1123:                 }
1.51      bowersj2 1124: 
                   1125:                 # links to open and close the folders
                   1126:                 my $linkopen = "<a href=\"$link\">";
                   1127:                 my $linkclose = "</a>";
                   1128: 
1.61      bowersj2 1129:                 my $icon = "<img src=\"/adm/lonIcons/html.gif\" alt=\"\" border=\"0\" />";
1.51      bowersj2 1130:                 if ($curRes->is_problem()) { 
1.66      bowersj2 1131:                     if ($part eq "0" || $condensed) {
                   1132:                         $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border=\"0\" />'; 
                   1133:                     } else {
                   1134:                         $icon = $indentString;
                   1135:                     }
1.51      bowersj2 1136:                 }
                   1137: 
                   1138:                 # Display the correct icon, link to open or shut map
                   1139:                 if ($curRes->is_map()) { 
                   1140:                     my $mapId = $curRes->map_pc();
1.82      bowersj2 1141:                     my $nowOpen = (!defined($filterHash{$mapId}));
                   1142:                     if ($condition) {$nowOpen = !$nowOpen;}
1.51      bowersj2 1143:                     $icon = $nowOpen ?
1.77      bowersj2 1144:                         "navmap.folder.closed.gif" : "navmap.folder.open.gif";
1.66      bowersj2 1145:                     $icon = "<img src=\"/adm/lonIcons/$icon\" alt=\"\" border=\"0\" />";
1.52      bowersj2 1146:                     $linkopen = "<a href=\"/adm/navmaps?filter=";
1.66      bowersj2 1147:                     $linkopen .= ($nowOpen xor $condition) ? 
1.51      bowersj2 1148:                         addToFilter(\%filterHash, $mapId) :
                   1149:                         removeFromFilter(\%filterHash, $mapId);
1.66      bowersj2 1150:                     $linkopen .= "&condition=$condition&$queryAdd\">";
1.51      bowersj2 1151:                     $linkclose = "</a>";
1.68      bowersj2 1152: 
1.51      bowersj2 1153:                 }
                   1154:                 
                   1155:                 my $colorizer = "";
1.86      bowersj2 1156:                 my $color;
1.51      bowersj2 1157:                 if ($curRes->is_problem()) {
                   1158:                     my $status = $curRes->status($part);
1.86      bowersj2 1159:                     $color = $colormap{$status};
1.74      bowersj2 1160: 
                   1161:                     # Special case in the navmaps: If in less then
                   1162:                     # 24 hours, give it a bit of urgency
1.86      bowersj2 1163:                     if (($status == $curRes->OPEN() || $status == $curRes->ATTEMPTED() ||
                   1164:                          $status == $curRes->TRIES_LEFT())
                   1165:                         && $curRes->duedate() &&
1.76      bowersj2 1166:                         $curRes->duedate() < time()+(24*60*60) && 
                   1167:                         $curRes->duedate() > time()) {
1.74      bowersj2 1168:                         $color = $hurryUpColor;
                   1169:                     }
1.79      bowersj2 1170:                     # Special case: If this is the last try, and there is
                   1171:                     # more then one available, give a bit of urgency
                   1172:                     my $tries = $curRes->tries($part);
                   1173:                     my $maxtries = $curRes->maxtries($part);
                   1174:                     if ($tries && $maxtries && $maxtries > 1 &&
                   1175:                         $maxtries - $tries == 1) {
                   1176:                         $color = $hurryUpColor;
                   1177:                     }
1.51      bowersj2 1178:                     if ($color ne "") {
                   1179:                         $colorizer = "bgcolor=\"$color\"";
                   1180:                     }
                   1181:                 }
                   1182: 
1.68      bowersj2 1183:                 if ($curRes->randomout()) {
                   1184:                     $nonLinkedText .= ' <i>(hidden)</i> ';
                   1185:                 }
                   1186: 
1.66      bowersj2 1187:                 # FIRST COL: The resource indentation, branch icon, and name
1.84      bowersj2 1188:                 $r->print("  <tr><td align=\"left\" valign=\"center\" width=\"60%\">\n");
1.51      bowersj2 1189: 
                   1190:                 # print indentation
1.59      bowersj2 1191:                 for (my $i = 0; $i < $indentLevel - $deltalevel; $i++) {
1.51      bowersj2 1192:                     $r->print($indentString);
                   1193:                 }
                   1194: 
1.61      bowersj2 1195:                 $r->print("  ${newBranchText}${linkopen}$icon${linkclose}\n");
1.51      bowersj2 1196: 
1.74      bowersj2 1197:                 my $curMarkerBegin = "";
                   1198:                 my $curMarkerEnd = "";
                   1199: 
                   1200:                 # Is this the current resource?
1.78      bowersj2 1201:                 if ($curRes->src() eq $currenturl && !$displayedHereMarker) {
1.74      bowersj2 1202:                     $curMarkerBegin = '<a name="curloc" /><font color="red" size="+2">&gt; </font>';
                   1203:                     $curMarkerEnd = '<font color="red" size="+2"> &lt;</font>';
1.78      bowersj2 1204:                     $displayedHereMarker = 1;
1.74      bowersj2 1205:                 }
                   1206: 
1.69      bowersj2 1207:                 if ($curRes->is_problem() && $part ne "0" && !$condensed) { 
1.66      bowersj2 1208:                     $partLabel = " (Part $part)"; 
                   1209:                     $title = "";
                   1210:                 }
1.83      bowersj2 1211:                 if ($multipart && $condensed) {
1.68      bowersj2 1212:                     $nonLinkedText .= ' (' . $curRes->countParts() . ' parts)';
1.66      bowersj2 1213:                 }
1.59      bowersj2 1214: 
1.74      bowersj2 1215:                 $r->print("  $curMarkerBegin<a href=\"$link\">$title$partLabel</a> $curMarkerEnd $nonLinkedText");
1.66      bowersj2 1216: 
                   1217:                 if ($curRes->{RESOURCE_ERROR}) {
                   1218:                     $r->print(&Apache::loncommon::help_open_topic ("Navmap_Host_Down",
1.84      bowersj2 1219:                                               '<font size="-1">Host down</font>'));
1.66      bowersj2 1220:                     }
                   1221: 
                   1222:                 my $discussionHTML = ""; my $feedbackHTML = "";
                   1223: 
1.85      bowersj2 1224:                 # SECOND COL: Is there text, feedback, errors??
1.66      bowersj2 1225:                 if ($curRes->hasDiscussion()) {
                   1226:                     $discussionHTML = $linkopen .
                   1227:                         '<img border="0" src="/adm/lonMisc/chat.gif" />' .
                   1228:                         $linkclose;
                   1229:                 }
                   1230: 
                   1231:                 if ($curRes->getFeedback()) {
                   1232:                     my $feedback = $curRes->getFeedback();
                   1233:                     foreach (split(/\,/, $feedback)) {
                   1234:                         if ($_) {
                   1235:                             $feedbackHTML .= '&nbsp;<a href="/adm/email?display='
                   1236:                                 . &Apache::lonnet::escape($_) . '">'
                   1237:                                 . '<img src="/adm/lonMisc/feedback.gif" '
                   1238:                                 . 'border="0" /></a>';
                   1239:                         }
                   1240:                     }
                   1241:                 }
                   1242: 
1.84      bowersj2 1243:                 $r->print("<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML&nbsp;</td>");
1.66      bowersj2 1244: 
                   1245:                 # Is this the first displayed part of a multi-part problem
                   1246:                 # that has not been condensed, so we should suppress these two
                   1247:                 # columns?
                   1248:                 my $firstDisplayed = !$condensed && $multipart && $part eq "0";
                   1249: 
1.69      bowersj2 1250:                 # THIRD COL: Problem status icon
1.66      bowersj2 1251:                 if ($curRes->is_problem() &&
                   1252:                     !$firstDisplayed) {
                   1253:                     my $icon = $statusIconMap{$curRes->status($part)};
1.69      bowersj2 1254:                     my $alt = $iconAltTags{$icon};
1.66      bowersj2 1255:                     if ($icon) {
1.84      bowersj2 1256:                         $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 1257:                     } else {
1.84      bowersj2 1258:                         $r->print("<td width=\"30\">&nbsp;</td>\n");
1.66      bowersj2 1259:                     }
                   1260:                 } else { # not problem, no icon
1.84      bowersj2 1261:                     $r->print("<td width=\"30\">&nbsp;</td>\n");
1.66      bowersj2 1262:                 }
                   1263: 
1.69      bowersj2 1264:                 # FOURTH COL: Text description
1.86      bowersj2 1265:                 #$r->print("<td $colorizer align=\"right\" valign=\"center\">\n");
                   1266:                 $r->print("<td align=\"right\" valign=\"center\">\n");
1.51      bowersj2 1267:                 
1.61      bowersj2 1268:                 if ($curRes->kind() eq "res" &&
                   1269:                     $curRes->is_problem() &&
1.66      bowersj2 1270:                     !$firstDisplayed) {
1.86      bowersj2 1271:                     $r->print ("<font color=\"$color\"><b>") if ($color);
1.53      bowersj2 1272:                     $r->print (getDescription($curRes, $part));
1.86      bowersj2 1273:                     $r->print ("</b></font>") if ($color);
1.51      bowersj2 1274:                 }
1.68      bowersj2 1275:                 if ($curRes->is_map() && advancedUser() && $curRes->randompick()) {
                   1276:                     $r->print('(randomly select ' . $curRes->randompick() .')');
                   1277:                 }
1.59      bowersj2 1278: 
1.84      bowersj2 1279:                 $r->print("&nbsp;</td></tr>\n");
1.51      bowersj2 1280:             }
                   1281:         }
                   1282:         $curRes = $mapIterator->next();
                   1283:     }
                   1284: 
                   1285:     $r->print("</table></body></html>");
                   1286: 
1.59      bowersj2 1287:     $navmap->untieHashes();
                   1288: 
1.51      bowersj2 1289:     return OK;
                   1290: }
                   1291: 
                   1292: # Convenience functions: Returns a string that adds or subtracts
                   1293: # the second argument from the first hash, appropriate for the 
                   1294: # query string that determines which folders to recurse on
                   1295: sub addToFilter {
                   1296:     my $hashIn = shift;
                   1297:     my $addition = shift;
                   1298:     my %hash = %$hashIn;
                   1299:     $hash{$addition} = 1;
                   1300: 
                   1301:     return join (",", keys(%hash));
                   1302: }
                   1303: 
                   1304: sub removeFromFilter {
                   1305:     my $hashIn = shift;
                   1306:     my $subtraction = shift;
                   1307:     my %hash = %$hashIn;
                   1308: 
                   1309:     delete $hash{$subtraction};
                   1310:     return join(",", keys(%hash));
                   1311: }
                   1312: 
                   1313: # Convenience function: Given a stack returned from getStack on the iterator,
                   1314: # return the correct src() value.
                   1315: # Later, this should add an anchor when we start putting anchors in pages.
                   1316: sub getLinkForResource {
                   1317:     my $stack = shift;
                   1318:     my $res;
                   1319: 
                   1320:     # Check to see if there are any pages in the stack
                   1321:     foreach $res (@$stack) {
                   1322:         if (defined($res) && $res->is_page()) {
                   1323:             return $res->src();
                   1324:         }
                   1325:     }
                   1326: 
                   1327:     # Failing that, return the src of the last resource that is defined
                   1328:     # (when we first recurse on a map, it puts an undefined resource
                   1329:     # on the bottom because $self->{HERE} isn't defined yet, and we
                   1330:     # want the src for the map anyhow)
                   1331:     foreach (@$stack) {
                   1332:         if (defined($_)) { $res = $_; }
                   1333:     }
                   1334: 
                   1335:     return $res->src();
                   1336: }
                   1337: 
1.53      bowersj2 1338: # Convenience function: This seperates the logic of how to create
                   1339: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
                   1340: # etc.) into a seperate function. It takes a resource object as the
                   1341: # first parameter, and the part number of the resource as the second.
                   1342: # It's basically a big switch statement on the status of the resource.
                   1343: 
                   1344: sub getDescription {
                   1345:     my $res = shift;
                   1346:     my $part = shift;
1.69      bowersj2 1347:     my $status = $res->status($part);
1.53      bowersj2 1348: 
                   1349:     if ($status == $res->NETWORK_FAILURE) { return ""; }
                   1350:     if ($status == $res->NOTHING_SET) {
                   1351:         return "Not currently assigned.";
                   1352:     }
                   1353:     if ($status == $res->OPEN_LATER) {
1.73      bowersj2 1354:         return "Open " . timeToHumanString($res->opendate($part));
1.53      bowersj2 1355:     }
                   1356:     if ($status == $res->OPEN) {
1.79      bowersj2 1357:         if ($res->duedate($part)) {
1.73      bowersj2 1358:             return "Due " . timeToHumanString($res->duedate($part));
1.66      bowersj2 1359:         } else {
                   1360:             return "Open, no due date";
                   1361:         }
1.53      bowersj2 1362:     }
1.54      bowersj2 1363:     if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73      bowersj2 1364:         return "Answer open " . timeToHumanString($res->answerdate($part));
1.54      bowersj2 1365:     }
                   1366:     if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73      bowersj2 1367:         return "Was due " . timeToHumanString($res->duedate($part));
1.53      bowersj2 1368:     }
                   1369:     if ($status == $res->ANSWER_OPEN) {
                   1370:         return "Answer available";
                   1371:     }
1.59      bowersj2 1372:     if ($status == $res->EXCUSED) {
1.66      bowersj2 1373:         return "Excused by instructor";
1.59      bowersj2 1374:     }
1.69      bowersj2 1375:     if ($status == $res->ATTEMPTED) {
                   1376:         return "Not yet graded.";
                   1377:     }
1.59      bowersj2 1378:     if ($status == $res->TRIES_LEFT) {
1.79      bowersj2 1379:         my $tries = $res->tries($part);
                   1380:         my $maxtries = $res->maxtries($part);
                   1381:         my $triesString = "";
                   1382:         if ($tries && $maxtries) {
                   1383:             $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
                   1384:             if ($maxtries > 1 && $maxtries - $tries == 1) {
                   1385:                 $triesString = "<b>$triesString</b>";
                   1386:             }
                   1387:         }
1.66      bowersj2 1388:         if ($res->duedate()) {
1.73      bowersj2 1389:             return "Due " . timeToHumanString($res->duedate($part)) .
1.66      bowersj2 1390:                 " $triesString";
                   1391:         } else {
                   1392:             return "No due date $triesString";
                   1393:         }
1.59      bowersj2 1394:     }
1.53      bowersj2 1395: }
                   1396: 
1.68      bowersj2 1397: sub advancedUser {
                   1398:     return $ENV{'user.adv'};
                   1399: }
                   1400: 
1.73      bowersj2 1401: 
                   1402: # timeToHumanString takes a time number and converts it to a
                   1403: # human-readable representation, meant to be used in the following
                   1404: # manner:
                   1405: # print "Due $timestring"
                   1406: # print "Open $timestring"
                   1407: # print "Answer available $timestring"
                   1408: # Very, very, very, VERY English-only... goodness help a localizer on
                   1409: # this func...
1.53      bowersj2 1410: sub timeToHumanString {
1.58      albertel 1411:     my ($time) = @_;
                   1412:     # zero, '0' and blank are bad times
1.73      bowersj2 1413:     if (!$time) {
                   1414:         return 'never';
                   1415:     }
                   1416: 
                   1417:     my $now = time();
                   1418: 
                   1419:     my @time = localtime($time);
                   1420:     my @now = localtime($now);
                   1421: 
                   1422:     # Positive = future
                   1423:     my $delta = $time - $now;
                   1424: 
                   1425:     my $minute = 60;
                   1426:     my $hour = 60 * $minute;
                   1427:     my $day = 24 * $hour;
                   1428:     my $week = 7 * $day;
                   1429:     my $inPast = 0;
                   1430: 
                   1431:     # Logic in comments:
                   1432:     # Is it now? (extremely unlikely)
                   1433:     if ( $delta == 0 ) {
                   1434:         return "this instant";
                   1435:     }
                   1436: 
                   1437:     if ($delta < 0) {
                   1438:         $inPast = 1;
                   1439:         $delta = -$delta;
                   1440:     }
                   1441: 
                   1442:     # Is it in the future?
                   1443:     if ( $delta > 0 ) {
                   1444:         # Is it less then a minute away?
                   1445:         my $tense = $inPast ? " ago" : "";
                   1446:         my $prefix = $inPast ? "" : "in ";
                   1447:         if ( $delta < $minute ) {
                   1448:             if ($delta == 1) { return "${prefix}1 second$tense"; }
                   1449:             return "$prefix$delta seconds$tense";
                   1450:         }
                   1451: 
                   1452:         # Is it less then an hour away?
                   1453:         if ( $delta < $hour ) {
                   1454:             # If so, use minutes
                   1455:             my $minutes = floor($delta / 60);
                   1456:             if ($minutes == 1) { return "${prefix}1 minute$tense"; }
                   1457:             return "$prefix$minutes minutes$tense";
                   1458:         }
                   1459:         
                   1460:         # Is it less then 24 hours away? If so,
                   1461:         # display hours + minutes
                   1462:         if ( $delta < $hour * 24) {
                   1463:             my $hours = floor($delta / $hour);
                   1464:             my $minutes = floor(($delta % $hour) / $minute);
                   1465:             my $hourString = "$hours hours";
                   1466:             my $minuteString = ", $minutes minutes";
                   1467:             if ($hours == 1) {
                   1468:                 $hourString = "1 hour";
                   1469:             }
                   1470:             if ($minutes == 1) {
                   1471:                 $minuteString = ", 1 minute";
                   1472:             }
                   1473:             if ($minutes == 0) {
                   1474:                 $minuteString = "";
                   1475:             }
                   1476:             return "$prefix$hourString$minuteString$tense";
                   1477:         }
                   1478: 
                   1479:         # Less then 5 days away, display day of the week and
                   1480:         # HH:MM
                   1481:         if ( $delta < $day * 5 ) {
1.85      bowersj2 1482:             my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73      bowersj2 1483:             $timeStr =~ s/12:00 am/midnight/;
                   1484:             $timeStr =~ s/12:00 pm/noon/;
                   1485:             return ($inPast ? "last " : "next ") .
                   1486:                 $timeStr;
                   1487:         }
                   1488:         
                   1489:         # Is it this year?
                   1490:         if ( $time[5] == $now[5]) {
                   1491:             # Return on Month Day, HH:MM meridian
                   1492:             my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
                   1493:             $timeStr =~ s/12:00 am/midnight/;
                   1494:             $timeStr =~ s/12:00 pm/noon/;
                   1495:             return $timeStr;
                   1496:         }
                   1497: 
                   1498:         # Not this year, so show the year
                   1499:         my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
                   1500:         $timeStr =~ s/12:00 am/midnight/;
                   1501:         $timeStr =~ s/12:00 pm/noon/;
                   1502:         return $timeStr;
1.58      albertel 1503:     }
1.53      bowersj2 1504: }
                   1505: 
1.51      bowersj2 1506: 1;
                   1507: 
                   1508: package Apache::lonnavmaps::navmap;
                   1509: 
                   1510: =pod
                   1511: 
                   1512: 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.
                   1513: 
                   1514: =head1 navmap object: Encapsulating the compiled nav map
                   1515: 
                   1516: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
                   1517: 
                   1518: 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.
                   1519: 
                   1520: You must obtain resource objects through the navmap object.
                   1521: 
                   1522: =head2 Methods
                   1523: 
                   1524: =over 4
                   1525: 
1.70      bowersj2 1526: =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 1527: 
                   1528: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
                   1529: 
                   1530: =cut
                   1531: 
                   1532: use strict;
                   1533: use GDBM_File;
                   1534: 
                   1535: sub new {
                   1536:     # magic invocation to create a class instance
                   1537:     my $proto = shift;
                   1538:     my $class = ref($proto) || $proto;
                   1539:     my $self = {};
                   1540: 
                   1541:     $self->{NAV_HASH_FILE} = shift;
                   1542:     $self->{PARM_HASH_FILE} = shift;
                   1543:     $self->{GENERATE_COURSE_USER_OPT} = shift;
1.55      bowersj2 1544:     $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
1.51      bowersj2 1545: 
                   1546:     # Resource cache stores navmapresource's as we reference them. We generate
                   1547:     # them on-demand so we don't pay for creating resources unless we use them.
                   1548:     $self->{RESOURCE_CACHE} = {};
                   1549: 
                   1550:     # Network failure flag, if we accessed the course or user opt and
                   1551:     # failed
                   1552:     $self->{NETWORK_FAILURE} = 0;
                   1553: 
                   1554:     # tie the nav hash
                   1555:     my %navmaphash;
                   1556:     if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
                   1557:               &GDBM_READER(), 0640))) {
                   1558:         return undef;
                   1559:     }
                   1560:     $self->{NAV_HASH} = \%navmaphash;
                   1561:     
                   1562:     my %parmhash;
                   1563:     if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
                   1564:               &GDBM_READER(), 0640)))
                   1565:     {
1.66      bowersj2 1566:         untie $self->{PARM_HASH};
1.51      bowersj2 1567:         return undef;
                   1568:     }
                   1569:     $self->{PARM_HASH} = \%parmhash;
                   1570:     $self->{HASH_TIED} = 1;
                   1571: 
                   1572:     # If the course opt hash and the user opt hash should be generated,
                   1573:     # generate them
                   1574:     if ($self->{GENERATE_COURSE_USER_OPT}) {
                   1575:         my $uname=$ENV{'user.name'};
                   1576:         my $udom=$ENV{'user.domain'};
                   1577:         my $uhome=$ENV{'user.home'};
                   1578:         my $cid=$ENV{'request.course.id'};
                   1579:         my $chome=$ENV{'course.'.$cid.'.home'};
                   1580:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1581:         
                   1582:         my $userprefix=$uname.'_'.$udom.'_';
                   1583:         
                   1584:         my %courserdatas; my %useropt; my %courseopt;
                   1585:         unless ($uhome eq 'no_host') { 
                   1586: # ------------------------------------------------- Get coursedata (if present)
                   1587:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
                   1588:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                   1589:                                                  ':resourcedata',$chome);
                   1590:                 if ($reply!~/^error\:/) {
                   1591:                     $courserdatas{$cid}=$reply;
                   1592:                     $courserdatas{$cid.'.last_cache'}=time;
                   1593:                 }
                   1594:                 # check to see if network failed
                   1595:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1596:                 {
                   1597:                     $self->{NETWORK_FAILURE} = 1;
                   1598:                 }
                   1599:             }
                   1600:             foreach (split(/\&/,$courserdatas{$cid})) {
                   1601:                 my ($name,$value)=split(/\=/,$_);
                   1602:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1603:                     &Apache::lonnet::unescape($value);
                   1604:             }
                   1605: # --------------------------------------------------- Get userdata (if present)
                   1606:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
                   1607:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
                   1608:                 if ($reply!~/^error\:/) {
                   1609:                     $userrdatas{$uname.'___'.$udom}=$reply;
                   1610:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
                   1611:                 }
                   1612:                 # check to see if network failed
                   1613:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1614:                 {
                   1615:                     $self->{NETWORK_FAILURE} = 1;
                   1616:                 }
                   1617:             }
                   1618:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
                   1619:                 my ($name,$value)=split(/\=/,$_);
                   1620:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1621:                     &Apache::lonnet::unescape($value);
                   1622:             }
1.55      bowersj2 1623:             $self->{COURSE_OPT} = \%courseopt;
                   1624:             $self->{USER_OPT} = \%useropt;
1.51      bowersj2 1625:         }
                   1626:     }   
                   1627: 
1.55      bowersj2 1628:     if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
                   1629:         my $cid=$ENV{'request.course.id'};
                   1630:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1631:         
                   1632:         my %emailstatus = &Apache::lonnet::dump('email_status');
1.56      bowersj2 1633:         my $logoutTime = $emailstatus{'logout'};
                   1634:         my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
1.55      bowersj2 1635:         $self->{LAST_CHECK} = ($courseLeaveTime > $logoutTime ?
                   1636:                                $courseLeaveTime : $logoutTime);
1.56      bowersj2 1637:         my %discussiontime = &Apache::lonnet::dump('discussiontimes', 
1.55      bowersj2 1638:                                                    $cdom, $cnum);
                   1639:         my %feedback=();
                   1640:         my %error=();
                   1641:         my $keys = &Apache::lonnet::reply('keys:'.
                   1642:                                           $ENV{'user.domain'}.':'.
                   1643:                                           $ENV{'user.name'}.':nohist_email',
                   1644:                                           $ENV{'user.home'});
                   1645: 
                   1646:         foreach my $msgid (split(/\&/, $keys)) {
                   1647:             $msgid=&Apache::lonnet::unescape($msgid);
                   1648:             my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
                   1649:             if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
                   1650:                 my ($what,$url)=($1,$2);
                   1651:                 my %status=
                   1652:                     &Apache::lonnet::get('email_status',[$msgid]);
                   1653:                 if ($status{$msgid}=~/^error\:/) { 
                   1654:                     $status{$msgid}=''; 
                   1655:                 }
                   1656:                 
                   1657:                 if (($status{$msgid} eq 'new') || 
                   1658:                     (!$status{$msgid})) { 
                   1659:                     if ($what eq 'Error') {
                   1660:                         $error{$url}.=','.$msgid; 
                   1661:                     } else {
                   1662:                         $feedback{$url}.=','.$msgid;
                   1663:                     }
                   1664:                 }
                   1665:             }
                   1666:         }
                   1667:         
                   1668:         $self->{FEEDBACK} = \%feedback;
                   1669:         $self->{ERROR_MSG} = \%error; # what is this? JB
                   1670:         $self->{DISCUSSION_TIME} = \%discussiontime;
                   1671:         $self->{EMAIL_STATUS} = \%emailstatus;
                   1672:         
                   1673:     }    
1.84      bowersj2 1674: 
                   1675:     $self->{PARM_CACHE} = {};
1.55      bowersj2 1676:     
1.51      bowersj2 1677:     bless($self);
1.55      bowersj2 1678:         
                   1679:     return $self;
                   1680: }
1.51      bowersj2 1681: 
1.55      bowersj2 1682: # Checks to see if coursemap is defined, matching test in old lonnavmaps
                   1683: sub courseMapDefined {
                   1684:     my $self = shift;
                   1685:     my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
                   1686: 
1.56      bowersj2 1687:     my $firstres = $self->{NAV_HASH}->{"map_start_$uri"};
                   1688:     my $lastres = $self->{NAV_HASH}->{"map_finish_$uri"};
1.55      bowersj2 1689:     return $firstres && $lastres;
1.51      bowersj2 1690: }
                   1691: 
                   1692: sub getIterator {
                   1693:     my $self = shift;
                   1694:     my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
1.63      bowersj2 1695:                                                      shift, undef, shift);
1.51      bowersj2 1696:     return $iterator;
                   1697: }
                   1698: 
                   1699: # unties the hash when done
                   1700: sub untieHashes {
                   1701:     my $self = shift;
1.60      bowersj2 1702:     untie %{$self->{NAV_HASH}} if ($self->{HASH_TIED});
                   1703:     untie %{$self->{PARM_HASH}} if ($self->{HASH_TIED});
1.51      bowersj2 1704:     $self->{HASH_TIED} = 0;
                   1705: }
                   1706: 
                   1707: # when the object is destroyed, be sure to untie all the hashes we tied.
                   1708: sub DESTROY {
                   1709:     my $self = shift;
                   1710:     $self->untieHashes();
                   1711: }
                   1712: 
1.59      bowersj2 1713: # Private function: Does the given resource (as a symb string) have
                   1714: # current discussion? Returns 0 if chat/mail data not extracted.
                   1715: sub hasDiscussion {
                   1716:     my $self = shift;
                   1717:     my $symb = shift;
                   1718:     if (!defined($self->{DISCUSSION_TIME})) { return 0; }
                   1719: 
                   1720:     return $self->{DISCUSSION_TIME}->{$symb} >
1.66      bowersj2 1721:            $self->{LAST_CHECK};
1.59      bowersj2 1722: }
                   1723: 
                   1724: # Private function: Does the given resource (as a symb string) have
                   1725: # current feedback? Returns the string in the feedback hash, which
                   1726: # will be false if it does not exist.
                   1727: sub getFeedback { 
                   1728:     my $self = shift;
                   1729:     my $symb = shift;
                   1730: 
                   1731:     if (!defined($self->{FEEDBACK})) { return ""; }
                   1732:     
                   1733:     return $self->{FEEDBACK}->{$symb};
                   1734: }
                   1735: 
1.51      bowersj2 1736: =pod
                   1737: 
                   1738: =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.
                   1739: 
                   1740: =cut
                   1741: 
                   1742: # The strategy here is to cache the resource objects, and only construct them
                   1743: # as we use them. The real point is to prevent reading any more from the tied
                   1744: # hash then we have to, which should hopefully alleviate speed problems.
                   1745: # Caching is just an incidental detail I throw in because it makes sense.
                   1746: 
                   1747: sub getById {
                   1748:     my $self = shift;
                   1749:     my $id = shift;
                   1750: 
                   1751:     if (defined ($self->{RESOURCE_CACHE}->{$id}))
                   1752:     {
                   1753:         return $self->{RESOURCE_CACHE}->{$id};
                   1754:     }
                   1755: 
                   1756:     # resource handles inserting itself into cache.
                   1757:     return Apache::lonnavmaps::resource->new($self, $id);
                   1758: }
                   1759: 
                   1760: =pod
                   1761: 
                   1762: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
                   1763: 
                   1764: =cut
                   1765: 
                   1766: sub firstResource {
                   1767:     my $self = shift;
                   1768:     my $firstResource = $self->{NAV_HASH}->{'map_start_' .
                   1769:                      &Apache::lonnet::clutter($ENV{'request.course.uri'})};
                   1770:     return $self->getById($firstResource);
                   1771: }
                   1772: 
                   1773: =pod
                   1774: 
                   1775: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
                   1776: 
                   1777: =cut
                   1778: 
                   1779: sub finishResource {
                   1780:     my $self = shift;
                   1781:     my $firstResource = $self->{NAV_HASH}->{'map_finish_' .
                   1782:                      &Apache::lonnet::clutter($ENV{'request.course.uri'})};
                   1783:     return $self->getById($firstResource);
                   1784: }
                   1785: 
                   1786: # -------------------------------------------- Figure out a cascading parameter
                   1787: #
                   1788: # For this function to work
                   1789: #
                   1790: # * parmhash needs to be tied
                   1791: # * courseopt and useropt need to be initialized for this user and course
                   1792: #
                   1793: 
                   1794: sub parmval {
                   1795:     my $self = shift;
                   1796:     my ($what,$symb)=@_;
1.84      bowersj2 1797:     my $hashkey = $what."|||".$symb;
                   1798: 
                   1799:     if (defined($self->{PARM_CACHE}->{$hashkey})) {
                   1800:         return $self->{PARM_CACHE}->{$hashkey};
                   1801:     }
                   1802: 
                   1803:     my $result = $self->parmval_real($what, $symb);
                   1804:     $self->{PARM_CACHE}->{$hashkey} = $result;
                   1805:     return $result;
                   1806: }
                   1807: 
                   1808: sub parmval_real {
                   1809:     my $self = shift;
                   1810:     my ($what,$symb) = @_;
                   1811: 
1.51      bowersj2 1812:     my $cid=$ENV{'request.course.id'};
                   1813:     my $csec=$ENV{'request.course.sec'};
                   1814:     my $uname=$ENV{'user.name'};
                   1815:     my $udom=$ENV{'user.domain'};
                   1816: 
                   1817:     unless ($symb) { return ''; }
                   1818:     my $result='';
                   1819: 
                   1820:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                   1821: 
                   1822: # ----------------------------------------------------- Cascading lookup scheme
                   1823:     my $rwhat=$what;
                   1824:     $what=~s/^parameter\_//;
                   1825:     $what=~s/\_/\./;
                   1826: 
                   1827:     my $symbparm=$symb.'.'.$what;
                   1828:     my $mapparm=$mapname.'___(all).'.$what;
                   1829:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
                   1830: 
                   1831:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
                   1832:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
                   1833:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
                   1834: 
                   1835:     my $courselevel= $usercourseprefix.'.'.$what;
                   1836:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
                   1837:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
                   1838: 
                   1839:     my $useropt = $self->{USER_OPT};
                   1840:     my $courseopt = $self->{COURSE_OPT};
                   1841:     my $parmhash = $self->{PARM_HASH};
                   1842: 
                   1843: # ---------------------------------------------------------- first, check user
                   1844:     if ($uname and defined($useropt)) {
1.57      albertel 1845:         if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
                   1846:         if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
                   1847:         if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
1.51      bowersj2 1848:     }
                   1849: 
                   1850: # ------------------------------------------------------- second, check course
                   1851:     if ($csec and defined($courseopt)) {
1.57      albertel 1852:         if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
                   1853:         if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
                   1854:         if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
1.51      bowersj2 1855:     }
                   1856: 
                   1857:     if (defined($courseopt)) {
1.57      albertel 1858:         if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
                   1859:         if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
                   1860:         if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
1.51      bowersj2 1861:     }
                   1862: 
                   1863: # ----------------------------------------------------- third, check map parms
                   1864: 
                   1865:     my $thisparm=$$parmhash{$symbparm};
1.57      albertel 1866:     if (defined($thisparm)) { return $thisparm; }
1.51      bowersj2 1867: 
                   1868: # ----------------------------------------------------- fourth , check default
                   1869: 
                   1870:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
1.57      albertel 1871:     if (defined($default)) { return $default}
1.51      bowersj2 1872: 
                   1873: # --------------------------------------------------- fifth , cascade up parts
                   1874: 
                   1875:     my ($space,@qualifier)=split(/\./,$rwhat);
                   1876:     my $qualifier=join('.',@qualifier);
                   1877:     unless ($space eq '0') {
                   1878:         my ($part,$id)=split(/\_/,$space);
                   1879:         if ($id) {
                   1880:             my $partgeneral=$self->parmval($part.".$qualifier",$symb);
1.57      albertel 1881:             if (defined($partgeneral)) { return $partgeneral; }
1.51      bowersj2 1882:         } else {
                   1883:             my $resourcegeneral=$self->parmval("0.$qualifier",$symb);
1.57      albertel 1884:             if (defined($resourcegeneral)) { return $resourcegeneral; }
1.51      bowersj2 1885:         }
                   1886:     }
                   1887:     return '';
                   1888: }
                   1889: 
                   1890: 
                   1891: 1;
                   1892: 
                   1893: package Apache::lonnavmaps::iterator;
                   1894: 
                   1895: # This package must precede "navmap" because "navmap" uses it. In
                   1896: # order to keep the documentation order straight, the iterator is documented
                   1897: # after the navmap object.
                   1898: 
                   1899: =pod
                   1900: 
                   1901: =back
                   1902: 
                   1903: =head1 navmap Iterator
                   1904: 
                   1905: 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.
                   1906: 
                   1907: 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:
                   1908: 
                   1909: C<my $resourceIterator = $navmap-E<gt>getIterator();>
                   1910: 
                   1911: To get the next thing from the iterator, call B<next>:
                   1912: 
                   1913: C<my $nextThing = $resourceIterator-E<gt>next()>
                   1914: 
                   1915: getIterator behaves as follows:
                   1916: 
                   1917: =over 4
                   1918: 
1.70      bowersj2 1919: =item B<getIterator>(firstResource, finishResource, filterHash, condition): 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.
1.51      bowersj2 1920: 
1.70      bowersj2 1921: 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 1922: 
                   1923: 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:
                   1924: 
                   1925: =over 4
                   1926: 
1.70      bowersj2 1927: =item * BEGIN_MAP: A new map is being recursed into. This is returned I<after> the map resource itself is returned.
                   1928: 
                   1929: =item * END_MAP: The map is now done.
                   1930: 
                   1931: =item * BEGIN_BRANCH: A branch is now starting. The next resource returned will be the first in that branch.
                   1932: 
                   1933: =item * END_BRANCH: The branch is now done.
1.51      bowersj2 1934: 
                   1935: =back
                   1936: 
1.70      bowersj2 1937: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_MAP.
                   1938: 
                   1939: =back
1.51      bowersj2 1940: 
                   1941: =cut
                   1942: 
                   1943: # Here are the tokens for the iterator:
                   1944: 
                   1945: sub BEGIN_MAP { return 1; }    # begining of a new map
                   1946: sub END_MAP { return 2; }      # end of the map
                   1947: sub BEGIN_BRANCH { return 3; } # beginning of a branch
                   1948: sub END_BRANCH { return 4; }   # end of a branch
                   1949: 
                   1950: # Params: nav map, start resource, end resource, filter, condition, 
                   1951: # already seen hash ref
                   1952: 
                   1953: sub new {
                   1954:     # magic invocation to create a class instance
                   1955:     my $proto = shift;
                   1956:     my $class = ref($proto) || $proto;
                   1957:     my $self = {};
                   1958: 
                   1959:     $self->{NAV_MAP} = shift;
                   1960:     return undef unless ($self->{NAV_MAP});
                   1961: 
                   1962:     # Handle the parameters
                   1963:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   1964:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   1965: 
                   1966:     # If the given resources are just the ID of the resource, get the
                   1967:     # objects
                   1968:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   1969:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   1970:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   1971:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   1972: 
                   1973:     $self->{FILTER} = shift;
                   1974: 
                   1975:     # A hash, used as a set, of resource already seen
                   1976:     $self->{ALREADY_SEEN} = shift;
                   1977:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63      bowersj2 1978:     $self->{CONDITION} = shift;
1.51      bowersj2 1979: 
                   1980:     # Flag: Have we started yet? If not, the first action is to return BEGIN_MAP.
                   1981:     $self->{STARTED} = 0;
                   1982: 
                   1983:     # Should we continue calling the recursive iterator, if any?
                   1984:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   1985:     # The recursive iterator, if any
                   1986:     $self->{RECURSIVE_ITERATOR} = undef;
                   1987:     # Are we recursing on a map, or a branch?
                   1988:     $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
                   1989:     # And the count of how deep it is, so that this iterator can keep track of
                   1990:     # when to pick back up again.
                   1991:     $self->{RECURSIVE_DEPTH} = 0;
                   1992: 
                   1993:     # For keeping track of our branches, we maintain our own stack
                   1994:     $self->{BRANCH_STACK} = [];
                   1995:     # If the size shrinks, we exhausted a branch
                   1996:     $self->{BRANCH_STACK_SIZE} = 0;
                   1997:     $self->{BRANCH_DEPTH} = 0;
                   1998: 
                   1999:     # For returning two things in a forced sequence
                   2000:     $self->{FORCE_NEXT} = undef;
                   2001: 
                   2002:     # Start with the first resource
                   2003:     push @{$self->{BRANCH_STACK}}, $self->{FIRST_RESOURCE};
                   2004:     $self->{BRANCH_STACK_SIZE} = 1;
                   2005: 
                   2006:     bless($self);
                   2007:     return $self;
                   2008: }
                   2009: 
                   2010: # Note... this function is *touchy*. I strongly recommend tracing
                   2011: # through it with the debugger a few times on a non-trivial map before
                   2012: # modifying it. Order is *everything*.
                   2013: sub next {
                   2014:     my $self = shift;
                   2015:     
                   2016:     # Iterator logic goes here
                   2017: 
                   2018:     # Are we using a recursive iterator? If so, pull from that and
                   2019:     # watch the depth; we want to resume our level at the correct time.
                   2020:     if ($self->{RECURSIVE_ITERATOR_FLAG})
                   2021:     {
                   2022:         # grab the next from the recursive iterator
                   2023:         my $next = $self->{RECURSIVE_ITERATOR}->next();
                   2024:         
                   2025:         # is it a begin or end map? Update depth if so
                   2026:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
                   2027:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
                   2028: 
                   2029:         # Are we back at depth 0? If so, stop recursing.
                   2030:         if ($self->{RECURSIVE_DEPTH} == 0) {
                   2031:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2032:         }
                   2033:         
                   2034:         return $next;
                   2035:     }
                   2036: 
1.68      bowersj2 2037:     # Is this return value pre-determined?
                   2038:     if (defined($self->{FORCE_NEXT})) {
                   2039:         my $tmp = $self->{FORCE_NEXT};
                   2040:         $self->{FORCE_NEXT} = undef;
                   2041:         return $tmp;
                   2042:     }
                   2043: 
1.51      bowersj2 2044:     # Is there a current resource to grab? If not, then return
                   2045:     # END_BRANCH and END_MAP in succession.
                   2046:     if (scalar(@{$self->{BRANCH_STACK}}) == 0) {
                   2047:         if ($self->{BRANCH_DEPTH} > 0) {
                   2048:             $self->{FORCE_NEXT} = $self->END_MAP();
1.52      bowersj2 2049:             $self->{BRANCH_DEPTH}--;
1.51      bowersj2 2050:             return $self->END_BRANCH();
                   2051:         } else {
                   2052:             return $self->END_MAP();
                   2053:         }
                   2054:     }
                   2055: 
                   2056:     # Have we not yet begun? If not, return BEGIN_MAP and 
                   2057:     # remember that we've started.
                   2058:     if ( !$self->{STARTED} ) {
                   2059:         $self->{STARTED} = 1;
                   2060:         return $self->BEGIN_MAP;
                   2061:     }
                   2062: 
                   2063:     # Did the branch stack shrink since last run? If so,
                   2064:     # we exhausted a branch last time, therefore, we're about
                   2065:     # to start a new one. (We know because we already checked to see
                   2066:     # if the stack was empty.)
                   2067:     if ( scalar (@{$self->{BRANCH_STACK}}) < $self->{BRANCH_STACK_SIZE}) {
1.52      bowersj2 2068:         $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
                   2069:         $self->{BRANCH_DEPTH}++;
                   2070:         return $self->BEGIN_BRANCH();
1.51      bowersj2 2071:     }
                   2072: 
1.52      bowersj2 2073:     # Remember the size for comparision next time.
                   2074:     $self->{BRANCH_STACK_SIZE} = scalar(@{$self->{BRANCH_STACK}});
                   2075: 
                   2076:     # If the next resource we mean to return is going to need
                   2077:     # a lower branch level, terminate branches until we get 
                   2078:     # there.
                   2079: 
1.51      bowersj2 2080:     # Get the next resource in the branch
                   2081:     $self->{HERE} = pop @{$self->{BRANCH_STACK}};
1.52      bowersj2 2082: 
                   2083:     # Are we at the right depth? If not, close a branch and return
                   2084:     # the current resource onto the branch stack
                   2085:     if (defined($self->{HERE}->{DATA}->{ITERATOR_DEPTH})
                   2086:         && $self->{HERE}->{DATA}->{ITERATOR_DEPTH} <
                   2087:         $self->{BRANCH_DEPTH} ) {
                   2088:         $self->{BRANCH_DEPTH}--;
                   2089:         # return it so we can pick it up eventually
                   2090:         push @{$self->{BRANCH_STACK}}, $self->{HERE};
                   2091:         return $self->END_BRANCH();
                   2092:     }
                   2093: 
1.51      bowersj2 2094:     # We always return it after this point and never before
                   2095:     # (proof: look at just the return statements), so we
                   2096:     # remember that we've seen this.
                   2097:     $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
                   2098:     
                   2099:     # Get the next possible resources
                   2100:     my $nextUnfiltered = $self->{HERE}->getNext();
                   2101:     my $next = [];
                   2102: 
                   2103:     # filter the next possibilities to remove things we've 
1.52      bowersj2 2104:     # already seen. Also, remember what branch depth they should
                   2105:     # be displayed at, since there's no other reliable way to tell.
1.51      bowersj2 2106:     foreach (@$nextUnfiltered) {
1.52      bowersj2 2107:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
                   2108:             push @$next, $_;
                   2109:             $_->{DATA}->{ITERATOR_DEPTH} = 
                   2110:                 $self->{BRANCH_DEPTH} + 1;
                   2111:         }
1.51      bowersj2 2112:     }
                   2113: 
                   2114:     # Handle branch cases:
                   2115:     # Nothing is available next: BRANCH_END
                   2116:     # 1 thing next: standard non-branch
                   2117:     # 2+ things next: have some branches
                   2118:     my $nextCount = scalar(@$next);
                   2119:     if ($nextCount == 0) {
1.52      bowersj2 2120:         # Return this and on the next run, close the branch up if we're 
                   2121:         # in a branch
                   2122:         if ($self->{BRANCH_DEPTH} > 0 ) {
                   2123:             $self->{FORCE_NEXT} = $self->END_BRANCH();
                   2124:             $self->{BRANCH_DEPTH}--;
                   2125:         }
1.51      bowersj2 2126:     }
                   2127:     
                   2128:     while (@$next) {
                   2129:         # copy the next possibilities over to the branch stack
                   2130:         # in the right order
1.52      bowersj2 2131:         push @{$self->{BRANCH_STACK}}, shift @$next;
1.51      bowersj2 2132:     }
                   2133: 
                   2134:     if ($nextCount >= 2) {
1.52      bowersj2 2135:         $self->{FORCE_NEXT} = $self->BEGIN_BRANCH();
                   2136:         $self->{BRANCH_DEPTH}++;
1.51      bowersj2 2137:         return $self->{HERE}; 
                   2138:     }
                   2139: 
                   2140:     # If this is a map and we want to recurse down it... (not filtered out)
1.70      bowersj2 2141:     if ($self->{HERE}->is_map() && 
1.63      bowersj2 2142:          (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) { 
1.51      bowersj2 2143:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
                   2144:         my $firstResource = $self->{HERE}->map_start();
                   2145:         my $finishResource = $self->{HERE}->map_finish();
                   2146: 
                   2147:         $self->{RECURSIVE_ITERATOR} =
                   2148:           Apache::lonnavmaps::iterator->new ($self->{NAV_MAP}, $firstResource, 
1.63      bowersj2 2149:                      $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.66      bowersj2 2150:                                              $self->{CONDITION});
1.51      bowersj2 2151:     }
                   2152: 
                   2153:     return $self->{HERE};
                   2154: }
                   2155: 
                   2156: =pod
                   2157: 
                   2158: 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).
                   2159: 
                   2160: =cut
                   2161: 
                   2162: sub getStack {
                   2163:     my $self=shift;
                   2164: 
                   2165:     my @stack;
                   2166: 
                   2167:     $self->populateStack(\@stack);
                   2168: 
                   2169:     return \@stack;
                   2170: }
                   2171: 
                   2172: # Private method: Calls the iterators recursively to populate the stack.
                   2173: sub populateStack {
                   2174:     my $self=shift;
                   2175:     my $stack = shift;
                   2176: 
                   2177:     push @$stack, $self->{HERE};
                   2178: 
                   2179:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
                   2180:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
                   2181:     }
                   2182: }
                   2183: 
1.1       www      2184: 1;
1.2       www      2185: 
1.51      bowersj2 2186: package Apache::lonnavmaps::resource;
                   2187: 
                   2188: use Apache::lonnet;
                   2189: 
                   2190: =pod
                   2191: 
                   2192: =head1 Object: resource
                   2193: 
                   2194: 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.
                   2195: 
                   2196: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
                   2197: 
                   2198: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
                   2199: 
                   2200: =head2 Public Members
                   2201: 
                   2202: 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.
                   2203: 
                   2204: =head2 Methods
                   2205: 
                   2206: =over 4
                   2207: 
1.70      bowersj2 2208: =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.
                   2209: 
                   2210: =back
1.51      bowersj2 2211: 
                   2212: =cut
                   2213: 
                   2214: sub new {
                   2215:     # magic invocation to create a class instance
                   2216:     my $proto = shift;
                   2217:     my $class = ref($proto) || $proto;
                   2218:     my $self = {};
                   2219: 
                   2220:     $self->{NAV_MAP} = shift;
                   2221:     $self->{ID} = shift;
                   2222: 
                   2223:     # Store this new resource in the parent nav map's cache.
                   2224:     $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66      bowersj2 2225:     $self->{RESOURCE_ERROR} = 0;
1.51      bowersj2 2226: 
                   2227:     # A hash that can be used by two-pass algorithms to store data
                   2228:     # about this resource in. Not used by the resource object
                   2229:     # directly.
                   2230:     $self->{DATA} = {};
                   2231:    
                   2232:     bless($self);
                   2233:     
                   2234:     return $self;
                   2235: }
                   2236: 
1.70      bowersj2 2237: # private function: simplify the NAV_HASH lookups we keep doing
                   2238: # pass the name, and to automatically append my ID, pass a true val on the
                   2239: # second param
                   2240: sub navHash {
                   2241:     my $self = shift;
                   2242:     my $param = shift;
                   2243:     my $id = shift;
                   2244:     return $self->{NAV_MAP}->{NAV_HASH}->{$param . ($id?$self->{ID}:"")};
                   2245: }
                   2246: 
1.51      bowersj2 2247: =pod
                   2248: 
1.70      bowersj2 2249: B<Metadata Retreival>
                   2250: 
                   2251: These are methods that help you retrieve metadata about the resource:
                   2252: 
                   2253: =over 4
                   2254: 
                   2255: =item * B<ext>: Returns true if the resource is external.
                   2256: 
                   2257: =item * B<goesto>: Returns the "goesto" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
                   2258: 
                   2259: =item * B<kind>: Returns the kind of the resource from the compiled nav map.
                   2260: 
                   2261: =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 2262: 
1.70      bowersj2 2263: =item * B<randompick>: Returns true for a map if the randompick feature is being used on the map. (?)
                   2264: 
                   2265: =item * B<src>: Returns the source for the resource.
                   2266: 
                   2267: =item * B<symb>: Returns the symb for the resource.
                   2268: 
                   2269: =item * B<title>: Returns the title of the resource.
                   2270: 
                   2271: =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 2272: 
                   2273: =back
                   2274: 
                   2275: =cut
                   2276: 
                   2277: # These info functions can be used directly, as they don't return
                   2278: # resource information.
1.85      bowersj2 2279: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70      bowersj2 2280: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85      bowersj2 2281: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51      bowersj2 2282: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
                   2283: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68      bowersj2 2284: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
                   2285: sub randompick { 
                   2286:     my $self = shift;
                   2287:     return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
                   2288:                                                '.0.parameter_randompick'};
                   2289: }
1.51      bowersj2 2290: sub src { 
                   2291:     my $self=shift;
                   2292:     return $self->navHash("src_", 1);
                   2293: }
                   2294: sub symb {
                   2295:     my $self=shift;
                   2296:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
                   2297:     my $symbSrc = &Apache::lonnet::declutter($self->src());
                   2298:     return &Apache::lonnet::declutter(
                   2299:          $self->navHash('map_id_'.$first)) 
                   2300:         . '___' . $second . '___' . $symbSrc;
                   2301: }
1.70      bowersj2 2302: sub title { my $self=shift; return $self->navHash("title_", 1); }
                   2303: sub to { my $self=shift; return $self->navHash("to_", 1); }
                   2304: 
                   2305: =pod
                   2306: 
                   2307: B<Predicate Testing the Resource>
                   2308: 
                   2309: These methods are shortcuts to deciding if a given resource has a given property.
                   2310: 
                   2311: =over 4
                   2312: 
                   2313: =item * B<is_map>: Returns true if the resource is a map type.
                   2314: 
                   2315: =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.)
                   2316: 
                   2317: =item * B<is_page>: Returns true if the resource is a page.
                   2318: 
                   2319: =item * B<is_problem>: Returns true if the resource is a problem.
                   2320: 
                   2321: =item * B<is_sequence>: Returns true if the resource sequence.
                   2322: 
                   2323: =back
                   2324: 
                   2325: =cut
                   2326: 
                   2327: 
                   2328: sub is_html {
1.51      bowersj2 2329:     my $self=shift;
                   2330:     my $src = $self->src();
1.70      bowersj2 2331:     return ($src =~ /html$/);
1.51      bowersj2 2332: }
1.70      bowersj2 2333: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
                   2334: sub is_page {
1.51      bowersj2 2335:     my $self=shift;
                   2336:     my $src = $self->src();
1.70      bowersj2 2337:     return ($src =~ /page$/);
1.51      bowersj2 2338: }
1.70      bowersj2 2339: sub is_problem {
1.51      bowersj2 2340:     my $self=shift;
                   2341:     my $src = $self->src();
1.70      bowersj2 2342:     return ($src =~ /problem$/);
1.51      bowersj2 2343: }
1.70      bowersj2 2344: sub is_sequence {
1.51      bowersj2 2345:     my $self=shift;
                   2346:     my $src = $self->src();
1.70      bowersj2 2347:     return ($src =~ /sequence$/);
1.51      bowersj2 2348: }
                   2349: 
                   2350: 
                   2351: 
                   2352: 
1.70      bowersj2 2353: 
1.51      bowersj2 2354: # Move this into POD: In order to use these correctly, courseopt
                   2355: # and useropt need to be generated
                   2356: sub parmval {
                   2357:     my $self = shift;
                   2358:     my $what = shift;
                   2359:     my $part = shift || "0";
                   2360:     return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
                   2361: }
                   2362: 
1.70      bowersj2 2363: =pod
                   2364: 
                   2365: B<Map Methods>
                   2366: 
                   2367: These methods are useful for getting information about the map properties of the resource, if the resource is a map (B<is_map>).
                   2368: 
                   2369: =over 4
                   2370: 
                   2371: =item * B<map_finish>: Returns a reference to a resource object corresponding to the finish resource of the map.
                   2372: 
                   2373: =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.
                   2374: 
                   2375: =item * B<map_start>: Returns a reference to a resource object corresponding to the start resource of the map.
                   2376: 
                   2377: =item * B<map_type>: Returns a string with the type of the map in it.
                   2378: 
                   2379: =back
1.51      bowersj2 2380: 
1.70      bowersj2 2381: =cut
1.51      bowersj2 2382: 
                   2383: sub map_finish {
                   2384:     my $self = shift;
                   2385:     my $src = $self->src();
                   2386:     my $res = $self->navHash("map_finish_$src", 0);
                   2387:     $res = $self->{NAV_MAP}->getById($res);
                   2388:     return $res;
                   2389: }
1.70      bowersj2 2390: sub map_pc {
                   2391:     my $self = shift;
                   2392:     my $src = $self->src();
                   2393:     return $self->navHash("map_pc_$src", 0);
                   2394: }
1.51      bowersj2 2395: sub map_start {
                   2396:     my $self = shift;
                   2397:     my $src = $self->src();
                   2398:     my $res = $self->navHash("map_start_$src", 0);
                   2399:     $res = $self->{NAV_MAP}->getById($res);
                   2400:     return $res;
                   2401: }
                   2402: sub map_type {
                   2403:     my $self = shift;
                   2404:     my $pc = $self->map_pc();
                   2405:     return $self->navHash("map_type_$pc", 0);
                   2406: }
                   2407: 
                   2408: 
                   2409: 
                   2410: #####
                   2411: # Property queries
                   2412: #####
                   2413: 
                   2414: # These functions will be responsible for returning the CORRECT
                   2415: # VALUE for the parameter, no matter what. So while they may look
                   2416: # like direct calls to parmval, they can be more then that.
                   2417: # So, for instance, the duedate function should use the "duedatetype"
                   2418: # information, rather then the resource object user.
                   2419: 
                   2420: =pod
                   2421: 
                   2422: =head2 Resource Parameters
                   2423: 
1.70      bowersj2 2424: 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 2425: 
                   2426: 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.)
                   2427: 
                   2428: =over 4
                   2429: 
                   2430: =item * B<acc>: Get the Client IP/Name Access Control information.
                   2431: 
                   2432: =item * B<answerdate>: Get the answer-reveal date for the problem.
                   2433: 
                   2434: =item * B<duedate>: Get the due date for the problem.
                   2435: 
                   2436: =item * B<tries>: Get the number of tries the student has used on the problem.
                   2437: 
                   2438: =item * B<maxtries>: Get the number of max tries allowed.
                   2439: 
                   2440: =item * B<opendate>: Get the open date for the problem.
                   2441: 
                   2442: =item * B<sig>: Get the significant figures setting.
                   2443: 
                   2444: =item * B<tol>: Get the tolerance for the problem.
                   2445: 
1.70      bowersj2 2446: =item * B<tries>: Get the number of tries the user has already used on the problem.
                   2447: 
1.51      bowersj2 2448: =item * B<type>: Get the question type for the problem.
                   2449: 
                   2450: =item * B<weight>: Get the weight for the problem.
                   2451: 
                   2452: =back
                   2453: 
                   2454: =cut
                   2455: 
                   2456: sub acc {
                   2457:     (my $self, my $part) = @_;
                   2458:     return $self->parmval("acc", $part);
                   2459: }
                   2460: sub answerdate {
                   2461:     (my $self, my $part) = @_;
                   2462:     # Handle intervals
                   2463:     if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
                   2464:         return $self->duedate($part) + 
                   2465:             $self->parmval("answerdate", $part);
                   2466:     }
                   2467:     return $self->parmval("answerdate", $part);
                   2468: }
                   2469: sub duedate {
                   2470:     (my $self, my $part) = @_;
                   2471:     return $self->parmval("duedate", $part);
                   2472: }
                   2473: sub maxtries {
                   2474:     (my $self, my $part) = @_;
                   2475:     return $self->parmval("maxtries", $part);
                   2476: }
                   2477: sub opendate {
                   2478:     (my $self, my $part) = @_;
                   2479:     if ($self->parmval("opendate.type", $part) eq 'date_interval') {
                   2480:         return $self->duedate($part) -
                   2481:             $self->parmval("opendate", $part);
                   2482:     }
                   2483:     return $self->parmval("opendate");
                   2484: }
                   2485: sub sig {
                   2486:     (my $self, my $part) = @_;
                   2487:     return $self->parmval("sig", $part);
                   2488: }
                   2489: sub tol {
                   2490:     (my $self, my $part) = @_;
                   2491:     return $self->parmval("tol", $part);
                   2492: }
                   2493: sub tries {
                   2494:     my $self = shift;
                   2495:     my $part = shift;
                   2496:     $part = '0' if (!defined($part));
                   2497: 
                   2498:     # Make sure return hash is loaded, should error check
                   2499:     $self->getReturnHash();
                   2500:     
                   2501:     my $tries = $self->{RETURN_HASH}->{'resource.'.$part.'.tries'};
                   2502:     if (!defined($tries)) {return '0';}
                   2503:     return $tries;
                   2504: }
1.70      bowersj2 2505: sub type {
                   2506:     (my $self, my $part) = @_;
                   2507:     return $self->parmval("type", $part);
                   2508: }
                   2509: sub weight {
                   2510:     (my $self, my $part) = @_;
                   2511:     return $self->parmval("weight", $part);
                   2512: }
1.51      bowersj2 2513: 
                   2514: # Multiple things need this
                   2515: sub getReturnHash {
                   2516:     my $self = shift;
                   2517:     
                   2518:     if (!defined($self->{RETURN_HASH})) {
1.84      bowersj2 2519:         my %tmpHash  = &Apache::lonnet::restore($self->symb());
1.51      bowersj2 2520:         $self->{RETURN_HASH} = \%tmpHash;
                   2521:     }
                   2522: }       
                   2523: 
                   2524: ######
                   2525: # Status queries
                   2526: ######
                   2527: 
                   2528: # These methods query the status of problems.
                   2529: 
                   2530: # If we need to count parts, this function determines the number of
                   2531: # parts from the metadata. When called, it returns a reference to a list
                   2532: # of strings corresponding to the parts. (Thus, using it in a scalar context
                   2533: # tells you how many parts you have in the problem:
                   2534: # $partcount = scalar($resource->countParts());
                   2535: # Don't use $self->{PARTS} directly because you don't know if it's been
                   2536: # computed yet.
                   2537: 
                   2538: =pod
                   2539: 
                   2540: =head2 Resource misc
                   2541: 
                   2542: Misc. functions for the resource.
                   2543: 
                   2544: =over 4
                   2545: 
1.59      bowersj2 2546: =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.
                   2547: 
                   2548: =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:
                   2549: 
                   2550:  for (split(/\,/, $res->getFeedback())) {
                   2551:     my $link = &Apache::lonnet::escape($_);
                   2552:     ...
                   2553: 
                   2554: and use the link as appropriate.
                   2555: 
                   2556: =cut
                   2557: 
                   2558: sub hasDiscussion {
                   2559:     my $self = shift;
                   2560:     return $self->{NAV_MAP}->hasDiscussion($self->symb());
                   2561: }
                   2562: 
                   2563: sub getFeedback {
                   2564:     my $self = shift;
1.85      bowersj2 2565:     return $self->{NAV_MAP}->getFeedback($self->src());
1.59      bowersj2 2566: }
                   2567: 
                   2568: =pod
                   2569: 
1.70      bowersj2 2570: =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 2571: 
1.70      bowersj2 2572: =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 2573: 
                   2574: =back
                   2575: 
                   2576: =cut
                   2577: 
                   2578: sub parts {
                   2579:     my $self = shift;
                   2580: 
1.67      bowersj2 2581:     if ($self->ext) { return ['0']; }
                   2582: 
1.51      bowersj2 2583:     $self->extractParts();
                   2584:     return $self->{PARTS};
                   2585: }
                   2586: 
                   2587: sub countParts {
                   2588:     my $self = shift;
                   2589:     
                   2590:     my $parts = $self->parts();
1.66      bowersj2 2591: 
                   2592:     if ($self->{RESOURCE_ERROR}) {
                   2593:         return 0;
                   2594:     }
                   2595: 
                   2596:     if (scalar(@{$parts}) < 2) { return 1;}
1.51      bowersj2 2597: 
                   2598:     return scalar(@{$parts}) - 1;
                   2599: }
                   2600: 
                   2601: # Private function: Extracts the parts information and saves it
                   2602: sub extractParts { 
                   2603:     my $self = shift;
                   2604:     
                   2605:     return if ($self->{PARTS});
1.67      bowersj2 2606:     return if ($self->ext);
1.51      bowersj2 2607: 
                   2608:     $self->{PARTS} = [];
                   2609: 
1.82      bowersj2 2610:     # Retrieve part count, if this is a problem
                   2611:     if ($self->is_problem()) {
                   2612:         my $metadata = &Apache::lonnet::metadata($self->src(), 'allpossiblekeys');
                   2613:         if (!$metadata) {
                   2614:             $self->{RESOURCE_ERROR} = 1;
                   2615:             $self->{PARTS} = [];
                   2616:             return;
                   2617:         }
                   2618:         
                   2619:         foreach (split(/\,/,$metadata)) {
                   2620:             if ($_ =~ /^parameter\_(.*)\_opendate$/) {
                   2621:                 push @{$self->{PARTS}}, $1;
                   2622:             }
1.51      bowersj2 2623:         }
1.82      bowersj2 2624:         
                   2625:         
                   2626:         # Is this possible to do in one line? - Jeremy
                   2627:         my @sortedParts = sort @{$self->{PARTS}};
                   2628:         $self->{PARTS} = \@sortedParts;
1.51      bowersj2 2629:     }
                   2630: 
                   2631:     return;
                   2632: }
                   2633: 
                   2634: =pod
                   2635: 
                   2636: =head2 Resource Status
                   2637: 
                   2638: 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.
                   2639: 
                   2640: There are two aspects to the status: the date-related information and the completion information.
                   2641: 
                   2642: Idiomatic usage of these two methods would probably look something like
                   2643: 
                   2644:  foreach ($resource->parts()) {
                   2645:     my $dateStatus = $resource->getDateStatus($_);
                   2646:     my $completionStatus = $resource->getCompletionStatus($_);
                   2647: 
1.70      bowersj2 2648:     or
                   2649: 
                   2650:     my $status = $resource->status($_);
                   2651: 
1.51      bowersj2 2652:     ... use it here ...
                   2653:  }
                   2654: 
                   2655: =over 4
                   2656: 
1.70      bowersj2 2657: =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 2658: 
                   2659: =back
                   2660: 
                   2661: B<Date Codes>
                   2662: 
                   2663: =over 4
                   2664: 
                   2665: =item * B<OPEN_LATER>: The problem will be opened later.
                   2666: 
                   2667: =item * B<OPEN>: Open and not yet due.
                   2668: 
1.59      bowersj2 2669: 
1.54      bowersj2 2670: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
                   2671: 
                   2672: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51      bowersj2 2673: 
                   2674: =item * B<ANSWER_OPEN>: The answer date is here.
                   2675: 
                   2676: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
                   2677: 
                   2678: =back
                   2679: 
                   2680: =cut
                   2681: 
                   2682: # Apparently the compiler optimizes these into constants automatically
1.54      bowersj2 2683: sub OPEN_LATER             { return 0; }
                   2684: sub OPEN                   { return 1; }
                   2685: sub PAST_DUE_NO_ANSWER     { return 2; }
                   2686: sub PAST_DUE_ANSWER_LATER  { return 3; }
                   2687: sub ANSWER_OPEN            { return 4; }
                   2688: sub NOTHING_SET            { return 5; } 
                   2689: sub NETWORK_FAILURE        { return 100; }
                   2690: 
                   2691: # getDateStatus gets the date status for a given problem part. 
                   2692: # Because answer date, due date, and open date are fully independent
                   2693: # (i.e., it is perfectly possible to *only* have an answer date), 
                   2694: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
                   2695: # (past, future, none given). This function handles this with a decision
                   2696: # tree. Read the comments to follow the decision tree.
1.51      bowersj2 2697: 
                   2698: sub getDateStatus {
                   2699:     my $self = shift;
                   2700:     my $part = shift;
                   2701:     $part = "0" if (!defined($part));
1.54      bowersj2 2702: 
                   2703:     # Always return network failure if there was one.
1.51      bowersj2 2704:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   2705: 
                   2706:     my $now = time();
                   2707: 
1.53      bowersj2 2708:     my $open = $self->opendate($part);
                   2709:     my $due = $self->duedate($part);
                   2710:     my $answer = $self->answerdate($part);
                   2711: 
                   2712:     if (!$open && !$due && !$answer) {
                   2713:         # no data on the problem at all
                   2714:         # should this be the same as "open later"? think multipart.
                   2715:         return $self->NOTHING_SET;
                   2716:     }
1.59      bowersj2 2717:     if (!$open || $now < $open) {return $self->OPEN_LATER}
                   2718:     if (!$due || $now < $due) {return $self->OPEN}
                   2719:     if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
                   2720:     if ($answer) { return $self->ANSWER_OPEN; }
1.54      bowersj2 2721:     return PAST_DUE_NO_ANSWER;
1.51      bowersj2 2722: }
                   2723: 
                   2724: =pod
                   2725: 
                   2726: B<>
                   2727: 
                   2728: =over 4
                   2729: 
                   2730: =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:
                   2731: 
                   2732: =back 
                   2733: 
                   2734: B<Completion Codes>
                   2735: 
                   2736: =over 4
                   2737: 
                   2738: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
                   2739: 
                   2740: =item * B<INCORRECT>: Attempted, but wrong by student.
                   2741: 
                   2742: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
                   2743: 
                   2744: =item * B<CORRECT>: Correct or correct by instructor.
                   2745: 
                   2746: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
                   2747: 
                   2748: =item * B<EXCUSED>: Excused. Not yet implemented.
                   2749: 
                   2750: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
                   2751: 
1.69      bowersj2 2752: =item * B<ATTEMPTED>: Attempted, and not yet graded.
                   2753: 
1.51      bowersj2 2754: =back
                   2755: 
                   2756: =cut
                   2757: 
1.53      bowersj2 2758: sub NOT_ATTEMPTED         { return 10; }
                   2759: sub INCORRECT             { return 11; }
                   2760: sub INCORRECT_BY_OVERRIDE { return 12; }
                   2761: sub CORRECT               { return 13; }
                   2762: sub CORRECT_BY_OVERRIDE   { return 14; }
                   2763: sub EXCUSED               { return 15; }
1.69      bowersj2 2764: sub ATTEMPTED             { return 16; }
1.51      bowersj2 2765: 
                   2766: sub getCompletionStatus {
                   2767:     my $self = shift;
                   2768:     my $part = shift;
                   2769:     $part = "0" if (!defined($part));
                   2770:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   2771: 
                   2772:     # Make sure return hash exists
                   2773:     $self->getReturnHash();
                   2774:     
                   2775:     my $status = $self->{RETURN_HASH}->{'resource.'.$part.'.solved'};
                   2776: 
                   2777:     # Left as seperate if statements in case we ever do more with this
                   2778:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
                   2779:     if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
                   2780:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
                   2781:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
                   2782:     if ($status eq 'excused') {return $self->EXCUSED; }
1.69      bowersj2 2783:     if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51      bowersj2 2784:     return $self->NOT_ATTEMPTED;
                   2785: }
                   2786: 
                   2787: =pod
                   2788: 
                   2789: B<Composite Status>
                   2790: 
                   2791: 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()):
                   2792: 
                   2793: =over 4
                   2794: 
1.70      bowersj2 2795: =item * B<NETWORK_FAILURE>: The network has failed and the information is not available.
1.51      bowersj2 2796: 
1.70      bowersj2 2797: =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 2798: 
1.70      bowersj2 2799: =item * B<CORRECT>: For any reason at all, the part is considered correct.
1.51      bowersj2 2800: 
1.70      bowersj2 2801: =item * B<EXCUSED>: For any reason at all, the problem is excused.
1.51      bowersj2 2802: 
1.70      bowersj2 2803: =item * B<PAST_DUE_NO_ANSWER>: The problem is past due, not considered correct, and no answer date is set.
1.54      bowersj2 2804: 
1.70      bowersj2 2805: =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 2806: 
1.70      bowersj2 2807: =item * B<ANSWER_OPEN>: The problem is past due, not correct, and the answer is now available.
1.51      bowersj2 2808: 
1.70      bowersj2 2809: =item * B<OPEN_LATER>: The problem is not yet open.
1.51      bowersj2 2810: 
1.70      bowersj2 2811: =item * B<TRIES_LEFT>: The problem is open, has been tried, is not correct, but there are tries left.
1.51      bowersj2 2812: 
1.70      bowersj2 2813: =item * B<INCORRECT>: The problem is open, and all tries have been used without getting the correct answer.
1.51      bowersj2 2814: 
1.70      bowersj2 2815: =item * B<OPEN>: The item is open and not yet tried.
1.51      bowersj2 2816: 
1.70      bowersj2 2817: =item * B<ATTEMPTED>: The problem has been attempted.
1.69      bowersj2 2818: 
1.51      bowersj2 2819: =back
                   2820: 
                   2821: =cut
                   2822: 
                   2823: sub TRIES_LEFT { return 10; }
                   2824: 
                   2825: sub status {
                   2826:     my $self = shift;
                   2827:     my $part = shift;
                   2828:     if (!defined($part)) { $part = "0"; }
                   2829:     my $completionStatus = $self->getCompletionStatus($part);
                   2830:     my $dateStatus = $self->getDateStatus($part);
                   2831: 
                   2832:     # What we have is a two-dimensional matrix with 4 entries on one
                   2833:     # dimension and 5 entries on the other, which we want to colorize,
1.54      bowersj2 2834:     # plus network failure and "no date data at all".
1.51      bowersj2 2835: 
1.53      bowersj2 2836:     if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51      bowersj2 2837: 
                   2838:     # There are a few whole rows we can dispose of:
1.53      bowersj2 2839:     if ($completionStatus == CORRECT ||
                   2840:         $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69      bowersj2 2841:         return CORRECT; 
                   2842:     }
                   2843: 
                   2844:     if ($completionStatus == ATTEMPTED) {
                   2845:         return ATTEMPTED;
1.53      bowersj2 2846:     }
                   2847: 
                   2848:     # If it's EXCUSED, then return that no matter what
                   2849:     if ($completionStatus == EXCUSED) {
                   2850:         return EXCUSED; 
1.51      bowersj2 2851:     }
                   2852: 
1.53      bowersj2 2853:     if ($dateStatus == NOTHING_SET) {
                   2854:         return NOTHING_SET;
1.51      bowersj2 2855:     }
                   2856: 
1.69      bowersj2 2857:     # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
                   2858:     # by 4 matrix (date statuses).
1.51      bowersj2 2859: 
1.54      bowersj2 2860:     if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59      bowersj2 2861:         $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54      bowersj2 2862:         return $dateStatus; 
1.51      bowersj2 2863:     }
                   2864: 
1.53      bowersj2 2865:     if ($dateStatus == ANSWER_OPEN) {
                   2866:         return ANSWER_OPEN;
1.51      bowersj2 2867:     }
                   2868: 
                   2869:     # Now: (incorrect, incorrect_override, not_attempted) x 
                   2870:     # (open_later), (open)
                   2871:     
1.53      bowersj2 2872:     if ($dateStatus == OPEN_LATER) {
                   2873:         return OPEN_LATER;
1.51      bowersj2 2874:     }
                   2875: 
                   2876:     # If it's WRONG...
1.53      bowersj2 2877:     if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51      bowersj2 2878:         # and there are TRIES LEFT:
1.79      bowersj2 2879:         if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53      bowersj2 2880:             return TRIES_LEFT;
1.51      bowersj2 2881:         }
1.53      bowersj2 2882:         return INCORRECT; # otherwise, return orange; student can't fix this
1.51      bowersj2 2883:     }
                   2884: 
                   2885:     # Otherwise, it's untried and open
1.53      bowersj2 2886:     return OPEN; 
1.51      bowersj2 2887: }
                   2888: 
                   2889: =pod
                   2890: 
                   2891: =head2 Resource/Nav Map Navigation
                   2892: 
                   2893: =over 4
                   2894: 
1.85      bowersj2 2895: =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.
                   2896: 
                   2897: =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 2898: 
                   2899: =cut
                   2900: 
                   2901: sub getNext {
                   2902:     my $self = shift;
                   2903:     my $alreadySeenHash = shift;
                   2904:     my @branches;
                   2905:     my $to = $self->to();
1.85      bowersj2 2906:     foreach my $branch ( split(/,/, $to) ) {
1.51      bowersj2 2907:         my $choice = $self->{NAV_MAP}->getById($branch);
                   2908:         my $next = $choice->goesto();
                   2909:         $next = $self->{NAV_MAP}->getById($next);
                   2910: 
1.61      bowersj2 2911:         # Don't remember it if we've already seen it or if
1.66      bowersj2 2912:         # the student doesn't have browse priviledges
                   2913:         my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
1.51      bowersj2 2914:         if (!defined($alreadySeenHash) ||
1.61      bowersj2 2915:             !defined($alreadySeenHash->{$next->{ID}}) ||
1.66      bowersj2 2916:             ($browsePriv ne '2' && $browsePriv ne 'F')) {
1.51      bowersj2 2917:                 push @branches, $next;
                   2918:             }
1.85      bowersj2 2919:     }
                   2920:     return \@branches;
                   2921: }
                   2922: 
                   2923: sub getPrevious {
                   2924:     my $self = shift;
1.87    ! www      2925:     my $alreadySeenHash = shift;
1.85      bowersj2 2926:     my @branches;
                   2927:     my $from = $self->from();
                   2928:     foreach my $branch ( split /,/, $from) {
                   2929:         my $choice = $self->{NAV_MAP}->getById($branch);
                   2930:         my $prev = $choice->comesfrom();
                   2931:         $prev = $self->{NAV_MAP}->getById($prev);
                   2932: 
                   2933:         # Skip it if we've already seen it or the user doesn't have
                   2934:         # browse privs
                   2935:         my $browsePriv = &Apache::lonnet::allowed('bre', $self->src);
                   2936:         if (!defined($alreadySeenHash) ||
1.87    ! www      2937:             !defined($alreadySeenHash->{$prev->{ID}}) ||
1.85      bowersj2 2938:             ($browsePriv ne '2' && $browsePriv ne 'F')) {
1.87    ! www      2939:             push @branches, $prev;
1.85      bowersj2 2940:         }
1.51      bowersj2 2941:     }
                   2942:     return \@branches;
                   2943: }
                   2944: 
                   2945: =pod
1.2       www      2946: 
1.51      bowersj2 2947: =back
1.2       www      2948: 
1.51      bowersj2 2949: =cut
1.2       www      2950: 
1.51      bowersj2 2951: 1;
1.2       www      2952: 
1.51      bowersj2 2953: __END__
1.2       www      2954: 
                   2955: 

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