File:  [LON-CAPA] / loncom / interface / lonnavdisplay.pm
Revision 1.42: download - view: text, annotated - select for diffs
Tue Jul 25 04:58:49 2023 UTC (9 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Prevent endless redirects in empty Placement Test course.

    1: # The LearningOnline Network with CAPA
    2: # Navigate Maps Display Handler
    3: #
    4: # $Id: lonnavdisplay.pm,v 1.42 2023/07/25 04:58:49 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: use Apache::lonuserstate;
   42: use LONCAPA::ltiutils;
   43: 
   44: sub handler {
   45:     my $r = shift;
   46:     real_handler($r);
   47: }
   48: 
   49: sub real_handler {
   50:     my $r = shift;
   51:     # Handle header-only request
   52:     if ($r->header_only) {
   53:         &Apache::loncommon::content_type($r,'text/html');
   54:         $r->send_http_header;
   55:         return OK;
   56:     }
   57: 
   58:     # Check for critical messages and redirect if present.  
   59:     my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'contents');
   60:     if ($redirect) {
   61:         &Apache::loncommon::content_type($r,'text/html');
   62:         $r->header_out(Location => $url);
   63:         return REDIRECT;
   64:     }
   65: 
   66: # ------------------------------------------------------------ Get query string
   67:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sort',
   68:                                                                   'showOnlyHomework',
   69:                                                                   'postsymb']);
   70:     # Check if course needs to be re-initialized
   71:     my $loncaparev = $r->dir_config('lonVersion');
   72:     my ($result,@reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
   73:     my %prog_state=();
   74:     my $closure;
   75: 
   76:     if ($result eq 'switch') {
   77:         &Apache::loncommon::content_type($r,'text/html');
   78:         $r->send_http_header;
   79:         $r->print(&Apache::loncommon::check_release_result(@reinit));
   80:         return OK;
   81:     }
   82:     my ($cid,$cnum,$cdom);
   83:     if ($result) {
   84:         $cid = $env{'request.course.id'};
   85:         $cnum = $env{'course.'.$cid.'.num'};
   86:         $cdom = $env{'course.'.$cid.'.domain'};
   87:     }
   88:     if (($result eq 'main') || ($result eq 'both')) {
   89:         &Apache::loncommon::content_type($r,'text/html');
   90:         $r->send_http_header;
   91:         &startpage($r);
   92:         my $preamble = '<div id="LC_update_'.$cid.'" class="LC_info">'.
   93:                        '<br />'.
   94:                        &mt('Your course session is being updated because of recent changes by course personnel.').
   95:                        ' '.&mt('Please be patient').'.<br /></div>'.
   96:                        '<div style="padding:0;clear:both;margin:0;border:0"></div>';
   97:         $closure = <<ENDCLOSE;
   98: <script type="text/javascript">
   99: // <![CDATA[
  100: \$("#LC_update_$cid").hide('slow');
  101: // ]]>
  102: </script>
  103: ENDCLOSE
  104:         %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
  105:         &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Updating course'));
  106:         $r->rflush();
  107:         my ($furl,$ferr) = &Apache::lonuserstate::readmap("$cdom/$cnum",\%prog_state,$r);
  108:         &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!'));
  109:         if ($ferr) {
  110:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  111:             $r->print($closure.&Apache::loncommon::end_page());
  112:             my $requrl = $r->uri;
  113:             $env{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  114:             $env{'user.reinit'} = 1;
  115:             return HTTP_NOT_ACCEPTABLE;
  116:         }
  117:     }
  118:     if (($result eq 'both') || ($result eq 'supp')) {
  119:         my $possdel;
  120:         if ($result eq 'supp') {
  121:             $possdel = 1;
  122:         }
  123:         my ($supplemental,$refs_updated) = &Apache::loncommon::get_supplemental($cnum,$cdom,'',$possdel);
  124:         unless ($refs_updated) {
  125:             &Apache::loncommon::set_supp_httprefs($cnum,$cdom,$supplemental,$possdel);
  126:         }
  127:     }
  128: 
  129:     my $course_type = &Apache::loncommon::course_type();
  130:     if (($course_type eq 'Placement') && (!$env{'request.role.adv'})) { 
  131:         my $furl = &Apache::lonpageflip::first_accessible_resource();
  132:         if ($result eq 'update') {
  133:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  134:             $r->print($closure.&Apache::loncommon::end_page());
  135:             return OK;
  136:         } else {
  137:             unless ($furl eq '/adm/navmaps') {
  138:                 &Apache::loncommon::content_type($r,'text/html');
  139:                 $r->header_out(Location => $furl);
  140:                 return REDIRECT;
  141:             }
  142:         }
  143:     }
  144: 
  145:     if ($env{'request.lti.login'}) {
  146:         if ($env{'request.lti.uri'} ne '') {
  147:             my $cid = $env{'request.course.id'};
  148:             my $cnum = $env{'course.'.$cid.'.num'};
  149:             my $cdom = $env{'course.'.$cid.'.domain'};
  150:             my ($scope,$url) = &LONCAPA::ltiutils::lti_provider_scope($env{'request.lti.uri'},$cdom,$cnum);
  151:             if (($scope eq 'map') || ($scope eq 'resource')) {
  152:                 &Apache::loncommon::content_type($r,'text/html');
  153:                 $r->header_out(Location => $url);
  154:                 return REDIRECT;
  155:             }
  156:         }
  157:     }
  158: 
  159:     # Create the nav map
  160:     my $navmap = Apache::lonnavmaps::navmap->new();
  161: 
  162:     if (!defined($navmap)) {
  163:         if ($result eq 'update') {
  164:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  165:             $r->print($closure.&Apache::loncommon::end_page());
  166:         }
  167:         my $requrl = $r->uri;
  168:         $env{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
  169:         $env{'user.reinit'} = 1;
  170:         return HTTP_NOT_ACCEPTABLE;
  171:     }
  172: 
  173:     if ($result eq 'update') {
  174:         $r->rflush();
  175:         &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  176:         $r->print($closure);
  177:         $r->rflush();
  178:     } else {
  179:         # Send header, don't cache this page
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         &startpage($r);
  183:     }
  184: 
  185:     &startContentScreen($r,'navmaps',$course_type);
  186:     unless ($result eq 'update') {
  187:         $r->rflush();
  188:     }
  189: 
  190:     # Check that it's defined
  191:     if (!($navmap->courseMapDefined())) {
  192: 	$r->print(&Apache::loncommon::help_open_menu('Navigation Screen','Navigation_Screen',undef,'RAT'));
  193:         $r->print('<span class="LC_error">'.&mt('Coursemap undefined.').
  194: 		  '</span>' .
  195:                   &Apache::loncommon::end_page());
  196:         return OK;
  197:     }
  198: 
  199:     my %toplinkitems=();
  200:     my @resources = $navmap->retrieveResources();
  201:     my $sequenceCount = 0;
  202:     my $problemCount = 0;
  203:     my $notaprobCount = 0;
  204:     my $sequenceId;
  205:     my $notools;
  206:     foreach my $curRes (@resources) {
  207:         if (ref($curRes)) {
  208:             if ($curRes->is_sequence()) {
  209:                 $sequenceCount++;
  210:                 $sequenceId = $curRes->map_pc();
  211:             } elsif ($curRes->is_problem()) {
  212:                 $problemCount ++;
  213:             } else {
  214:                 $notaprobCount ++;
  215:             }
  216:         }
  217:     }
  218:     if (($sequenceCount == 1) && (!$problemCount) && ($notaprobCount <= 1)) {
  219:         $notools = 1;
  220:     }
  221: 
  222:     # If there's only one map in the top-level and we don't
  223:     # already have a filter, automatically display it
  224:     if ($ENV{QUERY_STRING} !~ /filter/) {
  225:         if ($sequenceCount == 1) {
  226:             # The automatic iterator creation in the render call 
  227:             # will pick this up. We know the condition because
  228:             # the defined($env{'form.filter'}) also ensures this
  229:             # is a fresh call.
  230:             $env{'form.filter'} = "$sequenceId";
  231:         }
  232:     }
  233: 
  234:     # Check to see if the student is jumping to next open, do-able problem
  235:     if ($ENV{QUERY_STRING} =~ /^jumpToFirstHomework/) {
  236:         # Find the next homework problem that they can do.
  237:         my $iterator = $navmap->getIterator(undef, undef, undef, 1);
  238:         my $curRes;
  239:         my $foundDoableProblem = 0;
  240:         my $minimumduedate;
  241:         my $now = time();
  242: 	
  243:         while ($curRes = $iterator->next()) {
  244:             if (ref($curRes) && $curRes->is_problem()) {
  245:                 my $status = $curRes->status();
  246: 		my $thisduedate=$curRes->duedate();
  247:                 if ($thisduedate > $now 
  248: 		    && $curRes->completable()) {
  249:                         
  250:                     $foundDoableProblem = 1;
  251: 
  252:                     if (!defined($minimumduedate)
  253:                         || $thisduedate<$minimumduedate) {
  254: 			# Pop open all previous maps
  255: 			my $stack = $iterator->getStack();
  256: 			pop @$stack; # last resource in the stack is the problem
  257: 			# itself, which we don't need in the map stack
  258: 			my @mapPcs = map {$_->map_pc()} @$stack;
  259: 			$env{'form.filter'} = join(',', @mapPcs);
  260: 			
  261: 			# Mark as both "here" and "jump"
  262: 			$env{'form.postsymb'} = $curRes->symb();
  263:                         $minimumduedate=$thisduedate;
  264: 		    }
  265:                 }
  266:             }
  267:         }
  268: 
  269:         # If we found no problems, print a note to that effect.
  270:         if (!$foundDoableProblem) {
  271:             $r->print("<span class=\"LC_info\">"
  272:                      .&mt("All homework assignments have been completed.")
  273:                      ."</span>");
  274:         }
  275:     } else {
  276:         my $link = '/adm/navmaps?jumpToFirstHomework';
  277:         unless ($notools) {
  278: 	    &Apache::lonnavmaps::add_linkitem(\%toplinkitems,'firsthomework',
  279: 					      'location.href="'.$link.'"',
  280: 					      "Show my first due problem");
  281:         }
  282:     }
  283: 
  284:     my $suppressEmptySequences = 0;
  285:     my $filterFunc = undef;
  286:     my $resource_no_folder_link = 0;
  287: 
  288:     # Display only due homework.
  289:     my $showOnlyHomework = 0;
  290:     if ($env{'form.showOnlyHomework'} eq "1") {
  291:         $showOnlyHomework = 1;
  292:         $suppressEmptySequences = 1;
  293:         $filterFunc = sub { my $res = shift; 
  294:                             return $res->completable() || $res->is_map();
  295:                         };
  296:         my $link = '/adm/navmaps?sort='.$env{'form.sort'};
  297: 	&Apache::lonnavmaps::add_linkitem(\%toplinkitems,'everything',
  298: 					  'location.href="'.$link.'"',
  299: 					  'Show everything');
  300:         $r->print("<span class=\"LC_info\">".&mt("Uncompleted Problems")."</span>");
  301:         $env{'form.filter'} = '';
  302:         $env{'form.condition'} = 1;
  303: 	$resource_no_folder_link = 1;
  304:     } else {
  305:         my $link = '/adm/navmaps?sort='.$env{'form.sort'}.'&amp;showOnlyHomework=1';
  306:         unless ($notools) {
  307: 	    &Apache::lonnavmaps::add_linkitem(\%toplinkitems,'uncompleted',
  308: 					      'location.href="'.$link.'"',
  309: 					      'Show only uncompleted problems');
  310:         }
  311:     }
  312: 
  313:     my %selected=($env{'form.sort'} => ' selected="selected"');
  314:     my $sort_html;
  315:     unless ($notools) {
  316:         $sort_html=(
  317:               '<form name="sortForm" action="">
  318:                  <span class="LC_nobreak">
  319:                     <input type="hidden" name="showOnlyHomework" value="'.$env{'form.showOnlyHomework'}.'" />
  320:                     '.&mt('Sort by:').'
  321:                     <select name="sort" onchange="document.sortForm.submit()">
  322:                        <option value="default"'.$selected{'default'}.'>'.&mt('Default').'</option>
  323:                        <option value="title"'.$selected{'title'}.'>'.&mt('Title').'</option>
  324:                        <option value="duedate"'.$selected{'duedate'}.'>'.&mt('Due Date').'</option>
  325:                        <option value="discussion"'.$selected{'discussion'}.'>'.&mt('Has New Discussion').'</option>
  326:                     </select>
  327:                  </span>
  328:                </form>');
  329:     }
  330:     # renderer call
  331:     my $renderArgs = { 'cols' => [0,1,2,3],
  332: 		       'sort' => $env{'form.sort'},
  333:                        'url' => '/adm/navmaps',
  334:                        'navmap' => $navmap,
  335:                        'suppressNavmap' => 1,
  336:                        'suppressEmptySequences' => $suppressEmptySequences,
  337:                        'filterFunc' => $filterFunc,
  338: 		       'resource_no_folder_link' => $resource_no_folder_link,
  339: 		       'sort_html'=> $sort_html,
  340:                        'r' => $r,
  341:                        'caller' => 'navmapsdisplay',
  342:                        'linkitems' => \%toplinkitems,
  343:                        'notools' => $notools};
  344:                       
  345:     my $render = &Apache::lonnavmaps::render($renderArgs);
  346: 
  347:     # If no resources were printed, print a reassuring message so the
  348:     # user knows there was no error.
  349:     if ($renderArgs->{'counter'} == 0) {
  350:         if ($showOnlyHomework) {
  351:             $r->print("<p><span class=\"LC_info\">".&mt("All homework is currently completed.")."</span></p>");
  352:         } else { # both jumpToFirstHomework and normal use the same: course must be empty
  353:             $r->print("<p><span class=\"LC_info\">".&mt("This course is empty.")."</span></p>");
  354:         }
  355:     }
  356:     &endContentScreen($r);
  357:     $r->print(&Apache::loncommon::end_page());
  358:     $r->rflush();
  359: 
  360:     return OK;
  361: }
  362: 
  363: sub startpage {
  364:     my ($r) = @_;
  365: # ----------------------------------------------------- Force menu registration
  366:     # Header
  367:     my $course_type = &Apache::loncommon::course_type();
  368:     my $title = $course_type . ' Contents';
  369:     my $brcrum = [{href => '/adm/navmaps',
  370:                    text => &mt($course_type . ' Contents'),
  371:                    no_mt => 1},
  372:                  ];
  373:     my $args = {'bread_crumbs' => $brcrum};
  374:     my $start_page = &Apache::loncommon::start_page($title,undef,$args);
  375:     $r->print($start_page.
  376:               '<script type="text/javascript">'."\n".
  377:               '// <![CDATA['."\n".
  378:               'window.focus();'."\n".
  379:               '// ]]>'."\n".
  380:               '</script>');
  381:     return;
  382: }
  383: 
  384: sub startContentScreen {
  385:     my ($r,$mode,$course_type)=@_;
  386: 
  387:     $r->print("\n".'<ul class="LC_TabContentBigger" id="mainnav">'."\n");
  388:     $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");
  389:     my $supptab;
  390:     if ($env{'request.role.adv'}) {
  391:         $supptab = 1;
  392:     } else {
  393:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  394:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  395:         $supptab = &Apache::lonnet::has_unhidden_suppfiles($cnum,$cdom);
  396:     }
  397:     if ($supptab) {
  398:         $r->print('<li '.(($mode eq 'supplemental')?' class="active"':'').'><a href="/adm/supplemental"><b>'.&mt('Supplemental Content').'</b></a></li>');
  399:     }
  400:     unless ($course_type eq 'Placement') {
  401:         $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");
  402:         $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");
  403:     }
  404:     $r->print("\n".'</ul>'."\n");
  405:     $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;">');
  406: }
  407: 
  408: sub endContentScreen {
  409:    my ($r)=@_;
  410:    $r->print('</div></div></div>');
  411: }
  412: 
  413: 1;
  414: __END__
  415: 

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