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

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

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