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