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

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

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