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