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

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

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