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

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

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