File:  [LON-CAPA] / loncom / interface / lonnavdisplay.pm
Revision 1.36: download - view: text, annotated - select for diffs
Sat Apr 14 02:29:44 2018 UTC (6 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754 LON-CAPA as LTI Provider
  - Original LTI launch of LON-CAPA is for a resource, a map, or an entire
    course, and display is for iframe, tab or window; retain this in %env.

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

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