1: # The LearningOnline Network with CAPA
2: #
3: # $Id: lonstudentassessment.pm,v 1.109 2005/01/14 21:15:09 matthew Exp $
4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
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: # (Navigate problems for statistical reports
27: #
28: #######################################################
29: #######################################################
30:
31: =pod
32:
33: =head1 NAME
34:
35: lonstudentassessment
36:
37: =head1 SYNOPSIS
38:
39: Presents assessment data about a student or a group of students.
40:
41: =head1 Subroutines
42:
43: =over 4
44:
45: =cut
46:
47: #######################################################
48: #######################################################
49:
50: package Apache::lonstudentassessment;
51:
52: use strict;
53: use Apache::lonstatistics;
54: use Apache::lonhtmlcommon;
55: use Apache::loncommon();
56: use Apache::loncoursedata;
57: use Apache::lonnet; # for logging porpoises
58: use Apache::lonlocal;
59: use Spreadsheet::WriteExcel;
60: use Spreadsheet::WriteExcel::Utility();
61:
62: #######################################################
63: #######################################################
64: =pod
65:
66: =item Package Variables
67:
68: =over 4
69:
70: =item $Statistics Hash ref to store student data. Indexed by symb,
71: contains hashes with keys 'score' and 'max'.
72:
73: =cut
74:
75: #######################################################
76: #######################################################
77:
78: my $Statistics;
79:
80: #######################################################
81: #######################################################
82:
83: =pod
84:
85: =item $show_links 'yes' or 'no' for linking to student performance data
86:
87: =item $output_mode 'html', 'excel', or 'csv' for output mode
88:
89: =item $show 'all', 'totals', or 'scores' determines how much data is output
90:
91: =item $single_student_mode evaluates to true if we are showing only one
92: student.
93:
94: =cut
95:
96: #######################################################
97: #######################################################
98: my $show_links;
99: my $output_mode;
100: my $chosen_output;
101: my $single_student_mode;
102:
103: #######################################################
104: #######################################################
105: # End of package variable declarations
106:
107: =pod
108:
109: =back
110:
111: =cut
112:
113: #######################################################
114: #######################################################
115:
116: =pod
117:
118: =item &BuildStudentAssessmentPage()
119:
120: Inputs:
121:
122: =over 4
123:
124: =item $r Apache Request
125:
126: =item $c Apache Connection
127:
128: =back
129:
130: =cut
131:
132: #######################################################
133: #######################################################
134: sub BuildStudentAssessmentPage {
135: my ($r,$c)=@_;
136: #
137: undef($Statistics);
138: undef($show_links);
139: undef($output_mode);
140: undef($chosen_output);
141: undef($single_student_mode);
142: #
143: my %Saveable_Parameters = ('Status' => 'scalar',
144: 'chartoutputmode' => 'scalar',
145: 'chartoutputdata' => 'scalar',
146: 'Section' => 'array',
147: 'StudentData' => 'array',
148: 'Maps' => 'array');
149: &Apache::loncommon::store_course_settings('chart',\%Saveable_Parameters);
150: &Apache::loncommon::restore_course_settings('chart',\%Saveable_Parameters);
151: #
152: &Apache::lonstatistics::PrepareClasslist();
153: #
154: $single_student_mode = 0;
155: $single_student_mode = 1 if ($ENV{'form.SelectedStudent'});
156: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
157: ['selectstudent']);
158: if ($ENV{'form.selectstudent'}) {
159: &Apache::lonstatistics::DisplayClasslist($r);
160: return;
161: }
162: #
163: # Print out the HTML headers for the interface
164: # This also parses the output mode selector
165: # This step must *always* be done.
166: $r->print(&CreateInterface());
167: $r->print('<input type="hidden" name="notfirstrun" value="true" />');
168: $r->print('<input type="hidden" name="sort" value="'.
169: $ENV{'form.sort'}.'" />');
170: $r->rflush();
171: #
172: if (! exists($ENV{'form.notfirstrun'}) && ! $single_student_mode) {
173: return;
174: }
175: #
176: my $initialize = \&html_initialize;
177: my $output_student = \&html_outputstudent;
178: my $finish = \&html_finish;
179: #
180: if ($output_mode eq 'excel') {
181: $initialize = \&excel_initialize;
182: $output_student = \&excel_outputstudent;
183: $finish = \&excel_finish;
184: } elsif ($output_mode eq 'csv') {
185: $initialize = \&csv_initialize;
186: $output_student = \&csv_outputstudent;
187: $finish = \&csv_finish;
188: }
189: #
190: if($c->aborted()) { return ; }
191: #
192: # Determine which students we want to look at
193: my @Students;
194: if ($single_student_mode) {
195: @Students = (&Apache::lonstatistics::current_student());
196: $r->print(&next_and_previous_buttons());
197: $r->rflush();
198: } else {
199: @Students = @Apache::lonstatistics::Students;
200: }
201: #
202: # Perform generic initialization tasks
203: # Since we use lonnet::EXT to retrieve problem weights,
204: # to ensure current data we must clear the caches out.
205: # This makes sure that parameter changes at the student level
206: # are immediately reflected in the chart.
207: &Apache::lonnet::clear_EXT_cache_status();
208: #
209: # Clean out loncoursedata's package data, just to be safe.
210: &Apache::loncoursedata::clear_internal_caches();
211: #
212: # Call the initialize routine selected above
213: $initialize->($r);
214: foreach my $student (@Students) {
215: if($c->aborted()) {
216: $finish->($r);
217: return ;
218: }
219: # Call the output_student routine selected above
220: $output_student->($r,$student);
221: }
222: # Call the "finish" routine selected above
223: $finish->($r);
224: #
225: return;
226: }
227:
228: #######################################################
229: #######################################################
230: sub next_and_previous_buttons {
231: my $Str = '';
232: $Str .= '<input type="hidden" name="SelectedStudent" value="'.
233: $ENV{'form.SelectedStudent'}.'" />';
234: #
235: # Build the previous student link
236: my $previous = &Apache::lonstatistics::previous_student();
237: my $previousbutton = '';
238: if (defined($previous)) {
239: my $sname = $previous->{'username'}.':'.$previous->{'domain'};
240: $previousbutton .= '<input type="button" value="'.
241: 'Previous Student ('.
242: $previous->{'username'}.'@'.$previous->{'domain'}.')'.
243: '" onclick="document.Statistics.SelectedStudent.value='.
244: "'".$sname."'".';'.
245: 'document.Statistics.submit();" />';
246: } else {
247: $previousbutton .= '<input type="button" value="'.
248: 'Previous student (none)'.'" />';
249: }
250: #
251: # Build the next student link
252: my $next = &Apache::lonstatistics::next_student();
253: my $nextbutton = '';
254: if (defined($next)) {
255: my $sname = $next->{'username'}.':'.$next->{'domain'};
256: $nextbutton .= '<input type="button" value="'.
257: 'Next Student ('.
258: $next->{'username'}.'@'.$next->{'domain'}.')'.
259: '" onclick="document.Statistics.SelectedStudent.value='.
260: "'".$sname."'".';'.
261: 'document.Statistics.submit();" />';
262: } else {
263: $nextbutton .= '<input type="button" value="'.
264: 'Next student (none)'.'" />';
265: }
266: #
267: # Build the 'all students' button
268: my $all = '';
269: $all .= '<input type="button" value="All Students" '.
270: '" onclick="document.Statistics.SelectedStudent.value='.
271: "''".';'.'document.Statistics.submit();" />';
272: $Str .= $previousbutton.(' 'x5).$all.(' 'x5).$nextbutton;
273: return $Str;
274: }
275:
276: #######################################################
277: #######################################################
278:
279: sub get_student_fields_to_show {
280: my @to_show = @Apache::lonstatistics::SelectedStudentData;
281: foreach (@to_show) {
282: if ($_ eq 'all') {
283: @to_show = @Apache::lonstatistics::StudentDataOrder;
284: last;
285: }
286: }
287: return @to_show;
288: }
289:
290: #######################################################
291: #######################################################
292:
293: =pod
294:
295: =item &CreateInterface()
296:
297: Called by &BuildStudentAssessmentPage to create the top part of the
298: page which displays the chart.
299:
300: Inputs: None
301:
302: Returns: A string containing the HTML for the headers and top table for
303: the chart page.
304:
305: =cut
306:
307: #######################################################
308: #######################################################
309: sub CreateInterface {
310: my $Str = '';
311: $Str .= &Apache::lonhtmlcommon::breadcrumbs(undef,'Chart');
312: # $Str .= &CreateLegend();
313: $Str .= '<table cellspacing="5">'."\n";
314: $Str .= '<tr>';
315: $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
316: $Str .= '<td align="center"><b>'.&mt('Student Data</b>').'</td>';
317: $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
318: $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
319: $Str .= '<td align="center"><b>'.&mt('Output Format').'</b>'.
320: &Apache::loncommon::help_open_topic("Chart_Output_Formats").
321: '</td>';
322: $Str .= '<td align="center"><b>'.&mt('Output Data').'</b>'.
323: &Apache::loncommon::help_open_topic("Chart_Output_Data").
324: '</td>';
325: $Str .= '</tr>'."\n";
326: #
327: $Str .= '<tr><td align="center">'."\n";
328: $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
329: $Str .= '</td><td align="center">';
330: my $only_seq_with_assessments = sub {
331: my $s=shift;
332: if ($s->{'num_assess'} < 1) {
333: return 0;
334: } else {
335: return 1;
336: }
337: };
338: $Str .= &Apache::lonstatistics::StudentDataSelect('StudentData','multiple',
339: 5,undef);
340: $Str .= '</td><td>'."\n";
341: $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
342: $Str .= '</td><td>'."\n";
343: $Str .= &Apache::lonstatistics::MapSelect('Maps','multiple,all',5,
344: $only_seq_with_assessments);
345: $Str .= '</td><td>'."\n";
346: $Str .= &CreateAndParseOutputSelector();
347: $Str .= '</td><td>'."\n";
348: $Str .= &CreateAndParseOutputDataSelector();
349: $Str .= '</td></tr>'."\n";
350: $Str .= '</table>'."\n";
351: $Str .= '<input type="submit" name="Generate Chart" value="'.
352: &mt('Generate Chart').'" />';
353: $Str .= ' 'x5;
354: $Str .= '<input type="submit" name="selectstudent" value="'.
355: &mt('Select One Student').'" />';
356: $Str .= ' 'x5;
357: $Str .= '<input type="submit" name="ClearCache" value="'.
358: &mt('Clear Caches').'" />';
359: $Str .= ' 'x5;
360: $Str .=
361: &mt('Status [_1]',
362: '<input type="text" name="stats_status" size="60" value="" />');
363: $Str .= '<br />';
364: return $Str;
365: }
366:
367: #######################################################
368: #######################################################
369:
370: =pod
371:
372: =item &CreateAndParseOutputSelector()
373:
374: =cut
375:
376: #######################################################
377: #######################################################
378: my @OutputOptions =
379: ({ name => 'HTML, with links',
380: value => 'html, with links',
381: description => 'Output HTML with each symbol linked to the problem '.
382: 'which generated it.',
383: mode => 'html',
384: show_links => 'yes',
385: },
386: { name => 'HTML, with all links',
387: value => 'html, with all links',
388: description => 'Output HTML with each symbol linked to the problem '.
389: 'which generated it. '.
390: 'This includes links for unattempted problems.',
391: mode => 'html',
392: show_links => 'all',
393: },
394: { name => 'HTML, without links',
395: value => 'html, without links',
396: description => 'Output HTML. By not including links, the size of the'.
397: ' web page is greatly reduced. If your browser crashes on the '.
398: 'full display, try this.',
399: mode => 'html',
400: show_links => 'no',
401: },
402: { name => 'Excel',
403: value => 'excel',
404: description => 'Output an Excel file (compatable with Excel 95).',
405: mode => 'excel',
406: show_links => 'no',
407: },
408: { name => 'CSV',
409: value => 'csv',
410: description => 'Output a comma separated values file suitable for '.
411: 'import into a spreadsheet program. Using this method as opposed '.
412: 'to Excel output allows you to organize your data before importing'.
413: ' it into a spreadsheet program.',
414: mode => 'csv',
415: show_links => 'no',
416: },
417: );
418:
419: sub OutputDescriptions {
420: my $Str = '';
421: $Str .= "<h2>Output Formats</h2>\n";
422: $Str .= "<dl>\n";
423: foreach my $outputmode (@OutputOptions) {
424: $Str .=" <dt>".$outputmode->{'name'}."</dt>\n";
425: $Str .=" <dd>".$outputmode->{'description'}."</dd>\n";
426: }
427: $Str .= "</dl>\n";
428: return $Str;
429: }
430:
431: sub CreateAndParseOutputSelector {
432: my $Str = '';
433: my $elementname = 'chartoutputmode';
434: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
435: [$elementname]);
436: #
437: # Format for output options is 'mode, restrictions';
438: my $selected = 'html, without links';
439: if (exists($ENV{'form.'.$elementname})) {
440: if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
441: $selected = $ENV{'form.'.$elementname}->[0];
442: } else {
443: $selected = $ENV{'form.'.$elementname};
444: }
445: }
446: #
447: # Set package variables describing output mode
448: $show_links = 'no';
449: $output_mode = 'html';
450: foreach my $option (@OutputOptions) {
451: next if ($option->{'value'} ne $selected);
452: $output_mode = $option->{'mode'};
453: $show_links = $option->{'show_links'};
454: }
455:
456: #
457: # Build the form element
458: $Str = qq/<select size="5" name="$elementname">/;
459: foreach my $option (@OutputOptions) {
460: $Str .= "\n".' <option value="'.$option->{'value'}.'"';
461: $Str .= " selected " if ($option->{'value'} eq $selected);
462: $Str .= ">".&mt($option->{'name'})."<\/option>";
463: }
464: $Str .= "\n</select>";
465: return $Str;
466: }
467:
468: ##
469: ## Data selector stuff
470: ##
471: my @OutputDataOptions =
472: (
473: { name => 'Scores Summary',
474: base => 'scores',
475: value => 'sum and total',
476: scores => 1,
477: tries => 0,
478: every_problem => 0,
479: sequence_sum => 1,
480: sequence_max => 1,
481: grand_total => 1,
482: grand_maximum => 1,
483: summary_table => 1,
484: maximum_row => 1,
485: shortdesc => 'Total Score and Maximum Possible for each '.
486: 'Sequence or Folder',
487: longdesc => 'The score of each student as well as the '.
488: ' maximum possible on each Sequence or Folder.',
489: },
490: { name => 'Scores Per Problem',
491: base => 'scores',
492: value => 'scores',
493: scores => 1,
494: tries => 0,
495: correct => 0,
496: every_problem => 1,
497: sequence_sum => 1,
498: sequence_max => 1,
499: grand_total => 1,
500: grand_maximum => 1,
501: summary_table => 1,
502: maximum_row => 1,
503: shortdesc => 'Score on each Problem Part',
504: longdesc =>'The students score on each problem part, computed as'.
505: 'the part weight * part awarded',
506: },
507: { name =>'Tries',
508: base =>'tries',
509: value => 'tries',
510: scores => 0,
511: tries => 1,
512: correct => 0,
513: every_problem => 1,
514: sequence_sum => 0,
515: sequence_max => 0,
516: grand_total => 0,
517: grand_maximum => 0,
518: summary_table => 0,
519: maximum_row => 0,
520: shortdesc => 'Number of Tries before success on each Problem Part',
521: longdesc =>'The number of tries before success on each problem part.',
522: non_html_notes => 'negative values indicate an incorrect problem',
523: },
524: { name =>'Parts Correct',
525: base =>'tries',
526: value => 'parts correct total',
527: scores => 0,
528: tries => 0,
529: correct => 1,
530: every_problem => 1,
531: sequence_sum => 1,
532: sequence_max => 1,
533: grand_total => 1,
534: grand_maximum => 1,
535: summary_table => 1,
536: maximum_row => 0,
537: shortdesc => 'Number of Problem Parts completed successfully.',
538: longdesc => 'The Number of Problem Parts completed successfully and '.
539: 'the maximum possible for each student',
540: },
541: );
542:
543: sub HTMLifyOutputDataDescriptions {
544: my $Str = '';
545: $Str .= "<h2>Output Data</h2>\n";
546: $Str .= "<dl>\n";
547: foreach my $option (@OutputDataOptions) {
548: $Str .= ' <dt>'.$option->{'name'}.'</dt>';
549: $Str .= '<dd>'.$option->{'longdesc'}.'</dd>'."\n";
550: }
551: $Str .= "</dl>\n";
552: return $Str;
553: }
554:
555: sub CreateAndParseOutputDataSelector {
556: my $Str = '';
557: my $elementname = 'chartoutputdata';
558: #
559: my $selected = 'scores';
560: if (exists($ENV{'form.'.$elementname})) {
561: if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
562: $selected = $ENV{'form.'.$elementname}->[0];
563: } else {
564: $selected = $ENV{'form.'.$elementname};
565: }
566: }
567: #
568: $chosen_output = $OutputDataOptions[0];
569: foreach my $option (@OutputDataOptions) {
570: if ($option->{'value'} eq $selected) {
571: $chosen_output = $option;
572: }
573: }
574: #
575: # Build the form element
576: $Str = qq/<select size="5" name="$elementname">/;
577: foreach my $option (@OutputDataOptions) {
578: $Str .= "\n".' <option value="'.$option->{'value'}.'"';
579: $Str .= " selected " if ($option->{'value'} eq $chosen_output->{'value'});
580: $Str .= ">".&mt($option->{'name'})."<\/option>";
581: }
582: $Str .= "\n</select>";
583: return $Str;
584:
585: }
586:
587: #######################################################
588: #######################################################
589:
590: =pod
591:
592: =head2 HTML output routines
593:
594: =item &html_initialize($r)
595:
596: Create labels for the columns of student data to show.
597:
598: =item &html_outputstudent($r,$student)
599:
600: Return a line of the chart for a student.
601:
602: =item &html_finish($r)
603:
604: =cut
605:
606: #######################################################
607: #######################################################
608: {
609: my $padding;
610: my $count;
611:
612: my $nodata_count; # The number of students for which there is no data
613: my %prog_state; # progress state used by loncommon PrgWin routines
614: my $total_sum_width;
615:
616: sub html_initialize {
617: my ($r) = @_;
618: #
619: $padding = ' 'x3;
620: $count = 0;
621: $nodata_count = 0;
622: undef(%prog_state);
623: #
624: $r->print("<h3>".$ENV{'course.'.$ENV{'request.course.id'}.'.description'}.
625: " ".localtime(time)."</h3>");
626:
627: if ($chosen_output->{'base'} !~ /^final table/) {
628: $r->print("<h3>".$chosen_output->{'shortdesc'}."</h3>");
629: }
630: my $Str = "<pre>\n";
631: # First, the @StudentData fields need to be listed
632: my @to_show = &get_student_fields_to_show();
633: foreach my $field (@to_show) {
634: my $title=$Apache::lonstatistics::StudentData{$field}->{'title'};
635: my $base =$Apache::lonstatistics::StudentData{$field}->{'base_width'};
636: my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
637: $Str .= $title.' 'x($width-$base).$padding;
638: }
639: #
640: # Compute the column widths and output the sequence titles
641: my $total_count;
642: foreach my $sequence (&Apache::lonstatistics::Sequences_with_Assess()){
643: #
644: # Comptue column widths
645: $sequence->{'width_sum'} = 0;
646: if ($chosen_output->{'sequence_sum'}) {
647: if ($chosen_output->{'every_problem'}) {
648: # Use 1 digit for a space
649: $sequence->{'width_sum'} += 1;
650: }
651: $total_count += $sequence->{'num_assess_parts'};
652: # Use 3 digits for the sum
653: $sequence->{'width_sum'} += 3;
654: }
655: if ($chosen_output->{'sequence_max'}) {
656: if ($sequence->{'width_sum'}>0) {
657: # One digit for the '/'
658: $sequence->{'width_sum'} +=1;
659: }
660: # Use 3 digits for the total
661: $sequence->{'width_sum'}+=3;
662: }
663: #
664: if ($chosen_output->{'every_problem'}) {
665: # one problem per digit
666: $sequence->{'width_problem'} = $sequence->{'num_assess_parts'};
667: } else {
668: $sequence->{'width_problem'} = 0;
669: }
670: $sequence->{'width_total'} = $sequence->{'width_problem'} +
671: $sequence->{'width_sum'};
672: if ($sequence->{'width_total'} < length(&HTML::Entities::decode($sequence->{'title'}))) {
673: $sequence->{'width_total'} = length(&HTML::Entities::decode($sequence->{'title'}));
674: }
675: #
676: # Output the sequence titles
677: $Str .=
678: $sequence->{'title'}.' 'x($sequence->{'width_total'}-
679: length($sequence->{'title'})
680: ).$padding;
681: }
682: $total_sum_width = length($total_count)+1;
683: $Str .= " total</pre>\n";
684: $Str .= "<pre>";
685: $r->print($Str);
686: $r->rflush();
687: return;
688: }
689:
690: sub html_outputstudent {
691: my ($r,$student) = @_;
692: my $Str = '';
693: #
694: if($count++ % 5 == 0 && $count > 0) {
695: $r->print("</pre><pre>");
696: }
697: # First, the @StudentData fields need to be listed
698: my @to_show = &get_student_fields_to_show();
699: foreach my $field (@to_show) {
700: my $title=$student->{$field};
701: # Deal with 'comments' - how I love special cases
702: if ($field eq 'comments') {
703: $title = '<a href="/adm/'.$student->{'domain'}.'/'.$student->{'username'}.'/'.'aboutme#coursecomment">'.&mt('Comments').'</a>';
704: }
705: my $base = length($title);
706: my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
707: $Str .= $title.' 'x($width-$base).$padding;
708: }
709: # Get ALL the students data
710: my %StudentsData;
711: my @tmp = &Apache::loncoursedata::get_current_state
712: ($student->{'username'},$student->{'domain'},undef,
713: $ENV{'request.course.id'});
714: if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
715: %StudentsData = @tmp;
716: }
717: if (scalar(@tmp) < 1) {
718: $nodata_count++;
719: $Str .= '<font color="blue">No Course Data</font>'."\n";
720: $r->print($Str);
721: $r->rflush();
722: return;
723: }
724: #
725: # By sequence build up the data
726: my $studentstats;
727: my $PerformanceStr = '';
728: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
729: my ($performance,$performance_length,$score,$seq_max,$rawdata);
730: if ($chosen_output->{'tries'}) {
731: ($performance,$performance_length,$score,$seq_max,$rawdata) =
732: &StudentTriesOnSequence($student,\%StudentsData,
733: $seq,$show_links);
734: } else {
735: ($performance,$performance_length,$score,$seq_max,$rawdata) =
736: &StudentPerformanceOnSequence($student,\%StudentsData,
737: $seq,$show_links);
738: }
739: my $ratio='';
740: if ($chosen_output->{'every_problem'}) {
741: $ratio .= ' ';
742: }
743: if ($chosen_output->{'sequence_sum'} && $score ne ' ') {
744: $ratio .= sprintf("%3.0f",$score);
745: } elsif($chosen_output->{'sequence_sum'}) {
746: $ratio .= ' 'x3;
747: }
748: if ($chosen_output->{'sequence_max'}) {
749: if ($chosen_output->{'sequence_sum'}) {
750: $ratio .= '/';
751: }
752: $ratio .= sprintf("%3.0f",$seq_max);
753: }
754: #
755: if (! $chosen_output->{'every_problem'}) {
756: $performance = '';
757: $performance_length=0;
758: }
759: $performance .= ' 'x($seq->{'width_total'}-$performance_length-$seq->{'width_sum'}).
760: $ratio;
761: #
762: $Str .= $performance.$padding;
763: #
764: $studentstats->{$seq->{'symb'}}->{'score'}= $score;
765: $studentstats->{$seq->{'symb'}}->{'max'} = $seq_max;
766: }
767: #
768: # Total it up and store the statistics info.
769: my ($score,$max);
770: while (my ($symb,$seq_stats) = each (%{$studentstats})) {
771: $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
772: if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) {
773: $Statistics->{$symb}->{'max'} = $seq_stats->{'max'};
774: }
775: if ($seq_stats->{'score'} ne ' ') {
776: $score += $seq_stats->{'score'};
777: $Statistics->{$symb}->{'num_students'}++;
778: }
779: $max += $seq_stats->{'max'};
780: }
781: if (! defined($score)) {
782: $score = ' ' x $total_sum_width;
783: } else {
784: $score = sprintf("%.0f",$score);
785: }
786: $Str .= ' '.' 'x($total_sum_width-length($score)).$score.' / '.$max;
787: $Str .= " \n";
788: #
789: $r->print($Str);
790: #
791: $r->rflush();
792: return;
793: }
794:
795: sub html_finish {
796: my ($r) = @_;
797: #
798: # Check for suppressed output and close the progress window if so
799: $r->print("</pre>\n");
800: if ($chosen_output->{'summary_table'}) {
801: if ($single_student_mode) {
802: $r->print(&SingleStudentTotal());
803: } else {
804: $r->print(&StudentAverageTotal());
805: }
806: }
807: $r->rflush();
808: return;
809: }
810:
811: sub StudentAverageTotal {
812: my $Str = '<h3>'.&mt('Summary Tables').'</h3>'.$/;
813: $Str .= '<table border=2 cellspacing="1">'."\n";
814: $Str .= '<tr>'.
815: '<th>'.&mt('Title').'</th>'.
816: '<th>'.&mt('Average').'</th>'.
817: '<th>'.&mt('Maximum').'</th>'.
818: '</tr>'.$/;
819: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
820: my $ave;
821: my $num_students = $Statistics->{$seq->{'symb'}}->{'num_students'};
822: if ($num_students > 0) {
823: $ave = int(100*
824: ($Statistics->{$seq->{'symb'}}->{'score'}/$num_students)
825: )/100;
826: } else {
827: $ave = 0;
828: }
829: my $max = $Statistics->{$seq->{'symb'}}->{'max'};
830: $ave = sprintf("%.2f",$ave);
831: $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
832: '<td align="right">'.$ave.' </td>'.
833: '<td align="right">'.$max.' '.'</td></tr>'."\n";
834: }
835: $Str .= "</table>\n";
836: return $Str;
837: }
838:
839: sub SingleStudentTotal {
840: my $student = &Apache::lonstatistics::current_student();
841: my $Str = '<h3>'.&mt('Summary table for [_1] ([_2]@[_3])',
842: $student->{'fullname'},
843: $student->{'username'},$student->{'domain'}).'</h3>';
844: $Str .= $/;
845: $Str .= '<table border=2 cellspacing="1">'."\n";
846: $Str .=
847: '<tr>'.
848: '<th>'.&mt('Sequence or Folder').'</th>';
849: if ($chosen_output->{'base'} eq 'tries') {
850: $Str .= '<th>'.&mt('Parts Correct').'</th>';
851: } else {
852: $Str .= '<th>'.&mt('Score').'</th>';
853: }
854: $Str .= '<th>'.&mt('Maximum').'</th>'."</tr>\n";
855: my $total = 0;
856: my $total_max = 0;
857: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
858: my $value = $Statistics->{$seq->{'symb'}}->{'score'};
859: my $max = $Statistics->{$seq->{'symb'}}->{'max'};
860: $Str .= '<tr><td>'.$seq->{'title'}.'</td>'.
861: '<td align="right">'.$value.'</td>'.
862: '<td align="right">'.$max.'</td></tr>'."\n";
863: $total += $value;
864: $total_max +=$max;
865: }
866: $Str .= '<tr><td><b>'.&mt('Total').'</b></td>'.
867: '<td align="right">'.$total.'</td>'.
868: '<td align="right">'.$total_max."</td></tr>\n";
869: $Str .= "</table>\n";
870: return $Str;
871: }
872:
873: }
874:
875: #######################################################
876: #######################################################
877:
878: =pod
879:
880: =head2 EXCEL subroutines
881:
882: =item &excel_initialize($r)
883:
884: =item &excel_outputstudent($r,$student)
885:
886: =item &excel_finish($r)
887:
888: =cut
889:
890: #######################################################
891: #######################################################
892: {
893:
894: my $excel_sheet;
895: my $excel_workbook;
896:
897: my $filename;
898: my $rows_output;
899: my $cols_output;
900:
901: my %prog_state; # progress window state
902: my $request_aborted;
903:
904: my $total_formula;
905: my $maximum_formula;
906:
907: sub excel_initialize {
908: my ($r) = @_;
909: #
910: undef ($excel_sheet);
911: undef ($excel_workbook);
912: undef ($filename);
913: undef ($rows_output);
914: undef ($cols_output);
915: undef (%prog_state);
916: undef ($request_aborted);
917: undef ($total_formula);
918: undef ($maximum_formula);
919: #
920: my $total_columns = scalar(&get_student_fields_to_show());
921: my $num_students = scalar(@Apache::lonstatistics::Students);
922: #
923: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
924: if ($chosen_output->{'every_problem'}) {
925: $total_columns += $seq->{'num_assess_parts'};
926: }
927: # Add 2 because we need a 'sequence_sum' and 'total' column for each
928: $total_columns += 2;
929: }
930: my $too_many_cols_error_message =
931: '<h2>'.&mt('Unable to Complete Request').'</h2>'.$/.
932: '<p>'.&mt('LON-CAPA is unable to produce your Excel spreadsheet because your selections will result in more than 255 columns. Excel allows only 255 columns in a spreadsheet.').'</p>'.$/.
933: '<p>'.&mt('You may consider reducing the number of <b>Sequences or Folders</b> you have selected.').'</p>'.$/.
934: '<p>'.&mt('LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the <b>Scores Summary</b> data.').'</p>'.$/;
935: if ($chosen_output->{'base'} eq 'tries' && $total_columns > 255) {
936: $r->print($too_many_cols_error_message);
937: $request_aborted = 1;
938: }
939: if ($chosen_output->{'base'} eq 'scores' && $total_columns > 255) {
940: $r->print($too_many_cols_error_message);
941: $request_aborted = 1;
942: }
943: return if ($request_aborted);
944: #
945: $filename = '/prtspool/'.
946: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
947: time.'_'.rand(1000000000).'.xls';
948: #
949: $excel_workbook = undef;
950: $excel_sheet = undef;
951: #
952: $rows_output = 0;
953: $cols_output = 0;
954: #
955: # Determine rows
956: my $header_row = $rows_output++;
957: my $description_row = $rows_output++;
958: my $notes_row = $rows_output++;
959: $rows_output++; # blank row
960: my $summary_header_row;
961: if ($chosen_output->{'summary_table'}) {
962: $summary_header_row = $rows_output++;
963: $rows_output+= scalar(&Apache::lonstatistics::Sequences_with_Assess());
964: $rows_output++;
965: }
966: my $sequence_name_row = $rows_output++;
967: my $resource_name_row = $rows_output++;
968: my $maximum_data_row = $rows_output++;
969: if (! $chosen_output->{'maximum_row'}) {
970: $rows_output--;
971: }
972: my $first_data_row = $rows_output++;
973: #
974: # Create sheet
975: $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
976: #
977: # Check for errors
978: if (! defined($excel_workbook)) {
979: $r->log_error("Error creating excel spreadsheet $filename: $!");
980: $r->print("Problems creating new Excel file. ".
981: "This error has been logged. ".
982: "Please alert your LON-CAPA administrator");
983: return ;
984: }
985: #
986: # The excel spreadsheet stores temporary data in files, then put them
987: # together. If needed we should be able to disable this (memory only).
988: # The temporary directory must be specified before calling 'addworksheet'.
989: # File::Temp is used to determine the temporary directory.
990: $excel_workbook->set_tempdir($Apache::lonnet::tmpdir);
991: #
992: my $format = &Apache::loncommon::define_excel_formats($excel_workbook);
993: #
994: # Add a worksheet
995: my $sheetname = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
996: $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
997: $excel_sheet = $excel_workbook->addworksheet($sheetname);
998: #
999: # Put the course description in the header
1000: $excel_sheet->write($header_row,$cols_output++,
1001: $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
1002: $format->{'h1'});
1003: $cols_output += 3;
1004: #
1005: # Put a description of the sections listed
1006: my $sectionstring = '';
1007: my @Sections = @Apache::lonstatistics::SelectedSections;
1008: if (scalar(@Sections) > 1) {
1009: if (scalar(@Sections) > 2) {
1010: my $last = pop(@Sections);
1011: $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
1012: } else {
1013: $sectionstring = "Sections ".join(' and ',@Sections);
1014: }
1015: } else {
1016: if ($Sections[0] eq 'all') {
1017: $sectionstring = "All sections";
1018: } else {
1019: $sectionstring = "Section ".$Sections[0];
1020: }
1021: }
1022: $excel_sheet->write($header_row,$cols_output++,$sectionstring,
1023: $format->{'h3'});
1024: $cols_output += scalar(@Sections);
1025: #
1026: # Put the date in there too
1027: $excel_sheet->write($header_row,$cols_output++,
1028: 'Compiled on '.localtime(time),$format->{'h3'});
1029: #
1030: $cols_output = 0;
1031: $excel_sheet->write($description_row,$cols_output++,
1032: $chosen_output->{'shortdesc'},
1033: $format->{'b'});
1034: #
1035: $cols_output = 0;
1036: $excel_sheet->write($notes_row,$cols_output++,
1037: $chosen_output->{'non_html_notes'},
1038: $format->{'i'});
1039:
1040: ##############################################
1041: # Output headings for the raw data
1042: ##############################################
1043: #
1044: # Add the student headers
1045: $cols_output = 0;
1046: foreach my $field (&get_student_fields_to_show()) {
1047: $excel_sheet->write($resource_name_row,$cols_output++,$field,
1048: $format->{'bold'});
1049: }
1050: #
1051: # Add the remaining column headers
1052: my $total_formula_string = '=0';
1053: my $maximum_formula_string = '=0';
1054: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1055: $excel_sheet->write($sequence_name_row,,
1056: $cols_output,$seq->{'title'},$format->{'bold'});
1057: # Determine starting cell
1058: $seq->{'Excel:startcell'}=
1059: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1060: ($first_data_row,$cols_output);
1061: $seq->{'Excel:startcol'}=$cols_output;
1062: my $count = 0;
1063: if ($chosen_output->{'every_problem'}) {
1064: # Put the names of the problems and parts into the sheet
1065: foreach my $res (@{$seq->{'contents'}}) {
1066: if ($res->{'type'} ne 'assessment' ||
1067: ! exists($res->{'parts'}) ||
1068: ref($res->{'parts'}) ne 'ARRAY' ||
1069: scalar(@{$res->{'parts'}}) < 1) {
1070: next;
1071: }
1072: if (scalar(@{$res->{'parts'}}) > 1) {
1073: foreach my $part (@{$res->{'parts'}}) {
1074: $excel_sheet->write($resource_name_row,
1075: $cols_output++,
1076: $res->{'title'}.' part '.$part,
1077: $format->{'bold'});
1078: $count++;
1079: }
1080: } else {
1081: $excel_sheet->write($resource_name_row,
1082: $cols_output++,
1083: $res->{'title'},$format->{'bold'});
1084: $count++;
1085: }
1086: }
1087: }
1088: # Determine ending cell
1089: if ($count <= 1) {
1090: $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
1091: $seq->{'Excel:endcol'} = $seq->{'Excel:startcol'};
1092: } else {
1093: $seq->{'Excel:endcell'} =
1094: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1095: ($first_data_row,$cols_output-1);
1096: $seq->{'Excel:endcol'} = $cols_output-1;
1097: }
1098: # Create the formula for summing up this sequence
1099: if (! exists($seq->{'Excel:endcell'}) ||
1100: ! defined($seq->{'Excel:endcell'})) {
1101: $seq->{'Excel:endcell'} = $seq->{'Excel:startcell'};
1102: }
1103: $seq->{'Excel:sum'}= $excel_sheet->store_formula
1104: ('=SUM('.$seq->{'Excel:startcell'}.
1105: ':'.$seq->{'Excel:endcell'}.')');
1106: # Determine cell the score is held in
1107: $seq->{'Excel:scorecell'} =
1108: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1109: ($first_data_row,$cols_output);
1110: $seq->{'Excel:scorecol'}=$cols_output;
1111: if ($chosen_output->{'base'} eq 'parts correct total') {
1112: $excel_sheet->write($resource_name_row,$cols_output++,
1113: 'parts correct',
1114: $format->{'bold'});
1115: } elsif ($chosen_output->{'sequence_sum'}) {
1116: if ($chosen_output->{'correct'}) {
1117: # Only reporting the number correct, so do not call it score
1118: $excel_sheet->write($resource_name_row,$cols_output++,
1119: 'sum',
1120: $format->{'bold'});
1121: } else {
1122: $excel_sheet->write($resource_name_row,$cols_output++,
1123: 'score',
1124: $format->{'bold'});
1125: }
1126: }
1127: #
1128: $total_formula_string.='+'.
1129: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1130: ($first_data_row,$cols_output-1);
1131: if ($chosen_output->{'sequence_max'}) {
1132: $excel_sheet->write($resource_name_row,$cols_output,
1133: 'maximum',
1134: $format->{'bold'});
1135: $seq->{'Excel:maxcell'} =
1136: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1137: ($first_data_row,$cols_output);
1138: $seq->{'Excel:maxcol'}=$cols_output;
1139: $maximum_formula_string.='+'.
1140: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1141: ($first_data_row,$cols_output);
1142: $cols_output++;
1143:
1144: }
1145: }
1146: if ($chosen_output->{'grand_total'}) {
1147: $excel_sheet->write($resource_name_row,$cols_output++,'Total',
1148: $format->{'bold'});
1149: }
1150: if ($chosen_output->{'grand_maximum'}) {
1151: $excel_sheet->write($resource_name_row,$cols_output++,'Max. Total',
1152: $format->{'bold'});
1153: }
1154: $total_formula = $excel_sheet->store_formula($total_formula_string);
1155: $maximum_formula = $excel_sheet->store_formula($maximum_formula_string);
1156: ##############################################
1157: # Output a row for MAX, if appropriate
1158: ##############################################
1159: if ($chosen_output->{'maximum_row'}) {
1160: $cols_output = 0;
1161: foreach my $field (&get_student_fields_to_show()) {
1162: if ($field eq 'username' || $field eq 'fullname' ||
1163: $field eq 'id') {
1164: $excel_sheet->write($maximum_data_row,$cols_output++,'Maximum',
1165: $format->{'bold'});
1166: } else {
1167: $excel_sheet->write($maximum_data_row,$cols_output++,'');
1168: }
1169: }
1170: #
1171: # Add the maximums for each sequence or assessment
1172: my %total_cell_translation;
1173: my %maximum_cell_translation;
1174: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1175: $cols_output=$seq->{'Excel:startcol'};
1176: $total_cell_translation{$seq->{'Excel:scorecell'}} =
1177: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1178: ($maximum_data_row,$seq->{'Excel:scorecol'});
1179: $maximum_cell_translation{$seq->{'Excel:maxcell'}} =
1180: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1181: ($maximum_data_row,$seq->{'Excel:maxcol'});
1182: my $weight;
1183: my $max = 0;
1184: foreach my $resource (@{$seq->{'contents'}}) {
1185: next if ($resource->{'type'} ne 'assessment');
1186: foreach my $part (@{$resource->{'parts'}}) {
1187: $weight = 1;
1188: if ($chosen_output->{'scores'}) {
1189: $weight = &Apache::lonnet::EXT
1190: ('resource.'.$part.'.weight',$resource->{'symb'},
1191: undef,undef,undef);
1192: if (!defined($weight) || ($weight eq '')) {
1193: $weight=1;
1194: }
1195: }
1196: if ($chosen_output->{'scores'} &&
1197: $chosen_output->{'every_problem'}) {
1198: $excel_sheet->write($maximum_data_row,$cols_output++,
1199: $weight);
1200: }
1201: $max += $weight;
1202: }
1203: }
1204: #
1205: if ($chosen_output->{'sequence_sum'} &&
1206: $chosen_output->{'every_problem'}) {
1207: my %replaceCells;
1208: $replaceCells{$seq->{'Excel:startcell'}} =
1209: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1210: ($maximum_data_row,$seq->{'Excel:startcol'});
1211: $replaceCells{$seq->{'Excel:endcell'}} =
1212: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1213: ($maximum_data_row,$seq->{'Excel:endcol'});
1214: $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1215: $seq->{'Excel:sum'},undef,
1216: %replaceCells);
1217: } elsif ($chosen_output->{'sequence_sum'}) {
1218: $excel_sheet->write($maximum_data_row,$cols_output++,$max);
1219: }
1220: if ($chosen_output->{'sequence_max'}) {
1221: $excel_sheet->write($maximum_data_row,$cols_output++,$max);
1222: }
1223: #
1224: }
1225: if ($chosen_output->{'grand_total'}) {
1226: $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1227: $total_formula,undef,
1228: %total_cell_translation);
1229: }
1230: if ($chosen_output->{'grand_maximum'}) {
1231: $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1232: $maximum_formula,undef,
1233: %maximum_cell_translation);
1234: }
1235: } # End of MAXIMUM row output if ($chosen_output->{'maximum_row'}) {
1236: $rows_output = $first_data_row;
1237: ##############################################
1238: # Output summary table, which actually is above the sequence name row.
1239: ##############################################
1240: if ($chosen_output->{'summary_table'}) {
1241: $cols_output = 0;
1242: $excel_sheet->write($summary_header_row,$cols_output++,
1243: 'Summary Table',$format->{'bold'});
1244: if ($chosen_output->{'maximum_row'}) {
1245: $excel_sheet->write($summary_header_row,$cols_output++,
1246: 'Maximum',$format->{'bold'});
1247: }
1248: $excel_sheet->write($summary_header_row,$cols_output++,
1249: 'Average',$format->{'bold'});
1250: $excel_sheet->write($summary_header_row,$cols_output++,
1251: 'Median',$format->{'bold'});
1252: $excel_sheet->write($summary_header_row,$cols_output++,
1253: 'Std Dev',$format->{'bold'});
1254: my $row = $summary_header_row+1;
1255: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1256: $cols_output = 0;
1257: $excel_sheet->write($row,$cols_output++,
1258: $seq->{'title'},
1259: $format->{'bold'});
1260: if ($chosen_output->{'maximum_row'}) {
1261: $excel_sheet->write
1262: ($row,$cols_output++,
1263: '='.
1264: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1265: ($maximum_data_row,$seq->{'Excel:scorecol'})
1266: );
1267: }
1268: my $range =
1269: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1270: ($first_data_row,$seq->{'Excel:scorecol'}).
1271: ':'.
1272: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1273: ($first_data_row+$num_students-1,$seq->{'Excel:scorecol'});
1274: $excel_sheet->write($row,$cols_output++,
1275: '=AVERAGE('.$range.')');
1276: $excel_sheet->write($row,$cols_output++,
1277: '=MEDIAN('.$range.')');
1278: $excel_sheet->write($row,$cols_output++,
1279: '=STDEV('.$range.')');
1280: $row++;
1281: }
1282: }
1283: ##############################################
1284: # Take care of non-excel initialization
1285: ##############################################
1286: #
1287: # Let the user know what we are doing
1288: my $studentcount = scalar(@Apache::lonstatistics::Students);
1289: if ($ENV{'form.SelectedStudent'}) {
1290: $studentcount = '1';
1291: }
1292: if ($studentcount > 1) {
1293: $r->print('<h1>'.&mt('Compiling Excel spreadsheet for [_1] students',
1294: $studentcount)."</h1>\n");
1295: } else {
1296: $r->print('<h1>'.
1297: &mt('Compiling Excel spreadsheet for 1 student').
1298: "</h1>\n");
1299: }
1300: $r->rflush();
1301: #
1302: # Initialize progress window
1303: %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1304: ($r,'Excel File Compilation Status',
1305: 'Excel File Compilation Progress', $studentcount,
1306: 'inline',undef,'Statistics','stats_status');
1307: #
1308: &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
1309: 'Processing first student');
1310: return;
1311: }
1312:
1313: sub excel_outputstudent {
1314: my ($r,$student) = @_;
1315: return if ($request_aborted);
1316: return if (! defined($excel_sheet));
1317: $cols_output=0;
1318: #
1319: # Write out student data
1320: my @to_show = &get_student_fields_to_show();
1321: foreach my $field (@to_show) {
1322: my $value = $student->{$field};
1323: if ($field eq 'comments') {
1324: $value = &Apache::lonmsg::retrieve_instructor_comments
1325: ($student->{'username'},$student->{'domain'});
1326: }
1327: $excel_sheet->write($rows_output,$cols_output++,$value);
1328: }
1329: #
1330: # Get student assessment data
1331: my %StudentsData;
1332: my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
1333: $student->{'domain'},
1334: undef,
1335: $ENV{'request.course.id'});
1336: if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
1337: %StudentsData = @tmp;
1338: }
1339: #
1340: # Write out sequence scores and totals data
1341: my %total_cell_translation;
1342: my %maximum_cell_translation;
1343: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1344: $cols_output = $seq->{'Excel:startcol'};
1345: # Keep track of cells to translate in total cell
1346: $total_cell_translation{$seq->{'Excel:scorecell'}} =
1347: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1348: ($rows_output,$seq->{'Excel:scorecol'});
1349: # and maximum cell
1350: $maximum_cell_translation{$seq->{'Excel:maxcell'}} =
1351: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1352: ($rows_output,$seq->{'Excel:maxcol'});
1353: #
1354: my ($performance,$performance_length,$score,$seq_max,$rawdata);
1355: if ($chosen_output->{'tries'} || $chosen_output->{'correct'}){
1356: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1357: &StudentTriesOnSequence($student,\%StudentsData,
1358: $seq,'no');
1359: } else {
1360: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1361: &StudentPerformanceOnSequence($student,\%StudentsData,
1362: $seq,'no');
1363: }
1364: if ($chosen_output->{'every_problem'}) {
1365: if ($chosen_output->{'correct'}) {
1366: # only indiciate if each item is correct or not
1367: foreach my $value (@$rawdata) {
1368: # nonzero means correct
1369: $value = 1 if ($value > 0);
1370: $excel_sheet->write($rows_output,$cols_output++,$value);
1371: }
1372: } else {
1373: foreach my $value (@$rawdata) {
1374: if ($score eq ' ' || !defined($value)) {
1375: $cols_output++;
1376: } else {
1377: $excel_sheet->write($rows_output,$cols_output++,
1378: $value);
1379: }
1380: }
1381: }
1382: }
1383: if ($chosen_output->{'sequence_sum'} &&
1384: $chosen_output->{'every_problem'}) {
1385: # Write a formula for the sum of this sequence
1386: my %replaceCells;
1387: $replaceCells{$seq->{'Excel:startcell'}} =
1388: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1389: ($rows_output,$seq->{'Excel:startcol'});
1390: $replaceCells{$seq->{'Excel:endcell'}} =
1391: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1392: ($rows_output,$seq->{'Excel:endcol'});
1393: # The undef is for the format
1394: if (scalar(keys(%replaceCells)) == 1) {
1395: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1396: $seq->{'Excel:sum'},undef,
1397: %replaceCells,%replaceCells);
1398: } else {
1399: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1400: $seq->{'Excel:sum'},undef,
1401: %replaceCells);
1402: }
1403: } elsif ($chosen_output->{'sequence_sum'}) {
1404: if ($score eq ' ') {
1405: $cols_output++;
1406: } else {
1407: $excel_sheet->write($rows_output,$cols_output++,$score);
1408: }
1409: }
1410: if ($chosen_output->{'sequence_max'}) {
1411: $excel_sheet->write($rows_output,$cols_output++,$seq_max);
1412: }
1413: }
1414: #
1415: if ($chosen_output->{'grand_total'}) {
1416: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1417: $total_formula,undef,
1418: %total_cell_translation);
1419: }
1420: if ($chosen_output->{'grand_maximum'}) {
1421: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1422: $maximum_formula,undef,
1423: %maximum_cell_translation);
1424: }
1425: #
1426: # Bookkeeping
1427: $rows_output++;
1428: $cols_output=0;
1429: #
1430: # Update the progress window
1431: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1432: return;
1433: }
1434:
1435: sub excel_finish {
1436: my ($r) = @_;
1437: return if ($request_aborted);
1438: return if (! defined($excel_sheet));
1439: #
1440: # Write the excel file
1441: $excel_workbook->close();
1442: my $c = $r->connection();
1443: #
1444: return if($c->aborted());
1445: #
1446: # Close the progress window
1447: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1448: #
1449: # Tell the user where to get their excel file
1450: $r->print('<br />'.
1451: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
1452: $r->rflush();
1453: return;
1454: }
1455:
1456: }
1457: #######################################################
1458: #######################################################
1459:
1460: =pod
1461:
1462: =head2 CSV output routines
1463:
1464: =item &csv_initialize($r)
1465:
1466: =item &csv_outputstudent($r,$student)
1467:
1468: =item &csv_finish($r)
1469:
1470: =cut
1471:
1472: #######################################################
1473: #######################################################
1474: {
1475:
1476: my $outputfile;
1477: my $filename;
1478: my $request_aborted;
1479: my %prog_state; # progress window state
1480:
1481: sub csv_initialize{
1482: my ($r) = @_;
1483: #
1484: # Clean up
1485: undef($outputfile);
1486: undef($filename);
1487: undef($request_aborted);
1488: undef(%prog_state);
1489: #
1490: # Deal with unimplemented requests
1491: $request_aborted = undef;
1492: if ($chosen_output->{'base'} =~ /final table/) {
1493: $r->print(<<END);
1494: <h2>Unable to Complete Request</h2>
1495: <p>
1496: The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
1497: </p>
1498: END
1499: $request_aborted = 1;
1500: }
1501: return if ($request_aborted);
1502: #
1503: # Initialize progress window
1504: my $studentcount = scalar(@Apache::lonstatistics::Students);
1505: %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1506: ($r,'CSV File Compilation Status',
1507: 'CSV File Compilation Progress', $studentcount,
1508: 'inline',undef,'Statistics','stats_status');
1509: #
1510: # Open a file
1511: $filename = '/prtspool/'.
1512: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1513: time.'_'.rand(1000000000).'.csv';
1514: unless ($outputfile = Apache::File->new('>/home/httpd'.$filename)) {
1515: $r->log_error("Couldn't open $filename for output $!");
1516: $r->print("Problems occured in writing the csv file. ".
1517: "This error has been logged. ".
1518: "Please alert your LON-CAPA administrator.");
1519: $outputfile = undef;
1520: }
1521: #
1522: # Datestamp
1523: my $description = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1524: print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
1525: '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
1526: "\n";
1527: foreach my $item ('shortdesc','non_html_notes') {
1528: next if (! exists($chosen_output->{$item}));
1529: print $outputfile
1530: '"'.&Apache::loncommon::csv_translate($chosen_output->{$item}).'"'.
1531: "\n";
1532: }
1533: #
1534: # Print out the headings
1535: my $sequence_row = '';
1536: my $resource_row = undef;
1537: foreach my $field (&get_student_fields_to_show()) {
1538: $sequence_row .='"",';
1539: $resource_row .= '"'.&Apache::loncommon::csv_translate($field).'",';
1540: }
1541: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1542: $sequence_row .= '"'.
1543: &Apache::loncommon::csv_translate($seq->{'title'}).'",';
1544: my $count = 0;
1545: if ($chosen_output->{'every_problem'}) {
1546: foreach my $res (@{$seq->{'contents'}}) {
1547: if ($res->{'type'} ne 'assessment' ||
1548: ! exists($res->{'parts'}) ||
1549: ref($res->{'parts'}) ne 'ARRAY' ||
1550: scalar(@{$res->{'parts'}}) < 1) {
1551: next;
1552: }
1553: foreach my $part (@{$res->{'parts'}}) {
1554: $resource_row .= '"'.
1555: &Apache::loncommon::csv_translate($res->{'title'}.
1556: ', Part '.$part
1557: ).'",';
1558: $count++;
1559: }
1560: }
1561: }
1562: $sequence_row.='"",'x$count;
1563: if ($chosen_output->{'sequence_sum'}) {
1564: if($chosen_output->{'correct'}) {
1565: $resource_row .= '"sum",';
1566: } else {
1567: $resource_row .= '"score",';
1568: }
1569: }
1570: if ($chosen_output->{'sequence_max'}) {
1571: $sequence_row.= '"",';
1572: $resource_row .= '"maximum possible",';
1573: }
1574: }
1575: if ($chosen_output->{'grand_total'}) {
1576: $sequence_row.= '"",';
1577: $resource_row.= '"Total",';
1578: }
1579: if ($chosen_output->{'grand_maximum'}) {
1580: $sequence_row.= '"",';
1581: $resource_row.= '"Maximum",';
1582: }
1583: chomp($sequence_row);
1584: chomp($resource_row);
1585: print $outputfile $sequence_row."\n";
1586: print $outputfile $resource_row."\n";
1587: return;
1588: }
1589:
1590: sub csv_outputstudent {
1591: my ($r,$student) = @_;
1592: return if ($request_aborted);
1593: return if (! defined($outputfile));
1594: my $Str = '';
1595: #
1596: # Output student fields
1597: my @to_show = &get_student_fields_to_show();
1598: foreach my $field (@to_show) {
1599: my $value = $student->{$field};
1600: if ($field eq 'comments') {
1601: $value = &Apache::lonmsg::retrieve_instructor_comments
1602: ($student->{'username'},$student->{'domain'});
1603: }
1604: $Str .= '"'.&Apache::loncommon::csv_translate($value).'",';
1605: }
1606: #
1607: # Get student assessment data
1608: my %StudentsData;
1609: my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
1610: $student->{'domain'},
1611: undef,
1612: $ENV{'request.course.id'});
1613: if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
1614: %StudentsData = @tmp;
1615: }
1616: #
1617: # Output performance data
1618: my $total = 0;
1619: my $maximum = 0;
1620: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
1621: my ($performance,$performance_length,$score,$seq_max,$rawdata);
1622: if ($chosen_output->{'tries'}){
1623: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1624: &StudentTriesOnSequence($student,\%StudentsData,
1625: $seq,'no');
1626: } else {
1627: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1628: &StudentPerformanceOnSequence($student,\%StudentsData,
1629: $seq,'no');
1630: }
1631: if ($chosen_output->{'every_problem'}) {
1632: if ($chosen_output->{'correct'}) {
1633: $score = 0;
1634: # Deal with number of parts correct data
1635: $Str .= '"'.join('","',( map { if ($_>0) {
1636: $score += 1;
1637: 1;
1638: } else {
1639: 0;
1640: }
1641: } @$rawdata)).'",';
1642: } else {
1643: $Str .= '"'.join('","',(@$rawdata)).'",';
1644: }
1645: }
1646: if ($chosen_output->{'sequence_sum'}) {
1647: $Str .= '"'.$score.'",';
1648: }
1649: if ($chosen_output->{'sequence_max'}) {
1650: $Str .= '"'.$seq_max.'",';
1651: }
1652: $total+=$score;
1653: $maximum += $seq_max;
1654: }
1655: if ($chosen_output->{'grand_total'}) {
1656: $Str .= '"'.$total.'",';
1657: }
1658: if ($chosen_output->{'grand_maximum'}) {
1659: $Str .= '"'.$maximum.'",';
1660: }
1661: chop($Str);
1662: $Str .= "\n";
1663: print $outputfile $Str;
1664: #
1665: # Update the progress window
1666: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1667: return;
1668: }
1669:
1670: sub csv_finish {
1671: my ($r) = @_;
1672: return if ($request_aborted);
1673: return if (! defined($outputfile));
1674: close($outputfile);
1675: #
1676: my $c = $r->connection();
1677: return if ($c->aborted());
1678: #
1679: # Close the progress window
1680: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1681: #
1682: # Tell the user where to get their csv file
1683: $r->print('<br />'.
1684: '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
1685: $r->rflush();
1686: return;
1687:
1688: }
1689:
1690: }
1691:
1692: #######################################################
1693: #######################################################
1694:
1695: =pod
1696:
1697: =item &StudentTriesOnSequence()
1698:
1699: Inputs:
1700:
1701: =over 4
1702:
1703: =item $student
1704:
1705: =item $studentdata Hash ref to all student data
1706:
1707: =item $seq Hash ref, the sequence we are working on
1708:
1709: =item $links if defined we will output links to each resource.
1710:
1711: =back
1712:
1713: =cut
1714:
1715: #######################################################
1716: #######################################################
1717: sub StudentTriesOnSequence {
1718: my ($student,$studentdata,$seq,$links) = @_;
1719: $links = 'no' if (! defined($links));
1720: my $Str = '';
1721: my ($sum,$max) = (0,0);
1722: my $performance_length = 0;
1723: my @TriesData = ();
1724: my $tries;
1725: my $hasdata = 0; # flag - true if the student has any data on the sequence
1726: foreach my $resource (@{$seq->{'contents'}}) {
1727: next if ($resource->{'type'} ne 'assessment');
1728: my $resource_data = $studentdata->{$resource->{'symb'}};
1729: my $value = '';
1730: foreach my $partnum (@{$resource->{'parts'}}) {
1731: $tries = undef;
1732: $max++;
1733: $performance_length++;
1734: my $symbol = ' '; # default to space
1735: #
1736: my $awarded = 0;
1737: if (exists($resource_data->{'resource.'.$partnum.'.awarded'})) {
1738: $awarded = $resource_data->{'resource.'.$partnum.'.awarded'};
1739: $awarded = 0 if (! $awarded);
1740: }
1741: #
1742: my $status = '';
1743: if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
1744: $status = $resource_data->{'resource.'.$partnum.'.solved'};
1745: }
1746: #
1747: my $tries = 0;
1748: if(exists($resource_data->{'resource.'.$partnum.'.tries'})) {
1749: $tries = $resource_data->{'resource.'.$partnum.'.tries'};
1750: $hasdata =1;
1751: }
1752: #
1753: if ($awarded > 0) {
1754: # The student has gotten the problem correct to some degree
1755: if ($status eq 'excused') {
1756: $symbol = 'x';
1757: $max--;
1758: } elsif ($status eq 'correct_by_override') {
1759: $symbol = '+';
1760: $sum++;
1761: } elsif ($tries > 0) {
1762: if ($tries > 9) {
1763: $symbol = '*';
1764: } else {
1765: $symbol = $tries;
1766: }
1767: $sum++;
1768: } else {
1769: $symbol = '+';
1770: $sum++;
1771: }
1772: } else {
1773: # The student has the problem incorrect or it is ungraded
1774: if ($status eq 'excused') {
1775: $symbol = 'x';
1776: $max--;
1777: } elsif ($status eq 'incorrect_by_override') {
1778: $symbol = '-';
1779: } elsif ($status eq 'ungraded_attempted') {
1780: $symbol = '#';
1781: } elsif ($status eq 'incorrect_attempted' ||
1782: $tries > 0) {
1783: $symbol = '.';
1784: } else {
1785: # Problem is wrong and has not been attempted.
1786: $symbol=' ';
1787: }
1788: }
1789: #
1790: if (! defined($tries)) {
1791: $tries = 0;
1792: }
1793: if ($status =~ /^(incorrect|ungraded)/) {
1794: # Bug 3390: show '-' for tries on incorrect problems
1795: # (csv & excel only)
1796: push(@TriesData,-$tries);
1797: } else {
1798: push (@TriesData,$tries);
1799: }
1800: #
1801: if ( ($links eq 'yes' && $symbol ne ' ') ||
1802: ($links eq 'all')) {
1803: if (length($symbol) > 1) {
1804: &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
1805: }
1806: $symbol = '<a href="/adm/grades'.
1807: '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
1808: '&student='.$student->{'username'}.
1809: '&userdom='.$student->{'domain'}.
1810: '&command=submission">'.$symbol.'</a>';
1811: }
1812: $value .= $symbol;
1813: }
1814: $Str .= $value;
1815: }
1816: if ($seq->{'randompick'}) {
1817: $max = $seq->{'randompick'};
1818: }
1819: if (! $hasdata && $sum == 0) {
1820: $sum = ' ';
1821: }
1822: return ($Str,$performance_length,$sum,$max,\@TriesData);
1823: }
1824:
1825: #######################################################
1826: #######################################################
1827:
1828: =pod
1829:
1830: =item &StudentPerformanceOnSequence()
1831:
1832: Inputs:
1833:
1834: =over 4
1835:
1836: =item $student
1837:
1838: =item $studentdata Hash ref to all student data
1839:
1840: =item $seq Hash ref, the sequence we are working on
1841:
1842: =item $links if defined we will output links to each resource.
1843:
1844: =back
1845:
1846: =cut
1847:
1848: #######################################################
1849: #######################################################
1850: sub StudentPerformanceOnSequence {
1851: my ($student,$studentdata,$seq,$links) = @_;
1852: $links = 'no' if (! defined($links));
1853: my $Str = ''; # final result string
1854: my ($score,$max) = (0,0);
1855: my $performance_length = 0;
1856: my $symbol;
1857: my @ScoreData = ();
1858: my $partscore;
1859: my $hasdata = 0; # flag, 0 if there were no submissions on the sequence
1860: foreach my $resource (@{$seq->{'contents'}}) {
1861: next if ($resource->{'type'} ne 'assessment');
1862: my $resource_data = $studentdata->{$resource->{'symb'}};
1863: foreach my $part (@{$resource->{'parts'}}) {
1864: $partscore = undef;
1865: my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
1866: $resource->{'symb'},
1867: $student->{'domain'},
1868: $student->{'username'},
1869: $student->{'section'});
1870: if (!defined($weight) || ($weight eq '')) {
1871: $weight=1;
1872: }
1873: #
1874: $max += $weight; # see the 'excused' branch below...
1875: $performance_length++; # one character per part
1876: $symbol = ' '; # default to space
1877: #
1878: my $awarded;
1879: if (exists($resource_data->{'resource.'.$part.'.awarded'})) {
1880: $awarded = $resource_data->{'resource.'.$part.'.awarded'};
1881: $awarded = 0 if (! $awarded);
1882: $hasdata = 1;
1883: }
1884: #
1885: $partscore = $weight*$awarded;
1886: if (! defined($awarded)) {
1887: $partscore = undef;
1888: }
1889: $score += $partscore;
1890: $symbol = $partscore;
1891: if (abs($symbol - sprintf("%.0f",$symbol)) < 0.001) {
1892: $symbol = sprintf("%.0f",$symbol);
1893: }
1894: if (length($symbol) > 1) {
1895: $symbol = '*';
1896: }
1897: if (exists($resource_data->{'resource.'.$part.'.solved'})) {
1898: my $status = $resource_data->{'resource.'.$part.'.solved'};
1899: if ($status eq 'excused') {
1900: $symbol = 'x';
1901: $max -= $weight; # Do not count 'excused' problems.
1902: }
1903: $hasdata = 1;
1904: } else {
1905: # Unsolved. Did they try?
1906: if (exists($resource_data->{'resource.'.$part.'.tries'})){
1907: $symbol = '.';
1908: $hasdata = 1;
1909: } else {
1910: $symbol = ' ';
1911: }
1912: }
1913: #
1914: if (! defined($partscore)) {
1915: $partscore = $symbol;
1916: }
1917: push (@ScoreData,$partscore);
1918: #
1919: if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) {
1920: $symbol = '<a href="/adm/grades'.
1921: '?symb='.&Apache::lonnet::escape($resource->{'symb'}).
1922: '&student='.$student->{'username'}.
1923: '&userdom='.$student->{'domain'}.
1924: '&command=submission">'.$symbol.'</a>';
1925: }
1926: $Str .= $symbol;
1927: }
1928: }
1929: if (! $hasdata && $score == 0) {
1930: $score = ' ';
1931: }
1932: return ($Str,$performance_length,$score,$max,\@ScoreData);
1933: }
1934:
1935: #######################################################
1936: #######################################################
1937:
1938: =pod
1939:
1940: =item &CreateLegend()
1941:
1942: This function returns a formatted string containing the legend for the
1943: chart. The legend describes the symbols used to represent grades for
1944: problems.
1945:
1946: =cut
1947:
1948: #######################################################
1949: #######################################################
1950: sub CreateLegend {
1951: my $Str = "<p><pre>".
1952: " 1 correct by student in 1 try\n".
1953: " 7 correct by student in 7 tries\n".
1954: " * correct by student in more than 9 tries\n".
1955: " + correct by hand grading or override\n".
1956: " - incorrect by override\n".
1957: " . incorrect attempted\n".
1958: " # ungraded attempted\n".
1959: " not attempted (blank field)\n".
1960: " x excused".
1961: "</pre><p>";
1962: return $Str;
1963: }
1964:
1965: #######################################################
1966: #######################################################
1967:
1968: =pod
1969:
1970: =back
1971:
1972: =cut
1973:
1974: #######################################################
1975: #######################################################
1976:
1977: 1;
1978:
1979: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>