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