Annotation of loncom/homework/lonhomework.pm, revision 1.77
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.77 ! albertel 4: # $Id: lonhomework.pm,v 1.76 2002/04/09 04:46:33 albertel Exp $
1.63 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.52 albertel 28: # Guy Albertelli
1.17 www 29: # 11/30 Gerd Kortemeyer
1.56 www 30: # 6/1,8/17,8/18 Gerd Kortemeyer
1.1 albertel 31:
32: package Apache::lonhomework;
33: use strict;
1.73 albertel 34: use Apache::style();
35: use Apache::lonxml();
36: use Apache::lonnet();
37: use Apache::lonplot();
38: use Apache::inputtags();
39: use Apache::structuretags();
40: use Apache::randomlabel();
41: use Apache::response();
42: use Apache::hint();
43: use Apache::outputtags();
1.26 www 44: use Apache::Constants qw(:common);
1.73 albertel 45: use HTML::Entities();
1.47 albertel 46: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 47:
1.69 harris41 48: BEGIN {
1.43 albertel 49: &Apache::lonxml::register_insert();
50: }
51:
1.5 albertel 52: sub get_target {
1.52 albertel 53: if ( $ENV{'request.state'} eq "published") {
1.66 albertel 54: if ( defined($ENV{'form.grade_target'})
55: && ($Apache::lonhomework::viewgrades == 'F' )) {
56: return ($ENV{'form.grade_target'});
57: }
1.62 albertel 58: if ( defined($ENV{'form.submitted'})) {
1.52 albertel 59: return ('grade', 'web');
60: } else {
61: return ('web');
62: }
63: } elsif ($ENV{'request.state'} eq "construct") {
1.74 albertel 64: if ( defined($ENV{'form.grade_target'}) ) {
65: return ($ENV{'form.grade_target'});
66: }
1.62 albertel 67: if ( defined($ENV{'form.preview'})) {
68: if ( defined($ENV{'form.submitted'})) {
1.52 albertel 69: return ('grade', 'web');
70: } else {
71: return ('web');
72: }
73: } else {
74: if ( $ENV{'form.problemmode'} eq 'View' ) {
1.62 albertel 75: if ( defined($ENV{'form.submitted'}) &&
76: (!defined($ENV{'form.resetdata'})) ) {
1.60 albertel 77: return ('grade', 'web','answer');
1.42 albertel 78: } else {
1.60 albertel 79: return ('web','answer');
1.42 albertel 80: }
1.52 albertel 81: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
82: if ( $ENV{'form.submitted'} eq 'edit' ) {
83: return ('modified','edit');
1.42 albertel 84: } else {
1.52 albertel 85: return ('edit');
1.42 albertel 86: }
1.52 albertel 87: } else {
88: return ('web');
89: }
1.15 albertel 90: }
1.52 albertel 91: }
92: return ();
1.5 albertel 93: }
94:
1.3 albertel 95: sub setup_vars {
1.52 albertel 96: my ($target) = @_;
97: return ';'
1.11 albertel 98: # return ';$external::target='.$target.';';
1.2 albertel 99: }
100:
101: sub send_header {
1.52 albertel 102: my ($request)= @_;
103: $request->print(&Apache::lontexconvert::header());
1.16 albertel 104: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 105: }
106:
1.36 albertel 107: sub createmenu {
1.52 albertel 108: my ($which,$request)=@_;
109: if ($which eq 'grade') {
110: $request->print('<script language="JavaScript">
111: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
112: "height=350,width=150,menubar=no");
113: </script>');
114: }
1.36 albertel 115: }
116:
1.2 albertel 117: sub send_footer {
1.52 albertel 118: my ($request)= @_;
1.16 albertel 119: # $request->print('</form>');
1.52 albertel 120: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 121: }
122:
1.52 albertel 123: $Apache::lonxml::browse='';
1.53 www 124:
125: sub check_access {
1.52 albertel 126: my ($id) = @_;
127: my $date ='';
128: my $status = '';
129: my $datemsg = '';
130: my $lastdate = '';
131: my $temp;
132: my $type;
133: my $passed;
134: &Apache::lonxml::debug("checking for part :$id:");
1.73 albertel 135: &Apache::lonxml::debug("time:".time);
1.52 albertel 136: foreach $temp ("opendate","duedate","answerdate") {
137: $lastdate = $date;
138: $date = &Apache::lonnet::EXT("resource.$id.$temp");
139: &Apache::lonxml::debug("found :$date: for :$temp:");
140: if ($date eq '') {
141: $date = "an unknown date"; $passed = 0;
142: } elsif ($date eq 'con_lost') {
143: $date = "an indeterminate date"; $passed = 0;
144: } else {
145: if (time < $date) { $passed = 0; } else { $passed = 1; }
146: $date = localtime $date;
1.51 harris41 147: }
1.52 albertel 148: if (!$passed) { $type=$temp; last; }
149: }
150: &Apache::lonxml::debug("have :$type:$passed:");
151: if ($passed) {
152: $status='SHOW_ANSWER';
153: $datemsg=$date;
154: } elsif ($type eq 'opendate') {
155: $status='CLOSED';
156: $datemsg = "will open on $date";
157: } elsif ($type eq 'duedate') {
158: $status='CAN_ANSWER';
159: $datemsg = "is due at $date";
160: } elsif ($type eq 'answerdate') {
161: $status='CLOSED';
162: $datemsg = "was due on $lastdate, and answers will be available on $date";
163: }
164: if ($status eq 'CAN_ANSWER') {
165: #check #tries
166: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
167: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
168: if ( $tries eq '' ) { $tries = '0'; }
169: if ( $maxtries eq '' ) { $maxtries = '2'; }
170: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
171: }
1.54 www 172:
1.56 www 173: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
174: (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54 www 175: return ('UNCHECKEDOUT','needs to be checked out');
176: }
177:
178:
1.52 albertel 179: &Apache::lonxml::debug("sending back :$status:$datemsg:");
180: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
181: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
182: $status='CAN_ANSWER';
183: $datemsg='is closed but you are allowed to view it';
184: }
185: if ($ENV{'request.state'} eq "construct") {
186: &Apache::lonxml::debug("in construction ignoring dates");
187: $status='CAN_ANSWER';
188: $datemsg='is in under construction';
189: }
190: return ($status,$datemsg);
1.20 albertel 191: }
192:
1.41 albertel 193: sub showhash {
1.52 albertel 194: my (%hash) = @_;
195: my $resultkey;
196: foreach $resultkey (sort keys %hash) {
1.73 albertel 197: if (ref($hash{$resultkey})) {
198: if ($hash{$resultkey} =~ /ARRAY/ ) {
199: my $string="$resultkey ---- (";
200: foreach my $elm (@{ $hash{$resultkey} }) {
201: $string.="$elm,";
202: }
203: chop($string);
204: &Apache::lonxml::debug("$string)");
205: } else {
206: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
207: }
208: } else {
209: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
210: }
1.52 albertel 211: }
212: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
213: return '';
1.41 albertel 214: }
215:
216: sub setuppermissions {
1.52 albertel 217: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
218: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
219: return ''
1.41 albertel 220: }
221:
222: sub setupheader {
1.52 albertel 223: my $request=$_[0];
224: if ($ENV{'browser.mathml'}) {
225: $request->content_type('text/xml');
226: } else {
227: $request->content_type('text/html');
228: }
1.68 albertel 229: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
230: &Apache::loncommon::no_cache($request);
231: }
1.52 albertel 232: $request->send_http_header;
233: return OK if $request->header_only;
234: return ''
1.41 albertel 235: }
1.35 albertel 236:
1.47 albertel 237: sub handle_save_or_undo {
1.52 albertel 238: my ($request,$problem,$result) = @_;
1.70 albertel 239: my $file = &Apache::lonnet::filelocation("",$request->uri);
240: my $filebak =$file.".bak";
241: my $filetmp =$file.".tmp";
1.64 albertel 242: my $error=0;
1.52 albertel 243:
244: if ($ENV{'form.Undo'} eq 'undo') {
1.70 albertel 245: my $error=0;
246: if (!copy($file,$filetmp)) { $error=1; }
247: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
248: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
249: if (!$error) {
250: $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52 albertel 251: } else {
1.70 albertel 252: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64 albertel 253: $error=1;
1.52 albertel 254: }
255: } else {
1.70 albertel 256: my $fs=Apache::File->new(">$filebak");
1.52 albertel 257: if (defined($fs)) {
258: print $fs $$problem;
1.70 albertel 259: $request->print("<b>Making Backup to $filebak</b><br />");
1.52 albertel 260: } else {
1.70 albertel 261: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64 albertel 262: $error=2;
1.52 albertel 263: }
1.70 albertel 264: my $fh=Apache::File->new(">$file");
1.52 albertel 265: if (defined($fh)) {
266: print $fh $$result;
1.70 albertel 267: $request->print("<b>Saving Modifications to $file</b><br />");
1.47 albertel 268: } else {
1.70 albertel 269: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64 albertel 270: $error|=4;
1.47 albertel 271: }
1.52 albertel 272: }
1.64 albertel 273: return $error;
274: }
275:
1.74 albertel 276: sub analyze {
277: my ($request,$file) = @_;
278: &Apache::lonxml::debug("Analyze");
279: my $result=&Apache::lonnet::ssi($request->uri,('grade_target' => 'analyze'));
280: &Apache::lonxml::debug(":$result:");
281: (my $garbage,$result)=split(/_HASH_REF__/,$result,2);
282: &showhash(&Apache::lonnet::str2hash($result));
283: return $result;
284: }
285:
1.64 albertel 286: sub editxmlmode {
287: my ($request,$file) = @_;
288: my $result;
289: my $problem=&Apache::lonnet::getfile($file);
290: if ($problem == -1) {
291: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
292: $problem='';
293: }
294: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
295: my $error=&handle_save_or_undo($request,\$problem,
296: \$ENV{'form.editxmltext'});
297: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
298: }
299: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
300: if ($cols > 80) { $cols = 80; }
301: $result.='<html><body bgcolor="#FFFFFF">
302: <form name="lonhomework" method="POST" action="'.
303: $ENV{'request.uri'}.'">
304: <input type="hidden" name="problemmode" value="EditXML" />
305: <input type="submit" name="problemmode" value="View" />
306: <input type="submit" name="problemmode" value="Edit" />
307: <hr />
308: <input type="submit" name="submit" value="Submit Changes" />
309: <input type="submit" name="Undo" value="undo" />
310: <hr />
311: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73 albertel 312: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 313: </form></body></html>';
314: $request->print($result);
315: return '';
1.47 albertel 316: }
317:
1.41 albertel 318: sub renderpage {
1.52 albertel 319: my ($request,$file) = @_;
320:
321: my (@targets) = &get_target();
1.70 albertel 322: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52 albertel 323: foreach my $target (@targets) {
324: #my $t0 = [&gettimeofday()];
325: my $problem=&Apache::lonnet::getfile($file);
326: if ($problem == -1) {
327: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
328: $problem='';
329: }
330:
331: my %mystyle;
332: my $result = '';
333: &Apache::inputtags::initialize_inputtags;
334: &Apache::edit::initialize_edit;
1.74 albertel 335: if ($target eq 'analyze') { %Apache::lonhomework::anaylze=(); }
1.52 albertel 336: if ($target eq 'web') {
1.76 albertel 337: my ($symb)=&Apache::lonxml::whichuser();
338: if ($symb eq '') {
1.52 albertel 339: if ($ENV{'request.state'} eq "construct") {
1.36 albertel 340: } else {
1.52 albertel 341: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51 harris41 342: }
1.52 albertel 343: }
344: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
345: }
1.64 albertel 346: #if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
1.52 albertel 347:
348: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
349: if ($default == -1) {
350: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
351: $default='';
352: }
1.70 albertel 353: &Apache::lonxml::debug("Should be parsing now");
1.52 albertel 354: $result = &Apache::lonxml::xmlparse($target, $problem,
355: $default.&setup_vars($target),%mystyle);
356:
357: #$request->print("Result follows:");
358: if ($target eq 'modified') {
359: &handle_save_or_undo($request,\$problem,\$result);
360: } else {
1.74 albertel 361: if ($target eq 'analyze') {
362: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75 albertel 363: undef(%Apache::lonhomework::analyze);
1.74 albertel 364: }
1.52 albertel 365: #my $td=&tv_interval($t0);
366: #if ( $Apache::lonxml::debug) {
367: #$result =~ s:</body>::;
368: #$result.="<br />Spent $td seconds processing target $target\n</body>";
369: #}
370: $request->print($result);
371: }
372: #$request->print(":Result ends");
373: #my $td=&tv_interval($t0);
374: }
1.41 albertel 375: }
376:
1.42 albertel 377: # with no arg it returns a HTML <option> list of the template titles
378: # with one arg it returns the filename associated with the arg passed
379: sub get_template_list {
1.52 albertel 380: my ($namewanted,$extension) = @_;
381: my $result;
382: &Apache::lonxml::debug("Looking for :$extension:");
383: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
384: my $name=&Apache::lonnet::metadata($file,'title');
385: if ($namewanted && ($name eq $namewanted)) {
386: $result=$file;
387: last;
388: } else {
389: $result.="<option>$name</option>";
1.42 albertel 390: }
1.52 albertel 391: }
392: return $result;
1.42 albertel 393: }
394:
395: sub newproblem {
1.65 matthew 396: my ($request) = @_;
397: my $extension=$request->uri;
398: $extension=~s:^.*\.([\w]+)$:$1:;
399: &Apache::lonxml::debug("Looking for :$extension:");
400: if ($ENV{'form.template'}) {
401: use File::Copy;
402: my $file = &get_template_list($ENV{'form.template'},$extension);
403: my $dest = &Apache::lonnet::filelocation("",$request->uri);
404: copy($file,$dest);
405: &renderpage($request,$dest);
406: } elsif($ENV{'form.newfile'}) {
407: # I don't like hard-coded filenames but for now, this will work.
408: use File::Copy;
409: my $templatefilename =
1.66 albertel 410: $request->dir_config('lonIncludes').'/templates/blank.problem';
411: &Apache::lonxml::debug("$templatefilename");
1.65 matthew 412: my $dest = &Apache::lonnet::filelocation("",$request->uri);
413: copy($templatefilename,$dest);
414: &renderpage($request,$dest);
415: }else {
416: my $templatelist=&get_template_list('',$extension);
417: my $url=$request->uri;
418: my $dest = &Apache::lonnet::filelocation("",$request->uri);
419: if (!defined($templatelist)) {
420: # We didn't find a template, so just create a blank problem.
421: $request->print(<<ENDNEWPROBLEM);
422: <body bgcolor="#FFFFFF">
423: The requested file $url doesn\'t exist. You can create a new $extension <br />
424: <form action="$url" method="POST">
425: <input type="submit" name="newfile" value="New $extension"><br />
426: </form>
427: </body>
428: ENDNEWPROBLEM
429: return '';
430: }
431: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 432: <body bgcolor="#FFFFFF">
1.65 matthew 433: The requested file $url doesn\'t exist. You can create a new $extension <br />
1.42 albertel 434: <form action="$url" method="POST">
1.50 albertel 435: <input type="submit" value="New $extension"><br />
1.42 albertel 436: <select name="template">
437: $templatelist
438: </select>
439: </form>
440: </body>
441: ENDNEWPROBLEM
1.65 matthew 442: }
443: return '';
1.42 albertel 444: }
445:
446: sub view_or_edit_menu {
1.52 albertel 447: my ($request) = @_;
448: my $url=$request->uri;
449: $request->print(<<EDITMENU);
1.42 albertel 450: <body bgcolor="#FFFFFF">
451: <form action="$url" method="POST">
452: Would you like to <input type="submit" name="problemmode" value="View"> or
453: <input type="submit" name="problemmode" value="Edit"> the problem.
454: </form>
455: </body>
456: EDITMENU
457: }
458:
1.41 albertel 459: sub handler {
1.52 albertel 460: #my $t0 = [&gettimeofday()];
461: my $request=$_[0];
1.41 albertel 462:
1.52 albertel 463: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41 albertel 464:
465: if (&setupheader($request)) { return OK; }
1.52 albertel 466: $ENV{'request.uri'}=$request->uri;
1.41 albertel 467:
468: #setup permissions
1.52 albertel 469: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
470: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
471: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71 albertel 472: # some times multiple problemmodes are submitted, need to select
473: # the last one
1.72 albertel 474: if ( defined($ENV{'form.problemmode'}) &&
1.77 ! albertel 475: ref($ENV{'form.problemmode'}) ) {
1.72 albertel 476: my $mode=$ENV{'form.problemmode'}->[-1];
1.71 albertel 477: undef $ENV{'form.problemmode'};
1.72 albertel 478: $ENV{'form.problemmode'}=$mode;
1.71 albertel 479: }
1.64 albertel 480: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52 albertel 481: my $file=&Apache::lonnet::filelocation("",$request->uri);
482:
483: #check if we know where we are
484: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
485: # if we are browsing we might not be able to know where we are
486: if ($Apache::lonhomework::browse ne 'F') {
487: #should know where we are, so ask
488: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 489: }
1.52 albertel 490: }
1.41 albertel 491:
1.52 albertel 492: if ($ENV{'request.state'} eq "construct") {
1.62 albertel 493: if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
494: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
495: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
496: }
1.52 albertel 497: if ( -e $file ) {
498: if (!(defined $ENV{'form.problemmode'})) {
499: #first visit to problem in construction space
1.64 albertel 500: #&view_or_edit_menu($request);
501: $ENV{'form.problemmode'}='View';
502: &renderpage($request,$file);
503: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
504: &editxmlmode($request,$file);
1.74 albertel 505: } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
506: &analyze($request,$file);
1.52 albertel 507: } else {
508: &renderpage($request,$file);
1.41 albertel 509: }
510: } else {
1.52 albertel 511: # requested file doesn't exist in contruction space
512: &newproblem($request);
1.41 albertel 513: }
1.52 albertel 514: } else {
515: # just render the page normally outside of construction space
1.74 albertel 516: &Apache::lonxml::debug("not construct");
1.52 albertel 517: &renderpage($request,$file);
518: }
519: #my $td=&tv_interval($t0);
520: #&Apache::lonxml::debug("Spent $td seconds processing");
521: # &Apache::lonhomework::send_footer($request);
522: # always turn off debug messages
523: $Apache::lonxml::debug=0;
524: return OK;
525:
1.1 albertel 526: }
527:
528: 1;
529: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>