Annotation of loncom/homework/lonhomework.pm, revision 1.56
1.52 albertel 1: # The LON-CAPA Homework handler
2: # Guy Albertelli
1.17 www 3: # 11/30 Gerd Kortemeyer
1.56 ! www 4: # 6/1,8/17,8/18 Gerd Kortemeyer
1.1 albertel 5:
6: package Apache::lonhomework;
7: use strict;
8: use Apache::style;
9: use Apache::lonxml;
1.2 albertel 10: use Apache::lonnet;
11: use Apache::inputtags;
12: use Apache::structuretags;
1.9 albertel 13: use Apache::response;
1.22 albertel 14: use Apache::hint;
1.31 albertel 15: use Apache::outputtags;
1.26 www 16: use Apache::Constants qw(:common);
1.47 albertel 17: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 18:
19: sub BEGIN {
20: &Apache::lonxml::register_insert();
21: }
22:
1.5 albertel 23: sub get_target {
1.52 albertel 24: if ( $ENV{'request.state'} eq "published") {
25: if ( defined $ENV{'form.submitted'}) {
26: return ('grade', 'web');
27: } else {
28: return ('web');
29: }
30: } elsif ($ENV{'request.state'} eq "construct") {
31: if ( defined $ENV{'form.preview'}) {
32: if ( defined $ENV{'form.submitted'}) {
33: return ('grade', 'web');
34: } else {
35: return ('web');
36: }
37: } else {
38: if ( $ENV{'form.problemmode'} eq 'View' ) {
1.42 albertel 39: if ( defined $ENV{'form.submitted'}) {
1.52 albertel 40: return ('grade', 'web');
1.42 albertel 41: } else {
1.52 albertel 42: return ('web');
1.42 albertel 43: }
1.52 albertel 44: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
45: if ( $ENV{'form.submitted'} eq 'edit' ) {
46: return ('modified','edit');
1.42 albertel 47: } else {
1.52 albertel 48: return ('edit');
1.42 albertel 49: }
1.52 albertel 50: } else {
51: return ('web');
52: }
1.15 albertel 53: }
1.52 albertel 54: }
55: return ();
1.5 albertel 56: }
57:
1.3 albertel 58: sub setup_vars {
1.52 albertel 59: my ($target) = @_;
60: return ';'
1.11 albertel 61: # return ';$external::target='.$target.';';
1.2 albertel 62: }
63:
64: sub send_header {
1.52 albertel 65: my ($request)= @_;
66: $request->print(&Apache::lontexconvert::header());
1.16 albertel 67: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 68: }
69:
1.36 albertel 70: sub createmenu {
1.52 albertel 71: my ($which,$request)=@_;
72: if ($which eq 'grade') {
73: $request->print('<script language="JavaScript">
74: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
75: "height=350,width=150,menubar=no");
76: </script>');
77: }
1.36 albertel 78: }
79:
1.2 albertel 80: sub send_footer {
1.52 albertel 81: my ($request)= @_;
1.16 albertel 82: # $request->print('</form>');
1.52 albertel 83: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 84: }
85:
1.52 albertel 86: $Apache::lonxml::browse='';
1.53 www 87:
88: sub check_access {
1.52 albertel 89: my ($id) = @_;
90: my $date ='';
91: my $status = '';
92: my $datemsg = '';
93: my $lastdate = '';
94: my $temp;
95: my $type;
96: my $passed;
97: &Apache::lonxml::debug("checking for part :$id:");
98: foreach $temp ("opendate","duedate","answerdate") {
99: $lastdate = $date;
100: $date = &Apache::lonnet::EXT("resource.$id.$temp");
101: &Apache::lonxml::debug("found :$date: for :$temp:");
102: if ($date eq '') {
103: $date = "an unknown date"; $passed = 0;
104: } elsif ($date eq 'con_lost') {
105: $date = "an indeterminate date"; $passed = 0;
106: } else {
107: if (time < $date) { $passed = 0; } else { $passed = 1; }
108: $date = localtime $date;
1.51 harris41 109: }
1.52 albertel 110: if (!$passed) { $type=$temp; last; }
111: }
112: &Apache::lonxml::debug("have :$type:$passed:");
113: if ($passed) {
114: $status='SHOW_ANSWER';
115: $datemsg=$date;
116: } elsif ($type eq 'opendate') {
117: $status='CLOSED';
118: $datemsg = "will open on $date";
119: } elsif ($type eq 'duedate') {
120: $status='CAN_ANSWER';
121: $datemsg = "is due at $date";
122: } elsif ($type eq 'answerdate') {
123: $status='CLOSED';
124: $datemsg = "was due on $lastdate, and answers will be available on $date";
125: }
126: if ($status eq 'CAN_ANSWER') {
127: #check #tries
128: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
129: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
130: if ( $tries eq '' ) { $tries = '0'; }
131: if ( $maxtries eq '' ) { $maxtries = '2'; }
132: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
133: }
1.54 www 134:
1.56 ! www 135: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
! 136: (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54 www 137: return ('UNCHECKEDOUT','needs to be checked out');
138: }
139:
140:
1.52 albertel 141: &Apache::lonxml::debug("sending back :$status:$datemsg:");
142: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
143: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
144: $status='CAN_ANSWER';
145: $datemsg='is closed but you are allowed to view it';
146: }
147: if ($ENV{'request.state'} eq "construct") {
148: &Apache::lonxml::debug("in construction ignoring dates");
149: $status='CAN_ANSWER';
150: $datemsg='is in under construction';
151: }
152: return ($status,$datemsg);
1.20 albertel 153: }
154:
1.41 albertel 155: sub showhash {
1.52 albertel 156: my (%hash) = @_;
157: my $resultkey;
158: foreach $resultkey (sort keys %hash) {
159: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
160: }
161: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
162: return '';
1.41 albertel 163: }
164:
165: sub setuppermissions {
1.52 albertel 166: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
167: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
168: return ''
1.41 albertel 169: }
170:
171: sub setupheader {
1.52 albertel 172: my $request=$_[0];
173: if ($ENV{'browser.mathml'}) {
174: $request->content_type('text/xml');
175: } else {
176: $request->content_type('text/html');
177: }
178: $request->send_http_header;
179: return OK if $request->header_only;
180: return ''
1.41 albertel 181: }
1.35 albertel 182:
1.47 albertel 183: sub handle_save_or_undo {
1.52 albertel 184: my ($request,$problem,$result) = @_;
185: my $fileout = &Apache::lonnet::filelocation("",$request->uri);
186: my $filesave=$fileout.".bak";
187:
188: if ($ENV{'form.Undo'} eq 'undo') {
189: if (copy($filesave,$fileout)) {
190: $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
191: } else {
192: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
193: }
194: } else {
195: my $fs=Apache::File->new(">$filesave");
196: if (defined($fs)) {
197: print $fs $$problem;
198: $request->print("<b>Making Backup to $filesave</b><br />");
199: } else {
200: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
201: }
202: my $fh=Apache::File->new(">$fileout");
203: if (defined($fh)) {
204: print $fh $$result;
205: $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47 albertel 206: } else {
1.52 albertel 207: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.47 albertel 208: }
1.52 albertel 209: }
1.47 albertel 210: }
211:
1.41 albertel 212: sub renderpage {
1.52 albertel 213: my ($request,$file) = @_;
214:
215: my (@targets) = &get_target();
216: foreach my $target (@targets) {
217: #my $t0 = [&gettimeofday()];
218: my $problem=&Apache::lonnet::getfile($file);
219: if ($problem == -1) {
220: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
221: $problem='';
222: }
223:
224: my %mystyle;
225: my $result = '';
226: &Apache::inputtags::initialize_inputtags;
227: &Apache::edit::initialize_edit;
228: %Apache::lonhomework::results=();
229: %Apache::lonhomework::history=&Apache::lonnet::restore();
230: #ignore error conditions
231: my ($temp)=keys %Apache::lonhomework::history ;
232: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
233: if ($target eq 'web') {
234: if (&Apache::lonnet::symbread() eq '') {
235: if ($ENV{'request.state'} eq "construct") {
236: $request->print("In construction space, submissions ignored<br />");
1.36 albertel 237: } else {
1.52 albertel 238: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51 harris41 239: }
1.52 albertel 240: }
241: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
242: }
243: if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
244:
245: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
246: if ($default == -1) {
247: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
248: $default='';
249: }
250: $result = &Apache::lonxml::xmlparse($target, $problem,
251: $default.&setup_vars($target),%mystyle);
252:
253: #$request->print("Result follows:");
254: if ($target eq 'modified') {
255: &handle_save_or_undo($request,\$problem,\$result);
256: } else {
257: #my $td=&tv_interval($t0);
258: #if ( $Apache::lonxml::debug) {
259: #$result =~ s:</body>::;
260: #$result.="<br />Spent $td seconds processing target $target\n</body>";
261: #}
262: $request->print($result);
263: }
264: #$request->print(":Result ends");
265: if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
266:
267: #store new values
268: my ($temp) = keys %Apache::lonhomework::results;
269: if ( $temp ne '' ) {
270: &Apache::lonxml::debug("Store return message:".
271: &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.20 albertel 272: }
1.52 albertel 273: #my $td=&tv_interval($t0);
274: }
1.41 albertel 275: }
276:
1.42 albertel 277: # with no arg it returns a HTML <option> list of the template titles
278: # with one arg it returns the filename associated with the arg passed
279: sub get_template_list {
1.52 albertel 280: my ($namewanted,$extension) = @_;
281: my $result;
282: &Apache::lonxml::debug("Looking for :$extension:");
283: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
284: my $name=&Apache::lonnet::metadata($file,'title');
285: if ($namewanted && ($name eq $namewanted)) {
286: $result=$file;
287: last;
288: } else {
289: $result.="<option>$name</option>";
1.42 albertel 290: }
1.52 albertel 291: }
292: return $result;
1.42 albertel 293: }
294:
295: sub newproblem {
1.52 albertel 296: my ($request) = @_;
297:
298: my $extension=$request->uri;
299: $extension=~s:^.*\.([\w]+)$:$1:;
300: &Apache::lonxml::debug("Looking for :$extension:");
301: if ($ENV{'form.template'}) {
302: use File::Copy;
303: my $file = &get_template_list($ENV{'form.template'},$extension);
304: my $dest = &Apache::lonnet::filelocation("",$request->uri);
305: copy($file,$dest);
306: &renderpage($request,$file);
307: } else {
308: my $templatelist=&get_template_list('',$extension);
309: my $url=$request->uri;
310: my $dest = &Apache::lonnet::filelocation("",$request->uri);
311: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 312: <body bgcolor="#FFFFFF">
1.50 albertel 313: The request file $url doesn\'t exist. You can create a new $extension <br />
1.42 albertel 314: <form action="$url" method="POST">
1.50 albertel 315: <input type="submit" value="New $extension"><br />
1.42 albertel 316: <select name="template">
317: $templatelist
318: </select>
319: </form>
320: </body>
321: ENDNEWPROBLEM
322: }
323:
324: return '';
325: }
326:
327: sub view_or_edit_menu {
1.52 albertel 328: my ($request) = @_;
329: my $url=$request->uri;
330: $request->print(<<EDITMENU);
1.42 albertel 331: <body bgcolor="#FFFFFF">
332: <form action="$url" method="POST">
333: Would you like to <input type="submit" name="problemmode" value="View"> or
334: <input type="submit" name="problemmode" value="Edit"> the problem.
335: </form>
336: </body>
337: EDITMENU
338: }
339:
1.41 albertel 340: sub handler {
1.52 albertel 341: #my $t0 = [&gettimeofday()];
342: my $request=$_[0];
1.41 albertel 343:
1.52 albertel 344: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41 albertel 345:
346: if (&setupheader($request)) { return OK; }
1.52 albertel 347: $ENV{'request.uri'}=$request->uri;
1.41 albertel 348:
349: #setup permissions
1.52 albertel 350: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
351: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
352: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
353:
354: my $file=&Apache::lonnet::filelocation("",$request->uri);
355:
356: #check if we know where we are
357: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
358: # if we are browsing we might not be able to know where we are
359: if ($Apache::lonhomework::browse ne 'F') {
360: #should know where we are, so ask
361: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 362: }
1.52 albertel 363: }
1.41 albertel 364:
1.52 albertel 365: if ($ENV{'request.state'} eq "construct") {
366: if ( -e $file ) {
367: if (!(defined $ENV{'form.problemmode'})) {
368: #first visit to problem in construction space
369: &view_or_edit_menu($request);
370: #&renderpage($request,$file);
371: } else {
372: &renderpage($request,$file);
1.41 albertel 373: }
374: } else {
1.52 albertel 375: # requested file doesn't exist in contruction space
376: &newproblem($request);
1.41 albertel 377: }
1.52 albertel 378: } else {
379: # just render the page normally outside of construction space
380: &renderpage($request,$file);
381: }
382: #my $td=&tv_interval($t0);
383: #&Apache::lonxml::debug("Spent $td seconds processing");
384: # &Apache::lonhomework::send_footer($request);
385: # always turn off debug messages
386: $Apache::lonxml::debug=0;
387: return OK;
388:
1.1 albertel 389: }
390:
391: 1;
392: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>