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