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