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