File:  [LON-CAPA] / loncom / interface / lonnavdisplay.pm
Revision 1.31: download - view: text, annotated - select for diffs
Sun Mar 1 22:20:56 2015 UTC (9 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- &check_release_required() moved from lonroles.pm to loncommon.pm to
  facilitate reuse.
- Display of Contents page will check if it has been more than 10 minutes
  since last check for content change in course.
 - If it has been: loncommon::needs_coursereinit() will be called to check if
   course was changes after the "Big Hash" was last tied for the user's session
   in the course.
 - The course will be re-initialized (or a Switch Server link displayed),
   as needed.

    1: # The LearningOnline Network with CAPA
    2: # Navigate Maps Display Handler
    3: #
    4: # $Id: lonnavdisplay.pm,v 1.31 2015/03/01 22:20:56 raeburn Exp $
    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: #
   28: ###
   29: 
   30: package Apache::lonnavdisplay;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common :http REDIRECT);
   34: use Apache::lonmenu();
   35: use Apache::loncommon();
   36: use Apache::lonnavmaps();
   37: use Apache::lonhtmlcommon();
   38: use Apache::lonnet;
   39: use Apache::lonlocal;
   40: use Apache::londocs();
   41: 
   42: sub handler {
   43:     my $r = shift;
   44:     real_handler($r);
   45: }
   46: 
   47: sub real_handler {
   48:     my $r = shift;
   49:     # Handle header-only request
   50:     if ($r->header_only) {
   51:         &Apache::loncommon::content_type($r,'text/html');
   52:         $r->send_http_header;
   53:         return OK;
   54:     }
   55: 
   56:     # Check for critical messages and redirect if present.  
   57:     my ($redirect,$url) = &Apache::loncommon::critical_redirect(300);
   58:     if ($redirect) {
   59:         &Apache::loncommon::content_type($r,'text/html');
   60:         $r->header_out(Location => $url);
   61:         return REDIRECT;
   62:     }
   63: 
   64:     # Check if course needs to be re-initialized
   65:     my $loncaparev = $r->dir_config('lonVersion');
   66:     my ($result,@reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
   67: 
   68:     if ($result eq 'switch') {
   69:         &Apache::loncommon::content_type($r,'text/html');
   70:         $r->send_http_header;
   71:         $r->print(&Apache::loncommon::check_release_result(@reinit));
   72:         return OK;
   73:     } elsif ($result eq 'update') {
   74:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   75:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   76:         my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum");
   77:         if ($ferr) {
   78:             my $requrl = $r->uri;
   79:             $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
   80:             $env{'user.reinit'} = 1;
   81:             return HTTP_NOT_ACCEPTABLE;
   82:         }
   83:     }
   84: 
   85:     # Create the nav map
   86:     my $navmap = Apache::lonnavmaps::navmap->new();
   87: 
   88:     if (!defined($navmap)) {
   89:         my $requrl = $r->uri;
   90:         $env{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
   91:         $env{'user.reinit'} = 1;
   92:         return HTTP_NOT_ACCEPTABLE;
   93:     }
   94: 
   95:     # Send header, don't cache this page
   96:     &Apache::loncommon::content_type($r,'text/html');
   97:     $r->send_http_header;
   98: 
   99: # ------------------------------------------------------------ Get query string
  100:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sort','showOnlyHomework','postsymb','register']);
  101:     
  102: # ----------------------------------------------------- Force menu registration
  103:     # Header
  104:     my $course_type = &Apache::loncommon::course_type();
  105:     my $title = $course_type . ' Contents';
  106:     my ($start_page,$args);
  107:     if ($env{'form.register'}) {
  108:         $args = {'force_register' => $env{'form.register'}};
  109:         $start_page = &Apache::loncommon::start_page($title,undef,$args);
  110:     } else {
  111:         my $brcrum = [{href => '/adm/navmaps',
  112:                        text => &mt($course_type . ' Contents'),
  113:                        no_mt => 1},
  114:                      ];
  115:         $args = {'bread_crumbs' => $brcrum};
  116:         $start_page = &Apache::loncommon::start_page($title,undef,$args);
  117:     }
  118:     $r->print($start_page.
  119:               '<script type="text/javascript">'."\n".
  120:               '// <![CDATA['."\n".
  121:               'window.focus();'."\n".
  122:               '// ]]>'."\n".
  123:               '</script>');
  124:     &startContentScreen($r,'navmaps');
  125:     $r->rflush();
  126: 
  127:     # Check that it's defined
  128:     if (!($navmap->courseMapDefined())) {
  129: 	$r->print(&Apache::loncommon::help_open_menu('Navigation Screen','Navigation_Screen',undef,'RAT'));
  130:         $r->print('<span class="LC_error">'.&mt('Coursemap undefined.').
  131: 		  '</span>' .
  132:                   &Apache::loncommon::end_page());
  133:         return OK;
  134:     }
  135: 
  136:     my %toplinkitems=();
  137:     my @resources = $navmap->retrieveResources();
  138:     my $sequenceCount = 0;
  139:     my $problemCount = 0;
  140:     my $notaprobCount = 0;
  141:     my $sequenceId;
  142:     my $notools;
  143:     foreach my $curRes (@resources) {
  144:         if (ref($curRes)) {
  145:             if ($curRes->is_sequence()) {
  146:                 $sequenceCount++;
  147:                 $sequenceId = $curRes->map_pc();
  148:             } elsif ($curRes->is_problem()) {
  149:                 $problemCount ++;
  150:             } else {
  151:                 $notaprobCount ++;
  152:             }
  153:         }
  154:     }
  155:     if (($sequenceCount == 1) && (!$problemCount) && ($notaprobCount <= 1)) {
  156:         $notools = 1;
  157:     }
  158: 
  159:     # If there's only one map in the top-level and we don't
  160:     # already have a filter, automatically display it
  161:     if ($ENV{QUERY_STRING} !~ /filter/) {
  162:         if ($sequenceCount == 1) {
  163:             # The automatic iterator creation in the render call 
  164:             # will pick this up. We know the condition because
  165:             # the defined($env{'form.filter'}) also ensures this
  166:             # is a fresh call.
  167:             $env{'form.filter'} = "$sequenceId";
  168:         }
  169:     }
  170: 
  171:     # Check to see if the student is jumping to next open, do-able problem
  172:     if ($ENV{QUERY_STRING} =~ /^jumpToFirstHomework/) {
  173:         # Find the next homework problem that they can do.
  174:         my $iterator = $navmap->getIterator(undef, undef, undef, 1);
  175:         my $curRes;
  176:         my $foundDoableProblem = 0;
  177:         my $minimumduedate;
  178:         my $now = time();
  179: 	
  180:         while ($curRes = $iterator->next()) {
  181:             if (ref($curRes) && $curRes->is_problem()) {
  182:                 my $status = $curRes->status();
  183: 		my $thisduedate=$curRes->duedate();
  184:                 if ($thisduedate > $now 
  185: 		    && $curRes->completable()) {
  186:                         
  187:                     $foundDoableProblem = 1;
  188: 
  189:                     if (!defined($minimumduedate)
  190:                         || $thisduedate<$minimumduedate) {
  191: 			# Pop open all previous maps
  192: 			my $stack = $iterator->getStack();
  193: 			pop @$stack; # last resource in the stack is the problem
  194: 			# itself, which we don't need in the map stack
  195: 			my @mapPcs = map {$_->map_pc()} @$stack;
  196: 			$env{'form.filter'} = join(',', @mapPcs);
  197: 			
  198: 			# Mark as both "here" and "jump"
  199: 			$env{'form.postsymb'} = $curRes->symb();
  200:                         $minimumduedate=$thisduedate;
  201: 		    }
  202:                 }
  203:             }
  204:         }
  205: 
  206:         # If we found no problems, print a note to that effect.
  207:         if (!$foundDoableProblem) {
  208:             $r->print("<span class=\"LC_info\">"
  209:                      .&mt("All homework assignments have been completed.")
  210:                      ."</span>");
  211:         }
  212:     } else {
  213:         my $link = 'navmaps?jumpToFirstHomework';
  214:         if ($env{'form.register'}) {
  215:             $link .= '&amp;register='.$env{'form.register'}; 
  216:         }
  217:         unless ($notools) {
  218: 	    &Apache::lonnavmaps::add_linkitem(\%toplinkitems,'firsthomework',
  219: 					      'location.href="'.$link.'"',
  220: 					      "Show my first due problem");
  221:         }
  222:     }
  223: 
  224:     my $suppressEmptySequences = 0;
  225:     my $filterFunc = undef;
  226:     my $resource_no_folder_link = 0;
  227: 
  228:     # Display only due homework.
  229:     my $showOnlyHomework = 0;
  230:     if ($env{'form.showOnlyHomework'} eq "1") {
  231:         $showOnlyHomework = 1;
  232:         $suppressEmptySequences = 1;
  233:         $filterFunc = sub { my $res = shift; 
  234:                             return $res->completable() || $res->is_map();
  235:                         };
  236:         my $link = 'navmaps?sort='.$env{'form.sort'};
  237:         if ($env{'form.register'}) {
  238:             $link .= '&amp;register='.$env{'form.register'};
  239:         }
  240: 	&Apache::lonnavmaps::add_linkitem(\%toplinkitems,'everything',
  241: 					  'location.href="'.$link.'"',
  242: 					  'Show everything');
  243:         $r->print("<span class=\"LC_info\">".&mt("Uncompleted Problems")."</span>");
  244:         $env{'form.filter'} = '';
  245:         $env{'form.condition'} = 1;
  246: 	$resource_no_folder_link = 1;
  247:     } else {
  248:         my $link = 'navmaps?sort='.$env{'form.sort'}.'&amp;showOnlyHomework=1';
  249:         if ($env{'form.register'}) {
  250:             $link .= '&amp;register='.$env{'form.register'};
  251:         }
  252:         unless ($notools) {
  253: 	    &Apache::lonnavmaps::add_linkitem(\%toplinkitems,'uncompleted',
  254: 					      'location.href="'.$link.'"',
  255: 					      'Show only uncompleted problems');
  256:         }
  257:     }
  258: 
  259:     my %selected=($env{'form.sort'} => ' selected="selected"');
  260:     my $sort_html;
  261:     unless ($notools) {
  262:         $sort_html=(
  263:               '<form name="sortForm" action="">
  264:                  <span class="LC_nobreak">
  265:                     <input type="hidden" name="showOnlyHomework" value="'.$env{'form.showOnlyHomework'}.'" />
  266:                     '.&mt('Sort by:').'
  267:                     <select name="sort" onchange="document.sortForm.submit()">
  268:                        <option value="default"'.$selected{'default'}.'>'.&mt('Default').'</option>
  269:                        <option value="title"'.$selected{'title'}.'>'.&mt('Title').'</option>
  270:                        <option value="duedate"'.$selected{'duedate'}.'>'.&mt('Due Date').'</option>
  271:                        <option value="discussion"'.$selected{'discussion'}.'>'.&mt('Has New Discussion').'</option>
  272:                     </select>
  273:                     <input type="hidden" name="register" value="'.$env{'form.register'}.'" />
  274:                  </span>
  275:                </form>');
  276:     }
  277:     # renderer call
  278:     my $renderArgs = { 'cols' => [0,1,2,3],
  279: 		       'sort' => $env{'form.sort'},
  280:                        'url' => '/adm/navmaps',
  281:                        'navmap' => $navmap,
  282:                        'suppressNavmap' => 1,
  283:                        'suppressEmptySequences' => $suppressEmptySequences,
  284:                        'filterFunc' => $filterFunc,
  285: 		       'resource_no_folder_link' => $resource_no_folder_link,
  286: 		       'sort_html'=> $sort_html,
  287:                        'r' => $r,
  288:                        'caller' => 'navmapsdisplay',
  289:                        'linkitems' => \%toplinkitems,
  290:                        'notools' => $notools};
  291:                       
  292:     my $render = &Apache::lonnavmaps::render($renderArgs);
  293: 
  294:     # If no resources were printed, print a reassuring message so the
  295:     # user knows there was no error.
  296:     if ($renderArgs->{'counter'} == 0) {
  297:         if ($showOnlyHomework) {
  298:             $r->print("<p><span class=\"LC_info\">".&mt("All homework is currently completed.")."</span></p>");
  299:         } else { # both jumpToFirstHomework and normal use the same: course must be empty
  300:             $r->print("<p><span class=\"LC_info\">".&mt("This course is empty.")."</span></p>");
  301:         }
  302:     }
  303:     &endContentScreen($r);
  304:     $r->print(&Apache::loncommon::end_page());
  305:     $r->rflush();
  306: 
  307:     return OK;
  308: }
  309: 
  310: sub startContentScreen {
  311:     my ($r,$mode)=@_;
  312: 
  313:     $r->print("\n".'<ul class="LC_TabContentBigger" id="mainnav">'."\n");
  314:     $r->print('<li'.(($mode eq 'navmaps')?' class="active"':'').'><a href="/adm/navmaps"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Main Content').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n");
  315:     my $allowed = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  316:     my ($suppcount,$errors);
  317:     unless ($allowed) {
  318:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  319:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  320:         ($suppcount,$errors) = &Apache::lonnet::get_numsuppfiles($cnum,$cdom);
  321:     }
  322:     if ($allowed || $suppcount) {
  323:         $r->print('<li '.(($mode eq 'supplemental')?' class="active"':'').'><a href="/adm/supplemental"><b>'.&mt('Supplemental Content').'</b></a></li>');
  324:     }
  325:     $r->print('<li'.(($mode eq 'coursesearch')?' class="active"':'').'><a href="/adm/searchcourse"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Content Search').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n");
  326:     $r->print('<li'.(($mode eq 'courseindex')?' class="active"':'').'><a href="/adm/indexcourse"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Content Index').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n");
  327:     $r->print("\n".'</ul>'."\n");
  328:     $r->print('<div class="LC_Box" style="clear:both;margin:0;"><div id="maincoursedoc" style="margin:0 0;padding:0 0;"><div class="LC_ContentBox" id="mainCourseDocuments" style="display: block;">');
  329: }
  330: 
  331: sub endContentScreen {
  332:    my ($r)=@_;
  333:    $r->print('</div></div></div>');
  334: }
  335: 
  336: 1;
  337: __END__
  338: 

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