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

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

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