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

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

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