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