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