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

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

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