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

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

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