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

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

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