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

1.72      bowersj2    1: 
1.2       www         2: # The LearningOnline Network with CAPA
                      3: # Navigate Maps Handler
1.1       www         4: #
1.137     bowersj2    5: # $Id: lonnavmaps.pm,v 1.136 2003/02/04 16:37:51 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?'&':'?') .
1.137     bowersj2 1448:                 'symb=' . &Apache::lonnet::escape($curRes->symb());
1.132     bowersj2 1449:             
                   1450:             # Now, display each column.
                   1451:             foreach my $col (@$cols) {
                   1452: 
1.133     bowersj2 1453:                 # If this is the first column and it's time to print
                   1454:                 # the anchor, do so
                   1455:                 if ($col == $cols->[0] && 
                   1456:                     $args->{'counter'} == $args->{'currentJumpIndex'} - 
                   1457:                     $args->{'currentJumpDelta'}) {
                   1458:                     $result .= '<a name="curloc" />';
                   1459:                     $displayedJumpMarker = 1;
                   1460:                 }
                   1461: 
                   1462: 
                   1463:                 if (ref($col)) {
                   1464:                     $result .= &$col($curRes, $part, $args);
                   1465:                 } else {
                   1466:                     $result .= &{$preparedColumns[$col]}($curRes, $part, $args);
                   1467:                 }
1.132     bowersj2 1468: 
                   1469:             }
                   1470: 
                   1471:             $result .= "  </tr>\n";
                   1472:         }
                   1473:         
                   1474: 
                   1475:         $curRes = $it->next();
                   1476:     }
                   1477:     
1.134     bowersj2 1478:     # Print out the part that jumps to #curloc if it exists
                   1479:     if ($args->{"displayedJumpMarker"}) {
                   1480:         $result .= "<script>location += "#curloc";</script>\n";
                   1481:     }
                   1482: 
1.132     bowersj2 1483:     $result .= "</table>";
                   1484: 
                   1485:     return $result;
                   1486: }
                   1487: 
1.133     bowersj2 1488: 1;
                   1489: 
                   1490: package Apache::lonnavmaps::navmap;
                   1491: 
                   1492: =pod
                   1493: 
                   1494: 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.
                   1495: 
                   1496: =head1 navmap object: Encapsulating the compiled nav map
                   1497: 
                   1498: navmap is an object that encapsulates a compiled course map and provides a reasonable interface to it.
                   1499: 
                   1500: 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.
                   1501: 
                   1502: You must obtain resource objects through the navmap object.
                   1503: 
                   1504: =head2 Methods
                   1505: 
                   1506: =over 4
                   1507: 
                   1508: =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.
                   1509: 
                   1510: =item * B<getIterator>(first, finish, filter, condition): See iterator documentation below.
                   1511: 
                   1512: =cut
                   1513: 
                   1514: use strict;
                   1515: use GDBM_File;
                   1516: 
                   1517: sub new {
                   1518:     # magic invocation to create a class instance
                   1519:     my $proto = shift;
                   1520:     my $class = ref($proto) || $proto;
                   1521:     my $self = {};
                   1522: 
                   1523:     $self->{NAV_HASH_FILE} = shift;
                   1524:     $self->{PARM_HASH_FILE} = shift;
                   1525:     $self->{GENERATE_COURSE_USER_OPT} = shift;
                   1526:     $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
                   1527: 
                   1528:     # Resource cache stores navmap resources as we reference them. We generate
                   1529:     # them on-demand so we don't pay for creating resources unless we use them.
                   1530:     $self->{RESOURCE_CACHE} = {};
                   1531: 
                   1532:     # Network failure flag, if we accessed the course or user opt and
                   1533:     # failed
                   1534:     $self->{NETWORK_FAILURE} = 0;
                   1535: 
                   1536:     # tie the nav hash
                   1537: 
                   1538:     if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
                   1539:               &GDBM_READER(), 0640))) {
                   1540:         return undef;
                   1541:     }
                   1542:     
                   1543:     if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
                   1544:               &GDBM_READER(), 0640)))
                   1545:     {
                   1546:         untie $self->{PARM_HASH};
                   1547:         return undef;
                   1548:     }
                   1549: 
                   1550:     $self->{HASH_TIED} = 1;
                   1551:     $self->{NAV_HASH} = \%navmaphash;
                   1552:     $self->{PARM_HASH} = \%parmhash;
                   1553: 
                   1554:     bless($self);
                   1555:         
                   1556:     return $self;
                   1557: }
                   1558: 
                   1559: sub init {
                   1560:     my $self = shift;
                   1561: 
                   1562:     # If the course opt hash and the user opt hash should be generated,
                   1563:     # generate them
                   1564:     if ($self->{GENERATE_COURSE_USER_OPT}) {
                   1565:         my $uname=$ENV{'user.name'};
                   1566:         my $udom=$ENV{'user.domain'};
                   1567:         my $uhome=$ENV{'user.home'};
                   1568:         my $cid=$ENV{'request.course.id'};
                   1569:         my $chome=$ENV{'course.'.$cid.'.home'};
                   1570:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1571:         
                   1572:         my $userprefix=$uname.'_'.$udom.'_';
                   1573:         
                   1574:         my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
                   1575:         unless ($uhome eq 'no_host') { 
                   1576: # ------------------------------------------------- Get coursedata (if present)
                   1577:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
                   1578:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                   1579:                                                  ':resourcedata',$chome);
                   1580:                 if ($reply!~/^error\:/) {
                   1581:                     $courserdatas{$cid}=$reply;
                   1582:                     $courserdatas{$cid.'.last_cache'}=time;
                   1583:                 }
                   1584:                 # check to see if network failed
                   1585:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1586:                 {
                   1587:                     $self->{NETWORK_FAILURE} = 1;
                   1588:                 }
                   1589:             }
                   1590:             foreach (split(/\&/,$courserdatas{$cid})) {
                   1591:                 my ($name,$value)=split(/\=/,$_);
                   1592:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1593:                     &Apache::lonnet::unescape($value);
                   1594:             }
                   1595: # --------------------------------------------------- Get userdata (if present)
                   1596:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
                   1597:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
                   1598:                 if ($reply!~/^error\:/) {
                   1599:                     $userrdatas{$uname.'___'.$udom}=$reply;
                   1600:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
                   1601:                 }
                   1602:                 # check to see if network failed
                   1603:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1604:                 {
                   1605:                     $self->{NETWORK_FAILURE} = 1;
                   1606:                 }
                   1607:             }
                   1608:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
                   1609:                 my ($name,$value)=split(/\=/,$_);
                   1610:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1611:                     &Apache::lonnet::unescape($value);
                   1612:             }
                   1613:             $self->{COURSE_OPT} = \%courseopt;
                   1614:             $self->{USER_OPT} = \%useropt;
                   1615:         }
                   1616:     }   
                   1617: 
                   1618:     if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
                   1619:         my $cid=$ENV{'request.course.id'};
                   1620:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1621:         
                   1622:         my %emailstatus = &Apache::lonnet::dump('email_status');
                   1623:         my $logoutTime = $emailstatus{'logout'};
                   1624:         my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
                   1625:         $self->{LAST_CHECK} = ($courseLeaveTime < $logoutTime ?
                   1626:                                $courseLeaveTime : $logoutTime);
                   1627:         my %discussiontime = &Apache::lonnet::dump('discussiontimes', 
                   1628:                                                    $cdom, $cnum);
                   1629:         my %feedback=();
                   1630:         my %error=();
                   1631:         my $keys = &Apache::lonnet::reply('keys:'.
                   1632:                                           $ENV{'user.domain'}.':'.
                   1633:                                           $ENV{'user.name'}.':nohist_email',
                   1634:                                           $ENV{'user.home'});
                   1635: 
                   1636:         foreach my $msgid (split(/\&/, $keys)) {
                   1637:             $msgid=&Apache::lonnet::unescape($msgid);
                   1638:             my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
                   1639:             if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
                   1640:                 my ($what,$url)=($1,$2);
                   1641:                 my %status=
                   1642:                     &Apache::lonnet::get('email_status',[$msgid]);
                   1643:                 if ($status{$msgid}=~/^error\:/) { 
                   1644:                     $status{$msgid}=''; 
                   1645:                 }
                   1646:                 
                   1647:                 if (($status{$msgid} eq 'new') || 
                   1648:                     (!$status{$msgid})) { 
                   1649:                     if ($what eq 'Error') {
                   1650:                         $error{$url}.=','.$msgid; 
                   1651:                     } else {
                   1652:                         $feedback{$url}.=','.$msgid;
                   1653:                     }
                   1654:                 }
                   1655:             }
                   1656:         }
                   1657:         
                   1658:         $self->{FEEDBACK} = \%feedback;
                   1659:         $self->{ERROR_MSG} = \%error; # what is this? JB
                   1660:         $self->{DISCUSSION_TIME} = \%discussiontime;
                   1661:         $self->{EMAIL_STATUS} = \%emailstatus;
                   1662:         
                   1663:     }    
                   1664: 
                   1665:     $self->{PARM_CACHE} = {};
                   1666: }
                   1667: 
                   1668: # Internal function: Takes a key to look up in the nav hash and implements internal
                   1669: # memory caching of that key.
                   1670: sub navhash {
                   1671:     my $self = shift; my $key = shift;
                   1672:     return $self->{NAV_HASH}->{$key};
                   1673: }
                   1674: 
                   1675: # Checks to see if coursemap is defined, matching test in old lonnavmaps
                   1676: sub courseMapDefined {
                   1677:     my $self = shift;
                   1678:     my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
                   1679: 
                   1680:     my $firstres = $self->navhash("map_start_$uri");
                   1681:     my $lastres = $self->navhash("map_finish_$uri");
                   1682:     return $firstres && $lastres;
                   1683: }
                   1684: 
                   1685: sub getIterator {
                   1686:     my $self = shift;
                   1687:     my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
                   1688:                                                      shift, undef, shift);
                   1689:     return $iterator;
                   1690: }
                   1691: 
                   1692: # unties the hash when done
                   1693: sub untieHashes {
                   1694:     my $self = shift;
                   1695:     untie %{$self->{NAV_HASH}} if ($self->{HASH_TIED});
                   1696:     untie %{$self->{PARM_HASH}} if ($self->{HASH_TIED});
                   1697:     $self->{HASH_TIED} = 0;
                   1698: }
                   1699: 
                   1700: # when the object is destroyed, be sure to untie all the hashes we tied.
                   1701: sub DESTROY {
                   1702:     my $self = shift;
                   1703:     $self->untieHashes();
                   1704: }
                   1705: 
                   1706: # Private method: Does the given resource (as a symb string) have
                   1707: # current discussion? Returns 0 if chat/mail data not extracted.
                   1708: sub hasDiscussion {
                   1709:     my $self = shift;
                   1710:     my $symb = shift;
                   1711:     if (!defined($self->{DISCUSSION_TIME})) { return 0; }
                   1712: 
                   1713:     #return defined($self->{DISCUSSION_TIME}->{$symb});
                   1714:     return $self->{DISCUSSION_TIME}->{$symb} >
                   1715:            $self->{LAST_CHECK};
                   1716: }
                   1717: 
                   1718: # Private method: Does the given resource (as a symb string) have
                   1719: # current feedback? Returns the string in the feedback hash, which
                   1720: # will be false if it does not exist.
                   1721: sub getFeedback { 
                   1722:     my $self = shift;
                   1723:     my $symb = shift;
                   1724: 
                   1725:     if (!defined($self->{FEEDBACK})) { return ""; }
                   1726:     
                   1727:     return $self->{FEEDBACK}->{$symb};
                   1728: }
                   1729: 
                   1730: # Private method: Get the errors for that resource (by source).
                   1731: sub getErrors { 
                   1732:     my $self = shift;
                   1733:     my $src = shift;
                   1734:     
                   1735:     if (!defined($self->{ERROR_MSG})) { return ""; }
                   1736:     return $self->{ERROR_MSG}->{$src};
                   1737: }
                   1738: 
                   1739: =pod
                   1740: 
                   1741: =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.
                   1742: 
                   1743: =cut
                   1744: 
                   1745: # The strategy here is to cache the resource objects, and only construct them
                   1746: # as we use them. The real point is to prevent reading any more from the tied
                   1747: # hash then we have to, which should hopefully alleviate speed problems.
                   1748: # Caching is just an incidental detail I throw in because it makes sense.
                   1749: 
                   1750: sub getById {
                   1751:     my $self = shift;
                   1752:     my $id = shift;
                   1753: 
                   1754:     if (defined ($self->{RESOURCE_CACHE}->{$id}))
                   1755:     {
                   1756:         return $self->{RESOURCE_CACHE}->{$id};
                   1757:     }
                   1758: 
                   1759:     # resource handles inserting itself into cache.
                   1760:     # Not clear why the quotes are necessary, but as of this
                   1761:     # writing it doesn't work without them.
                   1762:     return "Apache::lonnavmaps::resource"->new($self, $id);
1.132     bowersj2 1763: }
1.133     bowersj2 1764: 
                   1765: =pod
                   1766: 
                   1767: =item * B<firstResource>(): Returns a resource object reference corresponding to the first resource in the navmap.
                   1768: 
                   1769: =cut
                   1770: 
                   1771: sub firstResource {
                   1772:     my $self = shift;
                   1773:     my $firstResource = $self->navhash('map_start_' .
                   1774:                      &Apache::lonnet::clutter($ENV{'request.course.uri'}));
                   1775:     return $self->getById($firstResource);
1.132     bowersj2 1776: }
1.133     bowersj2 1777: 
                   1778: =pod
                   1779: 
                   1780: =item * B<finishResource>(): Returns a resource object reference corresponding to the last resource in the navmap.
                   1781: 
                   1782: =cut
                   1783: 
                   1784: sub finishResource {
                   1785:     my $self = shift;
                   1786:     my $firstResource = $self->navhash('map_finish_' .
                   1787:                      &Apache::lonnet::clutter($ENV{'request.course.uri'}));
                   1788:     return $self->getById($firstResource);
1.132     bowersj2 1789: }
1.133     bowersj2 1790: 
                   1791: # Parmval reads the parm hash and cascades the lookups. parmval_real does
                   1792: # the actual lookup; parmval caches the results.
                   1793: sub parmval {
                   1794:     my $self = shift;
                   1795:     my ($what,$symb)=@_;
                   1796:     my $hashkey = $what."|||".$symb;
                   1797: 
                   1798:     if (defined($self->{PARM_CACHE}->{$hashkey})) {
                   1799:         return $self->{PARM_CACHE}->{$hashkey};
                   1800:     }
                   1801: 
                   1802:     my $result = $self->parmval_real($what, $symb);
                   1803:     $self->{PARM_CACHE}->{$hashkey} = $result;
                   1804:     return $result;
1.132     bowersj2 1805: }
                   1806: 
1.133     bowersj2 1807: sub parmval_real {
                   1808:     my $self = shift;
                   1809:     my ($what,$symb) = @_;
                   1810: 
                   1811:     my $cid=$ENV{'request.course.id'};
                   1812:     my $csec=$ENV{'request.course.sec'};
                   1813:     my $uname=$ENV{'user.name'};
                   1814:     my $udom=$ENV{'user.domain'};
                   1815: 
                   1816:     unless ($symb) { return ''; }
                   1817:     my $result='';
                   1818: 
                   1819:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                   1820: 
                   1821: # ----------------------------------------------------- Cascading lookup scheme
                   1822:     my $rwhat=$what;
                   1823:     $what=~s/^parameter\_//;
                   1824:     $what=~s/\_/\./;
                   1825: 
                   1826:     my $symbparm=$symb.'.'.$what;
                   1827:     my $mapparm=$mapname.'___(all).'.$what;
                   1828:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
                   1829: 
                   1830:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
                   1831:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
                   1832:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
                   1833: 
                   1834:     my $courselevel= $usercourseprefix.'.'.$what;
                   1835:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
                   1836:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
                   1837: 
                   1838:     my $useropt = $self->{USER_OPT};
                   1839:     my $courseopt = $self->{COURSE_OPT};
                   1840:     my $parmhash = $self->{PARM_HASH};
                   1841: 
                   1842: # ---------------------------------------------------------- first, check user
                   1843:     if ($uname and defined($useropt)) {
                   1844:         if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
                   1845:         if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
                   1846:         if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
                   1847:     }
                   1848: 
                   1849: # ------------------------------------------------------- second, check course
                   1850:     if ($csec and defined($courseopt)) {
                   1851:         if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
                   1852:         if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
                   1853:         if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
                   1854:     }
                   1855: 
                   1856:     if (defined($courseopt)) {
                   1857:         if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
                   1858:         if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
                   1859:         if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
                   1860:     }
                   1861: 
                   1862: # ----------------------------------------------------- third, check map parms
                   1863: 
                   1864:     my $thisparm=$$parmhash{$symbparm};
                   1865:     if (defined($thisparm)) { return $thisparm; }
                   1866: 
                   1867: # ----------------------------------------------------- fourth , check default
                   1868: 
                   1869:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
                   1870:     if (defined($default)) { return $default}
                   1871: 
                   1872: # --------------------------------------------------- fifth , cascade up parts
                   1873: 
                   1874:     my ($space,@qualifier)=split(/\./,$rwhat);
                   1875:     my $qualifier=join('.',@qualifier);
                   1876:     unless ($space eq '0') {
                   1877:         my ($part,$id)=split(/\_/,$space);
                   1878:         if ($id) {
                   1879:             my $partgeneral=$self->parmval($part.".$qualifier",$symb);
                   1880:             if (defined($partgeneral)) { return $partgeneral; }
                   1881:         } else {
                   1882:             my $resourcegeneral=$self->parmval("0.$qualifier",$symb);
                   1883:             if (defined($resourcegeneral)) { return $resourcegeneral; }
                   1884:         }
                   1885:     }
                   1886:     return '';
                   1887: }
1.51      bowersj2 1888: 
                   1889: 1;
                   1890: 
                   1891: package Apache::lonnavmaps::iterator;
                   1892: 
                   1893: =pod
                   1894: 
                   1895: =back
                   1896: 
                   1897: =head1 navmap Iterator
                   1898: 
                   1899: 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.
                   1900: 
                   1901: 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:
                   1902: 
                   1903: C<my $resourceIterator = $navmap-E<gt>getIterator();>
                   1904: 
                   1905: To get the next thing from the iterator, call B<next>:
                   1906: 
                   1907: C<my $nextThing = $resourceIterator-E<gt>next()>
                   1908: 
                   1909: getIterator behaves as follows:
                   1910: 
                   1911: =over 4
                   1912: 
1.116     bowersj2 1913: =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 1914: 
1.101     bowersj2 1915: 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 1916: 
                   1917: 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:
                   1918: 
                   1919: =over 4
                   1920: 
1.70      bowersj2 1921: =item * BEGIN_MAP: A new map is being recursed into. This is returned I<after> the map resource itself is returned.
                   1922: 
                   1923: =item * END_MAP: The map is now done.
                   1924: 
                   1925: =item * BEGIN_BRANCH: A branch is now starting. The next resource returned will be the first in that branch.
                   1926: 
                   1927: =item * END_BRANCH: The branch is now done.
1.51      bowersj2 1928: 
                   1929: =back
                   1930: 
1.70      bowersj2 1931: The tokens are retreivable via methods on the iterator object, i.e., $iterator->END_MAP.
                   1932: 
1.116     bowersj2 1933: 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.
                   1934: 
1.70      bowersj2 1935: =back
1.51      bowersj2 1936: 
                   1937: =cut
                   1938: 
                   1939: # Here are the tokens for the iterator:
                   1940: 
                   1941: sub BEGIN_MAP { return 1; }    # begining of a new map
                   1942: sub END_MAP { return 2; }      # end of the map
                   1943: sub BEGIN_BRANCH { return 3; } # beginning of a branch
                   1944: sub END_BRANCH { return 4; }   # end of a branch
1.89      bowersj2 1945: sub FORWARD { return 1; }      # go forward
                   1946: sub BACKWARD { return 2; }
1.51      bowersj2 1947: 
1.96      bowersj2 1948: sub min {
                   1949:     (my $a, my $b) = @_;
                   1950:     if ($a < $b) { return $a; } else { return $b; }
                   1951: }
                   1952: 
1.107     bowersj2 1953: # In the CVS repository, documentation of this algorithm is included 
                   1954: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
                   1955: # will reference the same location in the text as the part of the
                   1956: # algorithm is running through.
                   1957: 
1.94      bowersj2 1958: sub new {
                   1959:     # magic invocation to create a class instance
                   1960:     my $proto = shift;
                   1961:     my $class = ref($proto) || $proto;
                   1962:     my $self = {};
                   1963: 
                   1964:     $self->{NAV_MAP} = shift;
                   1965:     return undef unless ($self->{NAV_MAP});
                   1966: 
                   1967:     # Handle the parameters
                   1968:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   1969:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   1970: 
                   1971:     # If the given resources are just the ID of the resource, get the
                   1972:     # objects
                   1973:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   1974:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   1975:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   1976:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   1977: 
                   1978:     $self->{FILTER} = shift;
                   1979: 
                   1980:     # A hash, used as a set, of resource already seen
                   1981:     $self->{ALREADY_SEEN} = shift;
                   1982:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
                   1983:     $self->{CONDITION} = shift;
                   1984: 
1.116     bowersj2 1985:     # Do we want to automatically follow "redirection" maps?
                   1986:     $self->{FORCE_TOP} = shift;
                   1987: 
1.94      bowersj2 1988:     # Now, we need to pre-process the map, by walking forward and backward
                   1989:     # over the parts of the map we're going to look at.
1.96      bowersj2 1990: 
1.97      bowersj2 1991:     # The processing steps are exactly the same, except for a few small 
                   1992:     # changes, so I bundle those up in the following list of two elements:
                   1993:     # (direction_to_iterate, VAL_name, next_resource_method_to_call,
                   1994:     # first_resource).
                   1995:     # This prevents writing nearly-identical code twice.
                   1996:     my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext', 
                   1997:                         'FIRST_RESOURCE'],
                   1998:                        [BACKWARD(), 'BOT_UP_VAL', 'getPrevious', 
                   1999:                         'FINISH_RESOURCE'] );
                   2000: 
1.98      bowersj2 2001:     my $maxDepth = 0; # tracks max depth
                   2002: 
1.116     bowersj2 2003:     # If there is only one resource in this map, and it's a map, we
                   2004:     # want to remember that, so the user can ask for the first map
                   2005:     # that isn't just a redirector.
                   2006:     my $resource; my $resourceCount = 0;
                   2007: 
1.107     bowersj2 2008:     # **1**
                   2009: 
1.97      bowersj2 2010:     foreach my $pass (@iterations) {
                   2011:         my $direction = $pass->[0];
                   2012:         my $valName = $pass->[1];
                   2013:         my $nextResourceMethod = $pass->[2];
                   2014:         my $firstResourceName = $pass->[3];
                   2015: 
                   2016:         my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP}, 
                   2017:                                                             $self->{FIRST_RESOURCE},
                   2018:                                                             $self->{FINISH_RESOURCE},
                   2019:                                                             {}, undef, 0, $direction);
1.96      bowersj2 2020:     
1.97      bowersj2 2021:         # prime the recursion
                   2022:         $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98      bowersj2 2023:         my $depth = 0;
1.97      bowersj2 2024:         $iterator->next();
                   2025:         my $curRes = $iterator->next();
1.98      bowersj2 2026:         while ($depth > -1) {
1.97      bowersj2 2027:             if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
                   2028:             if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96      bowersj2 2029:         
1.97      bowersj2 2030:             if (ref($curRes)) {
1.116     bowersj2 2031:                 # If there's only one resource, this will save it
1.117     bowersj2 2032:                 # we have to filter empty resources from consideration here,
                   2033:                 # or even "empty", redirecting maps have two (start & finish)
                   2034:                 # or three (start, finish, plus redirector)
                   2035:                 if($direction == FORWARD && $curRes->src()) { 
                   2036:                     $resource = $curRes; $resourceCount++; 
                   2037:                 }
1.97      bowersj2 2038:                 my $resultingVal = $curRes->{DATA}->{$valName};
                   2039:                 my $nextResources = $curRes->$nextResourceMethod();
1.116     bowersj2 2040:                 my $nextCount = scalar(@{$nextResources});
1.104     bowersj2 2041: 
1.116     bowersj2 2042:                 if ($nextCount == 1) { # **3**
1.97      bowersj2 2043:                     my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
                   2044:                     $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
                   2045:                 }
                   2046:                 
1.116     bowersj2 2047:                 if ($nextCount > 1) { # **4**
1.97      bowersj2 2048:                     foreach my $res (@{$nextResources}) {
                   2049:                         my $current = $res->{DATA}->{$valName} || 999999999;
                   2050:                         $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
                   2051:                     }
                   2052:                 }
                   2053:             }
1.96      bowersj2 2054:             
1.107     bowersj2 2055:             # Assign the final val (**2**)
1.97      bowersj2 2056:             if (ref($curRes) && $direction == BACKWARD()) {
1.98      bowersj2 2057:                 my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
                   2058:                                      $curRes->{DATA}->{BOT_UP_VAL});
                   2059:                 
                   2060:                 $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
                   2061:                 if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
                   2062:                 }
1.97      bowersj2 2063:             $curRes = $iterator->next();
1.96      bowersj2 2064:         }
                   2065:     }
1.94      bowersj2 2066: 
1.116     bowersj2 2067:     # Check: Was this only one resource, a map?
                   2068:     if ($resourceCount == 1 && $resource->is_map() && !$self->{FORCE_TOP}) { 
                   2069:         my $firstResource = $resource->map_start();
                   2070:         my $finishResource = $resource->map_finish();
                   2071:         return 
                   2072:             Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
                   2073:                                               $finishResource, $self->{FILTER},
                   2074:                                               $self->{ALREADY_SEEN}, 
                   2075:                                               $self->{CONDITION}, 0);
                   2076:         
                   2077:     }
                   2078: 
1.98      bowersj2 2079:     # Set up some bookkeeping information.
                   2080:     $self->{CURRENT_DEPTH} = 0;
                   2081:     $self->{MAX_DEPTH} = $maxDepth;
                   2082:     $self->{STACK} = [];
                   2083:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2084: 
                   2085:     for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
                   2086:         push @{$self->{STACK}}, [];
                   2087:     }
                   2088: 
1.107     bowersj2 2089:     # Prime the recursion w/ the first resource **5**
1.98      bowersj2 2090:     push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
                   2091:     $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
                   2092: 
                   2093:     bless ($self);
                   2094: 
                   2095:     return $self;
                   2096: }
                   2097: 
                   2098: sub next {
                   2099:     my $self = shift;
                   2100: 
                   2101:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
                   2102:         # grab the next from the recursive iterator 
                   2103:         my $next = $self->{RECURSIVE_ITERATOR}->next();
                   2104: 
                   2105:         # is it a begin or end map? If so, update the depth
                   2106:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
                   2107:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
                   2108: 
                   2109:         # Are we back at depth 0? If so, stop recursing
                   2110:         if ($self->{RECURSIVE_DEPTH} == 0) {
                   2111:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2112:         }
                   2113: 
                   2114:         return $next;
                   2115:     }
                   2116: 
                   2117:     if (defined($self->{FORCE_NEXT})) {
                   2118:         my $tmp = $self->{FORCE_NEXT};
                   2119:         $self->{FORCE_NEXT} = undef;
                   2120:         return $tmp;
                   2121:     }
                   2122: 
                   2123:     # Have we not yet begun? If not, return BEGIN_MAP and
                   2124:     # remember we've started.
                   2125:     if ( !$self->{STARTED} ) { 
                   2126:         $self->{STARTED} = 1;
                   2127:         return $self->BEGIN_MAP();
                   2128:     }
                   2129: 
                   2130:     # Here's the guts of the iterator.
                   2131:     
                   2132:     # Find the next resource, if any.
                   2133:     my $found = 0;
                   2134:     my $i = $self->{MAX_DEPTH};
                   2135:     my $newDepth;
                   2136:     my $here;
                   2137:     while ( $i >= 0 && !$found ) {
1.107     bowersj2 2138:         if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
                   2139:             $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98      bowersj2 2140:             $found = 1;
                   2141:             $newDepth = $i;
                   2142:         }
                   2143:         $i--;
                   2144:     }
                   2145: 
                   2146:     # If we still didn't find anything, we're done.
                   2147:     if ( !$found ) {
                   2148:         # We need to get back down to the correct branch depth
                   2149:         if ( $self->{CURRENT_DEPTH} > 0 ) {
                   2150:             $self->{CURRENT_DEPTH}--;
                   2151:             return END_BRANCH();
                   2152:         } else {
                   2153:             return END_MAP();
                   2154:         }
                   2155:     }
                   2156: 
1.104     bowersj2 2157:     # If this is not a resource, it must be an END_BRANCH marker we want
                   2158:     # to return directly.
1.107     bowersj2 2159:     if (!ref($here)) { # **8**
1.104     bowersj2 2160:         if ($here == END_BRANCH()) { # paranoia, in case of later extension
                   2161:             $self->{CURRENT_DEPTH}--;
                   2162:             return $here;
                   2163:         }
                   2164:     }
                   2165: 
                   2166:     # Otherwise, it is a resource and it's safe to store in $self->{HERE}
                   2167:     $self->{HERE} = $here;
                   2168: 
1.98      bowersj2 2169:     # Get to the right level
                   2170:     if ( $self->{CURRENT_DEPTH} > $newDepth ) {
                   2171:         push @{$self->{STACK}->[$newDepth]}, $here;
                   2172:         $self->{CURRENT_DEPTH}--;
                   2173:         return END_BRANCH();
                   2174:     }
                   2175:     if ( $self->{CURRENT_DEPTH} < $newDepth) {
                   2176:         push @{$self->{STACK}->[$newDepth]}, $here;
                   2177:         $self->{CURRENT_DEPTH}++;
                   2178:         return BEGIN_BRANCH();
                   2179:     }
                   2180: 
                   2181:     # If we made it here, we have the next resource, and we're at the
                   2182:     # right branch level. So let's examine the resource for where
                   2183:     # we can get to from here.
                   2184: 
                   2185:     # So we need to look at all the resources we can get to from here,
                   2186:     # categorize them if we haven't seen them, remember if we have a new
                   2187:     my $nextUnfiltered = $here->getNext();
1.104     bowersj2 2188:     my $maxDepthAdded = -1;
                   2189:     
1.98      bowersj2 2190:     for (@$nextUnfiltered) {
                   2191:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104     bowersj2 2192:             my $depth = $_->{DATA}->{DISPLAY_DEPTH};
                   2193:             push @{$self->{STACK}->[$depth]}, $_;
1.98      bowersj2 2194:             $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104     bowersj2 2195:             if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98      bowersj2 2196:         }
                   2197:     }
1.104     bowersj2 2198: 
                   2199:     # Is this the end of a branch? If so, all of the resources examined above
                   2200:     # led to lower levels then the one we are currently at, so we push a END_BRANCH
                   2201:     # marker onto the stack so we don't forget.
                   2202:     # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
                   2203:     # BC branch and gets to C, it will see F as the only next resource, but it's
                   2204:     # one level lower. Thus, this is the end of the branch, since there are no
                   2205:     # more resources added to this level or above.
1.111     bowersj2 2206:     # We don't do this if the examined resource is the finish resource,
                   2207:     # because the condition given above is true, but the "END_MAP" will
                   2208:     # take care of things and we should already be at depth 0.
1.104     bowersj2 2209:     my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111     bowersj2 2210:     if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104     bowersj2 2211:         push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
                   2212:     }
                   2213: 
1.98      bowersj2 2214:     # That ends the main iterator logic. Now, do we want to recurse
                   2215:     # down this map (if this resource is a map)?
                   2216:     if ($self->{HERE}->is_map() &&
                   2217:         (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
                   2218:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
                   2219:         my $firstResource = $self->{HERE}->map_start();
                   2220:         my $finishResource = $self->{HERE}->map_finish();
                   2221: 
                   2222:         $self->{RECURSIVE_ITERATOR} = 
                   2223:             Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
                   2224:                                               $finishResource, $self->{FILTER},
                   2225:                                               $self->{ALREADY_SEEN}, $self->{CONDITION});
                   2226:     }
                   2227: 
1.116     bowersj2 2228:     # If this is a blank resource, don't actually return it.
1.117     bowersj2 2229:     # Should you ever find you need it, make sure to add an option to the code
                   2230:     #  that you can use; other things depend on this behavior.
1.138   ! bowersj2 2231:     my $browsePriv = $self->{HERE}->browsePriv();
        !          2232:     if (!$self->{HERE}->src() || 
        !          2233:         (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
1.116     bowersj2 2234:         return $self->next();
                   2235:     }
                   2236: 
1.98      bowersj2 2237:     return $self->{HERE};
                   2238: 
                   2239: }
                   2240: 
                   2241: =pod
                   2242: 
                   2243: 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).
                   2244: 
                   2245: =cut
                   2246: 
                   2247: sub getStack {
                   2248:     my $self=shift;
                   2249: 
                   2250:     my @stack;
                   2251: 
                   2252:     $self->populateStack(\@stack);
                   2253: 
                   2254:     return \@stack;
                   2255: }
                   2256: 
                   2257: # Private method: Calls the iterators recursively to populate the stack.
                   2258: sub populateStack {
                   2259:     my $self=shift;
                   2260:     my $stack = shift;
                   2261: 
                   2262:     push @$stack, $self->{HERE} if ($self->{HERE});
                   2263: 
                   2264:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
                   2265:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
                   2266:     }
1.94      bowersj2 2267: }
                   2268: 
                   2269: 1;
                   2270: 
                   2271: package Apache::lonnavmaps::DFSiterator;
                   2272: 
1.100     bowersj2 2273: # Not documented in the perldoc: This is a simple iterator that just walks
                   2274: #  through the nav map and presents the resources in a depth-first search
                   2275: #  fashion, ignorant of conditionals, randomized resources, etc. It presents
                   2276: #  BEGIN_MAP and END_MAP, but does not understand branches at all. It is
                   2277: #  useful for pre-processing of some kind, and is in fact used by the main
                   2278: #  iterator that way, but that's about it.
                   2279: # One could imagine merging this into the init routine of the main iterator,
                   2280: #  but this might as well be left seperate, since it is possible some other
                   2281: #  use might be found for it. - Jeremy
1.94      bowersj2 2282: 
1.117     bowersj2 2283: # Unlike the main iterator, this DOES return all resources, even blank ones.
                   2284: #  The main iterator needs them to correctly preprocess the map.
                   2285: 
1.94      bowersj2 2286: sub BEGIN_MAP { return 1; }    # begining of a new map
                   2287: sub END_MAP { return 2; }      # end of the map
                   2288: sub FORWARD { return 1; }      # go forward
                   2289: sub BACKWARD { return 2; }
                   2290: 
1.100     bowersj2 2291: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
                   2292: #         filter hash ref (or undef), already seen hash or undef, condition
                   2293: #         (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51      bowersj2 2294: sub new {
                   2295:     # magic invocation to create a class instance
                   2296:     my $proto = shift;
                   2297:     my $class = ref($proto) || $proto;
                   2298:     my $self = {};
                   2299: 
                   2300:     $self->{NAV_MAP} = shift;
                   2301:     return undef unless ($self->{NAV_MAP});
                   2302: 
                   2303:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   2304:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   2305: 
                   2306:     # If the given resources are just the ID of the resource, get the
                   2307:     # objects
                   2308:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   2309:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   2310:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   2311:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   2312: 
                   2313:     $self->{FILTER} = shift;
                   2314: 
                   2315:     # A hash, used as a set, of resource already seen
                   2316:     $self->{ALREADY_SEEN} = shift;
                   2317:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63      bowersj2 2318:     $self->{CONDITION} = shift;
1.89      bowersj2 2319:     $self->{DIRECTION} = shift || FORWARD();
1.51      bowersj2 2320: 
1.100     bowersj2 2321:     # Flag: Have we started yet?
1.51      bowersj2 2322:     $self->{STARTED} = 0;
                   2323: 
                   2324:     # Should we continue calling the recursive iterator, if any?
                   2325:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2326:     # The recursive iterator, if any
                   2327:     $self->{RECURSIVE_ITERATOR} = undef;
                   2328:     # Are we recursing on a map, or a branch?
                   2329:     $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
                   2330:     # And the count of how deep it is, so that this iterator can keep track of
                   2331:     # when to pick back up again.
                   2332:     $self->{RECURSIVE_DEPTH} = 0;
                   2333: 
                   2334:     # For keeping track of our branches, we maintain our own stack
1.100     bowersj2 2335:     $self->{STACK} = [];
1.51      bowersj2 2336: 
                   2337:     # Start with the first resource
1.89      bowersj2 2338:     if ($self->{DIRECTION} == FORWARD) {
1.100     bowersj2 2339:         push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89      bowersj2 2340:     } else {
1.100     bowersj2 2341:         push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89      bowersj2 2342:     }
1.51      bowersj2 2343: 
                   2344:     bless($self);
                   2345:     return $self;
                   2346: }
                   2347: 
                   2348: sub next {
                   2349:     my $self = shift;
                   2350:     
                   2351:     # Are we using a recursive iterator? If so, pull from that and
                   2352:     # watch the depth; we want to resume our level at the correct time.
1.98      bowersj2 2353:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51      bowersj2 2354:         # grab the next from the recursive iterator
                   2355:         my $next = $self->{RECURSIVE_ITERATOR}->next();
                   2356:         
                   2357:         # is it a begin or end map? Update depth if so
                   2358:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
                   2359:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
                   2360: 
                   2361:         # Are we back at depth 0? If so, stop recursing.
                   2362:         if ($self->{RECURSIVE_DEPTH} == 0) {
                   2363:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2364:         }
                   2365:         
                   2366:         return $next;
                   2367:     }
                   2368: 
                   2369:     # Is there a current resource to grab? If not, then return
1.100     bowersj2 2370:     # END_MAP, which will end the iterator.
                   2371:     if (scalar(@{$self->{STACK}}) == 0) {
                   2372:         return $self->END_MAP();
1.51      bowersj2 2373:     }
                   2374: 
                   2375:     # Have we not yet begun? If not, return BEGIN_MAP and 
                   2376:     # remember that we've started.
                   2377:     if ( !$self->{STARTED} ) {
                   2378:         $self->{STARTED} = 1;
                   2379:         return $self->BEGIN_MAP;
                   2380:     }
                   2381: 
                   2382:     # Get the next resource in the branch
1.100     bowersj2 2383:     $self->{HERE} = pop @{$self->{STACK}};
1.52      bowersj2 2384: 
1.100     bowersj2 2385:     # remember that we've seen this, so we don't return it again later
1.51      bowersj2 2386:     $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
                   2387:     
                   2388:     # Get the next possible resources
1.90      bowersj2 2389:     my $nextUnfiltered;
1.89      bowersj2 2390:     if ($self->{DIRECTION} == FORWARD()) {
1.90      bowersj2 2391:         $nextUnfiltered = $self->{HERE}->getNext();
1.89      bowersj2 2392:     } else {
1.90      bowersj2 2393:         $nextUnfiltered = $self->{HERE}->getPrevious();
1.89      bowersj2 2394:     }
1.51      bowersj2 2395:     my $next = [];
                   2396: 
                   2397:     # filter the next possibilities to remove things we've 
1.100     bowersj2 2398:     # already seen.
1.51      bowersj2 2399:     foreach (@$nextUnfiltered) {
1.52      bowersj2 2400:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
                   2401:             push @$next, $_;
                   2402:         }
1.51      bowersj2 2403:     }
                   2404: 
                   2405:     while (@$next) {
1.100     bowersj2 2406:         # copy the next possibilities over to the stack
                   2407:         push @{$self->{STACK}}, shift @$next;
1.51      bowersj2 2408:     }
                   2409: 
                   2410:     # If this is a map and we want to recurse down it... (not filtered out)
1.70      bowersj2 2411:     if ($self->{HERE}->is_map() && 
1.63      bowersj2 2412:          (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) { 
1.51      bowersj2 2413:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
                   2414:         my $firstResource = $self->{HERE}->map_start();
                   2415:         my $finishResource = $self->{HERE}->map_finish();
                   2416: 
                   2417:         $self->{RECURSIVE_ITERATOR} =
1.94      bowersj2 2418:           Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource, 
1.63      bowersj2 2419:                      $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91      bowersj2 2420:                                              $self->{CONDITION}, $self->{DIRECTION});
1.51      bowersj2 2421:     }
                   2422: 
                   2423:     return $self->{HERE};
                   2424: }
                   2425: 
1.1       www      2426: 1;
1.2       www      2427: 
1.51      bowersj2 2428: package Apache::lonnavmaps::resource;
                   2429: 
                   2430: use Apache::lonnet;
                   2431: 
                   2432: =pod
                   2433: 
                   2434: =head1 Object: resource
                   2435: 
                   2436: 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.
                   2437: 
                   2438: A resource only makes sense in the context of a navmap, as some of the data is stored in the navmap object.
                   2439: 
                   2440: You will probably never need to instantiate this object directly. Use Apache::lonnavmap::navmap, and use the "start" method to obtain the starting resource.
                   2441: 
                   2442: =head2 Public Members
                   2443: 
                   2444: 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.
                   2445: 
                   2446: =head2 Methods
                   2447: 
                   2448: =over 4
                   2449: 
1.70      bowersj2 2450: =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.
                   2451: 
                   2452: =back
1.51      bowersj2 2453: 
                   2454: =cut
                   2455: 
                   2456: sub new {
                   2457:     # magic invocation to create a class instance
                   2458:     my $proto = shift;
                   2459:     my $class = ref($proto) || $proto;
                   2460:     my $self = {};
                   2461: 
                   2462:     $self->{NAV_MAP} = shift;
                   2463:     $self->{ID} = shift;
                   2464: 
                   2465:     # Store this new resource in the parent nav map's cache.
                   2466:     $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66      bowersj2 2467:     $self->{RESOURCE_ERROR} = 0;
1.51      bowersj2 2468: 
                   2469:     # A hash that can be used by two-pass algorithms to store data
                   2470:     # about this resource in. Not used by the resource object
                   2471:     # directly.
                   2472:     $self->{DATA} = {};
                   2473:    
                   2474:     bless($self);
                   2475:     
                   2476:     return $self;
                   2477: }
                   2478: 
1.70      bowersj2 2479: # private function: simplify the NAV_HASH lookups we keep doing
                   2480: # pass the name, and to automatically append my ID, pass a true val on the
                   2481: # second param
                   2482: sub navHash {
                   2483:     my $self = shift;
                   2484:     my $param = shift;
                   2485:     my $id = shift;
1.115     bowersj2 2486:     return $self->{NAV_MAP}->navhash($param . ($id?$self->{ID}:""));
1.70      bowersj2 2487: }
                   2488: 
1.51      bowersj2 2489: =pod
                   2490: 
1.70      bowersj2 2491: B<Metadata Retreival>
                   2492: 
1.101     bowersj2 2493: 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 2494: 
                   2495: =over 4
                   2496: 
1.106     bowersj2 2497: =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").
                   2498: 
1.70      bowersj2 2499: =item * B<ext>: Returns true if the resource is external.
                   2500: 
                   2501: =item * B<goesto>: Returns the "goesto" value from the compiled nav map. (It is likely you want to use B<getNext> instead.)
                   2502: 
                   2503: =item * B<kind>: Returns the kind of the resource from the compiled nav map.
                   2504: 
1.101     bowersj2 2505: =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 2506: 
1.70      bowersj2 2507: =item * B<randompick>: Returns true for a map if the randompick feature is being used on the map. (?)
                   2508: 
                   2509: =item * B<src>: Returns the source for the resource.
                   2510: 
                   2511: =item * B<symb>: Returns the symb for the resource.
                   2512: 
                   2513: =item * B<title>: Returns the title of the resource.
                   2514: 
                   2515: =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 2516: 
                   2517: =back
                   2518: 
                   2519: =cut
                   2520: 
                   2521: # These info functions can be used directly, as they don't return
                   2522: # resource information.
1.85      bowersj2 2523: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70      bowersj2 2524: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85      bowersj2 2525: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51      bowersj2 2526: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
                   2527: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68      bowersj2 2528: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
                   2529: sub randompick { 
                   2530:     my $self = shift;
                   2531:     return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
                   2532:                                                '.0.parameter_randompick'};
                   2533: }
1.51      bowersj2 2534: sub src { 
                   2535:     my $self=shift;
                   2536:     return $self->navHash("src_", 1);
                   2537: }
                   2538: sub symb {
                   2539:     my $self=shift;
                   2540:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
                   2541:     my $symbSrc = &Apache::lonnet::declutter($self->src());
                   2542:     return &Apache::lonnet::declutter(
                   2543:          $self->navHash('map_id_'.$first)) 
                   2544:         . '___' . $second . '___' . $symbSrc;
                   2545: }
1.70      bowersj2 2546: sub title { my $self=shift; return $self->navHash("title_", 1); }
                   2547: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106     bowersj2 2548: sub compTitle {
                   2549:     my $self = shift;
                   2550:     my $title = $self->title();
                   2551:     if (!$title) {
                   2552:         $title = $self->src();
                   2553:         $title = substr($title, rindex($title, '/') + 1);
                   2554:     }
                   2555:     return $title;
                   2556: }
1.70      bowersj2 2557: =pod
                   2558: 
                   2559: B<Predicate Testing the Resource>
                   2560: 
                   2561: These methods are shortcuts to deciding if a given resource has a given property.
                   2562: 
                   2563: =over 4
                   2564: 
                   2565: =item * B<is_map>: Returns true if the resource is a map type.
                   2566: 
                   2567: =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.)
                   2568: 
                   2569: =item * B<is_page>: Returns true if the resource is a page.
                   2570: 
                   2571: =item * B<is_problem>: Returns true if the resource is a problem.
                   2572: 
1.101     bowersj2 2573: =item * B<is_sequence>: Returns true if the resource is a sequence.
1.70      bowersj2 2574: 
                   2575: =back
                   2576: 
                   2577: =cut
                   2578: 
                   2579: 
                   2580: sub is_html {
1.51      bowersj2 2581:     my $self=shift;
                   2582:     my $src = $self->src();
1.70      bowersj2 2583:     return ($src =~ /html$/);
1.51      bowersj2 2584: }
1.70      bowersj2 2585: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
                   2586: sub is_page {
1.51      bowersj2 2587:     my $self=shift;
                   2588:     my $src = $self->src();
1.70      bowersj2 2589:     return ($src =~ /page$/);
1.51      bowersj2 2590: }
1.70      bowersj2 2591: sub is_problem {
1.51      bowersj2 2592:     my $self=shift;
                   2593:     my $src = $self->src();
1.70      bowersj2 2594:     return ($src =~ /problem$/);
1.51      bowersj2 2595: }
1.70      bowersj2 2596: sub is_sequence {
1.51      bowersj2 2597:     my $self=shift;
                   2598:     my $src = $self->src();
1.70      bowersj2 2599:     return ($src =~ /sequence$/);
1.51      bowersj2 2600: }
                   2601: 
1.101     bowersj2 2602: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51      bowersj2 2603: sub parmval {
                   2604:     my $self = shift;
                   2605:     my $what = shift;
                   2606:     my $part = shift || "0";
                   2607:     return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
                   2608: }
                   2609: 
1.70      bowersj2 2610: =pod
                   2611: 
                   2612: B<Map Methods>
                   2613: 
                   2614: These methods are useful for getting information about the map properties of the resource, if the resource is a map (B<is_map>).
                   2615: 
                   2616: =over 4
                   2617: 
                   2618: =item * B<map_finish>: Returns a reference to a resource object corresponding to the finish resource of the map.
                   2619: 
                   2620: =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.
                   2621: 
                   2622: =item * B<map_start>: Returns a reference to a resource object corresponding to the start resource of the map.
                   2623: 
                   2624: =item * B<map_type>: Returns a string with the type of the map in it.
                   2625: 
                   2626: =back
1.51      bowersj2 2627: 
1.70      bowersj2 2628: =cut
1.51      bowersj2 2629: 
                   2630: sub map_finish {
                   2631:     my $self = shift;
                   2632:     my $src = $self->src();
                   2633:     my $res = $self->navHash("map_finish_$src", 0);
                   2634:     $res = $self->{NAV_MAP}->getById($res);
                   2635:     return $res;
                   2636: }
1.70      bowersj2 2637: sub map_pc {
                   2638:     my $self = shift;
                   2639:     my $src = $self->src();
                   2640:     return $self->navHash("map_pc_$src", 0);
                   2641: }
1.51      bowersj2 2642: sub map_start {
                   2643:     my $self = shift;
                   2644:     my $src = $self->src();
                   2645:     my $res = $self->navHash("map_start_$src", 0);
                   2646:     $res = $self->{NAV_MAP}->getById($res);
                   2647:     return $res;
                   2648: }
                   2649: sub map_type {
                   2650:     my $self = shift;
                   2651:     my $pc = $self->map_pc();
                   2652:     return $self->navHash("map_type_$pc", 0);
                   2653: }
                   2654: 
                   2655: 
                   2656: 
                   2657: #####
                   2658: # Property queries
                   2659: #####
                   2660: 
                   2661: # These functions will be responsible for returning the CORRECT
                   2662: # VALUE for the parameter, no matter what. So while they may look
                   2663: # like direct calls to parmval, they can be more then that.
                   2664: # So, for instance, the duedate function should use the "duedatetype"
                   2665: # information, rather then the resource object user.
                   2666: 
                   2667: =pod
                   2668: 
                   2669: =head2 Resource Parameters
                   2670: 
1.70      bowersj2 2671: 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 2672: 
                   2673: 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.)
                   2674: 
                   2675: =over 4
                   2676: 
                   2677: =item * B<acc>: Get the Client IP/Name Access Control information.
                   2678: 
                   2679: =item * B<answerdate>: Get the answer-reveal date for the problem.
                   2680: 
                   2681: =item * B<duedate>: Get the due date for the problem.
                   2682: 
                   2683: =item * B<tries>: Get the number of tries the student has used on the problem.
                   2684: 
                   2685: =item * B<maxtries>: Get the number of max tries allowed.
                   2686: 
                   2687: =item * B<opendate>: Get the open date for the problem.
                   2688: 
                   2689: =item * B<sig>: Get the significant figures setting.
                   2690: 
                   2691: =item * B<tol>: Get the tolerance for the problem.
                   2692: 
1.70      bowersj2 2693: =item * B<tries>: Get the number of tries the user has already used on the problem.
                   2694: 
1.51      bowersj2 2695: =item * B<type>: Get the question type for the problem.
                   2696: 
                   2697: =item * B<weight>: Get the weight for the problem.
                   2698: 
                   2699: =back
                   2700: 
                   2701: =cut
                   2702: 
                   2703: sub acc {
                   2704:     (my $self, my $part) = @_;
                   2705:     return $self->parmval("acc", $part);
                   2706: }
                   2707: sub answerdate {
                   2708:     (my $self, my $part) = @_;
                   2709:     # Handle intervals
                   2710:     if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
                   2711:         return $self->duedate($part) + 
                   2712:             $self->parmval("answerdate", $part);
                   2713:     }
                   2714:     return $self->parmval("answerdate", $part);
1.106     bowersj2 2715: }
1.108     bowersj2 2716: sub awarded { my $self = shift; return $self->queryRestoreHash('awarded', shift); }
1.51      bowersj2 2717: sub duedate {
                   2718:     (my $self, my $part) = @_;
                   2719:     return $self->parmval("duedate", $part);
                   2720: }
                   2721: sub maxtries {
                   2722:     (my $self, my $part) = @_;
                   2723:     return $self->parmval("maxtries", $part);
                   2724: }
                   2725: sub opendate {
                   2726:     (my $self, my $part) = @_;
                   2727:     if ($self->parmval("opendate.type", $part) eq 'date_interval') {
                   2728:         return $self->duedate($part) -
                   2729:             $self->parmval("opendate", $part);
                   2730:     }
                   2731:     return $self->parmval("opendate");
                   2732: }
                   2733: sub sig {
                   2734:     (my $self, my $part) = @_;
                   2735:     return $self->parmval("sig", $part);
                   2736: }
                   2737: sub tol {
                   2738:     (my $self, my $part) = @_;
                   2739:     return $self->parmval("tol", $part);
                   2740: }
1.108     bowersj2 2741: sub tries { 
                   2742:     my $self = shift; 
                   2743:     my $tries = $self->queryRestoreHash('tries', shift);
                   2744:     if (!defined($tries)) { return '0';}
1.51      bowersj2 2745:     return $tries;
                   2746: }
1.70      bowersj2 2747: sub type {
                   2748:     (my $self, my $part) = @_;
                   2749:     return $self->parmval("type", $part);
                   2750: }
1.109     bowersj2 2751: sub weight { 
                   2752:     my $self = shift; my $part = shift;
1.70      bowersj2 2753:     return $self->parmval("weight", $part);
                   2754: }
1.51      bowersj2 2755: 
                   2756: # Multiple things need this
                   2757: sub getReturnHash {
                   2758:     my $self = shift;
                   2759:     
                   2760:     if (!defined($self->{RETURN_HASH})) {
1.84      bowersj2 2761:         my %tmpHash  = &Apache::lonnet::restore($self->symb());
1.51      bowersj2 2762:         $self->{RETURN_HASH} = \%tmpHash;
                   2763:     }
                   2764: }       
                   2765: 
                   2766: ######
                   2767: # Status queries
                   2768: ######
                   2769: 
                   2770: # These methods query the status of problems.
                   2771: 
                   2772: # If we need to count parts, this function determines the number of
                   2773: # parts from the metadata. When called, it returns a reference to a list
                   2774: # of strings corresponding to the parts. (Thus, using it in a scalar context
                   2775: # tells you how many parts you have in the problem:
                   2776: # $partcount = scalar($resource->countParts());
                   2777: # Don't use $self->{PARTS} directly because you don't know if it's been
                   2778: # computed yet.
                   2779: 
                   2780: =pod
                   2781: 
                   2782: =head2 Resource misc
                   2783: 
                   2784: Misc. functions for the resource.
                   2785: 
                   2786: =over 4
                   2787: 
1.59      bowersj2 2788: =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.
                   2789: 
                   2790: =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:
                   2791: 
                   2792:  for (split(/\,/, $res->getFeedback())) {
                   2793:     my $link = &Apache::lonnet::escape($_);
                   2794:     ...
                   2795: 
                   2796: and use the link as appropriate.
                   2797: 
                   2798: =cut
                   2799: 
                   2800: sub hasDiscussion {
                   2801:     my $self = shift;
                   2802:     return $self->{NAV_MAP}->hasDiscussion($self->symb());
                   2803: }
                   2804: 
                   2805: sub getFeedback {
                   2806:     my $self = shift;
1.124     bowersj2 2807:     my $source = $self->src();
1.125     bowersj2 2808:     if ($source =~ /^\/res\//) { $source = substr $source, 5; }
1.124     bowersj2 2809:     return $self->{NAV_MAP}->getFeedback($source);
                   2810: }
                   2811: 
                   2812: sub getErrors {
                   2813:     my $self = shift;
                   2814:     my $source = $self->src();
                   2815:     if ($source =~ /^\/res\//) { $source = substr $source, 5; }
                   2816:     return $self->{NAV_MAP}->getErrors($source);
1.59      bowersj2 2817: }
                   2818: 
                   2819: =pod
                   2820: 
1.70      bowersj2 2821: =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 2822: 
1.70      bowersj2 2823: =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 2824: 
                   2825: =back
                   2826: 
                   2827: =cut
                   2828: 
                   2829: sub parts {
                   2830:     my $self = shift;
                   2831: 
1.67      bowersj2 2832:     if ($self->ext) { return ['0']; }
                   2833: 
1.51      bowersj2 2834:     $self->extractParts();
                   2835:     return $self->{PARTS};
                   2836: }
                   2837: 
                   2838: sub countParts {
                   2839:     my $self = shift;
                   2840:     
                   2841:     my $parts = $self->parts();
1.66      bowersj2 2842: 
                   2843:     if ($self->{RESOURCE_ERROR}) {
                   2844:         return 0;
                   2845:     }
                   2846: 
                   2847:     if (scalar(@{$parts}) < 2) { return 1;}
1.51      bowersj2 2848: 
                   2849:     return scalar(@{$parts}) - 1;
                   2850: }
                   2851: 
                   2852: # Private function: Extracts the parts information and saves it
                   2853: sub extractParts { 
                   2854:     my $self = shift;
                   2855:     
                   2856:     return if ($self->{PARTS});
1.67      bowersj2 2857:     return if ($self->ext);
1.51      bowersj2 2858: 
                   2859:     $self->{PARTS} = [];
                   2860: 
1.82      bowersj2 2861:     # Retrieve part count, if this is a problem
                   2862:     if ($self->is_problem()) {
                   2863:         my $metadata = &Apache::lonnet::metadata($self->src(), 'allpossiblekeys');
                   2864:         if (!$metadata) {
                   2865:             $self->{RESOURCE_ERROR} = 1;
                   2866:             $self->{PARTS} = [];
                   2867:             return;
                   2868:         }
                   2869:         
                   2870:         foreach (split(/\,/,$metadata)) {
                   2871:             if ($_ =~ /^parameter\_(.*)\_opendate$/) {
                   2872:                 push @{$self->{PARTS}}, $1;
                   2873:             }
1.51      bowersj2 2874:         }
1.82      bowersj2 2875:         
                   2876:         
                   2877:         # Is this possible to do in one line? - Jeremy
                   2878:         my @sortedParts = sort @{$self->{PARTS}};
                   2879:         $self->{PARTS} = \@sortedParts;
1.51      bowersj2 2880:     }
                   2881: 
                   2882:     return;
                   2883: }
                   2884: 
                   2885: =pod
                   2886: 
                   2887: =head2 Resource Status
                   2888: 
1.101     bowersj2 2889: Problem resources have status information, reflecting their various dates and completion statuses. 
1.51      bowersj2 2890: 
                   2891: There are two aspects to the status: the date-related information and the completion information.
                   2892: 
                   2893: Idiomatic usage of these two methods would probably look something like
                   2894: 
                   2895:  foreach ($resource->parts()) {
                   2896:     my $dateStatus = $resource->getDateStatus($_);
                   2897:     my $completionStatus = $resource->getCompletionStatus($_);
                   2898: 
1.70      bowersj2 2899:     or
                   2900: 
                   2901:     my $status = $resource->status($_);
                   2902: 
1.51      bowersj2 2903:     ... use it here ...
                   2904:  }
                   2905: 
1.101     bowersj2 2906: 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.
                   2907: 
                   2908: The symbolic constants shown below can be accessed through the resource object: $res->OPEN.
                   2909: 
1.51      bowersj2 2910: =over 4
                   2911: 
1.70      bowersj2 2912: =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 2913: 
                   2914: =back
                   2915: 
                   2916: B<Date Codes>
                   2917: 
                   2918: =over 4
                   2919: 
                   2920: =item * B<OPEN_LATER>: The problem will be opened later.
                   2921: 
                   2922: =item * B<OPEN>: Open and not yet due.
                   2923: 
1.59      bowersj2 2924: 
1.54      bowersj2 2925: =item * B<PAST_DUE_ANSWER_LATER>: The due date has passed, but the answer date has not yet arrived.
                   2926: 
                   2927: =item * B<PAST_DUE_NO_ANSWER>: The due date has passed and there is no answer opening date set.
1.51      bowersj2 2928: 
                   2929: =item * B<ANSWER_OPEN>: The answer date is here.
                   2930: 
                   2931: =item * B<NETWORK_FAILURE>: The information is unknown due to network failure.
                   2932: 
                   2933: =back
                   2934: 
                   2935: =cut
                   2936: 
                   2937: # Apparently the compiler optimizes these into constants automatically
1.54      bowersj2 2938: sub OPEN_LATER             { return 0; }
                   2939: sub OPEN                   { return 1; }
                   2940: sub PAST_DUE_NO_ANSWER     { return 2; }
                   2941: sub PAST_DUE_ANSWER_LATER  { return 3; }
                   2942: sub ANSWER_OPEN            { return 4; }
                   2943: sub NOTHING_SET            { return 5; } 
                   2944: sub NETWORK_FAILURE        { return 100; }
                   2945: 
                   2946: # getDateStatus gets the date status for a given problem part. 
                   2947: # Because answer date, due date, and open date are fully independent
                   2948: # (i.e., it is perfectly possible to *only* have an answer date), 
                   2949: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
                   2950: # (past, future, none given). This function handles this with a decision
                   2951: # tree. Read the comments to follow the decision tree.
1.51      bowersj2 2952: 
                   2953: sub getDateStatus {
                   2954:     my $self = shift;
                   2955:     my $part = shift;
                   2956:     $part = "0" if (!defined($part));
1.54      bowersj2 2957: 
                   2958:     # Always return network failure if there was one.
1.51      bowersj2 2959:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   2960: 
                   2961:     my $now = time();
                   2962: 
1.53      bowersj2 2963:     my $open = $self->opendate($part);
                   2964:     my $due = $self->duedate($part);
                   2965:     my $answer = $self->answerdate($part);
                   2966: 
                   2967:     if (!$open && !$due && !$answer) {
                   2968:         # no data on the problem at all
                   2969:         # should this be the same as "open later"? think multipart.
                   2970:         return $self->NOTHING_SET;
                   2971:     }
1.59      bowersj2 2972:     if (!$open || $now < $open) {return $self->OPEN_LATER}
                   2973:     if (!$due || $now < $due) {return $self->OPEN}
                   2974:     if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
                   2975:     if ($answer) { return $self->ANSWER_OPEN; }
1.54      bowersj2 2976:     return PAST_DUE_NO_ANSWER;
1.51      bowersj2 2977: }
                   2978: 
                   2979: =pod
                   2980: 
                   2981: B<>
                   2982: 
                   2983: =over 4
                   2984: 
                   2985: =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:
                   2986: 
                   2987: =back 
                   2988: 
                   2989: B<Completion Codes>
                   2990: 
                   2991: =over 4
                   2992: 
                   2993: =item * B<NOT_ATTEMPTED>: Has not been attempted at all.
                   2994: 
                   2995: =item * B<INCORRECT>: Attempted, but wrong by student.
                   2996: 
                   2997: =item * B<INCORRECT_BY_OVERRIDE>: Attempted, but wrong by instructor override.
                   2998: 
                   2999: =item * B<CORRECT>: Correct or correct by instructor.
                   3000: 
                   3001: =item * B<CORRECT_BY_OVERRIDE>: Correct by instructor override.
                   3002: 
                   3003: =item * B<EXCUSED>: Excused. Not yet implemented.
                   3004: 
                   3005: =item * B<NETWORK_FAILURE>: Information not available due to network failure.
                   3006: 
1.69      bowersj2 3007: =item * B<ATTEMPTED>: Attempted, and not yet graded.
                   3008: 
1.51      bowersj2 3009: =back
                   3010: 
                   3011: =cut
                   3012: 
1.53      bowersj2 3013: sub NOT_ATTEMPTED         { return 10; }
                   3014: sub INCORRECT             { return 11; }
                   3015: sub INCORRECT_BY_OVERRIDE { return 12; }
                   3016: sub CORRECT               { return 13; }
                   3017: sub CORRECT_BY_OVERRIDE   { return 14; }
                   3018: sub EXCUSED               { return 15; }
1.69      bowersj2 3019: sub ATTEMPTED             { return 16; }
1.51      bowersj2 3020: 
                   3021: sub getCompletionStatus {
                   3022:     my $self = shift;
                   3023:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3024: 
1.108     bowersj2 3025:     my $status = $self->queryRestoreHash('solved', shift);
1.51      bowersj2 3026: 
                   3027:     # Left as seperate if statements in case we ever do more with this
                   3028:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
                   3029:     if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
                   3030:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
                   3031:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
                   3032:     if ($status eq 'excused') {return $self->EXCUSED; }
1.69      bowersj2 3033:     if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51      bowersj2 3034:     return $self->NOT_ATTEMPTED;
1.108     bowersj2 3035: }
                   3036: 
                   3037: sub queryRestoreHash {
                   3038:     my $self = shift;
                   3039:     my $hashentry = shift;
                   3040:     my $part = shift;
                   3041:     $part = "0" if (!defined($part));
                   3042:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3043: 
                   3044:     $self->getReturnHash();
                   3045: 
                   3046:     return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51      bowersj2 3047: }
                   3048: 
                   3049: =pod
                   3050: 
                   3051: B<Composite Status>
                   3052: 
1.101     bowersj2 3053: 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 3054: 
                   3055: =over 4
                   3056: 
1.70      bowersj2 3057: =item * B<NETWORK_FAILURE>: The network has failed and the information is not available.
1.51      bowersj2 3058: 
1.70      bowersj2 3059: =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 3060: 
1.70      bowersj2 3061: =item * B<CORRECT>: For any reason at all, the part is considered correct.
1.51      bowersj2 3062: 
1.70      bowersj2 3063: =item * B<EXCUSED>: For any reason at all, the problem is excused.
1.51      bowersj2 3064: 
1.70      bowersj2 3065: =item * B<PAST_DUE_NO_ANSWER>: The problem is past due, not considered correct, and no answer date is set.
1.54      bowersj2 3066: 
1.70      bowersj2 3067: =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 3068: 
1.70      bowersj2 3069: =item * B<ANSWER_OPEN>: The problem is past due, not correct, and the answer is now available.
1.51      bowersj2 3070: 
1.70      bowersj2 3071: =item * B<OPEN_LATER>: The problem is not yet open.
1.51      bowersj2 3072: 
1.70      bowersj2 3073: =item * B<TRIES_LEFT>: The problem is open, has been tried, is not correct, but there are tries left.
1.51      bowersj2 3074: 
1.70      bowersj2 3075: =item * B<INCORRECT>: The problem is open, and all tries have been used without getting the correct answer.
1.51      bowersj2 3076: 
1.70      bowersj2 3077: =item * B<OPEN>: The item is open and not yet tried.
1.51      bowersj2 3078: 
1.70      bowersj2 3079: =item * B<ATTEMPTED>: The problem has been attempted.
1.69      bowersj2 3080: 
1.51      bowersj2 3081: =back
                   3082: 
                   3083: =cut
                   3084: 
                   3085: sub TRIES_LEFT { return 10; }
                   3086: 
                   3087: sub status {
                   3088:     my $self = shift;
                   3089:     my $part = shift;
                   3090:     if (!defined($part)) { $part = "0"; }
                   3091:     my $completionStatus = $self->getCompletionStatus($part);
                   3092:     my $dateStatus = $self->getDateStatus($part);
                   3093: 
                   3094:     # What we have is a two-dimensional matrix with 4 entries on one
                   3095:     # dimension and 5 entries on the other, which we want to colorize,
1.54      bowersj2 3096:     # plus network failure and "no date data at all".
1.51      bowersj2 3097: 
1.53      bowersj2 3098:     if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51      bowersj2 3099: 
                   3100:     # There are a few whole rows we can dispose of:
1.53      bowersj2 3101:     if ($completionStatus == CORRECT ||
                   3102:         $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69      bowersj2 3103:         return CORRECT; 
                   3104:     }
                   3105: 
                   3106:     if ($completionStatus == ATTEMPTED) {
                   3107:         return ATTEMPTED;
1.53      bowersj2 3108:     }
                   3109: 
                   3110:     # If it's EXCUSED, then return that no matter what
                   3111:     if ($completionStatus == EXCUSED) {
                   3112:         return EXCUSED; 
1.51      bowersj2 3113:     }
                   3114: 
1.53      bowersj2 3115:     if ($dateStatus == NOTHING_SET) {
                   3116:         return NOTHING_SET;
1.51      bowersj2 3117:     }
                   3118: 
1.69      bowersj2 3119:     # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
                   3120:     # by 4 matrix (date statuses).
1.51      bowersj2 3121: 
1.54      bowersj2 3122:     if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59      bowersj2 3123:         $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54      bowersj2 3124:         return $dateStatus; 
1.51      bowersj2 3125:     }
                   3126: 
1.53      bowersj2 3127:     if ($dateStatus == ANSWER_OPEN) {
                   3128:         return ANSWER_OPEN;
1.51      bowersj2 3129:     }
                   3130: 
                   3131:     # Now: (incorrect, incorrect_override, not_attempted) x 
                   3132:     # (open_later), (open)
                   3133:     
1.53      bowersj2 3134:     if ($dateStatus == OPEN_LATER) {
                   3135:         return OPEN_LATER;
1.51      bowersj2 3136:     }
                   3137: 
                   3138:     # If it's WRONG...
1.53      bowersj2 3139:     if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51      bowersj2 3140:         # and there are TRIES LEFT:
1.79      bowersj2 3141:         if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53      bowersj2 3142:             return TRIES_LEFT;
1.51      bowersj2 3143:         }
1.53      bowersj2 3144:         return INCORRECT; # otherwise, return orange; student can't fix this
1.51      bowersj2 3145:     }
                   3146: 
                   3147:     # Otherwise, it's untried and open
1.53      bowersj2 3148:     return OPEN; 
1.51      bowersj2 3149: }
                   3150: 
                   3151: =pod
                   3152: 
                   3153: =head2 Resource/Nav Map Navigation
                   3154: 
                   3155: =over 4
                   3156: 
1.101     bowersj2 3157: =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 3158: 
1.101     bowersj2 3159: =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 3160: 
                   3161: =cut
                   3162: 
                   3163: sub getNext {
                   3164:     my $self = shift;
                   3165:     my @branches;
                   3166:     my $to = $self->to();
1.85      bowersj2 3167:     foreach my $branch ( split(/,/, $to) ) {
1.51      bowersj2 3168:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3169:         my $next = $choice->goesto();
                   3170:         $next = $self->{NAV_MAP}->getById($next);
                   3171: 
1.131     bowersj2 3172:         push @branches, $next;
1.85      bowersj2 3173:     }
                   3174:     return \@branches;
                   3175: }
                   3176: 
                   3177: sub getPrevious {
                   3178:     my $self = shift;
                   3179:     my @branches;
                   3180:     my $from = $self->from();
                   3181:     foreach my $branch ( split /,/, $from) {
                   3182:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3183:         my $prev = $choice->comesfrom();
                   3184:         $prev = $self->{NAV_MAP}->getById($prev);
                   3185: 
1.131     bowersj2 3186:         push @branches, $prev;
1.51      bowersj2 3187:     }
                   3188:     return \@branches;
1.131     bowersj2 3189: }
                   3190: 
                   3191: sub browsePriv {
                   3192:     my $self = shift;
                   3193:     if (defined($self->{BROWSE_PRIV})) {
                   3194:         return $self->{BROWSE_PRIV};
                   3195:     }
                   3196: 
                   3197:     $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51      bowersj2 3198: }
                   3199: 
                   3200: =pod
1.2       www      3201: 
1.51      bowersj2 3202: =back
1.2       www      3203: 
1.51      bowersj2 3204: =cut
1.2       www      3205: 
1.51      bowersj2 3206: 1;
1.2       www      3207: 
1.51      bowersj2 3208: __END__
1.2       www      3209: 
                   3210: 

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