1: #
2: # $Id: lonspreadsheet.pm,v 1.61.6.1 2012/05/10 20:20:07 raeburn Exp $
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;
57: use warnings FATAL=>'all';
58: no warnings 'uninitialized';
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 Apache::lonlocal;
66: use Apache::loncoursedata();
67: use Apache::lonquickgrades();
68: use HTML::Entities();
69:
70: ##
71: ## HTML utility subroutines really should go in lonhtmlcommon
72: ##
73:
74: sub textfield {
75: my ($title,$name,$value)=@_;
76: return "\n<p><b>$title:</b><br />".
77: '<input type="text" name="'.$name.'" size="80" value="'.$value.'" />';
78: }
79:
80: sub hiddenfield {
81: my ($name,$value)=@_;
82: return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
83: }
84:
85: sub selectbox {
86: my ($title,$name,$value,%options)=@_;
87: my $selout="\n<p><b>$title:</b><br />".'<select name="'.$name.'">';
88: foreach (sort keys(%options)) {
89: $selout.='<option value="'.$_.'"';
90: if ($_ eq $value) { $selout.=' selected="selected"'; }
91: $selout.='>'.&mt($options{$_}).'</option>';
92: }
93: return $selout.'</select>';
94: }
95:
96: sub file_dialogs {
97: my ($spreadsheet) = @_;
98: my $bgcolor = "#FFFFFF";
99: my $sheettype = $spreadsheet->{'type'};
100: my $result = '';
101: my $message = '';
102: ##
103: ## Deal with saving the spreadsheet
104: $spreadsheet->check_formulas_loaded();
105: if ((exists($env{'form.save'}) || exists($env{'form.makedefault'})) &&
106: exists($env{'form.savefilename'})) {
107: $spreadsheet->filename($env{'form.savefilename'});
108: my $save_status = $spreadsheet->save();
109: if ($save_status ne 'ok') {
110: $message .= '<span class="LC_error">'.
111: &mt('An error occurred while saving the spreadsheet. The error is: [_1].',
112: $save_status).'</span>';
113: } else {
114: $message .= '<span class="LC_info">'.&mt('Spreadsheet saved as: [_1] .',
115: '<span class="LC_filename">'.$spreadsheet->filename().'</span>').
116: '</span>';
117: }
118: } elsif (exists($env{'form.newformula'}) &&
119: exists($env{'form.cell'}) &&
120: $env{'form.cell'} ne '' ) {
121: ##
122: ## Make any requested modifications to the spreadsheet
123: $spreadsheet->modify_cell($env{'form.cell'},
124: $env{'form.newformula'});
125: $spreadsheet->save_tmp();
126: # output that we are dealing with a temporary file
127: $result .=&hiddenfield('workcopy',$sheettype);
128: if ($env{'form.newformula'} !~ /^\s*$/) {
129: $message .='<table><tr>'.
130: '<td valign="top"><pre>'.&mt('Cell').' '.$env{'form.cell'}.' = </pre></td>'.
131: '<td><pre>'.$env{'form.newformula'}."</pre></td></tr></table>\n";
132: } else {
133: $message .= &mt('Deleted contents of cell').' '.$env{'form.cell'}.'.';
134: }
135: }
136: ##
137: ## Editing code
138: $result .=&hiddenfield('cell','').
139: &hiddenfield('newformula','');
140: ##
141: ## Create the save and load dialogs
142: my $filename = $spreadsheet->filename();
143: my $truefilename = $filename;
144: if ($spreadsheet->is_default()) {
145: $filename = 'Default';
146: }
147: my $save_dialog = '<span class="LC_nobreak">'.
148: '<input type="submit" name="save" value="'.&mt('Save as').'" /> '.
149: '<input type="text" name="savefilename" size="30" value="'.
150: $truefilename.'" />'.
151: '</span>';
152: my $makedefault_dialog = '<input type="submit" name="makedefault" '.
153: 'value="'.&mt('Save as & Make This Sheet the Default').'"/>';
154: #
155: my $link = '<a href="javascript:openbrowser'.
156: "('sheet','loadfilename','spreadsheet')\">".&mt('Select Spreadsheet File')."</a>";
157: my $load=&mt('Load:');
158: my $load_dialog = <<END;
159: <table bgcolor="$bgcolor">
160: <tr><td><input type="submit" name="load" value="$load" /></td>
161: <td><span class="LC_nobreak">
162: <input type="text" name="loadfilename" size="20" value="$filename" />
163: $link</span>
164: </td></tr>
165: <tr><td> </td><td>
166: <select name="fileselect" onchange="document.sheet.loadfilename.value=document.sheet.fileselect.value" >
167: END
168: my $default_filename_set = 0;
169: foreach my $sheetfilename ($spreadsheet->othersheets()) {
170: $load_dialog .= ' <option value="'.$sheetfilename.'"';
171: if ($filename eq $sheetfilename) {
172: $load_dialog .= ' selected="selected"';
173: $default_filename_set = 1;
174: }
175: $load_dialog .= '>'.$sheetfilename."</option>\n";
176: }
177: $load_dialog .= "</select>\n</td><td> </td></tr>\n</table>\n";
178: #
179: my $headline = &mt('File Dialogs');
180: $result .=<<END;
181: <!--
182: <fieldset title="File Dialogs" >
183: <legend>$headline</legend>
184: -->
185: <!-- load / save dialogs -->
186: <table cellspacing="2">
187: <tr>
188: <td>$load_dialog</td>
189: <td>
190: <table bgcolor="$bgcolor">
191: <tr><td>$save_dialog</td></tr>
192: <tr><td align="center">$makedefault_dialog</td></tr>
193: </table>
194: </td>
195: </tr>
196: </table>
197: <!--
198: </fieldset>
199: -->
200: END
201: return ($result,$message);
202: }
203:
204: sub handler {
205: my $r=shift;
206: #
207: # HTML Header
208: #
209: if ($r->header_only) {
210: &Apache::loncommon::content_type($r,'text/html');
211: $r->send_http_header;
212: return OK;
213: }
214: #
215: # Roles Checking
216: #
217: # Needs to be in a course
218: if (! $env{'request.course.fn'}) {
219: # Not in a course, or not allowed to modify parms
220: $env{'user.error.msg'}=
221: $r->uri.":opa:0:0:Cannot modify spreadsheet";
222: return HTTP_NOT_ACCEPTABLE;
223: }
224: my ($sheettype) = ($r->uri=~/\/(\w+)$/);
225: my $courseid = $env{'request.course.id'};
226:
227: ##
228: ## Check permissions
229: my $allowed_to_edit = &Apache::lonnet::allowed('mgr',
230: $env{'request.course.id'});
231: # Only those instructors/tas/whatevers with complete access
232: # (not section restricted) are able to modify spreadsheets.
233: my $allowed_to_view = &Apache::lonnet::allowed('vgr',
234: $env{'request.course.id'});
235: if (! $allowed_to_view) {
236: $allowed_to_view = &Apache::lonnet::allowed('vgr',
237: $env{'request.course.id'}.'/'.$env{'request.course.sec'});
238: # Those who are restricted by section are allowed to view.
239: # The routines in lonstatistics which decide which students'
240: # will be shown take care of the restriction by section.
241: }
242:
243: #
244: # Do not allow users without vgr or mgr priv to continue unless
245: # grading type is set to spreadsheet.
246: #
247:
248: if ((!$allowed_to_view) && (!$allowed_to_edit)) {
249: if ($env{'course.'.$courseid.'.grading'} eq 'spreadsheet') {
250: if ($sheettype ne 'studentcalc') {
251: $r->internal_redirect('/adm/studentcalc');
252: return OK;
253: }
254: } else {
255: $r->internal_redirect('/adm/quickgrades');
256: return OK;
257: }
258: }
259: #
260: # Get query string for limited number of parameters
261: #
262: &Apache::loncommon::get_unprocessed_cgi
263: ($ENV{'QUERY_STRING'},['sname','sdomain','usymb','filename','recalc',
264: 'output_format','not_first_run']);
265: #
266: # Deal with restricted student permissions
267: #
268: if ($env{'request.role'} =~ /^st\./) {
269: delete $env{'form.cell'} if (exists($env{'form.cell'}));
270: delete $env{'form.newformula'} if (exists($env{'form.newformula'}));
271: }
272: #
273: # Determine basic information about the spreadsheet
274: #
275: my $symb = undef;
276: $symb = $env{'form.usymb'} if (exists($env{'form.usymb'}));
277: my $name = $env{'user.name'};
278: my $domain = $env{'user.domain'};
279: my $warning;
280: if (exists($env{'form.sname'}) && $env{'form.sname'} ne '') {
281: if (($env{'form.sname'} ne $env{'user.name'}) ||
282: ($env{'form.sdomain'} ne $env{'user.domain'})) {
283: if (($allowed_to_view) || ($allowed_to_edit)) {
284: if (&Apache::lonnet::homeserver($env{'form.sname'},$env{'form.sdomain'}) ne 'no_host') {
285: $name = $env{'form.sname'};
286: $domain = $env{'form.sdomain'};
287: } else {
288: $warning = &mt('Requested user: "[_1]" does not exist; your own sheet is displayed instead.',$env{'form.sname'}.':'.$env{'form.sdomain'});
289: }
290: } else {
291: $warning = &mt('Your current role is not permitted to display this sheet for the requested user: "[_1]"; your own sheet is displayed instead.',$env{'form.sname'}.':'.$env{'form.sdomain'});
292: }
293: }
294: }
295: $env{'form.sname'} = $name;
296: $env{'form.sdomain'} = $domain;
297: my $section = &Apache::lonnet::getsection($domain,$name,
298: $env{'request.course.id'});
299: my @groups;
300: if (($env{'user.name'} eq $name) && ($env{'user.domain'} eq $domain)) {
301: @groups = &Apache::lonnet::sort_course_groups($env{'request.course.id'},
302: split(':',$env{'request.course.groups'}));
303: } else {
304: @groups = &Apache::lonnet::get_users_groups($domain,$name,
305: $env{'request.course.id'});
306: }
307:
308: #
309: # Only those able to view others grades will be allowed to continue
310: # if they are not requesting their own.
311: if ($sheettype eq 'classcalc') {
312: if (!$allowed_to_view) {
313: $r->internal_redirect('/adm/studentcalc');
314: return OK;
315: }
316: }
317: if ((($name ne $env{'user.name'} ) ||
318: ($domain ne $env{'user.domain'})) && $sheettype ne 'classcalc') {
319: # Check that the student is in their section?
320: if (exists($env{'request.course.sec'}) &&
321: $env{'request.course.sec'} ne '' ) {
322: my $stu_sec = &Apache::lonnet::getsection($domain,$name,
323: $env{'request.course.id'});
324: if ($stu_sec ne $env{'request.course.sec'}) {
325: $env{'user.error.msg'}=
326: $r->uri.":vgr:0:0:Requested student not in your section.";
327: return HTTP_NOT_ACCEPTABLE;
328: }
329: }
330: }
331:
332: #
333: # Open page, try to prevent browser cache.
334: #
335: &Apache::loncommon::content_type($r,'text/html');
336: &Apache::loncommon::no_cache($r);
337: $r->send_http_header;
338:
339: #
340: # Header....
341: #
342: my $nothing = &Apache::lonhtmlcommon::javascript_nothing();
343: ##
344: ## Spit out the javascript required for editing
345: ##
346: my $js;
347: if ($allowed_to_edit) {
348: my %lt=(
349: 'ce' => 'Cell',
350: 'ac' => 'Accept',
351: 'dc' => 'Discard Changes'
352: );
353: my $extra_javascript =
354: &Apache::loncommon::browser_and_searcher_javascript();
355:
356: my $cell_extra_js = &Apache::loncommon::resize_textarea_js();
357: my $cell_edit_start =
358: &Apache::loncommon::start_page('Cell Edit Window',$cell_extra_js,
359: {'only_body' => 1,
360: 'js_ready' => 1,
361: 'add_entries' => {
362: 'onresize' => "resize_textarea('LC_newformula','LC_aftertextarea')",
363: 'onload' => "resize_textarea('LC_newformula','LC_aftertextarea')",
364: }});
365: my $cell_edit_end =
366: &Apache::loncommon::end_page({'js_ready' => 1,});
367:
368: $js = <<ENDSCRIPT;
369: <script type="text/javascript">
370: //<!--
371: $extra_javascript
372:
373: var editwin;
374:
375: function celledit(cellname,cellformula) {
376: var edit_text = '';
377: // cellformula may contain less-than and greater-than symbols, so
378: // we need to escape them?
379: edit_text +='$cell_edit_start';
380: edit_text += '<form name="editwinform">';
381: edit_text += '<center><h3>$lt{'ce'} '+cellname+'</h3>';
382: edit_text += '<textarea id="LC_newformula" name="newformula" ';
383: edit_text += ' cols="60" rows="12"; wrap="off" style="width:100%">';
384: edit_text += cellformula+'</textarea>';
385: edit_text += '<div id="LC_aftertextarea"><br />';
386: edit_text += '<input type="button" name="accept" value="$lt{'ac'}"';
387: edit_text += ' onClick=\\\'javascript:';
388: edit_text += 'opener.document.sheet.cell.value=';
389: edit_text += '"'+cellname+'";';
390: edit_text += 'opener.document.sheet.newformula.value=';
391: edit_text += 'document.editwinform.newformula.value;';
392: edit_text += 'opener.document.sheet.submit();';
393: edit_text += 'self.close()\\\' />';
394: edit_text += ' ';
395: edit_text += '<input type="button" name="abort" ';
396: edit_text += 'value="$lt{'dc'}"';
397: edit_text += ' onClick="javascript:self.close()" />';
398: edit_text += '</center></div></form>$cell_edit_end';
399:
400: if (editwin != null && !(editwin.closed) ) {
401: editwin.close();
402: }
403:
404: editwin = window.open($nothing,'CellEditWin','height=280,width=480,scrollbars=no,resizable=yes,alwaysRaised=yes,dependent=yes',true);
405: editwin.document.write(edit_text);
406: editwin.document.close();
407: }
408: //-->
409: </script>
410: ENDSCRIPT
411: }
412: &Apache::lonhtmlcommon::clear_breadcrumbs();
413: &Apache::lonhtmlcommon::add_breadcrumb
414: ({href => $r->uri,
415: title => 'Spreadsheet',
416: text => 'Spreadsheet',
417: faq => 134,
418: bug => 'Spreadsheet'});
419: $r->print(&Apache::loncommon::start_page('Grades Spreadsheet',$js).
420: &Apache::lonhtmlcommon::breadcrumbs('Spreadsheet',
421: 'Spreadsheet_About').
422: &Apache::lonhtmlcommon::coursepreflink(&mt('Grade display settings'),
423: 'grading'));
424:
425: #
426: # Open the form
427: #
428: if ($warning) {
429: $r->print('<p class="LC_info">'.$warning.'</p>');
430: }
431: $r->print('<form action="'.$r->uri.'" name="sheet" method="post">');
432: $r->print(&hiddenfield('sname' ,$env{'form.sname'}).
433: &hiddenfield('sdomain',$env{'form.sdomain'}).
434: &hiddenfield('usymb' ,$env{'form.usymb'}));
435: $r->rflush();
436: ##
437: ## Determine the filename to use
438: my $filename = undef;
439: if ($allowed_to_edit) {
440: $filename = $env{'form.filename'} if (exists($env{'form.filename'}));
441: #
442: if (exists($env{'form.load'}) && exists($env{'form.loadfilename'})) {
443: $filename = $env{'form.loadfilename'};
444: $env{'form.workcopy'} = 'no';
445: }
446: }
447: ##
448: ## Take care of "backdoor" spreadsheet expiration / recalc stuff
449: if ($allowed_to_edit && exists($env{'form.recalc'})) {
450: if (exists($env{'form.recalc'})) {
451: &Apache::loncoursedata::delete_caches($env{'requres.course.id'});
452: }
453: if ($env{'form.recalc'} eq 'expireallsheets') {
454: &Apache::lonnet::logthis('spreadsheet expired: entire course');
455: # expire ALL spreadsheets
456: &Apache::lonnet::expirespread('','','studentcalc');
457: &Apache::lonnet::expirespread('','','assesscalc');
458: $r->print('<h3>'.
459: &mt('Expired spreadsheet caches for all students').
460: '</h3>');
461: } elsif ($env{'form.recalc'} =~ /^symb:/) {
462: # expire for all students on this symb
463: my ($symb) = ($env{'form.recalc'} =~ /^symb:(.*)$/);
464: &Apache::lonnet::logthis('spreadsheet expired: symb = '.$symb);
465: &Apache::lonnet::expirespread('','','assesscalc',$symb);
466: &Apache::lonnet::expirespread('','','studentcalc');
467: $r->print('<h3>'.
468: &mt('Expired spreadsheet caches for all students for symb [_1]',
469: $symb).
470: '</h3>');
471: } elsif ($env{'form.recalc'} =~ /^student:/) {
472: # expire all assessment spreadsheets for this user
473: my ($sname,$sdom) = ($env{'form.recalc'}=~/^student:(.*):(.*)$/);
474: &Apache::lonnet::logthis('spreadsheet expired: student = '.
475: $sname.'@'.$sdom);
476: if (defined($sname) && defined($sdom)) {
477: &Apache::lonnet::expirespread($sname,$sdom,'assesscalc');
478: &Apache::lonnet::expirespread($sname,$sdom,'studentcalc');
479: $r->print('<h3>'.
480: &mt('Expired spreadsheet caches for student [_1]',
481: $sname.'@'.$sdom).
482: '</h3>');
483: }
484: }
485: }
486: ##
487: ## Make the spreadsheet
488: &Apache::Spreadsheet::initialize_spreadsheet_package();
489: my $spreadsheet = undef;
490: if ($sheettype eq 'classcalc') {
491: $spreadsheet = Apache::classcalc->new($name,$domain,$filename,undef,
492: $section,\@groups);
493: } elsif ($sheettype eq 'studentcalc') {
494: $spreadsheet = Apache::studentcalc->new($name,$domain,$filename,undef,
495: $section,\@groups);
496: } elsif ($sheettype eq 'assesscalc' &&
497: defined($symb) &&
498: $allowed_to_edit) {
499: $spreadsheet = Apache::assesscalc->new($name,$domain,$filename,$symb,
500: $section,\@groups);
501: } else {
502: return HTTP_NOT_ACCEPTABLE;
503: }
504: if (! defined($spreadsheet)) {
505: # error error - run in circles, scream and shout
506: return;
507: }
508: $spreadsheet->initialize();
509: #
510: # Output selector
511: ##
512: ## Editing/loading/saving
513: if ($allowed_to_edit) {
514: my ($html,$action_message) = &file_dialogs($spreadsheet);
515: if ($env{'form.makedefault'}) {
516: $spreadsheet->make_default();
517: if ($action_message) {
518: $action_message .= '<br />';
519: }
520: $action_message .= &mt('Made this spreadsheet the default');
521: if ($sheettype eq 'classcalc') {
522: $action_message .= ' '.&mt('for the course');
523: } elsif ($sheettype eq 'studentcalc') {
524: $action_message .= ' '.&mt('for all students');
525: } elsif ($sheettype eq 'assesscalc') {
526: $action_message .= ' '.&mt('for all assessments');
527: }
528: $action_message .= '.';
529: }
530: $r->print('<table><tr><td valign="top">'.
531: $spreadsheet->html_header().
532: '</td>'.
533: '<td valign="center">'.$html."</td></tr></table>\n");
534: if ($action_message ne '') {
535: $r->print(<<END);
536: <table>
537: <tr><td valign="top"><b>Last Action:</b></td>
538: <td> </td>
539: <td>$action_message</td>
540: </tr>
541: </table>
542: END
543: }
544: $r->rflush();
545: } else {
546: $r->print('<table><tr><td>'.$spreadsheet->html_header().
547: "</td></tr></table>\n");
548: }
549: $r->rflush();
550: #
551: $r->print("<table><tr>");
552: $r->print('<td><input type="submit" value="'.
553: &mt('Generate Spreadsheet').'" />'.
554: '</td>');
555: if ($allowed_to_view) {
556: $r->print('<td>'.
557: &Apache::loncommon::help_open_topic("Spreadsheet_About",
558: 'Spreadsheet Help').
559: '</td>');
560: }
561: if ($allowed_to_edit) {
562: $r->print('<td>'.
563: &Apache::loncommon::help_open_topic("Spreadsheet_Editing",
564: 'Editing Help').
565: '</td>');
566: }
567: $r->print('</tr></table>');
568: #
569: # Keep track of the filename
570: $r->print(&hiddenfield('filename',$filename));
571: #
572: # Keep track of the number of times we have been called, sort of.
573: $r->print(&hiddenfield('not_first_run','1'));
574: #
575: if (exists($env{'form.not_first_run'}) || $sheettype ne 'classcalc') {
576: $r->print($spreadsheet->get_html_title());
577: if ($allowed_to_view || $allowed_to_edit) {
578: $r->print($spreadsheet->parent_link());
579: }
580: $r->rflush();
581: $spreadsheet->display($r);
582: }
583: $r->print('</form>');
584: $r->print(&Apache::loncommon::end_page());
585: $spreadsheet->clear_package();
586: return OK;
587: }
588:
589: 1;
590:
591: __END__
592:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>