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

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

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