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

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

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