Annotation of loncom/interface/loncommon.pm, revision 1.23

1.10      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # a pile of common routines
1.10      albertel    3: #
1.20      www         4: # $Id: loncommon.pm,v 1.19 2001/12/21 17:06:56 harris41 Exp $
1.10      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: #
1.12      harris41   28: # YEAR=2001
                     29: # 2/13-12/7 Guy Albertelli
1.17      harris41   30: # 12/11,12/12,12/17 Scott Harrison
1.18      www        31: # 12/21 Gerd Kortemeyer
1.20      www        32: # 12/21 Scott Harrison
1.22      www        33: # 12/25,12/28 Gerd Kortemeyer
1.23    ! www        34: # YEAR=2002
        !            35: # 1/4 Gerd Kortemeyer
1.1       albertel   36: 
                     37: # Makes a table out of the previous attempts
1.2       albertel   38: # Inputs result_from_symbread, user, domain, course_id
1.16      harris41   39: # Reads in non-network-related .tab files
1.1       albertel   40: 
                     41: package Apache::loncommon;
                     42: 
                     43: use strict;
1.22      www        44: use Apache::lonnet();
1.8       albertel   45: use POSIX qw(strftime);
1.1       albertel   46: use Apache::Constants qw(:common);
                     47: use Apache::lonmsg();
1.12      harris41   48: 
1.22      www        49: my $readit;
                     50: 
1.20      www        51: # ----------------------------------------------- Filetypes/Languages/Copyright
1.12      harris41   52: my %language;
                     53: my %cprtag;
                     54: my %fe; my %fd;
1.15      harris41   55: my %fc;
1.12      harris41   56: 
1.20      www        57: # -------------------------------------------------------------- Thesaurus data
1.21      www        58: my @therelated;
                     59: my @theword;
                     60: my @thecount;
                     61: my %theindex;
                     62: my $thetotalcount;
1.20      www        63: my $thefuzzy=2;
                     64: my $thethreshold=0.1/$thefuzzy;
                     65: my $theavecount;
                     66: 
1.12      harris41   67: # ----------------------------------------------------------------------- BEGIN
1.18      www        68: BEGIN {
1.22      www        69: 
                     70:     unless ($readit) {
1.12      harris41   71: # ------------------------------------------------------------------- languages
                     72:     {
                     73: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
                     74: 				 '/language.tab');
1.16      harris41   75: 	if ($fh) {
                     76: 	    while (<$fh>) {
                     77: 		next if /^\#/;
                     78: 		chomp;
                     79: 		my ($key,$val)=(split(/\s+/,$_,2));
                     80: 		$language{$key}=$val;
                     81: 	    }
1.12      harris41   82: 	}
                     83:     }
                     84: # ------------------------------------------------------------------ copyrights
                     85:     {
1.16      harris41   86: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonIncludes'}.
                     87: 				  '/copyright.tab');
                     88: 	if ($fh) {
                     89: 	    while (<$fh>) {
                     90: 		next if /^\#/;
                     91: 		chomp;
                     92: 		my ($key,$val)=(split(/\s+/,$_,2));
                     93: 		$cprtag{$key}=$val;
                     94: 	    }
1.12      harris41   95: 	}
                     96:     }
1.15      harris41   97: # ------------------------------------------------------------- file categories
                     98:     {
                     99: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
1.16      harris41  100: 				  '/filecategories.tab');
                    101: 	if ($fh) {
                    102: 	    while (<$fh>) {
                    103: 		next if /^\#/;
                    104: 		chomp;
                    105: 		my ($key,$val)=(split(/\s+/,$_,2));
                    106: 		push @{$fc{$key}},$val;
                    107: 	    }
1.15      harris41  108: 	}
                    109:     }
1.12      harris41  110: # ------------------------------------------------------------------ file types
                    111:     {
1.16      harris41  112: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
                    113: 	       '/filetypes.tab');
                    114: 	if ($fh) {
                    115:             while (<$fh>) {
                    116: 		next if (/^\#/);
                    117: 		chomp;
                    118: 		my ($ending,$emb,$descr)=split(/\s+/,$_,3);
                    119: 		if ($descr ne '') { 
                    120: 		    $fe{$ending}=lc($emb);
                    121: 		    $fd{$ending}=$descr;
                    122: 		}
1.12      harris41  123: 	    }
                    124: 	}
                    125:     }
1.20      www       126: # -------------------------------------------------------------- Thesaurus data
                    127:     {
                    128: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
                    129: 	       '/thesaurus.dat');
                    130: 	if ($fh) {
                    131:             while (<$fh>) {
                    132:                my ($tword,$tindex,$tcount,$trelated)=split(/\@/,$_);
                    133:                $theindex{$tword}=$tindex;
                    134:                $theword[$tindex]=$tword;
                    135:                $thecount[$tindex]=$tcount;
                    136:                $thetotalcount+=$tcount;
                    137:                $therelated[$tindex]=$trelated;
                    138: 	   }
                    139:         }
                    140:         $theavecount=$thetotalcount/$#thecount;
                    141:     }
1.22      www       142:     &Apache::lonnet::logthis(
                    143:               "<font color=yellow>INFO: Read file types and thesaurus</font>");
                    144:     $readit=1;
                    145: }
                    146: 
1.20      www       147: }
                    148: # ============================================================= END BEGIN BLOCK
                    149: 
                    150: 
                    151: # ---------------------------------------------------------- Is this a keyword?
                    152: 
                    153: sub keyword {
                    154:     my $newword=shift;
                    155:     $newword=~s/\W//g;
                    156:     $newword=~tr/A-Z/a-z/;
                    157:     my $tindex=$theindex{$newword};
                    158:     if ($tindex) {
                    159:         if ($thecount[$tindex]>$theavecount) {
                    160:            return 1;
                    161:         }
                    162:     }
                    163:     return 0;
                    164: }
                    165: # -------------------------------------------------------- Return related words
                    166: 
                    167: sub related {
                    168:     my $newword=shift;
                    169:     $newword=~s/\W//g;
                    170:     $newword=~tr/A-Z/a-z/;
                    171:     my $tindex=$theindex{$newword};
                    172:     if ($tindex) {
                    173:         my %found=();
                    174:         foreach (split(/\,/,$therelated[$tindex])) {
                    175: # - Related word found
                    176:             my ($ridx,$rcount)=split(/\:/,$_);
                    177: # - Direct relation index
                    178:             my $directrel=$rcount/$thecount[$tindex];
                    179:             if ($directrel>$thethreshold) {
                    180:                foreach (split(/\,/,$therelated[$ridx])) {
                    181:                   my ($rridx,$rrcount)=split(/\:/,$_);
                    182:                   if ($rridx==$tindex) {
                    183: # - Determine reverse relation index
                    184:                      my $revrel=$rrcount/$thecount[$ridx];
                    185: # - Calculate full index
                    186:                      $found{$ridx}=$directrel*$revrel;
                    187:                      if ($found{$ridx}>$thethreshold) {
                    188:                         foreach (split(/\,/,$therelated[$ridx])) {
                    189:                             my ($rrridx,$rrrcount)=split(/\:/,$_);
                    190:                             unless ($found{$rrridx}) {
                    191:                                my $revrevrel=$rrrcount/$thecount[$ridx];
                    192:                                if (
                    193:                           $directrel*$revrel*$revrevrel>$thethreshold
                    194:                                ) {
                    195:                                   $found{$rrridx}=
                    196:                                        $directrel*$revrel*$revrevrel;
                    197:                                }
                    198:                             }
                    199:                         }
                    200:                      }
                    201:                   }
                    202:                }
                    203:             }
                    204:         }
                    205:     }
                    206:     return ();
1.14      harris41  207: }
                    208: 
                    209: # ---------------------------------------------------------------- Language IDs
                    210: sub languageids {
1.16      harris41  211:     return sort(keys(%language));
1.14      harris41  212: }
                    213: 
                    214: # -------------------------------------------------------- Language Description
                    215: sub languagedescription {
1.16      harris41  216:     return $language{shift(@_)};
1.14      harris41  217: }
                    218: 
                    219: # --------------------------------------------------------------- Copyright IDs
                    220: sub copyrightids {
1.16      harris41  221:     return sort(keys(%cprtag));
1.14      harris41  222: }
                    223: 
                    224: # ------------------------------------------------------- Copyright Description
                    225: sub copyrightdescription {
1.16      harris41  226:     return $cprtag{shift(@_)};
1.14      harris41  227: }
                    228: 
                    229: # ------------------------------------------------------------- File Categories
                    230: sub filecategories {
1.16      harris41  231:     return sort(keys(%fc));
1.15      harris41  232: }
1.14      harris41  233: 
1.17      harris41  234: # -------------------------------------- File Types within a specified category
1.15      harris41  235: sub filecategorytypes {
1.16      harris41  236:     return @{$fc{lc(shift(@_))}};
1.14      harris41  237: }
                    238: 
                    239: # ------------------------------------------------------------------ File Types
                    240: sub fileextensions {
1.16      harris41  241:     return sort(keys(%fe));
1.14      harris41  242: }
                    243: 
                    244: # ------------------------------------------------------------- Embedding Style
                    245: sub fileembstyle {
1.16      harris41  246:     return $fe{lc(shift(@_))};
1.14      harris41  247: }
                    248: 
                    249: # ------------------------------------------------------------ Description Text
                    250: sub filedescription {
1.16      harris41  251:     return $fd{lc(shift(@_))};
                    252: }
                    253: 
                    254: # ------------------------------------------------------------ Description Text
                    255: sub filedescriptionex {
                    256:     my $ex=shift;
                    257:     return '.'.$ex.' '.$fd{lc($ex)};
1.12      harris41  258: }
1.1       albertel  259: 
                    260: sub get_previous_attempt {
1.2       albertel  261:   my ($symb,$username,$domain,$course)=@_;
1.1       albertel  262:   my $prevattempts='';
                    263:   if ($symb) {
1.3       albertel  264:     my (%returnhash)=
                    265:       &Apache::lonnet::restore($symb,$course,$domain,$username);
1.1       albertel  266:     if ($returnhash{'version'}) {
                    267:       my %lasthash=();
                    268:       my $version;
                    269:       for ($version=1;$version<=$returnhash{'version'};$version++) {
1.19      harris41  270:         foreach (sort(split(/\:/,$returnhash{$version.':keys'}))) {
1.1       albertel  271: 	  $lasthash{$_}=$returnhash{$version.':'.$_};
1.19      harris41  272:         }
1.1       albertel  273:       }
                    274:       $prevattempts='<table border=2></tr><th>History</th>';
1.16      harris41  275:       foreach (sort(keys %lasthash)) {
1.1       albertel  276:         $prevattempts.='<th>'.$_.'</th>';
1.16      harris41  277:       }
1.1       albertel  278:       for ($version=1;$version<=$returnhash{'version'};$version++) {
                    279:         $prevattempts.='</tr><tr><th>Attempt '.$version.'</th>';
1.16      harris41  280:         foreach (sort(keys %lasthash)) {
1.5       albertel  281: 	  my $value;
                    282: 	  if ($_ =~ /timestamp/) {
                    283: 	    $value=scalar(localtime($returnhash{$version.':'.$_}));
                    284: 	  } else {
                    285: 	    $value=$returnhash{$version.':'.$_};
                    286: 	  }
                    287: 	  $prevattempts.='<td>'.$value.'</td>';   
1.16      harris41  288:         }
1.1       albertel  289:       }
                    290:       $prevattempts.='</tr><tr><th>Current</th>';
1.16      harris41  291:       foreach (sort(keys %lasthash)) {
1.5       albertel  292: 	my $value;
                    293: 	if ($_ =~ /timestamp/) {
                    294: 	  $value=scalar(localtime($lasthash{$_}));
                    295: 	} else {
                    296: 	  $value=$lasthash{$_};
                    297: 	}
                    298: 	$prevattempts.='<td>'.$value.'</td>';
1.16      harris41  299:       }
1.1       albertel  300:       $prevattempts.='</tr></table>';
                    301:     } else {
                    302:       $prevattempts='Nothing submitted - no attempts.';
                    303:     }
                    304:   } else {
                    305:     $prevattempts='No data.';
                    306:   }
1.10      albertel  307: }
                    308: 
                    309: sub get_student_view {
                    310:   my ($symb,$username,$domain,$courseid) = @_;
                    311:   my ($map,$id,$feedurl) = split(/___/,$symb);
                    312:   my (%old,%moreenv);
                    313:   my @elements=('symb','courseid','domain','username');
                    314:   foreach my $element (@elements) {
                    315:     $old{$element}=$ENV{'form.grade_'.$element};
                    316:     $moreenv{'form.grade_'.$element}=eval '$'.$element #'
                    317:   }
1.11      albertel  318:   &Apache::lonnet::appenv(%moreenv);
                    319:   my $userview=&Apache::lonnet::ssi('/res/'.$feedurl);
                    320:   &Apache::lonnet::delenv('form.grade_');
                    321:   foreach my $element (@elements) {
                    322:     $ENV{'form.grade_'.$element}=$old{$element};
                    323:   }
                    324:   $userview=~s/\<body[^\>]*\>//gi;
                    325:   $userview=~s/\<\/body\>//gi;
                    326:   $userview=~s/\<html\>//gi;
                    327:   $userview=~s/\<\/html\>//gi;
                    328:   $userview=~s/\<head\>//gi;
                    329:   $userview=~s/\<\/head\>//gi;
                    330:   $userview=~s/action\s*\=/would_be_action\=/gi;
                    331:   return $userview;
                    332: }
                    333: 
                    334: sub get_student_answers {
                    335:   my ($symb,$username,$domain,$courseid) = @_;
                    336:   my ($map,$id,$feedurl) = split(/___/,$symb);
                    337:   my (%old,%moreenv);
                    338:   my @elements=('symb','courseid','domain','username');
                    339:   foreach my $element (@elements) {
                    340:     $old{$element}=$ENV{'form.grade_'.$element};
                    341:     $moreenv{'form.grade_'.$element}=eval '$'.$element #'
                    342:   }
                    343:   $moreenv{'form.grade_target'}='answer';
1.10      albertel  344:   &Apache::lonnet::appenv(%moreenv);
                    345:   my $userview=&Apache::lonnet::ssi('/res/'.$feedurl);
                    346:   &Apache::lonnet::delenv('form.grade_');
                    347:   foreach my $element (@elements) {
                    348:     $ENV{'form.grade_'.$element}=$old{$element};
                    349:   }
                    350:   $userview=~s/\<body[^\>]*\>//gi;
                    351:   $userview=~s/\<\/body\>//gi;
                    352:   $userview=~s/\<html\>//gi;
                    353:   $userview=~s/\<\/html\>//gi;
                    354:   $userview=~s/\<head\>//gi;
                    355:   $userview=~s/\<\/head\>//gi;
                    356:   $userview=~s/action\s*\=/would_be_action\=/gi;
                    357:   return $userview;
1.1       albertel  358: }
                    359: 
1.6       albertel  360: sub get_unprocessed_cgi {
                    361:   my ($query)= @_;
1.16      harris41  362:   foreach (split(/&/,$query)) {
1.6       albertel  363:     my ($name, $value) = split(/=/,$_);
                    364:     $value =~ tr/+/ /;
                    365:     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    366:     if (!defined($ENV{'form.'.$name})) { $ENV{'form.'.$name}=$value; }
1.16      harris41  367:   }
1.6       albertel  368: }
                    369: 
1.7       albertel  370: sub cacheheader {
1.23    ! www       371:   unless ($ENV{'request.method'} eq 'GET') { return ''; }
1.8       albertel  372:   my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
1.7       albertel  373:   my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
                    374:                 <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
                    375:                 <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
                    376:   return $output;
                    377: }
                    378: 
1.9       albertel  379: sub no_cache {
                    380:   my ($r) = @_;
1.23    ! www       381:   unless ($ENV{'request.method'} eq 'GET') { return ''; }
1.9       albertel  382:   my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
                    383:   $r->no_cache(1);
                    384:   $r->header_out("Pragma" => "no-cache");
                    385:   $r->header_out("Expires" => $date);
                    386: }
1.1       albertel  387: 1;
                    388: __END__;
1.17      harris41  389: 
                    390: 
                    391: =head1 NAME
                    392: 
                    393: Apache::loncommon - pile of common routines
                    394: 
                    395: =head1 SYNOPSIS
                    396: 
                    397: Referenced by other mod_perl Apache modules.
                    398: 
                    399: Invocation:
                    400:  &Apache::loncommon::SUBROUTINENAME(ARGUMENTS);
                    401: 
                    402: =head1 INTRODUCTION
                    403: 
                    404: Common collection of used subroutines.  This collection helps remove
                    405: redundancy from other modules and increase efficiency of memory usage.
                    406: 
                    407: Current things done:
                    408: 
                    409:  Makes a table out of the previous homework attempts
                    410:  Inputs result_from_symbread, user, domain, course_id
                    411:  Reads in non-network-related .tab files
                    412: 
                    413: This is part of the LearningOnline Network with CAPA project
                    414: described at http://www.lon-capa.org.
                    415: 
                    416: =head1 HANDLER SUBROUTINE
                    417: 
                    418: There is no handler subroutine.
                    419: 
                    420: =head1 OTHER SUBROUTINES
                    421: 
                    422: =over 4
                    423: 
                    424: =item *
                    425: 
                    426: BEGIN() : initialize values from language.tab, copyright.tab, filetypes.tab,
                    427: and filecategories.tab.
                    428: 
                    429: =item *
                    430: 
                    431: languageids() : returns list of all language ids
                    432: 
                    433: =item *
                    434: 
                    435: languagedescription() : returns description of a specified language id
                    436: 
                    437: =item *
                    438: 
                    439: copyrightids() : returns list of all copyrights
                    440: 
                    441: =item *
                    442: 
                    443: copyrightdescription() : returns description of a specified copyright id
                    444: 
                    445: =item *
                    446: 
                    447: filecategories() : returns list of all file categories
                    448: 
                    449: =item *
                    450: 
                    451: filecategorytypes() : returns list of file types belonging to a given file
                    452: category
                    453: 
                    454: =item *
                    455: 
                    456: fileembstyle() : returns embedding style for a specified file type
                    457: 
                    458: =item *
                    459: 
                    460: filedescription() : returns description for a specified file type
                    461: 
                    462: =item *
                    463: 
                    464: filedescriptionex() : returns description for a specified file type with
                    465: extra formatting
                    466: 
                    467: =item *
                    468: 
                    469: get_previous_attempt() : return string with previous attempt on problem
                    470: 
                    471: =item *
                    472: 
                    473: get_student_view() : show a snapshot of what student was looking at
                    474: 
                    475: =item *
                    476: 
                    477: get_student_answers() : show a snapshot of how student was answering problem
                    478: 
                    479: =item *
                    480: 
                    481: get_unprocessed_cgi() : get unparsed CGI parameters
                    482: 
                    483: =item *
                    484: 
                    485: cacheheader() : returns cache-controlling header code
                    486: 
                    487: =item *
                    488: 
                    489: nocache() : specifies header code to not have cache
                    490: 
                    491: =back
                    492: 
                    493: =cut

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