Annotation of loncom/interface/spreadsheet/assesscalc.pm, revision 1.61
1.1 matthew 1: #
1.61 ! raeburn 2: # $Id: assesscalc.pm,v 1.60 2014/02/11 19:11:30 bisitz 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: }
1.59 raeburn 159: if ((!defined($navmap)) || (!ref($navmap))) {
1.37 matthew 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='';
1.61 ! raeburn 318: my @recurseup;
1.1 matthew 319: #
320: # This should be a
1.44 albertel 321: if (!defined($mapname) || !defined($id) || !defined($fn)) {
322: ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
1.55 albertel 323: $mapname = &Apache::lonnet::deversion($mapname);
1.44 albertel 324: }
1.1 matthew 325: # Cascading lookup scheme
326: my $rwhat=$what;
327: $what =~ s/^parameter\_//;
328: $what =~ s/\_([^\_]+)$/\.$1/;
329: #
330: my $symbparm = $symb.'.'.$what;
1.61 ! raeburn 331: my $recurseparm=$mapname.'___(rec).'.$what;
1.1 matthew 332: my $mapparm = $mapname.'___(all).'.$what;
1.3 matthew 333: my $courseprefix = $self->{'cid'};
1.1 matthew 334: my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
335: #
1.3 matthew 336: my $seclevel = $courseprefix.'.['.$csec.'].'.$what;
337: my $seclevelr = $courseprefix.'.['.$csec.'].'.$symbparm;
1.61 ! raeburn 338: my $secleveli = $courseprefix.'.['.$csec.'].'.$recurseparm;
1.3 matthew 339: my $seclevelm = $courseprefix.'.['.$csec.'].'.$mapparm;
340: #
341: my $courselevel = $courseprefix.'.'.$what;
342: my $courselevelr = $courseprefix.'.'.$symbparm;
1.61 ! raeburn 343: my $courseleveli = $courseprefix.'.'.$recurseparm;
1.3 matthew 344: my $courselevelm = $courseprefix.'.'.$mapparm;
345: #
346: my $ucourselevel = $usercourseprefix.'.'.$what;
347: my $ucourselevelr = $usercourseprefix.'.'.$symbparm;
1.61 ! raeburn 348: my $ucourseleveli = $usercourseprefix.'.'.$recurseparm;
1.3 matthew 349: my $ucourselevelm = $usercourseprefix.'.'.$mapparm;
1.52 raeburn 350: # check user
1.1 matthew 351: if (defined($uname)) {
1.3 matthew 352: return $useropt{$ucourselevelr} if (defined($useropt{$ucourselevelr}));
353: return $useropt{$ucourselevelm} if (defined($useropt{$ucourselevelm}));
1.61 ! raeburn 354: return $useropt{$ucourseleveli} if (defined($useropt{$ucourseleveli}));
! 355: unless (@recurseup > 0) {
! 356: @recurseup = &Apache::lonnet::get_map_hierarchy($mapname);
! 357: }
! 358: foreach my $item (@recurseup) {
! 359: my $norecursechk=$usercourseprefix.'.'.$item.'___(all).'.$what;
! 360: last if (defined($useropt{$norecursechk}));
! 361: my $recursechk=$usercourseprefix.'.'.$item.'___(rec).'.$what;
! 362: return $useropt{$recursechk} if (defined($useropt{$recursechk}));
! 363: }
1.3 matthew 364: return $useropt{$ucourselevel} if (defined($useropt{$ucourselevel}));
1.1 matthew 365: }
1.53 raeburn 366: # check groups
1.54 raeburn 367: if (defined($groups) && ref($groups) eq 'ARRAY') {
1.53 raeburn 368: foreach my $group (@{$groups}) {
1.61 ! raeburn 369: foreach my $level ($symbparm,$mapparm,$recurseparm,$what) {
1.53 raeburn 370: my $item = $courseprefix.'.['.$group.'].'.$level;
1.61 ! raeburn 371: if ($level eq $what) {
! 372: unless (@recurseup > 0) {
! 373: @recurseup = &Apache::lonnet::get_map_hierarchy($mapname);
! 374: }
! 375: foreach my $item (@recurseup) {
! 376: my $norecursechk=$courseprefix.'.['.$group.'].'.$item.'___(all).'.$what;
! 377: last if (defined($courseopt{$norecursechk}));
! 378: my $recursechk=$courseprefix.'.['.$group.'].'.$item.'___(rec).'.$what;
! 379: return $courseopt{$recursechk} if (defined($courseopt{$recursechk}));
! 380: }
! 381: }
1.53 raeburn 382: if (defined($courseopt{$item})) {
383: return $courseopt{$item};
384: }
385: }
386: }
1.52 raeburn 387: }
1.1 matthew 388: # check section
389: if (defined($csec)) {
390: return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
391: return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
1.61 ! raeburn 392: return $courseopt{$secleveli} if (defined($courseopt{$secleveli}));
! 393: unless (@recurseup > 0) {
! 394: @recurseup = &Apache::lonnet::get_map_hierarchy($mapname);
! 395: }
! 396: foreach my $item (@recurseup) {
! 397: my $norecursechk=$courseprefix.'.['.$csec.'].'.$item.'___(all).'.$what;
! 398: last if (defined($courseopt{$norecursechk}));
! 399: my $recursechk=$courseprefix.'.['.$csec.'].'.$item.'___(rec).'.$what;
! 400: return $courseopt{$recursechk} if (defined($courseopt{$secleveli}));
! 401: }
1.1 matthew 402: return $courseopt{$seclevel} if (defined($courseopt{$seclevel}));
403: }
404: #
405: # check course
406: return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
407: # check map parms
408: my $thisparm = $parmhash{$symbparm};
409: return $thisparm if (defined($thisparm));
410: # check default
411: $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
412: return $thisparm if (defined($thisparm));
1.36 albertel 413: # check more course
414: return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
1.61 ! raeburn 415: return $courseopt{$courseleveli} if (defined($courseopt{$courseleveli}));
! 416: unless (@recurseup > 0) {
! 417: @recurseup = &Apache::lonnet::get_map_hierarchy($mapname);
! 418: }
! 419: foreach my $item (@recurseup) {
! 420: my $norecursechk=$courseprefix.'.'.$item.'___(all).'.$what;
! 421: last if (defined($courseopt{$norecursechk}));
! 422: my $recursechk=$courseprefix.'.'.$item.'___(rec).'.$what;
! 423: return $courseopt{$recursechk} if (defined($courseopt{$recursechk}));
! 424: }
1.36 albertel 425: return $courseopt{$courselevel} if (defined($courseopt{$courselevel}));
426:
1.1 matthew 427: # Cascade Up
428: my $space=$what;
1.35 albertel 429: $space=~s/\.[^._]+$//;
1.1 matthew 430: if ($space ne '0') {
431: my @parts=split(/_/,$space);
432: my $id=pop(@parts);
433: my $part=join('_',@parts);
434: if ($part eq '') { $part='0'; }
435: my $newwhat=$rwhat;
436: $newwhat=~s/\Q$space\E/$part/;
1.44 albertel 437: my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec,1,
1.53 raeburn 438: $mapname,$id,$fn,$groups);
1.1 matthew 439: if (defined($partgeneral)) { return $partgeneral; }
440: }
1.17 albertel 441: if ($recurse) { return undef; }
442: my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$what);
443: if (defined($pack_def)) { return $pack_def; }
1.1 matthew 444: #nothing defined
445: return '';
446: }
447:
1.8 matthew 448: sub get_html_title {
449: my $self = shift;
1.29 matthew 450: my ($assess_title,$name,$time) = $self->get_full_title();
1.8 matthew 451: my $title = '<h1>'.$assess_title.'</h1>'.
452: '<h2>'.$name.', '.
453: &Apache::loncommon::aboutmewrapper
454: ($self->{'name'}.'@'.$self->{'domain'},
455: $self->{'name'},$self->{'domain'});
456: $title .= '<h3>'.$time.'</h3>';
457: return $title;
458: }
459:
1.1 matthew 460: sub get_title {
461: my $self = shift;
1.16 matthew 462: if (($self->{'symb'} eq '_feedback') ||
463: ($self->{'symb'} eq '_evaluation') ||
464: ($self->{'symb'} eq '_discussion') ||
465: ($self->{'symb'} eq '_tutoring')) {
466: my $assess_title = ucfirst($self->{'symb'});
1.8 matthew 467: $assess_title =~ s/^_//;
1.29 matthew 468: return $assess_title;
1.1 matthew 469: } else {
1.29 matthew 470: return &Apache::lonnet::gettitle($self->{'symb'});
1.1 matthew 471: }
1.29 matthew 472: }
473:
474: sub get_full_title {
475: my $self = shift;
476: my @title = ($self->get_title());
1.2 matthew 477: # Look up the users identifying information
478: # Get the users information
1.34 albertel 479: my $name = &Apache::loncommon::plainname($self->{'name'},
480: $self->{'domain'});
1.8 matthew 481: push (@title,$name);
1.26 www 482: push (@title,&Apache::lonlocal::locallocaltime(time));
1.8 matthew 483: return @title;
1.1 matthew 484: }
485:
486: sub parent_link {
487: my $self = shift;
488: my $link .= '<p><a href="/adm/studentcalc?'.
489: 'sname='.$self->{'name'}.
490: '&sdomain='.$self->{'domain'}.'">'.
1.26 www 491: &mt('Student level sheet').'</a></p>'."\n";
1.1 matthew 492: return $link;
493: }
494:
495: sub outsheet_html {
496: my $self = shift;
497: my ($r) = @_;
1.29 matthew 498: ####################################
499: # Report any calculation errors #
500: ####################################
501: $r->print($self->html_report_error());
1.1 matthew 502: ###################################
503: # Determine table structure
504: ###################################
1.14 matthew 505: my $importcolor = '#FFFFFF';
1.15 matthew 506: my $exportcolor = '#FFFFAA';
1.1 matthew 507: my $num_uneditable = 1;
508: my $num_left = 52-$num_uneditable;
1.26 www 509: my %lt=&Apache::lonlocal::texthash(
510: 'as' => 'Assessment',
511: 'ca' => 'Calculations',
512: );
1.1 matthew 513: my $tableheader =<<"END";
514: <table border="2">
515: <tr>
1.26 www 516: <th colspan="2" rowspan="2"><font size="+2">$lt{'as'}</font></th>
1.14 matthew 517: <td bgcolor="$importcolor" colspan="$num_uneditable"> </td>
1.1 matthew 518: <td colspan="$num_left">
1.26 www 519: <b><font size="+1">$lt{'ca'}</font></b></td>
1.1 matthew 520: </tr><tr>
521: END
522: my $label_num = 0;
523: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
524: if ($label_num<$num_uneditable) {
1.14 matthew 525: $tableheader .= '<td bgcolor="'.$importcolor.'">';
1.1 matthew 526: } else {
527: $tableheader .= '<td>';
528: }
529: $tableheader .= "<b><font size=+1>$_</font></b></td>";
530: $label_num++;
531: }
532: $tableheader.="</tr>\n";
533: #
534: $r->print($tableheader);
535: #
536: # Print out template row
1.60 bisitz 537: $r->print('<tr><td>'.&mt('Template').'</td><td> </td>'.
1.14 matthew 538: $self->html_template_row($num_uneditable,$importcolor).
539: "</tr>\n");
1.1 matthew 540: #
541: # Print out summary/export row
1.60 bisitz 542: $r->print('<tr><td>'.&mt('Export').'</td><td>0</td>'.
1.14 matthew 543: $self->html_export_row($exportcolor)."</tr>\n");
1.1 matthew 544: #
545: # Prepare to output rows
1.60 bisitz 546: $tableheader =
547: '<table border="2">'.
548: '<tr><th>'.&mt('Row').'</th>'.
549: '<th>'.&mt('Item').'</th>';
1.1 matthew 550: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
551: if ($label_num<$num_uneditable) {
1.14 matthew 552: $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1 matthew 553: } else {
554: $tableheader.='<th>';
555: }
556: $tableheader.="<b><font size=+1>$_</font></b></th>";
557: }
558: #
559: my $num_output = 0;
1.8 matthew 560: foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
1.28 matthew 561: if (! $self->parameter_part_is_valid(
562: $self->{'formulas'}->{'A'.$rownum}
563: )) {
564: next;
565: }
1.1 matthew 566: if ($num_output++ % 50 == 0) {
567: $r->print("</table>\n".$tableheader);
568: }
569: $r->print('<tr><td>'.$rownum.'</td>'.
1.14 matthew 570: $self->assess_html_row($rownum,$importcolor)."</tr>\n");
1.1 matthew 571: }
572: $r->print("</table>\n");
573: return;
574: }
575:
576: sub assess_html_row {
577: my $self = shift();
1.14 matthew 578: my ($row,$importcolor) = @_;
1.1 matthew 579: my $parameter_name = $self->{'formulas'}->{'A'.$row};
580: my @rowdata = $self->get_row($row);
581: my $num_cols_output = 0;
582: my $row_html;
1.48 albertel 583: my $name=$self->get_parm_name($parameter_name);
584: if ($name ne '') {
1.1 matthew 585: $name =~ s/ /\ /g;
586: $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
587: } else {
588: $row_html .= '<td>'.$parameter_name.'</td>';
589: }
590: foreach my $cell (@rowdata) {
1.10 matthew 591: if ($num_cols_output < 1) {
1.14 matthew 592: $row_html .= '<td bgcolor="'.$importcolor.'">';
1.10 matthew 593: $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,
594: '#FFDDDD');
595: } else {
1.56 albertel 596: $row_html .= '<td bgcolor="#E0FFDD">';
1.10 matthew 597: $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,
598: '#E0FFDD',1);
599: }
1.1 matthew 600: $row_html .= '</td>';
1.10 matthew 601: $num_cols_output++;
1.1 matthew 602: }
603: return $row_html;
604: }
605:
1.9 matthew 606: sub csv_rows {
607: # writes the meat of the spreadsheet to an excel worksheet. Called
608: # by Spreadsheet::outsheet_excel;
1.1 matthew 609: my $self = shift;
1.30 matthew 610: my ($connection,$filehandle) = @_;
1.9 matthew 611: #
612: # Write a header row
613: $self->csv_output_row($filehandle,undef,
1.26 www 614: (&mt('Parameter'),&mt('Description'),&mt('Value')));
1.9 matthew 615: #
616: # Write each row
617: foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
618: my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
1.48 albertel 619: my $description = $self->get_parm_name($parameter_name);
1.9 matthew 620: $self->csv_output_row($filehandle,$rownum,
621: $parameter_name,$description);
622: }
623: return;
1.1 matthew 624: }
625:
1.8 matthew 626: sub excel_rows {
627: # writes the meat of the spreadsheet to an excel worksheet. Called
628: # by Spreadsheet::outsheet_excel;
1.1 matthew 629: my $self = shift;
1.38 matthew 630: my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
1.30 matthew 631: return if (! ref($worksheet));
1.8 matthew 632: #
633: # Write a header row
634: $cols_output = 0;
635: foreach my $value ('Parameter','Description','Value') {
1.38 matthew 636: $worksheet->write($rows_output,$cols_output++,$value,$format->{'h4'});
1.8 matthew 637: }
1.38 matthew 638: $rows_output++;
1.8 matthew 639: #
640: # Write each row
641: foreach my $rownum (sort {$a <=> $b} ($self->rows())) {
642: my $parameter_name = $self->{'formulas'}->{'A'.$rownum};
1.48 albertel 643: my $description = $self->get_parm_name($parameter_name);
1.8 matthew 644: $self->excel_output_row($worksheet,$rownum,$rows_output++,
645: $parameter_name,$description);
646: }
647: return;
1.1 matthew 648: }
649:
1.21 matthew 650: ##
651: ## Routines to support assesscalc::compute
652: ##
1.48 albertel 653: sub get_parm {
1.21 matthew 654: my $self = shift;
655: my @Mandatory_parameters = @_;
1.48 albertel 656: my %parameters;
1.21 matthew 657: #
1.23 albertel 658: my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
1.21 matthew 659: my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
660: foreach my $parm (@Mandatory_parameters,@Metadata) {
661: next if ($parm !~ /^(resource\.|stores|parameter)_/);
662: my $cleaned_name = $parm;
663: $cleaned_name =~ s/^resource\./stores_/;
664: $cleaned_name =~ s/\./_/g;
1.48 albertel 665: $parameters{$cleaned_name}=1;
666: }
667: return (keys(%parameters));
668: }
669:
670: sub get_parm_name {
671: my $self = shift;
672: my $parm = shift;
673: my ($symap,$syid,$srcf) = &Apache::lonnet::decode_symb($self->{'symb'});
674: my $display = &Apache::lonnet::metadata($srcf,$parm.'.display');
675: if (! $display) {
676: $display .= &Apache::lonnet::metadata($srcf,$parm.'.name');
1.21 matthew 677: }
1.48 albertel 678: return $display;
1.21 matthew 679: }
680:
681: sub get_parameter_values {
682: my $self = shift();
683: my @Parameters;
684: my ($parameters) = @_;
685: if (!ref($parameters)) {
686: @Parameters = @_;
687: } elsif (ref($parameters) eq 'ARRAY') {
688: @Parameters = @$parameters;
689: } elsif (ref($parameters) eq 'HASH') {
690: @Parameters = keys(%$parameters);
691: }
692: #
693: my %parameters;
694: #
695: my $filename = $self->{'coursefilename'}.'_parms.db';
696: if (tie(%parmhash,'GDBM_File',
697: $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
1.44 albertel 698: my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($self->{'symb'});
1.55 albertel 699: $mapname = &Apache::lonnet::deversion($mapname);
1.21 matthew 700: foreach my $parmname (@Parameters) {
1.47 albertel 701: my $value = $self->parmval($parmname,$self->{'symb'},
702: $self->{'name'},$self->{'domain'},
703: $self->{'section'},undef,
1.53 raeburn 704: $mapname,$id,$fn,$self->{'groups'});
1.21 matthew 705: $parameters{$parmname} =$value;
706: }
707: untie(%parmhash);
708: } else {
709: $self->logthis('unable to tie '.$filename);
710: }
711: return %parameters;
712: }
713:
714: sub deal_with_export_row {
715: my $self = shift();
716: my @exportarray = @_;
717: $Exportrows{$self->{'symb'}}->{'time'} = time;
718: $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
719: #
720: # Save the export data
721: $self->save_export_data();
722: return;
723: }
724:
1.22 matthew 725: sub get_problem_state {
726: my $self = shift;
727: my %student_parameters;
728: if (exists($userdata{$self->{'symb'}}) &&
729: ref($userdata{$self->{'symb'}}) eq 'HASH') {
730: %student_parameters = %{$userdata{$self->{'symb'}}};
731: }
732: return %student_parameters;
733: }
1.21 matthew 734:
1.28 matthew 735: sub determine_parts {
736: my $self = shift;
1.41 albertel 737: my $check_hidden = shift;
1.28 matthew 738: if (exists($self->{'Parts'}) && ref($self->{'Parts'}) eq 'HASH') {
739: return;
740: }
741: my (undef,undef,$url) = &Apache::lonnet::decode_symb($self->{'symb'});
742: my $src = &Apache::lonnet::clutter($url);
743: return if (! defined($src));
744: my %Parts;
745: my $metadata = &Apache::lonnet::metadata($src,'packages');
746: foreach (split(',',$metadata)) {
747: my ($part) = (/^part_(.*)$/);
1.41 albertel 748: if (!defined($part)) { next; }
749: if (!$check_hidden) { $Parts{$part}++; next; }
750: if (!&Apache::loncommon::check_if_partid_hidden
751: ($part,$self->{'symb'},$self->{'name'},$self->{'domain'})
1.28 matthew 752: ) {
753: $Parts{$part}++;
754: }
755: }
756: # Make sure part 0 is defined.
757: $Parts{'0'}++;
758: $self->{'Parts'} = \%Parts;
759: return;
760: }
761:
762: sub parameter_part_is_valid {
763: my $self = shift;
764: my ($parameter) = @_;
765: return 1 if ($parameter eq 'timestamp');
766: if (! defined($self->{'Parts'}) ||
767: ! ref ($self->{'Parts'}) ||
768: ref($self->{'Parts'}) ne 'HASH') {
769: return 1;
770: }
771: #
1.51 albertel 772: my ($start,@pieces)=split('_',$parameter);
773: if ( $start !~ m/^(resource|stores|parameter)$/) { return 0; }
774: while (@pieces) {
775: pop(@pieces);
776: my $testpart=join('_',@pieces);
777: if (exists($self->{'Parts'}->{$testpart}) &&
778: $self->{'Parts'}->{$testpart} ) {
779: return 1;
780: }
1.28 matthew 781: }
1.51 albertel 782: return 0;
1.28 matthew 783: }
784:
1.1 matthew 785: sub compute {
786: my $self = shift;
1.19 matthew 787: my ($r) = @_;
1.1 matthew 788: $self->initialize_safe_space();
1.11 matthew 789: #########################################
790: #########################################
791: ### ###
792: ### Retrieve the problem parameters ###
793: ### ###
794: #########################################
795: #########################################
796: my @Mandatory_parameters = ("stores_0_solved",
797: "stores_0_awarddetail",
798: "stores_0_awarded",
799: "timestamp",
800: "stores_0_tries",
801: "stores_0_award");
1.1 matthew 802: #
803: # Definitions
804: undef(%nice_parameter_name);
805: my %parameters; # holds underscored parameters by name
806: #
807: # Get the metadata fields and determine their proper names
1.48 albertel 808: my @parameters=$self->get_parm(@Mandatory_parameters);
1.1 matthew 809: #
810: # Get the values of the metadata fields
1.22 matthew 811: $self->ensure_current_caches();
1.48 albertel 812: %parameters = $self->get_parameter_values(@parameters);
1.1 matthew 813: #
814: # Clean out unnecessary parameters
815: foreach (keys(%parameters)) {
816: delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
817: }
818: #
819: # Get the students performance data
1.43 albertel 820: $self->determine_parts(($parameters{'parameter_0_hiddenparts'} ne ''));
1.22 matthew 821: my %student_parameters = $self->get_problem_state();
1.1 matthew 822: while (my ($parm,$value) = each(%student_parameters)) {
823: $parm =~ s/^resource\./stores_/;
824: $parm =~ s/\./_/g;
1.41 albertel 825: # Clean out any bad parameters
826: next if (! $self->parameter_part_is_valid($parm));
827: $parameters{$parm} = $value;
1.1 matthew 828: }
829: #
830: # Set up the formulas and parameter values
831: my %f=$self->formulas();
832: my %c;
833: #
1.5 matthew 834: # Check for blackout requirements
1.39 albertel 835: if ((!exists($env{'request.role.adv'}) || !$env{'request.role.adv'})) {
1.5 matthew 836: while (my ($parm,$value) = each(%parameters)) {
837: last if ($self->blackout());
838: next if ($parm !~ /^(parameter_.*)_problemstatus$/);
1.24 matthew 839: if ($parameters{$1.'_answerdate'} ne '' &&
1.21 matthew 840: $parameters{$1.'_answerdate'} < time) {
841: next;
842: }
1.5 matthew 843: if (lc($value) eq 'no') {
844: # We must blackout this sheet
845: $self->blackout(1);
846: }
847: }
848: }
849: #
850: # Move the parameters into the spreadsheet
1.1 matthew 851: while (my ($parm,$value) = each(%parameters)) {
852: my $cell = 'A'.$self->get_row_number_from_key($parm);
853: $f{$cell} = $parm;
1.31 matthew 854: if ($parm =~ /_submission$/ && $value =~ /(\{|\})/) {
855: $value = 'witheld';
856: }
1.49 albertel 857: $value = 'q{'.$value.'}' if ($value =~/([^\d\.]|\.\.)/);
1.6 matthew 858: $c{$parm} = $value;
1.1 matthew 859: }
1.50 albertel 860: foreach my $cell (grep(/^A/,keys(%f))) {
861: # Clean out any bad formulas
862: next if (exists($c{$f{$cell}}));
863: next if ($cell eq 'A0');
864: delete($f{$cell});
865: }
1.6 matthew 866: $self->formulas(\%f);
867: $self->constants(\%c);
1.1 matthew 868: $self->calcsheet();
869: #
870: # Store export row in cache
871: my @exportarray = $self->exportrow();
1.21 matthew 872: $self->deal_with_export_row(@exportarray);
1.12 matthew 873: $self->save() if ($self->need_to_save());
1.1 matthew 874: return;
875: }
876:
877: ##
878: ## sett overrides Spreadsheet::sett
879: ##
880: sub sett {
881: my $self = shift;
882: my %t=();
1.40 albertel 883: undef(%Apache::Spreadsheet::sheet_values);
1.1 matthew 884: #
885: # Deal with the template row by copying the template formulas into each
886: # row.
887: foreach my $col ($self->template_cells()) {
888: next if ($col=~/^A/);
889: foreach my $row ($self->rows()) {
890: # Get the name of this cell
891: my $cell=$col.$row;
892: # Grab the template declaration
893: $t{$cell}=$self->formula('template_'.$col);
894: # Replace '#' with the row number
895: $t{$cell}=~s/\#/$row/g;
896: # Replace '....' with ','
897: $t{$cell}=~s/\.\.+/\,/g;
898: # Replace 'A0' with the value from 'A0'
899: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
900: # Replace parameters
1.58 albertel 901: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge;
1.1 matthew 902: }
903: }
904: #
905: # Deal with the cells which have formulas
906: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
907: next if ($cell =~ /template_/);
908: if ($cell =~ /^A/ && $cell ne 'A0') {
1.45 albertel 909: if ($formula !~ /^\!/
910: && exists($self->{'constants'}->{$formula})
911: && $self->{'constants'}->{$formula} ne ''
912: ) {
1.40 albertel 913: $Apache::Spreadsheet::sheet_values{$cell}=
1.49 albertel 914: eval($self->{'constants'}->{$formula});
1.1 matthew 915: }
916: } else {
917: $t{$cell}=$formula;
918: $t{$cell}=~s/\.\.+/\,/g;
919: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.58 albertel 920: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/sge;
1.1 matthew 921: }
922: }
923: # Put %t into the safe space
924: %{$self->{'safe'}->varglob('t')}=%t;
925: }
926:
927:
928: ########################################################
929: ########################################################
930:
931: =pod
932:
933: =item &load_cached_export_rows()
934:
935: Retrieves and parsers the export rows of the assessment spreadsheets.
936: These rows are saved in the students directory in the format:
937:
938: sname:sdom:assesscalc:symb.time => time
939:
940: sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
941:
942: =cut
943:
944: ########################################################
945: ########################################################
946: sub load_cached_export_rows {
1.18 matthew 947: undef(%Exportrows);
1.1 matthew 948: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
1.39 albertel 949: $env{'request.course.id'},
1.1 matthew 950: $current_domain,$current_name,undef);
951: if ($tmp[0]!~/^error/) {
952: my %tmp = @tmp;
1.39 albertel 953: my $default_filename = $env{'course.'.$env{'request.course.id'}.
1.1 matthew 954: '.spreadsheet_default_assesscalc'};
955: # We only got one key, so we will access it directly.
956: while (my ($key,$sheetdata) = each(%tmp)) {
957: my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
1.33 matthew 958: if (! defined($sname) || $sname eq '' ||
959: ! defined($sdom) || $sdom eq '' ) {
960: next;
961: }
1.1 matthew 962: if ($symb =~ /\.time$/) {
963: $symb =~ s/\.time$//;
964: $Exportrows{$symb}->{'time'} = $sheetdata;
965: } else {
966: $sheetdata =~ s/^(.*)___=___//;
967: my $filename = $1;
968: $filename = $default_filename if (! defined($filename));
969: my @Data = split('___;___',$sheetdata);
970: $Exportrows{$symb}->{$filename} = \@Data;
971: }
972: }
973: }
974: }
975:
976: #############################################
977: #############################################
978:
979: =pod
980:
981: =item &export_data
982:
983: Returns the export data associated with the spreadsheet. Computes the
984: spreadsheet only if necessary.
985:
986: =cut
987:
988: #############################################
989: #############################################
990: sub export_data {
991: my $self = shift;
1.19 matthew 992: my ($r) = @_;
1.1 matthew 993: my $symb = $self->{'symb'};
1.39 albertel 994: if (! exists($env{'request.role.adv'}) || ! $env{'request.role.adv'} ||
1.5 matthew 995: ! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb}) ||
1.1 matthew 996: ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
997: ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
1.18 matthew 998: ! defined($Exportrows{$symb}->{$self->{'filename'}}) ||
999: ! ref($Exportrows{$symb}->{$self->{'filename'}})
1000: ) {
1.19 matthew 1001: $self->compute($r);
1.1 matthew 1002: }
1.29 matthew 1003: my @Data;
1004: if ($self->badcalc()) {
1005: @Data = ();
1006: } else {
1007: @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
1008: if ($Data[0] =~ /^(.*)___=___/) {
1009: $self->{'sheetname'} = $1;
1010: $Data[0] =~ s/^(.*)___=___//;
1011: }
1012: for (my $i=0;$i<$#Data;$i++) {
1013: if ($Data[$i]=~/\D/ && defined($Data[$i])) {
1014: $Data[$i]="'".$Data[$i]."'";
1015: }
1016: }
1.1 matthew 1017: }
1018: return @Data;
1019: }
1020:
1021: #############################################
1022: #############################################
1023:
1024: =pod
1025:
1026: =item &save_export_data()
1027:
1028: Writes the export data for this spreadsheet to the students cache.
1029:
1030: =cut
1031:
1032: #############################################
1033: #############################################
1034: sub save_export_data {
1035: my $self = shift;
1.7 matthew 1036: return if ($self->temporary());
1.1 matthew 1037: my $student = $self->{'name'}.':'.$self->{'domain'};
1038: my $symb = $self->{'symb'};
1.29 matthew 1039: if ($self->badcalc()){
1040: # do not save data away when calculations have not been done properly.
1041: delete($Exportrows{$symb});
1042: return;
1043: }
1.1 matthew 1044: if (! exists($Exportrows{$symb}) ||
1045: ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
1046: return;
1047: }
1048: my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
1049: my $timekey = $key.'.time';
1.22 matthew 1050: my $newstore= join('___;___',
1051: map {s/[^[:print:]]//g;$_;} # strip out unprintable
1052: @{$Exportrows{$symb}->{$self->{'filename'}}});
1.1 matthew 1053: $newstore = $self->{'filename'}.'___=___'.$newstore;
1.22 matthew 1054: $newExportrows{$student}->{$key} = $newstore;
1055: $newExportrows{$student}->{$timekey} = $Exportrows{$symb}->{'time'};
1.1 matthew 1056: return;
1057: }
1058:
1059: 1;
1060:
1061: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>