Annotation of loncom/homework/lonhomework.pm, revision 1.288
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.288 ! raeburn 4: # $Id: lonhomework.pm,v 1.287 2007/12/24 06:35:18 raeburn 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.83 albertel 52: use Apache::loncommon();
1.146 albertel 53: use Apache::lonlocal;
1.179 albertel 54: use Time::HiRes qw( gettimeofday tv_interval );
1.282 albertel 55: use HTML::Entities();
56: use File::Copy();
1.188 foxr 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:
1.276 foxr 66: =pod
67:
68: =item set_bubble_lines()
69:
70: Called at analysis time to set the bubble lines
71: hash for the problem.. This should be called in the
72: end_problemtype tag in analysis mode.
73:
74: We fetch the hash of part id counters from lonxml
75: and push them into analyze:{part_id.bubble_lines}.
76:
77: =cut
78:
79: sub set_bubble_lines {
80: my %bubble_counters = &Apache::lonxml::get_bubble_line_hash();
81:
82: foreach my $key (keys(%bubble_counters)) {
83: $Apache::lonhomework::analyze{"$key.bubble_lines"} =
84: $bubble_counters{"$key"};
85: }
86: }
87:
1.188 foxr 88: #
1.189 albertel 89: # Decides what targets to render for.
1.188 foxr 90: # Implicit inputs:
91: # Various session environment variables:
1.189 albertel 92: # request.state - published - is a /res/ resource
93: # uploaded - is a /uploaded/ resource
94: # contruct - is a /priv/ resource
95: # form.grade_target - a form parameter requesting a specific target
1.5 albertel 96: sub get_target {
1.204 albertel 97: &Apache::lonxml::debug("request.state = $env{'request.state'}");
98: if( defined($env{'form.grade_target'})) {
99: &Apache::lonxml::debug("form.grade_target= $env{'form.grade_target'}");
1.188 foxr 100: } else {
1.189 albertel 101: &Apache::lonxml::debug("form.grade_target <undefined>");
1.188 foxr 102: }
1.204 albertel 103: if (($env{'request.state'} eq "published") ||
104: ($env{'request.state'} eq "uploaded")) {
105: if ( defined($env{'form.grade_target'} )
106: && ($env{'form.grade_target'} eq 'tex')) {
107: return ($env{'form.grade_target'});
108: } elsif ( defined($env{'form.grade_target'} )
1.145 albertel 109: && ($Apache::lonhomework::viewgrades eq 'F' )) {
1.207 albertel 110: return ($env{'form.grade_target'});
1.244 albertel 111: } elsif ( $env{'form.grade_target'} eq 'webgrade'
112: && ($Apache::lonhomework::queuegrade eq 'F' )) {
113: return ($env{'form.grade_target'});
1.207 albertel 114: }
115: if ($env{'form.webgrade'} &&
1.244 albertel 116: ($Apache::lonhomework::modifygrades eq 'F'
117: || $Apache::lonhomework::queuegrade eq 'F' )) {
1.207 albertel 118: return ('grade','webgrade');
1.145 albertel 119: }
1.204 albertel 120: if ( defined($env{'form.submitted'}) &&
121: ( !defined($env{'form.newrandomization'}))) {
1.217 albertel 122: return ('grade', 'web');
1.145 albertel 123: } else {
1.217 albertel 124: return ('web');
1.145 albertel 125: }
1.204 albertel 126: } elsif ($env{'request.state'} eq "construct") {
127: if ( defined($env{'form.grade_target'}) ) {
128: return ($env{'form.grade_target'});
1.145 albertel 129: }
1.204 albertel 130: if ( defined($env{'form.preview'})) {
131: if ( defined($env{'form.submitted'})) {
1.145 albertel 132: return ('grade', 'web');
133: } else {
134: return ('web');
135: }
136: } else {
1.267 albertel 137: if ($env{'form.problemstate'} eq 'WEB_GRADE') {
138: #$env{'form.webgrade'} = 'yes';
139: return ('grade','webgrade','answer');
140: } elsif ( $env{'form.problemmode'} eq &mt('View') ||
1.204 albertel 141: $env{'form.problemmode'} eq &mt('Discard Edits and View')) {
142: if ( defined($env{'form.submitted'}) &&
143: (!defined($env{'form.resetdata'})) &&
144: (!defined($env{'form.newrandomization'}))) {
1.145 albertel 145: return ('grade', 'web','answer');
146: } else {
147: return ('web','answer');
148: }
1.204 albertel 149: } elsif ( $env{'form.problemmode'} eq &mt('Edit') ||
150: $env{'form.problemmode'} eq 'Edit') {
151: if ( $env{'form.submitted'} eq 'edit' ) {
1.281 albertel 152: if ( $env{'form.submit'} eq &mt('Save and View') ) {
1.145 albertel 153: return ('modified','web','answer');
154: } else {
1.211 albertel 155: return ('modified','no_output_web','edit');
1.145 albertel 156: }
157: } else {
1.211 albertel 158: return ('no_output_web','edit');
1.145 albertel 159: }
160: } else {
161: return ('web');
162: }
163: }
1.15 albertel 164: }
1.145 albertel 165: return ();
1.5 albertel 166: }
167:
1.3 albertel 168: sub setup_vars {
1.145 albertel 169: my ($target) = @_;
170: return ';'
1.11 albertel 171: # return ';$external::target='.$target.';';
1.2 albertel 172: }
173:
1.36 albertel 174: sub createmenu {
1.145 albertel 175: my ($which,$request)=@_;
176: if ($which eq 'grade') {
177: $request->print('<script language="JavaScript">
1.91 albertel 178: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52 albertel 179: "height=350,width=150,menubar=no");
180: </script>');
1.145 albertel 181: }
1.36 albertel 182: }
183:
1.200 albertel 184: sub proctor_checked_in {
1.226 albertel 185: my ($slot_name,$slot,$type)=@_;
186: my @possible_proctors=split(",",$slot->{'proctor'});
187:
1.248 albertel 188: return 1 if (!@possible_proctors);
189:
1.226 albertel 190: my $key;
191: if ($type eq 'Task') {
1.230 albertel 192: my $version=$Apache::lonhomework::history{'resource.0.version'};
193: $key ="resource.$version.0.checkedin";
1.226 albertel 194: } elsif ($type eq 'problem') {
195: $key ='resource.0.checkedin';
196: }
1.249 albertel 197: # backward compatability, used to be username@domain,
198: # now is username:domain
199: my $who = $Apache::lonhomework::history{$key};
200: if ($who !~ /:/) {
201: $who =~ tr/@/:/;
202: }
1.226 albertel 203: foreach my $possible (@possible_proctors) {
1.249 albertel 204: if ($who eq $possible
1.226 albertel 205: && $Apache::lonhomework::history{$key.'.slot'} eq $slot_name) {
1.202 albertel 206: return 1;
207: }
208: }
1.226 albertel 209:
1.200 albertel 210: return 0;
211: }
212:
1.152 albertel 213: sub check_ip_acc {
214: my ($acc)=@_;
1.218 albertel 215: &Apache::lonxml::debug("acc is $acc");
216: if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
217: return 1;
218: }
1.152 albertel 219: my $allowed=0;
1.272 albertel 220: my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
221:
1.152 albertel 222: my $name;
223: foreach my $pattern (split(',',$acc)) {
1.233 albertel 224: $pattern =~ s/^\s*//;
225: $pattern =~ s/\s*$//;
1.152 albertel 226: if ($pattern =~ /\*$/) {
227: #35.8.*
228: $pattern=~s/\*//;
229: if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
230: } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
231: #35.8.3.[34-56]
232: my $low=$2;
233: my $high=$3;
234: $pattern=$1;
235: if ($ip =~ /^\Q$pattern\E/) {
236: my $last=(split(/\./,$ip))[3];
237: if ($last <=$high && $last >=$low) { $allowed=1; }
238: }
239: } elsif ($pattern =~ /^\*/) {
240: #*.msu.edu
241: $pattern=~s/\*//;
242: if (!defined($name)) {
243: use Socket;
244: my $netaddr=inet_aton($ip);
245: ($name)=gethostbyaddr($netaddr,AF_INET);
1.158 albertel 246: }
1.152 albertel 247: if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
248: } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
249: #127.0.0.1
250: if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
251: } else {
252: #some.name.com
253: if (!defined($name)) {
254: use Socket;
255: my $netaddr=inet_aton($ip);
256: ($name)=gethostbyaddr($netaddr,AF_INET);
257: }
258: if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
259: }
260: if ($allowed) { last; }
261: }
262: return $allowed;
263: }
1.198 albertel 264:
1.226 albertel 265: sub check_slot_access {
266: my ($id,$type)=@_;
267:
1.207 albertel 268: # does it pass normal muster
1.226 albertel 269: my ($status,$datemsg)=&check_access($id);
270:
1.252 albertel 271: my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
1.251 albertel 272: if ($useslots ne 'resource' && $useslots ne 'map'
273: && $useslots ne 'map_map') {
1.226 albertel 274: return ($status,$datemsg);
275: }
276:
1.200 albertel 277: if ($status eq 'SHOW_ANSWER' ||
278: $status eq 'CLOSED' ||
279: $status eq 'INVALID_ACCESS' ||
280: $status eq 'UNAVAILABLE') {
281: return ($status,$datemsg);
282: }
1.204 albertel 283: if ($env{'request.state'} eq "construct") {
1.203 albertel 284: return ($status,$datemsg);
285: }
1.226 albertel 286:
287: if ($type eq 'Task') {
288: my $version=$Apache::lonhomework::history{'resource.version'};
1.230 albertel 289: if ($Apache::lonhomework::history{"resource.$version.0.checkedin"} &&
290: $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') {
1.226 albertel 291: return ('SHOW_ANSWER');
292: }
1.207 albertel 293: }
1.226 albertel 294:
1.288 ! raeburn 295: my $availablestudent = &Apache::lonnet::EXT("resource.0.availablestudent");
! 296: my $available = &Apache::lonnet::EXT("resource.0.available");
! 297: my @slots= (split(':',$availablestudent),split(':',$available));
1.210 albertel 298:
1.200 albertel 299: # if (!@slots) {
300: # return ($status,$datemsg);
301: # }
302: my $slotstatus='NOT_IN_A_SLOT';
1.206 albertel 303: my ($returned_slot,$slot_name);
1.210 albertel 304: foreach my $slot (@slots) {
1.241 albertel 305: $slot =~ s/(^\s*|\s*$)//g;
1.201 albertel 306: &Apache::lonxml::debug("getting $slot");
1.200 albertel 307: my %slot=&Apache::lonnet::get_slot($slot);
1.201 albertel 308: &Apache::lonhomework::showhash(%slot);
1.200 albertel 309: if ($slot{'starttime'} < time &&
310: $slot{'endtime'} > time &&
1.201 albertel 311: &check_ip_acc($slot{'ip'})) {
1.202 albertel 312: &Apache::lonxml::debug("$slot is good");
313: $slotstatus='NEEDS_CHECKIN';
314: $returned_slot=\%slot;
1.206 albertel 315: $slot_name=$slot;
1.200 albertel 316: last;
317: }
318: }
1.202 albertel 319: if ($slotstatus eq 'NEEDS_CHECKIN' &&
1.226 albertel 320: &proctor_checked_in($slot_name,$returned_slot,$type)) {
1.202 albertel 321: &Apache::lonxml::debug("protoctor checked in");
1.200 albertel 322: $slotstatus='CAN_ANSWER';
323: }
1.226 albertel 324:
1.235 albertel 325: my ($is_correct,$got_grade,$checkedin);
1.226 albertel 326: if ($type eq 'Task') {
1.230 albertel 327: my $version=$Apache::lonhomework::history{'resource.0.version'};
1.231 albertel 328: $got_grade =
329: ($Apache::lonhomework::history{"resource.$version.0.status"}
330: =~ /^(?:pass|fail)$/);
1.235 albertel 331: $is_correct =
332: ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass'
333: || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ );
1.226 albertel 334: $checkedin =
1.230 albertel 335: $Apache::lonhomework::history{"resource.$version.0.checkedin"};
1.226 albertel 336: } elsif ($type eq 'problem') {
1.252 albertel 337: $got_grade = 1;
338: $checkedin = $Apache::lonhomework::history{"resource.0.checkedin"};
339: $is_correct =
340: ($Apache::lonhomework::history{"resource.0.solved"} =~/^correct_/);
1.226 albertel 341: }
342:
1.235 albertel 343: &Apache::lonxml::debug(" slot is $slotstatus checkedin ($checkedin) got_grade ($got_grade) is_correct ($is_correct)");
344:
1.243 albertel 345: # no slot is currently open, and has been checked in for this version
346: # but hasn't got a grade, therefore must be awaiting a grade
347: if (!defined($slot_name)
348: && $checkedin
1.236 albertel 349: && !$got_grade) {
350: return ('WAITING_FOR_GRADE');
351: }
352:
1.252 albertel 353: # no slot is currently open, and has been checked in for this version
354: # previous slot is therefore CLOSED, so therefore the problem is
355: if (!defined($slot_name)
356: && $checkedin
357: && $type eq 'problem') {
358: return ('CLOSED',$datemsg);
359: }
360:
1.226 albertel 361: if ($slotstatus eq 'NOT_IN_A_SLOT'
362: && $checkedin ) {
363:
1.231 albertel 364: if ($got_grade) {
1.209 albertel 365: return ('SHOW_ANSWER');
366: } else {
367: return ('WAITING_FOR_GRADE');
368: }
1.226 albertel 369:
1.207 albertel 370: }
1.255 albertel 371:
1.235 albertel 372: if ( $is_correct) {
1.255 albertel 373: if ($type eq 'problem') {
374: return ($status);
375: }
1.235 albertel 376: return ('SHOW_ANSWER');
377: }
1.255 albertel 378:
1.225 albertel 379: if ( $status eq 'CANNOT_ANSWER' &&
380: ($slotstatus ne 'NEEDS_CHECKIN' && $slotstatus ne 'NOT_IN_A_SLOT')) {
381: return ($status,$datemsg);
382: }
383:
1.206 albertel 384: return ($slotstatus,$datemsg,$slot_name,$returned_slot);
1.198 albertel 385: }
1.200 albertel 386:
1.92 bowersj2 387: # JB, 9/24/2002: Any changes in this function may require a change
388: # in lonnavmaps::resource::getDateStatus.
1.53 www 389: sub check_access {
1.145 albertel 390: my ($id) = @_;
391: my $date ='';
392: my $status;
393: my $datemsg = '';
394: my $lastdate = '';
395: my $type;
396: my $passed;
397:
1.204 albertel 398: if ($env{'request.state'} eq "construct") {
399: if ($env{'form.problemstate'}) {
400: if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
1.277 albertel 401: if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct'
402: && &hide_problem_status())) {
1.168 albertel 403: return ('CANNOT_ANSWER',
1.174 www 404: &mt('is in this state due to author settings.'));
1.167 albertel 405: }
1.165 albertel 406: } else {
1.204 albertel 407: return ($env{'form.problemstate'},
1.174 www 408: &mt('is in this state due to author settings.'));
1.165 albertel 409: }
410: }
1.145 albertel 411: &Apache::lonxml::debug("in construction ignoring dates");
412: $status='CAN_ANSWER';
1.146 albertel 413: $datemsg=&mt('is in under construction');
1.163 albertel 414: # return ($status,$datemsg);
1.145 albertel 415: }
416:
417: &Apache::lonxml::debug("checking for part :$id:");
418: &Apache::lonxml::debug("time:".time);
1.152 albertel 419:
1.261 albertel 420: my ($symb)=&Apache::lonnet::whichuser();
1.212 albertel 421: &Apache::lonxml::debug("symb:".$symb);
422: #if ($env{'request.state'} ne "construct" && $symb ne '') {
1.204 albertel 423: if ($env{'request.state'} ne "construct") {
1.288 ! raeburn 424: my $idacc = &Apache::lonnet::EXT("resource.$id.acc");
! 425: my $allowed=&check_ip_acc($idacc);
1.163 albertel 426: if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
427: $status='INVALID_ACCESS';
428: $date=&mt("can not be accessed from your location.");
1.145 albertel 429: return($status,$date);
430: }
1.163 albertel 431:
1.226 albertel 432: foreach my $temp ("opendate","duedate","answerdate") {
1.163 albertel 433: $lastdate = $date;
1.247 albertel 434: if ($temp eq 'duedate') {
435: $date = &due_date($id);
436: } else {
437: $date = &Apache::lonnet::EXT("resource.$id.$temp");
438: }
439:
1.163 albertel 440: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
441: if ($thistype =~ /^(con_lost|no_such_host)/ ||
442: $date =~ /^(con_lost|no_such_host)/) {
443: $status='UNAVAILABLE';
444: $date=&mt("may open later.");
445: return($status,$date);
446: }
447: if ($thistype eq 'date_interval') {
448: if ($temp eq 'opendate') {
449: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
450: }
451: if ($temp eq 'answerdate') {
452: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
453: }
1.145 albertel 454: }
1.163 albertel 455: &Apache::lonxml::debug("found :$date: for :$temp:");
456: if ($date eq '') {
457: $date = &mt("an unknown date"); $passed = 0;
458: } elsif ($date eq 'con_lost') {
459: $date = &mt("an indeterminate date"); $passed = 0;
460: } else {
461: if (time < $date) { $passed = 0; } else { $passed = 1; }
1.274 www 462: $date = &Apache::lonlocal::locallocaltime($date);
1.145 albertel 463: }
1.163 albertel 464: if (!$passed) { $type=$temp; last; }
1.145 albertel 465: }
1.163 albertel 466: &Apache::lonxml::debug("have :$type:$passed:");
467: if ($passed) {
468: $status='SHOW_ANSWER';
469: $datemsg=$date;
470: } elsif ($type eq 'opendate') {
471: $status='CLOSED';
472: $datemsg = &mt("will open on")." $date";
473: } elsif ($type eq 'duedate') {
474: $status='CAN_ANSWER';
475: $datemsg = &mt("is due at")." $date";
476: } elsif ($type eq 'answerdate') {
477: $status='CLOSED';
478: $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
1.145 albertel 479: }
480: }
1.212 albertel 481: if ($status eq 'CAN_ANSWER' ||
482: (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED'))) {
1.145 albertel 483: #check #tries, and if correct.
484: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
485: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
486: if ( $tries eq '' ) { $tries = '0'; }
1.164 albertel 487: if ( $maxtries eq '' &&
1.204 albertel 488: $env{'request.state'} ne 'construct') { $maxtries = '2'; }
1.164 albertel 489: if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
1.145 albertel 490: # if (correct and show prob status) or excused then CANNOT_ANSWER
491: if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
492: &&
1.277 albertel 493: &show_problem_status())
1.145 albertel 494: ||
495: $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
496: $status = 'CANNOT_ANSWER';
497: }
1.285 albertel 498: if ($status eq 'CANNOT_ANSWER'
499: && &show_answer_problem_status()) {
500: $status = 'SHOW_ANSWER';
501: }
1.121 albertel 502: }
1.181 albertel 503: if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.286 albertel 504: my @interval=&Apache::lonnet::EXT("resource.$id.interval");
505: &Apache::lonxml::debug("looking for interval @interval");
506: if ($interval[0]) {
507: my $first_access=&Apache::lonnet::get_first_access($interval[1]);
1.177 albertel 508: &Apache::lonxml::debug("looking for accesstime $first_access");
509: if (!$first_access) {
510: $status='NOT_YET_VIEWED';
1.247 albertel 511: my $due_date = &due_date($id);
1.256 albertel 512: my $seconds_left = $due_date - time;
1.286 albertel 513: if ($seconds_left > $interval[0] || $due_date eq '') {
514: $seconds_left = $interval[0];
1.256 albertel 515: }
516: $datemsg=&seconds_to_human_length($seconds_left);
1.177 albertel 517: }
518: }
519: }
1.247 albertel 520:
1.133 albertel 521: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
522: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
523: # return ('UNCHECKEDOUT','needs to be checked out');
524: #}
1.54 www 525:
526:
1.145 albertel 527: &Apache::lonxml::debug("sending back :$status:$datemsg:");
528: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
529: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
530: $status='CAN_ANSWER';
1.146 albertel 531: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 532: }
1.106 albertel 533:
1.145 albertel 534: return ($status,$datemsg);
1.20 albertel 535: }
1.247 albertel 536: # this should work exactly like the copy in lonnavmaps.pm
1.246 albertel 537: sub due_date {
1.250 albertel 538: my ($part_id,$symb,$udom,$uname)=@_;
1.246 albertel 539: my $date;
1.286 albertel 540: my @interval= &Apache::lonnet::EXT("resource.$part_id.interval",$symb,
1.250 albertel 541: $udom,$uname);
1.286 albertel 542: &Apache::lonxml::debug("looking for interval $part_id $symb @interval");
1.250 albertel 543: my $due_date= &Apache::lonnet::EXT("resource.$part_id.duedate",$symb,
544: $udom,$uname);
1.247 albertel 545: &Apache::lonxml::debug("looking for due_date $part_id $symb $due_date");
1.286 albertel 546: if ($interval[0] =~ /\d+/) {
547: my $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
548: &Apache::lonxml::debug("looking for first_access $first_access ($interval[1])");
1.247 albertel 549: if (defined($first_access)) {
1.286 albertel 550: my $interval = $first_access+$interval[0];
1.283 albertel 551: $date = (!$due_date || $interval < $due_date) ? $interval
552: : $due_date;
1.247 albertel 553: } else {
554: $date = $due_date;
555: }
556: } else {
557: $date = $due_date;
1.246 albertel 558: }
559: return $date
560: }
561:
1.192 albertel 562: sub seconds_to_human_length {
563: my ($length)=@_;
564:
565: my $seconds=$length%60; $length=int($length/60);
566: my $minutes=$length%60; $length=int($length/60);
567: my $hours=$length%24; $length=int($length/24);
568: my $days=$length;
569:
570: my $timestr;
571: if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
572: if ($hours > 0) { $timestr.=($timestr?", ":"").
573: &mt('[quant,_1,hour]',$hours); }
574: if ($minutes > 0) { $timestr.=($timestr?", ":"").
575: &mt('[quant,_1,minute]',$minutes); }
576: if ($seconds > 0) { $timestr.=($timestr?", ":"").
577: &mt('[quant,_1,second]',$seconds); }
578: return $timestr;
579: }
580:
1.41 albertel 581: sub showhash {
1.145 albertel 582: my (%hash) = @_;
583: &showhashsubset(\%hash,'.');
584: return '';
1.79 albertel 585: }
586:
1.106 albertel 587: sub showarray {
588: my ($array)=@_;
589: my $string="(";
590: foreach my $elm (@{ $array }) {
1.193 albertel 591: if (ref($elm) eq 'ARRAY') {
592: $string.=&showarray($elm);
593: } elsif (ref($elm) eq 'HASH') {
594: $string.= "HASH --- \n<br />";
595: $string.= &showhashsubset($elm,'.');
1.106 albertel 596: } else {
597: $string.="$elm,"
598: }
599: }
600: chop($string);
601: $string.=")";
602: return $string;
603: }
604:
1.79 albertel 605: sub showhashsubset {
1.145 albertel 606: my ($hash,$keyre) = @_;
607: my $resultkey;
608: foreach $resultkey (sort keys %$hash) {
1.193 albertel 609: if ($resultkey !~ /$keyre/) { next; }
610: if (ref($$hash{$resultkey}) eq 'ARRAY' ) {
611: &Apache::lonxml::debug("$resultkey ---- ".
612: &showarray($$hash{$resultkey}));
613: } elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
614: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
615: &showhashsubset($$hash{$resultkey},'.');
616: } else {
617: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.145 albertel 618: }
619: }
620: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
621: return '';
1.41 albertel 622: }
623:
624: sub setuppermissions {
1.204 albertel 625: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
626: my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.145 albertel 627: if (! $viewgrades &&
1.204 albertel 628: exists($env{'request.course.sec'}) &&
629: $env{'request.course.sec'} !~ /^\s*$/) {
630: $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
631: '/'.$env{'request.course.sec'});
1.145 albertel 632: }
1.244 albertel 633: $Apache::lonhomework::viewgrades = $viewgrades;
634:
1.185 albertel 635: if ($Apache::lonhomework::browse eq 'F' &&
1.204 albertel 636: $env{'form.devalidatecourseresdata'} eq 'on') {
1.261 albertel 637: my (undef,$courseid) = &Apache::lonnet::whichuser();
1.204 albertel 638: &Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
639: $env{"course.$courseid.domain"});
1.185 albertel 640: }
1.244 albertel 641:
1.205 albertel 642: my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
643: if (! $modifygrades &&
644: exists($env{'request.course.sec'}) &&
645: $env{'request.course.sec'} !~ /^\s*$/) {
646: $modifygrades =
647: &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
648: '/'.$env{'request.course.sec'});
649: }
650: $Apache::lonhomework::modifygrades = $modifygrades;
1.244 albertel 651:
652: my $queuegrade = &Apache::lonnet::allowed('mqg',$env{'request.course.id'});
653: if (! $queuegrade &&
654: exists($env{'request.course.sec'}) &&
655: $env{'request.course.sec'} !~ /^\s*$/) {
656: $queuegrade =
657: &Apache::lonnet::allowed('qgr',$env{'request.course.id'}.
658: '/'.$env{'request.course.sec'});
659: }
660: $Apache::lonhomework::queuegrade = $queuegrade;
1.205 albertel 661: return '';
1.41 albertel 662: }
663:
1.253 albertel 664: sub unset_permissions {
665: undef($Apache::lonhomework::queuegrade);
666: undef($Apache::lonhomework::modifygrades);
667: undef($Apache::lonhomework::viewgrades);
668: undef($Apache::lonhomework::browse);
669: }
670:
1.41 albertel 671: sub setupheader {
1.120 albertel 672: my $request=$_[0];
1.197 albertel 673: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 674: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
675: &Apache::loncommon::no_cache($request);
676: }
1.196 albertel 677: # $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
678: # 'lastrevisiondate'));
1.120 albertel 679: $request->send_http_header;
680: return OK if $request->header_only;
681: return ''
1.41 albertel 682: }
1.35 albertel 683:
1.47 albertel 684: sub handle_save_or_undo {
1.145 albertel 685: my ($request,$problem,$result) = @_;
686: my $file = &Apache::lonnet::filelocation("",$request->uri);
687: my $filebak =$file.".bak";
688: my $filetmp =$file.".tmp";
689: my $error=0;
1.204 albertel 690: if ($env{'form.Undo'} eq &mt('undo')) {
1.145 albertel 691: my $error=0;
1.284 albertel 692: if (!&File::Copy::copy($file,$filetmp)) { $error=1; }
693: if ((!$error) && (!&File::Copy::copy($filebak,$file))) { $error=1; }
694: if ((!$error) && (!&File::Copy::move($filetmp,$filebak))) { $error=1; }
1.145 albertel 695: if (!$error) {
1.266 albertel 696: &Apache::lonxml::info("<p><b>".
697: &mt("Undid changes, Switched [_1] and [_2]",
698: '<span class="LC_filename">'.$filebak.
699: '</span>',
700: '<span class="LC_filename">'.$file.
701: '</span>')."</b></p>");
1.145 albertel 702: } else {
1.266 albertel 703: &Apache::lonxml::info("<p><span class=\"LC_error\">".
704: &mt("Unable to undo, unable to switch [_1] and [_2]",
705: '<span class="LC_filename">'.
706: $filebak.'</span>',
707: '<span class="LC_filename">'.
708: $file.'</span>')."</span></p>");
1.145 albertel 709: $error=1;
710: }
1.52 albertel 711: } else {
1.262 banghart 712: &Apache::lonnet::correct_line_ends($result);
1.145 albertel 713: my $fs=Apache::File->new(">$filebak");
714: if (defined($fs)) {
715: print $fs $$problem;
1.266 albertel 716: &Apache::lonxml::info("<b>".&mt("Making Backup to [_1]",
717: '<span class="LC_filename">'.
718: $filebak.'</span>').
719: "</b>");
1.145 albertel 720: } else {
1.266 albertel 721: &Apache::lonxml::info("<span class=\"LC_error\">".
722: &mt("Unable to make backup [_1]",
723: '<span class="LC_filename">'.
724: $filebak.'</span>')."</span>");
1.145 albertel 725: $error=2;
726: }
727: my $fh=Apache::File->new(">$file");
728: if (defined($fh)) {
729: print $fh $$result;
1.266 albertel 730: &Apache::lonxml::info("<b>".&mt("Saving Modifications to [_1]",
731: '<span class="LC_filename">'.
732: $file.'</span>' )."</b>");
1.145 albertel 733: } else {
1.266 albertel 734: &Apache::lonxml::info('<span class="LC_error">'.
735: &mt("Unable to write to [_1]",
736: '<span class="LC_filename">'.
737: $file.'</span>').
738: '</span>');
1.145 albertel 739: $error|=4;
740: }
1.52 albertel 741: }
1.145 albertel 742: return $error;
1.64 albertel 743: }
744:
1.101 albertel 745: sub analyze_header {
746: my ($request) = @_;
1.238 albertel 747: my $result =
1.240 albertel 748: &Apache::loncommon::start_page('Analyzing a problem',undef);
749:
1.238 albertel 750: $result .=
751: &Apache::lonxml::message_location().'
1.101 albertel 752: <form name="lonhomework" method="POST" action="'.
1.204 albertel 753: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179 albertel 754: &Apache::structuretags::remember_problem_state().'
1.278 albertel 755: <div class="LC_edit_problem_analyze_header">
1.146 albertel 756: <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
757: <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
1.278 albertel 758: <hr class="LC_edit_problem_divide" />
759:
1.146 albertel 760: <input type="submit" name="submit" value="'.&mt("View").'" />
1.278 albertel 761: <hr class="LC_edit_problem_divide" />
762: </div>
1.101 albertel 763: </form>';
1.171 albertel 764: &Apache::lonxml::add_messages(\$result);
1.101 albertel 765: $request->print($result);
766: $request->rflush();
767: }
768:
1.109 albertel 769: sub analyze_footer {
770: my ($request) = @_;
1.237 albertel 771: $request->print(&Apache::loncommon::end_page());
1.109 albertel 772: $request->rflush();
773: }
774:
1.74 albertel 775: sub analyze {
1.101 albertel 776: my ($request,$file) = @_;
777: &Apache::lonxml::debug("Analyze");
778: my $result;
779: my %overall;
1.219 www 780: my %seedexample;
1.101 albertel 781: my %allparts;
1.204 albertel 782: my $rndseed=$env{'form.rndseed'};
1.101 albertel 783: &analyze_header($request);
1.114 albertel 784: my %prog_state=
1.146 albertel 785: &Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
786: &mt('Getting Problem Variants'),
1.204 albertel 787: $env{'form.numtoanalyze'},
1.175 albertel 788: 'inline',undef);
1.204 albertel 789: for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 790: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
1.146 albertel 791: &mt('last problem'));
1.182 albertel 792: if (&Apache::loncommon::connection_aborted($request)) { return; }
1.219 www 793: my $thisseed=$i+$rndseed;
1.101 albertel 794: my $subresult=&Apache::lonnet::ssi($request->uri,
795: ('grade_target' => 'analyze'),
1.219 www 796: ('rndseed' => $thisseed));
1.101 albertel 797: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
798: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 799: my @parts;
800: if (defined(@{ $analyze{'parts'} })) {
801: @parts=@{ $analyze{'parts'} };
802: }
1.101 albertel 803: foreach my $part (@parts) {
804: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 805: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
806: $analyze{$part.'.type'} eq 'stringresponse' ||
807: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.263 albertel 808: foreach my $name (keys(%{ $analyze{$part.'.answer'} })) {
809: my $i=0;
810: foreach my $answer_part (@{ $analyze{$part.'.answer'}{$name} }) {
811: push( @{ $overall{$part.'.answer'}[$i] },
812: $answer_part);
813: my $concatanswer= join("\0",@{ $answer_part });
814: if (($concatanswer eq '') || ($concatanswer=~/^\@/)) {
1.266 albertel 815: $answer_part = ['<span class="LC_error">'.&mt('Error').'</span>'];
1.263 albertel 816: }
817: $seedexample{join("\0",$part,$i,@{$answer_part})}=
818: $thisseed;
819: $i++;
820: }
1.219 www 821: }
1.275 albertel 822: if (!keys(%{ $analyze{$part.'.answer'} })) {
823: my $answer_part =
824: ['<span class="LC_error">'.&mt('Error').'</span>'];
825: $seedexample{join("\0",$part,0,@{$answer_part})}=
826: $thisseed;
827: push( @{ $overall{$part.'.answer'}[0] },
828: $answer_part);
829: }
1.101 albertel 830: }
831: }
832: }
1.114 albertel 833: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
1.146 albertel 834: &mt('Analyzing Results'));
1.278 albertel 835: $request->print('<hr class="LC_edit_problem_divide" />'.&mt('List of possible answers').': ');
1.134 albertel 836: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 837: if (defined(@{ $overall{$part.'.answer'} })) {
1.263 albertel 838: for (my $i=0;$i<scalar(@{ $overall{$part.'.answer'} });$i++) {
839: my $num_cols=scalar(@{ $overall{$part.'.answer'}[$i][0] });
840: $request->print('<table class="thinborder"><tr><th colspan="'.($num_cols+1).'">'.&mt('Part').' '.$part);
841: if (scalar(@{ $overall{$part.'.answer'} }) > 1) {
842: $request->print(&mt(' Answer [_1]',$i+1));
843: }
844: $request->print('</th></tr>');
845: my %frequency;
846: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'}[$i] })) {
847: $frequency{join("\0",@{ $answer })}++;
848: }
849: $request->print('<tr><th colspan="'.($num_cols).'">'.&mt('Answer').'</th><th>'.&mt('Frequency').'<br />('
850: .&mt('click for example').')</th></tr>');
851: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
852: $request->print('<tr><td class="center">'.
853: join('</td><td class="center">',split("\0",$answer)).
854: '</td><td class="center"><a href="'.$request->uri.'?rndseed='.$seedexample{join("\0",$part,$i,$answer)}.'">'.$frequency{$answer}.
855: '</a></td></tr>');
856: }
857: $request->print('</table>');
1.109 albertel 858: }
859: } else {
1.162 albertel 860: $request->print('<p>'.&mt('Response').' '.$part.' '.
1.146 albertel 861: &mt('is not analyzable at this time').'</p>');
1.101 albertel 862: }
863: }
1.130 albertel 864: if (scalar(keys(%allparts)) == 0 ) {
1.215 raeburn 865: $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 866: }
1.114 albertel 867: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 868: &analyze_footer($request);
1.101 albertel 869: &Apache::lonhomework::showhash(%overall);
870: return $result;
1.74 albertel 871: }
872:
1.277 albertel 873: {
874: my $show_problem_status;
875: sub reset_show_problem_status {
876: undef($show_problem_status);
877: }
878:
879: sub set_show_problem_status {
880: my ($new_status) = @_;
881: $show_problem_status = lc($new_status);
882: }
883:
884: sub hide_problem_status {
885: return ($show_problem_status eq 'no'
886: || $show_problem_status eq 'no_feedback_ever');
887: }
888:
889: sub show_problem_status {
890: return ($show_problem_status eq 'yes'
1.285 albertel 891: || $show_problem_status eq 'answer'
1.277 albertel 892: || $show_problem_status eq '');
893: }
894:
895: sub show_some_problem_status {
896: return ($show_problem_status eq 'no');
897: }
898:
899: sub show_no_problem_status {
900: return ($show_problem_status eq 'no_feedback_ever');
901: }
1.285 albertel 902:
903: sub show_answer_problem_status {
904: return ($show_problem_status eq 'answer');
905: }
1.277 albertel 906: }
907:
1.64 albertel 908: sub editxmlmode {
1.145 albertel 909: my ($request,$file) = @_;
910: my $result;
911: my $problem=&Apache::lonnet::getfile($file);
912: if ($problem eq -1) {
1.146 albertel 913: &Apache::lonxml::error("<b> ".&mt('Unable to find').
914: " <i>$file</i></b>");
1.145 albertel 915: $problem='';
916: }
1.204 albertel 917: if (defined($env{'form.editxmltext'}) || defined($env{'form.Undo'})) {
1.145 albertel 918: my $error=&handle_save_or_undo($request,\$problem,
1.204 albertel 919: \$env{'form.editxmltext'});
1.145 albertel 920: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
921: }
1.204 albertel 922: &Apache::lonhomework::showhashsubset(\%env,'^form');
1.281 albertel 923: if ( $env{'form.submit'} eq &mt('Save and View') ) {
1.204 albertel 924: &Apache::lonhomework::showhashsubset(\%env,'^form');
925: $env{'form.problemmode'}='View';
1.145 albertel 926: &renderpage($request,$file);
927: } else {
928: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.160 www 929: my $xml_help = '<table><tr><td>'.
930: &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
931: "Problem Editing Help").
932: '</td><td>'.
1.254 albertel 933: &Apache::loncommon::help_open_menu(undef,undef,5,'Authoring').
1.178 albertel 934: '</td></tr></table>';
1.145 albertel 935: if ($cols > 80) { $cols = 80; }
936: if ($cols < 70) { $cols = 70; }
937: if ($rows < 20) { $rows = 20; }
1.269 albertel 938: my $js =
939: &Apache::edit::js_change_detection().
940: &Apache::loncommon::resize_textarea_js();
1.270 albertel 941: my $only_body = ($env{'environment.remote'} eq 'off')? 0 : 1;
1.238 albertel 942: my $start_page =
1.269 albertel 943: &Apache::loncommon::start_page(&mt("EditXML [_1]",$file),$js,
944: {'no_auto_mt_title' => 1,
1.270 albertel 945: 'only_body' => $only_body,
1.269 albertel 946: 'add_entries' => {
947: 'onresize' => q[resize_textarea('LC_editxmltext','LC_aftertextarea')],
948: 'onload' => q[resize_textarea('LC_editxmltext','LC_aftertextarea')],
949: }});
1.227 albertel 950:
1.238 albertel 951: $result.=$start_page.
1.213 albertel 952: &renderpage($request,$file,['no_output_web'],1).
1.281 albertel 953: '<form '.&Apache::edit::form_change_detection().' name="lonhomework" method="POST" action="'.
1.204 albertel 954: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179 albertel 955: &Apache::structuretags::remember_problem_state().'
1.278 albertel 956: <div class="LC_edit_problem_editxml_header">
1.280 albertel 957: <table class="LC_edit_problem_header_title"><tr><td>
958: '.&mt('Problem Editing').&Apache::loncommon::help_open_topic('Problem_Editor_XML_Index').'
959: </td><td align="right">
960: '.&Apache::loncommon::helpLatexCheatsheet().'
961: </td></tr>
962: </table>
963: <div class="LC_edit_problem_discards">
964: <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
965: <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
966: <input type="submit" '.&Apache::edit::submit_ask_anyway().'name="problemmode" accesskey="e" value="'.&mt('Edit').'" />
967: <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
968: </div>
969: <div class="LC_edit_problem_saves">
970: <input type="submit" name="submit" accesskey="s" value="'.&mt('Save').'" />
971: <input type="submit" name="submit" accesskey="v" value="'.&mt('Save and View').'" />
972: </div>
973: <hr class="LC_edit_problem_divide" />
1.281 albertel 974: '.&Apache::lonxml::message_location().'
1.278 albertel 975: </div>
1.280 albertel 976: ' . '
1.269 albertel 977: <textarea '.&Apache::edit::element_change_detection().
978: ' rows="'.$rows.'" cols="'.$cols.'" style="width:100%" '.
979: ' name="editxmltext" id="LC_editxmltext">'.
980: &HTML::Entities::encode($problem,'<>&"').'</textarea>
981: <div id="LC_aftertextarea">
982: </div>
1.238 albertel 983: </form>'.&Apache::loncommon::end_page();
1.171 albertel 984: &Apache::lonxml::add_messages(\$result);
1.145 albertel 985: $request->print($result);
986: }
987: return '';
1.47 albertel 988: }
1.189 albertel 989:
1.188 foxr 990: #
991: # Render the page in whatever target desired.
992: #
1.41 albertel 993: sub renderpage {
1.213 albertel 994: my ($request,$file,$targets,$return_string) = @_;
1.52 albertel 995:
1.211 albertel 996: my @targets = @{$targets || [&get_target()]};
1.204 albertel 997: &Apache::lonhomework::showhashsubset(\%env,'form.');
1.145 albertel 998: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.268 albertel 999:
1.171 albertel 1000: my $overall_result;
1.145 albertel 1001: foreach my $target (@targets) {
1.183 albertel 1002: # FIXME need to do something intelligent when a problem goes
1003: # from viewable to not viewable due to map conditions
1004: #&setuppermissions();
1005: #if ( $Apache::lonhomework::browse ne '2'
1006: # && $Apache::lonhomework::browse ne 'F' ) {
1007: # $request->print(" You most likely shouldn't see me.");
1008: #}
1.145 albertel 1009: #my $t0 = [&gettimeofday()];
1.211 albertel 1010: my $output=1;
1011: if ($target eq 'no_output_web') {
1012: $target = 'web'; $output=0;
1013: }
1.145 albertel 1014: my $problem=&Apache::lonnet::getfile($file);
1.222 albertel 1015: my $result;
1.145 albertel 1016: if ($problem eq -1) {
1.260 albertel 1017: $problem='';
1.222 albertel 1018: my $filename=(split('/',$file))[-1];
1.260 albertel 1019: my $error =
1020: "<b> ".&mt('Unable to find [_1]',
1021: ' <span class="LC_filename">'.$filename.'</span>')
1022: ."</b>";
1023: $result.=
1024: &Apache::loncommon::simple_error_page($request,'Not available',
1025: $error);
1026: return;
1.145 albertel 1027: }
1.52 albertel 1028:
1.145 albertel 1029: my %mystyle;
1030: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1031: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
1.204 albertel 1032: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
1.145 albertel 1033:
1034: &Apache::lonxml::debug("Should be parsing now");
1.222 albertel 1035: $result .= &Apache::lonxml::xmlparse($request, $target, $problem,
1036: &setup_vars($target),%mystyle);
1.273 albertel 1037: &finished_parsing();
1.214 albertel 1038: if (!$output) { $result = ''; }
1.145 albertel 1039: #$request->print("Result follows:");
1040: if ($target eq 'modified') {
1041: &handle_save_or_undo($request,\$problem,\$result);
1042: } else {
1043: if ($target eq 'analyze') {
1044: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1045: undef(%Apache::lonhomework::analyze);
1046: }
1047: #my $td=&tv_interval($t0);
1048: #if ( $Apache::lonxml::debug) {
1049: #$result =~ s:</body>::;
1050: #$result.="<br />Spent $td seconds processing target $target\n</body>";
1051: #}
1.171 albertel 1052: # $request->print($result);
1053: $overall_result.=$result;
1054: # $request->rflush();
1.145 albertel 1055: }
1056: #$request->print(":Result ends");
1057: #my $td=&tv_interval($t0);
1.52 albertel 1058: }
1.213 albertel 1059: if (!$return_string) {
1060: &Apache::lonxml::add_messages(\$overall_result);
1061: $request->print($overall_result);
1062: $request->rflush();
1063: } else {
1064: return $overall_result;
1065: }
1.41 albertel 1066: }
1067:
1.273 albertel 1068: sub finished_parsing {
1069: undef($Apache::lonhomework::parsing_a_problem);
1070: undef($Apache::lonhomework::parsing_a_task);
1071: }
1072:
1.42 albertel 1073: sub get_template_list {
1.282 albertel 1074: my ($extension) = @_;
1.145 albertel 1075: my $result;
1076: my @allnames;
1077: &Apache::lonxml::debug("Looking for :$extension:");
1.282 albertel 1078: my $glob_extension = $extension;
1079: if ($extension eq 'survey' || $extension eq 'exam') {
1080: $glob_extension = 'problem';
1081: }
1082: my @files = glob($Apache::lonnet::perlvar{'lonIncludes'}.
1083: '/templates/*.'.$glob_extension);
1084: @files = map {[$_,&mt(&Apache::lonnet::metadata($_, 'title'))]} (@files);
1085: @files = sort {$a->[1] cmp $b->[1]} (@files);
1.287 raeburn 1086: my ($midpoint,$seconddiv,$numfiles);
1087: $numfiles = 0;
1088: foreach my $file (@files) {
1089: next if ($file->[1] !~ /\S/);
1090: $numfiles ++;
1091: }
1092: if ($numfiles > 0) {
1093: $result = '<div class="LC_left_float">';
1094: $midpoint = int($numfiles/2);
1095: if ($numfiles%2) {
1096: $midpoint ++;
1097: }
1098: }
1099: my $count = 0;
1.282 albertel 1100: foreach my $file (@files) {
1101: next if ($file->[1] !~ /\S/);
1102: $result .=
1103: '<label><input type="radio" name="template" value="'.$file->[0].'" />'.
1.287 raeburn 1104: $file->[1].'</label><br />'."\n";
1105: $count ++;
1106: if ((!$seconddiv) && ($count >= $midpoint)) {
1107: $result .= '</div>'."\n".'<div class="LC_left_float">'."\n";
1108: $seconddiv = 1;
1109: }
1110: }
1111: if ($numfiles > 0) {
1112: $result .= '</div>'."\n".'<div class="LC_clear_float_footer"></div>'."\n";
1.42 albertel 1113: }
1.145 albertel 1114: return $result;
1.42 albertel 1115: }
1116:
1117: sub newproblem {
1.65 matthew 1118: my ($request) = @_;
1.282 albertel 1119:
1120: if ($env{'form.template'}) {
1121: my $file = $env{'form.template'};
1.65 matthew 1122: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.282 albertel 1123: &File::Copy::copy($file,$dest);
1.65 matthew 1124: &renderpage($request,$dest);
1.282 albertel 1125: return;
1126: }
1127:
1128: my ($extension) = ($request->uri =~ m/\.(\w+)$/);
1129: &Apache::lonxml::debug("Looking for :$extension:");
1130: my $templatelist=&get_template_list($extension);
1131: if ($env{'form.newfile'} && !$templatelist) {
1132: # no templates found
1.131 albertel 1133: my $templatefilename =
1.258 albertel 1134: $request->dir_config('lonIncludes').'/templates/blank.'.$extension;
1.131 albertel 1135: &Apache::lonxml::debug("$templatefilename");
1136: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.282 albertel 1137: &File::Copy::copy($templatefilename,$dest);
1.131 albertel 1138: &renderpage($request,$dest);
1.85 albertel 1139: } else {
1.176 albertel 1140: my $url=&HTML::Entities::encode($request->uri,'<>&"');
1141: my $shownurl=$url;
1.157 albertel 1142: $shownurl=~s-^/~-/priv/-;
1.65 matthew 1143: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 1144: my $errormsg;
1.85 albertel 1145: my $instructions;
1.240 albertel 1146: my $start_page =
1147: &Apache::loncommon::start_page("Create New $extension");
1.258 albertel 1148: $request->print("
1.240 albertel 1149: $start_page
1.258 albertel 1150: <h1>".&mt("Creating a new $extension resource.")."</h1>
1.128 albertel 1151: $errormsg
1.258 albertel 1152: ".&mt("The requested file [_1] currently does not exist.",
1153: "<tt>$shownurl</tt>")."
1.105 www 1154: <p>
1.258 albertel 1155: <b>
1156: ".&mt("To create a new $extension, select a template from the".
1.282 albertel 1157: " list below. Then click on the \"Create $extension\" button.")."</b>
1.259 albertel 1158: </p><form action=\"$url\" method=\"POST\">");
1.258 albertel 1159:
1.85 albertel 1160: if (defined($templatelist)) {
1.282 albertel 1161: $request->print($templatelist);
1.85 albertel 1162: }
1.282 albertel 1163: $request->print('<br /><input type="submit" name="newfile" value="'.
1164: &mt("Create $extension").'" />');
1.238 albertel 1165: $request->print("</form></p>".&Apache::loncommon::end_page());
1.65 matthew 1166: }
1.282 albertel 1167: return;
1.42 albertel 1168: }
1169:
1.268 albertel 1170: sub update_construct_style {
1171: if ($env{'request.state'} eq "construct"
1172: && $env{'form.problemmode'} eq &mt('View')
1173: && defined($env{'form.submitted'})
1174: && !defined($env{'form.resetdata'})
1175: && !defined($env{'form.newrandomization'})) {
1176: if ((!$env{'form.style_file'} && $env{'construct.style'})
1177: ||$env{'form.clear_style_file'}) {
1178: &Apache::lonnet::delenv('construct\\.style');
1179: } elsif ($env{'form.style_file'}
1180: && $env{'construct.style'} ne $env{'form.style_file'}) {
1181: &Apache::lonnet::appenv('construct.style' =>
1182: $env{'form.style_file'});
1183: }
1184: }
1185: }
1186:
1187:
1.41 albertel 1188: sub handler {
1.145 albertel 1189: #my $t0 = [&gettimeofday()];
1190: my $request=$_[0];
1.223 albertel 1191: $Apache::lonxml::request=$request;
1.204 albertel 1192: $Apache::lonxml::debug=$env{'user.debug'};
1193: $env{'request.uri'}=$request->uri;
1.180 albertel 1194: &setuppermissions();
1.145 albertel 1195: # some times multiple problemmodes are submitted, need to select
1196: # the last one
1.204 albertel 1197: if ( defined($env{'form.problemmode'}) && ref($env{'form.problemmode'}) ) {
1198: my $mode=$env{'form.problemmode'}->[-1];
1199: undef $env{'form.problemmode'};
1200: $env{'form.problemmode'}=$mode;
1.145 albertel 1201: }
1.193 albertel 1202:
1.145 albertel 1203: my $file=&Apache::lonnet::filelocation("",$request->uri);
1204:
1205: #check if we know where we are
1.204 albertel 1206: if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) {
1.145 albertel 1207: # if we are browsing we might not be able to know where we are
1.173 albertel 1208: if ($Apache::lonhomework::browse ne 'F' &&
1.204 albertel 1209: $env{'request.state'} ne "construct") {
1.145 albertel 1210: #should know where we are, so ask
1.253 albertel 1211: &unset_permissions();
1212: $request->internal_redirect('/adm/ambiguous');
1213: return OK;
1.145 albertel 1214: }
1215: }
1.253 albertel 1216: if (&setupheader($request)) {
1217: &unset_permissions();
1218: return OK;
1219: }
1.244 albertel 1220: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");
1.204 albertel 1221: &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
1.261 albertel 1222: my ($symb) = &Apache::lonnet::whichuser();
1.145 albertel 1223: &Apache::lonxml::debug('symb is '.$symb);
1.204 albertel 1224: if ($env{'request.state'} eq "construct") {
1.145 albertel 1225: if ( -e $file ) {
1226: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1227: ['problemmode']);
1.204 albertel 1228: if (!(defined $env{'form.problemmode'})) {
1.145 albertel 1229: #first visit to problem in construction space
1.204 albertel 1230: $env{'form.problemmode'}='View';
1.145 albertel 1231: &renderpage($request,$file);
1.204 albertel 1232: } elsif ($env{'form.problemmode'} eq &mt('EditXML') ||
1233: $env{'form.problemmode'} eq 'EditXML') {
1.145 albertel 1234: &editxmlmode($request,$file);
1.204 albertel 1235: } elsif ($env{'form.problemmode'} eq &mt('Calculate answers')) {
1.145 albertel 1236: &analyze($request,$file);
1237: } else {
1.268 albertel 1238: &update_construct_style();
1.145 albertel 1239: &renderpage($request,$file);
1240: }
1241: } else {
1242: # requested file doesn't exist in contruction space
1243: &newproblem($request);
1244: }
1245: } else {
1246: # just render the page normally outside of construction space
1247: &Apache::lonxml::debug("not construct");
1.52 albertel 1248: &renderpage($request,$file);
1.41 albertel 1249: }
1.145 albertel 1250: #my $td=&tv_interval($t0);
1251: #&Apache::lonxml::debug("Spent $td seconds processing");
1252: # always turn off debug messages
1253: $Apache::lonxml::debug=0;
1.253 albertel 1254: &unset_permissions();
1.145 albertel 1255: return OK;
1.52 albertel 1256:
1.1 albertel 1257: }
1258:
1259: 1;
1260: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>