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

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

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