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