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

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

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