File:  [LON-CAPA] / loncom / interface / lonnavmaps.pm
Revision 1.17: download - view: text, annotated - select for diffs
Tue Sep 25 18:24:37 2001 UTC (22 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Sort parts and print "0" instead of nothing for "no tries"

    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,9/21,9/24,9/25 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: 
  157:     my %duedate=();
  158:     my %opendate=();
  159:     my %answerdate=();
  160:     map {
  161:         if ($_=~/^parameter\_(.*)\_opendate$/) {
  162: 	    my $part=$1;
  163:             $duedate{$part}=&parmval($part.'.duedate',$symb);
  164:             $opendate{$part}=&parmval($part.'.opendate',$symb);
  165:             $answerdate{$part}=&parmval($part.'.answerdate',$symb);
  166:         }
  167:     } sort split(/\,/,&Apache::lonnet::metadata($hash{'src_'.$rid},'keys'));
  168: 
  169:     my $now=time;
  170:     my $tcode=0;
  171: 
  172:     my %returnhash=&Apache::lonnet::restore($symb);
  173: 
  174:   map {
  175: 
  176:    my $duedate=$duedate{$_};
  177:    my $opendate=$opendate{$_};
  178:    my $answerdate=$answerdate{$_};
  179:    my $preface='';
  180:    unless ($_ eq '0') { $preface=' Part: '.$_.' '; }
  181:    if ($opendate) {
  182:     if ($now<$duedate) {
  183:         unless ($tcode==4) { $tcode=2; } 
  184:         $ctext.=$preface.'Due: '.localtime($duedate);
  185:         if ($now<$opendate) { 
  186:           unless ($tcode) { $tcode=1; } 
  187:           $ctext.=$preface.'Open: '.localtime($opendate);
  188:         }
  189:         if ($duedate-$now<86400) {
  190: 	   $tcode=4;
  191:            $ctext.=$preface.'Due: '.localtime($duedate);
  192:         }
  193:      } else {
  194: 	 unless (($tcode==4) || ($tcode eq 2)) { $tcode=3; }
  195:        if ($now<$answerdate) {  
  196:           $ctext.='Answer: '.localtime($duedate);
  197:        }
  198:     }
  199:    } else {
  200:        unless (($tcode==2) || ($tcode==4)) { $tcode=1; }
  201:    }
  202: 
  203:     my $status=$returnhash{'resource.'.$_.'.solved'};
  204: 
  205: 	       if ($status eq 'correct_by_student') {
  206:                    unless ($code==2) { $code=3; }
  207:                    $ctext.=' solved';
  208:                } elsif ($status eq 'correct_by_override') {
  209:                    unless ($code==2) { $code=3; }
  210:                    $ctext.=' override';
  211:                } elsif ($status eq 'incorrect_attempted') {
  212:                    $code=2;
  213:                    $ctext.=' ('.
  214:                      ($returnhash{'resource.'.$_.'.tries'}?
  215:                       $returnhash{'resource.'.$_.'.tries'}:'0').'/'.
  216:                      &parmval($_.'.maxtries',$symb).' tries)';
  217:                } elsif ($status eq 'incorrect_by_override') {
  218:                    $code=2;
  219:                    $ctext.=' override';
  220:                } elsif ($status eq 'excused') {
  221:                    unless ($code==2) { $code=3; }
  222:                    $ctext.=' excused';
  223:                }
  224: 
  225:    } sort keys %opendate;
  226: 
  227:     return 'p'.$code.$tcode.'"'.$ctext.'"';
  228: }
  229: 
  230: # ------------------------------------------------------------ Build page table
  231: 
  232: sub tracetable {
  233:     my ($sofar,$rid,$beenhere)=@_;
  234:     my $further=$sofar;
  235:     unless ($beenhere=~/\&$rid\&/) {
  236:        $beenhere.=$rid.'&';  
  237: 
  238:        if (defined($hash{'is_map_'.$rid})) {
  239:            $sofar++;
  240:            my $tprefix='';
  241:            if ($hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$rid}}} 
  242:             eq 'sequence') { 
  243:                $tprefix='h'; 
  244:            }
  245:            if (defined($rows[$sofar])) {
  246:               $rows[$sofar].='&'.$tprefix.$rid;
  247:            } else {
  248:               $rows[$sofar]=$tprefix.$rid;
  249:            }
  250:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
  251:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}})) &&
  252:                ($tprefix eq 'h')) {
  253:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
  254: 	      $sofar=
  255:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
  256:                 '&'.$frid.'&');
  257:               $sofar++;
  258:               if ($hash{'src_'.$frid}) {
  259:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
  260:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
  261: 		 my $pprefix='';
  262:                  if ($hash{'src_'.$frid}=~
  263:                                  /\.(problem|exam|quiz|assess|survey|form)$/) {
  264: 		     $pprefix=&astatus($frid);
  265: 
  266:                  }
  267:                  if (defined($rows[$sofar])) {
  268:                    $rows[$sofar].='&'.$pprefix.$frid;
  269:                  } else {
  270:                    $rows[$sofar]=$pprefix.$frid;
  271:                  }
  272: 	       }
  273: 	      }
  274: 	   }
  275:        } else {
  276:           $sofar++;
  277:           if ($hash{'src_'.$rid}) {
  278:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
  279:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
  280: 	     my $pprefix='';
  281:              if ($hash{'src_'.$rid}=~
  282:                                  /\.(problem|exam|quiz|assess|survey|form)$/) {
  283: 	         $pprefix=&astatus($rid);
  284:              }
  285:              if (defined($rows[$sofar])) {
  286:                 $rows[$sofar].='&'.$pprefix.$rid;
  287:              } else {
  288:                $rows[$sofar]=$pprefix.$rid;
  289:              }
  290: 	   }
  291:           }
  292:        }
  293: 
  294:        if (defined($hash{'to_'.$rid})) {
  295: 	  my $mincond=1;
  296:           my $next='';
  297:           map {
  298:               my $thiscond=
  299:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  300:               if ($thiscond>=$mincond) {
  301: 		  if ($next) {
  302: 		      $next.=','.$_.':'.$thiscond;
  303:                   } else {
  304:                       $next=$_.':'.$thiscond;
  305: 		  }
  306:                   if ($thiscond>$mincond) { $mincond=$thiscond; }
  307: 	      }
  308:           } split(/\,/,$hash{'to_'.$rid});
  309:           map {
  310:               my ($linkid,$condval)=split(/\:/,$_);
  311:               if ($condval>=$mincond) {
  312:                 my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  313:                 if ($now>$further) { $further=$now; }
  314: 	      }
  315:           } split(/\,/,$next);
  316: 
  317:        }
  318:     }
  319:     return $further;
  320: }
  321: 
  322: # ================================================================ Main Handler
  323: 
  324: sub handler {
  325:   my $r=shift;
  326: 
  327: 
  328: # ------------------------------------------- Set document type for header only
  329: 
  330:   if ($r->header_only) {
  331:        if ($ENV{'browser.mathml'}) {
  332:            $r->content_type('text/xml');
  333:        } else {
  334:            $r->content_type('text/html');
  335:        }
  336:        $r->send_http_header;
  337:        return OK;
  338:    }
  339: 
  340:   my $requrl=$r->uri;
  341: # ----------------------------------------------------------------- Tie db file
  342:   if ($ENV{'request.course.fn'}) {
  343:       my $fn=$ENV{'request.course.fn'};
  344:       if (-e "$fn.db") {
  345:           if ((tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) &&
  346:              (tie(%parmhash,'GDBM_File',
  347:            $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640))) {
  348: # ------------------------------------------------------------------- Hash tied
  349:               my $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
  350:               my $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
  351:               if (($firstres) && ($lastres)) {
  352: # ----------------------------------------------------------------- Render page
  353: # -------------------------------------------------------------- Set parameters
  354: 
  355: 
  356: # ---------------------------- initialize coursedata and userdata for this user
  357:     undef %courseopt;
  358:     undef %useropt;
  359: 
  360:     my $uname=$ENV{'user.name'};
  361:     my $udom=$ENV{'user.domain'};
  362:     my $uhome=$ENV{'user.home'};
  363:     my $cid=$ENV{'request.course.id'};
  364:     my $chome=$ENV{'course.'.$cid.'.home'};
  365:     my ($cdom,$cnum)=split(/\_/,$cid);
  366: 
  367:     my $userprefix=$uname.'_'.$udom.'_';
  368: 
  369:     unless ($uhome eq 'no_host') { 
  370: # -------------------------------------------------------------- Get coursedata
  371:       unless
  372:         ((time-$courserdatas{$cid.'.last_cache'})<240) {
  373:          my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
  374:               ':resourcedata',$chome);
  375:          if ($reply!~/^error\:/) {
  376:             $courserdatas{$cid}=$reply;
  377:             $courserdatas{$cid.'.last_cache'}=time;
  378:          }
  379:       }
  380:       map {
  381:          my ($name,$value)=split(/\=/,$_);
  382:          $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
  383:                     &Apache::lonnet::unescape($value);
  384:       } split(/\&/,$courserdatas{$cid});
  385: # --------------------------------------------------- Get userdata (if present)
  386:       unless
  387:         ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
  388:          my $reply=
  389:        &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
  390:          if ($reply!~/^error\:/) {
  391: 	     $userrdatas{$uname.'___'.$udom}=$reply;
  392: 	     $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
  393:          }
  394:       }
  395:       map {
  396:          my ($name,$value)=split(/\=/,$_);
  397:          $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
  398: 	          &Apache::lonnet::unescape($value);
  399:       } split(/\&/,$userrdatas{$uname.'___'.$udom});
  400:     }
  401: 
  402:                   @rows=();
  403: 
  404:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  405:                   if ($hash{'src_'.$lastres}) {
  406:                      my $brepriv=
  407:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  408:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  409:                         $rows[$#rows+1]=''.$lastres;
  410: 		     }
  411: 		  }
  412: 
  413: # ------------------------------------------------------------------ Page parms
  414: 
  415:                   my $j;
  416:                   my $i;
  417:                   my $lcm=1;
  418:                   my $contents=0;
  419: 
  420: # ---------------------------------------------- Go through table to get layout
  421: 
  422:                   for ($i=0;$i<=$#rows;$i++) {
  423: 		     if ($rows[$i]) {
  424: 		      $contents++;
  425:                       my @colcont=split(/\&/,$rows[$i]);
  426:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  427:                      } 
  428:                   }
  429: 
  430: 
  431:                   unless ($contents) {
  432:                       $r->content_type('text/html');
  433:                       $r->send_http_header;
  434:                       $r->print('<html><body>Empty Map.</body></html>');
  435:                   } else {
  436: 
  437: # ------------------------------------------------------------------ Build page
  438: 
  439: 		      my $currenturl=$ENV{'form.postdata'};
  440:                       $currenturl=~s/^http\:\/\///;
  441:                       $currenturl=~s/^[^\/]+//;
  442: 
  443: # ---------------------------------------------------------------- Send headers
  444: 
  445:                           $r->content_type('text/html');
  446:                           $r->send_http_header;
  447:                           $r->print(
  448:                    '<html><head><title>Navigate LON-CAPA Maps</title></head>');
  449: 
  450: 			  $r->print('<body bgcolor="#FFFFFF"');
  451:                           if (($currenturl=~/^\/res/) &&
  452:                               ($currenturl!~/^\/res\/adm/)) {
  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:                           ($currenturl!~/^\/res\/adm/)) {
  462: 		       $r->print('<a href="#curloc">Current Location</a><p>');
  463:                       }
  464: # ----------------------------------------------------- The little content list
  465:                       for ($i=0;$i<=$#rows;$i++) {
  466: 			if ($rows[$i]) {
  467:                           my @colcont=split(/\&/,$rows[$i]);
  468:                           my $avespan=$lcm/($#colcont+1);
  469:                           for ($j=0;$j<=$#colcont;$j++) {
  470:                               my $rid=$colcont[$j];
  471:                               if ($rid=~/^h(.+)/) {
  472: 				  $rid=$1;
  473:                                   $r->print(
  474:      '&nbsp;&nbsp;&nbsp;<a href="#'.$rid.'">'.$hash{'title_'.$rid}.'</a><br>');
  475:                               }
  476:                           }
  477: 		        }
  478:                       }
  479: # ----------------------------------------------------------------- Start table
  480:                       $r->print('<hr><table cols="'.$lcm.'" border="0">');
  481:                       for ($i=0;$i<=$#rows;$i++) {
  482: 			if ($rows[$i]) {
  483:                           $r->print("\n<tr>");
  484:                           my @colcont=split(/\&/,$rows[$i]);
  485:                           my $avespan=$lcm/($#colcont+1);
  486:                           for ($j=0;$j<=$#colcont;$j++) {
  487:                               my $rid=$colcont[$j];
  488:                               my $add='<td>&nbsp;&nbsp;';
  489:                               my $adde='</td>';
  490:                               my $hwk='<font color="#223322">';
  491:                               my $hwke='</font>';
  492:                               if ($rid=~/^h(.+)/) {
  493: 				  $rid=$1;
  494:                                   $add=
  495:                                    '<th bgcolor="#AAFF55"><a name="'.$rid.'">';
  496:                                   $adde='</th>';
  497:                               }
  498:                             if ($rid=~/^p(\d)(\d)\"([\w\: \(\)\/\,]*)\"(.+)/) {
  499:                                   my $code=$1;
  500:                                   my $tcode=$2;
  501:                                   my $ctext=$3;
  502:                                   $rid=$4;
  503:                                   if ($tcode eq '1') {
  504:                                      $add='<td bgcolor="#AAAAAA">';
  505:                                   }
  506:                                   if ($code eq '3') {
  507:                                      $add='<td bgcolor="#AAFFAA">';
  508: 				  } else {
  509:                                       $add='<td bgcolor="#FFAAAA">';
  510: 				      if ($tcode eq '2') {
  511:                                          $add='<td bgcolor="#FFFFAA">';
  512:                                       }
  513:                                       if ($tcode eq '4') {
  514:                                          $add='<td bgcolor="#FFFF33"><blink>';
  515:                                          $adde='</blink></td>';
  516:                                       }
  517:                                   }
  518:                                   $hwk='<font color="#888811"><b>';
  519:                                   $hwke='</b></font>';
  520:                                   if ($code eq '1') {
  521:                                      $hwke='</b> ('.$ctext.')</font>';
  522:                                   }
  523:                                   if ($code eq '2') {
  524:                                      $hwk='<font color="#992222"><b>';
  525:                                      $hwke='</b> ('.$ctext.')</font>';
  526:                                   }
  527:                                   if ($code eq '3') {
  528:                                      $hwk='<font color="#229922"><b>';
  529:                                      $hwke='</b> ('.$ctext.')</font>';
  530:                                   }
  531:                               }
  532: 			      if ($hash{'src_'.$rid} eq $currenturl) {
  533:                                   $add=$add.'<a name="curloc"></a>'.
  534: 				      '<font color=red><b>-&gt; </b></font>';
  535:                                   $adde=
  536:                                 '<font color=red><b> &lt;-</b></font>'.$adde;
  537:                               }
  538:                               $r->print($add.'<a href="'.$hash{'src_'.$rid}.
  539:                                 '">'.$hwk.
  540:                                 $hash{'title_'.$rid}.$hwke.'</a>'.$adde);
  541:                           }
  542:                           $r->print('</tr>');
  543: 		        }
  544:                       }
  545:                       $r->print("\n</table>");
  546:                       $r->print('</body></html>');
  547: # -------------------------------------------------------------------- End page
  548:                   }                  
  549: # ------------------------------------------------------------- End render page
  550:               } else {
  551:                   $r->content_type('text/html');
  552:                   $r->send_http_header;
  553: 		  $r->print('<html><body>Coursemap undefined.</body></html>');
  554:               }
  555: # ------------------------------------------------------------------ Untie hash
  556:               unless (untie(%hash)) {
  557:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  558:                        "Could not untie coursemap $fn (browse).</font>"); 
  559:               }
  560:               unless (untie(%parmhash)) {
  561:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  562:                        "Could not untie parmhash (browse).</font>"); 
  563:               }
  564: # -------------------------------------------------------------------- All done
  565: 	      return OK;
  566: # ----------------------------------------------- Errors, hash could no be tied
  567:           }
  568:       } 
  569:   }
  570: 
  571:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  572:   return HTTP_NOT_ACCEPTABLE; 
  573: }
  574: 
  575: 1;
  576: __END__
  577: 
  578: 
  579: 
  580: 
  581: 
  582: 
  583: 

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