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

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

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