Annotation of loncom/homework/lonhomework.pm, revision 1.136
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.136 ! bowersj2 4: # $Id: lonhomework.pm,v 1.135 2003/06/16 21:32:43 www 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.82 bowersj2 31: # 7/18 Jeremy Bowers
1.1 albertel 32:
33: package Apache::lonhomework;
34: use strict;
1.73 albertel 35: use Apache::style();
36: use Apache::lonxml();
37: use Apache::lonnet();
38: use Apache::lonplot();
39: use Apache::inputtags();
40: use Apache::structuretags();
41: use Apache::randomlabel();
42: use Apache::response();
43: use Apache::hint();
44: use Apache::outputtags();
1.83 albertel 45: use Apache::caparesponse();
46: use Apache::radiobuttonresponse();
47: use Apache::optionresponse();
48: use Apache::imageresponse();
49: use Apache::essayresponse();
50: use Apache::externalresponse();
1.106 albertel 51: use Apache::rankresponse();
1.107 albertel 52: use Apache::matchresponse();
1.122 albertel 53: #use Apache::chemresponse();
1.26 www 54: use Apache::Constants qw(:common);
1.73 albertel 55: use HTML::Entities();
1.83 albertel 56: use Apache::loncommon();
1.47 albertel 57: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 58:
1.69 harris41 59: BEGIN {
1.43 albertel 60: &Apache::lonxml::register_insert();
61: }
62:
1.5 albertel 63: sub get_target {
1.126 www 64: if (($ENV{'request.state'} eq "published") ||
65: ($ENV{'request.state'} eq "uploaded")) {
1.90 sakharuk 66: if ( defined($ENV{'form.grade_target'} )
67: && ($ENV{'form.grade_target'} eq 'tex')) {
68: return ($ENV{'form.grade_target'});
69: } elsif ( defined($ENV{'form.grade_target'} )
1.119 albertel 70: && ($Apache::lonhomework::viewgrades eq 'F' )) {
1.66 albertel 71: return ($ENV{'form.grade_target'});
72: }
1.90 sakharuk 73:
1.118 albertel 74: if ( defined($ENV{'form.submitted'}) &&
75: ( !defined($ENV{'form.resetdata'}))) {
1.52 albertel 76: return ('grade', 'web');
77: } else {
78: return ('web');
79: }
80: } elsif ($ENV{'request.state'} eq "construct") {
1.74 albertel 81: if ( defined($ENV{'form.grade_target'}) ) {
82: return ($ENV{'form.grade_target'});
83: }
1.62 albertel 84: if ( defined($ENV{'form.preview'})) {
85: if ( defined($ENV{'form.submitted'})) {
1.52 albertel 86: return ('grade', 'web');
87: } else {
88: return ('web');
89: }
90: } else {
1.88 albertel 91: if ( $ENV{'form.problemmode'} eq 'View' ||
92: $ENV{'form.problemmode'} eq 'Discard Edits and View') {
1.62 albertel 93: if ( defined($ENV{'form.submitted'}) &&
94: (!defined($ENV{'form.resetdata'})) ) {
1.60 albertel 95: return ('grade', 'web','answer');
1.42 albertel 96: } else {
1.60 albertel 97: return ('web','answer');
1.42 albertel 98: }
1.52 albertel 99: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
100: if ( $ENV{'form.submitted'} eq 'edit' ) {
1.80 albertel 101: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
1.81 albertel 102: return ('modified','web','answer');
1.80 albertel 103: } else {
104: return ('modified','edit');
105: }
1.42 albertel 106: } else {
1.52 albertel 107: return ('edit');
1.42 albertel 108: }
1.52 albertel 109: } else {
110: return ('web');
111: }
1.15 albertel 112: }
1.52 albertel 113: }
114: return ();
1.5 albertel 115: }
116:
1.3 albertel 117: sub setup_vars {
1.52 albertel 118: my ($target) = @_;
119: return ';'
1.11 albertel 120: # return ';$external::target='.$target.';';
1.2 albertel 121: }
122:
123: sub send_header {
1.52 albertel 124: my ($request)= @_;
125: $request->print(&Apache::lontexconvert::header());
1.16 albertel 126: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 127: }
128:
1.36 albertel 129: sub createmenu {
1.52 albertel 130: my ($which,$request)=@_;
131: if ($which eq 'grade') {
132: $request->print('<script language="JavaScript">
1.91 albertel 133: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52 albertel 134: "height=350,width=150,menubar=no");
135: </script>');
136: }
1.36 albertel 137: }
138:
1.2 albertel 139: sub send_footer {
1.52 albertel 140: my ($request)= @_;
1.16 albertel 141: # $request->print('</form>');
1.52 albertel 142: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 143: }
144:
1.52 albertel 145: $Apache::lonxml::browse='';
1.53 www 146:
1.92 bowersj2 147: # JB, 9/24/2002: Any changes in this function may require a change
148: # in lonnavmaps::resource::getDateStatus.
1.53 www 149: sub check_access {
1.52 albertel 150: my ($id) = @_;
151: my $date ='';
1.103 albertel 152: my $status;
1.52 albertel 153: my $datemsg = '';
154: my $lastdate = '';
155: my $temp;
156: my $type;
157: my $passed;
1.106 albertel 158:
159: if ($ENV{'request.state'} eq "construct") {
160: &Apache::lonxml::debug("in construction ignoring dates");
161: $status='CAN_ANSWER';
162: $datemsg='is in under construction';
163: return ($status,$datemsg);
164: }
165:
1.52 albertel 166: &Apache::lonxml::debug("checking for part :$id:");
1.73 albertel 167: &Apache::lonxml::debug("time:".time);
1.52 albertel 168: foreach $temp ("opendate","duedate","answerdate") {
169: $lastdate = $date;
170: $date = &Apache::lonnet::EXT("resource.$id.$temp");
1.87 www 171: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
1.103 albertel 172: if ($thistype =~ /^(con_lost|no_such_host)/ ||
173: $date =~ /^(con_lost|no_such_host)/) {
174: $status='UNAVAILABLE';
175: $date="may open later.";
176: return($status,$date);
177: }
1.87 www 178: if ($thistype eq 'date_interval') {
179: if ($temp eq 'opendate') {
180: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
181: }
182: if ($temp eq 'answerdate') {
183: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
184: }
185: }
1.52 albertel 186: &Apache::lonxml::debug("found :$date: for :$temp:");
187: if ($date eq '') {
188: $date = "an unknown date"; $passed = 0;
189: } elsif ($date eq 'con_lost') {
190: $date = "an indeterminate date"; $passed = 0;
191: } else {
192: if (time < $date) { $passed = 0; } else { $passed = 1; }
193: $date = localtime $date;
1.51 harris41 194: }
1.52 albertel 195: if (!$passed) { $type=$temp; last; }
196: }
197: &Apache::lonxml::debug("have :$type:$passed:");
198: if ($passed) {
199: $status='SHOW_ANSWER';
200: $datemsg=$date;
201: } elsif ($type eq 'opendate') {
202: $status='CLOSED';
203: $datemsg = "will open on $date";
204: } elsif ($type eq 'duedate') {
205: $status='CAN_ANSWER';
206: $datemsg = "is due at $date";
207: } elsif ($type eq 'answerdate') {
208: $status='CLOSED';
209: $datemsg = "was due on $lastdate, and answers will be available on $date";
210: }
211: if ($status eq 'CAN_ANSWER') {
1.121 albertel 212: #check #tries, and if correct.
1.52 albertel 213: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
214: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
215: if ( $tries eq '' ) { $tries = '0'; }
216: if ( $maxtries eq '' ) { $maxtries = '2'; }
1.129 albertel 217: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
218: # if (correct and show prob status) or excused then CANNOT_ANSWER
219: if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
220: &&
221: lc($Apache::lonhomework::problemstatus) ne 'no')
222: ||
223: $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
1.121 albertel 224: $status = 'CANNOT_ANSWER';
225: }
1.52 albertel 226: }
1.54 www 227:
1.133 albertel 228: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
229: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
230: # return ('UNCHECKEDOUT','needs to be checked out');
231: #}
1.54 www 232:
233:
1.52 albertel 234: &Apache::lonxml::debug("sending back :$status:$datemsg:");
235: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
1.91 albertel 236: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
1.52 albertel 237: $status='CAN_ANSWER';
238: $datemsg='is closed but you are allowed to view it';
239: }
1.106 albertel 240:
1.52 albertel 241: return ($status,$datemsg);
1.20 albertel 242: }
243:
1.41 albertel 244: sub showhash {
1.52 albertel 245: my (%hash) = @_;
1.99 albertel 246: &showhashsubset(\%hash,'.');
1.79 albertel 247: return '';
248: }
249:
1.106 albertel 250: sub showarray {
251: my ($array)=@_;
252: my $string="(";
253: foreach my $elm (@{ $array }) {
254: if (ref($elm)) {
255: if ($elm =~ /ARRAY/ ) {
256: $string.=&showarray($elm);
257: }
258: } else {
259: $string.="$elm,"
260: }
261: }
262: chop($string);
263: $string.=")";
264: return $string;
265: }
266:
1.79 albertel 267: sub showhashsubset {
268: my ($hash,$keyre) = @_;
1.52 albertel 269: my $resultkey;
1.79 albertel 270: foreach $resultkey (sort keys %$hash) {
271: if ($resultkey =~ /$keyre/) {
272: if (ref($$hash{$resultkey})) {
273: if ($$hash{$resultkey} =~ /ARRAY/ ) {
1.106 albertel 274: &Apache::lonxml::debug("$resultkey ---- ".
275: &showarray($$hash{$resultkey}));
276: } elsif ($$hash{$resultkey} =~ /HASH/ ) {
277: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
278: &showhashsubset($$hash{$resultkey},'.');
1.79 albertel 279: } else {
1.106 albertel 280: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 281: }
282: } else {
1.79 albertel 283: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 284: }
285: }
1.52 albertel 286: }
287: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
288: return '';
1.41 albertel 289: }
290:
291: sub setuppermissions {
1.52 albertel 292: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
1.127 matthew 293: my $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
294: if (! $viewgrades &&
295: exists($ENV{'request.course.sec'}) &&
296: $ENV{'request.course.sec'} !~ /^\s*$/) {
297: $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}.
298: '/'.$ENV{'request.course.sec'});
299: }
300: $Apache::lonhomework::viewgrades = $viewgrades;
1.52 albertel 301: return ''
1.41 albertel 302: }
303:
304: sub setupheader {
1.120 albertel 305: my $request=$_[0];
306: if ($ENV{'browser.mathml'}) {
307: $request->content_type('text/xml');
308: } else {
309: $request->content_type('text/html');
310: }
311: $request->content_encoding('UTF-8');
312: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
313: &Apache::loncommon::no_cache($request);
314: }
315: $request->send_http_header;
316: return OK if $request->header_only;
317: return ''
1.41 albertel 318: }
1.35 albertel 319:
1.47 albertel 320: sub handle_save_or_undo {
1.52 albertel 321: my ($request,$problem,$result) = @_;
1.70 albertel 322: my $file = &Apache::lonnet::filelocation("",$request->uri);
323: my $filebak =$file.".bak";
324: my $filetmp =$file.".tmp";
1.64 albertel 325: my $error=0;
1.52 albertel 326:
327: if ($ENV{'form.Undo'} eq 'undo') {
1.70 albertel 328: my $error=0;
329: if (!copy($file,$filetmp)) { $error=1; }
330: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
331: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
332: if (!$error) {
333: $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52 albertel 334: } else {
1.70 albertel 335: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64 albertel 336: $error=1;
1.52 albertel 337: }
338: } else {
1.70 albertel 339: my $fs=Apache::File->new(">$filebak");
1.52 albertel 340: if (defined($fs)) {
341: print $fs $$problem;
1.70 albertel 342: $request->print("<b>Making Backup to $filebak</b><br />");
1.52 albertel 343: } else {
1.70 albertel 344: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64 albertel 345: $error=2;
1.52 albertel 346: }
1.70 albertel 347: my $fh=Apache::File->new(">$file");
1.52 albertel 348: if (defined($fh)) {
349: print $fh $$result;
1.70 albertel 350: $request->print("<b>Saving Modifications to $file</b><br />");
1.47 albertel 351: } else {
1.70 albertel 352: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64 albertel 353: $error|=4;
1.47 albertel 354: }
1.52 albertel 355: }
1.64 albertel 356: return $error;
357: }
358:
1.101 albertel 359: sub analyze_header {
360: my ($request) = @_;
1.109 albertel 361: my $result.='<html>
362: <head><title>Analyzing a problem</title></head>
363: <body bgcolor="#FFFFFF">
1.101 albertel 364: <form name="lonhomework" method="POST" action="'.
365: $ENV{'request.uri'}.'">
366: <input type="submit" name="problemmode" value="EditXML" />
367: <input type="submit" name="problemmode" value="Edit" />
368: <hr />
369: <input type="submit" name="submit" value="View" />
370: <hr />
371: List of possible answers:
372: </form>';
373: $request->print($result);
374: $request->rflush();
375: }
376:
1.109 albertel 377: sub analyze_footer {
378: my ($request) = @_;
379: my $result='</body></html>';
380: $request->print($result);
381: $request->rflush();
382: }
383:
1.74 albertel 384: sub analyze {
1.101 albertel 385: my ($request,$file) = @_;
386: &Apache::lonxml::debug("Analyze");
387: my $result;
388: my %overall;
389: my %allparts;
390: my $rndseed=$ENV{'form.rndseed'};
391: &analyze_header($request);
1.114 albertel 392: my %prog_state=
393: &Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
394: 'Getting Problem Variants',
395: $ENV{'form.numtoanalyze'});
1.102 albertel 396: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 397: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
398: 'last problem');
1.101 albertel 399: my $subresult=&Apache::lonnet::ssi($request->uri,
400: ('grade_target' => 'analyze'),
1.130 albertel 401: ('rndseed' => $i+$rndseed));
1.101 albertel 402: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
403: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 404: my @parts;
405: if (defined(@{ $analyze{'parts'} })) {
406: @parts=@{ $analyze{'parts'} };
407: }
1.101 albertel 408: foreach my $part (@parts) {
409: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 410: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
411: $analyze{$part.'.type'} eq 'stringresponse' ||
412: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.101 albertel 413: push( @{ $overall{$part.'.answer'} },
414: [@{ $analyze{$part.'.answer'} }]);
415: }
416: }
417: }
1.114 albertel 418: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
419: 'Analyzing Results');
1.134 albertel 420: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 421: if (defined(@{ $overall{$part.'.answer'} })) {
1.132 albertel 422: my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
423: $request->print('<table><tr><td colspan="'.($num_cols+1).'">Part '.$part.'</td></tr>');
1.130 albertel 424: my %frequency;
1.109 albertel 425: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
1.132 albertel 426: $frequency{join("\0",@{ $answer })}++;
1.130 albertel 427: }
1.132 albertel 428: $request->print('<tr><td colspan="'.($num_cols).'">Answer</td><td>Frequency</td></tr>');
429: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
430: $request->print('<tr><td align="right">'.
431: join('</td><td align="right">',split("\0",$answer)).
1.130 albertel 432: '</td><td>('.$frequency{$answer}.
433: ')</td></tr>');
1.109 albertel 434: }
435: $request->print('</table>');
436: } else {
437: $request->print('<p>Part '.$part.
1.130 albertel 438: ' is not analyzable at this time</p>');
1.101 albertel 439: }
440: }
1.130 albertel 441: if (scalar(keys(%allparts)) == 0 ) {
442: $request->print('<p>Found no analyzable parts in this problem,
443: currently only Numerical, Formula and String response
444: styles are supported.</p>');
445: }
1.114 albertel 446: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 447: &analyze_footer($request);
1.101 albertel 448: &Apache::lonhomework::showhash(%overall);
449: return $result;
1.74 albertel 450: }
451:
1.64 albertel 452: sub editxmlmode {
453: my ($request,$file) = @_;
454: my $result;
455: my $problem=&Apache::lonnet::getfile($file);
1.116 albertel 456: if ($problem eq -1) {
1.64 albertel 457: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
458: $problem='';
459: }
460: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
461: my $error=&handle_save_or_undo($request,\$problem,
462: \$ENV{'form.editxmltext'});
463: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
464: }
1.80 albertel 465: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
466: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
467: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
468: $ENV{'form.problemmode'}='View';
469: &renderpage($request,$file);
470: } else {
471: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.136 ! bowersj2 472: my $xml_help = Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
! 473: "Problem Editing Help");
1.80 albertel 474: if ($cols > 80) { $cols = 80; }
1.89 albertel 475: if ($cols < 70) { $cols = 70; }
476: if ($rows < 20) { $rows = 20; }
1.80 albertel 477: $result.='<html><body bgcolor="#FFFFFF">
1.64 albertel 478: <form name="lonhomework" method="POST" action="'.
479: $ENV{'request.uri'}.'">
480: <input type="hidden" name="problemmode" value="EditXML" />
1.80 albertel 481: <input type="submit" name="problemmode" value="Discard Edits and View" />
1.64 albertel 482: <input type="submit" name="problemmode" value="Edit" />
483: <hr />
484: <input type="submit" name="submit" value="Submit Changes" />
1.80 albertel 485: <input type="submit" name="submit" value="Submit Changes and View" />
1.64 albertel 486: <input type="submit" name="Undo" value="undo" />
487: <hr />
1.110 albertel 488: ' . $xml_help . '
1.64 albertel 489: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73 albertel 490: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 491: </form></body></html>';
1.80 albertel 492: $request->print($result);
493: }
1.64 albertel 494: return '';
1.47 albertel 495: }
496:
1.41 albertel 497: sub renderpage {
1.52 albertel 498: my ($request,$file) = @_;
499:
500: my (@targets) = &get_target();
1.70 albertel 501: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52 albertel 502: foreach my $target (@targets) {
503: #my $t0 = [&gettimeofday()];
504: my $problem=&Apache::lonnet::getfile($file);
1.117 albertel 505: if ($problem eq -1) {
1.52 albertel 506: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
507: $problem='';
508: }
509:
510: my %mystyle;
511: my $result = '';
1.109 albertel 512: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1.85 albertel 513: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
514: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
1.52 albertel 515:
1.70 albertel 516: &Apache::lonxml::debug("Should be parsing now");
1.78 albertel 517: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
1.98 albertel 518: &setup_vars($target),%mystyle);
1.113 albertel 519: undef($Apache::lonhomework::parsing_a_problem);
1.52 albertel 520: #$request->print("Result follows:");
521: if ($target eq 'modified') {
522: &handle_save_or_undo($request,\$problem,\$result);
523: } else {
1.74 albertel 524: if ($target eq 'analyze') {
525: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75 albertel 526: undef(%Apache::lonhomework::analyze);
1.74 albertel 527: }
1.52 albertel 528: #my $td=&tv_interval($t0);
529: #if ( $Apache::lonxml::debug) {
530: #$result =~ s:</body>::;
531: #$result.="<br />Spent $td seconds processing target $target\n</body>";
532: #}
533: $request->print($result);
1.112 albertel 534: $request->rflush();
1.52 albertel 535: }
536: #$request->print(":Result ends");
537: #my $td=&tv_interval($t0);
538: }
1.41 albertel 539: }
540:
1.42 albertel 541: # with no arg it returns a HTML <option> list of the template titles
542: # with one arg it returns the filename associated with the arg passed
543: sub get_template_list {
1.52 albertel 544: my ($namewanted,$extension) = @_;
545: my $result;
1.85 albertel 546: my @allnames;
1.52 albertel 547: &Apache::lonxml::debug("Looking for :$extension:");
1.91 albertel 548: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
1.52 albertel 549: my $name=&Apache::lonnet::metadata($file,'title');
550: if ($namewanted && ($name eq $namewanted)) {
551: $result=$file;
552: last;
553: } else {
1.104 www 554: if ($name) { push (@allnames, $name); }
1.42 albertel 555: }
1.52 albertel 556: }
1.86 albertel 557: if (@allnames && !$result) {
1.115 www 558: $result="<option>Select a $extension template</option>\n<option>".
1.128 albertel 559: join('</option><option>',sort(@allnames)).'</option>';
1.85 albertel 560: }
1.52 albertel 561: return $result;
1.42 albertel 562: }
563:
564: sub newproblem {
1.65 matthew 565: my ($request) = @_;
566: my $extension=$request->uri;
567: $extension=~s:^.*\.([\w]+)$:$1:;
568: &Apache::lonxml::debug("Looking for :$extension:");
1.131 albertel 569: my $templatelist=&get_template_list('',$extension);
1.85 albertel 570: if ($ENV{'form.template'} &&
1.128 albertel 571: $ENV{'form.template'} ne "Select a $extension template") {
1.65 matthew 572: use File::Copy;
573: my $file = &get_template_list($ENV{'form.template'},$extension);
574: my $dest = &Apache::lonnet::filelocation("",$request->uri);
575: copy($file,$dest);
576: &renderpage($request,$dest);
1.131 albertel 577: } elsif($ENV{'form.newfile'} && !$templatelist) {
578: # I don't like hard-coded filenames but for now, this will work.
579: use File::Copy;
580: my $templatefilename =
581: $request->dir_config('lonIncludes').'/templates/blank.problem';
582: &Apache::lonxml::debug("$templatefilename");
583: my $dest = &Apache::lonnet::filelocation("",$request->uri);
584: copy($templatefilename,$dest);
585: &renderpage($request,$dest);
1.85 albertel 586: } else {
1.65 matthew 587: my $url=$request->uri;
588: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 589: my $errormsg;
590: if ($ENV{'form.newfile'}) {
591: $errormsg='<p><font color="red">You did not select a template.</font></p>'."\n";
592: }
1.85 albertel 593: my $instructions;
1.105 www 594: if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
1.65 matthew 595: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 596: <body bgcolor="#FFFFFF">
1.105 www 597: <h1>Creating a new $extension resource</h1>
1.128 albertel 598: $errormsg
1.105 www 599: The requested file <tt>$url</tt> currently does not exist.
600: <p>
1.128 albertel 601: <b>To create a new $extension$instructions click on the "Create $extension" button.</b>
1.105 www 602: </p>
603: <p><form action="$url" method="POST">
1.42 albertel 604: ENDNEWPROBLEM
1.85 albertel 605: if (defined($templatelist)) {
606: $request->print("<select name=\"template\">$templatelist</select>");
607: }
608: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
1.105 www 609: $request->print("</form></p></body>");
1.65 matthew 610: }
611: return '';
1.42 albertel 612: }
613:
614: sub view_or_edit_menu {
1.52 albertel 615: my ($request) = @_;
616: my $url=$request->uri;
617: $request->print(<<EDITMENU);
1.42 albertel 618: <body bgcolor="#FFFFFF">
619: <form action="$url" method="POST">
620: Would you like to <input type="submit" name="problemmode" value="View"> or
621: <input type="submit" name="problemmode" value="Edit"> the problem.
622: </form>
623: </body>
624: EDITMENU
625: }
626:
1.41 albertel 627: sub handler {
1.52 albertel 628: #my $t0 = [&gettimeofday()];
629: my $request=$_[0];
1.41 albertel 630:
1.95 albertel 631: $Apache::lonxml::debug=$ENV{'user.debug'};
1.41 albertel 632:
633: if (&setupheader($request)) { return OK; }
1.52 albertel 634: $ENV{'request.uri'}=$request->uri;
1.41 albertel 635:
636: #setup permissions
1.52 albertel 637: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
638: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
639: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71 albertel 640: # some times multiple problemmodes are submitted, need to select
641: # the last one
1.80 albertel 642: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.72 albertel 643: if ( defined($ENV{'form.problemmode'}) &&
1.77 albertel 644: ref($ENV{'form.problemmode'}) ) {
1.80 albertel 645: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
1.72 albertel 646: my $mode=$ENV{'form.problemmode'}->[-1];
1.71 albertel 647: undef $ENV{'form.problemmode'};
1.72 albertel 648: $ENV{'form.problemmode'}=$mode;
1.71 albertel 649: }
1.64 albertel 650: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52 albertel 651: my $file=&Apache::lonnet::filelocation("",$request->uri);
652:
653: #check if we know where we are
654: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
655: # if we are browsing we might not be able to know where we are
656: if ($Apache::lonhomework::browse ne 'F') {
657: #should know where we are, so ask
658: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 659: }
1.52 albertel 660: }
1.41 albertel 661:
1.118 albertel 662: my ($symb) = &Apache::lonxml::whichuser();
663: &Apache::lonxml::debug('symb is '.$symb);
664: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
665: if ($ENV{'form.resetdata'} eq 'Reset Submissions' ||
666: $ENV{'form.resetdata'} eq 'New Problem Variation' ) {
667: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
668: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
669: }
670: }
1.52 albertel 671: if ($ENV{'request.state'} eq "construct") {
672: if ( -e $file ) {
1.135 www 673: &Apache::loncommon::get_unprocessed_cgi
674: ($ENV{'QUERY_STRING'},['problemmode']);
1.52 albertel 675: if (!(defined $ENV{'form.problemmode'})) {
676: #first visit to problem in construction space
1.64 albertel 677: #&view_or_edit_menu($request);
678: $ENV{'form.problemmode'}='View';
679: &renderpage($request,$file);
680: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
681: &editxmlmode($request,$file);
1.130 albertel 682: } elsif ($ENV{'form.problemmode'} eq 'Calculate answers') {
1.74 albertel 683: &analyze($request,$file);
1.52 albertel 684: } else {
685: &renderpage($request,$file);
1.41 albertel 686: }
687: } else {
1.52 albertel 688: # requested file doesn't exist in contruction space
689: &newproblem($request);
1.41 albertel 690: }
1.52 albertel 691: } else {
692: # just render the page normally outside of construction space
1.74 albertel 693: &Apache::lonxml::debug("not construct");
1.52 albertel 694: &renderpage($request,$file);
695: }
696: #my $td=&tv_interval($t0);
697: #&Apache::lonxml::debug("Spent $td seconds processing");
698: # &Apache::lonhomework::send_footer($request);
699: # always turn off debug messages
700: $Apache::lonxml::debug=0;
701: return OK;
702:
1.1 albertel 703: }
704:
705: 1;
706: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>