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