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