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

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

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