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