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