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