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