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

1.2       www         1: # The LearningOnline Network with CAPA
                      2: # Navigate Maps Handler
1.1       www         3: #
1.178   ! bowersj2    4: # $Id: lonnavmaps.pm,v 1.177 2003/04/19 20:47:41 www 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
        !           174:     if (!defined($ENV{'form.filter'})) {
        !           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();
        !           188:         }
        !           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: 
                    274:     if ($status == $res->NETWORK_FAILURE) { return ""; }
                    275:     if ($status == $res->NOTHING_SET) {
                    276:         return "Not currently assigned.";
                    277:     }
                    278:     if ($status == $res->OPEN_LATER) {
1.73      bowersj2  279:         return "Open " . timeToHumanString($res->opendate($part));
1.53      bowersj2  280:     }
                    281:     if ($status == $res->OPEN) {
1.79      bowersj2  282:         if ($res->duedate($part)) {
1.73      bowersj2  283:             return "Due " . timeToHumanString($res->duedate($part));
1.66      bowersj2  284:         } else {
                    285:             return "Open, no due date";
                    286:         }
1.53      bowersj2  287:     }
1.54      bowersj2  288:     if ($status == $res->PAST_DUE_ANSWER_LATER) {
1.73      bowersj2  289:         return "Answer open " . timeToHumanString($res->answerdate($part));
1.54      bowersj2  290:     }
                    291:     if ($status == $res->PAST_DUE_NO_ANSWER) {
1.73      bowersj2  292:         return "Was due " . timeToHumanString($res->duedate($part));
1.53      bowersj2  293:     }
                    294:     if ($status == $res->ANSWER_OPEN) {
                    295:         return "Answer available";
                    296:     }
1.59      bowersj2  297:     if ($status == $res->EXCUSED) {
1.66      bowersj2  298:         return "Excused by instructor";
1.59      bowersj2  299:     }
1.69      bowersj2  300:     if ($status == $res->ATTEMPTED) {
                    301:         return "Not yet graded.";
                    302:     }
1.59      bowersj2  303:     if ($status == $res->TRIES_LEFT) {
1.79      bowersj2  304:         my $tries = $res->tries($part);
                    305:         my $maxtries = $res->maxtries($part);
                    306:         my $triesString = "";
                    307:         if ($tries && $maxtries) {
                    308:             $triesString = "<font size=\"-1\"><i>($tries of $maxtries tries used)</i></font>";
                    309:             if ($maxtries > 1 && $maxtries - $tries == 1) {
                    310:                 $triesString = "<b>$triesString</b>";
                    311:             }
                    312:         }
1.66      bowersj2  313:         if ($res->duedate()) {
1.73      bowersj2  314:             return "Due " . timeToHumanString($res->duedate($part)) .
1.66      bowersj2  315:                 " $triesString";
                    316:         } else {
                    317:             return "No due date $triesString";
                    318:         }
1.59      bowersj2  319:     }
1.53      bowersj2  320: }
                    321: 
1.115     bowersj2  322: # Convenience function, so others can use it: Is the problem due in less then
                    323: # 24 hours, and still can be done?
                    324: 
                    325: sub dueInLessThen24Hours {
                    326:     my $res = shift;
                    327:     my $part = shift;
                    328:     my $status = $res->status($part);
                    329: 
                    330:     return ($status == $res->OPEN() || $status == $res->ATTEMPTED() ||
                    331:             $status == $res->TRIES_LEFT()) &&
                    332:            $res->duedate() && $res->duedate() < time()+(24*60*60) &&
                    333:            $res->duedate() > time();
                    334: }
                    335: 
                    336: # Convenience function, so others can use it: Is there only one try remaining for the
                    337: # part, with more then one try to begin with, not due yet and still can be done?
                    338: sub lastTry {
                    339:     my $res = shift;
                    340:     my $part = shift;
                    341: 
                    342:     my $tries = $res->tries($part);
                    343:     my $maxtries = $res->maxtries($part);
                    344:     return $tries && $maxtries && $maxtries > 1 &&
                    345:         $maxtries - $tries == 1 && $res->duedate() &&
                    346:         $res->duedate() > time();
                    347: }
                    348: 
1.101     bowersj2  349: # This puts a human-readable name on the ENV variable.
1.177     www       350: 
1.68      bowersj2  351: sub advancedUser {
1.177     www       352:     return $ENV{'request.role.adv'};
1.68      bowersj2  353: }
                    354: 
1.73      bowersj2  355: 
                    356: # timeToHumanString takes a time number and converts it to a
                    357: # human-readable representation, meant to be used in the following
                    358: # manner:
                    359: # print "Due $timestring"
                    360: # print "Open $timestring"
                    361: # print "Answer available $timestring"
                    362: # Very, very, very, VERY English-only... goodness help a localizer on
                    363: # this func...
1.53      bowersj2  364: sub timeToHumanString {
1.58      albertel  365:     my ($time) = @_;
                    366:     # zero, '0' and blank are bad times
1.73      bowersj2  367:     if (!$time) {
                    368:         return 'never';
                    369:     }
                    370: 
                    371:     my $now = time();
                    372: 
                    373:     my @time = localtime($time);
                    374:     my @now = localtime($now);
                    375: 
                    376:     # Positive = future
                    377:     my $delta = $time - $now;
                    378: 
                    379:     my $minute = 60;
                    380:     my $hour = 60 * $minute;
                    381:     my $day = 24 * $hour;
                    382:     my $week = 7 * $day;
                    383:     my $inPast = 0;
                    384: 
                    385:     # Logic in comments:
                    386:     # Is it now? (extremely unlikely)
                    387:     if ( $delta == 0 ) {
                    388:         return "this instant";
                    389:     }
                    390: 
                    391:     if ($delta < 0) {
                    392:         $inPast = 1;
                    393:         $delta = -$delta;
                    394:     }
                    395: 
                    396:     if ( $delta > 0 ) {
1.101     bowersj2  397: 
1.73      bowersj2  398:         my $tense = $inPast ? " ago" : "";
                    399:         my $prefix = $inPast ? "" : "in ";
1.101     bowersj2  400:         
                    401:         # Less then a minute
1.73      bowersj2  402:         if ( $delta < $minute ) {
                    403:             if ($delta == 1) { return "${prefix}1 second$tense"; }
                    404:             return "$prefix$delta seconds$tense";
                    405:         }
                    406: 
1.101     bowersj2  407:         # Less then an hour
1.73      bowersj2  408:         if ( $delta < $hour ) {
                    409:             # If so, use minutes
                    410:             my $minutes = floor($delta / 60);
                    411:             if ($minutes == 1) { return "${prefix}1 minute$tense"; }
                    412:             return "$prefix$minutes minutes$tense";
                    413:         }
                    414:         
                    415:         # Is it less then 24 hours away? If so,
                    416:         # display hours + minutes
                    417:         if ( $delta < $hour * 24) {
                    418:             my $hours = floor($delta / $hour);
                    419:             my $minutes = floor(($delta % $hour) / $minute);
                    420:             my $hourString = "$hours hours";
                    421:             my $minuteString = ", $minutes minutes";
                    422:             if ($hours == 1) {
                    423:                 $hourString = "1 hour";
                    424:             }
                    425:             if ($minutes == 1) {
                    426:                 $minuteString = ", 1 minute";
                    427:             }
                    428:             if ($minutes == 0) {
                    429:                 $minuteString = "";
                    430:             }
                    431:             return "$prefix$hourString$minuteString$tense";
                    432:         }
                    433: 
                    434:         # Less then 5 days away, display day of the week and
                    435:         # HH:MM
                    436:         if ( $delta < $day * 5 ) {
1.85      bowersj2  437:             my $timeStr = strftime("%A, %b %e at %I:%M %P", localtime($time));
1.73      bowersj2  438:             $timeStr =~ s/12:00 am/midnight/;
                    439:             $timeStr =~ s/12:00 pm/noon/;
                    440:             return ($inPast ? "last " : "next ") .
                    441:                 $timeStr;
                    442:         }
                    443:         
                    444:         # Is it this year?
                    445:         if ( $time[5] == $now[5]) {
                    446:             # Return on Month Day, HH:MM meridian
                    447:             my $timeStr = strftime("on %A, %b %e at %I:%M %P", localtime($time));
                    448:             $timeStr =~ s/12:00 am/midnight/;
                    449:             $timeStr =~ s/12:00 pm/noon/;
                    450:             return $timeStr;
                    451:         }
                    452: 
                    453:         # Not this year, so show the year
                    454:         my $timeStr = strftime("on %A, %b %e %G at %I:%M %P", localtime($time));
                    455:         $timeStr =~ s/12:00 am/midnight/;
                    456:         $timeStr =~ s/12:00 pm/noon/;
                    457:         return $timeStr;
1.58      albertel  458:     }
1.53      bowersj2  459: }
                    460: 
1.51      bowersj2  461: 
1.133     bowersj2  462: =pod
                    463: 
1.174     albertel  464: =head1 NAME
                    465: 
                    466: Apache::lonnavmap - Subroutines to handle and render the navigation maps
                    467: 
                    468: =head1 SYNOPSIS
                    469: 
                    470: The main handler generates the navigational listing for the course,
                    471: the other objects export this information in a usable fashion for
                    472: other modules
1.133     bowersj2  473: 
1.174     albertel  474: =head1 Object: render
1.133     bowersj2  475: 
1.174     albertel  476: The navmap renderer package provides a sophisticated rendering of the
                    477: standard navigation maps interface into HTML. The provided nav map
                    478: handler is actually just a glorified call to this.
1.133     bowersj2  479: 
1.174     albertel  480: Because of the large number of parameters this function presents,
                    481: instead of passing it arguments as is normal, pass it in an anonymous
                    482: hash with the given options. This is because there is no obvious order
                    483: you may wish to override these in and a hash is easier to read and
                    484: understand then "undef, undef, undef, 1, undef, undef, renderButton,
                    485: undef, 0" when you mostly want default behaviors.
                    486: 
                    487: The package provides a function called 'render', called as
                    488: Apache::lonnavmaps::renderer->render({}).
1.51      bowersj2  489: 
1.133     bowersj2  490: =head2 Overview of Columns
1.51      bowersj2  491: 
1.174     albertel  492: The renderer will build an HTML table for the navmap and return
                    493: it. The table is consists of several columns, and a row for each
                    494: resource (or possibly each part). You tell the renderer how many
                    495: columns to create and what to place in each column, optionally using
                    496: one or more of the preparent columns, and the renderer will assemble
                    497: the table.
                    498: 
                    499: Any additional generally useful column types should be placed in the
                    500: renderer code here, so anybody can use it anywhere else. Any code
                    501: specific to the current application (such as the addition of <input>
                    502: elements in a column) should be placed in the code of the thing using
                    503: the renderer.
                    504: 
                    505: At the core of the renderer is the array reference COLS (see Example
                    506: section below for how to pass this correctly). The COLS array will
                    507: consist of entries of one of two types of things: Either an integer
                    508: representing one of the pre-packaged column types, or a sub reference
                    509: that takes a resource reference, a part number, and a reference to the
                    510: argument hash passed to the renderer, and returns a string that will
                    511: be inserted into the HTML representation as it.
                    512: 
                    513: The pre-packaged column names are refered to by constants in the
                    514: Apache::lonnavmaps::renderer namespace. The following currently exist:
1.51      bowersj2  515: 
1.174     albertel  516: =over 4
1.51      bowersj2  517: 
1.174     albertel  518: =item * B<resource>:
1.51      bowersj2  519: 
1.174     albertel  520: The general info about the resource: Link, icon for the type, etc. The
                    521: first column in the standard nav map display. This column also accepts
                    522: the following parameter in the renderer hash:
1.51      bowersj2  523: 
1.133     bowersj2  524: =over 4
1.51      bowersj2  525: 
1.174     albertel  526: =item * B<resource_nolink>:
1.51      bowersj2  527: 
1.174     albertel  528: If true, the resource will not be linked. Default: false, resource
                    529: will have links.
                    530: 
                    531: =item * B<resource_part_count>:
1.51      bowersj2  532: 
1.174     albertel  533: If true (default), the resource will show a part count if the full
                    534: part list is not displayed. If false, the resource will never show a
                    535: part count.
1.133     bowersj2  536: 
1.174     albertel  537: =item * B<resource_no_folder_link>:
1.133     bowersj2  538: 
1.174     albertel  539: If true, the resource's folder will not be clickable to open or close
                    540: it. Default is false. True implies printCloseAll is false, since you
                    541: can't close or open folders when this is on anyhow.
1.144     bowersj2  542: 
1.133     bowersj2  543: =back
1.51      bowersj2  544: 
1.174     albertel  545: =item B<communication_status>:
                    546: 
                    547: Whether there is discussion on the resource, email for the user, or
                    548: (lumped in here) perl errors in the execution of the problem. This is
                    549: the second column in the main nav map.
                    550: 
                    551: =item B<quick_status>:
1.51      bowersj2  552: 
1.174     albertel  553: An icon for the status of a problem, with four possible states:
                    554: Correct, incorrect, open, or none (not open yet, not a problem). The
                    555: third column of the standard navmap.
1.51      bowersj2  556: 
1.174     albertel  557: =item B<long_status>:
                    558: 
                    559: A text readout of the details of the current status of the problem,
                    560: such as "Due in 22 hours". The fourth column of the standard navmap.
1.51      bowersj2  561: 
1.133     bowersj2  562: =back
1.51      bowersj2  563: 
1.133     bowersj2  564: If you add any others please be sure to document them here.
1.51      bowersj2  565: 
1.174     albertel  566: An example of a column renderer that will show the ID number of a
                    567: resource, along with the part name if any:
1.51      bowersj2  568: 
1.133     bowersj2  569:  sub { 
                    570:   my ($resource, $part, $params) = @_;   
                    571:   if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
                    572:   return '<td>' . $resource->{ID} . '</td>';
                    573:  }
1.51      bowersj2  574: 
1.174     albertel  575: Note these functions are responsible for the TD tags, which allow them
                    576: to override vertical and horizontal alignment, etc.
1.130     www       577: 
1.133     bowersj2  578: =head2 Parameters
1.115     bowersj2  579: 
1.174     albertel  580: Most of these parameters are only useful if you are *not* using the
                    581: folder interface (i.e., the default first column), which is probably
                    582: the common case. If you are using this interface, then you should be
                    583: able to get away with just using 'cols' (to specify the columns
                    584: shown), 'url' (necessary for the folders to link to the current screen
                    585: correctly), and possibly 'queryString' if your app calls for it. In
                    586: that case, maintaining the state of the folders will be done
                    587: automatically.
1.140     bowersj2  588: 
1.133     bowersj2  589: =over 4
1.51      bowersj2  590: 
1.174     albertel  591: =item * B<iterator>:
                    592: 
                    593: A reference to a fresh ::iterator to use from the navmaps. The
                    594: rendering will reflect the options passed to the iterator, so you can
                    595: use that to just render a certain part of the course, if you like. If
                    596: one is not passed, the renderer will attempt to construct one from
                    597: ENV{'form.filter'} and ENV{'form.condition'} information, plus the
                    598: 'iterator_map' parameter if any.
                    599: 
                    600: =item * B<iterator_map>:
                    601: 
                    602: If you are letting the renderer do the iterator handling, you can
                    603: instruct the renderer to render only a particular map by passing it
                    604: the source of the map you want to process, like
                    605: '/res/103/jerf/navmap.course.sequence'.
                    606: 
                    607: =item * B<navmap>:
                    608: 
                    609: A reference to a navmap, used only if an iterator is not passed in. If
                    610: this is necessary to make an iterator but it is not passed in, a new
                    611: one will be constructed based on ENV info. This is useful to do basic
                    612: error checking before passing it off to render.
                    613: 
                    614: =item * B<r>:
                    615: 
                    616: The standard Apache response object. This must be passed to the
                    617: renderer or the course hash will be locked.
                    618: 
                    619: =item * B<cols>:
                    620: 
                    621: An array reference
                    622: 
                    623: =item * B<showParts>:
                    624: 
                    625: A flag. If yes (default), a line for the resource itself, and a line
                    626: for each part will be displayed. If not, only one line for each
                    627: resource will be displayed.
                    628: 
                    629: =item * B<condenseParts>:
                    630: 
                    631: A flag. If yes (default), if all parts of the problem have the same
                    632: status and that status is Nothing Set, Correct, or Network Failure,
                    633: then only one line will be displayed for that resource anyhow. If no,
                    634: all parts will always be displayed. If showParts is 0, this is
                    635: ignored.
                    636: 
                    637: =item * B<jumpCount>:
                    638: 
                    639: A string identifying the URL to place the anchor 'curloc' at. Default
                    640: to no anchor at all. It is the responsibility of the renderer user to
                    641: ensure that the #curloc is in the URL. By default, determined through
                    642: the use of the ENV{} 'jump' information, and should normally "just
                    643: work" correctly.
1.144     bowersj2  644: 
1.174     albertel  645: =item * B<here>:
1.140     bowersj2  646: 
1.174     albertel  647: A Symb identifying where to place the 'here' marker. Default empty,
                    648: which means no marker.
1.92      bowersj2  649: 
1.174     albertel  650: =item * B<indentString>:
1.164     bowersj2  651: 
1.174     albertel  652: A string identifying the indentation string to use. By default, this
                    653: is a 25 pixel whitespace image with no alt text.
1.92      bowersj2  654: 
1.174     albertel  655: =item * B<queryString>:
1.51      bowersj2  656: 
1.174     albertel  657: A string which will be prepended to the query string used when the
                    658: folders are opened or closed.
1.55      bowersj2  659: 
1.174     albertel  660: =item * B<url>:
1.84      bowersj2  661: 
1.174     albertel  662: The url the folders will link to, which should be the current
                    663: page. Required if the resource info column is shown.
1.115     bowersj2  664: 
1.174     albertel  665: =item * B<currentJumpIndex>:
1.55      bowersj2  666: 
1.174     albertel  667: Describes the currently-open row number to cause the browser to jump
                    668: to, because the user just opened that folder. By default, pulled from
                    669: the Jump information in the ENV{'form.*'}.
1.51      bowersj2  670: 
1.174     albertel  671: =item * B<printKey>:
1.51      bowersj2  672: 
1.174     albertel  673: If true, print the key that appears on the top of the standard
                    674: navmaps. Default is false.
1.139     bowersj2  675: 
1.174     albertel  676: =item * B<printCloseAll>:
1.140     bowersj2  677: 
1.174     albertel  678: If true, print the "Close all folders" or "open all folders"
                    679: links. Default is true.
1.140     bowersj2  680: 
1.174     albertel  681: =item * B<filterFunc>:
1.153     bowersj2  682: 
1.174     albertel  683: A function that takes the resource object as its only parameter and
                    684: returns a true or false value. If true, the resource is displayed. If
                    685: false, it is simply skipped in the display. By default, all resources
                    686: are shown.
                    687: 
                    688: =item * B<suppressNavmaps>:
                    689: 
                    690: If true, will not display Navigate Content resources. Default to
                    691: false.
1.143     bowersj2  692: 
1.133     bowersj2  693: =back
1.51      bowersj2  694: 
1.133     bowersj2  695: =head2 Additional Info
1.51      bowersj2  696: 
1.174     albertel  697: In addition to the parameters you can pass to the renderer, which will
                    698: be passed through unchange to the column renderers, the renderer will
                    699: generate the following information which your renderer may find
                    700: useful:
                    701: 
                    702: If you want to know how many rows were printed, the 'counter' element
                    703: of the hash passed into the render function will contain the
                    704: count. You may want to check whether any resources were printed at
                    705: all.
1.145     bowersj2  706: 
1.133     bowersj2  707: =over 4
1.59      bowersj2  708: 
1.133     bowersj2  709: =back
1.59      bowersj2  710: 
1.133     bowersj2  711: =cut
1.59      bowersj2  712: 
1.133     bowersj2  713: sub resource { return 0; }
                    714: sub communication_status { return 1; }
                    715: sub quick_status { return 2; }
                    716: sub long_status { return 3; }
1.124     bowersj2  717: 
1.133     bowersj2  718: # Data for render_resource
1.51      bowersj2  719: 
1.133     bowersj2  720: sub render_resource {
                    721:     my ($resource, $part, $params) = @_;
1.51      bowersj2  722: 
1.133     bowersj2  723:     my $nonLinkedText = ''; # stuff after resource title not in link
1.51      bowersj2  724: 
1.134     bowersj2  725:     my $link = $params->{"resourceLink"};
                    726:     my $src = $resource->src();
                    727:     my $it = $params->{"iterator"};
1.133     bowersj2  728:     my $filter = $it->{FILTER};
                    729: 
                    730:     my $title = $resource->compTitle();
                    731:     if ($src =~ /^\/uploaded\//) {
                    732:         $nonLinkedText=$title;
                    733:         $title = '';
                    734:     }
                    735:     my $partLabel = "";
                    736:     my $newBranchText = "";
                    737:     
                    738:     # If this is a new branch, label it so
                    739:     if ($params->{'isNewBranch'}) {
                    740:         $newBranchText = "<img src='/adm/lonIcons/branch.gif' border='0' />";
1.51      bowersj2  741:     }
                    742: 
1.133     bowersj2  743:     # links to open and close the folder
                    744:     my $linkopen = "<a href='$link'>";
                    745:     my $linkclose = "</a>";
1.51      bowersj2  746: 
1.133     bowersj2  747:     # Default icon: HTML page
                    748:     my $icon = "<img src='/adm/lonIcons/html.gif' alt='' border='0' />";
                    749:     
                    750:     if ($resource->is_problem()) {
1.156     bowersj2  751:         if ($part eq "" || $params->{'condensed'}) {
1.133     bowersj2  752:             $icon = '<img src="/adm/lonIcons/problem.gif" alt="" border="0" />';
                    753:         } else {
                    754:             $icon = $params->{'indentString'};
                    755:         }
                    756:     }
1.51      bowersj2  757: 
1.133     bowersj2  758:     # Display the correct map icon to open or shut map
                    759:     if ($resource->is_map()) {
                    760:         my $mapId = $resource->map_pc();
                    761:         my $nowOpen = !defined($filter->{$mapId});
                    762:         if ($it->{CONDITION}) {
                    763:             $nowOpen = !$nowOpen;
                    764:         }
1.144     bowersj2  765: 
                    766:         if (!$params->{'resource_no_folder_link'}) {
                    767:             $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') . '.gif';
                    768:             $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
                    769: 
                    770:             $linkopen = "<a href='" . $params->{'url'} . '?' . 
                    771:                 $params->{'queryString'} . '&filter=';
                    772:             $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
                    773:                 addToFilter($filter, $mapId) :
                    774:                 removeFromFilter($filter, $mapId);
                    775:             $linkopen .= "&condition=" . $it->{CONDITION} . '&hereType='
                    776:                 . $params->{'hereType'} . '&here=' .
                    777:                 &Apache::lonnet::escape($params->{'here'}) . 
1.159     bowersj2  778:                 '&jump=' .
1.144     bowersj2  779:                 &Apache::lonnet::escape($resource->symb()) . 
                    780:                 "&folderManip=1'>";
                    781:         } else {
                    782:             # Don't allow users to manipulate folder
                    783:             $icon = 'navmap.folder.' . ($nowOpen ? 'closed' : 'open') .
                    784:                 '.nomanip.gif';
                    785:             $icon = "<img src='/adm/lonIcons/$icon' alt='' border='0' />";
                    786: 
                    787:             $linkopen = "";
                    788:             $linkclose = "";
                    789:         }
1.133     bowersj2  790:     }
1.51      bowersj2  791: 
1.133     bowersj2  792:     if ($resource->randomout()) {
                    793:         $nonLinkedText .= ' <i>(hidden)</i> ';
                    794:     }
                    795:     
                    796:     # We're done preparing and finally ready to start the rendering
                    797:     my $result = "<td align='left' valign='center'>";
1.140     bowersj2  798: 
                    799:     my $indentLevel = $params->{'indentLevel'};
                    800:     if ($newBranchText) { $indentLevel--; }
                    801: 
1.133     bowersj2  802:     # print indentation
1.140     bowersj2  803:     for (my $i = 0; $i < $indentLevel; $i++) {
1.133     bowersj2  804:         $result .= $params->{'indentString'};
1.84      bowersj2  805:     }
                    806: 
1.133     bowersj2  807:     # Decide what to display
                    808:     $result .= "$newBranchText$linkopen$icon$linkclose";
                    809:     
                    810:     my $curMarkerBegin = '';
                    811:     my $curMarkerEnd = '';
1.51      bowersj2  812: 
1.133     bowersj2  813:     # Is this the current resource?
1.158     bowersj2  814:     if (!$params->{'displayedHereMarker'} && 
                    815:         $resource->symb() eq $params->{'here'} ) {
1.133     bowersj2  816:         $curMarkerBegin = '<font color="red" size="+2">&gt; </font>';
                    817:         $curMarkerEnd = '<font color="red" size="+2">&lt;</font>';
1.158     bowersj2  818:         $params->{'displayedHereMarker'} = 1;
1.51      bowersj2  819:     }
                    820: 
1.156     bowersj2  821:     if ($resource->is_problem() && $part ne "" && 
1.133     bowersj2  822:         !$params->{'condensed'}) {
                    823:         $partLabel = " (Part $part)";
                    824:         $title = "";
1.51      bowersj2  825:     }
                    826: 
1.163     bowersj2  827:     if ($params->{'condensed'} && $resource->countParts() > 1) {
1.133     bowersj2  828:         $nonLinkedText .= ' (' . $resource->countParts() . ' parts)';
1.51      bowersj2  829:     }
                    830: 
1.144     bowersj2  831:     if (!$params->{'resource_nolink'}) {
                    832:         $result .= "  $curMarkerBegin<a href='$link'>$title$partLabel</a>$curMarkerEnd $nonLinkedText</td>";
                    833:     } else {
                    834:         $result .= "  $curMarkerBegin$title$partLabel$curMarkerEnd $nonLinkedText</td>";
                    835:     }
1.51      bowersj2  836: 
1.133     bowersj2  837:     return $result;
                    838: }
1.51      bowersj2  839: 
1.133     bowersj2  840: sub render_communication_status {
                    841:     my ($resource, $part, $params) = @_;
1.134     bowersj2  842:     my $discussionHTML = ""; my $feedbackHTML = ""; my $errorHTML = "";
                    843: 
                    844:     my $link = $params->{"resourceLink"};
                    845:     my $linkopen = "<a href='$link'>";
                    846:     my $linkclose = "</a>";
                    847: 
                    848:     if ($resource->hasDiscussion()) {
                    849:         $discussionHTML = $linkopen .
                    850:             '<img border="0" src="/adm/lonMisc/chat.gif" />' .
                    851:             $linkclose;
                    852:     }
                    853:     
                    854:     if ($resource->getFeedback()) {
                    855:         my $feedback = $resource->getFeedback();
                    856:         foreach (split(/\,/, $feedback)) {
                    857:             if ($_) {
                    858:                 $feedbackHTML .= '&nbsp;<a href="/adm/email?display='
                    859:                     . &Apache::lonnet::escape($_) . '">'
                    860:                     . '<img src="/adm/lonMisc/feedback.gif" '
                    861:                     . 'border="0" /></a>';
                    862:             }
                    863:         }
                    864:     }
                    865:     
                    866:     if ($resource->getErrors()) {
                    867:         my $errors = $resource->getErrors();
                    868:         foreach (split(/,/, $errors)) {
                    869:             if ($_) {
                    870:                 $errorHTML .= '&nbsp;<a href="/adm/email?display='
                    871:                     . &Apache::lonnet::escape($_) . '">'
                    872:                     . '<img src="/adm/lonMisc/bomb.gif" '
                    873:                     . 'border="0" /></a>';
                    874:             }
                    875:         }
                    876:     }
                    877: 
                    878:     return "<td width=\"75\" align=\"left\" valign=\"center\">$discussionHTML$feedbackHTML$errorHTML&nbsp;</td>";
                    879: 
1.133     bowersj2  880: }
                    881: sub render_quick_status {
                    882:     my ($resource, $part, $params) = @_;
1.135     bowersj2  883:     my $result = "";
                    884:     my $firstDisplayed = !$params->{'condensed'} && 
                    885:         $params->{'multipart'} && $part eq "0";
                    886: 
                    887:     my $link = $params->{"resourceLink"};
                    888:     my $linkopen = "<a href='$link'>";
                    889:     my $linkclose = "</a>";
                    890: 
                    891:     if ($resource->is_problem() &&
                    892:         !$firstDisplayed) {
                    893:         my $icon = $statusIconMap{$resource->status($part)};
                    894:         my $alt = $iconAltTags{$icon};
                    895:         if ($icon) {
                    896:             $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";
                    897:         } else {
                    898:             $result .= "<td width='30'>&nbsp;</td>\n";
                    899:         }
                    900:     } else { # not problem, no icon
                    901:         $result .= "<td width='30'>&nbsp;</td>\n";
                    902:     }
                    903: 
                    904:     return $result;
1.133     bowersj2  905: }
                    906: sub render_long_status {
                    907:     my ($resource, $part, $params) = @_;
1.136     bowersj2  908:     my $result = "<td align='right' valign='center'>\n";
                    909:     my $firstDisplayed = !$params->{'condensed'} && 
                    910:         $params->{'multipart'} && $part eq "0";
                    911:                 
                    912:     my $color;
                    913:     if ($resource->is_problem()) {
                    914:         $color = $colormap{$resource->status};
                    915:         
                    916:         if (dueInLessThen24Hours($resource, $part) ||
                    917:             lastTry($resource, $part)) {
                    918:             $color = $hurryUpColor;
                    919:         }
                    920:     }
                    921:     
                    922:     if ($resource->kind() eq "res" &&
                    923:         $resource->is_problem() &&
                    924:         !$firstDisplayed) {
                    925:         if ($color) {$result .= "<font color=\"$color\"><b>"; }
                    926:         $result .= getDescription($resource, $part);
                    927:         if ($color) {$result .= "</b></font>"; }
                    928:     }
                    929:     if ($resource->is_map() && advancedUser() && $resource->randompick()) {
                    930:         $result .= '(randomly select ' . $resource->randompick() .')';
                    931:     }
                    932:     
                    933:     $result .= "&nbsp;</td>\n";
                    934:     
                    935:     return $result;
1.51      bowersj2  936: }
                    937: 
1.133     bowersj2  938: my @preparedColumns = (\&render_resource, \&render_communication_status,
                    939:                        \&render_quick_status, \&render_long_status);
1.132     bowersj2  940: 
                    941: sub setDefault {
                    942:     my ($val, $default) = @_;
                    943:     if (!defined($val)) { return $default; }
                    944:     return $val;
                    945: }
                    946: 
                    947: sub render {
                    948:     my $args = shift;
1.140     bowersj2  949:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
                    950:     my $result = '';
                    951: 
1.132     bowersj2  952:     # Configure the renderer.
                    953:     my $cols = $args->{'cols'};
                    954:     if (!defined($cols)) {
                    955:         # no columns, no nav maps.
                    956:         return '';
                    957:     }
1.140     bowersj2  958:     my $mustCloseNavMap = 0;
                    959:     my $navmap;
                    960:     if (defined($args->{'navmap'})) {
                    961:         $navmap = $args->{'navmap'};
                    962:     }
                    963: 
1.158     bowersj2  964:     my $r = $args->{'r'};
1.140     bowersj2  965:     my $queryString = $args->{'queryString'};
1.159     bowersj2  966:     my $jump = $args->{'jump'};
1.158     bowersj2  967:     my $here = $args->{'here'};
1.153     bowersj2  968:     my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
1.140     bowersj2  969:     my $currentJumpDelta = 2; # change this to change how many resources are displayed
                    970:                              # before the current resource when using #current
                    971: 
                    972:     # If we were passed 'here' information, we are not rendering
                    973:     # after a folder manipulation, and we were not passed an
                    974:     # iterator, make sure we open the folders to show the "here"
                    975:     # marker
                    976:     my $filterHash = {};
                    977:     # Figure out what we're not displaying
                    978:     foreach (split(/\,/, $ENV{"form.filter"})) {
                    979:         if ($_) {
                    980:             $filterHash->{$_} = "1";
                    981:         }
                    982:     }
                    983: 
                    984:     my $condition = 0;
                    985:     if ($ENV{'form.condition'}) {
                    986:         $condition = 1;
                    987:     }
                    988: 
                    989:     if (!$ENV{'form.folderManip'} && !defined($args->{'iterator'})) {
                    990:         # Step 1: Check to see if we have a navmap
                    991:         if (!defined($navmap)) {
1.171     bowersj2  992:             $navmap = Apache::lonnavmaps::navmap->new(
1.140     bowersj2  993:                         $ENV{"request.course.fn"}.".db",
                    994:                         $ENV{"request.course.fn"}."_parms.db", 1, 1);
                    995:             $mustCloseNavMap = 1;
                    996:         }
                    997:         $navmap->init();
                    998: 
                    999:         # Step two: Locate what kind of here marker is necessary
                   1000:         # Determine where the "here" marker is and where the screen jumps to.
                   1001: 
1.175     bowersj2 1002:         if ($ENV{'form.postsymb'}) {
                   1003:             $here = $jump = $ENV{'form.postsymb'};
1.140     bowersj2 1004:         } elsif ($ENV{'form.postdata'}) {
                   1005:             # couldn't find a symb, is there a URL?
                   1006:             my $currenturl = $ENV{'form.postdata'};
1.158     bowersj2 1007:             #$currenturl=~s/^http\:\/\///;
                   1008:             #$currenturl=~s/^[^\/]+//;
1.140     bowersj2 1009:             
1.158     bowersj2 1010:             $here = $jump = &Apache::lonnet::symbread($currenturl);
1.140     bowersj2 1011:         }
1.158     bowersj2 1012: 
1.140     bowersj2 1013:         # Step three: Ensure the folders are open
                   1014:         my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
                   1015:         my $depth = 1;
                   1016:         $mapIterator->next(); # discard the first BEGIN_MAP
                   1017:         my $curRes = $mapIterator->next();
                   1018:         my $found = 0;
                   1019:         
                   1020:         # We only need to do this if we need to open the maps to show the
                   1021:         # current position. This will change the counter so we can't count
                   1022:         # for the jump marker with this loop.
                   1023:         while ($depth > 0 && !$found) {
                   1024:             if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
                   1025:             if ($curRes == $mapIterator->END_MAP()) { $depth--; }
                   1026:             
1.158     bowersj2 1027:             if (ref($curRes) && $curRes->symb() eq $here) {
1.140     bowersj2 1028:                 my $mapStack = $mapIterator->getStack();
                   1029:                 
                   1030:                 # Ensure the parent maps are open
                   1031:                 for my $map (@{$mapStack}) {
                   1032:                     if ($condition) {
                   1033:                         undef $filterHash->{$map->map_pc()};
                   1034:                     } else {
                   1035:                         $filterHash->{$map->map_pc()} = 1;
                   1036:                     }
                   1037:                 }
                   1038:                 $found = 1;
                   1039:             }
                   1040:             
                   1041:             $curRes = $mapIterator->next();
                   1042:         }            
1.159     bowersj2 1043:     }        
1.140     bowersj2 1044: 
                   1045:     if ( !defined($args->{'iterator'}) && $ENV{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
                   1046:         # If this is a click on a folder or something, we want to preserve the "here"
                   1047:         # from the querystring, and get the new "jump" marker
                   1048:         $here = $ENV{'form.here'};
                   1049:         $jump = $ENV{'form.jump'};
                   1050:     } 
                   1051:     
1.132     bowersj2 1052:     my $it = $args->{'iterator'};
                   1053:     if (!defined($it)) {
1.140     bowersj2 1054:         # Construct a default iterator based on $ENV{'form.'} information
                   1055:         
                   1056:         # Step 1: Check to see if we have a navmap
                   1057:         if (!defined($navmap)) {
1.164     bowersj2 1058:             $navmap = Apache::lonnavmaps::navmap->new($r, 
1.140     bowersj2 1059:                         $ENV{"request.course.fn"}.".db",
                   1060:                         $ENV{"request.course.fn"}."_parms.db", 1, 1);
                   1061:             $mustCloseNavMap = 1;
                   1062:         }
                   1063:         # Paranoia: Make sure it's ready
                   1064:         $navmap->init();
                   1065: 
1.144     bowersj2 1066:         # See if we're being passed a specific map
                   1067:         if ($args->{'iterator_map'}) {
                   1068:             my $map = $args->{'iterator_map'};
1.145     bowersj2 1069:             $map = $navmap->getResourceByUrl($map);
1.144     bowersj2 1070:             my $firstResource = $map->map_start();
                   1071:             my $finishResource = $map->map_finish();
                   1072: 
                   1073:             $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
                   1074:         } else {
                   1075:             $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition);
                   1076:         }
1.132     bowersj2 1077:     }
                   1078:     
1.159     bowersj2 1079:     # (re-)Locate the jump point, if any
                   1080:     my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
                   1081:     my $depth = 1;
                   1082:     $mapIterator->next();
                   1083:     my $curRes = $mapIterator->next();
                   1084:     my $foundJump = 0;
                   1085:     my $counter = 0;
                   1086:     
                   1087:     while ($depth > 0 && !$foundJump) {
                   1088:         if ($curRes == $mapIterator->BEGIN_MAP()) { $depth++; }
                   1089:         if ($curRes == $mapIterator->END_MAP()) { $depth--; }
                   1090:         if (ref($curRes)) { $counter++; }
                   1091:         
                   1092:         if (ref($curRes) && $jump eq $curRes->symb()) {
                   1093:             
                   1094:             # This is why we have to use the main iterator instead of the
                   1095:             # potentially faster DFS: The count has to be the same, so
                   1096:             # the order has to be the same, which DFS won't give us.
                   1097:             $args->{'currentJumpIndex'} = $counter;
                   1098:             $foundJump = 1;
                   1099:         }
                   1100:         
                   1101:         $curRes = $mapIterator->next();
                   1102:     }
                   1103: 
1.132     bowersj2 1104:     my $showParts = setDefault($args->{'showParts'}, 1);
                   1105:     my $condenseParts = setDefault($args->{'condenseParts'}, 1);
1.139     bowersj2 1106:     # keeps track of when the current resource is found,
                   1107:     # so we can back up a few and put the anchor above the
                   1108:     # current resource
1.140     bowersj2 1109:     my $printKey = $args->{'printKey'};
                   1110:     my $printCloseAll = $args->{'printCloseAll'};
                   1111:     if (!defined($printCloseAll)) { $printCloseAll = 1; }
1.143     bowersj2 1112:     my $filterFunc = setDefault($args->{'filterFunc'},
                   1113:                                 sub {return 1;});
1.144     bowersj2 1114:     
1.140     bowersj2 1115:     # Print key?
                   1116:     if ($printKey) {
                   1117:         $result .= '<table border="0" cellpadding="2" cellspacing="0">';
                   1118:         my $date=localtime;
                   1119:         $result.='<tr><td align="right" valign="bottom">Key:&nbsp;&nbsp;</td>';
                   1120:         if ($navmap->{LAST_CHECK}) {
                   1121:             $result .= 
                   1122:                 '<img src="/adm/lonMisc/chat.gif"> New discussion since '.
                   1123:                 strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
                   1124:                 '</td><td align="center" valign="bottom">&nbsp;&nbsp;'.
                   1125:                 '<img src="/adm/lonMisc/feedback.gif"> New message (click to open)<p>'.
                   1126:                 '</td>'; 
                   1127:         } else {
                   1128:             $result .= '<td align="center" valign="bottom">&nbsp;&nbsp;'.
                   1129:                 '<img src="/adm/lonMisc/chat.gif"> Discussions</td><td align="center" valign="bottom">'.
                   1130:                 '&nbsp;&nbsp;<img src="/adm/lonMisc/feedback.gif"> New message (click to open)'.
                   1131:                 '</td>'; 
                   1132:         }
                   1133: 
                   1134:         $result .= '</tr></table>';
                   1135:     }
                   1136: 
1.172     bowersj2 1137:     if ($printCloseAll && !$args->{'resource_no_folder_link'}) {
1.140     bowersj2 1138:         if ($condition) {
                   1139:             $result.="<a href=\"navmaps?condition=0&filter=&$queryString" .
1.158     bowersj2 1140:                 "&here=" . Apache::lonnet::escape($here) .
1.140     bowersj2 1141:                 "\">Close All Folders</a>";
                   1142:         } else {
                   1143:             $result.="<a href=\"navmaps?condition=1&filter=&$queryString" .
1.158     bowersj2 1144:                 "&here=" . Apache::lonnet::escape($here) . 
1.140     bowersj2 1145:                 "\">Open All Folders</a>";
                   1146:         }
1.143     bowersj2 1147:         $result .= "<br /><br />\n";
1.140     bowersj2 1148:     }    
                   1149: 
                   1150:     if ($r) {
                   1151:         $r->print($result);
                   1152:         $r->rflush();
                   1153:         $result = "";
                   1154:     }
1.132     bowersj2 1155:     # End parameter setting
1.133     bowersj2 1156:             
1.132     bowersj2 1157:     # Data
1.140     bowersj2 1158:     $result .= '<table cellspacing="0" cellpadding="3" border="0" bgcolor="#FFFFFF">' ."\n";
1.132     bowersj2 1159:     my $res = "Apache::lonnavmaps::resource";
                   1160:     my %condenseStatuses =
                   1161:         ( $res->NETWORK_FAILURE    => 1,
                   1162:           $res->NOTHING_SET        => 1,
                   1163:           $res->CORRECT            => 1 );
                   1164:     my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1.133     bowersj2 1165: 
                   1166:     # Shared variables
                   1167:     $args->{'counter'} = 0; # counts the rows
                   1168:     $args->{'indentLevel'} = 0;
                   1169:     $args->{'isNewBranch'} = 0;
                   1170:     $args->{'condensed'} = 0;    
                   1171:     $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='/adm/lonIcons/whitespace1.gif' width='25' height='1' alt='' border='0' />");
                   1172:     $args->{'displayedHereMarker'} = 0;
1.132     bowersj2 1173: 
1.133     bowersj2 1174:     my $displayedJumpMarker = 0;
1.132     bowersj2 1175:     # Set up iteration.
1.159     bowersj2 1176:     $depth = 1;
1.132     bowersj2 1177:     $it->next(); # discard initial BEGIN_MAP
1.159     bowersj2 1178:     $curRes = $it->next();
1.132     bowersj2 1179:     my $now = time();
                   1180:     my $in24Hours = $now + 24 * 60 * 60;
                   1181:     my $rownum = 0;
                   1182: 
1.141     bowersj2 1183:     # export "here" marker information
                   1184:     $args->{'here'} = $here;
                   1185: 
1.132     bowersj2 1186:     while ($depth > 0) {
                   1187:         if ($curRes == $it->BEGIN_MAP()) { $depth++; }
                   1188:         if ($curRes == $it->END_MAP()) { $depth--; }
                   1189: 
                   1190:         # Maintain indentation level.
                   1191:         if ($curRes == $it->BEGIN_MAP() ||
                   1192:             $curRes == $it->BEGIN_BRANCH() ) {
1.133     bowersj2 1193:             $args->{'indentLevel'}++;
1.132     bowersj2 1194:         }
                   1195:         if ($curRes == $it->END_MAP() ||
                   1196:             $curRes == $it->END_BRANCH() ) {
1.133     bowersj2 1197:             $args->{'indentLevel'}--;
1.132     bowersj2 1198:         }
                   1199:         # Notice new branches
                   1200:         if ($curRes == $it->BEGIN_BRANCH()) {
1.133     bowersj2 1201:             $args->{'isNewBranch'} = 1;
                   1202:         }
                   1203: 
                   1204:         # If this isn't an actual resource, continue on
                   1205:         if (!ref($curRes)) {
                   1206:             next;
1.132     bowersj2 1207:         }
1.133     bowersj2 1208: 
                   1209:         $args->{'counter'}++;
1.143     bowersj2 1210: 
                   1211:         # If this has been filtered out, continue on
                   1212:         if (!(&$filterFunc($curRes))) {
                   1213:             $args->{'isNewBranch'} = 0; # Don't falsely remember this
                   1214:             next;
                   1215:         } 
1.132     bowersj2 1216: 
1.153     bowersj2 1217:         # If we're suppressing navmaps and this is a navmap, continue on
                   1218:         if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
                   1219:             next;
                   1220:         }
                   1221: 
1.132     bowersj2 1222:         # Does it have multiple parts?
1.133     bowersj2 1223:         $args->{'multipart'} = 0;
                   1224:         $args->{'condensed'} = 0;
1.132     bowersj2 1225:         my @parts;
                   1226:             
                   1227:         # Decide what parts to show.
1.133     bowersj2 1228:         if ($curRes->is_problem() && $showParts) {
1.132     bowersj2 1229:             @parts = @{$curRes->parts()};
1.133     bowersj2 1230:             $args->{'multipart'} = scalar(@parts) > 1;
1.132     bowersj2 1231:             
                   1232:             if ($condenseParts) { # do the condensation
                   1233:                 if (!$curRes->opendate("0")) {
1.162     bowersj2 1234:                     @parts = ();
1.133     bowersj2 1235:                     $args->{'condensed'} = 1;
1.132     bowersj2 1236:                 }
1.133     bowersj2 1237:                 if (!$args->{'condensed'}) {
1.132     bowersj2 1238:                     # Decide whether to condense based on similarity
                   1239:                     my $status = $curRes->status($parts[1]);
                   1240:                     my $due = $curRes->duedate($parts[1]);
                   1241:                     my $open = $curRes->opendate($parts[1]);
                   1242:                     my $statusAllSame = 1;
                   1243:                     my $dueAllSame = 1;
                   1244:                     my $openAllSame = 1;
                   1245:                     for (my $i = 2; $i < scalar(@parts); $i++) {
                   1246:                         if ($curRes->status($parts[$i]) != $status){
                   1247:                             $statusAllSame = 0;
                   1248:                         }
                   1249:                         if ($curRes->duedate($parts[$i]) != $due ) {
                   1250:                             $dueAllSame = 0;
                   1251:                         }
                   1252:                         if ($curRes->opendate($parts[$i]) != $open) {
                   1253:                             $openAllSame = 0;
                   1254:                         }
                   1255:                     }
                   1256:                     # $*allSame is true if all the statuses were
                   1257:                     # the same. Now, if they are all the same and
                   1258:                     # match one of the statuses to condense, or they
                   1259:                     # are all open with the same due date, or they are
                   1260:                     # all OPEN_LATER with the same open date, display the
                   1261:                     # status of the first non-zero part (to get the 'correct'
                   1262:                     # status right, since 0 is never 'correct' or 'open').
                   1263:                     if (($statusAllSame && defined($condenseStatuses{$status})) ||
                   1264:                         ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
                   1265:                         ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
1.161     bowersj2 1266:                         @parts = ();
1.133     bowersj2 1267:                         $args->{'condensed'} = 1;
1.132     bowersj2 1268:                     }
                   1269:                     
                   1270:                 }
                   1271:             }
1.157     bowersj2 1272:         } 
1.132     bowersj2 1273:             
                   1274:         # If the multipart problem was condensed, "forget" it was multipart
                   1275:         if (scalar(@parts) == 1) {
1.133     bowersj2 1276:             $args->{'multipart'} = 0;
1.132     bowersj2 1277:         }
                   1278: 
                   1279:         # Now, we've decided what parts to show. Loop through them and
                   1280:         # show them.
1.156     bowersj2 1281:         foreach my $part ('', @parts) {
                   1282:             if ($part eq '0') {
                   1283:                 next;
                   1284:             }
1.132     bowersj2 1285:             $rownum ++;
                   1286:             my $backgroundColor = $backgroundColors[$rownum % scalar(@backgroundColors)];
                   1287:             
                   1288:             $result .= "  <tr bgcolor='$backgroundColor'>\n";
1.134     bowersj2 1289: 
                   1290:             # Set up some data about the parts that the cols might want
                   1291:             my $filter = $it->{FILTER};
                   1292:             my $stack = $it->getStack();
                   1293:             my $src = getLinkForResource($stack);
                   1294:             
                   1295:             my $srcHasQuestion = $src =~ /\?/;
                   1296:             $args->{"resourceLink"} = $src.
                   1297:                 ($srcHasQuestion?'&':'?') .
1.137     bowersj2 1298:                 'symb=' . &Apache::lonnet::escape($curRes->symb());
1.132     bowersj2 1299:             
                   1300:             # Now, display each column.
                   1301:             foreach my $col (@$cols) {
1.139     bowersj2 1302:                 my $colHTML = '';
                   1303:                 if (ref($col)) {
                   1304:                     $colHTML .= &$col($curRes, $part, $args);
                   1305:                 } else {
                   1306:                     $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
                   1307:                 }
1.132     bowersj2 1308: 
1.133     bowersj2 1309:                 # If this is the first column and it's time to print
                   1310:                 # the anchor, do so
                   1311:                 if ($col == $cols->[0] && 
                   1312:                     $args->{'counter'} == $args->{'currentJumpIndex'} - 
1.139     bowersj2 1313:                     $currentJumpDelta) {
                   1314:                     # Jam the anchor after the <td> tag;
                   1315:                     # necessary for valid HTML (which Mozilla requires)
                   1316:                     $colHTML =~ s/\>/\>\<a name="curloc" \/\>/;
1.133     bowersj2 1317:                     $displayedJumpMarker = 1;
                   1318:                 }
1.139     bowersj2 1319:                 $result .= $colHTML . "\n";
1.132     bowersj2 1320:             }
1.139     bowersj2 1321:             $result .= "    </tr>\n";
1.140     bowersj2 1322:             $args->{'isNewBranch'} = 0;
1.139     bowersj2 1323:         }
1.153     bowersj2 1324: 
1.139     bowersj2 1325:         if ($r && $rownum % 20 == 0) {
                   1326:             $r->print($result);
                   1327:             $result = "";
                   1328:             $r->rflush();
1.132     bowersj2 1329:         }
1.156     bowersj2 1330:     } continue {
1.132     bowersj2 1331:         $curRes = $it->next();
                   1332:     }
                   1333:     
1.134     bowersj2 1334:     # Print out the part that jumps to #curloc if it exists
1.159     bowersj2 1335:     # delay needed because the browser is processing the jump before
                   1336:     # it finishes rendering, so it goes to the wrong place!
                   1337:     # onload might be better, but this routine has no access to that.
                   1338:     # On mozilla, the 0-millisecond timeout seems to prevent this;
                   1339:     # it's quite likely this might fix other browsers, too, and 
                   1340:     # certainly won't hurt anything.
1.139     bowersj2 1341:     if ($displayedJumpMarker) {
1.159     bowersj2 1342:         $result .= "<script>setTimeout(\"location += '#curloc';\", 0)</script>\n";
1.134     bowersj2 1343:     }
                   1344: 
1.132     bowersj2 1345:     $result .= "</table>";
1.140     bowersj2 1346:     
                   1347:     if ($r) {
                   1348:         $r->print($result);
                   1349:         $result = "";
                   1350:         $r->rflush();
                   1351:     }
                   1352:         
1.175     bowersj2 1353:     if ($mustCloseNavMap) { $navmap->untieHashes(); } 
1.132     bowersj2 1354: 
                   1355:     return $result;
                   1356: }
                   1357: 
1.133     bowersj2 1358: 1;
                   1359: 
                   1360: package Apache::lonnavmaps::navmap;
                   1361: 
                   1362: =pod
                   1363: 
1.174     albertel 1364: lonnavmaps provides functions and objects for dealing with the
                   1365: compiled course hashes generated when a user enters the course, the
                   1366: Apache handler for the "Navigation Map" button, and a flexible
                   1367: prepared renderer for navigation maps that are easy to use anywhere.
                   1368: 
                   1369: =head1 Object: navmap
1.133     bowersj2 1370: 
1.174     albertel 1371: Encapsulating the compiled nav map
1.133     bowersj2 1372: 
1.174     albertel 1373: navmap is an object that encapsulates a compiled course map and
                   1374: provides a reasonable interface to it.
1.133     bowersj2 1375: 
1.174     albertel 1376: Most notably it provides a way to navigate the map sensibly and a
                   1377: flexible iterator that makes it easy to write various renderers based
                   1378: on nav maps.
1.133     bowersj2 1379: 
                   1380: You must obtain resource objects through the navmap object.
                   1381: 
                   1382: =head2 Methods
                   1383: 
                   1384: =over 4
                   1385: 
1.174     albertel 1386: =item * B<new>(navHashFile, parmHashFile, genCourseAndUserOptions,
                   1387:   genMailDiscussStatus):
1.133     bowersj2 1388: 
1.174     albertel 1389: Binds a new navmap object to the compiled nav map hash and parm hash
                   1390: given as filenames. genCourseAndUserOptions is a flag saying whether
                   1391: the course options and user options hash should be generated. This is
                   1392: for when you are using the parameters of the resources that require
                   1393: them; see documentation in resource object
                   1394: documentation. genMailDiscussStatus causes the nav map to retreive
                   1395: information about the email and discussion status of
                   1396: resources. Returns the navmap object if this is successful, or
                   1397: B<undef> if not. You must check for undef; errors will occur when you
                   1398: try to use the other methods otherwise.
                   1399: 
                   1400: =item * B<getIterator>(first, finish, filter, condition):
                   1401: 
                   1402: See iterator documentation below.
1.133     bowersj2 1403: 
                   1404: =cut
                   1405: 
                   1406: use strict;
                   1407: use GDBM_File;
                   1408: 
                   1409: sub new {
                   1410:     # magic invocation to create a class instance
                   1411:     my $proto = shift;
                   1412:     my $class = ref($proto) || $proto;
                   1413:     my $self = {};
                   1414: 
                   1415:     $self->{NAV_HASH_FILE} = shift;
                   1416:     $self->{PARM_HASH_FILE} = shift;
                   1417:     $self->{GENERATE_COURSE_USER_OPT} = shift;
                   1418:     $self->{GENERATE_EMAIL_DISCUSS_STATUS} = shift;
                   1419: 
                   1420:     # Resource cache stores navmap resources as we reference them. We generate
                   1421:     # them on-demand so we don't pay for creating resources unless we use them.
                   1422:     $self->{RESOURCE_CACHE} = {};
                   1423: 
                   1424:     # Network failure flag, if we accessed the course or user opt and
                   1425:     # failed
                   1426:     $self->{NETWORK_FAILURE} = 0;
                   1427: 
                   1428:     # tie the nav hash
                   1429: 
1.168     bowersj2 1430:     my %navmaphash;
                   1431:     my %parmhash;
                   1432:     if (!(tie(%navmaphash, 'GDBM_File', $self->{NAV_HASH_FILE},
1.133     bowersj2 1433:               &GDBM_READER(), 0640))) {
                   1434:         return undef;
                   1435:     }
                   1436:     
1.168     bowersj2 1437:     if (!(tie(%parmhash, 'GDBM_File', $self->{PARM_HASH_FILE},
1.133     bowersj2 1438:               &GDBM_READER(), 0640)))
                   1439:     {
1.170     matthew  1440:         untie %{$self->{PARM_HASH}};
1.133     bowersj2 1441:         return undef;
                   1442:     }
                   1443: 
1.168     bowersj2 1444:     $self->{NAV_HASH} = \%navmaphash;
                   1445:     $self->{PARM_HASH} = \%parmhash;
1.140     bowersj2 1446:     $self->{INITED} = 0;
1.133     bowersj2 1447: 
                   1448:     bless($self);
                   1449:         
                   1450:     return $self;
                   1451: }
                   1452: 
                   1453: sub init {
                   1454:     my $self = shift;
1.140     bowersj2 1455:     if ($self->{INITED}) { return; }
1.133     bowersj2 1456: 
                   1457:     # If the course opt hash and the user opt hash should be generated,
                   1458:     # generate them
                   1459:     if ($self->{GENERATE_COURSE_USER_OPT}) {
                   1460:         my $uname=$ENV{'user.name'};
                   1461:         my $udom=$ENV{'user.domain'};
                   1462:         my $uhome=$ENV{'user.home'};
                   1463:         my $cid=$ENV{'request.course.id'};
                   1464:         my $chome=$ENV{'course.'.$cid.'.home'};
                   1465:         my ($cdom,$cnum)=split(/\_/,$cid);
                   1466:         
                   1467:         my $userprefix=$uname.'_'.$udom.'_';
                   1468:         
                   1469:         my %courserdatas; my %useropt; my %courseopt; my %userrdatas;
                   1470:         unless ($uhome eq 'no_host') { 
                   1471: # ------------------------------------------------- Get coursedata (if present)
                   1472:             unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
                   1473:                 my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
                   1474:                                                  ':resourcedata',$chome);
                   1475:                 if ($reply!~/^error\:/) {
                   1476:                     $courserdatas{$cid}=$reply;
                   1477:                     $courserdatas{$cid.'.last_cache'}=time;
                   1478:                 }
                   1479:                 # check to see if network failed
                   1480:                 elsif ( $reply=~/no.such.host/i || $reply=~/con.*lost/i )
                   1481:                 {
                   1482:                     $self->{NETWORK_FAILURE} = 1;
                   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.82      bowersj2 3081:     # Retrieve part count, if this is a problem
                   3082:     if ($self->is_problem()) {
1.152     matthew  3083:         my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
1.82      bowersj2 3084:         if (!$metadata) {
                   3085:             $self->{RESOURCE_ERROR} = 1;
                   3086:             $self->{PARTS} = [];
                   3087:             return;
                   3088:         }
                   3089:         foreach (split(/\,/,$metadata)) {
1.152     matthew  3090:             if ($_ =~ /^part_(.*)$/) {
1.147     bowersj2 3091:                 my $part = $1;
                   3092:                 # check to see if part is turned off.
1.149     matthew  3093:                 if (! Apache::loncommon::check_if_partid_hidden($part, $self->symb())) {
1.147     bowersj2 3094:                     push @{$self->{PARTS}}, $1;
                   3095:                 }
1.82      bowersj2 3096:             }
1.51      bowersj2 3097:         }
1.82      bowersj2 3098:         
                   3099:         
                   3100:         my @sortedParts = sort @{$self->{PARTS}};
                   3101:         $self->{PARTS} = \@sortedParts;
1.154     bowersj2 3102:     }
                   3103: 
1.51      bowersj2 3104:     return;
                   3105: }
                   3106: 
                   3107: =pod
                   3108: 
                   3109: =head2 Resource Status
                   3110: 
1.174     albertel 3111: Problem resources have status information, reflecting their various
                   3112: dates and completion statuses.
1.51      bowersj2 3113: 
1.174     albertel 3114: There are two aspects to the status: the date-related information and
                   3115: the completion information.
1.51      bowersj2 3116: 
1.174     albertel 3117: Idiomatic usage of these two methods would probably look something
                   3118: like
1.51      bowersj2 3119: 
                   3120:  foreach ($resource->parts()) {
                   3121:     my $dateStatus = $resource->getDateStatus($_);
                   3122:     my $completionStatus = $resource->getCompletionStatus($_);
                   3123: 
1.70      bowersj2 3124:     or
                   3125: 
                   3126:     my $status = $resource->status($_);
                   3127: 
1.51      bowersj2 3128:     ... use it here ...
                   3129:  }
                   3130: 
1.174     albertel 3131: Which you use depends on exactly what you are looking for. The
                   3132: status() function has been optimized for the nav maps display and may
                   3133: not precisely match what you need elsewhere.
1.101     bowersj2 3134: 
1.174     albertel 3135: The symbolic constants shown below can be accessed through the
                   3136: resource object: C<$res->OPEN>.
1.101     bowersj2 3137: 
1.51      bowersj2 3138: =over 4
                   3139: 
1.174     albertel 3140: =item * B<getDateStatus>($part):
                   3141: 
                   3142: ($part defaults to 0). A convenience function that returns a symbolic
                   3143: constant telling you about the date status of the part. The possible
                   3144: return values are:
1.51      bowersj2 3145: 
                   3146: =back
                   3147: 
                   3148: B<Date Codes>
                   3149: 
                   3150: =over 4
                   3151: 
1.174     albertel 3152: =item * B<OPEN_LATER>:
                   3153: 
                   3154: The problem will be opened later.
                   3155: 
                   3156: =item * B<OPEN>:
                   3157: 
                   3158: Open and not yet due.
                   3159: 
1.51      bowersj2 3160: 
1.174     albertel 3161: =item * B<PAST_DUE_ANSWER_LATER>:
1.51      bowersj2 3162: 
1.174     albertel 3163: The due date has passed, but the answer date has not yet arrived.
1.59      bowersj2 3164: 
1.174     albertel 3165: =item * B<PAST_DUE_NO_ANSWER>:
1.54      bowersj2 3166: 
1.174     albertel 3167: The due date has passed and there is no answer opening date set.
1.51      bowersj2 3168: 
1.174     albertel 3169: =item * B<ANSWER_OPEN>:
1.51      bowersj2 3170: 
1.174     albertel 3171: The answer date is here.
                   3172: 
                   3173: =item * B<NETWORK_FAILURE>:
                   3174: 
                   3175: The information is unknown due to network failure.
1.51      bowersj2 3176: 
                   3177: =back
                   3178: 
                   3179: =cut
                   3180: 
                   3181: # Apparently the compiler optimizes these into constants automatically
1.54      bowersj2 3182: sub OPEN_LATER             { return 0; }
                   3183: sub OPEN                   { return 1; }
                   3184: sub PAST_DUE_NO_ANSWER     { return 2; }
                   3185: sub PAST_DUE_ANSWER_LATER  { return 3; }
                   3186: sub ANSWER_OPEN            { return 4; }
                   3187: sub NOTHING_SET            { return 5; } 
                   3188: sub NETWORK_FAILURE        { return 100; }
                   3189: 
                   3190: # getDateStatus gets the date status for a given problem part. 
                   3191: # Because answer date, due date, and open date are fully independent
                   3192: # (i.e., it is perfectly possible to *only* have an answer date), 
                   3193: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
                   3194: # (past, future, none given). This function handles this with a decision
                   3195: # tree. Read the comments to follow the decision tree.
1.51      bowersj2 3196: 
                   3197: sub getDateStatus {
                   3198:     my $self = shift;
                   3199:     my $part = shift;
                   3200:     $part = "0" if (!defined($part));
1.54      bowersj2 3201: 
                   3202:     # Always return network failure if there was one.
1.51      bowersj2 3203:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3204: 
                   3205:     my $now = time();
                   3206: 
1.53      bowersj2 3207:     my $open = $self->opendate($part);
                   3208:     my $due = $self->duedate($part);
                   3209:     my $answer = $self->answerdate($part);
                   3210: 
                   3211:     if (!$open && !$due && !$answer) {
                   3212:         # no data on the problem at all
                   3213:         # should this be the same as "open later"? think multipart.
                   3214:         return $self->NOTHING_SET;
                   3215:     }
1.59      bowersj2 3216:     if (!$open || $now < $open) {return $self->OPEN_LATER}
                   3217:     if (!$due || $now < $due) {return $self->OPEN}
                   3218:     if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
                   3219:     if ($answer) { return $self->ANSWER_OPEN; }
1.54      bowersj2 3220:     return PAST_DUE_NO_ANSWER;
1.51      bowersj2 3221: }
                   3222: 
                   3223: =pod
                   3224: 
                   3225: B<>
                   3226: 
                   3227: =over 4
                   3228: 
1.174     albertel 3229: =item * B<getCompletionStatus>($part):
1.51      bowersj2 3230: 
1.174     albertel 3231: ($part defaults to 0.) A convenience function that returns a symbolic
                   3232: constant telling you about the completion status of the part, with the
                   3233: following possible results:
                   3234: 
                   3235: =back
1.51      bowersj2 3236: 
                   3237: B<Completion Codes>
                   3238: 
                   3239: =over 4
                   3240: 
1.174     albertel 3241: =item * B<NOT_ATTEMPTED>:
                   3242: 
                   3243: Has not been attempted at all.
                   3244: 
                   3245: =item * B<INCORRECT>:
                   3246: 
                   3247: Attempted, but wrong by student.
1.51      bowersj2 3248: 
1.174     albertel 3249: =item * B<INCORRECT_BY_OVERRIDE>:
1.51      bowersj2 3250: 
1.174     albertel 3251: Attempted, but wrong by instructor override.
1.51      bowersj2 3252: 
1.174     albertel 3253: =item * B<CORRECT>:
1.51      bowersj2 3254: 
1.174     albertel 3255: Correct or correct by instructor.
1.51      bowersj2 3256: 
1.174     albertel 3257: =item * B<CORRECT_BY_OVERRIDE>:
1.51      bowersj2 3258: 
1.174     albertel 3259: Correct by instructor override.
1.51      bowersj2 3260: 
1.174     albertel 3261: =item * B<EXCUSED>:
                   3262: 
                   3263: Excused. Not yet implemented.
                   3264: 
                   3265: =item * B<NETWORK_FAILURE>:
                   3266: 
                   3267: Information not available due to network failure.
                   3268: 
                   3269: =item * B<ATTEMPTED>:
                   3270: 
                   3271: Attempted, and not yet graded.
1.69      bowersj2 3272: 
1.51      bowersj2 3273: =back
                   3274: 
                   3275: =cut
                   3276: 
1.53      bowersj2 3277: sub NOT_ATTEMPTED         { return 10; }
                   3278: sub INCORRECT             { return 11; }
                   3279: sub INCORRECT_BY_OVERRIDE { return 12; }
                   3280: sub CORRECT               { return 13; }
                   3281: sub CORRECT_BY_OVERRIDE   { return 14; }
                   3282: sub EXCUSED               { return 15; }
1.69      bowersj2 3283: sub ATTEMPTED             { return 16; }
1.51      bowersj2 3284: 
                   3285: sub getCompletionStatus {
                   3286:     my $self = shift;
                   3287:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3288: 
1.108     bowersj2 3289:     my $status = $self->queryRestoreHash('solved', shift);
1.51      bowersj2 3290: 
                   3291:     # Left as seperate if statements in case we ever do more with this
                   3292:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
                   3293:     if ($status eq 'correct_by_override') {return $self->CORRECT_BY_OVERRIDE; }
                   3294:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
                   3295:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
                   3296:     if ($status eq 'excused') {return $self->EXCUSED; }
1.69      bowersj2 3297:     if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
1.51      bowersj2 3298:     return $self->NOT_ATTEMPTED;
1.108     bowersj2 3299: }
                   3300: 
                   3301: sub queryRestoreHash {
                   3302:     my $self = shift;
                   3303:     my $hashentry = shift;
                   3304:     my $part = shift;
                   3305:     $part = "0" if (!defined($part));
                   3306:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
                   3307: 
                   3308:     $self->getReturnHash();
                   3309: 
                   3310:     return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
1.51      bowersj2 3311: }
                   3312: 
                   3313: =pod
                   3314: 
                   3315: B<Composite Status>
                   3316: 
1.174     albertel 3317: Along with directly returning the date or completion status, the
                   3318: resource object includes a convenience function B<status>() that will
                   3319: combine the two status tidbits into one composite status that can
                   3320: represent the status of the resource as a whole. The precise logic is
                   3321: documented in the comments of the status method. The following results
                   3322: may be returned, all available as methods on the resource object
                   3323: ($res->NETWORK_FAILURE):
1.51      bowersj2 3324: 
                   3325: =over 4
                   3326: 
1.174     albertel 3327: =item * B<NETWORK_FAILURE>:
                   3328: 
                   3329: The network has failed and the information is not available.
1.51      bowersj2 3330: 
1.174     albertel 3331: =item * B<NOTHING_SET>:
1.53      bowersj2 3332: 
1.174     albertel 3333: No dates have been set for this problem (part) at all. (Because only
                   3334: certain parts of a multi-part problem may be assigned, this can not be
                   3335: collapsed into "open later", as we do not know a given part will EVER
                   3336: be opened. For single part, this is the same as "OPEN_LATER".)
1.51      bowersj2 3337: 
1.174     albertel 3338: =item * B<CORRECT>:
1.51      bowersj2 3339: 
1.174     albertel 3340: For any reason at all, the part is considered correct.
1.54      bowersj2 3341: 
1.174     albertel 3342: =item * B<EXCUSED>:
1.51      bowersj2 3343: 
1.174     albertel 3344: For any reason at all, the problem is excused.
1.51      bowersj2 3345: 
1.174     albertel 3346: =item * B<PAST_DUE_NO_ANSWER>:
1.51      bowersj2 3347: 
1.174     albertel 3348: The problem is past due, not considered correct, and no answer date is
                   3349: set.
1.51      bowersj2 3350: 
1.174     albertel 3351: =item * B<PAST_DUE_ANSWER_LATER>:
1.51      bowersj2 3352: 
1.174     albertel 3353: The problem is past due, not considered correct, and an answer date in
                   3354: the future is set.
1.51      bowersj2 3355: 
1.174     albertel 3356: =item * B<ANSWER_OPEN>:
                   3357: 
                   3358: The problem is past due, not correct, and the answer is now available.
                   3359: 
                   3360: =item * B<OPEN_LATER>:
                   3361: 
                   3362: The problem is not yet open.
                   3363: 
                   3364: =item * B<TRIES_LEFT>:
                   3365: 
                   3366: The problem is open, has been tried, is not correct, but there are
                   3367: tries left.
                   3368: 
                   3369: =item * B<INCORRECT>:
                   3370: 
                   3371: The problem is open, and all tries have been used without getting the
                   3372: correct answer.
                   3373: 
                   3374: =item * B<OPEN>:
                   3375: 
                   3376: The item is open and not yet tried.
                   3377: 
                   3378: =item * B<ATTEMPTED>:
                   3379: 
                   3380: The problem has been attempted.
1.69      bowersj2 3381: 
1.51      bowersj2 3382: =back
                   3383: 
                   3384: =cut
                   3385: 
                   3386: sub TRIES_LEFT { return 10; }
                   3387: 
                   3388: sub status {
                   3389:     my $self = shift;
                   3390:     my $part = shift;
                   3391:     if (!defined($part)) { $part = "0"; }
                   3392:     my $completionStatus = $self->getCompletionStatus($part);
                   3393:     my $dateStatus = $self->getDateStatus($part);
                   3394: 
                   3395:     # What we have is a two-dimensional matrix with 4 entries on one
                   3396:     # dimension and 5 entries on the other, which we want to colorize,
1.54      bowersj2 3397:     # plus network failure and "no date data at all".
1.51      bowersj2 3398: 
1.53      bowersj2 3399:     if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
1.51      bowersj2 3400: 
                   3401:     # There are a few whole rows we can dispose of:
1.53      bowersj2 3402:     if ($completionStatus == CORRECT ||
                   3403:         $completionStatus == CORRECT_BY_OVERRIDE ) {
1.69      bowersj2 3404:         return CORRECT; 
                   3405:     }
                   3406: 
                   3407:     if ($completionStatus == ATTEMPTED) {
                   3408:         return ATTEMPTED;
1.53      bowersj2 3409:     }
                   3410: 
                   3411:     # If it's EXCUSED, then return that no matter what
                   3412:     if ($completionStatus == EXCUSED) {
                   3413:         return EXCUSED; 
1.51      bowersj2 3414:     }
                   3415: 
1.53      bowersj2 3416:     if ($dateStatus == NOTHING_SET) {
                   3417:         return NOTHING_SET;
1.51      bowersj2 3418:     }
                   3419: 
1.69      bowersj2 3420:     # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
                   3421:     # by 4 matrix (date statuses).
1.51      bowersj2 3422: 
1.54      bowersj2 3423:     if ($dateStatus == PAST_DUE_ANSWER_LATER ||
1.59      bowersj2 3424:         $dateStatus == PAST_DUE_NO_ANSWER ) {
1.54      bowersj2 3425:         return $dateStatus; 
1.51      bowersj2 3426:     }
                   3427: 
1.53      bowersj2 3428:     if ($dateStatus == ANSWER_OPEN) {
                   3429:         return ANSWER_OPEN;
1.51      bowersj2 3430:     }
                   3431: 
                   3432:     # Now: (incorrect, incorrect_override, not_attempted) x 
                   3433:     # (open_later), (open)
                   3434:     
1.53      bowersj2 3435:     if ($dateStatus == OPEN_LATER) {
                   3436:         return OPEN_LATER;
1.51      bowersj2 3437:     }
                   3438: 
                   3439:     # If it's WRONG...
1.53      bowersj2 3440:     if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
1.51      bowersj2 3441:         # and there are TRIES LEFT:
1.79      bowersj2 3442:         if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
1.53      bowersj2 3443:             return TRIES_LEFT;
1.51      bowersj2 3444:         }
1.53      bowersj2 3445:         return INCORRECT; # otherwise, return orange; student can't fix this
1.51      bowersj2 3446:     }
                   3447: 
                   3448:     # Otherwise, it's untried and open
1.53      bowersj2 3449:     return OPEN; 
1.51      bowersj2 3450: }
                   3451: 
                   3452: =pod
                   3453: 
                   3454: =head2 Resource/Nav Map Navigation
                   3455: 
                   3456: =over 4
                   3457: 
1.174     albertel 3458: =item * B<getNext>():
                   3459: 
                   3460: Retreive an array of the possible next resources after this
                   3461: one. Always returns an array, even in the one- or zero-element case.
                   3462: 
                   3463: =item * B<getPrevious>():
1.85      bowersj2 3464: 
1.174     albertel 3465: Retreive an array of the possible previous resources from this
                   3466: one. Always returns an array, even in the one- or zero-element case.
1.51      bowersj2 3467: 
                   3468: =cut
                   3469: 
                   3470: sub getNext {
                   3471:     my $self = shift;
                   3472:     my @branches;
                   3473:     my $to = $self->to();
1.85      bowersj2 3474:     foreach my $branch ( split(/,/, $to) ) {
1.51      bowersj2 3475:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3476:         my $next = $choice->goesto();
                   3477:         $next = $self->{NAV_MAP}->getById($next);
                   3478: 
1.131     bowersj2 3479:         push @branches, $next;
1.85      bowersj2 3480:     }
                   3481:     return \@branches;
                   3482: }
                   3483: 
                   3484: sub getPrevious {
                   3485:     my $self = shift;
                   3486:     my @branches;
                   3487:     my $from = $self->from();
                   3488:     foreach my $branch ( split /,/, $from) {
                   3489:         my $choice = $self->{NAV_MAP}->getById($branch);
                   3490:         my $prev = $choice->comesfrom();
                   3491:         $prev = $self->{NAV_MAP}->getById($prev);
                   3492: 
1.131     bowersj2 3493:         push @branches, $prev;
1.51      bowersj2 3494:     }
                   3495:     return \@branches;
1.131     bowersj2 3496: }
                   3497: 
                   3498: sub browsePriv {
                   3499:     my $self = shift;
                   3500:     if (defined($self->{BROWSE_PRIV})) {
                   3501:         return $self->{BROWSE_PRIV};
                   3502:     }
                   3503: 
                   3504:     $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre', $self->src());
1.51      bowersj2 3505: }
                   3506: 
                   3507: =pod
1.2       www      3508: 
1.51      bowersj2 3509: =back
1.2       www      3510: 
1.51      bowersj2 3511: =cut
1.2       www      3512: 
1.51      bowersj2 3513: 1;
1.2       www      3514: 
1.51      bowersj2 3515: __END__
1.2       www      3516: 
                   3517: 

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