Annotation of loncom/homework/lonhomework.pm, revision 1.164
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.164 ! albertel 4: # $Id: lonhomework.pm,v 1.163 2004/02/13 21:21:30 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") {
204: &Apache::lonxml::debug("in construction ignoring dates");
205: $status='CAN_ANSWER';
1.146 albertel 206: $datemsg=&mt('is in under construction');
1.163 albertel 207: # return ($status,$datemsg);
1.145 albertel 208: }
209:
210: &Apache::lonxml::debug("checking for part :$id:");
211: &Apache::lonxml::debug("time:".time);
1.152 albertel 212:
1.163 albertel 213: if ($ENV{'request.state'} ne "construct") {
214: my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
215: if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
216: $status='INVALID_ACCESS';
217: $date=&mt("can not be accessed from your location.");
1.145 albertel 218: return($status,$date);
219: }
1.163 albertel 220:
221: foreach $temp ("opendate","duedate","answerdate") {
222: $lastdate = $date;
223: $date = &Apache::lonnet::EXT("resource.$id.$temp");
224: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
225: if ($thistype =~ /^(con_lost|no_such_host)/ ||
226: $date =~ /^(con_lost|no_such_host)/) {
227: $status='UNAVAILABLE';
228: $date=&mt("may open later.");
229: return($status,$date);
230: }
231: if ($thistype eq 'date_interval') {
232: if ($temp eq 'opendate') {
233: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
234: }
235: if ($temp eq 'answerdate') {
236: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
237: }
1.145 albertel 238: }
1.163 albertel 239: &Apache::lonxml::debug("found :$date: for :$temp:");
240: if ($date eq '') {
241: $date = &mt("an unknown date"); $passed = 0;
242: } elsif ($date eq 'con_lost') {
243: $date = &mt("an indeterminate date"); $passed = 0;
244: } else {
245: if (time < $date) { $passed = 0; } else { $passed = 1; }
246: $date = localtime $date;
1.145 albertel 247: }
1.163 albertel 248: if (!$passed) { $type=$temp; last; }
1.145 albertel 249: }
1.163 albertel 250: &Apache::lonxml::debug("have :$type:$passed:");
251: if ($passed) {
252: $status='SHOW_ANSWER';
253: $datemsg=$date;
254: } elsif ($type eq 'opendate') {
255: $status='CLOSED';
256: $datemsg = &mt("will open on")." $date";
257: } elsif ($type eq 'duedate') {
258: $status='CAN_ANSWER';
259: $datemsg = &mt("is due at")." $date";
260: } elsif ($type eq 'answerdate') {
261: $status='CLOSED';
262: $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
1.145 albertel 263: }
264: }
265: if ($status eq 'CAN_ANSWER') {
266: #check #tries, and if correct.
267: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
268: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
269: if ( $tries eq '' ) { $tries = '0'; }
1.164 ! albertel 270: if ( $maxtries eq '' &&
! 271: $ENV{'request.state'} ne 'construct') { $maxtries = '2'; }
! 272: if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
1.145 albertel 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: }
1.121 albertel 281: }
1.54 www 282:
1.133 albertel 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: #}
1.54 www 287:
288:
1.145 albertel 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';
1.146 albertel 293: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 294: }
1.106 albertel 295:
1.145 albertel 296: return ($status,$datemsg);
1.20 albertel 297: }
298:
1.41 albertel 299: sub showhash {
1.145 albertel 300: my (%hash) = @_;
301: &showhashsubset(\%hash,'.');
302: return '';
1.79 albertel 303: }
304:
1.106 albertel 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:
1.79 albertel 322: sub showhashsubset {
1.145 albertel 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 '';
1.41 albertel 344: }
345:
346: sub setuppermissions {
1.145 albertel 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'}.
1.127 matthew 353: '/'.$ENV{'request.course.sec'});
1.145 albertel 354: }
355: $Apache::lonhomework::viewgrades = $viewgrades;
356: return ''
1.41 albertel 357: }
358:
359: sub setupheader {
1.120 albertel 360: my $request=$_[0];
361: if ($ENV{'browser.mathml'}) {
1.151 www 362: &Apache::loncommon::content_type($request,'text/xml');
1.120 albertel 363: } else {
1.151 www 364: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 365: }
366: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
367: &Apache::loncommon::no_cache($request);
368: }
369: $request->send_http_header;
370: return OK if $request->header_only;
371: return ''
1.41 albertel 372: }
1.35 albertel 373:
1.47 albertel 374: sub handle_save_or_undo {
1.145 albertel 375: my ($request,$problem,$result) = @_;
376: my $file = &Apache::lonnet::filelocation("",$request->uri);
377: my $filebak =$file.".bak";
378: my $filetmp =$file.".tmp";
379: my $error=0;
1.156 albertel 380:
381: &Apache::lonnet::correct_line_ends($result);
1.52 albertel 382:
1.150 albertel 383: if ($ENV{'form.Undo'} eq &mt('undo')) {
1.145 albertel 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) {
1.146 albertel 389: $request->print("<p><b>".&mt("Undid changes, Switched")." $filebak ".&mt("and")." $file</b></p>");
1.145 albertel 390: } else {
1.146 albertel 391: $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 392: $error=1;
393: }
1.52 albertel 394: } else {
1.145 albertel 395: my $fs=Apache::File->new(">$filebak");
396: if (defined($fs)) {
397: print $fs $$problem;
1.146 albertel 398: $request->print("<b>".&mt("Making Backup to").
399: " $filebak</b><br />");
1.145 albertel 400: } else {
1.146 albertel 401: $request->print("<font color=\"red\" size=\"+1\"><b>".&mt("Unable to make backup")." $filebak</b></font>");
1.145 albertel 402: $error=2;
403: }
404: my $fh=Apache::File->new(">$file");
405: if (defined($fh)) {
406: print $fh $$result;
1.146 albertel 407: $request->print("<b>".&mt("Saving Modifications to").
408: " $file</b><br />");
1.145 albertel 409: } else {
1.146 albertel 410: $request->print("<font color=\"red\" size=\"+1\"><b>".
411: &mt("Unable to write to")." $file</b></font>");
1.145 albertel 412: $error|=4;
413: }
1.52 albertel 414: }
1.145 albertel 415: return $error;
1.64 albertel 416: }
417:
1.101 albertel 418: sub analyze_header {
419: my ($request) = @_;
1.109 albertel 420: my $result.='<html>
1.146 albertel 421: <head><title>'.&mt("Analyzing a problem").'</title></head>
1.109 albertel 422: <body bgcolor="#FFFFFF">
1.101 albertel 423: <form name="lonhomework" method="POST" action="'.
1.145 albertel 424: $ENV{'request.uri'}.'">
1.146 albertel 425: <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
426: <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
1.101 albertel 427: <hr />
1.146 albertel 428: <input type="submit" name="submit" value="'.&mt("View").'" />
1.101 albertel 429: <hr />
1.146 albertel 430: '.&mt('List of possible answers').':
1.101 albertel 431: </form>';
432: $request->print($result);
433: $request->rflush();
434: }
435:
1.109 albertel 436: sub analyze_footer {
437: my ($request) = @_;
438: my $result='</body></html>';
439: $request->print($result);
440: $request->rflush();
441: }
442:
1.74 albertel 443: sub analyze {
1.101 albertel 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);
1.114 albertel 451: my %prog_state=
1.146 albertel 452: &Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
453: &mt('Getting Problem Variants'),
1.114 albertel 454: $ENV{'form.numtoanalyze'});
1.102 albertel 455: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 456: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
1.146 albertel 457: &mt('last problem'));
1.101 albertel 458: my $subresult=&Apache::lonnet::ssi($request->uri,
459: ('grade_target' => 'analyze'),
1.130 albertel 460: ('rndseed' => $i+$rndseed));
1.101 albertel 461: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
462: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 463: my @parts;
464: if (defined(@{ $analyze{'parts'} })) {
465: @parts=@{ $analyze{'parts'} };
466: }
1.101 albertel 467: foreach my $part (@parts) {
468: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 469: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
470: $analyze{$part.'.type'} eq 'stringresponse' ||
471: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.101 albertel 472: push( @{ $overall{$part.'.answer'} },
473: [@{ $analyze{$part.'.answer'} }]);
474: }
475: }
476: }
1.114 albertel 477: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
1.146 albertel 478: &mt('Analyzing Results'));
1.134 albertel 479: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 480: if (defined(@{ $overall{$part.'.answer'} })) {
1.132 albertel 481: my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
1.146 albertel 482: $request->print('<table><tr><td colspan="'.($num_cols+1).'">'.&mt('Part').' '.$part.'</td></tr>');
1.130 albertel 483: my %frequency;
1.109 albertel 484: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
1.132 albertel 485: $frequency{join("\0",@{ $answer })}++;
1.130 albertel 486: }
1.146 albertel 487: $request->print('<tr><td colspan="'.($num_cols).'">'.&mt('Answer').'</td><td>'.&mt('Frequency').'</td></tr>');
1.132 albertel 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)).
1.130 albertel 491: '</td><td>('.$frequency{$answer}.
492: ')</td></tr>');
1.109 albertel 493: }
494: $request->print('</table>');
495: } else {
1.162 albertel 496: $request->print('<p>'.&mt('Response').' '.$part.' '.
1.146 albertel 497: &mt('is not analyzable at this time').'</p>');
1.101 albertel 498: }
499: }
1.130 albertel 500: if (scalar(keys(%allparts)) == 0 ) {
1.162 albertel 501: $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 502: }
1.114 albertel 503: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 504: &analyze_footer($request);
1.101 albertel 505: &Apache::lonhomework::showhash(%overall);
506: return $result;
1.74 albertel 507: }
508:
1.64 albertel 509: sub editxmlmode {
1.145 albertel 510: my ($request,$file) = @_;
511: my $result;
512: my $problem=&Apache::lonnet::getfile($file);
513: if ($problem eq -1) {
1.146 albertel 514: &Apache::lonxml::error("<b> ".&mt('Unable to find').
515: " <i>$file</i></b>");
1.145 albertel 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: }
1.80 albertel 523: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
1.150 albertel 524: if ( $ENV{'form.submit'} eq &mt('Submit Changes and View') ) {
1.145 albertel 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);
1.160 www 530: my $xml_help = '<table><tr><td>'.
531: &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
532: "Problem Editing Help").
533: '</td><td>'.
534: &Apache::loncommon::help_open_faq(5).
535: &Apache::loncommon::help_open_bug('Authoring').'</td></tr></table>';
1.145 albertel 536: if ($cols > 80) { $cols = 80; }
537: if ($cols < 70) { $cols = 70; }
538: if ($rows < 20) { $rows = 20; }
539: $result.='<html><body bgcolor="#FFFFFF">
1.64 albertel 540: <form name="lonhomework" method="POST" action="'.
1.145 albertel 541: $ENV{'request.uri'}.'">
1.146 albertel 542: <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
543: <input type="submit" name="problemmode" value="'.&mt('Discard Edits and View').'" />
544: <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
1.64 albertel 545: <hr />
1.146 albertel 546: <input type="submit" name="submit" value="'.&mt('Submit Changes').'" />
547: <input type="submit" name="submit" value="'.&mt('Submit Changes and View').'" />
548: <input type="submit" name="Undo" value="'.&mt('undo').'" />
1.64 albertel 549: <hr />
1.110 albertel 550: ' . $xml_help . '
1.64 albertel 551: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.145 albertel 552: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 553: </form></body></html>';
1.145 albertel 554: $request->print($result);
555: }
556: return '';
1.47 albertel 557: }
558:
1.41 albertel 559: sub renderpage {
1.145 albertel 560: my ($request,$file) = @_;
1.52 albertel 561:
1.145 albertel 562: my (@targets) = &get_target();
1.161 albertel 563: &Apache::lonhomework::showhashsubset(\%ENV,'form.');
1.145 albertel 564: &Apache::lonxml::debug("Running targets ".join(':',@targets));
565: foreach my $target (@targets) {
566: #my $t0 = [&gettimeofday()];
567: my $problem=&Apache::lonnet::getfile($file);
568: if ($problem eq -1) {
1.146 albertel 569: &Apache::lonxml::error("<b> ".&mt('Unable to find')." <i>$file</i></b>");
1.145 albertel 570: $problem='';
571: }
1.52 albertel 572:
1.145 albertel 573: my %mystyle;
574: my $result = '';
575: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
576: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
577: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
578:
579: &Apache::lonxml::debug("Should be parsing now");
580: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
581: &setup_vars($target),%mystyle);
582: undef($Apache::lonhomework::parsing_a_problem);
583: #$request->print("Result follows:");
584: if ($target eq 'modified') {
585: &handle_save_or_undo($request,\$problem,\$result);
586: } else {
587: if ($target eq 'analyze') {
588: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
589: undef(%Apache::lonhomework::analyze);
590: }
591: #my $td=&tv_interval($t0);
592: #if ( $Apache::lonxml::debug) {
593: #$result =~ s:</body>::;
594: #$result.="<br />Spent $td seconds processing target $target\n</body>";
595: #}
596: $request->print($result);
597: $request->rflush();
598: }
599: #$request->print(":Result ends");
600: #my $td=&tv_interval($t0);
1.52 albertel 601: }
1.41 albertel 602: }
603:
1.42 albertel 604: # with no arg it returns a HTML <option> list of the template titles
605: # with one arg it returns the filename associated with the arg passed
606: sub get_template_list {
1.145 albertel 607: my ($namewanted,$extension) = @_;
608: my $result;
609: my @allnames;
610: &Apache::lonxml::debug("Looking for :$extension:");
611: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
612: my $name=&Apache::lonnet::metadata($file,'title');
613: if ($namewanted && ($name eq $namewanted)) {
614: $result=$file;
615: last;
616: } else {
617: if ($name) { push (@allnames, $name); }
618: }
619: }
620: if (@allnames && !$result) {
1.146 albertel 621: $result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
1.145 albertel 622: join('</option><option>',sort(@allnames)).'</option>';
1.42 albertel 623: }
1.145 albertel 624: return $result;
1.42 albertel 625: }
626:
627: sub newproblem {
1.65 matthew 628: my ($request) = @_;
629: my $extension=$request->uri;
630: $extension=~s:^.*\.([\w]+)$:$1:;
631: &Apache::lonxml::debug("Looking for :$extension:");
1.131 albertel 632: my $templatelist=&get_template_list('',$extension);
1.85 albertel 633: if ($ENV{'form.template'} &&
1.128 albertel 634: $ENV{'form.template'} ne "Select a $extension template") {
1.65 matthew 635: use File::Copy;
636: my $file = &get_template_list($ENV{'form.template'},$extension);
637: my $dest = &Apache::lonnet::filelocation("",$request->uri);
638: copy($file,$dest);
639: &renderpage($request,$dest);
1.131 albertel 640: } elsif($ENV{'form.newfile'} && !$templatelist) {
641: # I don't like hard-coded filenames but for now, this will work.
642: use File::Copy;
643: my $templatefilename =
644: $request->dir_config('lonIncludes').'/templates/blank.problem';
645: &Apache::lonxml::debug("$templatefilename");
646: my $dest = &Apache::lonnet::filelocation("",$request->uri);
647: copy($templatefilename,$dest);
648: &renderpage($request,$dest);
1.85 albertel 649: } else {
1.65 matthew 650: my $url=$request->uri;
1.157 albertel 651: my $shownurl=$url;
652: $shownurl=~s-^/~-/priv/-;
1.65 matthew 653: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 654: my $errormsg;
655: if ($ENV{'form.newfile'}) {
1.146 albertel 656: $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
1.128 albertel 657: }
1.85 albertel 658: my $instructions;
1.159 www 659: my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.146 albertel 660: if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
1.147 albertel 661: my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
1.146 albertel 662: 'resource' => 'resource',
663: 'requested' => 'The requested file',
664: 'not exist' => 'currently does not exist',
665: 'createnew' => 'To create a new',
666: 'click' => 'click on the',
667: 'Create' => 'Create',
668: 'button' => 'button');
1.65 matthew 669: $request->print(<<ENDNEWPROBLEM);
1.159 www 670: $bodytag
1.148 albertel 671: <h1>$lt{'create'} $extension $lt{'resource'}</h1>
1.128 albertel 672: $errormsg
1.157 albertel 673: $lt{'requested'} <tt>$shownurl</tt> $lt{'not exist'}.
1.105 www 674: <p>
1.146 albertel 675: <b>$lt{'createnew'} $extension$instructions $lt{'click'} "$lt{'Create'} $extension" $lt{'button'}.</b>
1.105 www 676: </p>
677: <p><form action="$url" method="POST">
1.42 albertel 678: ENDNEWPROBLEM
1.85 albertel 679: if (defined($templatelist)) {
680: $request->print("<select name=\"template\">$templatelist</select>");
681: }
1.146 albertel 682: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
1.105 www 683: $request->print("</form></p></body>");
1.65 matthew 684: }
685: return '';
1.42 albertel 686: }
687:
688: sub view_or_edit_menu {
1.145 albertel 689: my ($request) = @_;
690: my $url=$request->uri;
1.147 albertel 691: my %lt=&Apache::lonlocal::texthash( 'would' => 'Would you like to',
1.146 albertel 692: 'view' => 'View',
693: 'Edit' => 'edit',
694: 'or' => 'or',
695: 'the problem' => 'the problem');
1.145 albertel 696: $request->print(<<EDITMENU);
1.42 albertel 697: <body bgcolor="#FFFFFF">
698: <form action="$url" method="POST">
1.146 albertel 699: $lt{'would'} <input type="submit" name="problemmode" value="<{'view'}">
700: <{'or'} <input type="submit" name="problemmode" value="<{'Edit'}">
701: <{'the problem'}.
1.42 albertel 702: </form>
703: </body>
704: EDITMENU
705: }
706:
1.41 albertel 707: sub handler {
1.145 albertel 708: #my $t0 = [&gettimeofday()];
709: my $request=$_[0];
710:
711: $Apache::lonxml::debug=$ENV{'user.debug'};
712: if (&setupheader($request)) { return OK; }
713: $ENV{'request.uri'}=$request->uri;
1.41 albertel 714:
1.145 albertel 715: #setup permissions
716: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
717: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
718: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
719: # some times multiple problemmodes are submitted, need to select
720: # the last one
721: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
722: if ( defined($ENV{'form.problemmode'}) &&
723: ref($ENV{'form.problemmode'}) ) {
724: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
725: my $mode=$ENV{'form.problemmode'}->[-1];
726: undef $ENV{'form.problemmode'};
727: $ENV{'form.problemmode'}=$mode;
728: }
729: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
730: my $file=&Apache::lonnet::filelocation("",$request->uri);
731:
732: #check if we know where we are
733: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
734: # if we are browsing we might not be able to know where we are
735: if ($Apache::lonhomework::browse ne 'F') {
736: #should know where we are, so ask
737: if ( &Apache::lonnet::mod_perl_version() == 2 ) {
738: &Apache::lonnet::cleanenv();
739: }
740: $request->internal_redirect('/adm/ambiguous'); return;
741: }
742: }
1.41 albertel 743:
1.145 albertel 744: my ($symb) = &Apache::lonxml::whichuser();
745: &Apache::lonxml::debug('symb is '.$symb);
746: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
1.150 albertel 747: if ($ENV{'form.resetdata'} eq &mt('Reset Submissions') ||
748: $ENV{'form.resetdata'} eq &mt('New Problem Variation') ||
749: $ENV{'form.newrandomization'} eq &mt('New Randomization')) {
1.145 albertel 750: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
751: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
752: &Apache::lonxml::debug("Attempt reset");
753: }
754: }
755: if ($ENV{'request.state'} eq "construct") {
756: if ( -e $file ) {
757: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
758: ['problemmode']);
759: if (!(defined $ENV{'form.problemmode'})) {
760: #first visit to problem in construction space
761: #&view_or_edit_menu($request);
762: $ENV{'form.problemmode'}='View';
763: &renderpage($request,$file);
1.150 albertel 764: } elsif ($ENV{'form.problemmode'} eq &mt('EditXML')) {
1.145 albertel 765: &editxmlmode($request,$file);
1.150 albertel 766: } elsif ($ENV{'form.problemmode'} eq &mt('Calculate answers')) {
1.145 albertel 767: &analyze($request,$file);
768: } else {
769: &renderpage($request,$file);
770: }
771: } else {
772: # requested file doesn't exist in contruction space
773: &newproblem($request);
774: }
775: } else {
776: # just render the page normally outside of construction space
777: &Apache::lonxml::debug("not construct");
1.52 albertel 778: &renderpage($request,$file);
1.41 albertel 779: }
1.145 albertel 780: #my $td=&tv_interval($t0);
781: #&Apache::lonxml::debug("Spent $td seconds processing");
782: # &Apache::lonhomework::send_footer($request);
783: # always turn off debug messages
784: $Apache::lonxml::debug=0;
785: return OK;
1.52 albertel 786:
1.1 albertel 787: }
788:
789: 1;
790: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>