Annotation of loncom/homework/lonhomework.pm, revision 1.122
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.122 ! albertel 4: # $Id: lonhomework.pm,v 1.121 2003/05/08 15:33:15 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.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.52 albertel 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.121 albertel 217: if($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/) {
218: $status = 'CANNOT_ANSWER';
219: }
1.52 albertel 220: }
1.54 www 221:
1.56 www 222: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
223: (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54 www 224: return ('UNCHECKEDOUT','needs to be checked out');
225: }
226:
227:
1.52 albertel 228: &Apache::lonxml::debug("sending back :$status:$datemsg:");
229: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
1.91 albertel 230: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
1.52 albertel 231: $status='CAN_ANSWER';
232: $datemsg='is closed but you are allowed to view it';
233: }
1.106 albertel 234:
1.52 albertel 235: return ($status,$datemsg);
1.20 albertel 236: }
237:
1.41 albertel 238: sub showhash {
1.52 albertel 239: my (%hash) = @_;
1.99 albertel 240: &showhashsubset(\%hash,'.');
1.79 albertel 241: return '';
242: }
243:
1.106 albertel 244: sub showarray {
245: my ($array)=@_;
246: my $string="(";
247: foreach my $elm (@{ $array }) {
248: if (ref($elm)) {
249: if ($elm =~ /ARRAY/ ) {
250: $string.=&showarray($elm);
251: }
252: } else {
253: $string.="$elm,"
254: }
255: }
256: chop($string);
257: $string.=")";
258: return $string;
259: }
260:
1.79 albertel 261: sub showhashsubset {
262: my ($hash,$keyre) = @_;
1.52 albertel 263: my $resultkey;
1.79 albertel 264: foreach $resultkey (sort keys %$hash) {
265: if ($resultkey =~ /$keyre/) {
266: if (ref($$hash{$resultkey})) {
267: if ($$hash{$resultkey} =~ /ARRAY/ ) {
1.106 albertel 268: &Apache::lonxml::debug("$resultkey ---- ".
269: &showarray($$hash{$resultkey}));
270: } elsif ($$hash{$resultkey} =~ /HASH/ ) {
271: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
272: &showhashsubset($$hash{$resultkey},'.');
1.79 albertel 273: } else {
1.106 albertel 274: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 275: }
276: } else {
1.79 albertel 277: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73 albertel 278: }
279: }
1.52 albertel 280: }
281: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
282: return '';
1.41 albertel 283: }
284:
285: sub setuppermissions {
1.52 albertel 286: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
287: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
288: return ''
1.41 albertel 289: }
290:
291: sub setupheader {
1.120 albertel 292: my $request=$_[0];
293: if ($ENV{'browser.mathml'}) {
294: $request->content_type('text/xml');
295: } else {
296: $request->content_type('text/html');
297: }
298: $request->content_encoding('UTF-8');
299: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
300: &Apache::loncommon::no_cache($request);
301: }
302: $request->send_http_header;
303: return OK if $request->header_only;
304: return ''
1.41 albertel 305: }
1.35 albertel 306:
1.47 albertel 307: sub handle_save_or_undo {
1.52 albertel 308: my ($request,$problem,$result) = @_;
1.70 albertel 309: my $file = &Apache::lonnet::filelocation("",$request->uri);
310: my $filebak =$file.".bak";
311: my $filetmp =$file.".tmp";
1.64 albertel 312: my $error=0;
1.52 albertel 313:
314: if ($ENV{'form.Undo'} eq 'undo') {
1.70 albertel 315: my $error=0;
316: if (!copy($file,$filetmp)) { $error=1; }
317: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
318: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
319: if (!$error) {
320: $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52 albertel 321: } else {
1.70 albertel 322: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64 albertel 323: $error=1;
1.52 albertel 324: }
325: } else {
1.70 albertel 326: my $fs=Apache::File->new(">$filebak");
1.52 albertel 327: if (defined($fs)) {
328: print $fs $$problem;
1.70 albertel 329: $request->print("<b>Making Backup to $filebak</b><br />");
1.52 albertel 330: } else {
1.70 albertel 331: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64 albertel 332: $error=2;
1.52 albertel 333: }
1.70 albertel 334: my $fh=Apache::File->new(">$file");
1.52 albertel 335: if (defined($fh)) {
336: print $fh $$result;
1.70 albertel 337: $request->print("<b>Saving Modifications to $file</b><br />");
1.47 albertel 338: } else {
1.70 albertel 339: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64 albertel 340: $error|=4;
1.47 albertel 341: }
1.52 albertel 342: }
1.64 albertel 343: return $error;
344: }
345:
1.101 albertel 346: sub analyze_header {
347: my ($request) = @_;
1.109 albertel 348: my $result.='<html>
349: <head><title>Analyzing a problem</title></head>
350: <body bgcolor="#FFFFFF">
1.101 albertel 351: <form name="lonhomework" method="POST" action="'.
352: $ENV{'request.uri'}.'">
353: <input type="submit" name="problemmode" value="EditXML" />
354: <input type="submit" name="problemmode" value="Edit" />
355: <hr />
356: <input type="submit" name="submit" value="View" />
357: <hr />
358: List of possible answers:
359: </form>';
360: $request->print($result);
361: $request->rflush();
362: }
363:
1.109 albertel 364: sub analyze_footer {
365: my ($request) = @_;
366: my $result='</body></html>';
367: $request->print($result);
368: $request->rflush();
369: }
370:
1.74 albertel 371: sub analyze {
1.101 albertel 372: my ($request,$file) = @_;
373: &Apache::lonxml::debug("Analyze");
374: my $result;
375: my %overall;
376: my %allparts;
377: my $rndseed=$ENV{'form.rndseed'};
378: &analyze_header($request);
1.114 albertel 379: my %prog_state=
380: &Apache::lonhtmlcommon::Create_PrgWin($request,'Analyze Progress',
381: 'Getting Problem Variants',
382: $ENV{'form.numtoanalyze'});
1.102 albertel 383: for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 384: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
385: 'last problem');
1.101 albertel 386: my $subresult=&Apache::lonnet::ssi($request->uri,
387: ('grade_target' => 'analyze'),
1.102 albertel 388: ('rndseed' => $i));
389: &Apache::lonxml::debug(":$subresult:");
1.101 albertel 390: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
391: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 392: my @parts;
393: if (defined(@{ $analyze{'parts'} })) {
394: @parts=@{ $analyze{'parts'} };
395: }
1.101 albertel 396: foreach my $part (@parts) {
397: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 398: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
399: $analyze{$part.'.type'} eq 'stringresponse' ||
400: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.101 albertel 401: push( @{ $overall{$part.'.answer'} },
402: [@{ $analyze{$part.'.answer'} }]);
403: }
404: }
405: }
1.114 albertel 406: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
407: 'Analyzing Results');
1.101 albertel 408: foreach my $part (keys(%allparts)) {
1.109 albertel 409: if (defined(@{ $overall{$part.'.answer'} })) {
410: $request->print('<table><tr><td>Part '.$part.'</td></tr>');
411: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
412: $request->print('<tr><td>'.join('</td><td>',@{ $answer }).
413: '</td></tr>');
414: }
415: $request->print('</table>');
416: } else {
417: $request->print('<p>Part '.$part.
418: ' is not analyzabale at this time</p>');
1.101 albertel 419: }
420: }
1.114 albertel 421: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 422: &analyze_footer($request);
1.101 albertel 423: &Apache::lonhomework::showhash(%overall);
424: return $result;
1.74 albertel 425: }
426:
1.64 albertel 427: sub editxmlmode {
428: my ($request,$file) = @_;
429: my $result;
430: my $problem=&Apache::lonnet::getfile($file);
1.116 albertel 431: if ($problem eq -1) {
1.64 albertel 432: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
433: $problem='';
434: }
435: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
436: my $error=&handle_save_or_undo($request,\$problem,
437: \$ENV{'form.editxmltext'});
438: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
439: }
1.80 albertel 440: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
441: if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
442: &Apache::lonhomework::showhashsubset(\%ENV,'^form');
443: $ENV{'form.problemmode'}='View';
444: &renderpage($request,$file);
445: } else {
446: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.111 albertel 447: my $xml_help = '<table><tr><td>'.
448: &Apache::loncommon::help_open_topic("Problem_Editor_XML_Index",'Problem Editing Help')
449: .'</td><td>'.
450: &Apache::loncommon::help_open_topic("Greek_Symbols",'Greek Symbols',
451: undef,undef,600)
452: .'</td><td>'.
453: &Apache::loncommon::help_open_topic("Other_Symbols",'Other Symbols',
454: undef,undef,600)
455: .'</td></tr></table>';
1.80 albertel 456: if ($cols > 80) { $cols = 80; }
1.89 albertel 457: if ($cols < 70) { $cols = 70; }
458: if ($rows < 20) { $rows = 20; }
1.80 albertel 459: $result.='<html><body bgcolor="#FFFFFF">
1.64 albertel 460: <form name="lonhomework" method="POST" action="'.
461: $ENV{'request.uri'}.'">
462: <input type="hidden" name="problemmode" value="EditXML" />
1.80 albertel 463: <input type="submit" name="problemmode" value="Discard Edits and View" />
1.64 albertel 464: <input type="submit" name="problemmode" value="Edit" />
465: <hr />
466: <input type="submit" name="submit" value="Submit Changes" />
1.80 albertel 467: <input type="submit" name="submit" value="Submit Changes and View" />
1.64 albertel 468: <input type="submit" name="Undo" value="undo" />
469: <hr />
1.110 albertel 470: ' . $xml_help . '
1.64 albertel 471: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73 albertel 472: &HTML::Entities::encode($problem).'</textarea>
1.64 albertel 473: </form></body></html>';
1.80 albertel 474: $request->print($result);
475: }
1.64 albertel 476: return '';
1.47 albertel 477: }
478:
1.41 albertel 479: sub renderpage {
1.52 albertel 480: my ($request,$file) = @_;
481:
482: my (@targets) = &get_target();
1.70 albertel 483: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52 albertel 484: foreach my $target (@targets) {
485: #my $t0 = [&gettimeofday()];
486: my $problem=&Apache::lonnet::getfile($file);
1.117 albertel 487: if ($problem eq -1) {
1.52 albertel 488: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
489: $problem='';
490: }
491:
492: my %mystyle;
493: my $result = '';
1.109 albertel 494: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1.85 albertel 495: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
496: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
1.52 albertel 497:
1.70 albertel 498: &Apache::lonxml::debug("Should be parsing now");
1.78 albertel 499: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
1.98 albertel 500: &setup_vars($target),%mystyle);
1.113 albertel 501: undef($Apache::lonhomework::parsing_a_problem);
1.52 albertel 502: #$request->print("Result follows:");
503: if ($target eq 'modified') {
504: &handle_save_or_undo($request,\$problem,\$result);
505: } else {
1.74 albertel 506: if ($target eq 'analyze') {
507: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75 albertel 508: undef(%Apache::lonhomework::analyze);
1.74 albertel 509: }
1.52 albertel 510: #my $td=&tv_interval($t0);
511: #if ( $Apache::lonxml::debug) {
512: #$result =~ s:</body>::;
513: #$result.="<br />Spent $td seconds processing target $target\n</body>";
514: #}
515: $request->print($result);
1.112 albertel 516: $request->rflush();
1.52 albertel 517: }
518: #$request->print(":Result ends");
519: #my $td=&tv_interval($t0);
520: }
1.41 albertel 521: }
522:
1.42 albertel 523: # with no arg it returns a HTML <option> list of the template titles
524: # with one arg it returns the filename associated with the arg passed
525: sub get_template_list {
1.52 albertel 526: my ($namewanted,$extension) = @_;
527: my $result;
1.85 albertel 528: my @allnames;
1.52 albertel 529: &Apache::lonxml::debug("Looking for :$extension:");
1.91 albertel 530: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
1.52 albertel 531: my $name=&Apache::lonnet::metadata($file,'title');
532: if ($namewanted && ($name eq $namewanted)) {
533: $result=$file;
534: last;
535: } else {
1.104 www 536: if ($name) { push (@allnames, $name); }
1.42 albertel 537: }
1.52 albertel 538: }
1.86 albertel 539: if (@allnames && !$result) {
1.115 www 540: $result="<option>Select a $extension template</option>\n<option>".
1.85 albertel 541: join('</option><option>',sort(@allnames)).'</option>';
542: }
1.52 albertel 543: return $result;
1.42 albertel 544: }
545:
546: sub newproblem {
1.65 matthew 547: my ($request) = @_;
548: my $extension=$request->uri;
549: $extension=~s:^.*\.([\w]+)$:$1:;
550: &Apache::lonxml::debug("Looking for :$extension:");
1.85 albertel 551: if ($ENV{'form.template'} &&
552: $ENV{'form.template'} ne "Select a $extension type") {
1.65 matthew 553: use File::Copy;
554: my $file = &get_template_list($ENV{'form.template'},$extension);
555: my $dest = &Apache::lonnet::filelocation("",$request->uri);
556: copy($file,$dest);
557: &renderpage($request,$dest);
558: } elsif($ENV{'form.newfile'}) {
559: # I don't like hard-coded filenames but for now, this will work.
560: use File::Copy;
561: my $templatefilename =
1.66 albertel 562: $request->dir_config('lonIncludes').'/templates/blank.problem';
563: &Apache::lonxml::debug("$templatefilename");
1.65 matthew 564: my $dest = &Apache::lonnet::filelocation("",$request->uri);
565: copy($templatefilename,$dest);
566: &renderpage($request,$dest);
1.85 albertel 567: } else {
1.65 matthew 568: my $templatelist=&get_template_list('',$extension);
569: my $url=$request->uri;
570: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.85 albertel 571: my $instructions;
1.105 www 572: if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
1.65 matthew 573: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 574: <body bgcolor="#FFFFFF">
1.105 www 575: <h1>Creating a new $extension resource</h1>
576: The requested file <tt>$url</tt> currently does not exist.
577: <p>
578: To create a new $extension$instructions click on the "Create $extension" button.
579: </p>
580: <p><form action="$url" method="POST">
1.42 albertel 581: ENDNEWPROBLEM
1.85 albertel 582: if (defined($templatelist)) {
583: $request->print("<select name=\"template\">$templatelist</select>");
584: }
585: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
1.105 www 586: $request->print("</form></p></body>");
1.65 matthew 587: }
588: return '';
1.42 albertel 589: }
590:
591: sub view_or_edit_menu {
1.52 albertel 592: my ($request) = @_;
593: my $url=$request->uri;
594: $request->print(<<EDITMENU);
1.42 albertel 595: <body bgcolor="#FFFFFF">
596: <form action="$url" method="POST">
597: Would you like to <input type="submit" name="problemmode" value="View"> or
598: <input type="submit" name="problemmode" value="Edit"> the problem.
599: </form>
600: </body>
601: EDITMENU
602: }
603:
1.41 albertel 604: sub handler {
1.52 albertel 605: #my $t0 = [&gettimeofday()];
606: my $request=$_[0];
1.41 albertel 607:
1.95 albertel 608: $Apache::lonxml::debug=$ENV{'user.debug'};
1.41 albertel 609:
610: if (&setupheader($request)) { return OK; }
1.52 albertel 611: $ENV{'request.uri'}=$request->uri;
1.41 albertel 612:
613: #setup permissions
1.52 albertel 614: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
615: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
616: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71 albertel 617: # some times multiple problemmodes are submitted, need to select
618: # the last one
1.80 albertel 619: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.72 albertel 620: if ( defined($ENV{'form.problemmode'}) &&
1.77 albertel 621: ref($ENV{'form.problemmode'}) ) {
1.80 albertel 622: &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
1.72 albertel 623: my $mode=$ENV{'form.problemmode'}->[-1];
1.71 albertel 624: undef $ENV{'form.problemmode'};
1.72 albertel 625: $ENV{'form.problemmode'}=$mode;
1.71 albertel 626: }
1.64 albertel 627: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52 albertel 628: my $file=&Apache::lonnet::filelocation("",$request->uri);
629:
630: #check if we know where we are
631: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
632: # if we are browsing we might not be able to know where we are
633: if ($Apache::lonhomework::browse ne 'F') {
634: #should know where we are, so ask
635: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 636: }
1.52 albertel 637: }
1.41 albertel 638:
1.118 albertel 639: my ($symb) = &Apache::lonxml::whichuser();
640: &Apache::lonxml::debug('symb is '.$symb);
641: if ($ENV{'request.state'} eq "construct" || $symb eq '') {
642: if ($ENV{'form.resetdata'} eq 'Reset Submissions' ||
643: $ENV{'form.resetdata'} eq 'New Problem Variation' ) {
644: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
645: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
646: }
647: }
1.52 albertel 648: if ($ENV{'request.state'} eq "construct") {
649: if ( -e $file ) {
650: if (!(defined $ENV{'form.problemmode'})) {
651: #first visit to problem in construction space
1.64 albertel 652: #&view_or_edit_menu($request);
653: $ENV{'form.problemmode'}='View';
654: &renderpage($request,$file);
655: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
656: &editxmlmode($request,$file);
1.74 albertel 657: } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
658: &analyze($request,$file);
1.52 albertel 659: } else {
660: &renderpage($request,$file);
1.41 albertel 661: }
662: } else {
1.52 albertel 663: # requested file doesn't exist in contruction space
664: &newproblem($request);
1.41 albertel 665: }
1.52 albertel 666: } else {
667: # just render the page normally outside of construction space
1.74 albertel 668: &Apache::lonxml::debug("not construct");
1.52 albertel 669: &renderpage($request,$file);
670: }
671: #my $td=&tv_interval($t0);
672: #&Apache::lonxml::debug("Spent $td seconds processing");
673: # &Apache::lonhomework::send_footer($request);
674: # always turn off debug messages
675: $Apache::lonxml::debug=0;
676: return OK;
677:
1.1 albertel 678: }
679:
680: 1;
681: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>