Annotation of loncom/interface/spreadsheet/lonspreadsheet.pm, revision 1.26
1.1 matthew 1: #
1.26 ! matthew 2: # $Id: lonspreadsheet.pm,v 1.25 2003/09/29 14:00:21 www Exp $
1.1 matthew 3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
26: # The LearningOnline Network with CAPA
27: # Spreadsheet/Grades Display Handler
28: #
29: # POD required stuff:
30:
31: =head1 NAME
32:
33: lonspreadsheet
34:
35: =head1 SYNOPSIS
36:
37: Spreadsheet interface to internal LON-CAPA data
38:
39: =head1 DESCRIPTION
40:
41: Lonspreadsheet provides course coordinators the ability to manage their
42: students grades online. The students are able to view their own grades, but
43: not the grades of their peers. The spreadsheet is highly customizable,
44: offering the ability to use Perl code to manipulate data, as well as many
45: built-in functions.
46:
47: =head2 Functions available to user of lonspreadsheet
48:
49: =over 4
50:
51: =cut
52:
53:
54: package Apache::lonspreadsheet;
55:
56: use strict;
1.23 matthew 57: use warnings FATAL=>'all';
58: no warnings 'uninitialized';
1.1 matthew 59: use Apache::classcalc();
60: use Apache::studentcalc();
61: use Apache::assesscalc();
62: use Apache::Constants qw(:common :http);
63: use Apache::lonnet;
64: use Apache::lonhtmlcommon;
1.25 www 65: use Apache::lonlocal;
1.26 ! matthew 66: use Apache::loncoursedata();
1.1 matthew 67: use HTML::Entities();
68:
69: ##
70: ## HTML utility subroutines really should go in lonhtmlcommon
71: ##
72:
73: sub textfield {
74: my ($title,$name,$value)=@_;
75: return "\n<p><b>$title:</b><br>".
76: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
77: }
78:
79: sub hiddenfield {
80: my ($name,$value)=@_;
81: return '<input type=hidden name="'.$name.'" value="'.$value.'" />'."\n";
82: }
83:
84: sub selectbox {
85: my ($title,$name,$value,%options)=@_;
86: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
87: foreach (sort keys(%options)) {
88: $selout.='<option value="'.$_.'"';
89: if ($_ eq $value) { $selout.=' selected'; }
90: $selout.='>'.$options{$_}.'</option>';
91: }
92: return $selout.'</select>';
93: }
94:
1.5 matthew 95: sub file_dialogs {
96: my ($spreadsheet) = @_;
97: my $bgcolor = "#FFFFFF";
98: my $sheettype = $spreadsheet->{'type'};
99: my $result = '';
1.11 matthew 100: my $message = '';
1.5 matthew 101: ##
102: ## Deal with saving the spreadsheet
1.11 matthew 103: if ((exists($ENV{'form.save'}) || exists($ENV{'form.makedefault'})) &&
1.5 matthew 104: exists($ENV{'form.savefilename'})) {
105: $spreadsheet->filename($ENV{'form.savefilename'});
106: my $save_status = $spreadsheet->save();
107: if ($save_status ne 'ok') {
1.11 matthew 108: $message .= "An error occurred while saving the spreadsheet".
1.5 matthew 109: "There error is:".$save_status;
110: } else {
1.24 matthew 111: $message .= "Spreadsheet saved as ".$spreadsheet->filename();
1.5 matthew 112: }
113: } elsif (exists($ENV{'form.newformula'}) &&
114: exists($ENV{'form.cell'}) &&
115: $ENV{'form.cell'} ne '' ) {
116: ##
117: ## Make any requested modifications to the spreadsheet
118: $spreadsheet->modify_cell($ENV{'form.cell'},
119: $ENV{'form.newformula'});
120: $spreadsheet->save_tmp();
121: # output that we are dealing with a temporary file
122: $result .=&hiddenfield('workcopy',$sheettype);
1.11 matthew 123: if ($ENV{'form.newformula'} !~ /^\s*$/) {
124: $message .='<table><tr>'.
125: '<td valign="top"><pre>Cell '.$ENV{'form.cell'}.' = </pre></td>'.
126: '<td><pre>'.$ENV{'form.newformula'}."</pre></td></tr></table>\n";
127: } else {
128: $message .= 'Deleted contents of cell '.$ENV{'form.cell'}.'.';
129: }
1.5 matthew 130: }
131: ##
132: ## Editing code
133: $result .=&hiddenfield('cell','').
134: &hiddenfield('newformula','');
135: ##
136: ## Create the save and load dialogs
137: my $filename = $spreadsheet->filename();
1.10 matthew 138: my $truefilename = $filename;
139: if ($spreadsheet->is_default()) {
140: $filename = 'Default';
141: }
1.5 matthew 142: my $save_dialog = '<nobr>'.
143: '<input type="submit" name="save" value="Save as" /> '.
144: '<input type="text" name="savefilename" size="30" value="'.
1.10 matthew 145: $truefilename.'" />'.
1.5 matthew 146: '</nobr>';
147: my $makedefault_dialog = '<input type="submit" name="makedefault" '.
1.11 matthew 148: 'value="Save as & Make This Sheet the Default"/>';
1.5 matthew 149: #
150: my $link = '<a href="javascript:openbrowser'.
1.9 www 151: "('sheet','loadfilename','spreadsheet')\">Select Spreadsheet File</a>";
1.5 matthew 152: my $load_dialog = <<END;
153: <table bgcolor="$bgcolor">
154: <tr><td><input type="submit" name="load" value="Load" /></td>
155: <td><nobr>
1.10 matthew 156: <input type="text" name="loadfilename" size="20" value="$filename" />
1.5 matthew 157: $link</nobr>
158: </td></tr>
159: <tr><td> </td><td>
160: <select name="fileselect" onchange="document.sheet.loadfilename.value=document.sheet.fileselect.value" >
161: END
1.7 matthew 162: my $default_filename_set = 0;
1.5 matthew 163: foreach my $sheetfilename ($spreadsheet->othersheets()) {
164: $load_dialog .= ' <option name="'.$sheetfilename.'"';
165: if ($filename eq $sheetfilename) {
166: $load_dialog .= ' selected';
1.7 matthew 167: $default_filename_set = 1;
1.5 matthew 168: }
169: $load_dialog .= '>'.$sheetfilename."</option>\n";
170: }
1.22 matthew 171: $load_dialog .= "</select>\n</td><td> </td></tr>\n</table>\n";
1.5 matthew 172: #
173: $result .=<<END;
1.23 matthew 174: <!--
175: <fieldset title="File Dialogs" >
176: <legend>File Dialogs</legend>
177: -->
1.5 matthew 178: <!-- load / save dialogs -->
179: <table cellspacing="2">
180: <tr>
181: <td>$load_dialog</td>
182: <td>
183: <table bgcolor="$bgcolor">
184: <tr><td>$save_dialog</td></tr>
185: <tr><td align="center">$makedefault_dialog</td></tr>
186: </table>
187: </td>
188: </tr>
189: </table>
1.23 matthew 190: <!--
191: </fieldset>
192: -->
1.5 matthew 193: END
1.11 matthew 194: return ($result,$message);
1.5 matthew 195: }
196:
1.1 matthew 197: sub handler {
198: my $r=shift;
199: #
200: # Overload checking
201: #
202: # Check this server
203: my $loaderror=&Apache::lonnet::overloaderror($r);
204: if ($loaderror) { return $loaderror; }
205: # Check the course homeserver
206: $loaderror= &Apache::lonnet::overloaderror($r,
207: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
1.4 matthew 208: # if ($loaderror) { return $loaderror; }
1.1 matthew 209: #
210: # HTML Header
211: #
212: if ($r->header_only) {
1.25 www 213: &Apache::loncommon::content_type($r,'text/html');
1.1 matthew 214: $r->send_http_header;
215: return OK;
216: }
217: #
218: # Roles Checking
219: #
220: # Needs to be in a course
221: if (! $ENV{'request.course.fn'}) {
222: # Not in a course, or not allowed to modify parms
223: $ENV{'user.error.msg'}=
224: $r->uri.":opa:0:0:Cannot modify spreadsheet";
225: return HTTP_NOT_ACCEPTABLE;
1.17 matthew 226: }
227: my $courseid = $ENV{'request.course.id'};
228: #
229: # Do not allow students to continue if standard grading is in effect.
230: if ($ENV{'request.role'} =~ /^st\./) {
231: if ($ENV{'course.'.$courseid.'.grading'} eq 'standard') {
232: return HTTP_NOT_ACCEPTABLE;
233: }
1.1 matthew 234: }
235: #
236: # Get query string for limited number of parameters
237: #
238: &Apache::loncommon::get_unprocessed_cgi
1.19 matthew 239: ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename','recalc']);
1.1 matthew 240: #
241: # Deal with restricted student permissions
242: #
243: if ($ENV{'request.role'} =~ /^st\./) {
244: delete $ENV{'form.cell'} if (exists($ENV{'form.cell'}));
245: delete $ENV{'form.newformula'} if (exists($ENV{'form.newformula'}));
246: }
247: #
248: # Determine basic information about the spreadsheet
249: my ($sheettype) = ($r->uri=~/\/(\w+)$/);
250: #
251: my $symb = undef;
252: $symb = $ENV{'form.usymb'} if (exists($ENV{'form.usymb'}));
253: my $name = $ENV{'user.name'};
254: my $domain = $ENV{'user.domain'};
255: if (exists($ENV{'form.sname'})) {
256: $name = $ENV{'form.sname'};
257: $domain = $ENV{'form.sdomain'};
258: }
1.3 matthew 259: ##
260: ## Check permissions
261: my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
262: $ENV{'request.course.id'});
1.12 matthew 263: # Only those instructors/tas/whatevers with complete access
264: # (not section restricted) are able to modify spreadsheets.
1.3 matthew 265: my $allowed_to_view = &Apache::lonnet::allowed('vgr',
266: $ENV{'request.course.id'});
1.12 matthew 267: if (! $allowed_to_view) {
268: $allowed_to_view = &Apache::lonnet::allowed('vgr',
269: $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'});
270: # Those who are restricted by section are allowed to view.
271: # The routines in lonstatistics which decide which students'
272: # will be shown take care of the restriction by section.
273: }
1.1 matthew 274: #
1.3 matthew 275: # Only those able to view others grades will be allowed to continue
276: # if they are not requesting their own.
1.12 matthew 277: if ($sheettype eq 'classcalc') {
1.3 matthew 278: if (! $allowed_to_view) {
1.14 www 279: $ENV{'user.error.msg'}=
280: $r->uri.":vgr:0:0:Access Permission Denied";
281: return HTTP_NOT_ACCEPTABLE;
282: }
1.1 matthew 283: }
1.12 matthew 284: if ((($name ne $ENV{'user.name'} ) ||
285: ($domain ne $ENV{'user.domain'})) && $sheettype ne 'classcalc') {
286: # Check that the student is in their section?
287: if (exists($ENV{'request.course.sec'}) &&
288: $ENV{'request.course.sec'} ne '' ) {
289: my $stu_sec = &Apache::lonnet::usection($domain,$name,
290: $ENV{'request.course.id'});
291: if ($stu_sec ne $ENV{'request.course.sec'}) {
1.14 www 292: $ENV{'user.error.msg'}=
293: $r->uri.":vgr:0:0:Requested student not in your section.";
294: return HTTP_NOT_ACCEPTABLE;
1.12 matthew 295: }
296: }
297: }
1.14 www 298:
299: #
300: # Open page, try to prevent browser cache.
301: #
1.25 www 302: &Apache::loncommon::content_type($r,'text/html');
1.15 matthew 303: &Apache::loncommon::no_cache($r);
1.14 www 304: $r->send_http_header;
1.12 matthew 305:
1.1 matthew 306: #
307: # Header....
308: #
309: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
310: my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
311: ##
312: ## Spit out the javascript required for editing
313: ##
1.5 matthew 314: if ($allowed_to_edit) {
315: my $extra_javascript =
316: &Apache::loncommon::browser_and_searcher_javascript();
1.1 matthew 317: $r->print(<<ENDSCRIPT);
318: <script language="JavaScript">
319:
1.5 matthew 320: $extra_javascript
321:
1.1 matthew 322: var editwin;
323:
324: function celledit(cellname,cellformula) {
325: var edit_text = '';
326: // cellformula may contain less-than and greater-than symbols, so
327: // we need to escape them?
328: edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
329: edit_text += '<form name="editwinform">';
330: edit_text += '<center><h3>Cell '+cellname+'</h3>';
1.18 matthew 331: edit_text += '<textarea name="newformula" cols="60" rows="12"';
1.1 matthew 332: edit_text += ' wrap="off" >'+cellformula+'</textarea>';
333: edit_text += '</br>';
334: edit_text += '<input type="button" name="accept" value="Accept"';
335: edit_text += ' onClick=\\\'javascript:';
336: edit_text += 'opener.document.sheet.cell.value=';
337: edit_text += '"'+cellname+'";';
338: edit_text += 'opener.document.sheet.newformula.value=';
339: edit_text += 'document.editwinform.newformula.value;';
340: edit_text += 'opener.document.sheet.submit();';
341: edit_text += 'self.close()\\\' />';
342: edit_text += ' ';
343: edit_text += '<input type="button" name="abort" ';
344: edit_text += 'value="Discard Changes"';
345: edit_text += ' onClick="javascript:self.close()" />';
346: edit_text += '</center></body></html>';
347:
348: if (editwin != null && !(editwin.closed) ) {
349: editwin.close();
350: }
351:
1.18 matthew 352: editwin = window.open($nothing,'CellEditWin','height=280,width=480,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
1.1 matthew 353: editwin.document.write(edit_text);
354: }
355: </script>
356: ENDSCRIPT
357: }
358: $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
359: '<form action="'.$r->uri.'" name="sheet" method="post">');
360: $r->print(&hiddenfield('sname' ,$ENV{'form.sname'}).
361: &hiddenfield('sdomain',$ENV{'form.sdomain'}).
1.6 matthew 362: &hiddenfield('usymb' ,$ENV{'form.usymb'}));
1.1 matthew 363: $r->rflush();
364: ##
365: ## Determine the filename to use
366: my $filename = undef;
1.3 matthew 367: if ($allowed_to_edit) {
1.1 matthew 368: $filename = $ENV{'form.filename'} if (exists($ENV{'form.filename'}));
369: #
370: if (exists($ENV{'form.load'}) && exists($ENV{'form.loadfilename'})) {
371: $filename = $ENV{'form.loadfilename'};
1.7 matthew 372: $ENV{'form.workcopy'} = 'no';
1.1 matthew 373: }
374: }
375: ##
1.19 matthew 376: ## Take care of "backdoor" spreadsheet expiration / recalc stuff
377: if ($allowed_to_edit && exists($ENV{'form.recalc'})) {
1.26 ! matthew 378: if (exists($ENV{'form.recalc'})) {
! 379: &Apache::loncoursedata::delete_caches($ENV{'requres.course.id'});
! 380: }
1.19 matthew 381: if ($ENV{'form.recalc'} eq 'ilovewastingtime') {
382: &Apache::lonnet::logthis('ilovewastingtime');
383: # expire ALL spreadsheets
384: &Apache::lonnet::expirespread('','','studentcalc');
385: &Apache::lonnet::expirespread('','','assesscalc');
386: } elsif ($ENV{'form.recalc'} =~ /^symb:/) {
387: # expire for all students on this symb
388: my ($symb) = ($ENV{'form.recalc'} =~ /^symb:(.*)$/);
389: &Apache::lonnet::logthis('symb = '.$symb);
390: &Apache::lonnet::expirespread('','','assesscalc',$symb);
391: &Apache::lonnet::expirespread('','','studentcalc');
392: } elsif ($ENV{'form.recalc'} =~ /^student:/) {
393: # expire all assessment spreadsheets for this user
394: my ($sname,$sdom) = ($ENV{'form.recalc'}=~/^student:(.*):(.*)$/);
395: &Apache::lonnet::logthis('student = '.$sname.':'.$sdom);
396: if (defined($sname) && defined($sdom)) {
397: &Apache::lonnet::expirespread($sname,$sdom,'assesscalc');
398: &Apache::lonnet::expirespread($sname,$sdom,'studentcalc');
399: }
400: }
401: }
402: ##
1.1 matthew 403: ## Make the spreadsheet
404: &Apache::Spreadsheet::initialize_spreadsheet_package();
405: my $spreadsheet = undef;
406: if ($sheettype eq 'classcalc') {
407: $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef);
408: } elsif ($sheettype eq 'studentcalc') {
409: $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef);
1.2 matthew 410: } elsif ($sheettype eq 'assesscalc' &&
411: defined($symb) &&
1.3 matthew 412: $allowed_to_edit) {
1.1 matthew 413: $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb);
414: } else {
1.2 matthew 415: return HTTP_NOT_ACCEPTABLE;
416: }
417: if (! defined($spreadsheet)) {
418: # error error - run in circles, scream and shout
1.1 matthew 419: return;
420: }
1.8 matthew 421: $spreadsheet->initialize();
1.5 matthew 422: #
423: # Output selector
1.1 matthew 424: ##
425: ## Editing/loading/saving
1.3 matthew 426: if ($allowed_to_edit) {
1.11 matthew 427: my ($html,$action_message) = &file_dialogs($spreadsheet);
1.10 matthew 428: if ($ENV{'form.makedefault'}) {
429: $spreadsheet->make_default();
1.11 matthew 430: if ($action_message) {
431: $action_message .= '<br />';
432: }
433: $action_message .= 'Made this spreadsheet the default';
434: if ($sheettype eq 'classcalc') {
435: $action_message .= ' for the course';
436: } elsif ($sheettype eq 'studentcalc') {
437: $action_message .= ' for all students';
438: } elsif ($sheettype eq 'assesscalc') {
439: $action_message .= ' for all assessments';
440: }
441: $action_message .= '.';
1.10 matthew 442: }
1.5 matthew 443: $r->print('<table><tr><td>'.$spreadsheet->html_header().'</td>'.
1.11 matthew 444: '<td valign="bottom">'.$html."</td></tr></table>\n");
1.12 matthew 445: if ($action_message ne '') {
446: $r->print(<<END);
1.11 matthew 447: <table>
448: <tr><td valign="top"><b>Last Action:</b></td>
449: <td> </td>
450: <td>$action_message</td>
451: </tr>
452: </table>
453: END
1.12 matthew 454: }
1.1 matthew 455: $r->rflush();
1.5 matthew 456: } else {
457: $r->print('<table><tr><td>'.$spreadsheet->html_header().
458: "</td></tr></table>\n");
1.1 matthew 459: }
1.11 matthew 460: $r->rflush();
1.1 matthew 461: #
1.19 matthew 462: $r->print("<table><tr>");
1.13 matthew 463: if ($sheettype eq 'classcalc') {
1.25 www 464: $r->print('<td><input type="submit" value="'.
465: &mt('Generate Spreadsheet').'" />'.
1.19 matthew 466: '</td>');
467: }
1.21 matthew 468: if ($allowed_to_view) {
469: $r->print('<td>'.
470: &Apache::loncommon::help_open_topic("Spreadsheet_About",
471: 'Spreadsheet Help').
472: '</td>');
473: }
474: if ($allowed_to_edit) {
475: $r->print('<td>'.
476: &Apache::loncommon::help_open_topic("Spreadsheet_Editing",
477: 'Editing Help').
478: '</td>');
479: }
1.19 matthew 480: $r->print('</tr></table>');
1.10 matthew 481: #
1.1 matthew 482: # Keep track of the filename
483: $r->print(&hiddenfield('filename',$filename));
484: #
1.5 matthew 485: # Keep track of the number of times we have been called, sort of.
486: $r->print(&hiddenfield('not_first_run','whatever'));
487: #
488: if (exists($ENV{'form.not_first_run'}) || $sheettype ne 'classcalc') {
489: $r->print($spreadsheet->get_html_title());
490: if ($allowed_to_view || $allowed_to_edit) {
491: $r->print($spreadsheet->parent_link());
492: }
1.16 matthew 493: $r->rflush();
1.5 matthew 494: $spreadsheet->display($r);
1.1 matthew 495: }
496: $r->print('</form></body></html>');
497: return OK;
498: }
499:
500: 1;
501:
502: __END__
503:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>