Annotation of loncom/homework/lonhomework.pm, revision 1.182
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.182 ! albertel 4: # $Id: lonhomework.pm,v 1.181 2004/09/29 06:51:25 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.179 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.181 albertel 295: if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.177 albertel 296: my $interval=&Apache::lonnet::EXT("resource.$id.interval");
297: &Apache::lonxml::debug("looking for interval $interval");
298: if ($interval) {
299: my $first_access=&Apache::lonnet::get_first_access('map');
300: &Apache::lonxml::debug("looking for accesstime $first_access");
301: if (!$first_access) {
302: $status='NOT_YET_VIEWED';
303: $datemsg=' '.$interval.' seconds';
304: } else {
305: my $newdate=localtime($first_access+$interval);
306: if (time > ($first_access+$interval)) {
307: $status='CLOSED';
308: $datemsg = &mt("was due on")." $newdate".&mt(", and answers will be available on")." $date";
309: } else {
310: $datemsg = &mt("is due at")." $newdate";
311: }
312: }
313: }
314: }
1.133 albertel 315: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
316: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
317: # return ('UNCHECKEDOUT','needs to be checked out');
318: #}
1.54 www 319:
320:
1.145 albertel 321: &Apache::lonxml::debug("sending back :$status:$datemsg:");
322: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
323: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
324: $status='CAN_ANSWER';
1.146 albertel 325: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 326: }
1.106 albertel 327:
1.145 albertel 328: return ($status,$datemsg);
1.20 albertel 329: }
330:
1.41 albertel 331: sub showhash {
1.145 albertel 332: my (%hash) = @_;
333: &showhashsubset(\%hash,'.');
334: return '';
1.79 albertel 335: }
336:
1.106 albertel 337: sub showarray {
338: my ($array)=@_;
339: my $string="(";
340: foreach my $elm (@{ $array }) {
341: if (ref($elm)) {
342: if ($elm =~ /ARRAY/ ) {
343: $string.=&showarray($elm);
344: }
345: } else {
346: $string.="$elm,"
347: }
348: }
349: chop($string);
350: $string.=")";
351: return $string;
352: }
353:
1.79 albertel 354: sub showhashsubset {
1.145 albertel 355: my ($hash,$keyre) = @_;
356: my $resultkey;
357: foreach $resultkey (sort keys %$hash) {
358: if ($resultkey =~ /$keyre/) {
359: if (ref($$hash{$resultkey})) {
360: if ($$hash{$resultkey} =~ /ARRAY/ ) {
361: &Apache::lonxml::debug("$resultkey ---- ".
362: &showarray($$hash{$resultkey}));
363: } elsif ($$hash{$resultkey} =~ /HASH/ ) {
364: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
365: &showhashsubset($$hash{$resultkey},'.');
366: } else {
367: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
368: }
369: } else {
370: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
371: }
372: }
373: }
374: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
375: return '';
1.41 albertel 376: }
377:
378: sub setuppermissions {
1.145 albertel 379: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
380: my $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
381: if (! $viewgrades &&
382: exists($ENV{'request.course.sec'}) &&
383: $ENV{'request.course.sec'} !~ /^\s*$/) {
384: $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}.
1.127 matthew 385: '/'.$ENV{'request.course.sec'});
1.145 albertel 386: }
387: $Apache::lonhomework::viewgrades = $viewgrades;
388: return ''
1.41 albertel 389: }
390:
391: sub setupheader {
1.120 albertel 392: my $request=$_[0];
393: if ($ENV{'browser.mathml'}) {
1.151 www 394: &Apache::loncommon::content_type($request,'text/xml');
1.120 albertel 395: } else {
1.151 www 396: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 397: }
398: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
399: &Apache::loncommon::no_cache($request);
400: }
401: $request->send_http_header;
402: return OK if $request->header_only;
403: return ''
1.41 albertel 404: }
1.35 albertel 405:
1.47 albertel 406: sub handle_save_or_undo {
1.145 albertel 407: my ($request,$problem,$result) = @_;
408: my $file = &Apache::lonnet::filelocation("",$request->uri);
409: my $filebak =$file.".bak";
410: my $filetmp =$file.".tmp";
411: my $error=0;
1.156 albertel 412:
413: &Apache::lonnet::correct_line_ends($result);
1.52 albertel 414:
1.150 albertel 415: if ($ENV{'form.Undo'} eq &mt('undo')) {
1.145 albertel 416: my $error=0;
417: if (!copy($file,$filetmp)) { $error=1; }
418: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
419: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
420: if (!$error) {
1.171 albertel 421: &Apache::lonxml::info("<p><b>".&mt("Undid changes, Switched")." $filebak ".&mt("and")." $file</b></p>");
1.145 albertel 422: } else {
1.171 albertel 423: &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 424: $error=1;
425: }
1.52 albertel 426: } else {
1.145 albertel 427: my $fs=Apache::File->new(">$filebak");
428: if (defined($fs)) {
429: print $fs $$problem;
1.171 albertel 430: &Apache::lonxml::info("<b>".&mt("Making Backup to").
431: " $filebak</b>");
1.145 albertel 432: } else {
1.171 albertel 433: &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".&mt("Unable to make backup")." $filebak</b></font>");
1.145 albertel 434: $error=2;
435: }
436: my $fh=Apache::File->new(">$file");
437: if (defined($fh)) {
438: print $fh $$result;
1.171 albertel 439: &Apache::lonxml::info("<b>".&mt("Saving Modifications to").
440: " $file</b>");
1.145 albertel 441: } else {
1.171 albertel 442: &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".
443: &mt("Unable to write to")." $file</b></font>");
1.145 albertel 444: $error|=4;
445: }
1.52 albertel 446: }
1.145 albertel 447: return $error;
1.64 albertel 448: }
449:
1.101 albertel 450: sub analyze_header {
451: my ($request) = @_;
1.109 albertel 452: my $result.='<html>
1.146 albertel 453: <head><title>'.&mt("Analyzing a problem").'</title></head>
1.171 albertel 454: <body bgcolor="#FFFFFF">'.&Apache::lonxml::message_location().'
1.101 albertel 455: <form name="lonhomework" method="POST" action="'.
1.179 albertel 456: &HTML::Entities::encode($ENV{'request.uri'},'<>&"').'">'.
457: &Apache::structuretags::remember_problem_state().'
1.146 albertel 458: <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
459: <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
1.101 albertel 460: <hr />
1.146 albertel 461: <input type="submit" name="submit" value="'.&mt("View").'" />
1.101 albertel 462: <hr />
463: </form>';
1.171 albertel 464: &Apache::lonxml::add_messages(\$result);
1.101 albertel 465: $request->print($result);
466: $request->rflush();
467: }
468:
1.109 albertel 469: sub analyze_footer {
470: my ($request) = @_;
471: my $result='</body></html>';
472: $request->print($result);
473: $request->rflush();
474: }
475:
1.74 albertel 476: sub analyze {
1.101 albertel 477: my ($request,$file) = @_;
478: &Apache::lonxml::debug("Analyze");
479: my $result;
480: my %overall;
481: my %allparts;
482: my $rndseed=$ENV{'form.rndseed'};
483: &analyze_header($request);
1.114 albertel 484: my %prog_state=
1.146 albertel 485: &Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
486: &mt('Getting Problem Variants'),
1.175 albertel 487: $ENV{'form.numtoanalyze'},
488: 'inline',undef);
1.102 albertel 489: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 490: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
1.146 albertel 491: &mt('last problem'));
1.182 ! albertel 492: if (&Apache::loncommon::connection_aborted($request)) { return; }
1.101 albertel 493: my $subresult=&Apache::lonnet::ssi($request->uri,
494: ('grade_target' => 'analyze'),
1.130 albertel 495: ('rndseed' => $i+$rndseed));
1.101 albertel 496: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
497: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 498: my @parts;
499: if (defined(@{ $analyze{'parts'} })) {
500: @parts=@{ $analyze{'parts'} };
501: }
1.101 albertel 502: foreach my $part (@parts) {
503: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 504: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
505: $analyze{$part.'.type'} eq 'stringresponse' ||
506: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.101 albertel 507: push( @{ $overall{$part.'.answer'} },
508: [@{ $analyze{$part.'.answer'} }]);
509: }
510: }
511: }
1.114 albertel 512: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
1.146 albertel 513: &mt('Analyzing Results'));
1.175 albertel 514: $request->print('<hr />'.&mt('List of possible answers').': ');
1.134 albertel 515: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 516: if (defined(@{ $overall{$part.'.answer'} })) {
1.132 albertel 517: my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
1.146 albertel 518: $request->print('<table><tr><td colspan="'.($num_cols+1).'">'.&mt('Part').' '.$part.'</td></tr>');
1.130 albertel 519: my %frequency;
1.109 albertel 520: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
1.132 albertel 521: $frequency{join("\0",@{ $answer })}++;
1.130 albertel 522: }
1.146 albertel 523: $request->print('<tr><td colspan="'.($num_cols).'">'.&mt('Answer').'</td><td>'.&mt('Frequency').'</td></tr>');
1.132 albertel 524: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
525: $request->print('<tr><td align="right">'.
526: join('</td><td align="right">',split("\0",$answer)).
1.130 albertel 527: '</td><td>('.$frequency{$answer}.
528: ')</td></tr>');
1.109 albertel 529: }
530: $request->print('</table>');
531: } else {
1.162 albertel 532: $request->print('<p>'.&mt('Response').' '.$part.' '.
1.146 albertel 533: &mt('is not analyzable at this time').'</p>');
1.101 albertel 534: }
535: }
1.130 albertel 536: if (scalar(keys(%allparts)) == 0 ) {
1.162 albertel 537: $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 538: }
1.114 albertel 539: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 540: &analyze_footer($request);
1.101 albertel 541: &Apache::lonhomework::showhash(%overall);
542: return $result;
1.74 albertel 543: }
544:
1.64 albertel 545: sub editxmlmode {
1.145 albertel 546: my ($request,$file) = @_;
547: my $result;
548: my $problem=&Apache::lonnet::getfile($file);
549: if ($problem eq -1) {
1.146 albertel 550: &Apache::lonxml::error("<b> ".&mt('Unable to find').
551: " <i>$file</i></b>");
1.145 albertel 552: $problem='';
553: }
554: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
555: my $error=&handle_save_or_undo($request,\$problem,
556: \$ENV{'form.editxmltext'});
557: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
558: }
1.80 albertel 559: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
1.150 albertel 560: if ( $ENV{'form.submit'} eq &mt('Submit Changes and View') ) {
1.145 albertel 561: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
562: $ENV{'form.problemmode'}='View';
563: &renderpage($request,$file);
564: } else {
565: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.160 www 566: my $xml_help = '<table><tr><td>'.
567: &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
568: "Problem Editing Help").
569: '</td><td>'.
1.178 albertel 570: &Apache::loncommon::help_open_menu('',undef,undef,undef,5,'Authoring').
571: '</td></tr></table>';
1.145 albertel 572: if ($cols > 80) { $cols = 80; }
573: if ($cols < 70) { $cols = 70; }
574: if ($rows < 20) { $rows = 20; }
1.171 albertel 575: $result.='<html><body bgcolor="#FFFFFF">'.
576: &Apache::lonxml::message_location().'
1.64 albertel 577: <form name="lonhomework" method="POST" action="'.
1.179 albertel 578: &HTML::Entities::encode($ENV{'request.uri'},'<>&"').'">'.
579: &Apache::structuretags::remember_problem_state().'
1.146 albertel 580: <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
1.170 matthew 581: <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
582: <input type="submit" name="problemmode" accesskey="e" value="'.&mt('Edit').'" />
1.64 albertel 583: <hr />
1.170 matthew 584: <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes').'" />
585: <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" />
586: <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
1.64 albertel 587: <hr />
1.110 albertel 588: ' . $xml_help . '
1.64 albertel 589: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.172 albertel 590: &HTML::Entities::encode($problem,'<>&"').'</textarea>
1.64 albertel 591: </form></body></html>';
1.171 albertel 592: &Apache::lonxml::add_messages(\$result);
1.145 albertel 593: $request->print($result);
594: }
595: return '';
1.47 albertel 596: }
597:
1.41 albertel 598: sub renderpage {
1.145 albertel 599: my ($request,$file) = @_;
1.52 albertel 600:
1.145 albertel 601: my (@targets) = &get_target();
1.161 albertel 602: &Apache::lonhomework::showhashsubset(\%ENV,'form.');
1.145 albertel 603: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.171 albertel 604: my $overall_result;
1.145 albertel 605: foreach my $target (@targets) {
606: #my $t0 = [&gettimeofday()];
607: my $problem=&Apache::lonnet::getfile($file);
608: if ($problem eq -1) {
1.146 albertel 609: &Apache::lonxml::error("<b> ".&mt('Unable to find')." <i>$file</i></b>");
1.145 albertel 610: $problem='';
611: }
1.52 albertel 612:
1.145 albertel 613: my %mystyle;
614: my $result = '';
615: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
616: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
617: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
618:
619: &Apache::lonxml::debug("Should be parsing now");
620: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
621: &setup_vars($target),%mystyle);
622: undef($Apache::lonhomework::parsing_a_problem);
623: #$request->print("Result follows:");
624: if ($target eq 'modified') {
625: &handle_save_or_undo($request,\$problem,\$result);
626: } else {
627: if ($target eq 'analyze') {
628: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
629: undef(%Apache::lonhomework::analyze);
630: }
631: #my $td=&tv_interval($t0);
632: #if ( $Apache::lonxml::debug) {
633: #$result =~ s:</body>::;
634: #$result.="<br />Spent $td seconds processing target $target\n</body>";
635: #}
1.171 albertel 636: # $request->print($result);
637: $overall_result.=$result;
638: # $request->rflush();
1.145 albertel 639: }
640: #$request->print(":Result ends");
641: #my $td=&tv_interval($t0);
1.52 albertel 642: }
1.171 albertel 643: &Apache::lonxml::add_messages(\$overall_result);
644: $request->print($overall_result);
645: $request->rflush();
1.41 albertel 646: }
647:
1.42 albertel 648: # with no arg it returns a HTML <option> list of the template titles
649: # with one arg it returns the filename associated with the arg passed
650: sub get_template_list {
1.145 albertel 651: my ($namewanted,$extension) = @_;
652: my $result;
653: my @allnames;
654: &Apache::lonxml::debug("Looking for :$extension:");
655: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
656: my $name=&Apache::lonnet::metadata($file,'title');
657: if ($namewanted && ($name eq $namewanted)) {
658: $result=$file;
659: last;
660: } else {
661: if ($name) { push (@allnames, $name); }
662: }
663: }
664: if (@allnames && !$result) {
1.146 albertel 665: $result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
1.145 albertel 666: join('</option><option>',sort(@allnames)).'</option>';
1.42 albertel 667: }
1.145 albertel 668: return $result;
1.42 albertel 669: }
670:
671: sub newproblem {
1.65 matthew 672: my ($request) = @_;
673: my $extension=$request->uri;
674: $extension=~s:^.*\.([\w]+)$:$1:;
675: &Apache::lonxml::debug("Looking for :$extension:");
1.131 albertel 676: my $templatelist=&get_template_list('',$extension);
1.85 albertel 677: if ($ENV{'form.template'} &&
1.128 albertel 678: $ENV{'form.template'} ne "Select a $extension template") {
1.65 matthew 679: use File::Copy;
680: my $file = &get_template_list($ENV{'form.template'},$extension);
681: my $dest = &Apache::lonnet::filelocation("",$request->uri);
682: copy($file,$dest);
683: &renderpage($request,$dest);
1.131 albertel 684: } elsif($ENV{'form.newfile'} && !$templatelist) {
685: # I don't like hard-coded filenames but for now, this will work.
686: use File::Copy;
687: my $templatefilename =
688: $request->dir_config('lonIncludes').'/templates/blank.problem';
689: &Apache::lonxml::debug("$templatefilename");
690: my $dest = &Apache::lonnet::filelocation("",$request->uri);
691: copy($templatefilename,$dest);
692: &renderpage($request,$dest);
1.85 albertel 693: } else {
1.176 albertel 694: my $url=&HTML::Entities::encode($request->uri,'<>&"');
695: my $shownurl=$url;
1.157 albertel 696: $shownurl=~s-^/~-/priv/-;
1.65 matthew 697: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 698: my $errormsg;
699: if ($ENV{'form.newfile'}) {
1.146 albertel 700: $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
1.128 albertel 701: }
1.85 albertel 702: my $instructions;
1.159 www 703: my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.146 albertel 704: if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
1.147 albertel 705: my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
1.146 albertel 706: 'resource' => 'resource',
707: 'requested' => 'The requested file',
708: 'not exist' => 'currently does not exist',
709: 'createnew' => 'To create a new',
710: 'click' => 'click on the',
711: 'Create' => 'Create',
712: 'button' => 'button');
1.65 matthew 713: $request->print(<<ENDNEWPROBLEM);
1.159 www 714: $bodytag
1.148 albertel 715: <h1>$lt{'create'} $extension $lt{'resource'}</h1>
1.128 albertel 716: $errormsg
1.157 albertel 717: $lt{'requested'} <tt>$shownurl</tt> $lt{'not exist'}.
1.105 www 718: <p>
1.146 albertel 719: <b>$lt{'createnew'} $extension$instructions $lt{'click'} "$lt{'Create'} $extension" $lt{'button'}.</b>
1.105 www 720: </p>
721: <p><form action="$url" method="POST">
1.42 albertel 722: ENDNEWPROBLEM
1.85 albertel 723: if (defined($templatelist)) {
724: $request->print("<select name=\"template\">$templatelist</select>");
725: }
1.146 albertel 726: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
1.105 www 727: $request->print("</form></p></body>");
1.65 matthew 728: }
729: return '';
1.42 albertel 730: }
731:
732: sub view_or_edit_menu {
1.145 albertel 733: my ($request) = @_;
1.176 albertel 734: my $url=&HTML::Entities::encode($request->uri,'<>&"');
1.147 albertel 735: my %lt=&Apache::lonlocal::texthash( 'would' => 'Would you like to',
1.146 albertel 736: 'view' => 'View',
737: 'Edit' => 'edit',
738: 'or' => 'or',
739: 'the problem' => 'the problem');
1.145 albertel 740: $request->print(<<EDITMENU);
1.42 albertel 741: <body bgcolor="#FFFFFF">
742: <form action="$url" method="POST">
1.170 matthew 743: $lt{'would'} <input type="submit" name="problemmode" accesskey="v" value="<{'view'}">
744: <{'or'} <input type="submit" name="problemmode" accesskey="e" value="<{'Edit'}">
1.146 albertel 745: <{'the problem'}.
1.42 albertel 746: </form>
747: </body>
748: EDITMENU
749: }
750:
1.41 albertel 751: sub handler {
1.145 albertel 752: #my $t0 = [&gettimeofday()];
753: my $request=$_[0];
754:
755: $Apache::lonxml::debug=$ENV{'user.debug'};
756: if (&setupheader($request)) { return OK; }
757: $ENV{'request.uri'}=$request->uri;
1.41 albertel 758:
1.180 albertel 759: &setuppermissions();
1.145 albertel 760: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
761: # some times multiple problemmodes are submitted, need to select
762: # the last one
763: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
764: if ( defined($ENV{'form.problemmode'}) &&
765: ref($ENV{'form.problemmode'}) ) {
766: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
767: my $mode=$ENV{'form.problemmode'}->[-1];
768: undef $ENV{'form.problemmode'};
769: $ENV{'form.problemmode'}=$mode;
770: }
771: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
772: my $file=&Apache::lonnet::filelocation("",$request->uri);
773:
774: #check if we know where we are
775: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
776: # if we are browsing we might not be able to know where we are
1.173 albertel 777: if ($Apache::lonhomework::browse ne 'F' &&
778: $ENV{'request.state'} ne "construct") {
1.145 albertel 779: #should know where we are, so ask
780: if ( &Apache::lonnet::mod_perl_version() == 2 ) {
781: &Apache::lonnet::cleanenv();
782: }
783: $request->internal_redirect('/adm/ambiguous'); return;
784: }
785: }
1.41 albertel 786:
1.145 albertel 787: my ($symb) = &Apache::lonxml::whichuser();
788: &Apache::lonxml::debug('symb is '.$symb);
789: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
1.150 albertel 790: if ($ENV{'form.resetdata'} eq &mt('Reset Submissions') ||
791: $ENV{'form.resetdata'} eq &mt('New Problem Variation') ||
792: $ENV{'form.newrandomization'} eq &mt('New Randomization')) {
1.145 albertel 793: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
794: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
795: &Apache::lonxml::debug("Attempt reset");
796: }
797: }
798: if ($ENV{'request.state'} eq "construct") {
799: if ( -e $file ) {
800: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
801: ['problemmode']);
802: if (!(defined $ENV{'form.problemmode'})) {
803: #first visit to problem in construction space
804: #&view_or_edit_menu($request);
805: $ENV{'form.problemmode'}='View';
806: &renderpage($request,$file);
1.150 albertel 807: } elsif ($ENV{'form.problemmode'} eq &mt('EditXML')) {
1.145 albertel 808: &editxmlmode($request,$file);
1.150 albertel 809: } elsif ($ENV{'form.problemmode'} eq &mt('Calculate answers')) {
1.145 albertel 810: &analyze($request,$file);
811: } else {
812: &renderpage($request,$file);
813: }
814: } else {
815: # requested file doesn't exist in contruction space
816: &newproblem($request);
817: }
818: } else {
819: # just render the page normally outside of construction space
820: &Apache::lonxml::debug("not construct");
1.52 albertel 821: &renderpage($request,$file);
1.41 albertel 822: }
1.145 albertel 823: #my $td=&tv_interval($t0);
824: #&Apache::lonxml::debug("Spent $td seconds processing");
825: # &Apache::lonhomework::send_footer($request);
826: # always turn off debug messages
827: $Apache::lonxml::debug=0;
828: return OK;
1.52 albertel 829:
1.1 albertel 830: }
831:
832: 1;
833: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>