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