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