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