File:
[LON-CAPA] /
loncom /
homework /
lonhomework.pm
Revision
1.141:
download - view:
text,
annotated -
select for diffs
Mon Sep 15 18:43:53 2003 UTC (20 years, 11 months ago) by
www
Branches:
MAIN
CVS tags:
HEAD
Bug #2190: call to &Apache::exists_config_define is not defined before child
completed another request.
At least things don't crash anymore - found no way of getting around error
messages, neither with eval nor with defined.
1: # The LearningOnline Network with CAPA
2: # The LON-CAPA Homework handler
3: #
4: # $Id: lonhomework.pm,v 1.141 2003/09/15 18:43:53 www 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: # Guy Albertelli
29: # 11/30 Gerd Kortemeyer
30: # 6/1,8/17,8/18 Gerd Kortemeyer
31: # 7/18 Jeremy Bowers
32:
33: package Apache::lonhomework;
34: use strict;
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();
45: use Apache::caparesponse();
46: use Apache::radiobuttonresponse();
47: use Apache::optionresponse();
48: use Apache::imageresponse();
49: use Apache::essayresponse();
50: use Apache::externalresponse();
51: use Apache::rankresponse();
52: use Apache::matchresponse();
53: use Apache::chemresponse();
54: use Apache::Constants qw(:common);
55: use HTML::Entities();
56: use Apache::loncommon();
57: #use Time::HiRes qw( gettimeofday tv_interval );
58:
59: BEGIN {
60: &Apache::lonxml::register_insert();
61: }
62:
63: sub get_target {
64: if (($ENV{'request.state'} eq "published") ||
65: ($ENV{'request.state'} eq "uploaded")) {
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'} )
70: && ($Apache::lonhomework::viewgrades eq 'F' )) {
71: return ($ENV{'form.grade_target'});
72: } elsif (defined($ENV{'form.grade_target'} )
73: && ($ENV{'form.grade_target'} eq 'answer')) {
74: return ($ENV{'form.grade_target'});
75: }
76:
77: if ( defined($ENV{'form.submitted'}) &&
78: ( !defined($ENV{'form.resetdata'})) &&
79: ( !defined($ENV{'form.newrandomization'}))) {
80: return ('grade', 'web');
81: } else {
82: return ('web');
83: }
84: } elsif ($ENV{'request.state'} eq "construct") {
85: if ( defined($ENV{'form.grade_target'}) ) {
86: return ($ENV{'form.grade_target'});
87: }
88: if ( defined($ENV{'form.preview'})) {
89: if ( defined($ENV{'form.submitted'})) {
90: return ('grade', 'web');
91: } else {
92: return ('web');
93: }
94: } else {
95: if ( $ENV{'form.problemmode'} eq 'View' ||
96: $ENV{'form.problemmode'} eq 'Discard Edits and View') {
97: if ( defined($ENV{'form.submitted'}) &&
98: (!defined($ENV{'form.resetdata'})) &&
99: (!defined($ENV{'form.newrandomization'}))) {
100: return ('grade', 'web','answer');
101: } else {
102: return ('web','answer');
103: }
104: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
105: if ( $ENV{'form.submitted'} eq 'edit' ) {
106: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
107: return ('modified','web','answer');
108: } else {
109: return ('modified','edit');
110: }
111: } else {
112: return ('edit');
113: }
114: } else {
115: return ('web');
116: }
117: }
118: }
119: return ();
120: }
121:
122: sub setup_vars {
123: my ($target) = @_;
124: return ';'
125: # return ';$external::target='.$target.';';
126: }
127:
128: sub send_header {
129: my ($request)= @_;
130: $request->print(&Apache::lontexconvert::header());
131: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
132: }
133:
134: sub createmenu {
135: my ($which,$request)=@_;
136: if ($which eq 'grade') {
137: $request->print('<script language="JavaScript">
138: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
139: "height=350,width=150,menubar=no");
140: </script>');
141: }
142: }
143:
144: sub send_footer {
145: my ($request)= @_;
146: # $request->print('</form>');
147: $request->print(&Apache::lontexconvert::footer());
148: }
149:
150: $Apache::lonxml::browse='';
151:
152: # JB, 9/24/2002: Any changes in this function may require a change
153: # in lonnavmaps::resource::getDateStatus.
154: sub check_access {
155: my ($id) = @_;
156: my $date ='';
157: my $status;
158: my $datemsg = '';
159: my $lastdate = '';
160: my $temp;
161: my $type;
162: my $passed;
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:
171: &Apache::lonxml::debug("checking for part :$id:");
172: &Apache::lonxml::debug("time:".time);
173: foreach $temp ("opendate","duedate","answerdate") {
174: $lastdate = $date;
175: $date = &Apache::lonnet::EXT("resource.$id.$temp");
176: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
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: }
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: }
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;
199: }
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') {
217: #check #tries, and if correct.
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'; }
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/) {
229: $status = 'CANNOT_ANSWER';
230: }
231: }
232:
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: #}
237:
238:
239: &Apache::lonxml::debug("sending back :$status:$datemsg:");
240: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
241: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
242: $status='CAN_ANSWER';
243: $datemsg='is closed but you are allowed to view it';
244: }
245:
246: return ($status,$datemsg);
247: }
248:
249: sub showhash {
250: my (%hash) = @_;
251: &showhashsubset(\%hash,'.');
252: return '';
253: }
254:
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:
272: sub showhashsubset {
273: my ($hash,$keyre) = @_;
274: my $resultkey;
275: foreach $resultkey (sort keys %$hash) {
276: if ($resultkey =~ /$keyre/) {
277: if (ref($$hash{$resultkey})) {
278: if ($$hash{$resultkey} =~ /ARRAY/ ) {
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},'.');
284: } else {
285: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
286: }
287: } else {
288: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
289: }
290: }
291: }
292: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
293: return '';
294: }
295:
296: sub setuppermissions {
297: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
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;
306: return ''
307: }
308:
309: sub setupheader {
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 ''
323: }
324:
325: sub handle_save_or_undo {
326: my ($request,$problem,$result) = @_;
327: my $file = &Apache::lonnet::filelocation("",$request->uri);
328: my $filebak =$file.".bak";
329: my $filetmp =$file.".tmp";
330: my $error=0;
331:
332: if ($ENV{'form.Undo'} eq 'undo') {
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>");
339: } else {
340: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
341: $error=1;
342: }
343: } else {
344: my $fs=Apache::File->new(">$filebak");
345: if (defined($fs)) {
346: print $fs $$problem;
347: $request->print("<b>Making Backup to $filebak</b><br />");
348: } else {
349: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
350: $error=2;
351: }
352: my $fh=Apache::File->new(">$file");
353: if (defined($fh)) {
354: print $fh $$result;
355: $request->print("<b>Saving Modifications to $file</b><br />");
356: } else {
357: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
358: $error|=4;
359: }
360: }
361: return $error;
362: }
363:
364: sub analyze_header {
365: my ($request) = @_;
366: my $result.='<html>
367: <head><title>Analyzing a problem</title></head>
368: <body bgcolor="#FFFFFF">
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:
382: sub analyze_footer {
383: my ($request) = @_;
384: my $result='</body></html>';
385: $request->print($result);
386: $request->rflush();
387: }
388:
389: sub analyze {
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);
397: my %prog_state=
398: &Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
399: 'Getting Problem Variants',
400: $ENV{'form.numtoanalyze'});
401: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
402: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
403: 'last problem');
404: my $subresult=&Apache::lonnet::ssi($request->uri,
405: ('grade_target' => 'analyze'),
406: ('rndseed' => $i+$rndseed));
407: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
408: my %analyze=&Apache::lonnet::str2hash($subresult);
409: my @parts;
410: if (defined(@{ $analyze{'parts'} })) {
411: @parts=@{ $analyze{'parts'} };
412: }
413: foreach my $part (@parts) {
414: if (!exists($allparts{$part})) {$allparts{$part}=1;};
415: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
416: $analyze{$part.'.type'} eq 'stringresponse' ||
417: $analyze{$part.'.type'} eq 'formularesponse' ) {
418: push( @{ $overall{$part.'.answer'} },
419: [@{ $analyze{$part.'.answer'} }]);
420: }
421: }
422: }
423: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
424: 'Analyzing Results');
425: foreach my $part (sort(keys(%allparts))) {
426: if (defined(@{ $overall{$part.'.answer'} })) {
427: my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
428: $request->print('<table><tr><td colspan="'.($num_cols+1).'">Part '.$part.'</td></tr>');
429: my %frequency;
430: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
431: $frequency{join("\0",@{ $answer })}++;
432: }
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)).
437: '</td><td>('.$frequency{$answer}.
438: ')</td></tr>');
439: }
440: $request->print('</table>');
441: } else {
442: $request->print('<p>Part '.$part.
443: ' is not analyzable at this time</p>');
444: }
445: }
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: }
451: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
452: &analyze_footer($request);
453: &Apache::lonhomework::showhash(%overall);
454: return $result;
455: }
456:
457: sub editxmlmode {
458: my ($request,$file) = @_;
459: my $result;
460: my $problem=&Apache::lonnet::getfile($file);
461: if ($problem eq -1) {
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: }
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);
477: my $xml_help = Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
478: "Problem Editing Help");
479: if ($cols > 80) { $cols = 80; }
480: if ($cols < 70) { $cols = 70; }
481: if ($rows < 20) { $rows = 20; }
482: $result.='<html><body bgcolor="#FFFFFF">
483: <form name="lonhomework" method="POST" action="'.
484: $ENV{'request.uri'}.'">
485: <input type="hidden" name="problemmode" value="EditXML" />
486: <input type="submit" name="problemmode" value="Discard Edits and View" />
487: <input type="submit" name="problemmode" value="Edit" />
488: <hr />
489: <input type="submit" name="submit" value="Submit Changes" />
490: <input type="submit" name="submit" value="Submit Changes and View" />
491: <input type="submit" name="Undo" value="undo" />
492: <hr />
493: ' . $xml_help . '
494: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
495: &HTML::Entities::encode($problem).'</textarea>
496: </form></body></html>';
497: $request->print($result);
498: }
499: return '';
500: }
501:
502: sub renderpage {
503: my ($request,$file) = @_;
504:
505: my (@targets) = &get_target();
506: &Apache::lonxml::debug("Running targets ".join(':',@targets));
507: foreach my $target (@targets) {
508: #my $t0 = [&gettimeofday()];
509: my $problem=&Apache::lonnet::getfile($file);
510: if ($problem eq -1) {
511: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
512: $problem='';
513: }
514:
515: my %mystyle;
516: my $result = '';
517: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
518: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
519: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
520:
521: &Apache::lonxml::debug("Should be parsing now");
522: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
523: &setup_vars($target),%mystyle);
524: undef($Apache::lonhomework::parsing_a_problem);
525: #$request->print("Result follows:");
526: if ($target eq 'modified') {
527: &handle_save_or_undo($request,\$problem,\$result);
528: } else {
529: if ($target eq 'analyze') {
530: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
531: undef(%Apache::lonhomework::analyze);
532: }
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);
539: $request->rflush();
540: }
541: #$request->print(":Result ends");
542: #my $td=&tv_interval($t0);
543: }
544: }
545:
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 {
549: my ($namewanted,$extension) = @_;
550: my $result;
551: my @allnames;
552: &Apache::lonxml::debug("Looking for :$extension:");
553: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
554: my $name=&Apache::lonnet::metadata($file,'title');
555: if ($namewanted && ($name eq $namewanted)) {
556: $result=$file;
557: last;
558: } else {
559: if ($name) { push (@allnames, $name); }
560: }
561: }
562: if (@allnames && !$result) {
563: $result="<option>Select a $extension template</option>\n<option>".
564: join('</option><option>',sort(@allnames)).'</option>';
565: }
566: return $result;
567: }
568:
569: sub newproblem {
570: my ($request) = @_;
571: my $extension=$request->uri;
572: $extension=~s:^.*\.([\w]+)$:$1:;
573: &Apache::lonxml::debug("Looking for :$extension:");
574: my $templatelist=&get_template_list('',$extension);
575: if ($ENV{'form.template'} &&
576: $ENV{'form.template'} ne "Select a $extension template") {
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);
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);
591: } else {
592: my $url=$request->uri;
593: my $dest = &Apache::lonnet::filelocation("",$request->uri);
594: my $errormsg;
595: if ($ENV{'form.newfile'}) {
596: $errormsg='<p><font color="red">You did not select a template.</font></p>'."\n";
597: }
598: my $instructions;
599: if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
600: $request->print(<<ENDNEWPROBLEM);
601: <body bgcolor="#FFFFFF">
602: <h1>Creating a new $extension resource</h1>
603: $errormsg
604: The requested file <tt>$url</tt> currently does not exist.
605: <p>
606: <b>To create a new $extension$instructions click on the "Create $extension" button.</b>
607: </p>
608: <p><form action="$url" method="POST">
609: ENDNEWPROBLEM
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\" />");
614: $request->print("</form></p></body>");
615: }
616: return '';
617: }
618:
619: sub view_or_edit_menu {
620: my ($request) = @_;
621: my $url=$request->uri;
622: $request->print(<<EDITMENU);
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:
632: sub handler {
633: #my $t0 = [&gettimeofday()];
634: my $request=$_[0];
635:
636: $Apache::lonxml::debug=$ENV{'user.debug'};
637:
638: if (&setupheader($request)) { return OK; }
639: $ENV{'request.uri'}=$request->uri;
640:
641: #setup permissions
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:");
645: # some times multiple problemmodes are submitted, need to select
646: # the last one
647: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
648: if ( defined($ENV{'form.problemmode'}) &&
649: ref($ENV{'form.problemmode'}) ) {
650: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
651: my $mode=$ENV{'form.problemmode'}->[-1];
652: undef $ENV{'form.problemmode'};
653: $ENV{'form.problemmode'}=$mode;
654: }
655: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
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:
664: &Apache::lonnet::cleanenv();
665:
666: $request->internal_redirect('/adm/ambiguous'); return;
667: }
668: }
669:
670: my ($symb) = &Apache::lonxml::whichuser();
671: &Apache::lonxml::debug('symb is '.$symb);
672: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
673: if ($ENV{'form.resetdata'} eq 'Reset Submissions' ||
674: $ENV{'form.resetdata'} eq 'New Problem Variation' ||
675: $ENV{'form.newrandomization'} eq 'New Randomization') {
676: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
677: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
678: &Apache::lonxml::debug("Attempt reset");
679: }
680: }
681: if ($ENV{'request.state'} eq "construct") {
682: if ( -e $file ) {
683: &Apache::loncommon::get_unprocessed_cgi
684: ($ENV{'QUERY_STRING'},['problemmode']);
685: if (!(defined $ENV{'form.problemmode'})) {
686: #first visit to problem in construction space
687: #&view_or_edit_menu($request);
688: $ENV{'form.problemmode'}='View';
689: &renderpage($request,$file);
690: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
691: &editxmlmode($request,$file);
692: } elsif ($ENV{'form.problemmode'} eq 'Calculate answers') {
693: &analyze($request,$file);
694: } else {
695: &renderpage($request,$file);
696: }
697: } else {
698: # requested file doesn't exist in contruction space
699: &newproblem($request);
700: }
701: } else {
702: # just render the page normally outside of construction space
703: &Apache::lonxml::debug("not construct");
704: &renderpage($request,$file);
705: }
706: #my $td=&tv_interval($t0);
707: #&Apache::lonxml::debug("Spent $td seconds processing");
708: # &Apache::lonhomework::send_footer($request);
709: # always turn off debug messages
710: $Apache::lonxml::debug=0;
711: return OK;
712:
713: }
714:
715: 1;
716: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>