Annotation of loncom/homework/lonhomework.pm, revision 1.120
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.120 ! albertel 4: # $Id: lonhomework.pm,v 1.119 2003/04/03 21:46:27 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.26 www 53: use Apache::Constants qw(:common);
1.73 albertel 54: use HTML::Entities();
1.83 albertel 55: use Apache::loncommon();
1.47 albertel 56: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 57:
1.69 harris41 58: BEGIN {
1.43 albertel 59: &Apache::lonxml::register_insert();
60: }
61:
1.5 albertel 62: sub get_target {
1.52 albertel 63: if ( $ENV{'request.state'} eq "published") {
1.90 sakharuk 64: if ( defined($ENV{'form.grade_target'} )
65: && ($ENV{'form.grade_target'} eq 'tex')) {
66: return ($ENV{'form.grade_target'});
67: } elsif ( defined($ENV{'form.grade_target'} )
1.119 albertel 68: && ($Apache::lonhomework::viewgrades eq 'F' )) {
1.66 albertel 69: return ($ENV{'form.grade_target'});
70: }
1.90 sakharuk 71:
1.118 albertel 72: if ( defined($ENV{'form.submitted'}) &&
73: ( !defined($ENV{'form.resetdata'}))) {
1.52 albertel 74: return ('grade', 'web');
75: } else {
76: return ('web');
77: }
78: } elsif ($ENV{'request.state'} eq "construct") {
1.74 albertel 79: if ( defined($ENV{'form.grade_target'}) ) {
80: return ($ENV{'form.grade_target'});
81: }
1.62 albertel 82: if ( defined($ENV{'form.preview'})) {
83: if ( defined($ENV{'form.submitted'})) {
1.52 albertel 84: return ('grade', 'web');
85: } else {
86: return ('web');
87: }
88: } else {
1.88 albertel 89: if ( $ENV{'form.problemmode'} eq 'View' ||
90: $ENV{'form.problemmode'} eq 'Discard Edits and View') {
1.62 albertel 91: if ( defined($ENV{'form.submitted'}) &&
92: (!defined($ENV{'form.resetdata'})) ) {
1.60 albertel 93: return ('grade', 'web','answer');
1.42 albertel 94: } else {
1.60 albertel 95: return ('web','answer');
1.42 albertel 96: }
1.52 albertel 97: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
98: if ( $ENV{'form.submitted'} eq 'edit' ) {
1.80 albertel 99: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
1.81 albertel 100: return ('modified','web','answer');
1.80 albertel 101: } else {
102: return ('modified','edit');
103: }
1.42 albertel 104: } else {
1.52 albertel 105: return ('edit');
1.42 albertel 106: }
1.52 albertel 107: } else {
108: return ('web');
109: }
1.15 albertel 110: }
1.52 albertel 111: }
112: return ();
1.5 albertel 113: }
114:
1.3 albertel 115: sub setup_vars {
1.52 albertel 116: my ($target) = @_;
117: return ';'
1.11 albertel 118: # return ';$external::target='.$target.';';
1.2 albertel 119: }
120:
121: sub send_header {
1.52 albertel 122: my ($request)= @_;
123: $request->print(&Apache::lontexconvert::header());
1.16 albertel 124: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 125: }
126:
1.36 albertel 127: sub createmenu {
1.52 albertel 128: my ($which,$request)=@_;
129: if ($which eq 'grade') {
130: $request->print('<script language="JavaScript">
1.91 albertel 131: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52 albertel 132: "height=350,width=150,menubar=no");
133: </script>');
134: }
1.36 albertel 135: }
136:
1.2 albertel 137: sub send_footer {
1.52 albertel 138: my ($request)= @_;
1.16 albertel 139: # $request->print('</form>');
1.52 albertel 140: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 141: }
142:
1.52 albertel 143: $Apache::lonxml::browse='';
1.53 www 144:
1.92 bowersj2 145: # JB, 9/24/2002: Any changes in this function may require a change
146: # in lonnavmaps::resource::getDateStatus.
1.53 www 147: sub check_access {
1.52 albertel 148: my ($id) = @_;
149: my $date ='';
1.103 albertel 150: my $status;
1.52 albertel 151: my $datemsg = '';
152: my $lastdate = '';
153: my $temp;
154: my $type;
155: my $passed;
1.106 albertel 156:
157: if ($ENV{'request.state'} eq "construct") {
158: &Apache::lonxml::debug("in construction ignoring dates");
159: $status='CAN_ANSWER';
160: $datemsg='is in under construction';
161: return ($status,$datemsg);
162: }
163:
1.52 albertel 164: &Apache::lonxml::debug("checking for part :$id:");
1.73 albertel 165: &Apache::lonxml::debug("time:".time);
1.52 albertel 166: foreach $temp ("opendate","duedate","answerdate") {
167: $lastdate = $date;
168: $date = &Apache::lonnet::EXT("resource.$id.$temp");
1.87 www 169: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
1.103 albertel 170: if ($thistype =~ /^(con_lost|no_such_host)/ ||
171: $date =~ /^(con_lost|no_such_host)/) {
172: $status='UNAVAILABLE';
173: $date="may open later.";
174: return($status,$date);
175: }
1.87 www 176: if ($thistype eq 'date_interval') {
177: if ($temp eq 'opendate') {
178: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
179: }
180: if ($temp eq 'answerdate') {
181: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
182: }
183: }
1.52 albertel 184: &Apache::lonxml::debug("found :$date: for :$temp:");
185: if ($date eq '') {
186: $date = "an unknown date"; $passed = 0;
187: } elsif ($date eq 'con_lost') {
188: $date = "an indeterminate date"; $passed = 0;
189: } else {
190: if (time < $date) { $passed = 0; } else { $passed = 1; }
191: $date = localtime $date;
1.51 harris41 192: }
1.52 albertel 193: if (!$passed) { $type=$temp; last; }
194: }
195: &Apache::lonxml::debug("have :$type:$passed:");
196: if ($passed) {
197: $status='SHOW_ANSWER';
198: $datemsg=$date;
199: } elsif ($type eq 'opendate') {
200: $status='CLOSED';
201: $datemsg = "will open on $date";
202: } elsif ($type eq 'duedate') {
203: $status='CAN_ANSWER';
204: $datemsg = "is due at $date";
205: } elsif ($type eq 'answerdate') {
206: $status='CLOSED';
207: $datemsg = "was due on $lastdate, and answers will be available on $date";
208: }
209: if ($status eq 'CAN_ANSWER') {
210: #check #tries
211: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
212: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
213: if ( $tries eq '' ) { $tries = '0'; }
214: if ( $maxtries eq '' ) { $maxtries = '2'; }
215: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
216: }
1.54 www 217:
1.56 www 218: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
219: (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54 www 220: return ('UNCHECKEDOUT','needs to be checked out');
221: }
222:
223:
1.52 albertel 224: &Apache::lonxml::debug("sending back :$status:$datemsg:");
225: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
1.91 albertel 226: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
1.52 albertel 227: $status='CAN_ANSWER';
228: $datemsg='is closed but you are allowed to view it';
229: }
1.106 albertel 230:
1.52 albertel 231: return ($status,$datemsg);
1.20 albertel 232: }
233:
1.41 albertel 234: sub showhash {
1.52 albertel 235: my (%hash) = @_;
1.99 albertel 236: &showhashsubset(\%hash,'.');
1.79 albertel 237: return '';
238: }
239:
1.106 albertel 240: sub showarray {
241: my ($array)=@_;
242: my $string="(";
243: foreach my $elm (@{ $array }) {
244: if (ref($elm)) {
245: if ($elm =~ /ARRAY/ ) {
246: $string.=&showarray($elm);
247: }
248: } else {
249: $string.="$elm,"
250: }
251: }
252: chop($string);
253: $string.=")";
254: return $string;
255: }
256:
1.79 albertel 257: sub showhashsubset {
258: my ($hash,$keyre) = @_;
1.52 albertel 259: my $resultkey;
1.79 albertel 260: foreach $resultkey (sort keys %$hash) {
261: if ($resultkey =~ /$keyre/) {
262: if (ref($$hash{$resultkey})) {
263: if ($$hash{$resultkey} =~ /ARRAY/ ) {
1.106 albertel 264: &Apache::lonxml::debug("$resultkey ---- ".
265: &showarray($$hash{$resultkey}));
266: } elsif ($$hash{$resultkey} =~ /HASH/ ) {
267: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
268: &showhashsubset($$hash{$resultkey},'.');
1.79 albertel 269: } else {
1.106 albertel 270: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 271: }
272: } else {
1.79 albertel 273: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 274: }
275: }
1.52 albertel 276: }
277: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
278: return '';
1.41 albertel 279: }
280:
281: sub setuppermissions {
1.52 albertel 282: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
283: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
284: return ''
1.41 albertel 285: }
286:
287: sub setupheader {
1.120 ! albertel 288: my $request=$_[0];
! 289: if ($ENV{'browser.mathml'}) {
! 290: $request->content_type('text/xml');
! 291: } else {
! 292: $request->content_type('text/html');
! 293: }
! 294: $request->content_encoding('UTF-8');
! 295: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
! 296: &Apache::loncommon::no_cache($request);
! 297: }
! 298: $request->send_http_header;
! 299: return OK if $request->header_only;
! 300: return ''
1.41 albertel 301: }
1.35 albertel 302:
1.47 albertel 303: sub handle_save_or_undo {
1.52 albertel 304: my ($request,$problem,$result) = @_;
1.70 albertel 305: my $file = &Apache::lonnet::filelocation("",$request->uri);
306: my $filebak =$file.".bak";
307: my $filetmp =$file.".tmp";
1.64 albertel 308: my $error=0;
1.52 albertel 309:
310: if ($ENV{'form.Undo'} eq 'undo') {
1.70 albertel 311: my $error=0;
312: if (!copy($file,$filetmp)) { $error=1; }
313: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
314: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
315: if (!$error) {
316: $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52 albertel 317: } else {
1.70 albertel 318: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64 albertel 319: $error=1;
1.52 albertel 320: }
321: } else {
1.70 albertel 322: my $fs=Apache::File->new(">$filebak");
1.52 albertel 323: if (defined($fs)) {
324: print $fs $$problem;
1.70 albertel 325: $request->print("<b>Making Backup to $filebak</b><br />");
1.52 albertel 326: } else {
1.70 albertel 327: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64 albertel 328: $error=2;
1.52 albertel 329: }
1.70 albertel 330: my $fh=Apache::File->new(">$file");
1.52 albertel 331: if (defined($fh)) {
332: print $fh $$result;
1.70 albertel 333: $request->print("<b>Saving Modifications to $file</b><br />");
1.47 albertel 334: } else {
1.70 albertel 335: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64 albertel 336: $error|=4;
1.47 albertel 337: }
1.52 albertel 338: }
1.64 albertel 339: return $error;
340: }
341:
1.101 albertel 342: sub analyze_header {
343: my ($request) = @_;
1.109 albertel 344: my $result.='<html>
345: <head><title>Analyzing a problem</title></head>
346: <body bgcolor="#FFFFFF">
1.101 albertel 347: <form name="lonhomework" method="POST" action="'.
348: $ENV{'request.uri'}.'">
349: <input type="submit" name="problemmode" value="EditXML" />
350: <input type="submit" name="problemmode" value="Edit" />
351: <hr />
352: <input type="submit" name="submit" value="View" />
353: <hr />
354: List of possible answers:
355: </form>';
356: $request->print($result);
357: $request->rflush();
358: }
359:
1.109 albertel 360: sub analyze_footer {
361: my ($request) = @_;
362: my $result='</body></html>';
363: $request->print($result);
364: $request->rflush();
365: }
366:
1.74 albertel 367: sub analyze {
1.101 albertel 368: my ($request,$file) = @_;
369: &Apache::lonxml::debug("Analyze");
370: my $result;
371: my %overall;
372: my %allparts;
373: my $rndseed=$ENV{'form.rndseed'};
374: &analyze_header($request);
1.114 albertel 375: my %prog_state=
376: &Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
377: 'Getting Problem Variants',
378: $ENV{'form.numtoanalyze'});
1.102 albertel 379: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 380: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
381: 'last problem');
1.101 albertel 382: my $subresult=&Apache::lonnet::ssi($request->uri,
383: ('grade_target' => 'analyze'),
1.102 albertel 384: ('rndseed' => $i));
385: &Apache::lonxml::debug(":$subresult:");
1.101 albertel 386: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
387: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 388: my @parts;
389: if (defined(@{ $analyze{'parts'} })) {
390: @parts=@{ $analyze{'parts'} };
391: }
1.101 albertel 392: foreach my $part (@parts) {
393: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 394: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
395: $analyze{$part.'.type'} eq 'stringresponse' ||
396: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.101 albertel 397: push( @{ $overall{$part.'.answer'} },
398: [@{ $analyze{$part.'.answer'} }]);
399: }
400: }
401: }
1.114 albertel 402: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
403: 'Analyzing Results');
1.101 albertel 404: foreach my $part (keys(%allparts)) {
1.109 albertel 405: if (defined(@{ $overall{$part.'.answer'} })) {
406: $request->print('<table><tr><td>Part '.$part.'</td></tr>');
407: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
408: $request->print('<tr><td>'.join('</td><td>',@{ $answer }).
409: '</td></tr>');
410: }
411: $request->print('</table>');
412: } else {
413: $request->print('<p>Part '.$part.
414: ' is not analyzabale at this time</p>');
1.101 albertel 415: }
416: }
1.114 albertel 417: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 418: &analyze_footer($request);
1.101 albertel 419: &Apache::lonhomework::showhash(%overall);
420: return $result;
1.74 albertel 421: }
422:
1.64 albertel 423: sub editxmlmode {
424: my ($request,$file) = @_;
425: my $result;
426: my $problem=&Apache::lonnet::getfile($file);
1.116 albertel 427: if ($problem eq -1) {
1.64 albertel 428: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
429: $problem='';
430: }
431: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
432: my $error=&handle_save_or_undo($request,\$problem,
433: \$ENV{'form.editxmltext'});
434: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
435: }
1.80 albertel 436: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
437: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
438: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
439: $ENV{'form.problemmode'}='View';
440: &renderpage($request,$file);
441: } else {
442: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.111 albertel 443: my $xml_help = '<table><tr><td>'.
444: &Apache::loncommon::help_open_topic("Problem_Editor_XML_Index",'Problem Editing Help')
445: .'</td><td>'.
446: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
447: undef,undef,600)
448: .'</td><td>'.
449: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
450: undef,undef,600)
451: .'</td></tr></table>';
1.80 albertel 452: if ($cols > 80) { $cols = 80; }
1.89 albertel 453: if ($cols < 70) { $cols = 70; }
454: if ($rows < 20) { $rows = 20; }
1.80 albertel 455: $result.='<html><body bgcolor="#FFFFFF">
1.64 albertel 456: <form name="lonhomework" method="POST" action="'.
457: $ENV{'request.uri'}.'">
458: <input type="hidden" name="problemmode" value="EditXML" />
1.80 albertel 459: <input type="submit" name="problemmode" value="Discard Edits and View" />
1.64 albertel 460: <input type="submit" name="problemmode" value="Edit" />
461: <hr />
462: <input type="submit" name="submit" value="Submit Changes" />
1.80 albertel 463: <input type="submit" name="submit" value="Submit Changes and View" />
1.64 albertel 464: <input type="submit" name="Undo" value="undo" />
465: <hr />
1.110 albertel 466: ' . $xml_help . '
1.64 albertel 467: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73 albertel 468: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 469: </form></body></html>';
1.80 albertel 470: $request->print($result);
471: }
1.64 albertel 472: return '';
1.47 albertel 473: }
474:
1.41 albertel 475: sub renderpage {
1.52 albertel 476: my ($request,$file) = @_;
477:
478: my (@targets) = &get_target();
1.70 albertel 479: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52 albertel 480: foreach my $target (@targets) {
481: #my $t0 = [&gettimeofday()];
482: my $problem=&Apache::lonnet::getfile($file);
1.117 albertel 483: if ($problem eq -1) {
1.52 albertel 484: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
485: $problem='';
486: }
487:
488: my %mystyle;
489: my $result = '';
1.109 albertel 490: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1.85 albertel 491: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
492: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
1.52 albertel 493:
1.70 albertel 494: &Apache::lonxml::debug("Should be parsing now");
1.78 albertel 495: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
1.98 albertel 496: &setup_vars($target),%mystyle);
1.113 albertel 497: undef($Apache::lonhomework::parsing_a_problem);
1.52 albertel 498: #$request->print("Result follows:");
499: if ($target eq 'modified') {
500: &handle_save_or_undo($request,\$problem,\$result);
501: } else {
1.74 albertel 502: if ($target eq 'analyze') {
503: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75 albertel 504: undef(%Apache::lonhomework::analyze);
1.74 albertel 505: }
1.52 albertel 506: #my $td=&tv_interval($t0);
507: #if ( $Apache::lonxml::debug) {
508: #$result =~ s:</body>::;
509: #$result.="<br />Spent $td seconds processing target $target\n</body>";
510: #}
511: $request->print($result);
1.112 albertel 512: $request->rflush();
1.52 albertel 513: }
514: #$request->print(":Result ends");
515: #my $td=&tv_interval($t0);
516: }
1.41 albertel 517: }
518:
1.42 albertel 519: # with no arg it returns a HTML <option> list of the template titles
520: # with one arg it returns the filename associated with the arg passed
521: sub get_template_list {
1.52 albertel 522: my ($namewanted,$extension) = @_;
523: my $result;
1.85 albertel 524: my @allnames;
1.52 albertel 525: &Apache::lonxml::debug("Looking for :$extension:");
1.91 albertel 526: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
1.52 albertel 527: my $name=&Apache::lonnet::metadata($file,'title');
528: if ($namewanted && ($name eq $namewanted)) {
529: $result=$file;
530: last;
531: } else {
1.104 www 532: if ($name) { push (@allnames, $name); }
1.42 albertel 533: }
1.52 albertel 534: }
1.86 albertel 535: if (@allnames && !$result) {
1.115 www 536: $result="<option>Select a $extension template</option>\n<option>".
1.85 albertel 537: join('</option><option>',sort(@allnames)).'</option>';
538: }
1.52 albertel 539: return $result;
1.42 albertel 540: }
541:
542: sub newproblem {
1.65 matthew 543: my ($request) = @_;
544: my $extension=$request->uri;
545: $extension=~s:^.*\.([\w]+)$:$1:;
546: &Apache::lonxml::debug("Looking for :$extension:");
1.85 albertel 547: if ($ENV{'form.template'} &&
548: $ENV{'form.template'} ne "Select a $extension type") {
1.65 matthew 549: use File::Copy;
550: my $file = &get_template_list($ENV{'form.template'},$extension);
551: my $dest = &Apache::lonnet::filelocation("",$request->uri);
552: copy($file,$dest);
553: &renderpage($request,$dest);
554: } elsif($ENV{'form.newfile'}) {
555: # I don't like hard-coded filenames but for now, this will work.
556: use File::Copy;
557: my $templatefilename =
1.66 albertel 558: $request->dir_config('lonIncludes').'/templates/blank.problem';
559: &Apache::lonxml::debug("$templatefilename");
1.65 matthew 560: my $dest = &Apache::lonnet::filelocation("",$request->uri);
561: copy($templatefilename,$dest);
562: &renderpage($request,$dest);
1.85 albertel 563: } else {
1.65 matthew 564: my $templatelist=&get_template_list('',$extension);
565: my $url=$request->uri;
566: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.85 albertel 567: my $instructions;
1.105 www 568: if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
1.65 matthew 569: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 570: <body bgcolor="#FFFFFF">
1.105 www 571: <h1>Creating a new $extension resource</h1>
572: The requested file <tt>$url</tt> currently does not exist.
573: <p>
574: To create a new $extension$instructions click on the "Create $extension" button.
575: </p>
576: <p><form action="$url" method="POST">
1.42 albertel 577: ENDNEWPROBLEM
1.85 albertel 578: if (defined($templatelist)) {
579: $request->print("<select name=\"template\">$templatelist</select>");
580: }
581: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
1.105 www 582: $request->print("</form></p></body>");
1.65 matthew 583: }
584: return '';
1.42 albertel 585: }
586:
587: sub view_or_edit_menu {
1.52 albertel 588: my ($request) = @_;
589: my $url=$request->uri;
590: $request->print(<<EDITMENU);
1.42 albertel 591: <body bgcolor="#FFFFFF">
592: <form action="$url" method="POST">
593: Would you like to <input type="submit" name="problemmode" value="View"> or
594: <input type="submit" name="problemmode" value="Edit"> the problem.
595: </form>
596: </body>
597: EDITMENU
598: }
599:
1.41 albertel 600: sub handler {
1.52 albertel 601: #my $t0 = [&gettimeofday()];
602: my $request=$_[0];
1.41 albertel 603:
1.95 albertel 604: $Apache::lonxml::debug=$ENV{'user.debug'};
1.41 albertel 605:
606: if (&setupheader($request)) { return OK; }
1.52 albertel 607: $ENV{'request.uri'}=$request->uri;
1.41 albertel 608:
609: #setup permissions
1.52 albertel 610: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
611: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
612: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71 albertel 613: # some times multiple problemmodes are submitted, need to select
614: # the last one
1.80 albertel 615: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.72 albertel 616: if ( defined($ENV{'form.problemmode'}) &&
1.77 albertel 617: ref($ENV{'form.problemmode'}) ) {
1.80 albertel 618: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
1.72 albertel 619: my $mode=$ENV{'form.problemmode'}->[-1];
1.71 albertel 620: undef $ENV{'form.problemmode'};
1.72 albertel 621: $ENV{'form.problemmode'}=$mode;
1.71 albertel 622: }
1.64 albertel 623: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52 albertel 624: my $file=&Apache::lonnet::filelocation("",$request->uri);
625:
626: #check if we know where we are
627: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
628: # if we are browsing we might not be able to know where we are
629: if ($Apache::lonhomework::browse ne 'F') {
630: #should know where we are, so ask
631: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 632: }
1.52 albertel 633: }
1.41 albertel 634:
1.118 albertel 635: my ($symb) = &Apache::lonxml::whichuser();
636: &Apache::lonxml::debug('symb is '.$symb);
637: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
638: if ($ENV{'form.resetdata'} eq 'Reset Submissions' ||
639: $ENV{'form.resetdata'} eq 'New Problem Variation' ) {
640: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
641: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
642: }
643: }
1.52 albertel 644: if ($ENV{'request.state'} eq "construct") {
645: if ( -e $file ) {
646: if (!(defined $ENV{'form.problemmode'})) {
647: #first visit to problem in construction space
1.64 albertel 648: #&view_or_edit_menu($request);
649: $ENV{'form.problemmode'}='View';
650: &renderpage($request,$file);
651: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
652: &editxmlmode($request,$file);
1.74 albertel 653: } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
654: &analyze($request,$file);
1.52 albertel 655: } else {
656: &renderpage($request,$file);
1.41 albertel 657: }
658: } else {
1.52 albertel 659: # requested file doesn't exist in contruction space
660: &newproblem($request);
1.41 albertel 661: }
1.52 albertel 662: } else {
663: # just render the page normally outside of construction space
1.74 albertel 664: &Apache::lonxml::debug("not construct");
1.52 albertel 665: &renderpage($request,$file);
666: }
667: #my $td=&tv_interval($t0);
668: #&Apache::lonxml::debug("Spent $td seconds processing");
669: # &Apache::lonhomework::send_footer($request);
670: # always turn off debug messages
671: $Apache::lonxml::debug=0;
672: return OK;
673:
1.1 albertel 674: }
675:
676: 1;
677: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>