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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # Navigate Maps Handler
1.1       www         3: #
1.187     bowersj2    4: # $Id: lonnavmaps.pm,v 1.186 2003/05/12 19:22:39 bowersj2 Exp $
1.20      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.2       www        28: # (Page Handler
1.1       www        29: #
1.2       www        30: # (TeX Content Handler
1.1       www        31: #
1.2       www        32: # 05/29/00,05/30 Gerd Kortemeyer)
                     33: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
                     34: # 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        35: #
1.17      www        36: # 3/1/1,6/1,17/1,29/1,30/1,2/8,9/21,9/24,9/25 Gerd Kortemeyer
1.21      www        37: # YEAR=2002
                     38: # 1/1 Gerd Kortemeyer
1.105     bowersj2   39: # Oct-Nov Jeremy Bowers
1.153     bowersj2   40: # YEAR=2003
                     41: # Jeremy Bowers ... lots of days
1.2       www        42: 
1.1       www        43: package Apache::lonnavmaps;
                     44: 
                     45: use strict;
1.2       www        46: use Apache::Constants qw(:common :http);
1.18      albertel   47: use Apache::loncommon();
1.150     www        48: use Apache::lonmenu();
1.73      bowersj2   49: use POSIX qw (floor strftime);
1.2       www        50: 
1.133     bowersj2   51: # symbolic constants
                     52: sub SYMB { return 1; }
                     53: sub URL { return 2; }
                     54: sub NOTHING { return 3; }
                     55: 
                     56: # Some data
                     57: 
1.140     bowersj2   58: my $resObj = "Apache::lonnavmaps::resource";
                     59: 
1.135     bowersj2   60: # Keep these mappings in sync with lonquickgrades, which uses the colors
                     61: # instead of the icons.
                     62: my %statusIconMap = 
1.140     bowersj2   63:     ( $resObj->NETWORK_FAILURE    => '',
                     64:       $resObj->NOTHING_SET        => '',
                     65:       $resObj->CORRECT            => 'navmap.correct.gif',
                     66:       $resObj->EXCUSED            => 'navmap.correct.gif',
                     67:       $resObj->PAST_DUE_NO_ANSWER => 'navmap.wrong.gif',
                     68:       $resObj->PAST_DUE_ANSWER_LATER => 'navmap.wrong.gif',
                     69:       $resObj->ANSWER_OPEN        => 'navmap.wrong.gif',
                     70:       $resObj->OPEN_LATER         => '',
                     71:       $resObj->TRIES_LEFT         => 'navmap.open.gif',
                     72:       $resObj->INCORRECT          => 'navmap.wrong.gif',
                     73:       $resObj->OPEN               => 'navmap.open.gif',
1.185     bowersj2   74:       $resObj->ATTEMPTED          => 'navmap.open.gif',
                     75:       $resObj->ANSWER_SUBMITTED   => '' );
1.135     bowersj2   76: 
                     77: my %iconAltTags = 
                     78:     ( 'navmap.correct.gif' => 'Correct',
                     79:       'navmap.wrong.gif'   => 'Incorrect',
                     80:       'navmap.open.gif'    => 'Open' );
1.133     bowersj2   81: 
1.136     bowersj2   82: # Defines a status->color mapping, null string means don't color
                     83: my %colormap = 
1.140     bowersj2   84:     ( $resObj->NETWORK_FAILURE        => '',
                     85:       $resObj->CORRECT                => '',
                     86:       $resObj->EXCUSED                => '#3333FF',
                     87:       $resObj->PAST_DUE_ANSWER_LATER  => '',
                     88:       $resObj->PAST_DUE_NO_ANSWER     => '',
                     89:       $resObj->ANSWER_OPEN            => '#006600',
                     90:       $resObj->OPEN_LATER             => '',
                     91:       $resObj->TRIES_LEFT             => '',
                     92:       $resObj->INCORRECT              => '',
                     93:       $resObj->OPEN                   => '',
                     94:       $resObj->NOTHING_SET            => '' );
1.136     bowersj2   95: # And a special case in the nav map; what to do when the assignment
                     96: # is not yet done and due in less then 24 hours
                     97: my $hurryUpColor = "#FF0000";
1.130     www        98: 
1.1       www        99: sub handler {
1.99      bowersj2  100:     my $r = shift;
1.118     bowersj2  101:     real_handler($r);
                    102: }
                    103: 
                    104: sub real_handler {
                    105:     my $r = shift;
1.2       www       106: 
1.51      bowersj2  107:     # Handle header-only request
                    108:     if ($r->header_only) {
                    109:         if ($ENV{'browser.mathml'}) {
                    110:             $r->content_type('text/xml');
                    111:         } else {
                    112:             $r->content_type('text/html');
                    113:         }
                    114:         $r->send_http_header;
                    115:         return OK;
                    116:     }
                    117: 
                    118:     # Send header, don't cache this page
                    119:     if ($ENV{'browser.mathml'}) {
                    120:         $r->content_type('text/xml');
                    121:     } else {
                    122:         $r->content_type('text/html');
                    123:     }
                    124:     &Apache::loncommon::no_cache($r);
                    125:     $r->send_http_header;
                    126: 
1.106     bowersj2  127:     # Create the nav map
1.168     bowersj2  128:     my $navmap = Apache::lonnavmaps::navmap->new(
1.51      bowersj2  129:                         $ENV{"request.course.fn"}.".db",
1.80      bowersj2  130:                         $ENV{"request.course.fn"}."_parms.db", 1, 1);
1.51      bowersj2  131: 
1.55      bowersj2  132: 
                    133:     if (!defined($navmap)) {
                    134:         my $requrl = $r->uri;
                    135:         $ENV{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
                    136:         return HTTP_NOT_ACCEPTABLE;
                    137:     }
1.64      bowersj2  138: 
1.111     bowersj2  139:     $r->print("<html><head>\n");
1.150     www       140:     $r->print("<title>Navigate Course Contents</title>");
                    141: # ------------------------------------------------------------ Get query string
                    142:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['register']);
1.158     bowersj2  143:     
1.150     www       144: # ----------------------------------------------------- Force menu registration
                    145:     my $addentries='';
                    146:     if ($ENV{'form.register'}) {
                    147:        $addentries=' onLoad="'.&Apache::lonmenu::loadevents().
                    148: 	   '" onUnload="'.&Apache::lonmenu::unloadevents().'"';
                    149:        $r->print(&Apache::lonmenu::registerurl(1));
                    150:     }
1.111     bowersj2  151: 
1.64      bowersj2  152:     # Header
1.150     www       153:     $r->print('</head>'.
                    154:               &Apache::loncommon::bodytag('Navigate Course Contents','',
1.151     www       155:                                     $addentries,'','',$ENV{'form.register'}));
1.64      bowersj2  156:     $r->print('<script>window.focus();</script>');
1.101     bowersj2  157: 
1.124     bowersj2  158:     $r->rflush();
                    159: 
                    160:     # Now that we've displayed some stuff to the user, init the navmap
                    161:     $navmap->init();
                    162: 
1.95      bowersj2  163:     $r->print('<br>&nbsp;');
                    164:     $r->rflush();
                    165: 
1.55      bowersj2  166:     # Check that it's defined
                    167:     if (!($navmap->courseMapDefined())) {
                    168:         $r->print('<font size="+2" color="red">Coursemap undefined.</font>' .
                    169:                   '</body></html>');
                    170:         return OK;
                    171:     }
                    172: 
1.178     bowersj2  173:     # See if there's only one map in the top-level, if we don't
                    174:     # already have a filter... if so, automatically display it
1.179     bowersj2  175:     if ($ENV{QUERY_STRING} !~ /filter/) {
1.178     bowersj2  176:         my $iterator = $navmap->getIterator(undef, undef, undef, 0);
                    177:         my $depth = 1;
                    178:         $iterator->next();
                    179:         my $curRes = $iterator->next();
                    180:         my $sequenceCount = 0;
                    181:         my $sequenceId;
                    182:         while ($depth > 0) {
                    183:             if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
                    184:             if ($curRes == $iterator->END_MAP()) { $depth--; }
                    185:             
                    186:             if (ref($curRes) && $curRes->is_sequence()) {
                    187:                 $sequenceCount++;
                    188:                 $sequenceId = $curRes->map_pc();
1.179     bowersj2  189:             }
1.178     bowersj2  190:             
                    191:             $curRes = $iterator->next();
                    192:         }
                    193:         
                    194:         if ($sequenceCount == 1) {
                    195:             # The automatic iterator creation in the render call 
                    196:             # will pick this up. We know the condition because
                    197:             # the defined($ENV{'form.filter'}) also ensures this
                    198:             # is a fresh call.
                    199:             $ENV{'form.filter'} = "$sequenceId";
1.155     bowersj2  200:         }
                    201:     }
                    202: 
1.184     bowersj2  203:     # Check to see if the student is jumping to next open, do-able problem
                    204:     if ($ENV{QUERY_STRING} eq 'jumpToFirstHomework') {
                    205:         # Find the next homework problem that they can do.
                    206:         my $iterator = $navmap->getIterator(undef, undef, undef, 1);
                    207:         my $depth = 1;
                    208:         $iterator->next();
                    209:         my $curRes = $iterator->next();
                    210:         my $foundDoableProblem = 0;
                    211:         my $problemRes;
                    212:         
                    213:         while ($depth > 0 && !$foundDoableProblem) {
                    214:             if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
                    215:             if ($curRes == $iterator->END_MAP()) { $depth--; }
                    216: 
                    217:             if (ref($curRes) && $curRes->is_problem()) {
                    218:                 my $status = $curRes->status();
                    219:                 if (($status == $curRes->OPEN || 
                    220:                      $status == $curRes->TRIES_LEFT()) &&
                    221:                     $curRes->getCompletionStatus() != $curRes->ATTEMPTED()) {
                    222:                     $problemRes = $curRes;
                    223:                     $foundDoableProblem = 1;
                    224: 
                    225:                     # Pop open all previous maps
                    226:                     my $stack = $iterator->getStack();
                    227:                     pop @$stack; # last resource in the stack is the problem
                    228:                                  # itself, which we don't need in the map stack
                    229:                     my @mapPcs = map {$_->map_pc()} @$stack;
                    230:                     $ENV{'form.filter'} = join(',', @mapPcs);
                    231: 
                    232:                     # Mark as both "here" and "jump"
                    233:                     $ENV{'form.postsymb'} = $curRes->symb();
                    234:                 }
                    235:             }
                    236:         } continue {
                    237:             $curRes = $iterator->next();
                    238:         }
                    239: 
                    240:         # If we found no problems, print a note to that effect.
                    241:         if (!$foundDoableProblem) {
                    242:             $r->print("<font size='+2'>All homework assignments have been completed.</font><br /><br />");
                    243:         }
                    244:     } else {
                    245:         $r->print("<a href='navmaps?jumpToFirstHomework'>" .
                    246:                   "Go To My First Homework Problem</a><br />");
                    247:     }
                    248: 
1.133     bowersj2  249:     # renderer call
1.140     bowersj2  250:     my $render = render({ 'cols' => [0,1,2,3],
                    251:                           'url' => '/adm/navmaps',
1.169     bowersj2  252:                           'navmap' => $navmap,
1.153     bowersj2  253:                           'suppressNavmap' => 1,
1.140     bowersj2  254:                           'r' => $r});
1.133     bowersj2  255: 
1.168     bowersj2  256:     $navmap->untieHashes();
1.51      bowersj2  257: 
1.128     bowersj2  258:     $r->print("</body></html>");
1.134     bowersj2  259:     $r->rflush();
1.59      bowersj2  260: 
1.51      bowersj2  261:     return OK;
                    262: }
                    263: 
                    264: # Convenience functions: Returns a string that adds or subtracts
                    265: # the second argument from the first hash, appropriate for the 
                    266: # query string that determines which folders to recurse on
                    267: sub addToFilter {
                    268:     my $hashIn = shift;
                    269:     my $addition = shift;
                    270:     my %hash = %$hashIn;
                    271:     $hash{$addition} = 1;
                    272: 
                    273:     return join (",", keys(%hash));
                    274: }
                    275: 
                    276: sub removeFromFilter {
                    277:     my $hashIn = shift;
                    278:     my $subtraction = shift;
                    279:     my %hash = %$hashIn;
                    280: 
                    281:     delete $hash{$subtraction};
                    282:     return join(",", keys(%hash));
                    283: }
                    284: 
                    285: # Convenience function: Given a stack returned from getStack on the iterator,
                    286: # return the correct src() value.
                    287: # Later, this should add an anchor when we start putting anchors in pages.
                    288: sub getLinkForResource {
                    289:     my $stack = shift;
                    290:     my $res;
                    291: 
                    292:     # Check to see if there are any pages in the stack
                    293:     foreach $res (@$stack) {
                    294:         if (defined($res) && $res->is_page()) {
                    295:             return $res->src();
                    296:         }
                    297:     }
                    298: 
                    299:     # Failing that, return the src of the last resource that is defined
                    300:     # (when we first recurse on a map, it puts an undefined resource
                    301:     # on the bottom because $self->{HERE} isn't defined yet, and we
                    302:     # want the src for the map anyhow)
                    303:     foreach (@$stack) {
                    304:         if (defined($_)) { $res = $_; }
                    305:     }
                    306: 
                    307:     return $res->src();
                    308: }
                    309: 
1.53      bowersj2  310: # Convenience function: This seperates the logic of how to create
                    311: # the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
                    312: # etc.) into a seperate function. It takes a resource object as the
                    313: # first parameter, and the part number of the resource as the second.
                    314: # It's basically a big switch statement on the status of the resource.
                    315: 
                    316: sub getDescription {
                    317:     my $res = shift;
                    318:     my $part = shift;
1.69      bowersj2  319:     my $status = $res->status($part);
1.53      bowersj2  320: 
1.182     bowersj2  321:     if ($status == $res->NETWORK_FAILURE) { 
                    322:         return "Having technical difficulties; please check status later"; 
                    323:     }
1.53      bowersj2  324:     if ($status == $res->NOTHING_SET) {
                    325:         return "Not currently assigned.";
                    326:     }
                    327:     if ($status == $res->OPEN_LATER) {
1.73      bowersj2  328:         return "Open " . timeToHumanString($res->opendate($part));
1.53      bowersj2  329:     }
                    330:     if ($status == $res->OPEN) {
1.79      bowersj2  331:         if ($res->duedate($part)) {
1.73      bowersj2  332:             return "Due " . timeToHumanString($res->duedate($part));
1.66      bowersj2  333:         } else {
                    334:             return "Open, no due date";
                    335:         }
1.53      bowersj2  336:     }
1.54      bowersj2  337:     if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73      bowersj2  338:         return "Answer open " . timeToHumanString($res->answerdate($part));
1.54      bowersj2  339:     }
                    340:     if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73      bowersj2  341:         return "Was due " . timeToHumanString($res->duedate($part));
1.53      bowersj2  342:     }
                    343:     if ($status == $res->ANSWER_OPEN) {
                    344:         return "Answer available";
                    345:     }
1.59      bowersj2  346:     if ($status == $res->EXCUSED) {
1.66      bowersj2  347:         return "Excused by instructor";
1.59      bowersj2  348:     }
1.69      bowersj2  349:     if ($status == $res->ATTEMPTED) {
                    350:         return "Not yet graded.";
                    351:     }
1.59      bowersj2  352:     if ($status == $res->TRIES_LEFT) {
1.79      bowersj2  353:         my $tries = $res->tries($part);
                    354:         my $maxtries = $res->maxtries($part);
                    355:         my $triesString = "";
                    356:         if ($tries && $maxtries) {
                    357:             $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
                    358:             if ($maxtries > 1 && $maxtries - $tries == 1) {
                    359:                 $triesString = "<b>$triesString</b>";
                    360:             }
                    361:         }
1.66      bowersj2  362:         if ($res->duedate()) {
1.73      bowersj2  363:             return "Due " . timeToHumanString($res->duedate($part)) .
1.66      bowersj2  364:                 " $triesString";
                    365:         } else {
                    366:             return "No due date $triesString";
                    367:         }
1.59      bowersj2  368:     }
1.185     bowersj2  369:     if ($status == $res->ANSWER_SUBMITTED) {
                    370:         return 'Answer submitted';
                    371:     }
1.53      bowersj2  372: }
                    373: 
1.115     bowersj2  374: # Convenience function, so others can use it: Is the problem due in less then
                    375: # 24 hours, and still can be done?
                    376: 
                    377: sub dueInLessThen24Hours {
                    378:     my $res = shift;
                    379:     my $part = shift;
                    380:     my $status = $res->status($part);
                    381: 
                    382:     return ($status == $res->OPEN() || $status == $res->ATTEMPTED() ||
                    383:             $status == $res->TRIES_LEFT()) &&
                    384:            $res->duedate() && $res->duedate() < time()+(24*60*60) &&
                    385:            $res->duedate() > time();
                    386: }
                    387: 
                    388: # Convenience function, so others can use it: Is there only one try remaining for the
                    389: # part, with more then one try to begin with, not due yet and still can be done?
                    390: sub lastTry {
                    391:     my $res = shift;
                    392:     my $part = shift;
                    393: 
                    394:     my $tries = $res->tries($part);
                    395:     my $maxtries = $res->maxtries($part);
                    396:     return $tries && $maxtries && $maxtries > 1 &&
                    397:         $maxtries - $tries == 1 && $res->duedate() &&
                    398:         $res->duedate() > time();
                    399: }
                    400: 
1.101     bowersj2  401: # This puts a human-readable name on the ENV variable.
1.177     www       402: 
1.68      bowersj2  403: sub advancedUser {
1.177     www       404:     return $ENV{'request.role.adv'};
1.68      bowersj2  405: }
                    406: 
1.73      bowersj2  407: 
                    408: # timeToHumanString takes a time number and converts it to a
                    409: # human-readable representation, meant to be used in the following
                    410: # manner:
                    411: # print "Due $timestring"
                    412: # print "Open $timestring"
                    413: # print "Answer available $timestring"
                    414: # Very, very, very, VERY English-only... goodness help a localizer on
                    415: # this func...
1.53      bowersj2  416: sub timeToHumanString {
1.58      albertel  417:     my ($time) = @_;
                    418:     # zero, '0' and blank are bad times
1.73      bowersj2  419:     if (!$time) {
                    420:         return 'never';
                    421:     }
                    422: 
                    423:     my $now = time();
                    424: 
                    425:     my @time = localtime($time);
                    426:     my @now = localtime($now);
                    427: 
                    428:     # Positive = future
                    429:     my $delta = $time - $now;
                    430: 
                    431:     my $minute = 60;
                    432:     my $hour = 60 * $minute;
                    433:     my $day = 24 * $hour;
                    434:     my $week = 7 * $day;
                    435:     my $inPast = 0;
                    436: 
                    437:     # Logic in comments:
                    438:     # Is it now? (extremely unlikely)
                    439:     if ( $delta == 0 ) {
                    440:         return "this instant";
                    441:     }
                    442: 
                    443:     if ($delta < 0) {
                    444:         $inPast = 1;
                    445:         $delta = -$delta;
                    446:     }
                    447: 
                    448:     if ( $delta > 0 ) {
1.101     bowersj2  449: 
1.73      bowersj2  450:         my $tense = $inPast ? " ago" : "";
                    451:         my $prefix = $inPast ? "" : "in ";
1.101     bowersj2  452:         
                    453:         # Less then a minute
1.73      bowersj2  454:         if ( $delta < $minute ) {
                    455:             if ($delta == 1) { return "${prefix}1 second$tense"; }
                    456:             return "$prefix$delta seconds$tense";
                    457:         }
                    458: 
1.101     bowersj2  459:         # Less then an hour
1.73      bowersj2  460:         if ( $delta < $hour ) {
                    461:             # If so, use minutes
                    462:             my $minutes = floor($delta / 60);
                    463:             if ($minutes == 1) { return "${prefix}1 minute$tense"; }
                    464:             return "$prefix$minutes minutes$tense";
                    465:         }
                    466:         
                    467:         # Is it less then 24 hours away? If so,
                    468:         # display hours + minutes
                    469:         if ( $delta < $hour * 24) {
                    470:             my $hours = floor($delta / $hour);
                    471:             my $minutes = floor(($delta % $hour) / $minute);
                    472:             my $hourString = "$hours hours";
                    473:             my $minuteString = ", $minutes minutes";
                    474:             if ($hours == 1) {
                    475:                 $hourString = "1 hour";
                    476:             }
                    477:             if ($minutes == 1) {
                    478:                 $minuteString = ", 1 minute";
                    479:             }
                    480:             if ($minutes == 0) {
                    481:                 $minuteString = "";
                    482:             }
                    483:             return "$prefix$hourString$minuteString$tense";
                    484:         }
                    485: 
                    486:         # Less then 5 days away, display day of the week and
                    487:         # HH:MM
                    488:         if ( $delta < $day * 5 ) {
1.85      bowersj2  489:             my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73      bowersj2  490:             $timeStr =~ s/12:00 am/midnight/;
                    491:             $timeStr =~ s/12:00 pm/noon/;
                    492:             return ($inPast ? "last " : "next ") .
                    493:                 $timeStr;
                    494:         }
                    495:         
                    496:         # Is it this year?
                    497:         if ( $time[5] == $now[5]) {
                    498:             # Return on Month Day, HH:MM meridian
                    499:             my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
                    500:             $timeStr =~ s/12:00 am/midnight/;
                    501:             $timeStr =~ s/12:00 pm/noon/;
                    502:             return $timeStr;
                    503:         }
                    504: 
                    505:         # Not this year, so show the year
                    506:         my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
                    507:         $timeStr =~ s/12:00 am/midnight/;
                    508:         $timeStr =~ s/12:00 pm/noon/;
                    509:         return $timeStr;
1.58      albertel  510:     }
1.53      bowersj2  511: }
                    512: 
1.51      bowersj2  513: 
1.133     bowersj2  514: =pod
                    515: 
1.174     albertel  516: =head1 NAME
                    517: 
                    518: Apache::lonnavmap - Subroutines to handle and render the navigation maps
                    519: 
                    520: =head1 SYNOPSIS
                    521: 
                    522: The main handler generates the navigational listing for the course,
                    523: the other objects export this information in a usable fashion for
                    524: other modules
1.133     bowersj2  525: 
1.174     albertel  526: =head1 Object: render
1.133     bowersj2  527: 
1.174     albertel  528: The navmap renderer package provides a sophisticated rendering of the
                    529: standard navigation maps interface into HTML. The provided nav map
                    530: handler is actually just a glorified call to this.
1.133     bowersj2  531: 
1.174     albertel  532: Because of the large number of parameters this function presents,
                    533: instead of passing it arguments as is normal, pass it in an anonymous
                    534: hash with the given options. This is because there is no obvious order
                    535: you may wish to override these in and a hash is easier to read and
                    536: understand then "undef, undef, undef, 1, undef, undef, renderButton,
                    537: undef, 0" when you mostly want default behaviors.
                    538: 
                    539: The package provides a function called 'render', called as
                    540: Apache::lonnavmaps::renderer->render({}).
1.51      bowersj2  541: 
1.133     bowersj2  542: =head2 Overview of Columns
1.51      bowersj2  543: 
1.174     albertel  544: The renderer will build an HTML table for the navmap and return
                    545: it. The table is consists of several columns, and a row for each
                    546: resource (or possibly each part). You tell the renderer how many
                    547: columns to create and what to place in each column, optionally using
                    548: one or more of the preparent columns, and the renderer will assemble
                    549: the table.
                    550: 
                    551: Any additional generally useful column types should be placed in the
                    552: renderer code here, so anybody can use it anywhere else. Any code
                    553: specific to the current application (such as the addition of <input>
                    554: elements in a column) should be placed in the code of the thing using
                    555: the renderer.
                    556: 
                    557: At the core of the renderer is the array reference COLS (see Example
                    558: section below for how to pass this correctly). The COLS array will
                    559: consist of entries of one of two types of things: Either an integer
                    560: representing one of the pre-packaged column types, or a sub reference
                    561: that takes a resource reference, a part number, and a reference to the
                    562: argument hash passed to the renderer, and returns a string that will
                    563: be inserted into the HTML representation as it.
                    564: 
                    565: The pre-packaged column names are refered to by constants in the
                    566: Apache::lonnavmaps::renderer namespace. The following currently exist:
1.51      bowersj2  567: 
1.174     albertel  568: =over 4
1.51      bowersj2  569: 
1.174     albertel  570: =item * B<resource>:
1.51      bowersj2  571: 
1.174     albertel  572: The general info about the resource: Link, icon for the type, etc. The
                    573: first column in the standard nav map display. This column also accepts
                    574: the following parameter in the renderer hash:
1.51      bowersj2  575: 
1.133     bowersj2  576: =over 4
1.51      bowersj2  577: 
1.174     albertel  578: =item * B<resource_nolink>:
1.51      bowersj2  579: 
1.174     albertel  580: If true, the resource will not be linked. Default: false, resource
                    581: will have links.
                    582: 
                    583: =item * B<resource_part_count>:
1.51      bowersj2  584: 
1.174     albertel  585: If true (default), the resource will show a part count if the full
                    586: part list is not displayed. If false, the resource will never show a
                    587: part count.
1.133     bowersj2  588: 
1.174     albertel  589: =item * B<resource_no_folder_link>:
1.133     bowersj2  590: 
1.174     albertel  591: If true, the resource's folder will not be clickable to open or close
                    592: it. Default is false. True implies printCloseAll is false, since you
                    593: can't close or open folders when this is on anyhow.
1.144     bowersj2  594: 
1.133     bowersj2  595: =back
1.51      bowersj2  596: 
1.174     albertel  597: =item B<communication_status>:
                    598: 
                    599: Whether there is discussion on the resource, email for the user, or
                    600: (lumped in here) perl errors in the execution of the problem. This is
                    601: the second column in the main nav map.
                    602: 
                    603: =item B<quick_status>:
1.51      bowersj2  604: 
1.174     albertel  605: An icon for the status of a problem, with four possible states:
                    606: Correct, incorrect, open, or none (not open yet, not a problem). The
                    607: third column of the standard navmap.
1.51      bowersj2  608: 
1.174     albertel  609: =item B<long_status>:
                    610: 
                    611: A text readout of the details of the current status of the problem,
                    612: such as "Due in 22 hours". The fourth column of the standard navmap.
1.51      bowersj2  613: 
1.133     bowersj2  614: =back
1.51      bowersj2  615: 
1.133     bowersj2  616: If you add any others please be sure to document them here.
1.51      bowersj2  617: 
1.174     albertel  618: An example of a column renderer that will show the ID number of a
                    619: resource, along with the part name if any:
1.51      bowersj2  620: 
1.133     bowersj2  621:  sub { 
                    622:   my ($resource, $part, $params) = @_;   
                    623:   if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
                    624:   return '<td>' . $resource->{ID} . '</td>';
                    625:  }
1.51      bowersj2  626: 
1.174     albertel  627: Note these functions are responsible for the TD tags, which allow them
                    628: to override vertical and horizontal alignment, etc.
1.130     www       629: 
1.133     bowersj2  630: =head2 Parameters
1.115     bowersj2  631: 
1.174     albertel  632: Most of these parameters are only useful if you are *not* using the
                    633: folder interface (i.e., the default first column), which is probably
                    634: the common case. If you are using this interface, then you should be
                    635: able to get away with just using 'cols' (to specify the columns
                    636: shown), 'url' (necessary for the folders to link to the current screen
                    637: correctly), and possibly 'queryString' if your app calls for it. In
                    638: that case, maintaining the state of the folders will be done
                    639: automatically.
1.140     bowersj2  640: 
1.133     bowersj2  641: =over 4
1.51      bowersj2  642: 
1.174     albertel  643: =item * B<iterator>:
                    644: 
                    645: A reference to a fresh ::iterator to use from the navmaps. The
                    646: rendering will reflect the options passed to the iterator, so you can
                    647: use that to just render a certain part of the course, if you like. If
                    648: one is not passed, the renderer will attempt to construct one from
                    649: ENV{'form.filter'} and ENV{'form.condition'} information, plus the
                    650: 'iterator_map' parameter if any.
                    651: 
                    652: =item * B<iterator_map>:
                    653: 
                    654: If you are letting the renderer do the iterator handling, you can
                    655: instruct the renderer to render only a particular map by passing it
                    656: the source of the map you want to process, like
                    657: '/res/103/jerf/navmap.course.sequence'.
                    658: 
                    659: =item * B<navmap>:
                    660: 
                    661: A reference to a navmap, used only if an iterator is not passed in. If
                    662: this is necessary to make an iterator but it is not passed in, a new
                    663: one will be constructed based on ENV info. This is useful to do basic
                    664: error checking before passing it off to render.
                    665: 
                    666: =item * B<r>:
                    667: 
                    668: The standard Apache response object. This must be passed to the
                    669: renderer or the course hash will be locked.
                    670: 
                    671: =item * B<cols>:
                    672: 
                    673: An array reference
                    674: 
                    675: =item * B<showParts>:
                    676: 
                    677: A flag. If yes (default), a line for the resource itself, and a line
                    678: for each part will be displayed. If not, only one line for each
                    679: resource will be displayed.
                    680: 
                    681: =item * B<condenseParts>:
                    682: 
                    683: A flag. If yes (default), if all parts of the problem have the same
                    684: status and that status is Nothing Set, Correct, or Network Failure,
                    685: then only one line will be displayed for that resource anyhow. If no,
                    686: all parts will always be displayed. If showParts is 0, this is
                    687: ignored.
                    688: 
                    689: =item * B<jumpCount>:
                    690: 
                    691: A string identifying the URL to place the anchor 'curloc' at. Default
                    692: to no anchor at all. It is the responsibility of the renderer user to
                    693: ensure that the #curloc is in the URL. By default, determined through
                    694: the use of the ENV{} 'jump' information, and should normally "just
                    695: work" correctly.
1.144     bowersj2  696: 
1.174     albertel  697: =item * B<here>:
1.140     bowersj2  698: 
1.174     albertel  699: A Symb identifying where to place the 'here' marker. Default empty,
                    700: which means no marker.
1.92      bowersj2  701: 
1.174     albertel  702: =item * B<indentString>:
1.164     bowersj2  703: 
1.174     albertel  704: A string identifying the indentation string to use. By default, this
                    705: is a 25 pixel whitespace image with no alt text.
1.92      bowersj2  706: 
1.174     albertel  707: =item * B<queryString>:
1.51      bowersj2  708: 
1.174     albertel  709: A string which will be prepended to the query string used when the
                    710: folders are opened or closed.
1.55      bowersj2  711: 
1.174     albertel  712: =item * B<url>:
1.84      bowersj2  713: 
1.174     albertel  714: The url the folders will link to, which should be the current
                    715: page. Required if the resource info column is shown.
1.115     bowersj2  716: 
1.174     albertel  717: =item * B<currentJumpIndex>:
1.55      bowersj2  718: 
1.174     albertel  719: Describes the currently-open row number to cause the browser to jump
                    720: to, because the user just opened that folder. By default, pulled from
                    721: the Jump information in the ENV{'form.*'}.
1.51      bowersj2  722: 
1.174     albertel  723: =item * B<printKey>:
1.51      bowersj2  724: 
1.174     albertel  725: If true, print the key that appears on the top of the standard
                    726: navmaps. Default is false.
1.139     bowersj2  727: 
1.174     albertel  728: =item * B<printCloseAll>:
1.140     bowersj2  729: 
1.174     albertel  730: If true, print the "Close all folders" or "open all folders"
                    731: links. Default is true.
1.140     bowersj2  732: 
1.174     albertel  733: =item * B<filterFunc>:
1.153     bowersj2  734: 
1.174     albertel  735: A function that takes the resource object as its only parameter and
                    736: returns a true or false value. If true, the resource is displayed. If
                    737: false, it is simply skipped in the display. By default, all resources
                    738: are shown.
                    739: 
1.190   ! bowersj2  740: =item * B<suppressEmptySequences>:
        !           741: 
        !           742: If you're using a filter function, and displaying sequences to orient
        !           743: the user, then frequently some sequences will be empty. Setting this to
        !           744: true will cause those sequences not to display, so as not to confuse the
        !           745: user into thinking that if the sequence is there there should be things
        !           746: under it.
        !           747: 
1.174     albertel  748: =item * B<suppressNavmaps>:
                    749: 
                    750: If true, will not display Navigate Content resources. Default to
                    751: false.
1.143     bowersj2  752: 
1.133     bowersj2  753: =back
1.51      bowersj2  754: 
1.133     bowersj2  755: =head2 Additional Info
1.51      bowersj2  756: 
1.174     albertel  757: In addition to the parameters you can pass to the renderer, which will
                    758: be passed through unchange to the column renderers, the renderer will
                    759: generate the following information which your renderer may find
                    760: useful:
                    761: 
                    762: If you want to know how many rows were printed, the 'counter' element
                    763: of the hash passed into the render function will contain the
                    764: count. You may want to check whether any resources were printed at
                    765: all.
1.145     bowersj2  766: 
1.133     bowersj2  767: =over 4
1.59      bowersj2  768: 
1.133     bowersj2  769: =back
1.59      bowersj2  770: 
1.133     bowersj2  771: =cut
1.59      bowersj2  772: 
1.133     bowersj2  773: sub resource { return 0; }
                    774: sub communication_status { return 1; }
                    775: sub quick_status { return 2; }
                    776: sub long_status { return 3; }
1.124     bowersj2  777: 
1.133     bowersj2  778: # Data for render_resource
1.51      bowersj2  779: 
1.133     bowersj2  780: sub render_resource {
                    781:     my ($resource, $part, $params) = @_;
1.51      bowersj2  782: 
1.133     bowersj2  783:     my $nonLinkedText = ''; # stuff after resource title not in link
1.51      bowersj2  784: 
1.134     bowersj2  785:     my $link = $params->{"resourceLink"};
                    786:     my $src = $resource->src();
                    787:     my $it = $params->{"iterator"};
1.133     bowersj2  788:     my $filter = $it->{FILTER};
                    789: 
                    790:     my $title = $resource->compTitle();
                    791:     if ($src =~ /^\/uploaded\//) {
                    792:         $nonLinkedText=$title;
                    793:         $title = '';
                    794:     }
                    795:     my $partLabel = "";
                    796:     my $newBranchText = "";
                    797:     
                    798:     # If this is a new branch, label it so
                    799:     if ($params->{'isNewBranch'}) {
                    800:         $newBranchText = "<img src='/adm/lonIcons/branch.gif' border='0' />";
1.51      bowersj2  801:     }
                    802: 
1.133     bowersj2  803:     # links to open and close the folder
                    804:     my $linkopen = "<a href='$link'>";
                    805:     my $linkclose = "</a>";
1.51      bowersj2  806: 
1.133     bowersj2  807:     # Default icon: HTML page
                    808:     my $icon = "<img src='/adm/lonIcons/html.gif' alt='' border='0' />";
                    809:     
                    810:     if ($resource->is_problem()) {
1.156     bowersj2  811:         if ($part eq "" || $params->{'condensed'}) {
1.133     bowersj2  812:             $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border="0" />';
                    813:         } else {
                    814:             $icon = $params->{'indentString'};
                    815:         }
                    816:     }
1.51      bowersj2  817: 
1.133     bowersj2  818:     # Display the correct map icon to open or shut map
                    819:     if ($resource->is_map()) {
                    820:         my $mapId = $resource->map_pc();
                    821:         my $nowOpen = !defined($filter->{$mapId});
                    822:         if ($it->{CONDITION}) {
                    823:             $nowOpen = !$nowOpen;
                    824:         }
1.144     bowersj2  825: 
                    826:         if (!$params->{'resource_no_folder_link'}) {
                    827:             $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif';
                    828:             $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
                    829: 
                    830:             $linkopen = "<a href='" . $params->{'url'} . '?' . 
                    831:                 $params->{'queryString'} . '&filter=';
                    832:             $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
                    833:                 addToFilter($filter, $mapId) :
                    834:                 removeFromFilter($filter, $mapId);
                    835:             $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='
                    836:                 . $params->{'hereType'} . '&here=' .
                    837:                 &Apache::lonnet::escape($params->{'here'}) . 
1.159     bowersj2  838:                 '&jump=' .
1.144     bowersj2  839:                 &Apache::lonnet::escape($resource->symb()) . 
                    840:                 "&folderManip=1'>";
                    841:         } else {
                    842:             # Don't allow users to manipulate folder
                    843:             $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') .
                    844:                 '.nomanip.gif';
                    845:             $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
                    846: 
                    847:             $linkopen = "";
                    848:             $linkclose = "";
                    849:         }
1.133     bowersj2  850:     }
1.51      bowersj2  851: 
1.133     bowersj2  852:     if ($resource->randomout()) {
                    853:         $nonLinkedText .= ' <i>(hidden)</i> ';
                    854:     }
                    855:     
                    856:     # We're done preparing and finally ready to start the rendering
                    857:     my $result = "<td align='left' valign='center'>";
1.140     bowersj2  858: 
                    859:     my $indentLevel = $params->{'indentLevel'};
                    860:     if ($newBranchText) { $indentLevel--; }
                    861: 
1.133     bowersj2  862:     # print indentation
1.140     bowersj2  863:     for (my $i = 0; $i < $indentLevel; $i++) {
1.133     bowersj2  864:         $result .= $params->{'indentString'};
1.84      bowersj2  865:     }
                    866: 
1.133     bowersj2  867:     # Decide what to display
                    868:     $result .= "$newBranchText$linkopen$icon$linkclose";
                    869:     
                    870:     my $curMarkerBegin = '';
                    871:     my $curMarkerEnd = '';
1.51      bowersj2  872: 
1.133     bowersj2  873:     # Is this the current resource?
1.158     bowersj2  874:     if (!$params->{'displayedHereMarker'} && 
                    875:         $resource->symb() eq $params->{'here'} ) {
1.133     bowersj2  876:         $curMarkerBegin = '<font color="red" size="+2">&gt; </font>';
                    877:         $curMarkerEnd = '<font color="red" size="+2">&lt;</font>';
1.158     bowersj2  878:         $params->{'displayedHereMarker'} = 1;
1.51      bowersj2  879:     }
                    880: 
1.156     bowersj2  881:     if ($resource->is_problem() && $part ne "" && 
1.133     bowersj2  882:         !$params->{'condensed'}) {
                    883:         $partLabel = " (Part $part)";
                    884:         $title = "";
1.51      bowersj2  885:     }
                    886: 
1.163     bowersj2  887:     if ($params->{'condensed'} && $resource->countParts() > 1) {
1.133     bowersj2  888:         $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';
1.51      bowersj2  889:     }
                    890: 
1.144     bowersj2  891:     if (!$params->{'resource_nolink'}) {
                    892:         $result .= "  $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";
                    893:     } else {
                    894:         $result .= "  $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText</td>";
                    895:     }
1.51      bowersj2  896: 
1.133     bowersj2  897:     return $result;
                    898: }
1.51      bowersj2  899: 
1.133     bowersj2  900: sub render_communication_status {
                    901:     my ($resource, $part, $params) = @_;
1.134     bowersj2  902:     my $discussionHTML = ""; my $feedbackHTML = ""; my $errorHTML = "";
                    903: 
                    904:     my $link = $params->{"resourceLink"};
                    905:     my $linkopen = "<a href='$link'>";
                    906:     my $linkclose = "</a>";
                    907: 
                    908:     if ($resource->hasDiscussion()) {
                    909:         $discussionHTML = $linkopen .
                    910:             '<img border="0" src="/adm/lonMisc/chat.gif" />' .
                    911:             $linkclose;
                    912:     }
                    913:     
                    914:     if ($resource->getFeedback()) {
                    915:         my $feedback = $resource->getFeedback();
                    916:         foreach (split(/\,/, $feedback)) {
                    917:             if ($_) {
                    918:                 $feedbackHTML .= '&nbsp;<a href="/adm/email?display='
                    919:                     . &Apache::lonnet::escape($_) . '">'
                    920:                     . '<img src="/adm/lonMisc/feedback.gif" '
                    921:                     . 'border="0" /></a>';
                    922:             }
                    923:         }
                    924:     }
                    925:     
                    926:     if ($resource->getErrors()) {
                    927:         my $errors = $resource->getErrors();
                    928:         foreach (split(/,/, $errors)) {
                    929:             if ($_) {
                    930:                 $errorHTML .= '&nbsp;<a href="/adm/email?display='
                    931:                     . &Apache::lonnet::escape($_) . '">'
                    932:                     . '<img src="/adm/lonMisc/bomb.gif" '
                    933:                     . 'border="0" /></a>';
                    934:             }
                    935:         }
                    936:     }
                    937: 
                    938:     return "<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML$errorHTML&nbsp;</td>";
                    939: 
1.133     bowersj2  940: }
                    941: sub render_quick_status {
                    942:     my ($resource, $part, $params) = @_;
1.135     bowersj2  943:     my $result = "";
                    944:     my $firstDisplayed = !$params->{'condensed'} && 
                    945:         $params->{'multipart'} && $part eq "0";
                    946: 
                    947:     my $link = $params->{"resourceLink"};
                    948:     my $linkopen = "<a href='$link'>";
                    949:     my $linkclose = "</a>";
                    950: 
                    951:     if ($resource->is_problem() &&
                    952:         !$firstDisplayed) {
                    953:         my $icon = $statusIconMap{$resource->status($part)};
                    954:         my $alt = $iconAltTags{$icon};
                    955:         if ($icon) {
                    956:             $result .= "<td width='30' valign='center' width='50' align='right'>$linkopen<img width='25' height='25' src='/adm/lonIcons/$icon' border='0' alt='$alt' />$linkclose</td>\n";
                    957:         } else {
                    958:             $result .= "<td width='30'>&nbsp;</td>\n";
                    959:         }
                    960:     } else { # not problem, no icon
                    961:         $result .= "<td width='30'>&nbsp;</td>\n";
                    962:     }
                    963: 
                    964:     return $result;
1.133     bowersj2  965: }
                    966: sub render_long_status {
                    967:     my ($resource, $part, $params) = @_;
1.136     bowersj2  968:     my $result = "<td align='right' valign='center'>\n";
                    969:     my $firstDisplayed = !$params->{'condensed'} && 
                    970:         $params->{'multipart'} && $part eq "0";
                    971:                 
                    972:     my $color;
                    973:     if ($resource->is_problem()) {
                    974:         $color = $colormap{$resource->status};
                    975:         
                    976:         if (dueInLessThen24Hours($resource, $part) ||
                    977:             lastTry($resource, $part)) {
                    978:             $color = $hurryUpColor;
                    979:         }
                    980:     }
                    981:     
                    982:     if ($resource->kind() eq "res" &&
                    983:         $resource->is_problem() &&
                    984:         !$firstDisplayed) {
                    985:         if ($color) {$result .= "<font color=\"$color\"><b>"; }
                    986:         $result .= getDescription($resource, $part);
                    987:         if ($color) {$result .= "</b></font>"; }
                    988:     }
                    989:     if ($resource->is_map() && advancedUser() && $resource->randompick()) {
                    990:         $result .= '(randomly select ' . $resource->randompick() .')';
                    991:     }
                    992:     
                    993:     $result .= "&nbsp;</td>\n";
                    994:     
                    995:     return $result;
1.51      bowersj2  996: }
                    997: 
1.133     bowersj2  998: my @preparedColumns = (\&render_resource, \&render_communication_status,
                    999:                        \&render_quick_status, \&render_long_status);
1.132     bowersj2 1000: 
                   1001: sub setDefault {
                   1002:     my ($val, $default) = @_;
                   1003:     if (!defined($val)) { return $default; }
                   1004:     return $val;
                   1005: }
                   1006: 
                   1007: sub render {
                   1008:     my $args = shift;
1.140     bowersj2 1009:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                   1010:     my $result = '';
                   1011: 
1.132     bowersj2 1012:     # Configure the renderer.
                   1013:     my $cols = $args->{'cols'};
                   1014:     if (!defined($cols)) {
                   1015:         # no columns, no nav maps.
                   1016:         return '';
                   1017:     }
1.140     bowersj2 1018:     my $mustCloseNavMap = 0;
                   1019:     my $navmap;
                   1020:     if (defined($args->{'navmap'})) {
                   1021:         $navmap = $args->{'navmap'};
                   1022:     }
                   1023: 
1.158     bowersj2 1024:     my $r = $args->{'r'};
1.140     bowersj2 1025:     my $queryString = $args->{'queryString'};
1.159     bowersj2 1026:     my $jump = $args->{'jump'};
1.158     bowersj2 1027:     my $here = $args->{'here'};
1.153     bowersj2 1028:     my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
1.140     bowersj2 1029:     my $currentJumpDelta = 2; # change this to change how many resources are displayed
                   1030:                              # before the current resource when using #current
                   1031: 
                   1032:     # If we were passed 'here' information, we are not rendering
                   1033:     # after a folder manipulation, and we were not passed an
                   1034:     # iterator, make sure we open the folders to show the "here"
                   1035:     # marker
                   1036:     my $filterHash = {};
                   1037:     # Figure out what we're not displaying
                   1038:     foreach (split(/\,/, $ENV{"form.filter"})) {
                   1039:         if ($_) {
                   1040:             $filterHash->{$_} = "1";
                   1041:         }
                   1042:     }
                   1043: 
                   1044:     my $condition = 0;
                   1045:     if ($ENV{'form.condition'}) {
                   1046:         $condition = 1;
                   1047:     }
                   1048: 
                   1049:     if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) {
                   1050:         # Step 1: Check to see if we have a navmap
                   1051:         if (!defined($navmap)) {
1.171     bowersj2 1052:             $navmap = Apache::lonnavmaps::navmap->new(
1.140     bowersj2 1053:                         $ENV{"request.course.fn"}.".db",
                   1054:                         $ENV{"request.course.fn"}."_parms.db", 1, 1);
                   1055:             $mustCloseNavMap = 1;
                   1056:         }
                   1057:         $navmap->init();
                   1058: 
                   1059:         # Step two: Locate what kind of here marker is necessary
                   1060:         # Determine where the "here" marker is and where the screen jumps to.
                   1061: 
1.175     bowersj2 1062:         if ($ENV{'form.postsymb'}) {
                   1063:             $here = $jump = $ENV{'form.postsymb'};
1.140     bowersj2 1064:         } elsif ($ENV{'form.postdata'}) {
                   1065:             # couldn't find a symb, is there a URL?
                   1066:             my $currenturl = $ENV{'form.postdata'};
1.158     bowersj2 1067:             #$currenturl=~s/^http\:\/\///;
                   1068:             #$currenturl=~s/^[^\/]+//;
1.140     bowersj2 1069:             
1.158     bowersj2 1070:             $here = $jump = &Apache::lonnet::symbread($currenturl);
1.140     bowersj2 1071:         }
1.158     bowersj2 1072: 
1.140     bowersj2 1073:         # Step three: Ensure the folders are open
                   1074:         my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
                   1075:         my $depth = 1;
                   1076:         $mapIterator->next(); # discard the first BEGIN_MAP
                   1077:         my $curRes = $mapIterator->next();
                   1078:         my $found = 0;
                   1079:         
                   1080:         # We only need to do this if we need to open the maps to show the
                   1081:         # current position. This will change the counter so we can't count
                   1082:         # for the jump marker with this loop.
                   1083:         while ($depth > 0 && !$found) {
                   1084:             if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
                   1085:             if ($curRes == $mapIterator->END_MAP()) { $depth--; }
                   1086:             
1.158     bowersj2 1087:             if (ref($curRes) && $curRes->symb() eq $here) {
1.140     bowersj2 1088:                 my $mapStack = $mapIterator->getStack();
                   1089:                 
                   1090:                 # Ensure the parent maps are open
                   1091:                 for my $map (@{$mapStack}) {
                   1092:                     if ($condition) {
                   1093:                         undef $filterHash->{$map->map_pc()};
                   1094:                     } else {
                   1095:                         $filterHash->{$map->map_pc()} = 1;
                   1096:                     }
                   1097:                 }
                   1098:                 $found = 1;
                   1099:             }
                   1100:             
                   1101:             $curRes = $mapIterator->next();
                   1102:         }            
1.159     bowersj2 1103:     }        
1.140     bowersj2 1104: 
                   1105:     if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
                   1106:         # If this is a click on a folder or something, we want to preserve the "here"
                   1107:         # from the querystring, and get the new "jump" marker
                   1108:         $here = $ENV{'form.here'};
                   1109:         $jump = $ENV{'form.jump'};
                   1110:     } 
                   1111:     
1.132     bowersj2 1112:     my $it = $args->{'iterator'};
                   1113:     if (!defined($it)) {
1.140     bowersj2 1114:         # Construct a default iterator based on $ENV{'form.'} information
                   1115:         
                   1116:         # Step 1: Check to see if we have a navmap
                   1117:         if (!defined($navmap)) {
1.164     bowersj2 1118:             $navmap = Apache::lonnavmaps::navmap->new($r, 
1.140     bowersj2 1119:                         $ENV{"request.course.fn"}.".db",
                   1120:                         $ENV{"request.course.fn"}."_parms.db", 1, 1);
                   1121:             $mustCloseNavMap = 1;
                   1122:         }
                   1123:         # Paranoia: Make sure it's ready
                   1124:         $navmap->init();
                   1125: 
1.144     bowersj2 1126:         # See if we're being passed a specific map
                   1127:         if ($args->{'iterator_map'}) {
                   1128:             my $map = $args->{'iterator_map'};
1.145     bowersj2 1129:             $map = $navmap->getResourceByUrl($map);
1.144     bowersj2 1130:             my $firstResource = $map->map_start();
                   1131:             my $finishResource = $map->map_finish();
                   1132: 
                   1133:             $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
                   1134:         } else {
                   1135:             $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
                   1136:         }
1.132     bowersj2 1137:     }
                   1138:     
1.159     bowersj2 1139:     # (re-)Locate the jump point, if any
                   1140:     my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
                   1141:     my $depth = 1;
                   1142:     $mapIterator->next();
                   1143:     my $curRes = $mapIterator->next();
                   1144:     my $foundJump = 0;
                   1145:     my $counter = 0;
                   1146:     
                   1147:     while ($depth > 0 && !$foundJump) {
                   1148:         if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
                   1149:         if ($curRes == $mapIterator->END_MAP()) { $depth--; }
                   1150:         if (ref($curRes)) { $counter++; }
                   1151:         
                   1152:         if (ref($curRes) && $jump eq $curRes->symb()) {
                   1153:             
                   1154:             # This is why we have to use the main iterator instead of the
                   1155:             # potentially faster DFS: The count has to be the same, so
                   1156:             # the order has to be the same, which DFS won't give us.
                   1157:             $args->{'currentJumpIndex'} = $counter;
                   1158:             $foundJump = 1;
                   1159:         }
                   1160:         
                   1161:         $curRes = $mapIterator->next();
                   1162:     }
                   1163: 
1.132     bowersj2 1164:     my $showParts = setDefault($args->{'showParts'}, 1);
                   1165:     my $condenseParts = setDefault($args->{'condenseParts'}, 1);
1.139     bowersj2 1166:     # keeps track of when the current resource is found,
                   1167:     # so we can back up a few and put the anchor above the
                   1168:     # current resource
1.140     bowersj2 1169:     my $printKey = $args->{'printKey'};
                   1170:     my $printCloseAll = $args->{'printCloseAll'};
                   1171:     if (!defined($printCloseAll)) { $printCloseAll = 1; }
1.143     bowersj2 1172:     my $filterFunc = setDefault($args->{'filterFunc'},
                   1173:                                 sub {return 1;});
1.144     bowersj2 1174:     
1.140     bowersj2 1175:     # Print key?
                   1176:     if ($printKey) {
                   1177:         $result .= '<table border="0" cellpadding="2" cellspacing="0">';
                   1178:         my $date=localtime;
                   1179:         $result.='<tr><td align="right" valign="bottom">Key:&nbsp;&nbsp;</td>';
                   1180:         if ($navmap->{LAST_CHECK}) {
                   1181:             $result .= 
                   1182:                 '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
                   1183:                 strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
                   1184:                 '</td><td align="center" valign="bottom">&nbsp;&nbsp;'.
                   1185:                 '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
                   1186:                 '</td>'; 
                   1187:         } else {
                   1188:             $result .= '<td align="center" valign="bottom">&nbsp;&nbsp;'.
                   1189:                 '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
                   1190:                 '&nbsp;&nbsp;<img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
                   1191:                 '</td>'; 
                   1192:         }
                   1193: 
                   1194:         $result .= '</tr></table>';
                   1195:     }
                   1196: 
1.172     bowersj2 1197:     if ($printCloseAll && !$args->{'resource_no_folder_link'}) {
1.140     bowersj2 1198:         if ($condition) {
                   1199:             $result.="<a href=\"navmaps?condition=0&filter=&$queryString" .
1.158     bowersj2 1200:                 "&here=" . Apache::lonnet::escape($here) .
1.140     bowersj2 1201:                 "\">Close All Folders</a>";
                   1202:         } else {
                   1203:             $result.="<a href=\"navmaps?condition=1&filter=&$queryString" .
1.158     bowersj2 1204:                 "&here=" . Apache::lonnet::escape($here) . 
1.140     bowersj2 1205:                 "\">Open All Folders</a>";
                   1206:         }
1.143     bowersj2 1207:         $result .= "<br /><br />\n";
1.140     bowersj2 1208:     }    
                   1209: 
                   1210:     if ($r) {
                   1211:         $r->print($result);
                   1212:         $r->rflush();
                   1213:         $result = "";
                   1214:     }
1.132     bowersj2 1215:     # End parameter setting
1.133     bowersj2 1216:             
1.132     bowersj2 1217:     # Data
1.140     bowersj2 1218:     $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
1.132     bowersj2 1219:     my $res = "Apache::lonnavmaps::resource";
                   1220:     my %condenseStatuses =
                   1221:         ( $res->NETWORK_FAILURE    => 1,
                   1222:           $res->NOTHING_SET        => 1,
                   1223:           $res->CORRECT            => 1 );
                   1224:     my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1.133     bowersj2 1225: 
                   1226:     # Shared variables
                   1227:     $args->{'counter'} = 0; # counts the rows
                   1228:     $args->{'indentLevel'} = 0;
                   1229:     $args->{'isNewBranch'} = 0;
                   1230:     $args->{'condensed'} = 0;    
                   1231:     $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
                   1232:     $args->{'displayedHereMarker'} = 0;
1.132     bowersj2 1233: 
1.190   ! bowersj2 1234:     # If we're suppressing empty sequences, look for them here. Use DFS for speed,
        !          1235:     # since structure actually doesn't matter, except what map has what resources.
        !          1236:     if ($args->{'suppressEmptySequences'}) {
        !          1237:         my $dfsit = Apache::lonnavmaps::DFSiterator->new($navmap,
        !          1238:                                                          $it->{FIRST_RESOURCE},
        !          1239:                                                          $it->{FINISH_RESOURCE},
        !          1240:                                                          {}, undef, 1);
        !          1241:         $depth = 0;
        !          1242:         $dfsit->next();
        !          1243:         my $curRes = $dfsit->next();
        !          1244:         while ($depth > -1) {
        !          1245:             if ($curRes == $dfsit->BEGIN_MAP()) { $depth++; }
        !          1246:             if ($curRes == $dfsit->END_MAP()) { $depth--; }
        !          1247: 
        !          1248:             if (ref($curRes)) { 
        !          1249:                 # Parallel pre-processing: Do sequences have non-filtered-out children?
        !          1250:                 if ($curRes->is_sequence()) {
        !          1251:                     $curRes->{DATA}->{HAS_VISIBLE_CHILDREN} = 0;
        !          1252:                     # Sequences themselves do not count as visible children,
        !          1253:                     # unless those sequences also have visible children.
        !          1254:                     # This means if a sequence appears, there's a "promise"
        !          1255:                     # that there's something under it if you open it, somewhere.
        !          1256:                 } else {
        !          1257:                     # Not a sequence: if it's filtered, ignore it, otherwise
        !          1258:                     # rise up the stack and mark the sequences as having children
        !          1259:                     if (&$filterFunc($curRes)) {
        !          1260:                         for my $sequence (@{$dfsit->getStack()}) {
        !          1261:                             $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} = 1;
        !          1262:                         }
        !          1263:                     }
        !          1264:                 }
        !          1265:             }
        !          1266:         } continue {
        !          1267:             $curRes = $dfsit->next();
        !          1268:         }
        !          1269:     }
        !          1270: 
1.133     bowersj2 1271:     my $displayedJumpMarker = 0;
1.132     bowersj2 1272:     # Set up iteration.
1.159     bowersj2 1273:     $depth = 1;
1.132     bowersj2 1274:     $it->next(); # discard initial BEGIN_MAP
1.159     bowersj2 1275:     $curRes = $it->next();
1.132     bowersj2 1276:     my $now = time();
                   1277:     my $in24Hours = $now + 24 * 60 * 60;
                   1278:     my $rownum = 0;
                   1279: 
1.141     bowersj2 1280:     # export "here" marker information
                   1281:     $args->{'here'} = $here;
                   1282: 
1.132     bowersj2 1283:     while ($depth > 0) {
                   1284:         if ($curRes == $it->BEGIN_MAP()) { $depth++; }
                   1285:         if ($curRes == $it->END_MAP()) { $depth--; }
                   1286: 
                   1287:         # Maintain indentation level.
                   1288:         if ($curRes == $it->BEGIN_MAP() ||
                   1289:             $curRes == $it->BEGIN_BRANCH() ) {
1.133     bowersj2 1290:             $args->{'indentLevel'}++;
1.132     bowersj2 1291:         }
                   1292:         if ($curRes == $it->END_MAP() ||
                   1293:             $curRes == $it->END_BRANCH() ) {
1.133     bowersj2 1294:             $args->{'indentLevel'}--;
1.132     bowersj2 1295:         }
                   1296:         # Notice new branches
                   1297:         if ($curRes == $it->BEGIN_BRANCH()) {
1.133     bowersj2 1298:             $args->{'isNewBranch'} = 1;
                   1299:         }
                   1300: 
                   1301:         # If this isn't an actual resource, continue on
                   1302:         if (!ref($curRes)) {
                   1303:             next;
1.132     bowersj2 1304:         }
1.133     bowersj2 1305: 
                   1306:         $args->{'counter'}++;
1.143     bowersj2 1307: 
                   1308:         # If this has been filtered out, continue on
                   1309:         if (!(&$filterFunc($curRes))) {
                   1310:             $args->{'isNewBranch'} = 0; # Don't falsely remember this
                   1311:             next;
                   1312:         } 
1.132     bowersj2 1313: 
1.190   ! bowersj2 1314:         # If this is an empty sequence and we're filtering them, continue on
        !          1315:         if ($curRes->is_sequence() && $args->{'suppressEmptySequences'} &&
        !          1316:             !$curRes->{DATA}->{HAS_VISIBLE_CHILDREN}) {
        !          1317:             next;
        !          1318:         }
        !          1319: 
1.153     bowersj2 1320:         # If we're suppressing navmaps and this is a navmap, continue on
                   1321:         if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
                   1322:             next;
                   1323:         }
                   1324: 
1.132     bowersj2 1325:         # Does it have multiple parts?
1.133     bowersj2 1326:         $args->{'multipart'} = 0;
                   1327:         $args->{'condensed'} = 0;
1.132     bowersj2 1328:         my @parts;
                   1329:             
                   1330:         # Decide what parts to show.
1.133     bowersj2 1331:         if ($curRes->is_problem() && $showParts) {
1.132     bowersj2 1332:             @parts = @{$curRes->parts()};
1.133     bowersj2 1333:             $args->{'multipart'} = scalar(@parts) > 1;
1.132     bowersj2 1334:             
                   1335:             if ($condenseParts) { # do the condensation
                   1336:                 if (!$curRes->opendate("0")) {
1.162     bowersj2 1337:                     @parts = ();
1.133     bowersj2 1338:                     $args->{'condensed'} = 1;
1.132     bowersj2 1339:                 }
1.133     bowersj2 1340:                 if (!$args->{'condensed'}) {
1.132     bowersj2 1341:                     # Decide whether to condense based on similarity
                   1342:                     my $status = $curRes->status($parts[1]);
                   1343:                     my $due = $curRes->duedate($parts[1]);
                   1344:                     my $open = $curRes->opendate($parts[1]);
                   1345:                     my $statusAllSame = 1;
                   1346:                     my $dueAllSame = 1;
                   1347:                     my $openAllSame = 1;
                   1348:                     for (my $i = 2; $i < scalar(@parts); $i++) {
                   1349:                         if ($curRes->status($parts[$i]) != $status){
                   1350:                             $statusAllSame = 0;
                   1351:                         }
                   1352:                         if ($curRes->duedate($parts[$i]) != $due ) {
                   1353:                             $dueAllSame = 0;
                   1354:                         }
                   1355:                         if ($curRes->opendate($parts[$i]) != $open) {
                   1356:                             $openAllSame = 0;
                   1357:                         }
                   1358:                     }
                   1359:                     # $*allSame is true if all the statuses were
                   1360:                     # the same. Now, if they are all the same and
                   1361:                     # match one of the statuses to condense, or they
                   1362:                     # are all open with the same due date, or they are
                   1363:                     # all OPEN_LATER with the same open date, display the
                   1364:                     # status of the first non-zero part (to get the 'correct'
                   1365:                     # status right, since 0 is never 'correct' or 'open').
                   1366:                     if (($statusAllSame && defined($condenseStatuses{$status})) ||
                   1367:                         ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
                   1368:                         ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
1.161     bowersj2 1369:                         @parts = ();
1.133     bowersj2 1370:                         $args->{'condensed'} = 1;
1.132     bowersj2 1371:                     }
                   1372:                     
                   1373:                 }
                   1374:             }
1.157     bowersj2 1375:         } 
1.132     bowersj2 1376:             
                   1377:         # If the multipart problem was condensed, "forget" it was multipart
                   1378:         if (scalar(@parts) == 1) {
1.133     bowersj2 1379:             $args->{'multipart'} = 0;
1.132     bowersj2 1380:         }
                   1381: 
                   1382:         # Now, we've decided what parts to show. Loop through them and
                   1383:         # show them.
1.156     bowersj2 1384:         foreach my $part ('', @parts) {
                   1385:             if ($part eq '0') {
                   1386:                 next;
                   1387:             }
1.132     bowersj2 1388:             $rownum ++;
                   1389:             my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
                   1390:             
                   1391:             $result .= "  <tr bgcolor='$backgroundColor'>\n";
1.134     bowersj2 1392: 
                   1393:             # Set up some data about the parts that the cols might want
                   1394:             my $filter = $it->{FILTER};
                   1395:             my $stack = $it->getStack();
                   1396:             my $src = getLinkForResource($stack);
                   1397:             
                   1398:             my $srcHasQuestion = $src =~ /\?/;
                   1399:             $args->{"resourceLink"} = $src.
                   1400:                 ($srcHasQuestion?'&':'?') .
1.137     bowersj2 1401:                 'symb=' . &Apache::lonnet::escape($curRes->symb());
1.132     bowersj2 1402:             
                   1403:             # Now, display each column.
                   1404:             foreach my $col (@$cols) {
1.139     bowersj2 1405:                 my $colHTML = '';
                   1406:                 if (ref($col)) {
                   1407:                     $colHTML .= &$col($curRes, $part, $args);
                   1408:                 } else {
                   1409:                     $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
                   1410:                 }
1.132     bowersj2 1411: 
1.133     bowersj2 1412:                 # If this is the first column and it's time to print
                   1413:                 # the anchor, do so
                   1414:                 if ($col == $cols->[0] && 
                   1415:                     $args->{'counter'} == $args->{'currentJumpIndex'} - 
1.139     bowersj2 1416:                     $currentJumpDelta) {
                   1417:                     # Jam the anchor after the <td> tag;
                   1418:                     # necessary for valid HTML (which Mozilla requires)
                   1419:                     $colHTML =~ s/\>/\>\<a name="curloc" \/\>/;
1.133     bowersj2 1420:                     $displayedJumpMarker = 1;
                   1421:                 }
1.139     bowersj2 1422:                 $result .= $colHTML . "\n";
1.132     bowersj2 1423:             }
1.139     bowersj2 1424:             $result .= "    </tr>\n";
1.140     bowersj2 1425:             $args->{'isNewBranch'} = 0;
1.139     bowersj2 1426:         }
1.153     bowersj2 1427: 
1.139     bowersj2 1428:         if ($r && $rownum % 20 == 0) {
                   1429:             $r->print($result);
                   1430:             $result = "";
                   1431:             $r->rflush();
1.132     bowersj2 1432:         }
1.156     bowersj2 1433:     } continue {
1.132     bowersj2 1434:         $curRes = $it->next();
                   1435:     }
                   1436:     
1.134     bowersj2 1437:     # Print out the part that jumps to #curloc if it exists
1.159     bowersj2 1438:     # delay needed because the browser is processing the jump before
                   1439:     # it finishes rendering, so it goes to the wrong place!
                   1440:     # onload might be better, but this routine has no access to that.
                   1441:     # On mozilla, the 0-millisecond timeout seems to prevent this;
                   1442:     # it's quite likely this might fix other browsers, too, and 
                   1443:     # certainly won't hurt anything.
1.139     bowersj2 1444:     if ($displayedJumpMarker) {
1.159     bowersj2 1445:         $result .= "<script>setTimeout(\"location += '#curloc';\", 0)</script>\n";
1.134     bowersj2 1446:     }
                   1447: 
1.132     bowersj2 1448:     $result .= "</table>";
1.140     bowersj2 1449:     
                   1450:     if ($r) {
                   1451:         $r->print($result);
                   1452:         $result = "";
                   1453:         $r->rflush();
                   1454:     }
                   1455:         
1.175     bowersj2 1456:     if ($mustCloseNavMap) { $navmap->untieHashes(); } 
1.132     bowersj2 1457: 
                   1458:     return $result;
                   1459: }
                   1460: 
1.133     bowersj2 1461: 1;
                   1462: 
                   1463: package Apache::lonnavmaps::navmap;
                   1464: 
                   1465: =pod
                   1466: 
1.174     albertel 1467: lonnavmaps provides functions and objects for dealing with the
                   1468: compiled course hashes generated when a user enters the course, the
                   1469: Apache handler for the "Navigation Map" button, and a flexible
                   1470: prepared renderer for navigation maps that are easy to use anywhere.
                   1471: 
                   1472: =head1 Object: navmap
1.133     bowersj2 1473: 
1.174     albertel 1474: Encapsulating the compiled nav map
1.133     bowersj2 1475: 
1.174     albertel 1476: navmap is an object that encapsulates a compiled course map and
                   1477: provides a reasonable interface to it.
1.133     bowersj2 1478: 
1.174     albertel 1479: Most notably it provides a way to navigate the map sensibly and a
                   1480: flexible iterator that makes it easy to write various renderers based
                   1481: on nav maps.
1.133     bowersj2 1482: 
                   1483: You must obtain resource objects through the navmap object.
                   1484: 
                   1485: =head2 Methods
                   1486: 
                   1487: =over 4
                   1488: 
1.174     albertel 1489: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions,
                   1490:   genMailDiscussStatus):
1.133     bowersj2 1491: 
1.174     albertel 1492: Binds a new navmap object to the compiled nav map hash and parm hash
                   1493: given as filenames. genCourseAndUserOptions is a flag saying whether
                   1494: the course options and user options hash should be generated. This is
                   1495: for when you are using the parameters of the resources that require
                   1496: them; see documentation in resource object
                   1497: documentation. genMailDiscussStatus causes the nav map to retreive
                   1498: information about the email and discussion status of
                   1499: resources. Returns the navmap object if this is successful, or
                   1500: B<undef> if not. You must check for undef; errors will occur when you
                   1501: try to use the other methods otherwise.
                   1502: 
                   1503: =item * B<getIterator>(first, finish, filter, condition):
                   1504: 
                   1505: See iterator documentation below.
1.133     bowersj2 1506: 
                   1507: =cut
                   1508: 
                   1509: use strict;
                   1510: use GDBM_File;
                   1511: 
                   1512: sub new {
                   1513:     # magic invocation to create a class instance
                   1514:     my $proto = shift;
                   1515:     my $class = ref($proto) || $proto;
                   1516:     my $self = {};
                   1517: 
                   1518:     $self->{NAV_HASH_FILE} = shift;
                   1519:     $self->{PARM_HASH_FILE} = shift;
                   1520:     $self->{GENERATE_COURSE_USER_OPT} = shift;
                   1521:     $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
                   1522: 
                   1523:     # Resource cache stores navmap resources as we reference them. We generate
                   1524:     # them on-demand so we don't pay for creating resources unless we use them.
                   1525:     $self->{RESOURCE_CACHE} = {};
                   1526: 
                   1527:     # Network failure flag, if we accessed the course or user opt and
                   1528:     # failed
                   1529:     $self->{NETWORK_FAILURE} = 0;
                   1530: 
                   1531:     # tie the nav hash
                   1532: 
1.168     bowersj2 1533:     my %navmaphash;
                   1534:     my %parmhash;
                   1535:     if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
1.133     bowersj2 1536:               &GDBM_READER(), 0640))) {
                   1537:         return undef;
                   1538:     }
                   1539:     
1.168     bowersj2 1540:     if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
1.133     bowersj2 1541:               &GDBM_READER(), 0640)))
                   1542:     {
1.170     matthew  1543:         untie %{$self->{PARM_HASH}};
1.133     bowersj2 1544:         return undef;
                   1545:     }
                   1546: 
1.168     bowersj2 1547:     $self->{NAV_HASH} = \%navmaphash;
                   1548:     $self->{PARM_HASH} = \%parmhash;
1.140     bowersj2 1549:     $self->{INITED} = 0;
1.133     bowersj2 1550: 
                   1551:     bless($self);
                   1552:         
                   1553:     return $self;
                   1554: }
                   1555: 
                   1556: sub init {
                   1557:     my $self = shift;
1.140     bowersj2 1558:     if ($self->{INITED}) { return; }
1.133     bowersj2 1559: 
                   1560:     # If the course opt hash and the user opt hash should be generated,
                   1561:     # generate them
                   1562:     if ($self->{GENERATE_COURSE_USER_OPT}) {
                   1563:         my $uname=$ENV{'user.name'};
                   1564:         my $udom=$ENV{'user.domain'};
                   1565:         my $uhome=$ENV{'user.home'};
                   1566:         my $cid=$ENV{'request.course.id'};
                   1567:         my $chome=$ENV{'course.'.$cid.'.home'};
                   1568:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1569:         
                   1570:         my $userprefix=$uname.'_'.$udom.'_';
                   1571:         
                   1572:         my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
                   1573:         unless ($uhome eq 'no_host') { 
                   1574: # ------------------------------------------------- Get coursedata (if present)
                   1575:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
                   1576:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                   1577:                                                  ':resourcedata',$chome);
1.182     bowersj2 1578:                 # Check for network failure
                   1579:                 if ( $reply =~ /no.such.host/i || $reply =~ /con_lost/i) {
                   1580:                     $self->{NETWORK_FAILURE} = 1;
                   1581:                 } elsif ($reply!~/^error\:/) {
1.133     bowersj2 1582:                     $courserdatas{$cid}=$reply;
                   1583:                     $courserdatas{$cid.'.last_cache'}=time;
                   1584:                 }
                   1585:             }
                   1586:             foreach (split(/\&/,$courserdatas{$cid})) {
                   1587:                 my ($name,$value)=split(/\=/,$_);
                   1588:                 $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1589:                     &Apache::lonnet::unescape($value);
                   1590:             }
                   1591: # --------------------------------------------------- Get userdata (if present)
                   1592:             unless ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
                   1593:                 my $reply=&Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
                   1594:                 if ($reply!~/^error\:/) {
                   1595:                     $userrdatas{$uname.'___'.$udom}=$reply;
                   1596:                     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
                   1597:                 }
                   1598:                 # check to see if network failed
                   1599:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1600:                 {
                   1601:                     $self->{NETWORK_FAILURE} = 1;
                   1602:                 }
                   1603:             }
                   1604:             foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
                   1605:                 my ($name,$value)=split(/\=/,$_);
                   1606:                 $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
                   1607:                     &Apache::lonnet::unescape($value);
                   1608:             }
                   1609:             $self->{COURSE_OPT} = \%courseopt;
                   1610:             $self->{USER_OPT} = \%useropt;
                   1611:         }
                   1612:     }   
                   1613: 
                   1614:     if ($self->{GENERATE_EMAIL_DISCUSS_STATUS}) {
                   1615:         my $cid=$ENV{'request.course.id'};
                   1616:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1617:         
                   1618:         my %emailstatus = &Apache::lonnet::dump('email_status');
                   1619:         my $logoutTime = $emailstatus{'logout'};
                   1620:         my $courseLeaveTime = $emailstatus{'logout_'.$ENV{'request.course.id'}};
                   1621:         $self->{LAST_CHECK} = ($courseLeaveTime < $logoutTime ?
                   1622:                                $courseLeaveTime : $logoutTime);
                   1623:         my %discussiontime = &Apache::lonnet::dump('discussiontimes', 
                   1624:                                                    $cdom, $cnum);
                   1625:         my %feedback=();
                   1626:         my %error=();
                   1627:         my $keys = &Apache::lonnet::reply('keys:'.
                   1628:                                           $ENV{'user.domain'}.':'.
                   1629:                                           $ENV{'user.name'}.':nohist_email',
                   1630:                                           $ENV{'user.home'});
                   1631: 
                   1632:         foreach my $msgid (split(/\&/, $keys)) {
                   1633:             $msgid=&Apache::lonnet::unescape($msgid);
                   1634:             my $plain=&Apache::lonnet::unescape(&Apache::lonnet::unescape($msgid));
                   1635:             if ($plain=~/(Error|Feedback) \[([^\]]+)\]/) {
                   1636:                 my ($what,$url)=($1,$2);
                   1637:                 my %status=
                   1638:                     &Apache::lonnet::get('email_status',[$msgid]);
                   1639:                 if ($status{$msgid}=~/^error\:/) { 
                   1640:                     $status{$msgid}=''; 
                   1641:                 }
                   1642:                 
                   1643:                 if (($status{$msgid} eq 'new') || 
                   1644:                     (!$status{$msgid})) { 
                   1645:                     if ($what eq 'Error') {
                   1646:                         $error{$url}.=','.$msgid; 
                   1647:                     } else {
                   1648:                         $feedback{$url}.=','.$msgid;
                   1649:                     }
                   1650:                 }
                   1651:             }
                   1652:         }
                   1653:         
                   1654:         $self->{FEEDBACK} = \%feedback;
                   1655:         $self->{ERROR_MSG} = \%error; # what is this? JB
                   1656:         $self->{DISCUSSION_TIME} = \%discussiontime;
                   1657:         $self->{EMAIL_STATUS} = \%emailstatus;
                   1658:         
                   1659:     }    
                   1660: 
                   1661:     $self->{PARM_CACHE} = {};
1.140     bowersj2 1662:     $self->{INITED} = 1;
1.133     bowersj2 1663: }
                   1664: 
                   1665: # Internal function: Takes a key to look up in the nav hash and implements internal
                   1666: # memory caching of that key.
                   1667: sub navhash {
                   1668:     my $self = shift; my $key = shift;
                   1669:     return $self->{NAV_HASH}->{$key};
                   1670: }
                   1671: 
                   1672: # Checks to see if coursemap is defined, matching test in old lonnavmaps
                   1673: sub courseMapDefined {
                   1674:     my $self = shift;
                   1675:     my $uri = &Apache::lonnet::clutter($ENV{'request.course.uri'});
                   1676: 
                   1677:     my $firstres = $self->navhash("map_start_$uri");
                   1678:     my $lastres = $self->navhash("map_finish_$uri");
                   1679:     return $firstres && $lastres;
                   1680: }
                   1681: 
                   1682: sub getIterator {
                   1683:     my $self = shift;
                   1684:     my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
                   1685:                                                      shift, undef, shift);
                   1686:     return $iterator;
                   1687: }
                   1688: 
                   1689: # unties the hash when done
                   1690: sub untieHashes {
1.168     bowersj2 1691:     my $self = shift;
1.170     matthew  1692:     untie %{$self->{NAV_HASH}};
                   1693:     untie %{$self->{PARM_HASH}};
1.133     bowersj2 1694: }
                   1695: 
                   1696: # Private method: Does the given resource (as a symb string) have
                   1697: # current discussion? Returns 0 if chat/mail data not extracted.
                   1698: sub hasDiscussion {
                   1699:     my $self = shift;
                   1700:     my $symb = shift;
                   1701:     if (!defined($self->{DISCUSSION_TIME})) { return 0; }
                   1702: 
                   1703:     #return defined($self->{DISCUSSION_TIME}->{$symb});
                   1704:     return $self->{DISCUSSION_TIME}->{$symb} >
                   1705:            $self->{LAST_CHECK};
                   1706: }
                   1707: 
                   1708: # Private method: Does the given resource (as a symb string) have
                   1709: # current feedback? Returns the string in the feedback hash, which
                   1710: # will be false if it does not exist.
                   1711: sub getFeedback { 
                   1712:     my $self = shift;
                   1713:     my $symb = shift;
                   1714: 
                   1715:     if (!defined($self->{FEEDBACK})) { return ""; }
                   1716:     
                   1717:     return $self->{FEEDBACK}->{$symb};
                   1718: }
                   1719: 
                   1720: # Private method: Get the errors for that resource (by source).
                   1721: sub getErrors { 
                   1722:     my $self = shift;
                   1723:     my $src = shift;
                   1724:     
                   1725:     if (!defined($self->{ERROR_MSG})) { return ""; }
                   1726:     return $self->{ERROR_MSG}->{$src};
                   1727: }
                   1728: 
                   1729: =pod
                   1730: 
1.174     albertel 1731: =item * B<getById>(id):
                   1732: 
                   1733: Based on the ID of the resource (1.1, 3.2, etc.), get a resource
                   1734: object for that resource. This method, or other methods that use it
                   1735: (as in the resource object) is the only proper way to obtain a
                   1736: resource object.
1.133     bowersj2 1737: 
                   1738: =cut
                   1739: 
                   1740: # The strategy here is to cache the resource objects, and only construct them
                   1741: # as we use them. The real point is to prevent reading any more from the tied
                   1742: # hash then we have to, which should hopefully alleviate speed problems.
                   1743: # Caching is just an incidental detail I throw in because it makes sense.
                   1744: 
                   1745: sub getById {
                   1746:     my $self = shift;
                   1747:     my $id = shift;
                   1748: 
                   1749:     if (defined ($self->{RESOURCE_CACHE}->{$id}))
                   1750:     {
                   1751:         return $self->{RESOURCE_CACHE}->{$id};
                   1752:     }
                   1753: 
                   1754:     # resource handles inserting itself into cache.
                   1755:     # Not clear why the quotes are necessary, but as of this
                   1756:     # writing it doesn't work without them.
                   1757:     return "Apache::lonnavmaps::resource"->new($self, $id);
1.132     bowersj2 1758: }
1.133     bowersj2 1759: 
1.172     bowersj2 1760: sub getBySymb {
                   1761:     my $self = shift;
                   1762:     my $symb = shift;
                   1763:     my ($mapUrl, $id, $filename) = split (/___/, $symb);
                   1764:     my $map = $self->getResourceByUrl($mapUrl);
                   1765:     return $self->getById($map->map_pc() . '.' . $id);
                   1766: }
                   1767: 
1.133     bowersj2 1768: =pod
                   1769: 
1.174     albertel 1770: =item * B<firstResource>():
                   1771: 
                   1772: Returns a resource object reference corresponding to the first
                   1773: resource in the navmap.
1.133     bowersj2 1774: 
                   1775: =cut
                   1776: 
                   1777: sub firstResource {
                   1778:     my $self = shift;
                   1779:     my $firstResource = $self->navhash('map_start_' .
                   1780:                      &Apache::lonnet::clutter($ENV{'request.course.uri'}));
                   1781:     return $self->getById($firstResource);
1.132     bowersj2 1782: }
1.133     bowersj2 1783: 
                   1784: =pod
                   1785: 
1.174     albertel 1786: =item * B<finishResource>():
                   1787: 
                   1788: Returns a resource object reference corresponding to the last resource
                   1789: in the navmap.
1.133     bowersj2 1790: 
                   1791: =cut
                   1792: 
                   1793: sub finishResource {
                   1794:     my $self = shift;
                   1795:     my $firstResource = $self->navhash('map_finish_' .
                   1796:                      &Apache::lonnet::clutter($ENV{'request.course.uri'}));
                   1797:     return $self->getById($firstResource);
1.132     bowersj2 1798: }
1.133     bowersj2 1799: 
                   1800: # Parmval reads the parm hash and cascades the lookups. parmval_real does
                   1801: # the actual lookup; parmval caches the results.
                   1802: sub parmval {
                   1803:     my $self = shift;
                   1804:     my ($what,$symb)=@_;
                   1805:     my $hashkey = $what."|||".$symb;
                   1806: 
                   1807:     if (defined($self->{PARM_CACHE}->{$hashkey})) {
                   1808:         return $self->{PARM_CACHE}->{$hashkey};
                   1809:     }
                   1810: 
                   1811:     my $result = $self->parmval_real($what, $symb);
                   1812:     $self->{PARM_CACHE}->{$hashkey} = $result;
                   1813:     return $result;
1.132     bowersj2 1814: }
                   1815: 
1.133     bowersj2 1816: sub parmval_real {
                   1817:     my $self = shift;
                   1818:     my ($what,$symb) = @_;
                   1819: 
                   1820:     my $cid=$ENV{'request.course.id'};
                   1821:     my $csec=$ENV{'request.course.sec'};
                   1822:     my $uname=$ENV{'user.name'};
                   1823:     my $udom=$ENV{'user.domain'};
                   1824: 
                   1825:     unless ($symb) { return ''; }
                   1826:     my $result='';
                   1827: 
                   1828:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
                   1829: 
                   1830: # ----------------------------------------------------- Cascading lookup scheme
                   1831:     my $rwhat=$what;
                   1832:     $what=~s/^parameter\_//;
                   1833:     $what=~s/\_/\./;
                   1834: 
                   1835:     my $symbparm=$symb.'.'.$what;
                   1836:     my $mapparm=$mapname.'___(all).'.$what;
                   1837:     my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
                   1838: 
                   1839:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
                   1840:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
                   1841:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
                   1842: 
                   1843:     my $courselevel= $usercourseprefix.'.'.$what;
                   1844:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
                   1845:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
                   1846: 
                   1847:     my $useropt = $self->{USER_OPT};
                   1848:     my $courseopt = $self->{COURSE_OPT};
                   1849:     my $parmhash = $self->{PARM_HASH};
                   1850: 
                   1851: # ---------------------------------------------------------- first, check user
                   1852:     if ($uname and defined($useropt)) {
                   1853:         if (defined($$useropt{$courselevelr})) { return $$useropt{$courselevelr}; }
                   1854:         if (defined($$useropt{$courselevelm})) { return $$useropt{$courselevelm}; }
                   1855:         if (defined($$useropt{$courselevel})) { return $$useropt{$courselevel}; }
                   1856:     }
                   1857: 
                   1858: # ------------------------------------------------------- second, check course
                   1859:     if ($csec and defined($courseopt)) {
                   1860:         if (defined($$courseopt{$seclevelr})) { return $$courseopt{$seclevelr}; }
                   1861:         if (defined($$courseopt{$seclevelm})) { return $$courseopt{$seclevelm}; }
                   1862:         if (defined($$courseopt{$seclevel})) { return $$courseopt{$seclevel}; }
                   1863:     }
                   1864: 
                   1865:     if (defined($courseopt)) {
                   1866:         if (defined($$courseopt{$courselevelr})) { return $$courseopt{$courselevelr}; }
                   1867:         if (defined($$courseopt{$courselevelm})) { return $$courseopt{$courselevelm}; }
                   1868:         if (defined($$courseopt{$courselevel})) { return $$courseopt{$courselevel}; }
                   1869:     }
                   1870: 
                   1871: # ----------------------------------------------------- third, check map parms
                   1872: 
                   1873:     my $thisparm=$$parmhash{$symbparm};
                   1874:     if (defined($thisparm)) { return $thisparm; }
                   1875: 
                   1876: # ----------------------------------------------------- fourth , check default
                   1877: 
                   1878:     my $default=&Apache::lonnet::metadata($fn,$rwhat.'.default');
                   1879:     if (defined($default)) { return $default}
                   1880: 
                   1881: # --------------------------------------------------- fifth , cascade up parts
                   1882: 
                   1883:     my ($space,@qualifier)=split(/\./,$rwhat);
                   1884:     my $qualifier=join('.',@qualifier);
                   1885:     unless ($space eq '0') {
1.160     albertel 1886: 	my @parts=split(/_/,$space);
                   1887: 	my $id=pop(@parts);
                   1888: 	my $part=join('_',@parts);
                   1889: 	if ($part eq '') { $part='0'; }
                   1890: 	my $partgeneral=$self->parmval($part.".$qualifier",$symb);
                   1891: 	if (defined($partgeneral)) { return $partgeneral; }
1.133     bowersj2 1892:     }
                   1893:     return '';
1.145     bowersj2 1894: }
                   1895: 
1.174     albertel 1896: =pod
1.145     bowersj2 1897: 
1.174     albertel 1898: =item * B<getResourceByUrl>(url):
1.145     bowersj2 1899: 
1.174     albertel 1900: Retrieves a resource object by URL of the resource. If passed a
                   1901: resource object, it will simply return it, so it is safe to use this
                   1902: method in code like "$res = $navmap->getResourceByUrl($res)", if
                   1903: you're not sure if $res is already an object, or just a URL. If the
                   1904: resource appears multiple times in the course, only the first instance
                   1905: will be returned. As a result, this is probably useful only for maps.
                   1906: 
                   1907: =item * B<retrieveResources>(map, filterFunc, recursive, bailout):
                   1908: 
                   1909: The map is a specification of a map to retreive the resources from,
                   1910: either as a url or as an object. The filterFunc is a reference to a
                   1911: function that takes a resource object as its one argument and returns
                   1912: true if the resource should be included, or false if it should not
                   1913: be. If recursive is true, the map will be recursively examined,
                   1914: otherwise it will not be. If bailout is true, the function will return
                   1915: as soon as it finds a resource, if false it will finish. By default,
                   1916: the map is the top-level map of the course, filterFunc is a function
                   1917: that always returns 1, recursive is true, bailout is false. The
                   1918: resources will be returned in a list containing the resource objects
                   1919: for the corresponding resources, with B<no structure information> in
                   1920: the list; regardless of branching, recursion, etc., it will be a flat
                   1921: list.
                   1922: 
                   1923: Thus, this is suitable for cases where you don't want the structure,
                   1924: just a list of all resources. It is also suitable for finding out how
                   1925: many resources match a given description; for this use, if all you
                   1926: want to know is if I<any> resources match the description, the bailout
                   1927: parameter will allow you to avoid potentially expensive enumeration of
                   1928: all matching resources.
1.145     bowersj2 1929: 
1.174     albertel 1930: =item * B<hasResources>(map, filterFunc, recursive):
1.145     bowersj2 1931: 
1.174     albertel 1932: Convience method for
1.146     bowersj2 1933: 
                   1934:  scalar(retrieveResources($map, $filterFunc, $recursive, 1)) > 0
                   1935: 
1.174     albertel 1936: which will tell whether the map has resources matching the description
                   1937: in the filter function.
1.146     bowersj2 1938: 
1.145     bowersj2 1939: =cut
                   1940: 
                   1941: sub getResourceByUrl {
                   1942:     my $self = shift;
                   1943:     my $resUrl = shift;
                   1944: 
                   1945:     if (ref($resUrl)) { return $resUrl; }
                   1946: 
                   1947:     $resUrl = &Apache::lonnet::clutter($resUrl);
                   1948:     my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
                   1949:     if ($resId =~ /,/) {
                   1950:         $resId = (split (/,/, $resId))[0];
                   1951:     }
                   1952:     if (!$resId) { return ''; }
                   1953:     return $self->getById($resId);
                   1954: }
                   1955: 
                   1956: sub retrieveResources {
                   1957:     my $self = shift;
                   1958:     my $map = shift;
                   1959:     my $filterFunc = shift;
                   1960:     if (!defined ($filterFunc)) {
                   1961:         $filterFunc = sub {return 1;};
                   1962:     }
                   1963:     my $recursive = shift;
                   1964:     if (!defined($recursive)) { $recursive = 1; }
                   1965:     my $bailout = shift;
                   1966:     if (!defined($bailout)) { $bailout = 0; }
                   1967: 
                   1968:     # Create the necessary iterator.
                   1969:     if (!ref($map)) { # assume it's a url of a map.
1.172     bowersj2 1970:         $map = $self->getResourceByUrl($map);
1.145     bowersj2 1971:     }
                   1972: 
                   1973:     # Check the map's validity.
                   1974:     if (!$map || !$map->is_map()) {
                   1975:         # Oh, to throw an exception.... how I'd love that!
                   1976:         return ();
                   1977:     }
                   1978: 
1.146     bowersj2 1979:     # Get an iterator.
                   1980:     my $it = $self->getIterator($map->map_start(), $map->map_finish(),
                   1981:                                 !$recursive);
                   1982: 
                   1983:     my @resources = ();
                   1984: 
                   1985:     # Run down the iterator and collect the resources.
                   1986:     my $depth = 1;
                   1987:     $it->next();
                   1988:     my $curRes = $it->next();
                   1989: 
                   1990:     while ($depth > 0) {
                   1991:         if ($curRes == $it->BEGIN_MAP()) {
                   1992:             $depth++;
                   1993:         }
                   1994:         if ($curRes == $it->END_MAP()) {
                   1995:             $depth--;
                   1996:         }
                   1997:         
                   1998:         if (ref($curRes)) {
                   1999:             if (!&$filterFunc($curRes)) {
                   2000:                 next;
                   2001:             }
                   2002: 
                   2003:             push @resources, $curRes;
                   2004: 
                   2005:             if ($bailout) {
                   2006:                 return @resources;
                   2007:             }
                   2008:         }
                   2009: 
                   2010:         $curRes = $it->next();
                   2011:     }
                   2012: 
                   2013:     return @resources;
                   2014: }
                   2015: 
                   2016: sub hasResource {
                   2017:     my $self = shift;
                   2018:     my $map = shift;
                   2019:     my $filterFunc = shift;
                   2020:     my $recursive = shift;
                   2021:     
                   2022:     return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1)) > 0;
1.133     bowersj2 2023: }
1.51      bowersj2 2024: 
                   2025: 1;
                   2026: 
                   2027: package Apache::lonnavmaps::iterator;
                   2028: 
                   2029: =pod
                   2030: 
                   2031: =back
                   2032: 
1.174     albertel 2033: =head1 Object: navmap Iterator
1.51      bowersj2 2034: 
1.174     albertel 2035: An I<iterator> encapsulates the logic required to traverse a data
                   2036: structure. navmap uses an iterator to traverse the course map
                   2037: according to the criteria you wish to use.
                   2038: 
                   2039: To obtain an iterator, call the B<getIterator>() function of a
                   2040: B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator
                   2041: directly.) This will return a reference to the iterator:
1.51      bowersj2 2042: 
                   2043: C<my $resourceIterator = $navmap-E<gt>getIterator();>
                   2044: 
                   2045: To get the next thing from the iterator, call B<next>:
                   2046: 
                   2047: C<my $nextThing = $resourceIterator-E<gt>next()>
                   2048: 
                   2049: getIterator behaves as follows:
                   2050: 
                   2051: =over 4
                   2052: 
1.174     albertel 2053: =item * B<getIterator>(firstResource, finishResource, filterHash, condition, forceTop, returnTopMap):
                   2054: 
                   2055: All parameters are optional. firstResource is a resource reference
                   2056: corresponding to where the iterator should start. It defaults to
                   2057: navmap->firstResource() for the corresponding nav map. finishResource
                   2058: corresponds to where you want the iterator to end, defaulting to
                   2059: navmap->finishResource(). filterHash is a hash used as a set
                   2060: containing strings representing the resource IDs, defaulting to
                   2061: empty. Condition is a 1 or 0 that sets what to do with the filter
                   2062: hash: If a 0, then only resource that exist IN the filterHash will be
                   2063: recursed on. If it is a 1, only resources NOT in the filterHash will
                   2064: be recursed on. Defaults to 0. forceTop is a boolean value. If it is
                   2065: false (default), the iterator will only return the first level of map
                   2066: that is not just a single, 'redirecting' map. If true, the iterator
                   2067: will return all information, starting with the top-level map,
                   2068: regardless of content. returnTopMap, if true (default false), will
                   2069: cause the iterator to return the top-level map object (resource 0.0)
                   2070: before anything else.
                   2071: 
                   2072: Thus, by default, only top-level resources will be shown. Change the
                   2073: condition to a 1 without changing the hash, and all resources will be
                   2074: shown. Changing the condition to 1 and including some values in the
                   2075: hash will allow you to selectively suppress parts of the navmap, while
                   2076: leaving it on 0 and adding things to the hash will allow you to
                   2077: selectively add parts of the nav map. See the handler code for
                   2078: examples.
                   2079: 
                   2080: The iterator will return either a reference to a resource object, or a
                   2081: token representing something in the map, such as the beginning of a
                   2082: new branch. The possible tokens are:
                   2083: 
                   2084: =over 4
1.51      bowersj2 2085: 
1.174     albertel 2086: =item * BEGIN_MAP:
1.51      bowersj2 2087: 
1.174     albertel 2088: A new map is being recursed into. This is returned I<after> the map
                   2089: resource itself is returned.
1.51      bowersj2 2090: 
1.174     albertel 2091: =item * END_MAP:
                   2092: 
                   2093: The map is now done.
1.51      bowersj2 2094: 
1.174     albertel 2095: =item * BEGIN_BRANCH:
1.70      bowersj2 2096: 
1.174     albertel 2097: A branch is now starting. The next resource returned will be the first
                   2098: in that branch.
1.70      bowersj2 2099: 
1.174     albertel 2100: =item * END_BRANCH:
1.70      bowersj2 2101: 
1.174     albertel 2102: The branch is now done.
1.51      bowersj2 2103: 
                   2104: =back
                   2105: 
1.174     albertel 2106: The tokens are retreivable via methods on the iterator object, i.e.,
                   2107: $iterator->END_MAP.
1.70      bowersj2 2108: 
1.174     albertel 2109: Maps can contain empty resources. The iterator will automatically skip
                   2110: over such resources, but will still treat the structure
                   2111: correctly. Thus, a complicated map with several branches, but
                   2112: consisting entirely of empty resources except for one beginning or
                   2113: ending resource, will cause a lot of BRANCH_STARTs and BRANCH_ENDs,
                   2114: but only one resource will be returned.
1.116     bowersj2 2115: 
1.70      bowersj2 2116: =back
1.51      bowersj2 2117: 
                   2118: =cut
                   2119: 
                   2120: # Here are the tokens for the iterator:
                   2121: 
                   2122: sub BEGIN_MAP { return 1; }    # begining of a new map
                   2123: sub END_MAP { return 2; }      # end of the map
                   2124: sub BEGIN_BRANCH { return 3; } # beginning of a branch
                   2125: sub END_BRANCH { return 4; }   # end of a branch
1.89      bowersj2 2126: sub FORWARD { return 1; }      # go forward
                   2127: sub BACKWARD { return 2; }
1.51      bowersj2 2128: 
1.96      bowersj2 2129: sub min {
                   2130:     (my $a, my $b) = @_;
                   2131:     if ($a < $b) { return $a; } else { return $b; }
                   2132: }
                   2133: 
1.107     bowersj2 2134: # In the CVS repository, documentation of this algorithm is included 
                   2135: # in /doc/lonnavdocs, as a PDF and .tex source. Markers like **1**
                   2136: # will reference the same location in the text as the part of the
                   2137: # algorithm is running through.
                   2138: 
1.94      bowersj2 2139: sub new {
                   2140:     # magic invocation to create a class instance
                   2141:     my $proto = shift;
                   2142:     my $class = ref($proto) || $proto;
                   2143:     my $self = {};
                   2144: 
                   2145:     $self->{NAV_MAP} = shift;
                   2146:     return undef unless ($self->{NAV_MAP});
                   2147: 
                   2148:     # Handle the parameters
                   2149:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   2150:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   2151: 
                   2152:     # If the given resources are just the ID of the resource, get the
                   2153:     # objects
                   2154:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   2155:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   2156:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   2157:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   2158: 
                   2159:     $self->{FILTER} = shift;
                   2160: 
                   2161:     # A hash, used as a set, of resource already seen
                   2162:     $self->{ALREADY_SEEN} = shift;
                   2163:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
                   2164:     $self->{CONDITION} = shift;
                   2165: 
1.116     bowersj2 2166:     # Do we want to automatically follow "redirection" maps?
                   2167:     $self->{FORCE_TOP} = shift;
                   2168: 
1.162     bowersj2 2169:     # Do we want to return the top-level map object (resource 0.0)?
                   2170:     $self->{RETURN_0} = shift;
                   2171:     # have we done that yet?
                   2172:     $self->{HAVE_RETURNED_0} = 0;
                   2173: 
1.94      bowersj2 2174:     # Now, we need to pre-process the map, by walking forward and backward
                   2175:     # over the parts of the map we're going to look at.
1.96      bowersj2 2176: 
1.97      bowersj2 2177:     # The processing steps are exactly the same, except for a few small 
                   2178:     # changes, so I bundle those up in the following list of two elements:
                   2179:     # (direction_to_iterate, VAL_name, next_resource_method_to_call,
                   2180:     # first_resource).
                   2181:     # This prevents writing nearly-identical code twice.
                   2182:     my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext', 
                   2183:                         'FIRST_RESOURCE'],
                   2184:                        [BACKWARD(), 'BOT_UP_VAL', 'getPrevious', 
                   2185:                         'FINISH_RESOURCE'] );
                   2186: 
1.98      bowersj2 2187:     my $maxDepth = 0; # tracks max depth
                   2188: 
1.116     bowersj2 2189:     # If there is only one resource in this map, and it's a map, we
                   2190:     # want to remember that, so the user can ask for the first map
                   2191:     # that isn't just a redirector.
                   2192:     my $resource; my $resourceCount = 0;
                   2193: 
1.107     bowersj2 2194:     # **1**
                   2195: 
1.97      bowersj2 2196:     foreach my $pass (@iterations) {
                   2197:         my $direction = $pass->[0];
                   2198:         my $valName = $pass->[1];
                   2199:         my $nextResourceMethod = $pass->[2];
                   2200:         my $firstResourceName = $pass->[3];
                   2201: 
                   2202:         my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP}, 
                   2203:                                                             $self->{FIRST_RESOURCE},
                   2204:                                                             $self->{FINISH_RESOURCE},
                   2205:                                                             {}, undef, 0, $direction);
1.96      bowersj2 2206:     
1.97      bowersj2 2207:         # prime the recursion
                   2208:         $self->{$firstResourceName}->{DATA}->{$valName} = 0;
1.98      bowersj2 2209:         my $depth = 0;
1.97      bowersj2 2210:         $iterator->next();
                   2211:         my $curRes = $iterator->next();
1.98      bowersj2 2212:         while ($depth > -1) {
1.97      bowersj2 2213:             if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
                   2214:             if ($curRes == $iterator->END_MAP()) { $depth--; }
1.96      bowersj2 2215:         
1.97      bowersj2 2216:             if (ref($curRes)) {
1.116     bowersj2 2217:                 # If there's only one resource, this will save it
1.117     bowersj2 2218:                 # we have to filter empty resources from consideration here,
                   2219:                 # or even "empty", redirecting maps have two (start & finish)
                   2220:                 # or three (start, finish, plus redirector)
                   2221:                 if($direction == FORWARD && $curRes->src()) { 
                   2222:                     $resource = $curRes; $resourceCount++; 
                   2223:                 }
1.97      bowersj2 2224:                 my $resultingVal = $curRes->{DATA}->{$valName};
                   2225:                 my $nextResources = $curRes->$nextResourceMethod();
1.116     bowersj2 2226:                 my $nextCount = scalar(@{$nextResources});
1.104     bowersj2 2227: 
1.116     bowersj2 2228:                 if ($nextCount == 1) { # **3**
1.97      bowersj2 2229:                     my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
                   2230:                     $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
                   2231:                 }
                   2232:                 
1.116     bowersj2 2233:                 if ($nextCount > 1) { # **4**
1.97      bowersj2 2234:                     foreach my $res (@{$nextResources}) {
                   2235:                         my $current = $res->{DATA}->{$valName} || 999999999;
                   2236:                         $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
                   2237:                     }
                   2238:                 }
                   2239:             }
1.96      bowersj2 2240:             
1.107     bowersj2 2241:             # Assign the final val (**2**)
1.97      bowersj2 2242:             if (ref($curRes) && $direction == BACKWARD()) {
1.98      bowersj2 2243:                 my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
                   2244:                                      $curRes->{DATA}->{BOT_UP_VAL});
                   2245:                 
                   2246:                 $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
                   2247:                 if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
1.190   ! bowersj2 2248:             }
        !          2249:         } continue {
1.97      bowersj2 2250:             $curRes = $iterator->next();
1.96      bowersj2 2251:         }
                   2252:     }
1.94      bowersj2 2253: 
1.116     bowersj2 2254:     # Check: Was this only one resource, a map?
                   2255:     if ($resourceCount == 1 && $resource->is_map() && !$self->{FORCE_TOP}) { 
                   2256:         my $firstResource = $resource->map_start();
                   2257:         my $finishResource = $resource->map_finish();
                   2258:         return 
                   2259:             Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
                   2260:                                               $finishResource, $self->{FILTER},
                   2261:                                               $self->{ALREADY_SEEN}, 
                   2262:                                               $self->{CONDITION}, 0);
                   2263:         
                   2264:     }
                   2265: 
1.98      bowersj2 2266:     # Set up some bookkeeping information.
                   2267:     $self->{CURRENT_DEPTH} = 0;
                   2268:     $self->{MAX_DEPTH} = $maxDepth;
                   2269:     $self->{STACK} = [];
                   2270:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2271: 
                   2272:     for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
                   2273:         push @{$self->{STACK}}, [];
                   2274:     }
                   2275: 
1.107     bowersj2 2276:     # Prime the recursion w/ the first resource **5**
1.98      bowersj2 2277:     push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
                   2278:     $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
                   2279: 
                   2280:     bless ($self);
                   2281: 
                   2282:     return $self;
                   2283: }
                   2284: 
                   2285: sub next {
                   2286:     my $self = shift;
1.162     bowersj2 2287: 
                   2288:     # If we want to return the top-level map object, and haven't yet,
                   2289:     # do so.
                   2290:     if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0}) {
                   2291:         $self->{HAVE_RETURNED_0} = 1;
                   2292:         return $self->{NAV_MAP}->getById('0.0');
                   2293:     }
1.98      bowersj2 2294: 
                   2295:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
                   2296:         # grab the next from the recursive iterator 
                   2297:         my $next = $self->{RECURSIVE_ITERATOR}->next();
                   2298: 
                   2299:         # is it a begin or end map? If so, update the depth
                   2300:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
                   2301:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
                   2302: 
                   2303:         # Are we back at depth 0? If so, stop recursing
                   2304:         if ($self->{RECURSIVE_DEPTH} == 0) {
                   2305:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2306:         }
                   2307: 
                   2308:         return $next;
                   2309:     }
                   2310: 
                   2311:     if (defined($self->{FORCE_NEXT})) {
                   2312:         my $tmp = $self->{FORCE_NEXT};
                   2313:         $self->{FORCE_NEXT} = undef;
                   2314:         return $tmp;
                   2315:     }
                   2316: 
                   2317:     # Have we not yet begun? If not, return BEGIN_MAP and
                   2318:     # remember we've started.
                   2319:     if ( !$self->{STARTED} ) { 
                   2320:         $self->{STARTED} = 1;
                   2321:         return $self->BEGIN_MAP();
                   2322:     }
                   2323: 
                   2324:     # Here's the guts of the iterator.
                   2325:     
                   2326:     # Find the next resource, if any.
                   2327:     my $found = 0;
                   2328:     my $i = $self->{MAX_DEPTH};
                   2329:     my $newDepth;
                   2330:     my $here;
                   2331:     while ( $i >= 0 && !$found ) {
1.107     bowersj2 2332:         if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
                   2333:             $here = pop @{$self->{STACK}->[$i]}; # **7**
1.98      bowersj2 2334:             $found = 1;
                   2335:             $newDepth = $i;
                   2336:         }
                   2337:         $i--;
                   2338:     }
                   2339: 
                   2340:     # If we still didn't find anything, we're done.
                   2341:     if ( !$found ) {
                   2342:         # We need to get back down to the correct branch depth
                   2343:         if ( $self->{CURRENT_DEPTH} > 0 ) {
                   2344:             $self->{CURRENT_DEPTH}--;
                   2345:             return END_BRANCH();
                   2346:         } else {
                   2347:             return END_MAP();
                   2348:         }
                   2349:     }
                   2350: 
1.104     bowersj2 2351:     # If this is not a resource, it must be an END_BRANCH marker we want
                   2352:     # to return directly.
1.107     bowersj2 2353:     if (!ref($here)) { # **8**
1.104     bowersj2 2354:         if ($here == END_BRANCH()) { # paranoia, in case of later extension
                   2355:             $self->{CURRENT_DEPTH}--;
                   2356:             return $here;
                   2357:         }
                   2358:     }
                   2359: 
                   2360:     # Otherwise, it is a resource and it's safe to store in $self->{HERE}
                   2361:     $self->{HERE} = $here;
                   2362: 
1.98      bowersj2 2363:     # Get to the right level
                   2364:     if ( $self->{CURRENT_DEPTH} > $newDepth ) {
                   2365:         push @{$self->{STACK}->[$newDepth]}, $here;
                   2366:         $self->{CURRENT_DEPTH}--;
                   2367:         return END_BRANCH();
                   2368:     }
                   2369:     if ( $self->{CURRENT_DEPTH} < $newDepth) {
                   2370:         push @{$self->{STACK}->[$newDepth]}, $here;
                   2371:         $self->{CURRENT_DEPTH}++;
                   2372:         return BEGIN_BRANCH();
                   2373:     }
                   2374: 
                   2375:     # If we made it here, we have the next resource, and we're at the
                   2376:     # right branch level. So let's examine the resource for where
                   2377:     # we can get to from here.
                   2378: 
                   2379:     # So we need to look at all the resources we can get to from here,
                   2380:     # categorize them if we haven't seen them, remember if we have a new
                   2381:     my $nextUnfiltered = $here->getNext();
1.104     bowersj2 2382:     my $maxDepthAdded = -1;
                   2383:     
1.98      bowersj2 2384:     for (@$nextUnfiltered) {
                   2385:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
1.104     bowersj2 2386:             my $depth = $_->{DATA}->{DISPLAY_DEPTH};
                   2387:             push @{$self->{STACK}->[$depth]}, $_;
1.98      bowersj2 2388:             $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
1.104     bowersj2 2389:             if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
1.98      bowersj2 2390:         }
                   2391:     }
1.104     bowersj2 2392: 
                   2393:     # Is this the end of a branch? If so, all of the resources examined above
                   2394:     # led to lower levels then the one we are currently at, so we push a END_BRANCH
                   2395:     # marker onto the stack so we don't forget.
                   2396:     # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
                   2397:     # BC branch and gets to C, it will see F as the only next resource, but it's
                   2398:     # one level lower. Thus, this is the end of the branch, since there are no
                   2399:     # more resources added to this level or above.
1.111     bowersj2 2400:     # We don't do this if the examined resource is the finish resource,
                   2401:     # because the condition given above is true, but the "END_MAP" will
                   2402:     # take care of things and we should already be at depth 0.
1.104     bowersj2 2403:     my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
1.111     bowersj2 2404:     if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
1.104     bowersj2 2405:         push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
                   2406:     }
                   2407: 
1.98      bowersj2 2408:     # That ends the main iterator logic. Now, do we want to recurse
                   2409:     # down this map (if this resource is a map)?
                   2410:     if ($self->{HERE}->is_map() &&
                   2411:         (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) {
                   2412:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
                   2413:         my $firstResource = $self->{HERE}->map_start();
                   2414:         my $finishResource = $self->{HERE}->map_finish();
                   2415: 
                   2416:         $self->{RECURSIVE_ITERATOR} = 
                   2417:             Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
                   2418:                                               $finishResource, $self->{FILTER},
                   2419:                                               $self->{ALREADY_SEEN}, $self->{CONDITION});
                   2420:     }
                   2421: 
1.116     bowersj2 2422:     # If this is a blank resource, don't actually return it.
1.117     bowersj2 2423:     # Should you ever find you need it, make sure to add an option to the code
                   2424:     #  that you can use; other things depend on this behavior.
1.138     bowersj2 2425:     my $browsePriv = $self->{HERE}->browsePriv();
                   2426:     if (!$self->{HERE}->src() || 
                   2427:         (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
1.116     bowersj2 2428:         return $self->next();
                   2429:     }
                   2430: 
1.98      bowersj2 2431:     return $self->{HERE};
                   2432: 
                   2433: }
                   2434: 
                   2435: =pod
                   2436: 
1.174     albertel 2437: The other method available on the iterator is B<getStack>, which
                   2438: returns an array populated with the current 'stack' of maps, as
                   2439: references to the resource objects. Example: This is useful when
                   2440: making the navigation map, as we need to check whether we are under a
                   2441: page map to see if we need to link directly to the resource, or to the
                   2442: page. The first elements in the array will correspond to the top of
                   2443: the stack (most inclusive map).
1.98      bowersj2 2444: 
                   2445: =cut
                   2446: 
                   2447: sub getStack {
                   2448:     my $self=shift;
                   2449: 
                   2450:     my @stack;
                   2451: 
                   2452:     $self->populateStack(\@stack);
                   2453: 
                   2454:     return \@stack;
                   2455: }
                   2456: 
                   2457: # Private method: Calls the iterators recursively to populate the stack.
                   2458: sub populateStack {
                   2459:     my $self=shift;
                   2460:     my $stack = shift;
                   2461: 
                   2462:     push @$stack, $self->{HERE} if ($self->{HERE});
                   2463: 
                   2464:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
                   2465:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
                   2466:     }
1.94      bowersj2 2467: }
                   2468: 
                   2469: 1;
                   2470: 
                   2471: package Apache::lonnavmaps::DFSiterator;
                   2472: 
1.100     bowersj2 2473: # Not documented in the perldoc: This is a simple iterator that just walks
                   2474: #  through the nav map and presents the resources in a depth-first search
                   2475: #  fashion, ignorant of conditionals, randomized resources, etc. It presents
                   2476: #  BEGIN_MAP and END_MAP, but does not understand branches at all. It is
                   2477: #  useful for pre-processing of some kind, and is in fact used by the main
                   2478: #  iterator that way, but that's about it.
                   2479: # One could imagine merging this into the init routine of the main iterator,
                   2480: #  but this might as well be left seperate, since it is possible some other
                   2481: #  use might be found for it. - Jeremy
1.94      bowersj2 2482: 
1.117     bowersj2 2483: # Unlike the main iterator, this DOES return all resources, even blank ones.
                   2484: #  The main iterator needs them to correctly preprocess the map.
                   2485: 
1.94      bowersj2 2486: sub BEGIN_MAP { return 1; }    # begining of a new map
                   2487: sub END_MAP { return 2; }      # end of the map
                   2488: sub FORWARD { return 1; }      # go forward
                   2489: sub BACKWARD { return 2; }
                   2490: 
1.100     bowersj2 2491: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
                   2492: #         filter hash ref (or undef), already seen hash or undef, condition
                   2493: #         (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
1.51      bowersj2 2494: sub new {
                   2495:     # magic invocation to create a class instance
                   2496:     my $proto = shift;
                   2497:     my $class = ref($proto) || $proto;
                   2498:     my $self = {};
                   2499: 
                   2500:     $self->{NAV_MAP} = shift;
                   2501:     return undef unless ($self->{NAV_MAP});
                   2502: 
                   2503:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
                   2504:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
                   2505: 
                   2506:     # If the given resources are just the ID of the resource, get the
                   2507:     # objects
                   2508:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
                   2509:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
                   2510:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
                   2511:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
                   2512: 
                   2513:     $self->{FILTER} = shift;
                   2514: 
                   2515:     # A hash, used as a set, of resource already seen
                   2516:     $self->{ALREADY_SEEN} = shift;
1.140     bowersj2 2517:      if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
1.63      bowersj2 2518:     $self->{CONDITION} = shift;
1.89      bowersj2 2519:     $self->{DIRECTION} = shift || FORWARD();
1.51      bowersj2 2520: 
1.100     bowersj2 2521:     # Flag: Have we started yet?
1.51      bowersj2 2522:     $self->{STARTED} = 0;
                   2523: 
                   2524:     # Should we continue calling the recursive iterator, if any?
                   2525:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2526:     # The recursive iterator, if any
                   2527:     $self->{RECURSIVE_ITERATOR} = undef;
                   2528:     # Are we recursing on a map, or a branch?
                   2529:     $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
                   2530:     # And the count of how deep it is, so that this iterator can keep track of
                   2531:     # when to pick back up again.
                   2532:     $self->{RECURSIVE_DEPTH} = 0;
                   2533: 
                   2534:     # For keeping track of our branches, we maintain our own stack
1.100     bowersj2 2535:     $self->{STACK} = [];
1.51      bowersj2 2536: 
                   2537:     # Start with the first resource
1.89      bowersj2 2538:     if ($self->{DIRECTION} == FORWARD) {
1.100     bowersj2 2539:         push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
1.89      bowersj2 2540:     } else {
1.100     bowersj2 2541:         push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
1.89      bowersj2 2542:     }
1.51      bowersj2 2543: 
                   2544:     bless($self);
                   2545:     return $self;
                   2546: }
                   2547: 
                   2548: sub next {
                   2549:     my $self = shift;
                   2550:     
                   2551:     # Are we using a recursive iterator? If so, pull from that and
                   2552:     # watch the depth; we want to resume our level at the correct time.
1.98      bowersj2 2553:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
1.51      bowersj2 2554:         # grab the next from the recursive iterator
                   2555:         my $next = $self->{RECURSIVE_ITERATOR}->next();
                   2556:         
                   2557:         # is it a begin or end map? Update depth if so
                   2558:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
                   2559:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
                   2560: 
                   2561:         # Are we back at depth 0? If so, stop recursing.
                   2562:         if ($self->{RECURSIVE_DEPTH} == 0) {
                   2563:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
                   2564:         }
                   2565:         
                   2566:         return $next;
                   2567:     }
                   2568: 
                   2569:     # Is there a current resource to grab? If not, then return
1.100     bowersj2 2570:     # END_MAP, which will end the iterator.
                   2571:     if (scalar(@{$self->{STACK}}) == 0) {
                   2572:         return $self->END_MAP();
1.51      bowersj2 2573:     }
                   2574: 
                   2575:     # Have we not yet begun? If not, return BEGIN_MAP and 
                   2576:     # remember that we've started.
                   2577:     if ( !$self->{STARTED} ) {
                   2578:         $self->{STARTED} = 1;
                   2579:         return $self->BEGIN_MAP;
                   2580:     }
                   2581: 
                   2582:     # Get the next resource in the branch
1.100     bowersj2 2583:     $self->{HERE} = pop @{$self->{STACK}};
1.52      bowersj2 2584: 
1.100     bowersj2 2585:     # remember that we've seen this, so we don't return it again later
1.51      bowersj2 2586:     $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
                   2587:     
                   2588:     # Get the next possible resources
1.90      bowersj2 2589:     my $nextUnfiltered;
1.89      bowersj2 2590:     if ($self->{DIRECTION} == FORWARD()) {
1.90      bowersj2 2591:         $nextUnfiltered = $self->{HERE}->getNext();
1.89      bowersj2 2592:     } else {
1.90      bowersj2 2593:         $nextUnfiltered = $self->{HERE}->getPrevious();
1.89      bowersj2 2594:     }
1.51      bowersj2 2595:     my $next = [];
                   2596: 
                   2597:     # filter the next possibilities to remove things we've 
1.100     bowersj2 2598:     # already seen.
1.51      bowersj2 2599:     foreach (@$nextUnfiltered) {
1.52      bowersj2 2600:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
                   2601:             push @$next, $_;
                   2602:         }
1.51      bowersj2 2603:     }
                   2604: 
                   2605:     while (@$next) {
1.100     bowersj2 2606:         # copy the next possibilities over to the stack
                   2607:         push @{$self->{STACK}}, shift @$next;
1.51      bowersj2 2608:     }
                   2609: 
                   2610:     # If this is a map and we want to recurse down it... (not filtered out)
1.70      bowersj2 2611:     if ($self->{HERE}->is_map() && 
1.63      bowersj2 2612:          (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) { 
1.51      bowersj2 2613:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
                   2614:         my $firstResource = $self->{HERE}->map_start();
                   2615:         my $finishResource = $self->{HERE}->map_finish();
                   2616: 
                   2617:         $self->{RECURSIVE_ITERATOR} =
1.94      bowersj2 2618:           Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource, 
1.63      bowersj2 2619:                      $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
1.91      bowersj2 2620:                                              $self->{CONDITION}, $self->{DIRECTION});
1.51      bowersj2 2621:     }
                   2622: 
                   2623:     return $self->{HERE};
1.190   ! bowersj2 2624: }
        !          2625: 
        !          2626: # Identical to the full iterator methods of the same name. Hate to copy/paste
        !          2627: # but I also hate to "inherit" either iterator from the other.
        !          2628: 
        !          2629: sub getStack {
        !          2630:     my $self=shift;
        !          2631: 
        !          2632:     my @stack;
        !          2633: 
        !          2634:     $self->populateStack(\@stack);
        !          2635: 
        !          2636:     return \@stack;
        !          2637: }
        !          2638: 
        !          2639: # Private method: Calls the iterators recursively to populate the stack.
        !          2640: sub populateStack {
        !          2641:     my $self=shift;
        !          2642:     my $stack = shift;
        !          2643: 
        !          2644:     push @$stack, $self->{HERE} if ($self->{HERE});
        !          2645: 
        !          2646:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
        !          2647:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
        !          2648:     }
1.51      bowersj2 2649: }
                   2650: 
1.1       www      2651: 1;
1.2       www      2652: 
1.51      bowersj2 2653: package Apache::lonnavmaps::resource;
                   2654: 
                   2655: use Apache::lonnet;
                   2656: 
                   2657: =pod
                   2658: 
                   2659: =head1 Object: resource
                   2660: 
1.174     albertel 2661: A resource object encapsulates a resource in a resource map, allowing
                   2662: easy manipulation of the resource, querying the properties of the
                   2663: resource (including user properties), and represents a reference that
                   2664: can be used as the canonical representation of the resource by
                   2665: lonnavmap clients like renderers.
                   2666: 
                   2667: A resource only makes sense in the context of a navmap, as some of the
                   2668: data is stored in the navmap object.
                   2669: 
                   2670: You will probably never need to instantiate this object directly. Use
                   2671: Apache::lonnavmaps::navmap, and use the "start" method to obtain the
                   2672: starting resource.
1.51      bowersj2 2673: 
1.188     bowersj2 2674: Resource objects respect the parameter_hiddenparts, which suppresses 
                   2675: various parts according to the wishes of the map author. As of this
                   2676: writing, there is no way to override this parameter, and suppressed
                   2677: parts will never be returned, nor will their response types or ids be
                   2678: stored.
                   2679: 
1.51      bowersj2 2680: =head2 Public Members
                   2681: 
1.174     albertel 2682: resource objects have a hash called DATA ($resourceRef->{DATA}) that
                   2683: you can store whatever you want in. This allows you to easily do
                   2684: two-pass algorithms without worrying about managing your own
                   2685: resource->data hash.
1.51      bowersj2 2686: 
                   2687: =head2 Methods
                   2688: 
                   2689: =over 4
                   2690: 
1.174     albertel 2691: =item * B<new>($navmapRef, $idString):
                   2692: 
                   2693: The first arg is a reference to the parent navmap object. The second
                   2694: is the idString of the resource itself. Very rarely, if ever, called
                   2695: directly. Use the nav map->getByID() method.
1.70      bowersj2 2696: 
                   2697: =back
1.51      bowersj2 2698: 
                   2699: =cut
                   2700: 
                   2701: sub new {
                   2702:     # magic invocation to create a class instance
                   2703:     my $proto = shift;
                   2704:     my $class = ref($proto) || $proto;
                   2705:     my $self = {};
                   2706: 
                   2707:     $self->{NAV_MAP} = shift;
                   2708:     $self->{ID} = shift;
                   2709: 
                   2710:     # Store this new resource in the parent nav map's cache.
                   2711:     $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
1.66      bowersj2 2712:     $self->{RESOURCE_ERROR} = 0;
1.51      bowersj2 2713: 
                   2714:     # A hash that can be used by two-pass algorithms to store data
                   2715:     # about this resource in. Not used by the resource object
                   2716:     # directly.
                   2717:     $self->{DATA} = {};
                   2718:    
                   2719:     bless($self);
                   2720:     
                   2721:     return $self;
                   2722: }
                   2723: 
1.70      bowersj2 2724: # private function: simplify the NAV_HASH lookups we keep doing
                   2725: # pass the name, and to automatically append my ID, pass a true val on the
                   2726: # second param
                   2727: sub navHash {
                   2728:     my $self = shift;
                   2729:     my $param = shift;
                   2730:     my $id = shift;
1.115     bowersj2 2731:     return $self->{NAV_MAP}->navhash($param . ($id?$self->{ID}:""));
1.70      bowersj2 2732: }
                   2733: 
1.51      bowersj2 2734: =pod
                   2735: 
1.70      bowersj2 2736: B<Metadata Retreival>
                   2737: 
1.174     albertel 2738: These are methods that help you retrieve metadata about the resource:
                   2739: Method names are based on the fields in the compiled course
                   2740: representation.
1.70      bowersj2 2741: 
                   2742: =over 4
                   2743: 
1.174     albertel 2744: =item * B<compTitle>:
                   2745: 
                   2746: Returns a "composite title", that is equal to $res->title() if the
                   2747: resource has a title, and is otherwise the last part of the URL (e.g.,
                   2748: "problem.problem").
                   2749: 
                   2750: =item * B<ext>:
                   2751: 
                   2752: Returns true if the resource is external.
                   2753: 
                   2754: =item * B<goesto>:
                   2755: 
                   2756: Returns the "goesto" value from the compiled nav map. (It is likely
                   2757: you want to use B<getNext> instead.)
                   2758: 
                   2759: =item * B<kind>:
                   2760: 
                   2761: Returns the kind of the resource from the compiled nav map.
                   2762: 
                   2763: =item * B<randomout>:
1.106     bowersj2 2764: 
1.174     albertel 2765: Returns true if this resource was chosen to NOT be shown to the user
                   2766: by the random map selection feature. In other words, this is usually
                   2767: false.
1.70      bowersj2 2768: 
1.174     albertel 2769: =item * B<randompick>:
1.70      bowersj2 2770: 
1.174     albertel 2771: Returns true for a map if the randompick feature is being used on the
                   2772: map. (?)
1.70      bowersj2 2773: 
1.174     albertel 2774: =item * B<src>:
1.51      bowersj2 2775: 
1.174     albertel 2776: Returns the source for the resource.
1.70      bowersj2 2777: 
1.174     albertel 2778: =item * B<symb>:
1.70      bowersj2 2779: 
1.174     albertel 2780: Returns the symb for the resource.
1.70      bowersj2 2781: 
1.174     albertel 2782: =item * B<title>:
1.70      bowersj2 2783: 
1.174     albertel 2784: Returns the title of the resource.
                   2785: 
                   2786: =item * B<to>:
                   2787: 
                   2788: Returns the "to" value from the compiled nav map. (It is likely you
                   2789: want to use B<getNext> instead.)
1.51      bowersj2 2790: 
                   2791: =back
                   2792: 
                   2793: =cut
                   2794: 
                   2795: # These info functions can be used directly, as they don't return
                   2796: # resource information.
1.85      bowersj2 2797: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
1.70      bowersj2 2798: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
1.85      bowersj2 2799: sub from { my $self=shift; return $self->navHash("from_", 1); }
1.51      bowersj2 2800: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
                   2801: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
1.68      bowersj2 2802: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
                   2803: sub randompick { 
                   2804:     my $self = shift;
                   2805:     return $self->{NAV_MAP}->{PARM_HASH}->{$self->symb .
                   2806:                                                '.0.parameter_randompick'};
                   2807: }
1.51      bowersj2 2808: sub src { 
                   2809:     my $self=shift;
                   2810:     return $self->navHash("src_", 1);
                   2811: }
                   2812: sub symb {
                   2813:     my $self=shift;
                   2814:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
                   2815:     my $symbSrc = &Apache::lonnet::declutter($self->src());
                   2816:     return &Apache::lonnet::declutter(
                   2817:          $self->navHash('map_id_'.$first)) 
                   2818:         . '___' . $second . '___' . $symbSrc;
                   2819: }
1.70      bowersj2 2820: sub title { my $self=shift; return $self->navHash("title_", 1); }
                   2821: sub to { my $self=shift; return $self->navHash("to_", 1); }
1.106     bowersj2 2822: sub compTitle {
                   2823:     my $self = shift;
                   2824:     my $title = $self->title();
1.176     www      2825:     $title=~s/\&colon\;/\:/gs;
1.106     bowersj2 2826:     if (!$title) {
                   2827:         $title = $self->src();
                   2828:         $title = substr($title, rindex($title, '/') + 1);
                   2829:     }
                   2830:     return $title;
                   2831: }
1.70      bowersj2 2832: =pod
                   2833: 
                   2834: B<Predicate Testing the Resource>
                   2835: 
                   2836: These methods are shortcuts to deciding if a given resource has a given property.
                   2837: 
                   2838: =over 4
                   2839: 
1.174     albertel 2840: =item * B<is_map>:
                   2841: 
                   2842: Returns true if the resource is a map type.
                   2843: 
                   2844: =item * B<is_problem>:
1.70      bowersj2 2845: 
1.174     albertel 2846: Returns true if the resource is a problem type, false
                   2847: otherwise. (Looks at the extension on the src field; might need more
                   2848: to work correctly.)
1.70      bowersj2 2849: 
1.174     albertel 2850: =item * B<is_page>:
1.70      bowersj2 2851: 
1.174     albertel 2852: Returns true if the resource is a page.
                   2853: 
                   2854: =item * B<is_sequence>:
                   2855: 
                   2856: Returns true if the resource is a sequence.
1.70      bowersj2 2857: 
                   2858: =back
                   2859: 
                   2860: =cut
                   2861: 
                   2862: 
                   2863: sub is_html {
1.51      bowersj2 2864:     my $self=shift;
                   2865:     my $src = $self->src();
1.70      bowersj2 2866:     return ($src =~ /html$/);
1.51      bowersj2 2867: }
1.70      bowersj2 2868: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
                   2869: sub is_page {
1.51      bowersj2 2870:     my $self=shift;
                   2871:     my $src = $self->src();
1.70      bowersj2 2872:     return ($src =~ /page$/);
1.51      bowersj2 2873: }
1.70      bowersj2 2874: sub is_problem {
1.51      bowersj2 2875:     my $self=shift;
                   2876:     my $src = $self->src();
1.70      bowersj2 2877:     return ($src =~ /problem$/);
1.51      bowersj2 2878: }
1.70      bowersj2 2879: sub is_sequence {
1.51      bowersj2 2880:     my $self=shift;
                   2881:     my $src = $self->src();
1.70      bowersj2 2882:     return ($src =~ /sequence$/);
1.51      bowersj2 2883: }
                   2884: 
1.101     bowersj2 2885: # Private method: Shells out to the parmval in the nav map, handler parts.
1.51      bowersj2 2886: sub parmval {
                   2887:     my $self = shift;
                   2888:     my $what = shift;
1.185     bowersj2 2889:     my $part = shift;
                   2890:     if (!defined($part)) { 
                   2891:         $part = '0'; 
                   2892:     }
1.51      bowersj2 2893:     return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->symb());
                   2894: }
                   2895: 
1.70      bowersj2 2896: =pod
                   2897: 
                   2898: B<Map Methods>
                   2899: 
1.174     albertel 2900: These methods are useful for getting information about the map
                   2901: properties of the resource, if the resource is a map (B<is_map>).
1.70      bowersj2 2902: 
                   2903: =over 4
                   2904: 
1.174     albertel 2905: =item * B<map_finish>:
                   2906: 
                   2907: Returns a reference to a resource object corresponding to the finish
                   2908: resource of the map.
1.70      bowersj2 2909: 
1.174     albertel 2910: =item * B<map_pc>:
1.70      bowersj2 2911: 
1.174     albertel 2912: Returns the pc value of the map, which is the first number that
                   2913: appears in the resource ID of the resources in the map, and is the
                   2914: number that appears around the middle of the symbs of the resources in
                   2915: that map.
1.70      bowersj2 2916: 
1.174     albertel 2917: =item * B<map_start>:
                   2918: 
                   2919: Returns a reference to a resource object corresponding to the start
                   2920: resource of the map.
                   2921: 
                   2922: =item * B<map_type>:
                   2923: 
                   2924: Returns a string with the type of the map in it.
1.70      bowersj2 2925: 
                   2926: =back
1.51      bowersj2 2927: 
1.70      bowersj2 2928: =cut
1.51      bowersj2 2929: 
                   2930: sub map_finish {
                   2931:     my $self = shift;
                   2932:     my $src = $self->src();
1.144     bowersj2 2933:     $src = Apache::lonnet::clutter($src);
1.51      bowersj2 2934:     my $res = $self->navHash("map_finish_$src", 0);
                   2935:     $res = $self->{NAV_MAP}->getById($res);
                   2936:     return $res;
                   2937: }
1.70      bowersj2 2938: sub map_pc {
                   2939:     my $self = shift;
                   2940:     my $src = $self->src();
                   2941:     return $self->navHash("map_pc_$src", 0);
                   2942: }
1.51      bowersj2 2943: sub map_start {
                   2944:     my $self = shift;
                   2945:     my $src = $self->src();
1.144     bowersj2 2946:     $src = Apache::lonnet::clutter($src);
1.51      bowersj2 2947:     my $res = $self->navHash("map_start_$src", 0);
                   2948:     $res = $self->{NAV_MAP}->getById($res);
                   2949:     return $res;
                   2950: }
                   2951: sub map_type {
                   2952:     my $self = shift;
                   2953:     my $pc = $self->map_pc();
                   2954:     return $self->navHash("map_type_$pc", 0);
                   2955: }
                   2956: 
                   2957: 
                   2958: 
                   2959: #####
                   2960: # Property queries
                   2961: #####
                   2962: 
                   2963: # These functions will be responsible for returning the CORRECT
                   2964: # VALUE for the parameter, no matter what. So while they may look
                   2965: # like direct calls to parmval, they can be more then that.
                   2966: # So, for instance, the duedate function should use the "duedatetype"
                   2967: # information, rather then the resource object user.
                   2968: 
                   2969: =pod
                   2970: 
                   2971: =head2 Resource Parameters
                   2972: 
1.174     albertel 2973: In order to use the resource parameters correctly, the nav map must
                   2974: have been instantiated with genCourseAndUserOptions set to true, so
                   2975: the courseopt and useropt is read correctly. Then, you can call these
                   2976: functions to get the relevant parameters for the resource. Each
                   2977: function defaults to part "0", but can be directed to another part by
                   2978: passing the part as the parameter.
                   2979: 
                   2980: These methods are responsible for getting the parameter correct, not
                   2981: merely reflecting the contents of the GDBM hashes. As we move towards
                   2982: dates relative to other dates, these methods should be updated to
                   2983: reflect that. (Then, anybody using these methods will not have to update
                   2984: their code.)
                   2985: 
                   2986: =over 4
                   2987: 
                   2988: =item * B<acc>:
                   2989: 
                   2990: Get the Client IP/Name Access Control information.
1.51      bowersj2 2991: 
1.174     albertel 2992: =item * B<answerdate>:
1.51      bowersj2 2993: 
1.174     albertel 2994: Get the answer-reveal date for the problem.
                   2995: 
                   2996: =item * B<duedate>:
                   2997: 
                   2998: Get the due date for the problem.
                   2999: 
                   3000: =item * B<tries>:
                   3001: 
                   3002: Get the number of tries the student has used on the problem.
                   3003: 
                   3004: =item * B<maxtries>:
                   3005: 
                   3006: Get the number of max tries allowed.
                   3007: 
                   3008: =item * B<opendate>:
1.51      bowersj2 3009: 
1.174     albertel 3010: Get the open date for the problem.
1.51      bowersj2 3011: 
1.174     albertel 3012: =item * B<sig>:
1.51      bowersj2 3013: 
1.174     albertel 3014: Get the significant figures setting.
1.51      bowersj2 3015: 
1.174     albertel 3016: =item * B<tol>:
1.51      bowersj2 3017: 
1.174     albertel 3018: Get the tolerance for the problem.
1.51      bowersj2 3019: 
1.174     albertel 3020: =item * B<tries>:
1.51      bowersj2 3021: 
1.174     albertel 3022: Get the number of tries the user has already used on the problem.
1.51      bowersj2 3023: 
1.174     albertel 3024: =item * B<type>:
1.51      bowersj2 3025: 
1.174     albertel 3026: Get the question type for the problem.
1.70      bowersj2 3027: 
1.174     albertel 3028: =item * B<weight>:
1.51      bowersj2 3029: 
1.174     albertel 3030: Get the weight for the problem.
1.51      bowersj2 3031: 
                   3032: =back
                   3033: 
                   3034: =cut
                   3035: 
                   3036: sub acc {
                   3037:     (my $self, my $part) = @_;
                   3038:     return $self->parmval("acc", $part);
                   3039: }
                   3040: sub answerdate {
                   3041:     (my $self, my $part) = @_;
                   3042:     # Handle intervals
                   3043:     if ($self->parmval("answerdate.type", $part) eq 'date_interval') {
                   3044:         return $self->duedate($part) + 
                   3045:             $self->parmval("answerdate", $part);
                   3046:     }
                   3047:     return $self->parmval("answerdate", $part);
1.106     bowersj2 3048: }
1.108     bowersj2 3049: sub awarded { my $self = shift; return $self->queryRestoreHash('awarded', shift); }
1.51      bowersj2 3050: sub duedate {
                   3051:     (my $self, my $part) = @_;
                   3052:     return $self->parmval("duedate", $part);
                   3053: }
                   3054: sub maxtries {
                   3055:     (my $self, my $part) = @_;
                   3056:     return $self->parmval("maxtries", $part);
                   3057: }
                   3058: sub opendate {
                   3059:     (my $self, my $part) = @_;
                   3060:     if ($self->parmval("opendate.type", $part) eq 'date_interval') {
                   3061:         return $self->duedate($part) -
                   3062:             $self->parmval("opendate", $part);
                   3063:     }
                   3064:     return $self->parmval("opendate");
                   3065: }
1.185     bowersj2 3066: sub problemstatus {
                   3067:     (my $self, my $part) = @_;
                   3068:     return $self->parmval("problemstatus", $part);
                   3069: }
1.51      bowersj2 3070: sub sig {
                   3071:     (my $self, my $part) = @_;
                   3072:     return $self->parmval("sig", $part);
                   3073: }
                   3074: sub tol {
                   3075:     (my $self, my $part) = @_;
                   3076:     return $self->parmval("tol", $part);
                   3077: }
1.108     bowersj2 3078: sub tries { 
                   3079:     my $self = shift; 
                   3080:     my $tries = $self->queryRestoreHash('tries', shift);
                   3081:     if (!defined($tries)) { return '0';}
1.51      bowersj2 3082:     return $tries;
                   3083: }
1.70      bowersj2 3084: sub type {
                   3085:     (my $self, my $part) = @_;
                   3086:     return $self->parmval("type", $part);
                   3087: }
1.109     bowersj2 3088: sub weight { 
                   3089:     my $self = shift; my $part = shift;
1.70      bowersj2 3090:     return $self->parmval("weight", $part);
                   3091: }
1.51      bowersj2 3092: 
                   3093: # Multiple things need this
                   3094: sub getReturnHash {
                   3095:     my $self = shift;
                   3096:     
                   3097:     if (!defined($self->{RETURN_HASH})) {
1.84      bowersj2 3098:         my %tmpHash  = &Apache::lonnet::restore($self->symb());
1.51      bowersj2 3099:         $self->{RETURN_HASH} = \%tmpHash;
                   3100:     }
                   3101: }       
                   3102: 
                   3103: ######
                   3104: # Status queries
                   3105: ######
                   3106: 
                   3107: # These methods query the status of problems.
                   3108: 
                   3109: # If we need to count parts, this function determines the number of
                   3110: # parts from the metadata. When called, it returns a reference to a list
                   3111: # of strings corresponding to the parts. (Thus, using it in a scalar context
                   3112: # tells you how many parts you have in the problem:
                   3113: # $partcount = scalar($resource->countParts());
                   3114: # Don't use $self->{PARTS} directly because you don't know if it's been
                   3115: # computed yet.
                   3116: 
                   3117: =pod
                   3118: 
                   3119: =head2 Resource misc
                   3120: 
                   3121: Misc. functions for the resource.
                   3122: 
                   3123: =over 4
                   3124: 
1.174     albertel 3125: =item * B<hasDiscussion>:
1.59      bowersj2 3126: 
1.174     albertel 3127: Returns a false value if there has been discussion since the user last
                   3128: logged in, true if there has. Always returns false if the discussion
                   3129: data was not extracted when the nav map was constructed.
                   3130: 
                   3131: =item * B<getFeedback>:
                   3132: 
                   3133: Gets the feedback for the resource and returns the raw feedback string
                   3134: for the resource, or the null string if there is no feedback or the
                   3135: email data was not extracted when the nav map was constructed. Usually
                   3136: used like this:
1.59      bowersj2 3137: 
                   3138:  for (split(/\,/, $res->getFeedback())) {
                   3139:     my $link = &Apache::lonnet::escape($_);
                   3140:     ...
                   3141: 
                   3142: and use the link as appropriate.
                   3143: 
                   3144: =cut
                   3145: 
                   3146: sub hasDiscussion {
                   3147:     my $self = shift;
                   3148:     return $self->{NAV_MAP}->hasDiscussion($self->symb());
                   3149: }
                   3150: 
                   3151: sub getFeedback {
                   3152:     my $self = shift;
1.124     bowersj2 3153:     my $source = $self->src();
1.125     bowersj2 3154:     if ($source =~ /^\/res\//) { $source = substr $source, 5; }
1.124     bowersj2 3155:     return $self->{NAV_MAP}->getFeedback($source);
                   3156: }
                   3157: 
                   3158: sub getErrors {
                   3159:     my $self = shift;
                   3160:     my $source = $self->src();
                   3161:     if ($source =~ /^\/res\//) { $source = substr $source, 5; }
                   3162:     return $self->{NAV_MAP}->getErrors($source);
1.59      bowersj2 3163: }
                   3164: 
                   3165: =pod
                   3166: 
1.174     albertel 3167: =item * B<parts>():
1.51      bowersj2 3168: 
1.174     albertel 3169: Returns a list reference containing sorted strings corresponding to
                   3170: each part of the problem. To count the number of parts, use the list
                   3171: in a scalar context, and subtract one if greater than two. (One part
                   3172: problems have a part 0. Multi-parts have a part 0, plus a part for
                   3173: each part. Filtering part 0 if you want it is up to you.)
                   3174: 
                   3175: =item * B<countParts>():
                   3176: 
                   3177: Returns the number of parts of the problem a student can answer. Thus,
                   3178: for single part problems, returns 1. For multipart, it returns the
                   3179: number of parts in the problem, not including psuedo-part 0. Thus,
                   3180: B<parts> may return an array with fewer parts in it then countParts
                   3181: might lead you to believe.
1.51      bowersj2 3182: 
1.187     bowersj2 3183: =item * B<responseType>($part):
                   3184: 
                   3185: Returns the response type of the part, without the word "response" on the
                   3186: end. Example return values: 'string', 'essay', 'numeric', etc.
                   3187: 
                   3188: =item * B<responseId>($part):
                   3189: 
                   3190: Retreives the response ID for the given part, which may be an empty string.
                   3191: 
1.51      bowersj2 3192: =back
                   3193: 
                   3194: =cut
                   3195: 
                   3196: sub parts {
                   3197:     my $self = shift;
                   3198: 
1.67      bowersj2 3199:     if ($self->ext) { return ['0']; }
                   3200: 
1.51      bowersj2 3201:     $self->extractParts();
                   3202:     return $self->{PARTS};
                   3203: }
                   3204: 
                   3205: sub countParts {
                   3206:     my $self = shift;
                   3207:     
                   3208:     my $parts = $self->parts();
1.163     bowersj2 3209:     my $delta = 0;
                   3210:     for my $part (@$parts) {
                   3211:         if ($part eq '0') { $delta--; }
                   3212:     }
1.66      bowersj2 3213: 
                   3214:     if ($self->{RESOURCE_ERROR}) {
                   3215:         return 0;
                   3216:     }
                   3217: 
1.163     bowersj2 3218:     return scalar(@{$parts}) + $delta;
1.51      bowersj2 3219: }
                   3220: 
1.187     bowersj2 3221: sub responseType {
1.184     bowersj2 3222:     my $self = shift;
                   3223:     my $part = shift;
                   3224: 
                   3225:     $self->extractParts();
1.187     bowersj2 3226:     return $self->{RESPONSE_TYPE}->{$part};
                   3227: }
                   3228: 
                   3229: sub responseId {
                   3230:     my $self = shift;
                   3231:     my $part = shift;
                   3232: 
                   3233:     $self->extractParts();
                   3234:     return $self->{RESPONSE_IDS}->{$part};
1.184     bowersj2 3235: }
                   3236: 
                   3237: # Private function: Extracts the parts information, both part names and
1.187     bowersj2 3238: # part types, and saves it. 
1.51      bowersj2 3239: sub extractParts { 
                   3240:     my $self = shift;
                   3241:     
1.153     bowersj2 3242:     return if (defined($self->{PARTS}));
1.67      bowersj2 3243:     return if ($self->ext);
1.51      bowersj2 3244: 
                   3245:     $self->{PARTS} = [];
                   3246: 
1.181     bowersj2 3247:     my %parts;
                   3248: 
1.82      bowersj2 3249:     # Retrieve part count, if this is a problem
                   3250:     if ($self->is_problem()) {
1.152     matthew  3251:         my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
1.82      bowersj2 3252:         if (!$metadata) {
                   3253:             $self->{RESOURCE_ERROR} = 1;
                   3254:             $self->{PARTS} = [];
1.184     bowersj2 3255:             $self->{PART_TYPE} = {};
1.82      bowersj2 3256:             return;
                   3257:         }
                   3258:         foreach (split(/\,/,$metadata)) {
1.152     matthew  3259:             if ($_ =~ /^part_(.*)$/) {
1.147     bowersj2 3260:                 my $part = $1;
1.183     bowersj2 3261:                 # This floods the logs if it blows up
                   3262:                 if (defined($parts{$part})) {
                   3263:                     Apache::lonnet::logthis("$part multiply defined in metadata for " . $self->symb());
                   3264:                   }
1.181     bowersj2 3265: 
1.147     bowersj2 3266:                 # check to see if part is turned off.
1.181     bowersj2 3267: 
                   3268:                 if (!Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
                   3269:                     $parts{$part} = 1;
1.147     bowersj2 3270:                 }
1.82      bowersj2 3271:             }
1.51      bowersj2 3272:         }
1.82      bowersj2 3273:         
                   3274:         
1.181     bowersj2 3275:         my @sortedParts = sort keys %parts;
1.82      bowersj2 3276:         $self->{PARTS} = \@sortedParts;
1.187     bowersj2 3277: 
                   3278:         my %responseIdHash;
                   3279:         my %responseTypeHash;
                   3280: 
1.189     bowersj2 3281: 
                   3282:         # Init the responseIdHash
                   3283:         foreach (@{$self->{PARTS}}) {
                   3284:             $responseIdHash{$_} = [];
                   3285:         }
                   3286: 
1.187     bowersj2 3287:         # Now, the unfortunate thing about this is that parts, part name, and
                   3288:         # response if are delimited by underscores, but both the part
                   3289:         # name and response id can themselves have underscores in them.
                   3290:         # So we have to use our knowlege of part names to figure out 
                   3291:         # where the part names begin and end, and even then, it is possible
                   3292:         # to construct ambiguous situations.
                   3293:         foreach (split /,/, $metadata) {
                   3294:             if ($_ =~ /^([a-zA-Z]+)response_(.*)/) {
                   3295:                 my $responseType = $1;
                   3296:                 my $partStuff = $2;
                   3297:                 my $partIdSoFar = '';
                   3298:                 my @partChunks = split /_/, $partStuff;
                   3299:                 my $i = 0;
                   3300: 
                   3301:                 for ($i = 0; $i < scalar(@partChunks); $i++) {
                   3302:                     if ($partIdSoFar) { $partIdSoFar .= '_'; }
                   3303:                     $partIdSoFar .= $partChunks[$i];
                   3304:                     if ($parts{$partIdSoFar}) {
                   3305:                         my @otherChunks = @partChunks[$i+1..$#partChunks];
                   3306:                         my $responseId = join('_', @otherChunks);
1.188     bowersj2 3307:                         push @{$responseIdHash{$partIdSoFar}}, $responseId;
1.187     bowersj2 3308:                         $responseTypeHash{$partIdSoFar} = $responseType;
                   3309:                         last;
                   3310:                     }
                   3311:                 }
                   3312:             }
                   3313:         }
                   3314: 
                   3315:         $self->{RESPONSE_IDS} = \%responseIdHash;
                   3316:         $self->{RESPONSE_TYPES} = \%responseTypeHash;
1.154     bowersj2 3317:     }
                   3318: 
1.51      bowersj2 3319:     return;
                   3320: }
                   3321: 
                   3322: =pod
                   3323: 
                   3324: =head2 Resource Status
                   3325: 
1.174     albertel 3326: Problem resources have status information, reflecting their various
                   3327: dates and completion statuses.
1.51      bowersj2 3328: 
1.174     albertel 3329: There are two aspects to the status: the date-related information and
                   3330: the completion information.
1.51      bowersj2 3331: 
1.174     albertel 3332: Idiomatic usage of these two methods would probably look something
                   3333: like
1.51      bowersj2 3334: 
                   3335:  foreach ($resource->parts()) {
                   3336:     my $dateStatus = $resource->getDateStatus($_);
                   3337:     my $completionStatus = $resource->getCompletionStatus($_);
                   3338: 
1.70      bowersj2 3339:     or
                   3340: 
                   3341:     my $status = $resource->status($_);
                   3342: 
1.51      bowersj2 3343:     ... use it here ...
                   3344:  }
                   3345: 
1.174     albertel 3346: Which you use depends on exactly what you are looking for. The
                   3347: status() function has been optimized for the nav maps display and may
                   3348: not precisely match what you need elsewhere.
1.101     bowersj2 3349: 
1.174     albertel 3350: The symbolic constants shown below can be accessed through the
                   3351: resource object: C<$res->OPEN>.
1.101     bowersj2 3352: 
1.51      bowersj2 3353: =over 4
                   3354: 
1.174     albertel 3355: =item * B<getDateStatus>($part):
                   3356: 
                   3357: ($part defaults to 0). A convenience function that returns a symbolic
                   3358: constant telling you about the date status of the part. The possible
                   3359: return values are:
1.51      bowersj2 3360: 
                   3361: =back
                   3362: 
                   3363: B<Date Codes>
                   3364: 
                   3365: =over 4
                   3366: 
1.174     albertel 3367: =item * B<OPEN_LATER>:
                   3368: 
                   3369: The problem will be opened later.
                   3370: 
                   3371: =item * B<OPEN>:
                   3372: 
                   3373: Open and not yet due.
                   3374: 
1.51      bowersj2 3375: 
1.174     albertel 3376: =item * B<PAST_DUE_ANSWER_LATER>:
1.51      bowersj2 3377: 
1.174     albertel 3378: The due date has passed, but the answer date has not yet arrived.
1.59      bowersj2 3379: 
1.174     albertel 3380: =item * B<PAST_DUE_NO_ANSWER>:
1.54      bowersj2 3381: 
1.174     albertel 3382: The due date has passed and there is no answer opening date set.
1.51      bowersj2 3383: 
1.174     albertel 3384: =item * B<ANSWER_OPEN>:
1.51      bowersj2 3385: 
1.174     albertel 3386: The answer date is here.
                   3387: 
                   3388: =item * B<NETWORK_FAILURE>:
                   3389: 
                   3390: The information is unknown due to network failure.
1.51      bowersj2 3391: 
                   3392: =back
                   3393: 
                   3394: =cut
                   3395: 
                   3396: # Apparently the compiler optimizes these into constants automatically
1.54      bowersj2 3397: sub OPEN_LATER             { return 0; }
                   3398: sub OPEN                   { return 1; }
                   3399: sub PAST_DUE_NO_ANSWER     { return 2; }
                   3400: sub PAST_DUE_ANSWER_LATER  { return 3; }
                   3401: sub ANSWER_OPEN            { return 4; }
                   3402: sub NOTHING_SET            { return 5; } 
                   3403: sub NETWORK_FAILURE        { return 100; }
                   3404: 
                   3405: # getDateStatus gets the date status for a given problem part. 
                   3406: # Because answer date, due date, and open date are fully independent
                   3407: # (i.e., it is perfectly possible to *only* have an answer date), 
                   3408: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
                   3409: # (past, future, none given). This function handles this with a decision
                   3410: # tree. Read the comments to follow the decision tree.
1.51      bowersj2 3411: 
                   3412: sub getDateStatus {
                   3413:     my $self = shift;
                   3414:     my $part = shift;
                   3415:     $part = "0" if (!defined($part));
1.54      bowersj2 3416: 
                   3417:     # Always return network failure if there was one.
1.51      bowersj2 3418:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3419: 
                   3420:     my $now = time();
                   3421: 
1.53      bowersj2 3422:     my $open = $self->opendate($part);
                   3423:     my $due = $self->duedate($part);
                   3424:     my $answer = $self->answerdate($part);
                   3425: 
                   3426:     if (!$open && !$due && !$answer) {
                   3427:         # no data on the problem at all
                   3428:         # should this be the same as "open later"? think multipart.
                   3429:         return $self->NOTHING_SET;
                   3430:     }
1.59      bowersj2 3431:     if (!$open || $now < $open) {return $self->OPEN_LATER}
                   3432:     if (!$due || $now < $due) {return $self->OPEN}
                   3433:     if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
                   3434:     if ($answer) { return $self->ANSWER_OPEN; }
1.54      bowersj2 3435:     return PAST_DUE_NO_ANSWER;
1.51      bowersj2 3436: }
                   3437: 
                   3438: =pod
                   3439: 
                   3440: B<>
                   3441: 
                   3442: =over 4
                   3443: 
1.174     albertel 3444: =item * B<getCompletionStatus>($part):
1.51      bowersj2 3445: 
1.174     albertel 3446: ($part defaults to 0.) A convenience function that returns a symbolic
                   3447: constant telling you about the completion status of the part, with the
                   3448: following possible results:
                   3449: 
                   3450: =back
1.51      bowersj2 3451: 
                   3452: B<Completion Codes>
                   3453: 
                   3454: =over 4
                   3455: 
1.174     albertel 3456: =item * B<NOT_ATTEMPTED>:
                   3457: 
                   3458: Has not been attempted at all.
                   3459: 
                   3460: =item * B<INCORRECT>:
                   3461: 
                   3462: Attempted, but wrong by student.
1.51      bowersj2 3463: 
1.174     albertel 3464: =item * B<INCORRECT_BY_OVERRIDE>:
1.51      bowersj2 3465: 
1.174     albertel 3466: Attempted, but wrong by instructor override.
1.51      bowersj2 3467: 
1.174     albertel 3468: =item * B<CORRECT>:
1.51      bowersj2 3469: 
1.174     albertel 3470: Correct or correct by instructor.
1.51      bowersj2 3471: 
1.174     albertel 3472: =item * B<CORRECT_BY_OVERRIDE>:
1.51      bowersj2 3473: 
1.174     albertel 3474: Correct by instructor override.
1.51      bowersj2 3475: 
1.174     albertel 3476: =item * B<EXCUSED>:
                   3477: 
                   3478: Excused. Not yet implemented.
                   3479: 
                   3480: =item * B<NETWORK_FAILURE>:
                   3481: 
                   3482: Information not available due to network failure.
                   3483: 
                   3484: =item * B<ATTEMPTED>:
                   3485: 
                   3486: Attempted, and not yet graded.
1.69      bowersj2 3487: 
1.51      bowersj2 3488: =back
                   3489: 
                   3490: =cut
                   3491: 
1.53      bowersj2 3492: sub NOT_ATTEMPTED         { return 10; }
                   3493: sub INCORRECT             { return 11; }
                   3494: sub INCORRECT_BY_OVERRIDE { return 12; }
                   3495: sub CORRECT               { return 13; }
                   3496: sub CORRECT_BY_OVERRIDE   { return 14; }
                   3497: sub EXCUSED               { return 15; }
1.69      bowersj2 3498: sub ATTEMPTED             { return 16; }
1.51      bowersj2 3499: 
                   3500: sub getCompletionStatus {
                   3501:     my $self = shift;
                   3502:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3503: 
1.108     bowersj2 3504:     my $status = $self->queryRestoreHash('solved', shift);
1.51      bowersj2 3505: 
                   3506:     # Left as seperate if statements in case we ever do more with this
                   3507:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
                   3508:     if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
                   3509:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
                   3510:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
                   3511:     if ($status eq 'excused') {return $self->EXCUSED; }
1.69      bowersj2 3512:     if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51      bowersj2 3513:     return $self->NOT_ATTEMPTED;
1.108     bowersj2 3514: }
                   3515: 
                   3516: sub queryRestoreHash {
                   3517:     my $self = shift;
                   3518:     my $hashentry = shift;
                   3519:     my $part = shift;
1.185     bowersj2 3520:     $part = "0" if (!defined($part) || $part eq '');
1.108     bowersj2 3521:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3522: 
                   3523:     $self->getReturnHash();
                   3524: 
                   3525:     return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51      bowersj2 3526: }
                   3527: 
                   3528: =pod
                   3529: 
                   3530: B<Composite Status>
                   3531: 
1.174     albertel 3532: Along with directly returning the date or completion status, the
                   3533: resource object includes a convenience function B<status>() that will
                   3534: combine the two status tidbits into one composite status that can
                   3535: represent the status of the resource as a whole. The precise logic is
                   3536: documented in the comments of the status method. The following results
                   3537: may be returned, all available as methods on the resource object
1.185     bowersj2 3538: ($res->NETWORK_FAILURE): In addition to the return values that match
                   3539: the date or completion status, this function can return "ANSWER_SUBMITTED"
                   3540: if that problemstatus parameter value is set to No, suppressing the
                   3541: incorrect/correct feedback.
1.51      bowersj2 3542: 
                   3543: =over 4
                   3544: 
1.174     albertel 3545: =item * B<NETWORK_FAILURE>:
                   3546: 
                   3547: The network has failed and the information is not available.
1.51      bowersj2 3548: 
1.174     albertel 3549: =item * B<NOTHING_SET>:
1.53      bowersj2 3550: 
1.174     albertel 3551: No dates have been set for this problem (part) at all. (Because only
                   3552: certain parts of a multi-part problem may be assigned, this can not be
                   3553: collapsed into "open later", as we do not know a given part will EVER
                   3554: be opened. For single part, this is the same as "OPEN_LATER".)
1.51      bowersj2 3555: 
1.174     albertel 3556: =item * B<CORRECT>:
1.51      bowersj2 3557: 
1.174     albertel 3558: For any reason at all, the part is considered correct.
1.54      bowersj2 3559: 
1.174     albertel 3560: =item * B<EXCUSED>:
1.51      bowersj2 3561: 
1.174     albertel 3562: For any reason at all, the problem is excused.
1.51      bowersj2 3563: 
1.174     albertel 3564: =item * B<PAST_DUE_NO_ANSWER>:
1.51      bowersj2 3565: 
1.174     albertel 3566: The problem is past due, not considered correct, and no answer date is
                   3567: set.
1.51      bowersj2 3568: 
1.174     albertel 3569: =item * B<PAST_DUE_ANSWER_LATER>:
1.51      bowersj2 3570: 
1.174     albertel 3571: The problem is past due, not considered correct, and an answer date in
                   3572: the future is set.
1.51      bowersj2 3573: 
1.174     albertel 3574: =item * B<ANSWER_OPEN>:
                   3575: 
                   3576: The problem is past due, not correct, and the answer is now available.
                   3577: 
                   3578: =item * B<OPEN_LATER>:
                   3579: 
                   3580: The problem is not yet open.
                   3581: 
                   3582: =item * B<TRIES_LEFT>:
                   3583: 
                   3584: The problem is open, has been tried, is not correct, but there are
                   3585: tries left.
                   3586: 
                   3587: =item * B<INCORRECT>:
                   3588: 
                   3589: The problem is open, and all tries have been used without getting the
                   3590: correct answer.
                   3591: 
                   3592: =item * B<OPEN>:
                   3593: 
                   3594: The item is open and not yet tried.
                   3595: 
                   3596: =item * B<ATTEMPTED>:
                   3597: 
                   3598: The problem has been attempted.
1.69      bowersj2 3599: 
1.185     bowersj2 3600: =item * B<ANSWER_SUBMITTED>:
                   3601: 
                   3602: An answer has been submitted, but the student should not see it.
                   3603: 
1.51      bowersj2 3604: =back
                   3605: 
                   3606: =cut
                   3607: 
1.185     bowersj2 3608: sub TRIES_LEFT       { return 20; }
                   3609: sub ANSWER_SUBMITTED { return 21; }
1.51      bowersj2 3610: 
                   3611: sub status {
                   3612:     my $self = shift;
                   3613:     my $part = shift;
                   3614:     if (!defined($part)) { $part = "0"; }
                   3615:     my $completionStatus = $self->getCompletionStatus($part);
                   3616:     my $dateStatus = $self->getDateStatus($part);
                   3617: 
                   3618:     # What we have is a two-dimensional matrix with 4 entries on one
                   3619:     # dimension and 5 entries on the other, which we want to colorize,
1.54      bowersj2 3620:     # plus network failure and "no date data at all".
1.51      bowersj2 3621: 
1.53      bowersj2 3622:     if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51      bowersj2 3623: 
1.186     bowersj2 3624:     my $suppressFeedback = lc($self->parmval("problemstatus", $part)) eq 'no';
1.185     bowersj2 3625: 
1.51      bowersj2 3626:     # There are a few whole rows we can dispose of:
1.53      bowersj2 3627:     if ($completionStatus == CORRECT ||
                   3628:         $completionStatus == CORRECT_BY_OVERRIDE ) {
1.185     bowersj2 3629:         return $suppressFeedback? ANSWER_SUBMITTED : CORRECT; 
1.69      bowersj2 3630:     }
                   3631: 
                   3632:     if ($completionStatus == ATTEMPTED) {
                   3633:         return ATTEMPTED;
1.53      bowersj2 3634:     }
                   3635: 
                   3636:     # If it's EXCUSED, then return that no matter what
                   3637:     if ($completionStatus == EXCUSED) {
                   3638:         return EXCUSED; 
1.51      bowersj2 3639:     }
                   3640: 
1.53      bowersj2 3641:     if ($dateStatus == NOTHING_SET) {
                   3642:         return NOTHING_SET;
1.51      bowersj2 3643:     }
                   3644: 
1.69      bowersj2 3645:     # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
                   3646:     # by 4 matrix (date statuses).
1.51      bowersj2 3647: 
1.54      bowersj2 3648:     if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59      bowersj2 3649:         $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54      bowersj2 3650:         return $dateStatus; 
1.51      bowersj2 3651:     }
                   3652: 
1.53      bowersj2 3653:     if ($dateStatus == ANSWER_OPEN) {
                   3654:         return ANSWER_OPEN;
1.51      bowersj2 3655:     }
                   3656: 
                   3657:     # Now: (incorrect, incorrect_override, not_attempted) x 
                   3658:     # (open_later), (open)
                   3659:     
1.53      bowersj2 3660:     if ($dateStatus == OPEN_LATER) {
                   3661:         return OPEN_LATER;
1.51      bowersj2 3662:     }
                   3663: 
                   3664:     # If it's WRONG...
1.53      bowersj2 3665:     if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51      bowersj2 3666:         # and there are TRIES LEFT:
1.79      bowersj2 3667:         if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53      bowersj2 3668:             return TRIES_LEFT;
1.51      bowersj2 3669:         }
1.185     bowersj2 3670:         return $suppressFeedback ? ANSWER_SUBMITTED : INCORRECT; # otherwise, return orange; student can't fix this
1.51      bowersj2 3671:     }
                   3672: 
                   3673:     # Otherwise, it's untried and open
1.53      bowersj2 3674:     return OPEN; 
1.51      bowersj2 3675: }
                   3676: 
                   3677: =pod
                   3678: 
                   3679: =head2 Resource/Nav Map Navigation
                   3680: 
                   3681: =over 4
                   3682: 
1.174     albertel 3683: =item * B<getNext>():
                   3684: 
                   3685: Retreive an array of the possible next resources after this
                   3686: one. Always returns an array, even in the one- or zero-element case.
                   3687: 
                   3688: =item * B<getPrevious>():
1.85      bowersj2 3689: 
1.174     albertel 3690: Retreive an array of the possible previous resources from this
                   3691: one. Always returns an array, even in the one- or zero-element case.
1.51      bowersj2 3692: 
                   3693: =cut
                   3694: 
                   3695: sub getNext {
                   3696:     my $self = shift;
                   3697:     my @branches;
                   3698:     my $to = $self->to();
1.85      bowersj2 3699:     foreach my $branch ( split(/,/, $to) ) {
1.51      bowersj2 3700:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3701:         my $next = $choice->goesto();
                   3702:         $next = $self->{NAV_MAP}->getById($next);
                   3703: 
1.131     bowersj2 3704:         push @branches, $next;
1.85      bowersj2 3705:     }
                   3706:     return \@branches;
                   3707: }
                   3708: 
                   3709: sub getPrevious {
                   3710:     my $self = shift;
                   3711:     my @branches;
                   3712:     my $from = $self->from();
                   3713:     foreach my $branch ( split /,/, $from) {
                   3714:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3715:         my $prev = $choice->comesfrom();
                   3716:         $prev = $self->{NAV_MAP}->getById($prev);
                   3717: 
1.131     bowersj2 3718:         push @branches, $prev;
1.51      bowersj2 3719:     }
                   3720:     return \@branches;
1.131     bowersj2 3721: }
                   3722: 
                   3723: sub browsePriv {
                   3724:     my $self = shift;
                   3725:     if (defined($self->{BROWSE_PRIV})) {
                   3726:         return $self->{BROWSE_PRIV};
                   3727:     }
                   3728: 
                   3729:     $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51      bowersj2 3730: }
                   3731: 
                   3732: =pod
1.2       www      3733: 
1.51      bowersj2 3734: =back
1.2       www      3735: 
1.51      bowersj2 3736: =cut
1.2       www      3737: 
1.51      bowersj2 3738: 1;
1.2       www      3739: 
1.51      bowersj2 3740: __END__
1.2       www      3741: 
                   3742: 

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