File:  [LON-CAPA] / loncom / interface / lonnavmaps.pm
Revision 1.13: download - view: text, annotated - select for diffs
Thu Feb 8 17:01:34 2001 UTC (23 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Automatically goes to current location, has little table of contents

    1: # The LearningOnline Network with CAPA
    2: # Navigate Maps Handler
    3: #
    4: # (Page Handler
    5: #
    6: # (TeX Content Handler
    7: #
    8: # 05/29/00,05/30 Gerd Kortemeyer)
    9: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   10: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer)
   11: #
   12: # 3/1/1,6/1,17/1,29/1,30/1,2/8 Gerd Kortemeyer
   13: 
   14: package Apache::lonnavmaps;
   15: 
   16: use strict;
   17: use Apache::Constants qw(:common :http);
   18: use Apache::lonnet();
   19: use HTML::TokeParser;
   20: use GDBM_File;
   21: 
   22: # -------------------------------------------------------------- Module Globals
   23: my %hash;
   24: my @rows;
   25: 
   26: #
   27: # These cache hashes need to be independent of user, resource and course
   28: # (user and course can/should be in the keys)
   29: #
   30: 
   31: my %courserdatas;
   32: my %userrdatas;
   33: 
   34: #
   35: # These global hashes are dependent on user, course and resource, 
   36: # and need to be initialized every time when a sheet is calculated
   37: #
   38: my %courseopt;
   39: my %useropt;
   40: my %parmhash;
   41: 
   42: 
   43: # ------------------------------------------------------------------ Euclid gcd
   44: 
   45: sub euclid {
   46:     my ($e,$f)=@_;
   47:     my $a; my $b; my $r;
   48:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   49:     while ($r!=0) {
   50: 	$a=$b; $b=$r;
   51:         $r=$a%$b;
   52:     }
   53:     return $b;
   54: }
   55: 
   56: # --------------------------------------------------------------------- Parmval
   57: 
   58: # -------------------------------------------- Figure out a cascading parameter
   59: #
   60: # For this function to work
   61: #
   62: # * parmhash needs to be tied
   63: # * courseopt and useropt need to be initialized for this user and course
   64: #
   65: 
   66: sub parmval {
   67:     my ($what,$symb)=@_;
   68:     my $cid=$ENV{'request.course.id'};
   69:     my $csec=$ENV{'request.course.sec'};
   70:     my $uname=$ENV{'user.name'};
   71:     my $udom=$ENV{'user.domain'};
   72: 
   73:     unless ($symb) { return ''; }
   74:     my $result='';
   75: 
   76:     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
   77: 
   78: # ----------------------------------------------------- Cascading lookup scheme
   79:        my $rwhat=$what;
   80:        $what=~s/^parameter\_//;
   81:        $what=~s/\_/\./;
   82: 
   83:        my $symbparm=$symb.'.'.$what;
   84:        my $mapparm=$mapname.'___(all).'.$what;
   85:        my $usercourseprefix=$uname.'_'.$udom.'_'.$cid;
   86: 
   87:        my $seclevel=
   88:             $usercourseprefix.'.['.
   89: 		$csec.'].'.$what;
   90:        my $seclevelr=
   91:             $usercourseprefix.'.['.
   92: 		$csec.'].'.$symbparm;
   93:        my $seclevelm=
   94:             $usercourseprefix.'.['.
   95: 		$csec.'].'.$mapparm;
   96: 
   97:        my $courselevel=
   98:             $usercourseprefix.'.'.$what;
   99:        my $courselevelr=
  100:             $usercourseprefix.'.'.$symbparm;
  101:        my $courselevelm=
  102:             $usercourseprefix.'.'.$mapparm;
  103: 
  104: # ---------------------------------------------------------- fourth, check user
  105:       
  106:       if ($uname) { 
  107: 
  108:        if ($useropt{$courselevelr}) { return $useropt{$courselevelr}; }
  109: 
  110:        if ($useropt{$courselevelm}) { return $useropt{$courselevelm}; }
  111: 
  112:        if ($useropt{$courselevel}) { return $useropt{$courselevel}; }
  113: 
  114:       }
  115: 
  116: # --------------------------------------------------------- third, check course
  117:      
  118:        if ($csec) {
  119:  
  120:         if ($courseopt{$seclevelr}) { return $courseopt{$seclevelr}; }
  121: 
  122:         if ($courseopt{$seclevelm}) { return $courseopt{$seclevelm}; }  
  123: 
  124:         if ($courseopt{$seclevel}) { return $courseopt{$seclevel}; }
  125:   
  126:       }
  127: 
  128:        if ($courseopt{$courselevelr}) { return $courseopt{$courselevelr}; }
  129: 
  130:        if ($courseopt{$courselevelm}) { return $courseopt{$courselevelm}; }
  131: 
  132:        if ($courseopt{$courselevel}) { return $courseopt{$courselevel}; }
  133: 
  134: # ----------------------------------------------------- second, check map parms
  135: 
  136:        my $thisparm=$parmhash{$symbparm};
  137:        if ($thisparm) { return $thisparm; }
  138: 
  139: # -------------------------------------------------------- first, check default
  140: 
  141:        return &Apache::lonnet::metadata($fn,$rwhat.'.default');
  142:         
  143: }
  144: 
  145: 
  146: 
  147: # ------------------------------------------------------------- Find out status
  148: 
  149: sub astatus {
  150:     my $rid=shift;
  151:     my $code=1;
  152:     my $ctext='';
  153:     $rid=~/(\d+)\.(\d+)/;
  154:     my $symb=&Apache::lonnet::declutter($hash{'map_id_'.$1}).'___'.$2.'___'.
  155: 	     &Apache::lonnet::declutter($hash{'src_'.$rid});
  156:     my $duedate=&parmval('0.duedate',$symb);
  157:     my $opendate=&parmval('0.opendate',$symb);
  158:     my $answerdate=&parmval('0.answerdate',$symb);
  159:     my $now=time;
  160:     my $tcode=0;
  161:    if ($opendate) {
  162:     if ($now<$duedate) {
  163:         $tcode=2; 
  164:         $ctext='Due: '.localtime($duedate);
  165:         if ($now<$opendate) { 
  166:           $tcode=1; 
  167:           $ctext='Open: '.localtime($opendate);
  168:         }
  169:         if ($duedate-$now<86400) {
  170: 	   $tcode=4;
  171:            $ctext='Due: '.localtime($duedate);
  172:         }
  173:      } else {
  174:        $tcode=3;
  175:        if ($now<$answerdate) {  
  176:           $ctext='Answer: '.localtime($duedate);
  177:        }
  178:     }
  179:    } else {
  180:     $tcode=1;
  181:    }
  182:     my $answer=&Apache::lonnet::reply(
  183:               "restore:$ENV{'user.domain'}:$ENV{'user.name'}:".
  184:               $ENV{'request.course.id'}.':'.
  185:               &Apache::lonnet::escape($symb),
  186:               "$ENV{'user.home'}");
  187:     my %returnhash=();
  188:     map {
  189: 	my ($name,$value)=split(/\=/,$_);
  190:         $returnhash{&Apache::lonnet::unescape($name)}=
  191:                     &Apache::lonnet::unescape($value);
  192:     } split(/\&/,$answer);
  193:     if ($returnhash{'version'}) {
  194:        my $version;
  195:        for ($version=1;$version<=$returnhash{'version'};$version++) {
  196:           map {
  197:              $returnhash{$_}=$returnhash{$version.':'.$_};
  198:           } split(/\:/,$returnhash{$version.':keys'});
  199:        }
  200:        map {
  201:            if (($_=~/\.(\w+)\.solved$/) && ($_!~/^\d+\:/)) {
  202:                my $part=$1;
  203:                if ($ctext) { $ctext.=', '; }
  204:                if ($part) {
  205: 		   $ctext.='Part '.$part.': ';
  206:                }
  207: 	       if ($returnhash{$_} eq 'correct_by_student') {
  208:                    unless ($code==2) { $code=3; }
  209:                    $ctext.='solved';
  210:                } elsif ($returnhash{$_} eq 'correct_by_override') {
  211:                    unless ($code==2) { $code=3; }
  212:                    $ctext.='override';
  213:                } elsif ($returnhash{$_} eq 'incorrect_attempted') {
  214:                    $code=2;
  215:                    $ctext.=
  216:                      $returnhash{'resource.'.$part.'.tries'}.'/'.
  217:                      &parmval($part.'.maxtries',$symb).' tries';
  218:                } elsif ($returnhash{$_} eq 'incorrect_by_override') {
  219:                    $code=2;
  220:                    $ctext.='override';
  221:                } elsif ($returnhash{$_} eq 'excused') {
  222:                    unless ($code==2) { $code=3; }
  223:                    $ctext.='excused';
  224:                }
  225:            }
  226:        } keys %returnhash;
  227:     }
  228:     return 'p'.$code.$tcode.'"'.$ctext.'"';
  229: }
  230: 
  231: # ------------------------------------------------------------ Build page table
  232: 
  233: sub tracetable {
  234:     my ($sofar,$rid,$beenhere)=@_;
  235:     my $further=$sofar;
  236:     unless ($beenhere=~/\&$rid\&/) {
  237:        $beenhere.=$rid.'&';  
  238: 
  239:        if (defined($hash{'is_map_'.$rid})) {
  240:            $sofar++;
  241:            my $tprefix='';
  242:            if ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}} 
  243:             eq 'sequence') { 
  244:                $tprefix='h'; 
  245:            }
  246:            if (defined($rows[$sofar])) {
  247:               $rows[$sofar].='&'.$tprefix.$rid;
  248:            } else {
  249:               $rows[$sofar]=$tprefix.$rid;
  250:            }
  251:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
  252:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) &&
  253:                ($tprefix eq 'h')) {
  254:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
  255: 	      $sofar=
  256:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
  257:                 '&'.$frid.'&');
  258:               $sofar++;
  259:               if ($hash{'src_'.$frid}) {
  260:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
  261:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
  262: 		 my $pprefix='';
  263:                  if ($hash{'src_'.$frid}=~
  264:                                  /\.(problem|exam|quiz|assess|survey|form)$/) {
  265: 		     $pprefix=&astatus($frid);
  266: 
  267:                  }
  268:                  if (defined($rows[$sofar])) {
  269:                    $rows[$sofar].='&'.$pprefix.$frid;
  270:                  } else {
  271:                    $rows[$sofar]=$pprefix.$frid;
  272:                  }
  273: 	       }
  274: 	      }
  275: 	   }
  276:        } else {
  277:           $sofar++;
  278:           if ($hash{'src_'.$rid}) {
  279:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
  280:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
  281: 	     my $pprefix='';
  282:              if ($hash{'src_'.$rid}=~
  283:                                  /\.(problem|exam|quiz|assess|survey|form)$/) {
  284: 	         $pprefix=&astatus($rid);
  285:              }
  286:              if (defined($rows[$sofar])) {
  287:                 $rows[$sofar].='&'.$pprefix.$rid;
  288:              } else {
  289:                $rows[$sofar]=$pprefix.$rid;
  290:              }
  291: 	   }
  292:           }
  293:        }
  294: 
  295:        if (defined($hash{'to_'.$rid})) {
  296: 	  my $mincond=1;
  297:           my $next='';
  298:           map {
  299:               my $thiscond=
  300:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  301:               if ($thiscond>=$mincond) {
  302: 		  if ($next) {
  303: 		      $next.=','.$_.':'.$thiscond;
  304:                   } else {
  305:                       $next=$_.':'.$thiscond;
  306: 		  }
  307:                   if ($thiscond>$mincond) { $mincond=$thiscond; }
  308: 	      }
  309:           } split(/\,/,$hash{'to_'.$rid});
  310:           map {
  311:               my ($linkid,$condval)=split(/\:/,$_);
  312:               if ($condval>=$mincond) {
  313:                 my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  314:                 if ($now>$further) { $further=$now; }
  315: 	      }
  316:           } split(/\,/,$next);
  317: 
  318:        }
  319:     }
  320:     return $further;
  321: }
  322: 
  323: # ================================================================ Main Handler
  324: 
  325: sub handler {
  326:   my $r=shift;
  327: 
  328: 
  329: # ------------------------------------------- Set document type for header only
  330: 
  331:   if ($r->header_only) {
  332:        if ($ENV{'browser.mathml'}) {
  333:            $r->content_type('text/xml');
  334:        } else {
  335:            $r->content_type('text/html');
  336:        }
  337:        $r->send_http_header;
  338:        return OK;
  339:    }
  340: 
  341:   my $requrl=$r->uri;
  342: # ----------------------------------------------------------------- Tie db file
  343:   if ($ENV{'request.course.fn'}) {
  344:       my $fn=$ENV{'request.course.fn'};
  345:       if (-e "$fn.db") {
  346:           if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) &&
  347:              (tie(%parmhash,'GDBM_File',
  348:            $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640))) {
  349: # ------------------------------------------------------------------- Hash tied
  350:               my $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
  351:               my $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
  352:               if (($firstres) && ($lastres)) {
  353: # ----------------------------------------------------------------- Render page
  354: # -------------------------------------------------------------- Set parameters
  355: 
  356: 
  357: # ---------------------------- initialize coursedata and userdata for this user
  358:     undef %courseopt;
  359:     undef %useropt;
  360: 
  361:     my $uname=$ENV{'user.name'};
  362:     my $udom=$ENV{'user.domain'};
  363:     my $uhome=$ENV{'user.home'};
  364:     my $cid=$ENV{'request.course.id'};
  365:     my $chome=$ENV{'course.'.$cid.'.home'};
  366:     my ($cdom,$cnum)=split(/\_/,$cid);
  367: 
  368:     my $userprefix=$uname.'_'.$udom.'_';
  369: 
  370:     unless ($uhome eq 'no_host') { 
  371: # -------------------------------------------------------------- Get coursedata
  372:       unless
  373:         ((time-$courserdatas{$cid.'.last_cache'})<240) {
  374:          my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
  375:               ':resourcedata',$chome);
  376:          if ($reply!~/^error\:/) {
  377:             $courserdatas{$cid}=$reply;
  378:             $courserdatas{$cid.'.last_cache'}=time;
  379:          }
  380:       }
  381:       map {
  382:          my ($name,$value)=split(/\=/,$_);
  383:          $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
  384:                     &Apache::lonnet::unescape($value);
  385:       } split(/\&/,$courserdatas{$cid});
  386: # --------------------------------------------------- Get userdata (if present)
  387:       unless
  388:         ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
  389:          my $reply=
  390:        &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
  391:          if ($reply!~/^error\:/) {
  392: 	     $userrdatas{$uname.'___'.$udom}=$reply;
  393: 	     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
  394:          }
  395:       }
  396:       map {
  397:          my ($name,$value)=split(/\=/,$_);
  398:          $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
  399: 	          &Apache::lonnet::unescape($value);
  400:       } split(/\&/,$userrdatas{$uname.'___'.$udom});
  401:     }
  402: 
  403:                   @rows=();
  404: 
  405:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  406:                   if ($hash{'src_'.$lastres}) {
  407:                      my $brepriv=
  408:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  409:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  410:                         $rows[$#rows+1]=''.$lastres;
  411: 		     }
  412: 		  }
  413: 
  414: # ------------------------------------------------------------------ Page parms
  415: 
  416:                   my $j;
  417:                   my $i;
  418:                   my $lcm=1;
  419:                   my $contents=0;
  420: 
  421: # ---------------------------------------------- Go through table to get layout
  422: 
  423:                   for ($i=0;$i<=$#rows;$i++) {
  424: 		     if ($rows[$i]) {
  425: 		      $contents++;
  426:                       my @colcont=split(/\&/,$rows[$i]);
  427:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  428:                      } 
  429:                   }
  430: 
  431: 
  432:                   unless ($contents) {
  433:                       $r->content_type('text/html');
  434:                       $r->send_http_header;
  435:                       $r->print('<html><body>Empty Map.</body></html>');
  436:                   } else {
  437: 
  438: # ------------------------------------------------------------------ Build page
  439: 
  440: 		      my $currenturl=$ENV{'form.postdata'};
  441:                       $currenturl=~s/^http\:\/\///;
  442:                       $currenturl=~s/^[^\/]+//;
  443: 
  444: # ---------------------------------------------------------------- Send headers
  445: 
  446:                           $r->content_type('text/html');
  447:                           $r->send_http_header;
  448:                           $r->print(
  449:                    '<html><head><title>Navigate LON-CAPA Maps</title></head>');
  450: 
  451: 			  $r->print('<body bgcolor="#FFFFFF"');
  452:                           if ($currenturl=~/^\/res/) {
  453:                              $r->print(' onLoad="window.location.hash='.
  454: 				       "'curloc'".'"');
  455: 			  }
  456:                           $r->print('><script>window.focus();</script>'.
  457:                            '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
  458:                                     '<h1>Navigate Course Map</h1>');
  459: 		      $r->rflush();
  460:                       if ($currenturl=~/^\/res/) {
  461: 		       $r->print('<a href="#curloc">Current Location</a><p>');
  462:                       }
  463: # ----------------------------------------------------- The little content list
  464:                       for ($i=0;$i<=$#rows;$i++) {
  465: 			if ($rows[$i]) {
  466:                           my @colcont=split(/\&/,$rows[$i]);
  467:                           my $avespan=$lcm/($#colcont+1);
  468:                           for ($j=0;$j<=$#colcont;$j++) {
  469:                               my $rid=$colcont[$j];
  470:                               if ($rid=~/^h(.+)/) {
  471: 				  $rid=$1;
  472:                                   $r->print(
  473:      '&nbsp;&nbsp;&nbsp;<a href="#'.$rid.'">'.$hash{'title_'.$rid}.'</a><br>');
  474:                               }
  475:                           }
  476: 		        }
  477:                       }
  478: # ----------------------------------------------------------------- Start table
  479:                       $r->print('<table cols="'.$lcm.'" border="0">');
  480:                       for ($i=0;$i<=$#rows;$i++) {
  481: 			if ($rows[$i]) {
  482:                           $r->print("\n<tr>");
  483:                           my @colcont=split(/\&/,$rows[$i]);
  484:                           my $avespan=$lcm/($#colcont+1);
  485:                           for ($j=0;$j<=$#colcont;$j++) {
  486:                               my $rid=$colcont[$j];
  487:                               my $add='<td>&nbsp;&nbsp;';
  488:                               my $adde='</td>';
  489:                               my $hwk='<font color="#223322">';
  490:                               my $hwke='</font>';
  491:                               if ($rid=~/^h(.+)/) {
  492: 				  $rid=$1;
  493:                                   $add=
  494:                                    '<th bgcolor="#AAFF55"><a name="'.$rid.'">';
  495:                                   $adde='</th>';
  496:                               }
  497:                             if ($rid=~/^p(\d)(\d)\"([\w\: \(\)\/\,]*)\"(.+)/) {
  498:                                   my $code=$1;
  499:                                   my $tcode=$2;
  500:                                   my $ctext=$3;
  501:                                   $rid=$4;
  502:                                   if ($tcode eq '1') {
  503:                                      $add='<td bgcolor="#AAAAAA">';
  504:                                   }
  505:                                   if ($code eq '3') {
  506:                                      $add='<td bgcolor="#AAFFAA">';
  507: 				  } else {
  508:                                       $add='<td bgcolor="#FFAAAA">';
  509: 				      if ($tcode eq '2') {
  510:                                          $add='<td bgcolor="#FFFFAA">';
  511:                                       }
  512:                                       if ($tcode eq '4') {
  513:                                          $add='<td bgcolor="#FFFF33"><blink>';
  514:                                          $adde='</blink></td>';
  515:                                       }
  516:                                   }
  517:                                   $hwk='<font color="#888811"><b>';
  518:                                   $hwke='</b></font>';
  519:                                   if ($code eq '1') {
  520:                                      $hwke='</b> ('.$ctext.')</font>';
  521:                                   }
  522:                                   if ($code eq '2') {
  523:                                      $hwk='<font color="#992222"><b>';
  524:                                      $hwke='</b> ('.$ctext.')</font>';
  525:                                   }
  526:                                   if ($code eq '3') {
  527:                                      $hwk='<font color="#229922"><b>';
  528:                                      $hwke='</b> ('.$ctext.')</font>';
  529:                                   }
  530:                               }
  531: 			      if ($hash{'src_'.$rid} eq $currenturl) {
  532:                                   $add=$add.'<a name="curloc"></a>'.
  533: 				      '<font color=red><b>-&gt; </b></font>';
  534:                                   $adde=
  535:                                 '<font color=red><b> &lt;-</b></font>'.$adde;
  536:                               }
  537:                               $r->print($add.'<a href="'.$hash{'src_'.$rid}.
  538:                                 '">'.$hwk.
  539:                                 $hash{'title_'.$rid}.$hwke.'</a>'.$adde);
  540:                           }
  541:                           $r->print('</tr>');
  542: 		        }
  543:                       }
  544:                       $r->print("\n</table>");
  545:                       $r->print('</body></html>');
  546: # -------------------------------------------------------------------- End page
  547:                   }                  
  548: # ------------------------------------------------------------- End render page
  549:               } else {
  550:                   $r->content_type('text/html');
  551:                   $r->send_http_header;
  552: 		  $r->print('<html><body>Coursemap undefined.</body></html>');
  553:               }
  554: # ------------------------------------------------------------------ Untie hash
  555:               unless (untie(%hash)) {
  556:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  557:                        "Could not untie coursemap $fn (browse).</font>"); 
  558:               }
  559:               unless (untie(%parmhash)) {
  560:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  561:                        "Could not untie parmhash (browse).</font>"); 
  562:               }
  563: # -------------------------------------------------------------------- All done
  564: 	      return OK;
  565: # ----------------------------------------------- Errors, hash could no be tied
  566:           }
  567:       } 
  568:   }
  569: 
  570:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  571:   return HTTP_NOT_ACCEPTABLE; 
  572: }
  573: 
  574: 1;
  575: __END__
  576: 
  577: 
  578: 
  579: 
  580: 
  581: 
  582: 

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