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