Annotation of loncom/homework/lonhomework.pm, revision 1.152
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.152 ! albertel 4: # $Id: lonhomework.pm,v 1.151 2003/09/21 21:40:06 www 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/
27: #
1.52 albertel 28: # Guy Albertelli
1.17 www 29: # 11/30 Gerd Kortemeyer
1.56 www 30: # 6/1,8/17,8/18 Gerd Kortemeyer
1.82 bowersj2 31: # 7/18 Jeremy Bowers
1.1 albertel 32:
33: package Apache::lonhomework;
34: use strict;
1.73 albertel 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();
1.83 albertel 45: use Apache::caparesponse();
46: use Apache::radiobuttonresponse();
47: use Apache::optionresponse();
48: use Apache::imageresponse();
49: use Apache::essayresponse();
50: use Apache::externalresponse();
1.106 albertel 51: use Apache::rankresponse();
1.107 albertel 52: use Apache::matchresponse();
1.137 albertel 53: use Apache::chemresponse();
1.26 www 54: use Apache::Constants qw(:common);
1.73 albertel 55: use HTML::Entities();
1.83 albertel 56: use Apache::loncommon();
1.146 albertel 57: use Apache::lonlocal;
1.47 albertel 58: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 59:
1.69 harris41 60: BEGIN {
1.145 albertel 61: &Apache::lonxml::register_insert();
1.43 albertel 62: }
63:
1.5 albertel 64: sub get_target {
1.145 albertel 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:
1.62 albertel 75: if ( defined($ENV{'form.submitted'}) &&
1.145 albertel 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 {
1.150 albertel 93: if ( $ENV{'form.problemmode'} eq &mt('View') ||
94: $ENV{'form.problemmode'} eq &mt('Discard Edits and View')) {
1.145 albertel 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: }
1.150 albertel 102: } elsif ( $ENV{'form.problemmode'} eq &mt('Edit') ) {
1.145 albertel 103: if ( $ENV{'form.submitted'} eq 'edit' ) {
1.150 albertel 104: if ( $ENV{'form.submit'} eq &mt('Submit Changes and View') ) {
1.145 albertel 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: }
1.15 albertel 116: }
1.145 albertel 117: return ();
1.5 albertel 118: }
119:
1.3 albertel 120: sub setup_vars {
1.145 albertel 121: my ($target) = @_;
122: return ';'
1.11 albertel 123: # return ';$external::target='.$target.';';
1.2 albertel 124: }
125:
126: sub send_header {
1.145 albertel 127: my ($request)= @_;
128: $request->print(&Apache::lontexconvert::header());
1.16 albertel 129: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 130: }
131:
1.36 albertel 132: sub createmenu {
1.145 albertel 133: my ($which,$request)=@_;
134: if ($which eq 'grade') {
135: $request->print('<script language="JavaScript">
1.91 albertel 136: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52 albertel 137: "height=350,width=150,menubar=no");
138: </script>');
1.145 albertel 139: }
1.36 albertel 140: }
141:
1.2 albertel 142: sub send_footer {
1.145 albertel 143: my ($request)= @_;
1.16 albertel 144: # $request->print('</form>');
1.145 albertel 145: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 146: }
147:
1.52 albertel 148: $Apache::lonxml::browse='';
1.53 www 149:
1.152 ! albertel 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: }
1.92 bowersj2 195: # JB, 9/24/2002: Any changes in this function may require a change
196: # in lonnavmaps::resource::getDateStatus.
1.53 www 197: sub check_access {
1.145 albertel 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';
1.146 albertel 210: $datemsg=&mt('is in under construction');
1.145 albertel 211: return ($status,$datemsg);
212: }
213:
214: &Apache::lonxml::debug("checking for part :$id:");
215: &Apache::lonxml::debug("time:".time);
1.152 ! albertel 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:
1.145 albertel 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';
1.146 albertel 232: $date=&mt("may open later.");
1.145 albertel 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 '') {
1.146 albertel 245: $date = &mt("an unknown date"); $passed = 0;
1.145 albertel 246: } elsif ($date eq 'con_lost') {
1.146 albertel 247: $date = &mt("an indeterminate date"); $passed = 0;
1.145 albertel 248: } else {
249: if (time < $date) { $passed = 0; } else { $passed = 1; }
250: $date = localtime $date;
251: }
252: if (!$passed) { $type=$temp; last; }
1.51 harris41 253: }
1.145 albertel 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';
1.146 albertel 260: $datemsg = &mt("will open on")." $date";
1.145 albertel 261: } elsif ($type eq 'duedate') {
262: $status='CAN_ANSWER';
1.146 albertel 263: $datemsg = &mt("is due at")." $date";
1.145 albertel 264: } elsif ($type eq 'answerdate') {
265: $status='CLOSED';
1.146 albertel 266: $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
1.145 albertel 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: }
1.121 albertel 283: }
1.54 www 284:
1.133 albertel 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: #}
1.54 www 289:
290:
1.145 albertel 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';
1.146 albertel 295: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 296: }
1.106 albertel 297:
1.145 albertel 298: return ($status,$datemsg);
1.20 albertel 299: }
300:
1.41 albertel 301: sub showhash {
1.145 albertel 302: my (%hash) = @_;
303: &showhashsubset(\%hash,'.');
304: return '';
1.79 albertel 305: }
306:
1.106 albertel 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:
1.79 albertel 324: sub showhashsubset {
1.145 albertel 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 '';
1.41 albertel 346: }
347:
348: sub setuppermissions {
1.145 albertel 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'}.
1.127 matthew 355: '/'.$ENV{'request.course.sec'});
1.145 albertel 356: }
357: $Apache::lonhomework::viewgrades = $viewgrades;
358: return ''
1.41 albertel 359: }
360:
361: sub setupheader {
1.120 albertel 362: my $request=$_[0];
363: if ($ENV{'browser.mathml'}) {
1.151 www 364: &Apache::loncommon::content_type($request,'text/xml');
1.120 albertel 365: } else {
1.151 www 366: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 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 ''
1.41 albertel 374: }
1.35 albertel 375:
1.47 albertel 376: sub handle_save_or_undo {
1.145 albertel 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;
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.146 albertel 496: $request->print('<p>'.&mt('Part').' '.$part.' '.
497: &mt('is not analyzable at this time').'</p>');
1.101 albertel 498: }
499: }
1.130 albertel 500: if (scalar(keys(%allparts)) == 0 ) {
1.149 www 501: $request->print('<p>'.&mt('Found no analyzable parts 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);
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">
1.64 albertel 536: <form name="lonhomework" method="POST" action="'.
1.145 albertel 537: $ENV{'request.uri'}.'">
1.146 albertel 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').'" />
1.64 albertel 541: <hr />
1.146 albertel 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').'" />
1.64 albertel 545: <hr />
1.110 albertel 546: ' . $xml_help . '
1.64 albertel 547: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.145 albertel 548: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 549: </form></body></html>';
1.145 albertel 550: $request->print($result);
551: }
552: return '';
1.47 albertel 553: }
554:
1.41 albertel 555: sub renderpage {
1.145 albertel 556: my ($request,$file) = @_;
1.52 albertel 557:
1.145 albertel 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) {
1.146 albertel 564: &Apache::lonxml::error("<b> ".&mt('Unable to find')." <i>$file</i></b>");
1.145 albertel 565: $problem='';
566: }
1.52 albertel 567:
1.145 albertel 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);
1.52 albertel 596: }
1.41 albertel 597: }
598:
1.42 albertel 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 {
1.145 albertel 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) {
1.146 albertel 616: $result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
1.145 albertel 617: join('</option><option>',sort(@allnames)).'</option>';
1.42 albertel 618: }
1.145 albertel 619: return $result;
1.42 albertel 620: }
621:
622: sub newproblem {
1.65 matthew 623: my ($request) = @_;
624: my $extension=$request->uri;
625: $extension=~s:^.*\.([\w]+)$:$1:;
626: &Apache::lonxml::debug("Looking for :$extension:");
1.131 albertel 627: my $templatelist=&get_template_list('',$extension);
1.85 albertel 628: if ($ENV{'form.template'} &&
1.128 albertel 629: $ENV{'form.template'} ne "Select a $extension template") {
1.65 matthew 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);
1.131 albertel 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);
1.85 albertel 644: } else {
1.65 matthew 645: my $url=$request->uri;
646: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 647: my $errormsg;
648: if ($ENV{'form.newfile'}) {
1.146 albertel 649: $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
1.128 albertel 650: }
1.85 albertel 651: my $instructions;
1.146 albertel 652: if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
1.147 albertel 653: my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
1.146 albertel 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');
1.65 matthew 661: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 662: <body bgcolor="#FFFFFF">
1.148 albertel 663: <h1>$lt{'create'} $extension $lt{'resource'}</h1>
1.128 albertel 664: $errormsg
1.146 albertel 665: $lt{'requested'} <tt>$url</tt> $lt{'not exist'}.
1.105 www 666: <p>
1.146 albertel 667: <b>$lt{'createnew'} $extension$instructions $lt{'click'} "$lt{'Create'} $extension" $lt{'button'}.</b>
1.105 www 668: </p>
669: <p><form action="$url" method="POST">
1.42 albertel 670: ENDNEWPROBLEM
1.85 albertel 671: if (defined($templatelist)) {
672: $request->print("<select name=\"template\">$templatelist</select>");
673: }
1.146 albertel 674: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
1.105 www 675: $request->print("</form></p></body>");
1.65 matthew 676: }
677: return '';
1.42 albertel 678: }
679:
680: sub view_or_edit_menu {
1.145 albertel 681: my ($request) = @_;
682: my $url=$request->uri;
1.147 albertel 683: my %lt=&Apache::lonlocal::texthash( 'would' => 'Would you like to',
1.146 albertel 684: 'view' => 'View',
685: 'Edit' => 'edit',
686: 'or' => 'or',
687: 'the problem' => 'the problem');
1.145 albertel 688: $request->print(<<EDITMENU);
1.42 albertel 689: <body bgcolor="#FFFFFF">
690: <form action="$url" method="POST">
1.146 albertel 691: $lt{'would'} <input type="submit" name="problemmode" value="<{'view'}">
692: <{'or'} <input type="submit" name="problemmode" value="<{'Edit'}">
693: <{'the problem'}.
1.42 albertel 694: </form>
695: </body>
696: EDITMENU
697: }
698:
1.41 albertel 699: sub handler {
1.145 albertel 700: #my $t0 = [&gettimeofday()];
701: my $request=$_[0];
702:
703: $Apache::lonxml::debug=$ENV{'user.debug'};
1.41 albertel 704:
1.145 albertel 705: if (&setupheader($request)) { return OK; }
706: $ENV{'request.uri'}=$request->uri;
1.41 albertel 707:
1.145 albertel 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: }
1.41 albertel 736:
1.145 albertel 737: my ($symb) = &Apache::lonxml::whichuser();
738: &Apache::lonxml::debug('symb is '.$symb);
739: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
1.150 albertel 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')) {
1.145 albertel 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);
1.150 albertel 757: } elsif ($ENV{'form.problemmode'} eq &mt('EditXML')) {
1.145 albertel 758: &editxmlmode($request,$file);
1.150 albertel 759: } elsif ($ENV{'form.problemmode'} eq &mt('Calculate answers')) {
1.145 albertel 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");
1.52 albertel 771: &renderpage($request,$file);
1.41 albertel 772: }
1.145 albertel 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;
1.52 albertel 779:
1.1 albertel 780: }
781:
782: 1;
783: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>