1: # The LearningOnline Network with CAPA
2: # a pile of common routines
3: #
4: # $Id: loncommon.pm,v 1.11 2001/12/07 23:41:25 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # 2/13 Guy Albertelli
29:
30: # Makes a table out of the previous attempts
31: # Inputs result_from_symbread, user, domain, course_id
32:
33: package Apache::loncommon;
34:
35: use strict;
36: use POSIX qw(strftime);
37: use Apache::Constants qw(:common);
38: use Apache::lonmsg();
39:
40: sub get_previous_attempt {
41: my ($symb,$username,$domain,$course)=@_;
42: my $prevattempts='';
43: if ($symb) {
44: my (%returnhash)=
45: &Apache::lonnet::restore($symb,$course,$domain,$username);
46: if ($returnhash{'version'}) {
47: my %lasthash=();
48: my $version;
49: for ($version=1;$version<=$returnhash{'version'};$version++) {
50: map {
51: $lasthash{$_}=$returnhash{$version.':'.$_};
52: } sort(split(/\:/,$returnhash{$version.':keys'}));
53: }
54: $prevattempts='<table border=2></tr><th>History</th>';
55: map {
56: $prevattempts.='<th>'.$_.'</th>';
57: } sort(keys %lasthash);
58: for ($version=1;$version<=$returnhash{'version'};$version++) {
59: $prevattempts.='</tr><tr><th>Attempt '.$version.'</th>';
60: map {
61: my $value;
62: if ($_ =~ /timestamp/) {
63: $value=scalar(localtime($returnhash{$version.':'.$_}));
64: } else {
65: $value=$returnhash{$version.':'.$_};
66: }
67: $prevattempts.='<td>'.$value.'</td>';
68: } sort(keys %lasthash);
69: }
70: $prevattempts.='</tr><tr><th>Current</th>';
71: map {
72: my $value;
73: if ($_ =~ /timestamp/) {
74: $value=scalar(localtime($lasthash{$_}));
75: } else {
76: $value=$lasthash{$_};
77: }
78: $prevattempts.='<td>'.$value.'</td>';
79: } sort(keys %lasthash);
80: $prevattempts.='</tr></table>';
81: } else {
82: $prevattempts='Nothing submitted - no attempts.';
83: }
84: } else {
85: $prevattempts='No data.';
86: }
87: }
88:
89: sub get_student_view {
90: my ($symb,$username,$domain,$courseid) = @_;
91: my ($map,$id,$feedurl) = split(/___/,$symb);
92: my (%old,%moreenv);
93: my @elements=('symb','courseid','domain','username');
94: foreach my $element (@elements) {
95: $old{$element}=$ENV{'form.grade_'.$element};
96: $moreenv{'form.grade_'.$element}=eval '$'.$element #'
97: }
98: &Apache::lonnet::appenv(%moreenv);
99: my $userview=&Apache::lonnet::ssi('/res/'.$feedurl);
100: &Apache::lonnet::delenv('form.grade_');
101: foreach my $element (@elements) {
102: $ENV{'form.grade_'.$element}=$old{$element};
103: }
104: $userview=~s/\<body[^\>]*\>//gi;
105: $userview=~s/\<\/body\>//gi;
106: $userview=~s/\<html\>//gi;
107: $userview=~s/\<\/html\>//gi;
108: $userview=~s/\<head\>//gi;
109: $userview=~s/\<\/head\>//gi;
110: $userview=~s/action\s*\=/would_be_action\=/gi;
111: return $userview;
112: }
113:
114: sub get_student_answers {
115: my ($symb,$username,$domain,$courseid) = @_;
116: my ($map,$id,$feedurl) = split(/___/,$symb);
117: my (%old,%moreenv);
118: my @elements=('symb','courseid','domain','username');
119: foreach my $element (@elements) {
120: $old{$element}=$ENV{'form.grade_'.$element};
121: $moreenv{'form.grade_'.$element}=eval '$'.$element #'
122: }
123: $moreenv{'form.grade_target'}='answer';
124: &Apache::lonnet::appenv(%moreenv);
125: my $userview=&Apache::lonnet::ssi('/res/'.$feedurl);
126: &Apache::lonnet::delenv('form.grade_');
127: foreach my $element (@elements) {
128: $ENV{'form.grade_'.$element}=$old{$element};
129: }
130: $userview=~s/\<body[^\>]*\>//gi;
131: $userview=~s/\<\/body\>//gi;
132: $userview=~s/\<html\>//gi;
133: $userview=~s/\<\/html\>//gi;
134: $userview=~s/\<head\>//gi;
135: $userview=~s/\<\/head\>//gi;
136: $userview=~s/action\s*\=/would_be_action\=/gi;
137: return $userview;
138: }
139:
140: sub get_unprocessed_cgi {
141: my ($query)= @_;
142: map {
143: my ($name, $value) = split(/=/,$_);
144: $value =~ tr/+/ /;
145: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
146: if (!defined($ENV{'form.'.$name})) { $ENV{'form.'.$name}=$value; }
147: } (split(/&/,$query));
148: }
149:
150: sub cacheheader {
151: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
152: my $output .='<meta HTTP-EQUIV="Expires" CONTENT="'.$date.'" />
153: <meta HTTP-EQUIV="Cache-control" CONTENT="no-cache" />
154: <meta HTTP-EQUIV="Pragma" CONTENT="no-cache" />';
155: return $output;
156: }
157:
158: sub no_cache {
159: my ($r) = @_;
160: my $date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime);
161: $r->no_cache(1);
162: $r->header_out("Pragma" => "no-cache");
163: $r->header_out("Expires" => $date);
164: }
165: 1;
166: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>