Annotation of loncom/homework/lonhomework.pm, revision 1.60
1.52 albertel 1: # The LON-CAPA Homework handler
2: # Guy Albertelli
1.17 www 3: # 11/30 Gerd Kortemeyer
1.56 www 4: # 6/1,8/17,8/18 Gerd Kortemeyer
1.1 albertel 5:
6: package Apache::lonhomework;
7: use strict;
8: use Apache::style;
9: use Apache::lonxml;
1.2 albertel 10: use Apache::lonnet;
11: use Apache::inputtags;
12: use Apache::structuretags;
1.59 tsai 13: use Apache::randomlabel;
1.9 albertel 14: use Apache::response;
1.22 albertel 15: use Apache::hint;
1.31 albertel 16: use Apache::outputtags;
1.26 www 17: use Apache::Constants qw(:common);
1.47 albertel 18: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 19:
20: sub BEGIN {
21: &Apache::lonxml::register_insert();
22: }
23:
1.5 albertel 24: sub get_target {
1.52 albertel 25: if ( $ENV{'request.state'} eq "published") {
26: if ( defined $ENV{'form.submitted'}) {
27: return ('grade', 'web');
28: } else {
29: return ('web');
30: }
31: } elsif ($ENV{'request.state'} eq "construct") {
32: if ( defined $ENV{'form.preview'}) {
33: if ( defined $ENV{'form.submitted'}) {
34: return ('grade', 'web');
35: } else {
36: return ('web');
37: }
38: } else {
39: if ( $ENV{'form.problemmode'} eq 'View' ) {
1.42 albertel 40: if ( defined $ENV{'form.submitted'}) {
1.60 ! albertel 41: return ('grade', 'web','answer');
1.42 albertel 42: } else {
1.60 ! albertel 43: return ('web','answer');
1.42 albertel 44: }
1.52 albertel 45: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
46: if ( $ENV{'form.submitted'} eq 'edit' ) {
47: return ('modified','edit');
1.42 albertel 48: } else {
1.52 albertel 49: return ('edit');
1.42 albertel 50: }
1.52 albertel 51: } else {
52: return ('web');
53: }
1.15 albertel 54: }
1.52 albertel 55: }
56: return ();
1.5 albertel 57: }
58:
1.3 albertel 59: sub setup_vars {
1.52 albertel 60: my ($target) = @_;
61: return ';'
1.11 albertel 62: # return ';$external::target='.$target.';';
1.2 albertel 63: }
64:
65: sub send_header {
1.52 albertel 66: my ($request)= @_;
67: $request->print(&Apache::lontexconvert::header());
1.16 albertel 68: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 69: }
70:
1.36 albertel 71: sub createmenu {
1.52 albertel 72: my ($which,$request)=@_;
73: if ($which eq 'grade') {
74: $request->print('<script language="JavaScript">
75: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
76: "height=350,width=150,menubar=no");
77: </script>');
78: }
1.36 albertel 79: }
80:
1.2 albertel 81: sub send_footer {
1.52 albertel 82: my ($request)= @_;
1.16 albertel 83: # $request->print('</form>');
1.52 albertel 84: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 85: }
86:
1.52 albertel 87: $Apache::lonxml::browse='';
1.53 www 88:
89: sub check_access {
1.52 albertel 90: my ($id) = @_;
91: my $date ='';
92: my $status = '';
93: my $datemsg = '';
94: my $lastdate = '';
95: my $temp;
96: my $type;
97: my $passed;
98: &Apache::lonxml::debug("checking for part :$id:");
99: foreach $temp ("opendate","duedate","answerdate") {
100: $lastdate = $date;
101: $date = &Apache::lonnet::EXT("resource.$id.$temp");
102: &Apache::lonxml::debug("found :$date: for :$temp:");
103: if ($date eq '') {
104: $date = "an unknown date"; $passed = 0;
105: } elsif ($date eq 'con_lost') {
106: $date = "an indeterminate date"; $passed = 0;
107: } else {
108: if (time < $date) { $passed = 0; } else { $passed = 1; }
109: $date = localtime $date;
1.51 harris41 110: }
1.52 albertel 111: if (!$passed) { $type=$temp; last; }
112: }
113: &Apache::lonxml::debug("have :$type:$passed:");
114: if ($passed) {
115: $status='SHOW_ANSWER';
116: $datemsg=$date;
117: } elsif ($type eq 'opendate') {
118: $status='CLOSED';
119: $datemsg = "will open on $date";
120: } elsif ($type eq 'duedate') {
121: $status='CAN_ANSWER';
122: $datemsg = "is due at $date";
123: } elsif ($type eq 'answerdate') {
124: $status='CLOSED';
125: $datemsg = "was due on $lastdate, and answers will be available on $date";
126: }
127: if ($status eq 'CAN_ANSWER') {
128: #check #tries
129: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
130: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
131: if ( $tries eq '' ) { $tries = '0'; }
132: if ( $maxtries eq '' ) { $maxtries = '2'; }
133: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
134: }
1.54 www 135:
1.56 www 136: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
137: (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54 www 138: return ('UNCHECKEDOUT','needs to be checked out');
139: }
140:
141:
1.52 albertel 142: &Apache::lonxml::debug("sending back :$status:$datemsg:");
143: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
144: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
145: $status='CAN_ANSWER';
146: $datemsg='is closed but you are allowed to view it';
147: }
148: if ($ENV{'request.state'} eq "construct") {
149: &Apache::lonxml::debug("in construction ignoring dates");
150: $status='CAN_ANSWER';
151: $datemsg='is in under construction';
152: }
153: return ($status,$datemsg);
1.20 albertel 154: }
155:
1.41 albertel 156: sub showhash {
1.52 albertel 157: my (%hash) = @_;
158: my $resultkey;
159: foreach $resultkey (sort keys %hash) {
160: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
161: }
162: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
163: return '';
1.41 albertel 164: }
165:
166: sub setuppermissions {
1.52 albertel 167: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
168: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
169: return ''
1.41 albertel 170: }
171:
172: sub setupheader {
1.52 albertel 173: my $request=$_[0];
174: if ($ENV{'browser.mathml'}) {
175: $request->content_type('text/xml');
176: } else {
177: $request->content_type('text/html');
178: }
179: $request->send_http_header;
180: return OK if $request->header_only;
181: return ''
1.41 albertel 182: }
1.35 albertel 183:
1.47 albertel 184: sub handle_save_or_undo {
1.52 albertel 185: my ($request,$problem,$result) = @_;
186: my $fileout = &Apache::lonnet::filelocation("",$request->uri);
187: my $filesave=$fileout.".bak";
188:
189: if ($ENV{'form.Undo'} eq 'undo') {
190: if (copy($filesave,$fileout)) {
191: $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
192: } else {
193: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
194: }
195: } else {
196: my $fs=Apache::File->new(">$filesave");
197: if (defined($fs)) {
198: print $fs $$problem;
199: $request->print("<b>Making Backup to $filesave</b><br />");
200: } else {
201: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
202: }
203: my $fh=Apache::File->new(">$fileout");
204: if (defined($fh)) {
205: print $fh $$result;
206: $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47 albertel 207: } else {
1.52 albertel 208: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.47 albertel 209: }
1.52 albertel 210: }
1.47 albertel 211: }
212:
1.41 albertel 213: sub renderpage {
1.52 albertel 214: my ($request,$file) = @_;
215:
216: my (@targets) = &get_target();
217: foreach my $target (@targets) {
218: #my $t0 = [&gettimeofday()];
219: my $problem=&Apache::lonnet::getfile($file);
220: if ($problem == -1) {
221: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
222: $problem='';
223: }
224:
225: my %mystyle;
226: my $result = '';
227: &Apache::inputtags::initialize_inputtags;
228: &Apache::edit::initialize_edit;
229: if ($target eq 'web') {
230: if (&Apache::lonnet::symbread() eq '') {
231: if ($ENV{'request.state'} eq "construct") {
1.36 albertel 232: } else {
1.52 albertel 233: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51 harris41 234: }
1.52 albertel 235: }
236: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
237: }
238: if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
239:
240: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
241: if ($default == -1) {
242: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
243: $default='';
244: }
245: $result = &Apache::lonxml::xmlparse($target, $problem,
246: $default.&setup_vars($target),%mystyle);
247:
248: #$request->print("Result follows:");
249: if ($target eq 'modified') {
250: &handle_save_or_undo($request,\$problem,\$result);
251: } else {
252: #my $td=&tv_interval($t0);
253: #if ( $Apache::lonxml::debug) {
254: #$result =~ s:</body>::;
255: #$result.="<br />Spent $td seconds processing target $target\n</body>";
256: #}
257: $request->print($result);
258: }
259: #$request->print(":Result ends");
260: #my $td=&tv_interval($t0);
261: }
1.41 albertel 262: }
263:
1.42 albertel 264: # with no arg it returns a HTML <option> list of the template titles
265: # with one arg it returns the filename associated with the arg passed
266: sub get_template_list {
1.52 albertel 267: my ($namewanted,$extension) = @_;
268: my $result;
269: &Apache::lonxml::debug("Looking for :$extension:");
270: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
271: my $name=&Apache::lonnet::metadata($file,'title');
272: if ($namewanted && ($name eq $namewanted)) {
273: $result=$file;
274: last;
275: } else {
276: $result.="<option>$name</option>";
1.42 albertel 277: }
1.52 albertel 278: }
279: return $result;
1.42 albertel 280: }
281:
282: sub newproblem {
1.52 albertel 283: my ($request) = @_;
284:
285: my $extension=$request->uri;
286: $extension=~s:^.*\.([\w]+)$:$1:;
287: &Apache::lonxml::debug("Looking for :$extension:");
288: if ($ENV{'form.template'}) {
289: use File::Copy;
290: my $file = &get_template_list($ENV{'form.template'},$extension);
291: my $dest = &Apache::lonnet::filelocation("",$request->uri);
292: copy($file,$dest);
293: &renderpage($request,$file);
294: } else {
295: my $templatelist=&get_template_list('',$extension);
296: my $url=$request->uri;
297: my $dest = &Apache::lonnet::filelocation("",$request->uri);
298: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 299: <body bgcolor="#FFFFFF">
1.50 albertel 300: The request file $url doesn\'t exist. You can create a new $extension <br />
1.42 albertel 301: <form action="$url" method="POST">
1.50 albertel 302: <input type="submit" value="New $extension"><br />
1.42 albertel 303: <select name="template">
304: $templatelist
305: </select>
306: </form>
307: </body>
308: ENDNEWPROBLEM
309: }
310:
311: return '';
312: }
313:
314: sub view_or_edit_menu {
1.52 albertel 315: my ($request) = @_;
316: my $url=$request->uri;
317: $request->print(<<EDITMENU);
1.42 albertel 318: <body bgcolor="#FFFFFF">
319: <form action="$url" method="POST">
320: Would you like to <input type="submit" name="problemmode" value="View"> or
321: <input type="submit" name="problemmode" value="Edit"> the problem.
322: </form>
323: </body>
324: EDITMENU
325: }
326:
1.41 albertel 327: sub handler {
1.52 albertel 328: #my $t0 = [&gettimeofday()];
329: my $request=$_[0];
1.41 albertel 330:
1.52 albertel 331: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41 albertel 332:
333: if (&setupheader($request)) { return OK; }
1.52 albertel 334: $ENV{'request.uri'}=$request->uri;
1.41 albertel 335:
336: #setup permissions
1.52 albertel 337: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
338: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
339: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
340:
341: my $file=&Apache::lonnet::filelocation("",$request->uri);
342:
343: #check if we know where we are
344: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
345: # if we are browsing we might not be able to know where we are
346: if ($Apache::lonhomework::browse ne 'F') {
347: #should know where we are, so ask
348: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 349: }
1.52 albertel 350: }
1.41 albertel 351:
1.52 albertel 352: if ($ENV{'request.state'} eq "construct") {
353: if ( -e $file ) {
354: if (!(defined $ENV{'form.problemmode'})) {
355: #first visit to problem in construction space
356: &view_or_edit_menu($request);
357: #&renderpage($request,$file);
358: } else {
359: &renderpage($request,$file);
1.41 albertel 360: }
361: } else {
1.52 albertel 362: # requested file doesn't exist in contruction space
363: &newproblem($request);
1.41 albertel 364: }
1.52 albertel 365: } else {
366: # just render the page normally outside of construction space
367: &renderpage($request,$file);
368: }
369: #my $td=&tv_interval($t0);
370: #&Apache::lonxml::debug("Spent $td seconds processing");
371: # &Apache::lonhomework::send_footer($request);
372: # always turn off debug messages
373: $Apache::lonxml::debug=0;
374: return OK;
375:
1.1 albertel 376: }
377:
378: 1;
379: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>