Annotation of loncom/homework/lonhomework.pm, revision 1.138.2.1
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.138.2.1! albertel 4: # $Id: lonhomework.pm,v 1.138 2003/07/22 19:55:26 albertel Exp $
1.63 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.52 albertel 28: # Guy Albertelli
1.17 www 29: # 11/30 Gerd Kortemeyer
1.56 www 30: # 6/1,8/17,8/18 Gerd Kortemeyer
1.82 bowersj2 31: # 7/18 Jeremy Bowers
1.1 albertel 32:
33: package Apache::lonhomework;
34: use strict;
1.73 albertel 35: use Apache::style();
36: use Apache::lonxml();
37: use Apache::lonnet();
38: use Apache::lonplot();
39: use Apache::inputtags();
40: use Apache::structuretags();
41: use Apache::randomlabel();
42: use Apache::response();
43: use Apache::hint();
44: use Apache::outputtags();
1.83 albertel 45: use Apache::caparesponse();
46: use Apache::radiobuttonresponse();
47: use Apache::optionresponse();
48: use Apache::imageresponse();
49: use Apache::essayresponse();
50: use Apache::externalresponse();
1.106 albertel 51: use Apache::rankresponse();
1.107 albertel 52: use Apache::matchresponse();
1.137 albertel 53: use Apache::chemresponse();
1.26 www 54: use Apache::Constants qw(:common);
1.73 albertel 55: use HTML::Entities();
1.83 albertel 56: use Apache::loncommon();
1.47 albertel 57: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 58:
1.69 harris41 59: BEGIN {
1.43 albertel 60: &Apache::lonxml::register_insert();
61: }
62:
1.5 albertel 63: sub get_target {
1.126 www 64: if (($ENV{'request.state'} eq "published") ||
65: ($ENV{'request.state'} eq "uploaded")) {
1.90 sakharuk 66: if ( defined($ENV{'form.grade_target'} )
67: && ($ENV{'form.grade_target'} eq 'tex')) {
68: return ($ENV{'form.grade_target'});
69: } elsif ( defined($ENV{'form.grade_target'} )
1.119 albertel 70: && ($Apache::lonhomework::viewgrades eq 'F' )) {
1.66 albertel 71: return ($ENV{'form.grade_target'});
72: }
1.90 sakharuk 73:
1.118 albertel 74: if ( defined($ENV{'form.submitted'}) &&
1.138 albertel 75: ( !defined($ENV{'form.resetdata'})) &&
76: ( !defined($ENV{'form.newrandomization'}))) {
1.52 albertel 77: return ('grade', 'web');
78: } else {
79: return ('web');
80: }
81: } elsif ($ENV{'request.state'} eq "construct") {
1.74 albertel 82: if ( defined($ENV{'form.grade_target'}) ) {
83: return ($ENV{'form.grade_target'});
84: }
1.62 albertel 85: if ( defined($ENV{'form.preview'})) {
86: if ( defined($ENV{'form.submitted'})) {
1.52 albertel 87: return ('grade', 'web');
88: } else {
89: return ('web');
90: }
91: } else {
1.88 albertel 92: if ( $ENV{'form.problemmode'} eq 'View' ||
93: $ENV{'form.problemmode'} eq 'Discard Edits and View') {
1.62 albertel 94: if ( defined($ENV{'form.submitted'}) &&
1.138 albertel 95: (!defined($ENV{'form.resetdata'})) &&
96: (!defined($ENV{'form.newrandomization'}))) {
1.60 albertel 97: return ('grade', 'web','answer');
1.42 albertel 98: } else {
1.60 albertel 99: return ('web','answer');
1.42 albertel 100: }
1.52 albertel 101: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
102: if ( $ENV{'form.submitted'} eq 'edit' ) {
1.80 albertel 103: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
1.81 albertel 104: return ('modified','web','answer');
1.80 albertel 105: } else {
106: return ('modified','edit');
107: }
1.42 albertel 108: } else {
1.52 albertel 109: return ('edit');
1.42 albertel 110: }
1.52 albertel 111: } else {
112: return ('web');
113: }
1.15 albertel 114: }
1.52 albertel 115: }
116: return ();
1.5 albertel 117: }
118:
1.3 albertel 119: sub setup_vars {
1.52 albertel 120: my ($target) = @_;
121: return ';'
1.11 albertel 122: # return ';$external::target='.$target.';';
1.2 albertel 123: }
124:
125: sub send_header {
1.52 albertel 126: my ($request)= @_;
127: $request->print(&Apache::lontexconvert::header());
1.16 albertel 128: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 129: }
130:
1.36 albertel 131: sub createmenu {
1.52 albertel 132: my ($which,$request)=@_;
133: if ($which eq 'grade') {
134: $request->print('<script language="JavaScript">
1.91 albertel 135: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52 albertel 136: "height=350,width=150,menubar=no");
137: </script>');
138: }
1.36 albertel 139: }
140:
1.2 albertel 141: sub send_footer {
1.52 albertel 142: my ($request)= @_;
1.16 albertel 143: # $request->print('</form>');
1.52 albertel 144: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 145: }
146:
1.52 albertel 147: $Apache::lonxml::browse='';
1.53 www 148:
1.138.2.1! albertel 149: sub check_ip_acc {
! 150: my ($acc)=@_;
! 151: if (!defined($acc) || $acc =~ /^\s*$/) { return 1; }
! 152: my $allowed=0;
! 153: my $ip=$ENV{'REMOTE_ADDR'};
! 154: my $name;
! 155: foreach my $pattern (split(',',$acc)) {
! 156: if ($pattern =~ /\*$/) {
! 157: #35.8.*
! 158: $pattern=~s/\*//;
! 159: if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
! 160: } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
! 161: #35.8.3.[34-56]
! 162: my $low=$2;
! 163: my $high=$3;
! 164: $pattern=$1;
! 165: if ($ip =~ /^\Q$pattern\E/) {
! 166: my $last=(split(/\./,$ip))[3];
! 167: if ($last <=$high && $last >=$low) { $allowed=1; }
! 168: }
! 169: } elsif ($pattern =~ /^\*/) {
! 170: #*.msu.edu
! 171: $pattern=~s/\*//;
! 172: if (!defined($name)) {
! 173: use Socket;
! 174: my $netaddr=inet_aton($ip);
! 175: ($name)=gethostbyaddr($netaddr,AF_INET);
! 176: }
! 177: if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
! 178: } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
! 179: #127.0.0.1
! 180: if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
! 181: } else {
! 182: #some.name.com
! 183: if (!defined($name)) {
! 184: use Socket;
! 185: my $netaddr=inet_aton($ip);
! 186: ($name)=gethostbyaddr($netaddr,AF_INET);
! 187: }
! 188: if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
! 189: }
! 190: if ($allowed) { last; }
! 191: }
! 192: return $allowed;
! 193: }
1.92 bowersj2 194: # JB, 9/24/2002: Any changes in this function may require a change
195: # in lonnavmaps::resource::getDateStatus.
1.53 www 196: sub check_access {
1.52 albertel 197: my ($id) = @_;
198: my $date ='';
1.103 albertel 199: my $status;
1.52 albertel 200: my $datemsg = '';
201: my $lastdate = '';
202: my $temp;
203: my $type;
204: my $passed;
1.106 albertel 205:
206: if ($ENV{'request.state'} eq "construct") {
207: &Apache::lonxml::debug("in construction ignoring dates");
208: $status='CAN_ANSWER';
209: $datemsg='is in under construction';
210: return ($status,$datemsg);
211: }
212:
1.52 albertel 213: &Apache::lonxml::debug("checking for part :$id:");
1.73 albertel 214: &Apache::lonxml::debug("time:".time);
1.138.2.1! albertel 215:
! 216: my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
! 217: if (!$allowed) {
! 218: $status='INVALID_ACCESS';
! 219: $date=&mt("can not be accessed from your location.");
! 220: return($status,$date);
! 221: }
! 222:
1.52 albertel 223: foreach $temp ("opendate","duedate","answerdate") {
224: $lastdate = $date;
225: $date = &Apache::lonnet::EXT("resource.$id.$temp");
1.87 www 226: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
1.103 albertel 227: if ($thistype =~ /^(con_lost|no_such_host)/ ||
228: $date =~ /^(con_lost|no_such_host)/) {
229: $status='UNAVAILABLE';
230: $date="may open later.";
231: return($status,$date);
232: }
1.87 www 233: if ($thistype eq 'date_interval') {
234: if ($temp eq 'opendate') {
235: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
236: }
237: if ($temp eq 'answerdate') {
238: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
239: }
240: }
1.52 albertel 241: &Apache::lonxml::debug("found :$date: for :$temp:");
242: if ($date eq '') {
243: $date = "an unknown date"; $passed = 0;
244: } elsif ($date eq 'con_lost') {
245: $date = "an indeterminate date"; $passed = 0;
246: } else {
247: if (time < $date) { $passed = 0; } else { $passed = 1; }
248: $date = localtime $date;
1.51 harris41 249: }
1.52 albertel 250: if (!$passed) { $type=$temp; last; }
251: }
252: &Apache::lonxml::debug("have :$type:$passed:");
253: if ($passed) {
254: $status='SHOW_ANSWER';
255: $datemsg=$date;
256: } elsif ($type eq 'opendate') {
257: $status='CLOSED';
258: $datemsg = "will open on $date";
259: } elsif ($type eq 'duedate') {
260: $status='CAN_ANSWER';
261: $datemsg = "is due at $date";
262: } elsif ($type eq 'answerdate') {
263: $status='CLOSED';
264: $datemsg = "was due on $lastdate, and answers will be available on $date";
265: }
266: if ($status eq 'CAN_ANSWER') {
1.121 albertel 267: #check #tries, and if correct.
1.52 albertel 268: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
269: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
270: if ( $tries eq '' ) { $tries = '0'; }
271: if ( $maxtries eq '' ) { $maxtries = '2'; }
1.129 albertel 272: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
273: # if (correct and show prob status) or excused then CANNOT_ANSWER
274: if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
275: &&
276: lc($Apache::lonhomework::problemstatus) ne 'no')
277: ||
278: $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
1.121 albertel 279: $status = 'CANNOT_ANSWER';
280: }
1.52 albertel 281: }
1.54 www 282:
1.133 albertel 283: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
284: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
285: # return ('UNCHECKEDOUT','needs to be checked out');
286: #}
1.54 www 287:
288:
1.52 albertel 289: &Apache::lonxml::debug("sending back :$status:$datemsg:");
290: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
1.91 albertel 291: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
1.52 albertel 292: $status='CAN_ANSWER';
293: $datemsg='is closed but you are allowed to view it';
294: }
1.106 albertel 295:
1.52 albertel 296: return ($status,$datemsg);
1.20 albertel 297: }
298:
1.41 albertel 299: sub showhash {
1.52 albertel 300: my (%hash) = @_;
1.99 albertel 301: &showhashsubset(\%hash,'.');
1.79 albertel 302: return '';
303: }
304:
1.106 albertel 305: sub showarray {
306: my ($array)=@_;
307: my $string="(";
308: foreach my $elm (@{ $array }) {
309: if (ref($elm)) {
310: if ($elm =~ /ARRAY/ ) {
311: $string.=&showarray($elm);
312: }
313: } else {
314: $string.="$elm,"
315: }
316: }
317: chop($string);
318: $string.=")";
319: return $string;
320: }
321:
1.79 albertel 322: sub showhashsubset {
323: my ($hash,$keyre) = @_;
1.52 albertel 324: my $resultkey;
1.79 albertel 325: foreach $resultkey (sort keys %$hash) {
326: if ($resultkey =~ /$keyre/) {
327: if (ref($$hash{$resultkey})) {
328: if ($$hash{$resultkey} =~ /ARRAY/ ) {
1.106 albertel 329: &Apache::lonxml::debug("$resultkey ---- ".
330: &showarray($$hash{$resultkey}));
331: } elsif ($$hash{$resultkey} =~ /HASH/ ) {
332: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
333: &showhashsubset($$hash{$resultkey},'.');
1.79 albertel 334: } else {
1.106 albertel 335: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 336: }
337: } else {
1.79 albertel 338: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 339: }
340: }
1.52 albertel 341: }
342: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
343: return '';
1.41 albertel 344: }
345:
346: sub setuppermissions {
1.52 albertel 347: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
1.127 matthew 348: my $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
349: if (! $viewgrades &&
350: exists($ENV{'request.course.sec'}) &&
351: $ENV{'request.course.sec'} !~ /^\s*$/) {
352: $viewgrades = &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'}.
353: '/'.$ENV{'request.course.sec'});
354: }
355: $Apache::lonhomework::viewgrades = $viewgrades;
1.52 albertel 356: return ''
1.41 albertel 357: }
358:
359: sub setupheader {
1.120 albertel 360: my $request=$_[0];
361: if ($ENV{'browser.mathml'}) {
362: $request->content_type('text/xml');
363: } else {
364: $request->content_type('text/html');
365: }
366: $request->content_encoding('UTF-8');
367: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
368: &Apache::loncommon::no_cache($request);
369: }
370: $request->send_http_header;
371: return OK if $request->header_only;
372: return ''
1.41 albertel 373: }
1.35 albertel 374:
1.47 albertel 375: sub handle_save_or_undo {
1.52 albertel 376: my ($request,$problem,$result) = @_;
1.70 albertel 377: my $file = &Apache::lonnet::filelocation("",$request->uri);
378: my $filebak =$file.".bak";
379: my $filetmp =$file.".tmp";
1.64 albertel 380: my $error=0;
1.52 albertel 381:
382: if ($ENV{'form.Undo'} eq 'undo') {
1.70 albertel 383: my $error=0;
384: if (!copy($file,$filetmp)) { $error=1; }
385: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
386: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
387: if (!$error) {
388: $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52 albertel 389: } else {
1.70 albertel 390: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64 albertel 391: $error=1;
1.52 albertel 392: }
393: } else {
1.70 albertel 394: my $fs=Apache::File->new(">$filebak");
1.52 albertel 395: if (defined($fs)) {
396: print $fs $$problem;
1.70 albertel 397: $request->print("<b>Making Backup to $filebak</b><br />");
1.52 albertel 398: } else {
1.70 albertel 399: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64 albertel 400: $error=2;
1.52 albertel 401: }
1.70 albertel 402: my $fh=Apache::File->new(">$file");
1.52 albertel 403: if (defined($fh)) {
404: print $fh $$result;
1.70 albertel 405: $request->print("<b>Saving Modifications to $file</b><br />");
1.47 albertel 406: } else {
1.70 albertel 407: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64 albertel 408: $error|=4;
1.47 albertel 409: }
1.52 albertel 410: }
1.64 albertel 411: return $error;
412: }
413:
1.101 albertel 414: sub analyze_header {
415: my ($request) = @_;
1.109 albertel 416: my $result.='<html>
417: <head><title>Analyzing a problem</title></head>
418: <body bgcolor="#FFFFFF">
1.101 albertel 419: <form name="lonhomework" method="POST" action="'.
420: $ENV{'request.uri'}.'">
421: <input type="submit" name="problemmode" value="EditXML" />
422: <input type="submit" name="problemmode" value="Edit" />
423: <hr />
424: <input type="submit" name="submit" value="View" />
425: <hr />
426: List of possible answers:
427: </form>';
428: $request->print($result);
429: $request->rflush();
430: }
431:
1.109 albertel 432: sub analyze_footer {
433: my ($request) = @_;
434: my $result='</body></html>';
435: $request->print($result);
436: $request->rflush();
437: }
438:
1.74 albertel 439: sub analyze {
1.101 albertel 440: my ($request,$file) = @_;
441: &Apache::lonxml::debug("Analyze");
442: my $result;
443: my %overall;
444: my %allparts;
445: my $rndseed=$ENV{'form.rndseed'};
446: &analyze_header($request);
1.114 albertel 447: my %prog_state=
448: &Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
449: 'Getting Problem Variants',
450: $ENV{'form.numtoanalyze'});
1.102 albertel 451: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 452: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
453: 'last problem');
1.101 albertel 454: my $subresult=&Apache::lonnet::ssi($request->uri,
455: ('grade_target' => 'analyze'),
1.130 albertel 456: ('rndseed' => $i+$rndseed));
1.101 albertel 457: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
458: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 459: my @parts;
460: if (defined(@{ $analyze{'parts'} })) {
461: @parts=@{ $analyze{'parts'} };
462: }
1.101 albertel 463: foreach my $part (@parts) {
464: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 465: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
466: $analyze{$part.'.type'} eq 'stringresponse' ||
467: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.101 albertel 468: push( @{ $overall{$part.'.answer'} },
469: [@{ $analyze{$part.'.answer'} }]);
470: }
471: }
472: }
1.114 albertel 473: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
474: 'Analyzing Results');
1.134 albertel 475: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 476: if (defined(@{ $overall{$part.'.answer'} })) {
1.132 albertel 477: my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
478: $request->print('<table><tr><td colspan="'.($num_cols+1).'">Part '.$part.'</td></tr>');
1.130 albertel 479: my %frequency;
1.109 albertel 480: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
1.132 albertel 481: $frequency{join("\0",@{ $answer })}++;
1.130 albertel 482: }
1.132 albertel 483: $request->print('<tr><td colspan="'.($num_cols).'">Answer</td><td>Frequency</td></tr>');
484: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
485: $request->print('<tr><td align="right">'.
486: join('</td><td align="right">',split("\0",$answer)).
1.130 albertel 487: '</td><td>('.$frequency{$answer}.
488: ')</td></tr>');
1.109 albertel 489: }
490: $request->print('</table>');
491: } else {
492: $request->print('<p>Part '.$part.
1.130 albertel 493: ' is not analyzable at this time</p>');
1.101 albertel 494: }
495: }
1.130 albertel 496: if (scalar(keys(%allparts)) == 0 ) {
497: $request->print('<p>Found no analyzable parts in this problem,
498: currently only Numerical, Formula and String response
499: styles are supported.</p>');
500: }
1.114 albertel 501: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 502: &analyze_footer($request);
1.101 albertel 503: &Apache::lonhomework::showhash(%overall);
504: return $result;
1.74 albertel 505: }
506:
1.64 albertel 507: sub editxmlmode {
508: my ($request,$file) = @_;
509: my $result;
510: my $problem=&Apache::lonnet::getfile($file);
1.116 albertel 511: if ($problem eq -1) {
1.64 albertel 512: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
513: $problem='';
514: }
515: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
516: my $error=&handle_save_or_undo($request,\$problem,
517: \$ENV{'form.editxmltext'});
518: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
519: }
1.80 albertel 520: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
521: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
522: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
523: $ENV{'form.problemmode'}='View';
524: &renderpage($request,$file);
525: } else {
526: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.136 bowersj2 527: my $xml_help = Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
528: "Problem Editing Help");
1.80 albertel 529: if ($cols > 80) { $cols = 80; }
1.89 albertel 530: if ($cols < 70) { $cols = 70; }
531: if ($rows < 20) { $rows = 20; }
1.80 albertel 532: $result.='<html><body bgcolor="#FFFFFF">
1.64 albertel 533: <form name="lonhomework" method="POST" action="'.
534: $ENV{'request.uri'}.'">
535: <input type="hidden" name="problemmode" value="EditXML" />
1.80 albertel 536: <input type="submit" name="problemmode" value="Discard Edits and View" />
1.64 albertel 537: <input type="submit" name="problemmode" value="Edit" />
538: <hr />
539: <input type="submit" name="submit" value="Submit Changes" />
1.80 albertel 540: <input type="submit" name="submit" value="Submit Changes and View" />
1.64 albertel 541: <input type="submit" name="Undo" value="undo" />
542: <hr />
1.110 albertel 543: ' . $xml_help . '
1.64 albertel 544: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73 albertel 545: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 546: </form></body></html>';
1.80 albertel 547: $request->print($result);
548: }
1.64 albertel 549: return '';
1.47 albertel 550: }
551:
1.41 albertel 552: sub renderpage {
1.52 albertel 553: my ($request,$file) = @_;
554:
555: my (@targets) = &get_target();
1.70 albertel 556: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52 albertel 557: foreach my $target (@targets) {
558: #my $t0 = [&gettimeofday()];
559: my $problem=&Apache::lonnet::getfile($file);
1.117 albertel 560: if ($problem eq -1) {
1.52 albertel 561: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
562: $problem='';
563: }
564:
565: my %mystyle;
566: my $result = '';
1.109 albertel 567: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1.85 albertel 568: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
569: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
1.52 albertel 570:
1.70 albertel 571: &Apache::lonxml::debug("Should be parsing now");
1.78 albertel 572: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
1.98 albertel 573: &setup_vars($target),%mystyle);
1.113 albertel 574: undef($Apache::lonhomework::parsing_a_problem);
1.52 albertel 575: #$request->print("Result follows:");
576: if ($target eq 'modified') {
577: &handle_save_or_undo($request,\$problem,\$result);
578: } else {
1.74 albertel 579: if ($target eq 'analyze') {
580: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75 albertel 581: undef(%Apache::lonhomework::analyze);
1.74 albertel 582: }
1.52 albertel 583: #my $td=&tv_interval($t0);
584: #if ( $Apache::lonxml::debug) {
585: #$result =~ s:</body>::;
586: #$result.="<br />Spent $td seconds processing target $target\n</body>";
587: #}
588: $request->print($result);
1.112 albertel 589: $request->rflush();
1.52 albertel 590: }
591: #$request->print(":Result ends");
592: #my $td=&tv_interval($t0);
593: }
1.41 albertel 594: }
595:
1.42 albertel 596: # with no arg it returns a HTML <option> list of the template titles
597: # with one arg it returns the filename associated with the arg passed
598: sub get_template_list {
1.52 albertel 599: my ($namewanted,$extension) = @_;
600: my $result;
1.85 albertel 601: my @allnames;
1.52 albertel 602: &Apache::lonxml::debug("Looking for :$extension:");
1.91 albertel 603: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
1.52 albertel 604: my $name=&Apache::lonnet::metadata($file,'title');
605: if ($namewanted && ($name eq $namewanted)) {
606: $result=$file;
607: last;
608: } else {
1.104 www 609: if ($name) { push (@allnames, $name); }
1.42 albertel 610: }
1.52 albertel 611: }
1.86 albertel 612: if (@allnames && !$result) {
1.115 www 613: $result="<option>Select a $extension template</option>\n<option>".
1.128 albertel 614: join('</option><option>',sort(@allnames)).'</option>';
1.85 albertel 615: }
1.52 albertel 616: return $result;
1.42 albertel 617: }
618:
619: sub newproblem {
1.65 matthew 620: my ($request) = @_;
621: my $extension=$request->uri;
622: $extension=~s:^.*\.([\w]+)$:$1:;
623: &Apache::lonxml::debug("Looking for :$extension:");
1.131 albertel 624: my $templatelist=&get_template_list('',$extension);
1.85 albertel 625: if ($ENV{'form.template'} &&
1.128 albertel 626: $ENV{'form.template'} ne "Select a $extension template") {
1.65 matthew 627: use File::Copy;
628: my $file = &get_template_list($ENV{'form.template'},$extension);
629: my $dest = &Apache::lonnet::filelocation("",$request->uri);
630: copy($file,$dest);
631: &renderpage($request,$dest);
1.131 albertel 632: } elsif($ENV{'form.newfile'} && !$templatelist) {
633: # I don't like hard-coded filenames but for now, this will work.
634: use File::Copy;
635: my $templatefilename =
636: $request->dir_config('lonIncludes').'/templates/blank.problem';
637: &Apache::lonxml::debug("$templatefilename");
638: my $dest = &Apache::lonnet::filelocation("",$request->uri);
639: copy($templatefilename,$dest);
640: &renderpage($request,$dest);
1.85 albertel 641: } else {
1.65 matthew 642: my $url=$request->uri;
643: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 644: my $errormsg;
645: if ($ENV{'form.newfile'}) {
646: $errormsg='<p><font color="red">You did not select a template.</font></p>'."\n";
647: }
1.85 albertel 648: my $instructions;
1.105 www 649: if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
1.65 matthew 650: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 651: <body bgcolor="#FFFFFF">
1.105 www 652: <h1>Creating a new $extension resource</h1>
1.128 albertel 653: $errormsg
1.105 www 654: The requested file <tt>$url</tt> currently does not exist.
655: <p>
1.128 albertel 656: <b>To create a new $extension$instructions click on the "Create $extension" button.</b>
1.105 www 657: </p>
658: <p><form action="$url" method="POST">
1.42 albertel 659: ENDNEWPROBLEM
1.85 albertel 660: if (defined($templatelist)) {
661: $request->print("<select name=\"template\">$templatelist</select>");
662: }
663: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
1.105 www 664: $request->print("</form></p></body>");
1.65 matthew 665: }
666: return '';
1.42 albertel 667: }
668:
669: sub view_or_edit_menu {
1.52 albertel 670: my ($request) = @_;
671: my $url=$request->uri;
672: $request->print(<<EDITMENU);
1.42 albertel 673: <body bgcolor="#FFFFFF">
674: <form action="$url" method="POST">
675: Would you like to <input type="submit" name="problemmode" value="View"> or
676: <input type="submit" name="problemmode" value="Edit"> the problem.
677: </form>
678: </body>
679: EDITMENU
680: }
681:
1.41 albertel 682: sub handler {
1.52 albertel 683: #my $t0 = [&gettimeofday()];
684: my $request=$_[0];
1.41 albertel 685:
1.95 albertel 686: $Apache::lonxml::debug=$ENV{'user.debug'};
1.41 albertel 687:
688: if (&setupheader($request)) { return OK; }
1.52 albertel 689: $ENV{'request.uri'}=$request->uri;
1.41 albertel 690:
691: #setup permissions
1.52 albertel 692: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
693: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
694: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71 albertel 695: # some times multiple problemmodes are submitted, need to select
696: # the last one
1.80 albertel 697: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.72 albertel 698: if ( defined($ENV{'form.problemmode'}) &&
1.77 albertel 699: ref($ENV{'form.problemmode'}) ) {
1.80 albertel 700: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
1.72 albertel 701: my $mode=$ENV{'form.problemmode'}->[-1];
1.71 albertel 702: undef $ENV{'form.problemmode'};
1.72 albertel 703: $ENV{'form.problemmode'}=$mode;
1.71 albertel 704: }
1.64 albertel 705: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52 albertel 706: my $file=&Apache::lonnet::filelocation("",$request->uri);
707:
708: #check if we know where we are
709: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
710: # if we are browsing we might not be able to know where we are
711: if ($Apache::lonhomework::browse ne 'F') {
712: #should know where we are, so ask
713: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 714: }
1.52 albertel 715: }
1.41 albertel 716:
1.118 albertel 717: my ($symb) = &Apache::lonxml::whichuser();
718: &Apache::lonxml::debug('symb is '.$symb);
719: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
720: if ($ENV{'form.resetdata'} eq 'Reset Submissions' ||
1.138 albertel 721: $ENV{'form.resetdata'} eq 'New Problem Variation' ||
722: $ENV{'form.newrandomization'} eq 'New Randomization') {
1.118 albertel 723: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
724: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
1.138 albertel 725: &Apache::lonxml::debug("Attempt reset");
1.118 albertel 726: }
727: }
1.52 albertel 728: if ($ENV{'request.state'} eq "construct") {
729: if ( -e $file ) {
1.135 www 730: &Apache::loncommon::get_unprocessed_cgi
731: ($ENV{'QUERY_STRING'},['problemmode']);
1.52 albertel 732: if (!(defined $ENV{'form.problemmode'})) {
733: #first visit to problem in construction space
1.64 albertel 734: #&view_or_edit_menu($request);
735: $ENV{'form.problemmode'}='View';
736: &renderpage($request,$file);
737: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
738: &editxmlmode($request,$file);
1.130 albertel 739: } elsif ($ENV{'form.problemmode'} eq 'Calculate answers') {
1.74 albertel 740: &analyze($request,$file);
1.52 albertel 741: } else {
742: &renderpage($request,$file);
1.41 albertel 743: }
744: } else {
1.52 albertel 745: # requested file doesn't exist in contruction space
746: &newproblem($request);
1.41 albertel 747: }
1.52 albertel 748: } else {
749: # just render the page normally outside of construction space
1.74 albertel 750: &Apache::lonxml::debug("not construct");
1.52 albertel 751: &renderpage($request,$file);
752: }
753: #my $td=&tv_interval($t0);
754: #&Apache::lonxml::debug("Spent $td seconds processing");
755: # &Apache::lonhomework::send_footer($request);
756: # always turn off debug messages
757: $Apache::lonxml::debug=0;
758: return OK;
759:
1.1 albertel 760: }
761:
762: 1;
763: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>