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

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

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