File:  [LON-CAPA] / loncom / interface / lonnavdisplay.pm
Revision 1.22.4.9.2.1: download - view: text, annotated - select for diffs
Mon May 25 16:12:12 2020 UTC (3 years, 11 months ago) by raeburn
Branches: version_2_11_2_msu
Diff to branchpoint 1.22.4.9: preferred, unified
- For 2.11.2 (modified)
  Include changes in 1.34

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

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