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