Annotation of loncom/interface/lonnavdisplay.pm, revision 1.39

1.1       albertel    1: # The LearningOnline Network with CAPA
1.28      raeburn     2: # Navigate Maps Display Handler
1.1       albertel    3: #
1.39    ! raeburn     4: # $Id: lonnavdisplay.pm,v 1.38 2021/02/18 14:48:02 raeburn Exp $
1.1       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: #
                     28: ###
                     29: 
                     30: package Apache::lonnavdisplay;
                     31: 
                     32: use strict;
1.27      musolffc   33: use Apache::Constants qw(:common :http REDIRECT);
1.1       albertel   34: use Apache::lonmenu();
                     35: use Apache::loncommon();
                     36: use Apache::lonnavmaps();
                     37: use Apache::lonhtmlcommon();
                     38: use Apache::lonnet;
                     39: use Apache::lonlocal;
1.19      www        40: use Apache::londocs();
1.32      raeburn    41: use Apache::lonuserstate;
1.36      raeburn    42: use LONCAPA::ltiutils;
1.1       albertel   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) {
1.30      raeburn    53:         &Apache::loncommon::content_type($r,'text/html');
1.1       albertel   54:         $r->send_http_header;
                     55:         return OK;
                     56:     }
                     57: 
1.27      musolffc   58:     # Check for critical messages and redirect if present.  
1.34      raeburn    59:     my ($redirect,$url) = &Apache::loncommon::critical_redirect(300,'contents');
1.27      musolffc   60:     if ($redirect) {
                     61:         &Apache::loncommon::content_type($r,'text/html');
                     62:         $r->header_out(Location => $url);
                     63:         return REDIRECT;
                     64:     }
                     65: 
1.35      raeburn    66: # ------------------------------------------------------------ Get query string
                     67:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sort',
                     68:                                                                   'showOnlyHomework',
1.39    ! raeburn    69:                                                                   'postsymb']);
1.31      raeburn    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);
1.35      raeburn    73:     my %prog_state=();
                     74:     my $closure;
1.31      raeburn    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') {
1.35      raeburn    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.').
1.38      raeburn    91:                        ' '.&mt('Please be patient').'.<br /></div>'.
1.35      raeburn    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);
1.38      raeburn   104:         &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Finished!'));
1.31      raeburn   105:         if ($ferr) {
1.35      raeburn   106:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    107:             $r->print($closure.&Apache::loncommon::end_page());
1.31      raeburn   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: 
1.33      raeburn   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();
1.35      raeburn   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:         }
1.33      raeburn   127:     }
                    128: 
1.36      raeburn   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: 
1.1       albertel  143:     # Create the nav map
                    144:     my $navmap = Apache::lonnavmaps::navmap->new();
                    145: 
                    146:     if (!defined($navmap)) {
1.35      raeburn   147:         if ($result eq 'update') {
                    148:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    149:             $r->print($closure.&Apache::loncommon::end_page());
                    150:         }
1.1       albertel  151:         my $requrl = $r->uri;
                    152:         $env{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
1.5       raeburn   153:         $env{'user.reinit'} = 1;
1.1       albertel  154:         return HTTP_NOT_ACCEPTABLE;
                    155:     }
1.30      raeburn   156: 
1.35      raeburn   157:     if ($result eq 'update') {
                    158:         $r->rflush();
                    159:         &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    160:         $r->print($closure);
                    161:         $r->rflush();
1.17      raeburn   162:     } else {
1.35      raeburn   163:         # Send header, don't cache this page
                    164:         &Apache::loncommon::content_type($r,'text/html');
                    165:         $r->send_http_header;
                    166:         &startpage($r);
1.17      raeburn   167:     }
1.35      raeburn   168: 
1.20      raeburn   169:     &startContentScreen($r,'navmaps');
1.35      raeburn   170:     unless ($result eq 'update') {
                    171:         $r->rflush();
                    172:     }
1.1       albertel  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: 
1.30      raeburn   183:     my %toplinkitems=();
1.21      raeburn   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()) {
1.1       albertel  193:                 $sequenceCount++;
                    194:                 $sequenceId = $curRes->map_pc();
1.21      raeburn   195:             } elsif ($curRes->is_problem()) {
                    196:                 $problemCount ++;
                    197:             } else {
                    198:                 $notaprobCount ++;
1.1       albertel  199:             }
                    200:         }
1.21      raeburn   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/) {
1.1       albertel  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;
1.2       albertel  225:         my $now = time();
                    226: 	
1.1       albertel  227:         while ($curRes = $iterator->next()) {
                    228:             if (ref($curRes) && $curRes->is_problem()) {
                    229:                 my $status = $curRes->status();
1.2       albertel  230: 		my $thisduedate=$curRes->duedate();
                    231:                 if ($thisduedate > $now 
                    232: 		    && $curRes->completable()) {
1.1       albertel  233:                         
                    234:                     $foundDoableProblem = 1;
                    235: 
1.2       albertel  236:                     if (!defined($minimumduedate)
                    237:                         || $thisduedate<$minimumduedate) {
1.1       albertel  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) {
1.7       schulted  255:             $r->print("<span class=\"LC_info\">"
1.4       bisitz    256:                      .&mt("All homework assignments have been completed.")
1.7       schulted  257:                      ."</span>");
1.1       albertel  258:         }
                    259:     } else {
1.37      raeburn   260:         my $link = '/adm/navmaps?jumpToFirstHomework';
1.21      raeburn   261:         unless ($notools) {
                    262: 	    &Apache::lonnavmaps::add_linkitem(\%toplinkitems,'firsthomework',
                    263: 					      'location.href="'.$link.'"',
                    264: 					      "Show my first due problem");
                    265:         }
1.1       albertel  266:     }
                    267: 
                    268:     my $suppressEmptySequences = 0;
                    269:     my $filterFunc = undef;
                    270:     my $resource_no_folder_link = 0;
                    271: 
                    272:     # Display only due homework.
                    273:     my $showOnlyHomework = 0;
                    274:     if ($env{'form.showOnlyHomework'} eq "1") {
                    275:         $showOnlyHomework = 1;
                    276:         $suppressEmptySequences = 1;
                    277:         $filterFunc = sub { my $res = shift; 
                    278:                             return $res->completable() || $res->is_map();
                    279:                         };
1.37      raeburn   280:         my $link = '/adm/navmaps?sort='.$env{'form.sort'};
1.1       albertel  281: 	&Apache::lonnavmaps::add_linkitem(\%toplinkitems,'everything',
1.17      raeburn   282: 					  'location.href="'.$link.'"',
                    283: 					  'Show everything');
1.7       schulted  284:         $r->print("<span class=\"LC_info\">".&mt("Uncompleted Problems")."</span>");
1.1       albertel  285:         $env{'form.filter'} = '';
                    286:         $env{'form.condition'} = 1;
                    287: 	$resource_no_folder_link = 1;
                    288:     } else {
1.37      raeburn   289:         my $link = '/adm/navmaps?sort='.$env{'form.sort'}.'&amp;showOnlyHomework=1';
1.21      raeburn   290:         unless ($notools) {
                    291: 	    &Apache::lonnavmaps::add_linkitem(\%toplinkitems,'uncompleted',
                    292: 					      'location.href="'.$link.'"',
                    293: 					      'Show only uncompleted problems');
                    294:         }
1.1       albertel  295:     }
                    296: 
1.10      bisitz    297:     my %selected=($env{'form.sort'} => ' selected="selected"');
1.21      raeburn   298:     my $sort_html;
                    299:     unless ($notools) {
                    300:         $sort_html=(
                    301:               '<form name="sortForm" action="">
1.17      raeburn   302:                  <span class="LC_nobreak">
                    303:                     <input type="hidden" name="showOnlyHomework" value="'.$env{'form.showOnlyHomework'}.'" />
                    304:                     '.&mt('Sort by:').'
1.18      bisitz    305:                     <select name="sort" onchange="document.sortForm.submit()">
1.17      raeburn   306:                        <option value="default"'.$selected{'default'}.'>'.&mt('Default').'</option>
                    307:                        <option value="title"'.$selected{'title'}.'>'.&mt('Title').'</option>
1.25      bisitz    308:                        <option value="duedate"'.$selected{'duedate'}.'>'.&mt('Due Date').'</option>
1.17      raeburn   309:                        <option value="discussion"'.$selected{'discussion'}.'>'.&mt('Has New Discussion').'</option>
1.1       albertel  310:                     </select>
1.9       bisitz    311:                  </span>
1.17      raeburn   312:                </form>');
1.21      raeburn   313:     }
1.1       albertel  314:     # renderer call
                    315:     my $renderArgs = { 'cols' => [0,1,2,3],
                    316: 		       'sort' => $env{'form.sort'},
                    317:                        'url' => '/adm/navmaps',
                    318:                        'navmap' => $navmap,
                    319:                        'suppressNavmap' => 1,
                    320:                        'suppressEmptySequences' => $suppressEmptySequences,
                    321:                        'filterFunc' => $filterFunc,
                    322: 		       'resource_no_folder_link' => $resource_no_folder_link,
                    323: 		       'sort_html'=> $sort_html,
                    324:                        'r' => $r,
                    325:                        'caller' => 'navmapsdisplay',
1.21      raeburn   326:                        'linkitems' => \%toplinkitems,
                    327:                        'notools' => $notools};
                    328:                       
1.1       albertel  329:     my $render = &Apache::lonnavmaps::render($renderArgs);
                    330: 
                    331:     # If no resources were printed, print a reassuring message so the
                    332:     # user knows there was no error.
                    333:     if ($renderArgs->{'counter'} == 0) {
                    334:         if ($showOnlyHomework) {
1.7       schulted  335:             $r->print("<p><span class=\"LC_info\">".&mt("All homework is currently completed.")."</span></p>");
1.1       albertel  336:         } else { # both jumpToFirstHomework and normal use the same: course must be empty
1.7       schulted  337:             $r->print("<p><span class=\"LC_info\">".&mt("This course is empty.")."</span></p>");
1.1       albertel  338:         }
                    339:     }
1.20      raeburn   340:     &endContentScreen($r);
1.1       albertel  341:     $r->print(&Apache::loncommon::end_page());
                    342:     $r->rflush();
                    343: 
                    344:     return OK;
                    345: }
                    346: 
1.35      raeburn   347: sub startpage {
                    348:     my ($r) = @_;
                    349: # ----------------------------------------------------- Force menu registration
                    350:     # Header
                    351:     my $course_type = &Apache::loncommon::course_type();
                    352:     my $title = $course_type . ' Contents';
1.39    ! raeburn   353:     my $brcrum = [{href => '/adm/navmaps',
        !           354:                    text => &mt($course_type . ' Contents'),
        !           355:                    no_mt => 1},
        !           356:                  ];
        !           357:     my $args = {'bread_crumbs' => $brcrum};
        !           358:     my $start_page = &Apache::loncommon::start_page($title,undef,$args);
1.35      raeburn   359:     $r->print($start_page.
                    360:               '<script type="text/javascript">'."\n".
                    361:               '// <![CDATA['."\n".
                    362:               'window.focus();'."\n".
                    363:               '// ]]>'."\n".
                    364:               '</script>');
                    365:     return;
                    366: }
                    367: 
1.20      raeburn   368: sub startContentScreen {
                    369:     my ($r,$mode)=@_;
                    370: 
                    371:     $r->print("\n".'<ul class="LC_TabContentBigger" id="mainnav">'."\n");
1.22      www       372:     $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");
1.23      raeburn   373:     my $allowed = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
                    374:     my ($suppcount,$errors);
                    375:     unless ($allowed) {
                    376:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.24      raeburn   377:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.23      raeburn   378:         ($suppcount,$errors) = &Apache::lonnet::get_numsuppfiles($cnum,$cdom);
                    379:     }
1.24      raeburn   380:     if ($allowed || $suppcount) {
1.23      raeburn   381:         $r->print('<li '.(($mode eq 'supplemental')?' class="active"':'').'><a href="/adm/supplemental"><b>'.&mt('Supplemental Content').'</b></a></li>');
                    382:     }
1.20      raeburn   383:     $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");
                    384:     $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");
                    385:     $r->print("\n".'</ul>'."\n");
                    386:     $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;">');
                    387: }
                    388: 
                    389: sub endContentScreen {
                    390:    my ($r)=@_;
                    391:    $r->print('</div></div></div>');
                    392: }
                    393: 
1.1       albertel  394: 1;
                    395: __END__
                    396: 

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