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