Annotation of loncom/interface/spreadsheet/assesscalc.pm, revision 1.58
1.1 matthew 1: #
1.58 ! albertel 2: # $Id: assesscalc.pm,v 1.57 2006/12/21 02:54:19 albertel 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: assesscalc
34:
35: =head1 SYNOPSIS
36:
37: =head1 DESCRIPTION
38:
39: =cut
40:
41: ###################################################
42: ### AssessSheet ###
43: ###################################################
44: package Apache::assesscalc;
45:
46: use strict;
1.18 matthew 47: use warnings FATAL=>'all';
48: no warnings 'uninitialized';
1.1 matthew 49: use Apache::Constants qw(:common :http);
50: use Apache::lonnet;
1.2 matthew 51: use Apache::loncommon;
1.1 matthew 52: use Apache::Spreadsheet;
1.25 matthew 53: use Apache::loncoursedata();
1.1 matthew 54: use HTML::Entities();
55: use Spreadsheet::WriteExcel;
56: use GDBM_File;
57: use Time::HiRes;
1.26 www 58: use Apache::lonlocal;
1.1 matthew 59:
60: @Apache::assesscalc::ISA = ('Apache::Spreadsheet');
61:
62: ########################################################
63: ########################################################
64:
65: =pod
66:
67: =head2 Package Variables
68:
69: =over 4
70:
71: =item %Exportrows
72:
73: =item $current_name
74:
75: =item $current_domain
76:
77: =item $current_course
78:
79: =item %parmhash
80:
81: =item %nice_parameter_name
82:
83: =item %useropt
84:
85: =item %courseopt
86:
87: =back
88:
89: =cut
90:
91: ########################################################
92: ########################################################
93:
94: my %Exportrows;
1.22 matthew 95: my %newExportrows;
1.1 matthew 96:
97: my $current_name;
98: my $current_domain;
99: my $current_course;
100:
101: my %parmhash;
102: my %nice_parameter_name;
103:
104: my %useropt;
1.22 matthew 105: my %userdata;
1.1 matthew 106: my %courseopt;
1.37 matthew 107: my $navmap;
1.1 matthew 108:
109: ########################################################
110: ########################################################
111:
112: =pod
113:
114: =head2 Package Subroutines
115:
116: =item &clear_package()
117:
1.22 matthew 118: Reset all package variables and clean up caches.
1.1 matthew 119:
120: =cut
121:
122: ########################################################
123: ########################################################
124: sub clear_package {
1.22 matthew 125: if (defined($current_name) &&
126: defined($current_domain) &&
127: defined($current_course) &&
1.39 albertel 128: $current_course eq $env{'request.course.id'} &&
1.22 matthew 129: %newExportrows) {
130: &save_cached_export_rows($current_name,$current_domain);
131: }
1.1 matthew 132: undef(%Exportrows);
1.22 matthew 133: undef(%newExportrows);
1.1 matthew 134: undef($current_name);
135: undef($current_domain);
136: undef($current_course);
137: undef(%useropt);
1.22 matthew 138: undef(%userdata);
1.1 matthew 139: undef(%courseopt);
1.37 matthew 140: undef($navmap);
1.1 matthew 141: }
142:
1.22 matthew 143: sub save_cached_export_rows {
144: my ($sname,$sdomain) = @_;
145: my $result = &Apache::lonnet::put
1.39 albertel 146: ('nohist_calculatedsheets_'.$env{'request.course.id'},
1.22 matthew 147: $newExportrows{$sname.':'.$sdomain},
148: $sdomain,$sname);
149: delete($newExportrows{$sname.':'.$sdomain});
150: }
151:
1.11 matthew 152: sub initialize {
1.37 matthew 153: my ($in_navmap) = @_;
1.11 matthew 154: &clear_package();
1.37 matthew 155: $navmap = $in_navmap;
156: if (! defined($navmap)) {
157: $navmap = Apache::lonnavmaps::navmap->new();
158: }
159: if (!defined($navmap)) {
160: &Apache::lonnet::logthis('assesscalc:Can not open Coursemap');
161: }
1.25 matthew 162: &Apache::loncoursedata::clear_internal_caches();
1.11 matthew 163: }
164:
1.1 matthew 165: ########################################################
166: ########################################################
167:
168: =pod
169:
170: =item &initialize_package()
171:
172: =cut
173:
174: ########################################################
175: ########################################################
176: sub initialize_package {
1.57 albertel 177: my ($sname,$sdomain,$in_navmap,$calling_sheet) = @_;
1.1 matthew 178: $current_name = $sname;
179: $current_domain = $sdomain;
1.37 matthew 180: $navmap = $in_navmap;
1.21 matthew 181: undef(%useropt);
1.22 matthew 182: undef(%userdata);
1.39 albertel 183: if ($current_course ne $env{'request.course.id'}) {
184: $current_course = $env{'request.course.id'};
1.13 matthew 185: undef(%courseopt);
186: }
1.1 matthew 187: &load_cached_export_rows();
1.57 albertel 188: &load_parameter_caches($calling_sheet);
1.25 matthew 189: &Apache::loncoursedata::clear_internal_caches();
1.1 matthew 190: }
191:
1.22 matthew 192:
1.1 matthew 193: ########################################################
194: ########################################################
195:
196: =pod
197:
198: =item &load_parameter_caches()
199:
200: =cut
201:
202: ########################################################
203: ########################################################
204: sub load_parameter_caches {
1.57 albertel 205: my ($calling_sheet) = @_;
1.1 matthew 206: my $userprefix = $current_name.':'.$current_domain.'_';
207: $userprefix =~ s/:/_/g;
208: #
209: # Course Parameters Cache
210: if (! %courseopt) {
1.39 albertel 211: $current_course = $env{'request.course.id'};
1.1 matthew 212: undef(%courseopt);
213: if (! defined($current_name) || ! defined($current_domain)) {
214: return;
215: }
1.39 albertel 216: my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
217: my $id = $env{'course.'.$env{'request.course.id'}.'.num'};
1.1 matthew 218: my %Tmp = &Apache::lonnet::dump('resourcedata',$dom,$id);
219: while (my ($name,$value) = each(%Tmp)) {
1.3 matthew 220: $courseopt{$name}=$value;
1.1 matthew 221: }
222: }
223: if (! %useropt) {
224: my %Tmp = &Apache::lonnet::dump('resourcedata',
225: $current_domain,$current_name);
226: while (my ($name,$value) = each(%Tmp)) {
227: if ($name =~ /^error: 2/ || $name =~ /no such file/) {
228: undef(%useropt);
229: last;
230: }
231: $useropt{$userprefix.$name}=$value;
232: }
1.21 matthew 233: $useropt{'loadtime'} = time;
1.1 matthew 234: }
1.22 matthew 235: if (! %userdata) {
236: %userdata = &Apache::loncoursedata::get_current_state($current_name,
237: $current_domain);
1.57 albertel 238: my ($tmp) = %userdata;
239: if ($tmp =~ /^error:(.*)/) {
240: $calling_sheet->set_calcerror($1);
241: }
1.22 matthew 242: $userdata{'loadtime'} = time;
243: }
244: return;
1.1 matthew 245: }
246:
247: ########################################################
248: ########################################################
249:
250: =pod
251:
252: =head2 assesscalc object methods
253:
254: =cut
255:
256: ########################################################
257: ########################################################
1.22 matthew 258: sub ensure_current_caches {
1.1 matthew 259: my $self = shift;
1.19 matthew 260: ##
261: ## Check for a modified parameters
262: ##
1.1 matthew 263: if (! defined($current_course) ||
1.39 albertel 264: $current_course ne $env{'request.course.id'} ) {
265: $current_course = $env{'request.course.id'};
1.1 matthew 266: undef(%courseopt);
1.22 matthew 267: undef(%useropt);
268: undef(%userdata);
1.1 matthew 269: }
1.19 matthew 270: ##
271: ## Check for new user
272: ##
1.1 matthew 273: if (! defined($current_name) || $current_name ne $self->{'name'} ||
274: ! defined($current_domain) || $current_domain ne $self->{'domain'}) {
275: $current_domain = $self->{'domain'};
276: $current_name = $self->{'name'};
277: undef(%useropt);
1.22 matthew 278: undef(%userdata);
1.1 matthew 279: }
1.57 albertel 280: &load_parameter_caches($self);
1.1 matthew 281: }
282:
283: ##################################################
284: ##################################################
285:
286: =pod
287:
288: =item &parmval()
289:
290: Determine the value of a parameter.
291:
292: Inputs: $what, the parameter needed, $symb, $uname, $udom, $csec
293:
294: Returns: The value of a parameter, or '' if none.
295:
296: This function cascades through the possible levels searching for a value for
297: a parameter. The levels are checked in the following order:
1.52 raeburn 298: user, course (at group, section level and course level), map, and
299: lonnet::metadata.
1.1 matthew 300: This function uses %parmhash, which must be tied prior to calling it.
301: This function also requires %courseopt and %useropt to be initialized for
302: this user and course.
303:
304: =cut
305:
306: ##################################################
307: ##################################################
308: sub parmval {
309: my $self = shift;
1.53 raeburn 310: my ($what,$symb,$uname,$udom,$csec,$recurse,$mapname,$id,$fn,$groups)=@_;
311: $uname = $self->{'name'} if (! defined($uname));
312: $udom = $self->{'domain'} if (! defined($udom));
313: $csec = $self->{'section'} if (! defined($csec));
314: $groups = $self->{'groups'} if (! defined($groups));
315: $symb = $self->{'symb'} if (! defined($symb));
1.1 matthew 316: #
317: my $result='';
318: #
319: # This should be a
1.44 albertel 320: if (!defined($mapname) || !defined($id) || !defined($fn)) {
321: ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
1.55 albertel 322: $mapname = &Apache::lonnet::deversion($mapname);
1.44 albertel 323: }
1.1 matthew 324: # Cascading lookup scheme
325: my $rwhat=$what;
326: $what =~ s/^parameter\_//;
327: $what =~ s/\_([^\_]+)$/\.$1/;
328: #
329: my $symbparm = $symb.'.'.$what;
330: my $mapparm = $mapname.'___(all).'.$what;
1.3 matthew 331: my $courseprefix = $self->{'cid'};
1.1 matthew 332: my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
333: #
1.3 matthew 334: my $seclevel = $courseprefix.'.['.$csec.'].'.$what;
335: my $seclevelr = $courseprefix.'.['.$csec.'].'.$symbparm;
336: my $seclevelm = $courseprefix.'.['.$csec.'].'.$mapparm;
337: #
338: my $courselevel = $courseprefix.'.'.$what;
339: my $courselevelr = $courseprefix.'.'.$symbparm;
340: my $courselevelm = $courseprefix.'.'.$mapparm;
341: #
342: my $ucourselevel = $usercourseprefix.'.'.$what;
343: my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
344: my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
1.52 raeburn 345: # check user
1.1 matthew 346: if (defined($uname)) {
1.3 matthew 347: return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
348: return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
349: return $useropt{$ucourselevel} if (defined($useropt{$ucourselevel}));
1.1 matthew 350: }
1.53 raeburn 351: # check groups
1.54 raeburn 352: if (defined($groups) && ref($groups) eq 'ARRAY') {
1.53 raeburn 353: foreach my $group (@{$groups}) {
354: foreach my $level ($symbparm,$mapparm,$what) {
355: my $item = $courseprefix.'.['.$group.'].'.$level;
356: if (defined($courseopt{$item})) {
357: return $courseopt{$item};
358: }
359: }
360: }
1.52 raeburn 361: }
1.1 matthew 362: # check section
363: if (defined($csec)) {
364: return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
365: return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
366: return $courseopt{$seclevel} if (defined($courseopt{$seclevel}));
367: }
368: #
369: # check course
370: return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
371: # check map parms
372: my $thisparm = $parmhash{$symbparm};
373: return $thisparm if (defined($thisparm));
374: # check default
375: $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
376: return $thisparm if (defined($thisparm));
1.36 albertel 377: # check more course
378: return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
379: return $courseopt{$courselevel} if (defined($courseopt{$courselevel}));
380:
1.1 matthew 381: # Cascade Up
382: my $space=$what;
1.35 albertel 383: $space=~s/\.[^._]+$//;
1.1 matthew 384: if ($space ne '0') {
385: my @parts=split(/_/,$space);
386: my $id=pop(@parts);
387: my $part=join('_',@parts);
388: if ($part eq '') { $part='0'; }
389: my $newwhat=$rwhat;
390: $newwhat=~s/\Q$space\E/$part/;
1.44 albertel 391: my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1,
1.53 raeburn 392: $mapname,$id,$fn,$groups);
1.1 matthew 393: if (defined($partgeneral)) { return $partgeneral; }
394: }
1.17 albertel 395: if ($recurse) { return undef; }
396: my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
397: if (defined($pack_def)) { return $pack_def; }
1.1 matthew 398: #nothing defined
399: return '';
400: }
401:
1.8 matthew 402: sub get_html_title {
403: my $self = shift;
1.29 matthew 404: my ($assess_title,$name,$time) = $self->get_full_title();
1.8 matthew 405: my $title = '<h1>'.$assess_title.'</h1>'.
406: '<h2>'.$name.', '.
407: &Apache::loncommon::aboutmewrapper
408: ($self->{'name'}.'@'.$self->{'domain'},
409: $self->{'name'},$self->{'domain'});
410: $title .= '<h3>'.$time.'</h3>';
411: return $title;
412: }
413:
1.1 matthew 414: sub get_title {
415: my $self = shift;
1.16 matthew 416: if (($self->{'symb'} eq '_feedback') ||
417: ($self->{'symb'} eq '_evaluation') ||
418: ($self->{'symb'} eq '_discussion') ||
419: ($self->{'symb'} eq '_tutoring')) {
420: my $assess_title = ucfirst($self->{'symb'});
1.8 matthew 421: $assess_title =~ s/^_//;
1.29 matthew 422: return $assess_title;
1.1 matthew 423: } else {
1.29 matthew 424: return &Apache::lonnet::gettitle($self->{'symb'});
1.1 matthew 425: }
1.29 matthew 426: }
427:
428: sub get_full_title {
429: my $self = shift;
430: my @title = ($self->get_title());
1.2 matthew 431: # Look up the users identifying information
432: # Get the users information
1.34 albertel 433: my $name = &Apache::loncommon::plainname($self->{'name'},
434: $self->{'domain'});
1.8 matthew 435: push (@title,$name);
1.26 www 436: push (@title,&Apache::lonlocal::locallocaltime(time));
1.8 matthew 437: return @title;
1.1 matthew 438: }
439:
440: sub parent_link {
441: my $self = shift;
442: my $link .= '<p><a href="/adm/studentcalc?'.
443: 'sname='.$self->{'name'}.
444: '&sdomain='.$self->{'domain'}.'">'.
1.26 www 445: &mt('Student level sheet').'</a></p>'."\n";
1.1 matthew 446: return $link;
447: }
448:
449: sub outsheet_html {
450: my $self = shift;
451: my ($r) = @_;
1.29 matthew 452: ####################################
453: # Report any calculation errors #
454: ####################################
455: $r->print($self->html_report_error());
1.1 matthew 456: ###################################
457: # Determine table structure
458: ###################################
1.14 matthew 459: my $importcolor = '#FFFFFF';
1.15 matthew 460: my $exportcolor = '#FFFFAA';
1.1 matthew 461: my $num_uneditable = 1;
462: my $num_left = 52-$num_uneditable;
1.26 www 463: my %lt=&Apache::lonlocal::texthash(
464: 'as' => 'Assessment',
465: 'ca' => 'Calculations',
466: );
1.1 matthew 467: my $tableheader =<<"END";
468: <table border="2">
469: <tr>
1.26 www 470: <th colspan="2" rowspan="2"><font size="+2">$lt{'as'}</font></th>
1.14 matthew 471: <td bgcolor="$importcolor" colspan="$num_uneditable"> </td>
1.1 matthew 472: <td colspan="$num_left">
1.26 www 473: <b><font size="+1">$lt{'ca'}</font></b></td>
1.1 matthew 474: </tr><tr>
475: END
476: my $label_num = 0;
477: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
478: if ($label_num<$num_uneditable) {
1.14 matthew 479: $tableheader .= '<td bgcolor="'.$importcolor.'">';
1.1 matthew 480: } else {
481: $tableheader .= '<td>';
482: }
483: $tableheader .= "<b><font size=+1>$_</font></b></td>";
484: $label_num++;
485: }
486: $tableheader.="</tr>\n";
487: #
488: $r->print($tableheader);
489: #
490: # Print out template row
491: $r->print('<tr><td>Template</td><td> </td>'.
1.14 matthew 492: $self->html_template_row($num_uneditable,$importcolor).
493: "</tr>\n");
1.1 matthew 494: #
495: # Print out summary/export row
496: $r->print('<tr><td>Export</td><td>0</td>'.
1.14 matthew 497: $self->html_export_row($exportcolor)."</tr>\n");
1.1 matthew 498: #
499: # Prepare to output rows
500: $tableheader =<<"END";
501: <table border="2">
502: <tr><th>row</th><th>Item</th>
503: END
504: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
505: if ($label_num<$num_uneditable) {
1.14 matthew 506: $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1 matthew 507: } else {
508: $tableheader.='<th>';
509: }
510: $tableheader.="<b><font size=+1>$_</font></b></th>";
511: }
512: #
513: my $num_output = 0;
1.8 matthew 514: foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
1.28 matthew 515: if (! $self->parameter_part_is_valid(
516: $self->{'formulas'}->{'A'.$rownum}
517: )) {
518: next;
519: }
1.1 matthew 520: if ($num_output++ % 50 == 0) {
521: $r->print("</table>\n".$tableheader);
522: }
523: $r->print('<tr><td>'.$rownum.'</td>'.
1.14 matthew 524: $self->assess_html_row($rownum,$importcolor)."</tr>\n");
1.1 matthew 525: }
526: $r->print("</table>\n");
527: return;
528: }
529:
530: sub assess_html_row {
531: my $self = shift();
1.14 matthew 532: my ($row,$importcolor) = @_;
1.1 matthew 533: my $parameter_name = $self->{'formulas'}->{'A'.$row};
534: my @rowdata = $self->get_row($row);
535: my $num_cols_output = 0;
536: my $row_html;
1.48 albertel 537: my $name=$self->get_parm_name($parameter_name);
538: if ($name ne '') {
1.1 matthew 539: $name =~ s/ /\ /g;
540: $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
541: } else {
542: $row_html .= '<td>'.$parameter_name.'</td>';
543: }
544: foreach my $cell (@rowdata) {
1.10 matthew 545: if ($num_cols_output < 1) {
1.14 matthew 546: $row_html .= '<td bgcolor="'.$importcolor.'">';
1.10 matthew 547: $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
548: '#FFDDDD');
549: } else {
1.56 albertel 550: $row_html .= '<td bgcolor="#E0FFDD">';
1.10 matthew 551: $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
552: '#E0FFDD',1);
553: }
1.1 matthew 554: $row_html .= '</td>';
1.10 matthew 555: $num_cols_output++;
1.1 matthew 556: }
557: return $row_html;
558: }
559:
1.9 matthew 560: sub csv_rows {
561: # writes the meat of the spreadsheet to an excel worksheet. Called
562: # by Spreadsheet::outsheet_excel;
1.1 matthew 563: my $self = shift;
1.30 matthew 564: my ($connection,$filehandle) = @_;
1.9 matthew 565: #
566: # Write a header row
567: $self->csv_output_row($filehandle,undef,
1.26 www 568: (&mt('Parameter'),&mt('Description'),&mt('Value')));
1.9 matthew 569: #
570: # Write each row
571: foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
572: my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
1.48 albertel 573: my $description = $self->get_parm_name($parameter_name);
1.9 matthew 574: $self->csv_output_row($filehandle,$rownum,
575: $parameter_name,$description);
576: }
577: return;
1.1 matthew 578: }
579:
1.8 matthew 580: sub excel_rows {
581: # writes the meat of the spreadsheet to an excel worksheet. Called
582: # by Spreadsheet::outsheet_excel;
1.1 matthew 583: my $self = shift;
1.38 matthew 584: my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
1.30 matthew 585: return if (! ref($worksheet));
1.8 matthew 586: #
587: # Write a header row
588: $cols_output = 0;
589: foreach my $value ('Parameter','Description','Value') {
1.38 matthew 590: $worksheet->write($rows_output,$cols_output++,$value,$format->{'h4'});
1.8 matthew 591: }
1.38 matthew 592: $rows_output++;
1.8 matthew 593: #
594: # Write each row
595: foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
596: my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
1.48 albertel 597: my $description = $self->get_parm_name($parameter_name);
1.8 matthew 598: $self->excel_output_row($worksheet,$rownum,$rows_output++,
599: $parameter_name,$description);
600: }
601: return;
1.1 matthew 602: }
603:
1.21 matthew 604: ##
605: ## Routines to support assesscalc::compute
606: ##
1.48 albertel 607: sub get_parm {
1.21 matthew 608: my $self = shift;
609: my @Mandatory_parameters = @_;
1.48 albertel 610: my %parameters;
1.21 matthew 611: #
1.23 albertel 612: my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
1.21 matthew 613: my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
614: foreach my $parm (@Mandatory_parameters,@Metadata) {
615: next if ($parm !~ /^(resource\.|stores|parameter)_/);
616: my $cleaned_name = $parm;
617: $cleaned_name =~ s/^resource\./stores_/;
618: $cleaned_name =~ s/\./_/g;
1.48 albertel 619: $parameters{$cleaned_name}=1;
620: }
621: return (keys(%parameters));
622: }
623:
624: sub get_parm_name {
625: my $self = shift;
626: my $parm = shift;
627: my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
628: my $display = &Apache::lonnet::metadata($srcf,$parm.'.display');
629: if (! $display) {
630: $display .= &Apache::lonnet::metadata($srcf,$parm.'.name');
1.21 matthew 631: }
1.48 albertel 632: return $display;
1.21 matthew 633: }
634:
635: sub get_parameter_values {
636: my $self = shift();
637: my @Parameters;
638: my ($parameters) = @_;
639: if (!ref($parameters)) {
640: @Parameters = @_;
641: } elsif (ref($parameters) eq 'ARRAY') {
642: @Parameters = @$parameters;
643: } elsif (ref($parameters) eq 'HASH') {
644: @Parameters = keys(%$parameters);
645: }
646: #
647: my %parameters;
648: #
649: my $filename = $self->{'coursefilename'}.'_parms.db';
650: if (tie(%parmhash,'GDBM_File',
651: $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
1.44 albertel 652: my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($self->{'symb'});
1.55 albertel 653: $mapname = &Apache::lonnet::deversion($mapname);
1.21 matthew 654: foreach my $parmname (@Parameters) {
1.47 albertel 655: my $value = $self->parmval($parmname,$self->{'symb'},
656: $self->{'name'},$self->{'domain'},
657: $self->{'section'},undef,
1.53 raeburn 658: $mapname,$id,$fn,$self->{'groups'});
1.21 matthew 659: $parameters{$parmname} =$value;
660: }
661: untie(%parmhash);
662: } else {
663: $self->logthis('unable to tie '.$filename);
664: }
665: return %parameters;
666: }
667:
668: sub deal_with_export_row {
669: my $self = shift();
670: my @exportarray = @_;
671: $Exportrows{$self->{'symb'}}->{'time'} = time;
672: $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
673: #
674: # Save the export data
675: $self->save_export_data();
676: return;
677: }
678:
1.22 matthew 679: sub get_problem_state {
680: my $self = shift;
681: my %student_parameters;
682: if (exists($userdata{$self->{'symb'}}) &&
683: ref($userdata{$self->{'symb'}}) eq 'HASH') {
684: %student_parameters = %{$userdata{$self->{'symb'}}};
685: }
686: return %student_parameters;
687: }
1.21 matthew 688:
1.28 matthew 689: sub determine_parts {
690: my $self = shift;
1.41 albertel 691: my $check_hidden = shift;
1.28 matthew 692: if (exists($self->{'Parts'}) && ref($self->{'Parts'}) eq 'HASH') {
693: return;
694: }
695: my (undef,undef,$url) = &Apache::lonnet::decode_symb($self->{'symb'});
696: my $src = &Apache::lonnet::clutter($url);
697: return if (! defined($src));
698: my %Parts;
699: my $metadata = &Apache::lonnet::metadata($src,'packages');
700: foreach (split(',',$metadata)) {
701: my ($part) = (/^part_(.*)$/);
1.41 albertel 702: if (!defined($part)) { next; }
703: if (!$check_hidden) { $Parts{$part}++; next; }
704: if (!&Apache::loncommon::check_if_partid_hidden
705: ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
1.28 matthew 706: ) {
707: $Parts{$part}++;
708: }
709: }
710: # Make sure part 0 is defined.
711: $Parts{'0'}++;
712: $self->{'Parts'} = \%Parts;
713: return;
714: }
715:
716: sub parameter_part_is_valid {
717: my $self = shift;
718: my ($parameter) = @_;
719: return 1 if ($parameter eq 'timestamp');
720: if (! defined($self->{'Parts'}) ||
721: ! ref ($self->{'Parts'}) ||
722: ref($self->{'Parts'}) ne 'HASH') {
723: return 1;
724: }
725: #
1.51 albertel 726: my ($start,@pieces)=split('_',$parameter);
727: if ( $start !~ m/^(resource|stores|parameter)$/) { return 0; }
728: while (@pieces) {
729: pop(@pieces);
730: my $testpart=join('_',@pieces);
731: if (exists($self->{'Parts'}->{$testpart}) &&
732: $self->{'Parts'}->{$testpart} ) {
733: return 1;
734: }
1.28 matthew 735: }
1.51 albertel 736: return 0;
1.28 matthew 737: }
738:
1.1 matthew 739: sub compute {
740: my $self = shift;
1.19 matthew 741: my ($r) = @_;
1.1 matthew 742: $self->initialize_safe_space();
1.11 matthew 743: #########################################
744: #########################################
745: ### ###
746: ### Retrieve the problem parameters ###
747: ### ###
748: #########################################
749: #########################################
750: my @Mandatory_parameters = ("stores_0_solved",
751: "stores_0_awarddetail",
752: "stores_0_awarded",
753: "timestamp",
754: "stores_0_tries",
755: "stores_0_award");
1.1 matthew 756: #
757: # Definitions
758: undef(%nice_parameter_name);
759: my %parameters; # holds underscored parameters by name
760: #
761: # Get the metadata fields and determine their proper names
1.48 albertel 762: my @parameters=$self->get_parm(@Mandatory_parameters);
1.1 matthew 763: #
764: # Get the values of the metadata fields
1.22 matthew 765: $self->ensure_current_caches();
1.48 albertel 766: %parameters = $self->get_parameter_values(@parameters);
1.1 matthew 767: #
768: # Clean out unnecessary parameters
769: foreach (keys(%parameters)) {
770: delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
771: }
772: #
773: # Get the students performance data
1.43 albertel 774: $self->determine_parts(($parameters{'parameter_0_hiddenparts'} ne ''));
1.22 matthew 775: my %student_parameters = $self->get_problem_state();
1.1 matthew 776: while (my ($parm,$value) = each(%student_parameters)) {
777: $parm =~ s/^resource\./stores_/;
778: $parm =~ s/\./_/g;
1.41 albertel 779: # Clean out any bad parameters
780: next if (! $self->parameter_part_is_valid($parm));
781: $parameters{$parm} = $value;
1.1 matthew 782: }
783: #
784: # Set up the formulas and parameter values
785: my %f=$self->formulas();
786: my %c;
787: #
1.5 matthew 788: # Check for blackout requirements
1.39 albertel 789: if ((!exists($env{'request.role.adv'}) || !$env{'request.role.adv'})) {
1.5 matthew 790: while (my ($parm,$value) = each(%parameters)) {
791: last if ($self->blackout());
792: next if ($parm !~ /^(parameter_.*)_problemstatus$/);
1.24 matthew 793: if ($parameters{$1.'_answerdate'} ne '' &&
1.21 matthew 794: $parameters{$1.'_answerdate'} < time) {
795: next;
796: }
1.5 matthew 797: if (lc($value) eq 'no') {
798: # We must blackout this sheet
799: $self->blackout(1);
800: }
801: }
802: }
803: #
804: # Move the parameters into the spreadsheet
1.1 matthew 805: while (my ($parm,$value) = each(%parameters)) {
806: my $cell = 'A'.$self->get_row_number_from_key($parm);
807: $f{$cell} = $parm;
1.31 matthew 808: if ($parm =~ /_submission$/ && $value =~ /(\{|\})/) {
809: $value = 'witheld';
810: }
1.49 albertel 811: $value = 'q{'.$value.'}' if ($value =~/([^\d\.]|\.\.)/);
1.6 matthew 812: $c{$parm} = $value;
1.1 matthew 813: }
1.50 albertel 814: foreach my $cell (grep(/^A/,keys(%f))) {
815: # Clean out any bad formulas
816: next if (exists($c{$f{$cell}}));
817: next if ($cell eq 'A0');
818: delete($f{$cell});
819: }
1.6 matthew 820: $self->formulas(\%f);
821: $self->constants(\%c);
1.1 matthew 822: $self->calcsheet();
823: #
824: # Store export row in cache
825: my @exportarray = $self->exportrow();
1.21 matthew 826: $self->deal_with_export_row(@exportarray);
1.12 matthew 827: $self->save() if ($self->need_to_save());
1.1 matthew 828: return;
829: }
830:
831: ##
832: ## sett overrides Spreadsheet::sett
833: ##
834: sub sett {
835: my $self = shift;
836: my %t=();
1.40 albertel 837: undef(%Apache::Spreadsheet::sheet_values);
1.1 matthew 838: #
839: # Deal with the template row by copying the template formulas into each
840: # row.
841: foreach my $col ($self->template_cells()) {
842: next if ($col=~/^A/);
843: foreach my $row ($self->rows()) {
844: # Get the name of this cell
845: my $cell=$col.$row;
846: # Grab the template declaration
847: $t{$cell}=$self->formula('template_'.$col);
848: # Replace '#' with the row number
849: $t{$cell}=~s/\#/$row/g;
850: # Replace '....' with ','
851: $t{$cell}=~s/\.\.+/\,/g;
852: # Replace 'A0' with the value from 'A0'
853: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
854: # Replace parameters
1.58 ! albertel 855: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge;
1.1 matthew 856: }
857: }
858: #
859: # Deal with the cells which have formulas
860: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
861: next if ($cell =~ /template_/);
862: if ($cell =~ /^A/ && $cell ne 'A0') {
1.45 albertel 863: if ($formula !~ /^\!/
864: && exists($self->{'constants'}->{$formula})
865: && $self->{'constants'}->{$formula} ne ''
866: ) {
1.40 albertel 867: $Apache::Spreadsheet::sheet_values{$cell}=
1.49 albertel 868: eval($self->{'constants'}->{$formula});
1.1 matthew 869: }
870: } else {
871: $t{$cell}=$formula;
872: $t{$cell}=~s/\.\.+/\,/g;
873: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.58 ! albertel 874: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge;
1.1 matthew 875: }
876: }
877: # Put %t into the safe space
878: %{$self->{'safe'}->varglob('t')}=%t;
879: }
880:
881:
882: ########################################################
883: ########################################################
884:
885: =pod
886:
887: =item &load_cached_export_rows()
888:
889: Retrieves and parsers the export rows of the assessment spreadsheets.
890: These rows are saved in the students directory in the format:
891:
892: sname:sdom:assesscalc:symb.time => time
893:
894: sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
895:
896: =cut
897:
898: ########################################################
899: ########################################################
900: sub load_cached_export_rows {
1.18 matthew 901: undef(%Exportrows);
1.1 matthew 902: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
1.39 albertel 903: $env{'request.course.id'},
1.1 matthew 904: $current_domain,$current_name,undef);
905: if ($tmp[0]!~/^error/) {
906: my %tmp = @tmp;
1.39 albertel 907: my $default_filename = $env{'course.'.$env{'request.course.id'}.
1.1 matthew 908: '.spreadsheet_default_assesscalc'};
909: # We only got one key, so we will access it directly.
910: while (my ($key,$sheetdata) = each(%tmp)) {
911: my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
1.33 matthew 912: if (! defined($sname) || $sname eq '' ||
913: ! defined($sdom) || $sdom eq '' ) {
914: next;
915: }
1.1 matthew 916: if ($symb =~ /\.time$/) {
917: $symb =~ s/\.time$//;
918: $Exportrows{$symb}->{'time'} = $sheetdata;
919: } else {
920: $sheetdata =~ s/^(.*)___=___//;
921: my $filename = $1;
922: $filename = $default_filename if (! defined($filename));
923: my @Data = split('___;___',$sheetdata);
924: $Exportrows{$symb}->{$filename} = \@Data;
925: }
926: }
927: }
928: }
929:
930: #############################################
931: #############################################
932:
933: =pod
934:
935: =item &export_data
936:
937: Returns the export data associated with the spreadsheet. Computes the
938: spreadsheet only if necessary.
939:
940: =cut
941:
942: #############################################
943: #############################################
944: sub export_data {
945: my $self = shift;
1.19 matthew 946: my ($r) = @_;
1.1 matthew 947: my $symb = $self->{'symb'};
1.39 albertel 948: if (! exists($env{'request.role.adv'}) || ! $env{'request.role.adv'} ||
1.5 matthew 949: ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb}) ||
1.1 matthew 950: ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
951: ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
1.18 matthew 952: ! defined($Exportrows{$symb}->{$self->{'filename'}}) ||
953: ! ref($Exportrows{$symb}->{$self->{'filename'}})
954: ) {
1.19 matthew 955: $self->compute($r);
1.1 matthew 956: }
1.29 matthew 957: my @Data;
958: if ($self->badcalc()) {
959: @Data = ();
960: } else {
961: @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
962: if ($Data[0] =~ /^(.*)___=___/) {
963: $self->{'sheetname'} = $1;
964: $Data[0] =~ s/^(.*)___=___//;
965: }
966: for (my $i=0;$i<$#Data;$i++) {
967: if ($Data[$i]=~/\D/ && defined($Data[$i])) {
968: $Data[$i]="'".$Data[$i]."'";
969: }
970: }
1.1 matthew 971: }
972: return @Data;
973: }
974:
975: #############################################
976: #############################################
977:
978: =pod
979:
980: =item &save_export_data()
981:
982: Writes the export data for this spreadsheet to the students cache.
983:
984: =cut
985:
986: #############################################
987: #############################################
988: sub save_export_data {
989: my $self = shift;
1.7 matthew 990: return if ($self->temporary());
1.1 matthew 991: my $student = $self->{'name'}.':'.$self->{'domain'};
992: my $symb = $self->{'symb'};
1.29 matthew 993: if ($self->badcalc()){
994: # do not save data away when calculations have not been done properly.
995: delete($Exportrows{$symb});
996: return;
997: }
1.1 matthew 998: if (! exists($Exportrows{$symb}) ||
999: ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
1000: return;
1001: }
1002: my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
1003: my $timekey = $key.'.time';
1.22 matthew 1004: my $newstore= join('___;___',
1005: map {s/[^[:print:]]//g;$_;} # strip out unprintable
1006: @{$Exportrows{$symb}->{$self->{'filename'}}});
1.1 matthew 1007: $newstore = $self->{'filename'}.'___=___'.$newstore;
1.22 matthew 1008: $newExportrows{$student}->{$key} = $newstore;
1009: $newExportrows{$student}->{$timekey} = $Exportrows{$symb}->{'time'};
1.1 matthew 1010: return;
1011: }
1012:
1013: 1;
1014:
1015: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>