1: # The LearningOnline Network with CAPA
2: #
3: # $Id: lonstudentassessment.pm,v 1.124 2005/04/19 01:28:25 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 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: #
714: # Let the user know what we are doing
715: my $studentcount = scalar(@Apache::lonstatistics::Students);
716: if ($env{'form.SelectedStudent'}) {
717: $studentcount = '1';
718: }
719: #
720: # Initialize progress window
721: %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
722: ($r,'HTML Chart Status',
723: 'HTML Chart Progress', $studentcount,
724: 'inline',undef,'Statistics','stats_status');
725: #
726: &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
727: 'Processing first student');
728: return;
729: }
730:
731: sub html_outputstudent {
732: my ($r,$student) = @_;
733: my $Str = '';
734: return if (! defined($navmap));
735: #
736: if($count++ % 5 == 0 && $count > 0) {
737: $r->print("</pre><pre>");
738: }
739: # First, the @StudentData fields need to be listed
740: my @to_show = &get_student_fields_to_show();
741: foreach my $field (@to_show) {
742: my $title=$student->{$field};
743: # Deal with 'comments' - how I love special cases
744: if ($field eq 'comments') {
745: $title = '<a href="/adm/'.$student->{'domain'}.'/'.$student->{'username'}.'/'.'aboutme#coursecomment">'.&mt('Comments').'</a>';
746: }
747: my $base = length($title);
748: my $width=$Apache::lonstatistics::StudentData{$field}->{'width'};
749: $Str .= $title.' 'x($width-$base).$padding;
750: }
751: # Get ALL the students data
752: my %StudentsData;
753: my @tmp = &Apache::loncoursedata::get_current_state
754: ($student->{'username'},$student->{'domain'},undef,
755: $env{'request.course.id'});
756: if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
757: %StudentsData = @tmp;
758: }
759: if (scalar(@tmp) < 1) {
760: $nodata_count++;
761: $Str .= '<font color="blue">No Course Data</font>'."\n";
762: $r->print($Str);
763: $r->rflush();
764: return;
765: }
766: #
767: # By sequence build up the data
768: my $studentstats;
769: my $PerformanceStr = '';
770: foreach my $seq (@sequences) {
771: my $symb = $seq->symb;
772: my ($performance,$performance_length,$score,$seq_max,$rawdata);
773: if ($chosen_output->{'tries'}) {
774: ($performance,$performance_length,$score,$seq_max,$rawdata) =
775: &student_tries_on_sequence($student,\%StudentsData,
776: $navmap,$seq,$show_links);
777: } else {
778: ($performance,$performance_length,$score,$seq_max,$rawdata) =
779: &student_performance_on_sequence($student,\%StudentsData,
780: $navmap,$seq,$show_links);
781: }
782: my $ratio='';
783: if ($chosen_output->{'every_problem'} &&
784: $chosen_output->{'sequence_sum'}) {
785: $ratio .= ' ';
786: }
787: if ($chosen_output->{'sequence_sum'} && $score ne ' ') {
788: my $score .= sprintf("%3.0f",$score);
789: $ratio .= (' 'x(3-length($score))).$score;
790: } elsif($chosen_output->{'sequence_sum'}) {
791: $ratio .= ' 'x3;
792: }
793: if ($chosen_output->{'sequence_max'}) {
794: if ($chosen_output->{'sequence_sum'}) {
795: $ratio .= '/';
796: }
797: $ratio .= sprintf("%3.0f",$seq_max);
798: }
799: #
800: if (! $chosen_output->{'every_problem'}) {
801: $performance = '';
802: $performance_length=0;
803: }
804: $performance .= ' 'x($width{$symb}->{'width_total'} -
805: $performance_length -
806: $width{$symb}->{'width_sum'}).
807: $ratio;
808: #
809: $Str .= $performance.$padding;
810: #
811: $studentstats->{$symb}->{'score'}= $score;
812: $studentstats->{$symb}->{'max'} = $seq_max;
813: }
814: #
815: # Total it up and store the statistics info.
816: my ($score,$max);
817: while (my ($symb,$seq_stats) = each (%{$studentstats})) {
818: $Statistics->{$symb}->{'score'} += $seq_stats->{'score'};
819: if ($Statistics->{$symb}->{'max'} < $seq_stats->{'max'}) {
820: $Statistics->{$symb}->{'max'} = $seq_stats->{'max'};
821: }
822: if ($seq_stats->{'score'} ne ' ') {
823: $score += $seq_stats->{'score'};
824: $Statistics->{$symb}->{'num_students'}++;
825: }
826: $max += $seq_stats->{'max'};
827: }
828: if (! defined($score)) {
829: $score = ' ' x $total_sum_width;
830: } else {
831: $score = sprintf("%.0f",$score);
832: $score = (' 'x(3-length($score))).$score;
833: }
834: $Str .= ' '.' 'x($total_sum_width-length($score)).$score.' / '.$max;
835: $Str .= " \n";
836: #
837: $r->print($Str);
838: #
839: $r->rflush();
840: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
841: return;
842: }
843:
844: sub html_finish {
845: my ($r) = @_;
846: return if (! defined($navmap));
847: #
848: # Check for suppressed output and close the progress window if so
849: $r->print("</pre>\n");
850: if ($chosen_output->{'summary_table'}) {
851: if ($single_student_mode) {
852: $r->print(&SingleStudentTotal());
853: } else {
854: $r->print(&StudentAverageTotal());
855: }
856: }
857: $r->rflush();
858: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
859: undef($navmap);
860: return;
861: }
862:
863: sub StudentAverageTotal {
864: my $Str = '<h3>'.&mt('Summary Tables').'</h3>'.$/;
865: $Str .= '<table border=2 cellspacing="1">'."\n";
866: $Str .= '<tr>'.
867: '<th>'.&mt('Title').'</th>'.
868: '<th>'.&mt('Average').'</th>'.
869: '<th>'.&mt('Maximum').'</th>'.
870: '</tr>'.$/;
871: foreach my $seq (@sequences) {
872: my $symb = $seq->symb;
873: my $ave;
874: my $num_students = $Statistics->{$symb}->{'num_students'};
875: if ($num_students > 0) {
876: $ave = int(100*
877: ($Statistics->{$symb}->{'score'}/$num_students)
878: )/100;
879: } else {
880: $ave = 0;
881: }
882: my $max = $Statistics->{$symb}->{'max'};
883: $ave = sprintf("%.2f",$ave);
884: $Str .= '<tr><td>'.$seq->compTitle.'</td>'.
885: '<td align="right">'.$ave.' </td>'.
886: '<td align="right">'.$max.' '.'</td></tr>'."\n";
887: }
888: $Str .= "</table>\n";
889: return $Str;
890: }
891:
892: sub SingleStudentTotal {
893: return if (! defined($navmap));
894: my $student = &Apache::lonstatistics::current_student();
895: my $Str = '<h3>'.&mt('Summary table for [_1] ([_2]@[_3])',
896: $student->{'fullname'},
897: $student->{'username'},$student->{'domain'}).'</h3>';
898: $Str .= $/;
899: $Str .= '<table border=2 cellspacing="1">'."\n";
900: $Str .=
901: '<tr>'.
902: '<th>'.&mt('Sequence or Folder').'</th>';
903: if ($chosen_output->{'base'} eq 'tries') {
904: $Str .= '<th>'.&mt('Parts Correct').'</th>';
905: } else {
906: $Str .= '<th>'.&mt('Score').'</th>';
907: }
908: $Str .= '<th>'.&mt('Maximum').'</th>'."</tr>\n";
909: my $total = 0;
910: my $total_max = 0;
911: foreach my $seq (@sequences) {
912: my $value = $Statistics->{$seq->symb}->{'score'};
913: my $max = $Statistics->{$seq->symb}->{'max'};
914: $Str .= '<tr><td>'.&HTML::Entities::encode($seq->compTitle).'</td>'.
915: '<td align="right">'.$value.'</td>'.
916: '<td align="right">'.$max.'</td></tr>'."\n";
917: $total += $value;
918: $total_max +=$max;
919: }
920: $Str .= '<tr><td><b>'.&mt('Total').'</b></td>'.
921: '<td align="right">'.$total.'</td>'.
922: '<td align="right">'.$total_max."</td></tr>\n";
923: $Str .= "</table>\n";
924: return $Str;
925: }
926:
927: }
928:
929: #######################################################
930: #######################################################
931:
932: =pod
933:
934: =head2 EXCEL subroutines
935:
936: =item &excel_initialize($r)
937:
938: =item &excel_outputstudent($r,$student)
939:
940: =item &excel_finish($r)
941:
942: =cut
943:
944: #######################################################
945: #######################################################
946: {
947:
948: my $excel_sheet;
949: my $excel_workbook;
950: my $format;
951:
952: my $filename;
953: my $rows_output;
954: my $cols_output;
955:
956: my %prog_state; # progress window state
957: my $request_aborted;
958:
959: my $total_formula;
960: my $maximum_formula;
961: my %formula_data;
962:
963: my $navmap;
964: my @sequences;
965:
966: sub excel_initialize {
967: my ($r) = @_;
968: #
969: undef ($excel_sheet);
970: undef ($excel_workbook);
971: undef ($filename);
972: undef ($rows_output);
973: undef ($cols_output);
974: undef (%prog_state);
975: undef ($request_aborted);
976: undef ($total_formula);
977: undef ($maximum_formula);
978: #
979: undef(%formula_data);
980: #
981: undef($navmap);
982: undef(@sequences);
983: ($navmap,@sequences) =
984: &Apache::lonstatistics::selected_sequences_with_assessments();
985: if (! ref($navmap)) {
986: # Unable to get data, so bail out
987: $r->print("<h3>".
988: &mt('Unable to retrieve course information.').
989: '</h3>');
990: }
991: #
992: my $total_columns = scalar(&get_student_fields_to_show());
993: my $num_students = scalar(@Apache::lonstatistics::Students);
994: #
995: foreach my $seq (@sequences) {
996: if ($chosen_output->{'every_problem'}) {
997: $total_columns+=&count_parts($navmap,$seq);
998: }
999: # Add 2 because we need a 'sequence_sum' and 'total' column for each
1000: $total_columns += 2;
1001: }
1002: my $too_many_cols_error_message =
1003: '<h2>'.&mt('Unable to Complete Request').'</h2>'.$/.
1004: '<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>'.$/.
1005: '<p>'.&mt('You may consider reducing the number of <b>Sequences or Folders</b> you have selected.').'</p>'.$/.
1006: '<p>'.&mt('LON-CAPA can produce <b>CSV</b> files of this data or Excel files of the <b>Scores Summary</b> data.').'</p>'.$/;
1007: if ($chosen_output->{'base'} eq 'tries' && $total_columns > 255) {
1008: $r->print($too_many_cols_error_message);
1009: $request_aborted = 1;
1010: }
1011: if ($chosen_output->{'base'} eq 'scores' && $total_columns > 255) {
1012: $r->print($too_many_cols_error_message);
1013: $request_aborted = 1;
1014: }
1015: return if ($request_aborted);
1016: #
1017: #
1018: $excel_workbook = undef;
1019: $excel_sheet = undef;
1020: #
1021: $rows_output = 0;
1022: $cols_output = 0;
1023: #
1024: # Determine rows
1025: my $header_row = $rows_output++;
1026: my $description_row = $rows_output++;
1027: my $notes_row = $rows_output++;
1028: $rows_output++; # blank row
1029: my $summary_header_row;
1030: if ($chosen_output->{'summary_table'}) {
1031: $summary_header_row = $rows_output++;
1032: $rows_output+= scalar(@sequences);
1033: $rows_output++;
1034: }
1035: my $sequence_name_row = $rows_output++;
1036: my $resource_name_row = $rows_output++;
1037: my $maximum_data_row = $rows_output++;
1038: if (! $chosen_output->{'maximum_row'}) {
1039: $rows_output--;
1040: }
1041: my $first_data_row = $rows_output++;
1042: #
1043: # Create sheet
1044: ($excel_workbook,$filename,$format)=
1045: &Apache::loncommon::create_workbook($r);
1046: return if (! defined($excel_workbook));
1047: #
1048: # Add a worksheet
1049: my $sheetname = $env{'course.'.$env{'request.course.id'}.'.description'};
1050: $sheetname = &Apache::loncommon::clean_excel_name($sheetname);
1051: $excel_sheet = $excel_workbook->addworksheet($sheetname);
1052: #
1053: # Put the course description in the header
1054: $excel_sheet->write($header_row,$cols_output++,
1055: $env{'course.'.$env{'request.course.id'}.'.description'},
1056: $format->{'h1'});
1057: $cols_output += 3;
1058: #
1059: # Put a description of the sections listed
1060: my $sectionstring = '';
1061: my @Sections = &Apache::lonstatistics::get_selected_sections();
1062: $excel_sheet->write($header_row,$cols_output++,
1063: &Apache::lonstatistics::section_and_enrollment_description('plaintext'),
1064: $format->{'h3'});
1065: #
1066: # Put the date in there too
1067: $excel_sheet->write($header_row,$cols_output++,
1068: 'Compiled on '.localtime(time),$format->{'h3'});
1069: #
1070: $cols_output = 0;
1071: $excel_sheet->write($description_row,$cols_output++,
1072: $chosen_output->{'shortdesc'},
1073: $format->{'b'});
1074: #
1075: $cols_output = 0;
1076: $excel_sheet->write($notes_row,$cols_output++,
1077: $chosen_output->{'non_html_notes'},
1078: $format->{'i'});
1079:
1080: ##############################################
1081: # Output headings for the raw data
1082: ##############################################
1083: #
1084: # Add the student headers
1085: $cols_output = 0;
1086: foreach my $field (&get_student_fields_to_show()) {
1087: $excel_sheet->write($resource_name_row,$cols_output++,$field,
1088: $format->{'bold'});
1089: }
1090: #
1091: # Add the remaining column headers
1092: my $total_formula_string = '=0';
1093: my $maximum_formula_string = '=0';
1094: foreach my $seq (@sequences) {
1095: my $symb = $seq->symb;
1096: $excel_sheet->write($sequence_name_row,,
1097: $cols_output,$seq->compTitle,$format->{'bold'});
1098: # Determine starting cell
1099: $formula_data{$symb}->{'Excel:startcell'}=
1100: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1101: ($first_data_row,$cols_output);
1102: $formula_data{$symb}->{'Excel:startcol'}=$cols_output;
1103: my $count = 0;
1104: if ($chosen_output->{'every_problem'}) {
1105: # Put the names of the problems and parts into the sheet
1106: foreach my $res (&get_resources($navmap,$seq)) {
1107: if (scalar(@{$res->parts}) > 1) {
1108: foreach my $part (@{$res->parts}) {
1109: $excel_sheet->write($resource_name_row,
1110: $cols_output++,
1111: $res->compTitle.' part '.$res->part_display($part),
1112: $format->{'bold'});
1113: $count++;
1114: }
1115: } else {
1116: $excel_sheet->write($resource_name_row,
1117: $cols_output++,
1118: $res->compTitle,$format->{'bold'});
1119: $count++;
1120: }
1121: }
1122: }
1123: # Determine ending cell
1124: if ($count <= 1) {
1125: $formula_data{$symb}->{'Excel:endcell'} = $formula_data{$symb}->{'Excel:startcell'};
1126: $formula_data{$symb}->{'Excel:endcol'} = $formula_data{$symb}->{'Excel:startcol'};
1127: } else {
1128: $formula_data{$symb}->{'Excel:endcell'} =
1129: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1130: ($first_data_row,$cols_output-1);
1131: $formula_data{$symb}->{'Excel:endcol'} = $cols_output-1;
1132: }
1133: # Create the formula for summing up this sequence
1134: if (! exists($formula_data{$symb}->{'Excel:endcell'}) ||
1135: ! defined($formula_data{$symb}->{'Excel:endcell'})) {
1136: $formula_data{$symb}->{'Excel:endcell'} = $formula_data{$symb}->{'Excel:startcell'};
1137: }
1138: $formula_data{$symb}->{'Excel:sum'}= $excel_sheet->store_formula
1139: ('=SUM('.$formula_data{$symb}->{'Excel:startcell'}.
1140: ':'.$formula_data{$symb}->{'Excel:endcell'}.')');
1141: # Determine cell the score is held in
1142: $formula_data{$symb}->{'Excel:scorecell'} =
1143: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1144: ($first_data_row,$cols_output);
1145: $formula_data{$symb}->{'Excel:scorecol'}=$cols_output;
1146: if ($chosen_output->{'base'} eq 'parts correct total') {
1147: $excel_sheet->write($resource_name_row,$cols_output++,
1148: 'parts correct',
1149: $format->{'bold'});
1150: } elsif ($chosen_output->{'sequence_sum'}) {
1151: if ($chosen_output->{'correct'}) {
1152: # Only reporting the number correct, so do not call it score
1153: $excel_sheet->write($resource_name_row,$cols_output++,
1154: 'sum',
1155: $format->{'bold'});
1156: } else {
1157: $excel_sheet->write($resource_name_row,$cols_output++,
1158: 'score',
1159: $format->{'bold'});
1160: }
1161: }
1162: #
1163: $total_formula_string.='+'.
1164: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1165: ($first_data_row,$cols_output-1);
1166: if ($chosen_output->{'sequence_max'}) {
1167: $excel_sheet->write($resource_name_row,$cols_output,
1168: 'maximum',
1169: $format->{'bold'});
1170: $formula_data{$symb}->{'Excel:maxcell'} =
1171: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1172: ($first_data_row,$cols_output);
1173: $formula_data{$symb}->{'Excel:maxcol'}=$cols_output;
1174: $maximum_formula_string.='+'.
1175: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1176: ($first_data_row,$cols_output);
1177: $cols_output++;
1178:
1179: }
1180: }
1181: if ($chosen_output->{'grand_total'}) {
1182: $excel_sheet->write($resource_name_row,$cols_output++,'Total',
1183: $format->{'bold'});
1184: }
1185: if ($chosen_output->{'grand_maximum'}) {
1186: $excel_sheet->write($resource_name_row,$cols_output++,'Max. Total',
1187: $format->{'bold'});
1188: }
1189: $total_formula = $excel_sheet->store_formula($total_formula_string);
1190: $maximum_formula = $excel_sheet->store_formula($maximum_formula_string);
1191: ##############################################
1192: # Output a row for MAX, if appropriate
1193: ##############################################
1194: if ($chosen_output->{'maximum_row'}) {
1195: $cols_output = 0;
1196: foreach my $field (&get_student_fields_to_show()) {
1197: if ($field eq 'username' || $field eq 'fullname' ||
1198: $field eq 'id') {
1199: $excel_sheet->write($maximum_data_row,$cols_output++,'Maximum',
1200: $format->{'bold'});
1201: } else {
1202: $excel_sheet->write($maximum_data_row,$cols_output++,'');
1203: }
1204: }
1205: #
1206: # Add the maximums for each sequence or assessment
1207: my %total_cell_translation;
1208: my %maximum_cell_translation;
1209: foreach my $seq (@sequences) {
1210: my $symb = $seq->symb;
1211: $cols_output=$formula_data{$symb}->{'Excel:startcol'};
1212: $total_cell_translation{$formula_data{$symb}->{'Excel:scorecell'}}=
1213: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1214: ($maximum_data_row,$formula_data{$symb}->{'Excel:scorecol'});
1215: $maximum_cell_translation{$formula_data{$symb}->{'Excel:maxcell'}}=
1216: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1217: ($maximum_data_row,$formula_data{$symb}->{'Excel:maxcol'});
1218: my $weight;
1219: my $max = 0;
1220: foreach my $resource (&get_resources($navmap,$seq)) {
1221: foreach my $part (@{$resource->parts}){
1222: $weight = 1;
1223: if ($chosen_output->{'scores'}) {
1224: $weight = &Apache::lonnet::EXT
1225: ('resource.'.$part.'.weight',$resource->{'symb'},
1226: undef,undef,undef);
1227: if (!defined($weight) || ($weight eq '')) {
1228: $weight=1;
1229: }
1230: }
1231: if ($chosen_output->{'scores'} &&
1232: $chosen_output->{'every_problem'}) {
1233: $excel_sheet->write($maximum_data_row,$cols_output++,
1234: $weight);
1235: }
1236: $max += $weight;
1237: }
1238: }
1239: #
1240: if ($chosen_output->{'sequence_sum'} &&
1241: $chosen_output->{'every_problem'}) {
1242: my %replaceCells=
1243: ('^'.$formula_data{$symb}->{'Excel:startcell'}.':'.
1244: $formula_data{$symb}->{'Excel:endcell'}.'$' =>
1245: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell($maximum_data_row,$formula_data{$symb}->{'Excel:startcol'}).':'.
1246: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell($maximum_data_row,$formula_data{$symb}->{'Excel:endcol'}));
1247: $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1248: $formula_data{$symb}->{'Excel:sum'},undef,
1249: %replaceCells);
1250:
1251: } elsif ($chosen_output->{'sequence_sum'}) {
1252: $excel_sheet->write($maximum_data_row,$cols_output++,$max);
1253: }
1254: if ($chosen_output->{'sequence_max'}) {
1255: $excel_sheet->write($maximum_data_row,$cols_output++,$max);
1256: }
1257: #
1258: }
1259: if ($chosen_output->{'grand_total'}) {
1260: $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1261: $total_formula,undef,
1262: %total_cell_translation);
1263: }
1264: if ($chosen_output->{'grand_maximum'}) {
1265: $excel_sheet->repeat_formula($maximum_data_row,$cols_output++,
1266: $maximum_formula,undef,
1267: %maximum_cell_translation);
1268: }
1269: } # End of MAXIMUM row output if ($chosen_output->{'maximum_row'}) {
1270: $rows_output = $first_data_row;
1271: ##############################################
1272: # Output summary table, which actually is above the sequence name row.
1273: ##############################################
1274: if ($chosen_output->{'summary_table'}) {
1275: $cols_output = 0;
1276: $excel_sheet->write($summary_header_row,$cols_output++,
1277: 'Summary Table',$format->{'bold'});
1278: if ($chosen_output->{'maximum_row'}) {
1279: $excel_sheet->write($summary_header_row,$cols_output++,
1280: 'Maximum',$format->{'bold'});
1281: }
1282: $excel_sheet->write($summary_header_row,$cols_output++,
1283: 'Average',$format->{'bold'});
1284: $excel_sheet->write($summary_header_row,$cols_output++,
1285: 'Median',$format->{'bold'});
1286: $excel_sheet->write($summary_header_row,$cols_output++,
1287: 'Std Dev',$format->{'bold'});
1288: my $row = $summary_header_row+1;
1289: foreach my $seq (@sequences) {
1290: my $symb = $seq->symb;
1291: $cols_output = 0;
1292: $excel_sheet->write($row,$cols_output++,
1293: $seq->compTitle,
1294: $format->{'bold'});
1295: if ($chosen_output->{'maximum_row'}) {
1296: $excel_sheet->write
1297: ($row,$cols_output++,
1298: '='.
1299: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1300: ($maximum_data_row,$formula_data{$symb}->{'Excel:scorecol'})
1301: );
1302: }
1303: my $range =
1304: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1305: ($first_data_row,$formula_data{$symb}->{'Excel:scorecol'}).
1306: ':'.
1307: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1308: ($first_data_row+$num_students-1,$formula_data{$symb}->{'Excel:scorecol'});
1309: $excel_sheet->write($row,$cols_output++,
1310: '=AVERAGE('.$range.')');
1311: $excel_sheet->write($row,$cols_output++,
1312: '=MEDIAN('.$range.')');
1313: $excel_sheet->write($row,$cols_output++,
1314: '=STDEV('.$range.')');
1315: $row++;
1316: }
1317: }
1318: ##############################################
1319: # Take care of non-excel initialization
1320: ##############################################
1321: #
1322: # Let the user know what we are doing
1323: my $studentcount = scalar(@Apache::lonstatistics::Students);
1324: if ($env{'form.SelectedStudent'}) {
1325: $studentcount = '1';
1326: }
1327: if ($studentcount > 1) {
1328: $r->print('<h1>'.&mt('Compiling Excel spreadsheet for [_1] students',
1329: $studentcount)."</h1>\n");
1330: } else {
1331: $r->print('<h1>'.
1332: &mt('Compiling Excel spreadsheet for 1 student').
1333: "</h1>\n");
1334: }
1335: $r->rflush();
1336: #
1337: # Initialize progress window
1338: %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1339: ($r,'Excel File Compilation Status',
1340: 'Excel File Compilation Progress', $studentcount,
1341: 'inline',undef,'Statistics','stats_status');
1342: #
1343: &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
1344: 'Processing first student');
1345: return;
1346: }
1347:
1348: sub excel_outputstudent {
1349: my ($r,$student) = @_;
1350: if ($request_aborted || ! defined($navmap) || ! defined($excel_sheet)) {
1351: return;
1352: }
1353: $cols_output=0;
1354: #
1355: # Write out student data
1356: my @to_show = &get_student_fields_to_show();
1357: foreach my $field (@to_show) {
1358: my $value = $student->{$field};
1359: if ($field eq 'comments') {
1360: $value = &Apache::lonmsg::retrieve_instructor_comments
1361: ($student->{'username'},$student->{'domain'});
1362: }
1363: $excel_sheet->write($rows_output,$cols_output++,$value);
1364: }
1365: #
1366: # Get student assessment data
1367: my %StudentsData;
1368: my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
1369: $student->{'domain'},
1370: undef,
1371: $env{'request.course.id'});
1372: if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
1373: %StudentsData = @tmp;
1374: }
1375: #
1376: # Write out sequence scores and totals data
1377: my %total_cell_translation;
1378: my %maximum_cell_translation;
1379: foreach my $seq (@sequences) {
1380: my $symb = $seq->symb;
1381: $cols_output = $formula_data{$symb}->{'Excel:startcol'};
1382: # Keep track of cells to translate in total cell
1383: $total_cell_translation{$formula_data{$symb}->{'Excel:scorecell'}} =
1384: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1385: ($rows_output,$formula_data{$symb}->{'Excel:scorecol'});
1386: # and maximum cell
1387: $maximum_cell_translation{$formula_data{$symb}->{'Excel:maxcell'}} =
1388: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell
1389: ($rows_output,$formula_data{$symb}->{'Excel:maxcol'});
1390: #
1391: my ($performance,$performance_length,$score,$seq_max,$rawdata);
1392: if ($chosen_output->{'tries'} || $chosen_output->{'correct'}){
1393: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1394: &student_tries_on_sequence($student,\%StudentsData,
1395: $navmap,$seq,'no');
1396: } else {
1397: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1398: &student_performance_on_sequence($student,\%StudentsData,
1399: $navmap,$seq,'no');
1400: }
1401: if ($chosen_output->{'every_problem'}) {
1402: if ($chosen_output->{'correct'}) {
1403: # only indiciate if each item is correct or not
1404: foreach my $value (@$rawdata) {
1405: # nonzero means correct
1406: $value = 1 if ($value > 0);
1407: $excel_sheet->write($rows_output,$cols_output++,$value);
1408: }
1409: } else {
1410: foreach my $value (@$rawdata) {
1411: if ($score eq ' ' || !defined($value)) {
1412: $cols_output++;
1413: } else {
1414: $excel_sheet->write($rows_output,$cols_output++,
1415: $value);
1416: }
1417: }
1418: }
1419: }
1420: if ($chosen_output->{'sequence_sum'} &&
1421: $chosen_output->{'every_problem'}) {
1422: # Write a formula for the sum of this sequence
1423: my %replaceCells=
1424: ('^'.$formula_data{$symb}->{'Excel:startcell'}.':'.$formula_data{$symb}->{'Excel:endcell'}.'$'
1425: =>
1426: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell($rows_output,$formula_data{$symb}->{'Excel:startcol'}).':'.
1427: &Spreadsheet::WriteExcel::Utility::xl_rowcol_to_cell($rows_output,$formula_data{$symb}->{'Excel:endcol'})
1428: );
1429: # The undef is for the format
1430: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1431: $formula_data{$symb}->{'Excel:sum'},undef,
1432: %replaceCells);
1433: } elsif ($chosen_output->{'sequence_sum'}) {
1434: if ($score eq ' ') {
1435: $cols_output++;
1436: } else {
1437: $excel_sheet->write($rows_output,$cols_output++,$score);
1438: }
1439: }
1440: if ($chosen_output->{'sequence_max'}) {
1441: $excel_sheet->write($rows_output,$cols_output++,$seq_max);
1442: }
1443: }
1444: #
1445: if ($chosen_output->{'grand_total'}) {
1446: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1447: $total_formula,undef,
1448: %total_cell_translation);
1449: }
1450: if ($chosen_output->{'grand_maximum'}) {
1451: $excel_sheet->repeat_formula($rows_output,$cols_output++,
1452: $maximum_formula,undef,
1453: %maximum_cell_translation);
1454: }
1455: #
1456: # Bookkeeping
1457: $rows_output++;
1458: $cols_output=0;
1459: #
1460: # Update the progress window
1461: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1462: return;
1463: }
1464:
1465: sub excel_finish {
1466: my ($r) = @_;
1467: if ($request_aborted || ! defined($navmap) || ! defined($excel_sheet)) {
1468: return;
1469: }
1470: #
1471: # Write the excel file
1472: $excel_workbook->close();
1473: my $c = $r->connection();
1474: #
1475: return if($c->aborted());
1476: #
1477: # Close the progress window
1478: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1479: #
1480: # Tell the user where to get their excel file
1481: $r->print('<br />'.
1482: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
1483: $r->rflush();
1484: return;
1485: }
1486:
1487: }
1488: #######################################################
1489: #######################################################
1490:
1491: =pod
1492:
1493: =head2 CSV output routines
1494:
1495: =item &csv_initialize($r)
1496:
1497: =item &csv_outputstudent($r,$student)
1498:
1499: =item &csv_finish($r)
1500:
1501: =cut
1502:
1503: #######################################################
1504: #######################################################
1505: {
1506:
1507: my $outputfile;
1508: my $filename;
1509: my $request_aborted;
1510: my %prog_state; # progress window state
1511: my $navmap;
1512: my @sequences;
1513:
1514: sub csv_initialize{
1515: my ($r) = @_;
1516: #
1517: # Clean up
1518: undef($outputfile);
1519: undef($filename);
1520: undef($request_aborted);
1521: undef(%prog_state);
1522: #
1523: undef($navmap);
1524: undef(@sequences);
1525: ($navmap,@sequences) =
1526: &Apache::lonstatistics::selected_sequences_with_assessments();
1527: if (! ref($navmap)) {
1528: # Unable to get data, so bail out
1529: $r->print("<h3>".
1530: &mt('Unable to retrieve course information.').
1531: '</h3>');
1532: }
1533: #
1534: # Deal with unimplemented requests
1535: $request_aborted = undef;
1536: if ($chosen_output->{'base'} =~ /final table/) {
1537: $r->print(<<END);
1538: <h2>Unable to Complete Request</h2>
1539: <p>
1540: The <b>Summary Table (Scores)</b> option is not available for non-HTML output.
1541: </p>
1542: END
1543: $request_aborted = 1;
1544: }
1545: return if ($request_aborted);
1546: #
1547: # Initialize progress window
1548: my $studentcount = scalar(@Apache::lonstatistics::Students);
1549: %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1550: ($r,'CSV File Compilation Status',
1551: 'CSV File Compilation Progress', $studentcount,
1552: 'inline',undef,'Statistics','stats_status');
1553: #
1554: # Open a file
1555: ($outputfile,$filename) = &Apache::loncommon::create_text_file($r,'csv');
1556: if (! defined($outputfile)) { return ''; }
1557: #
1558: # Datestamp
1559: my $description = $env{'course.'.$env{'request.course.id'}.'.description'};
1560: print $outputfile '"'.&Apache::loncommon::csv_translate($description).'",'.
1561: '"'.&Apache::loncommon::csv_translate(scalar(localtime(time))).'"'.
1562: "\n";
1563: print $outputfile '"'.
1564: &Apache::loncommon::csv_translate
1565: (&Apache::lonstatistics::section_and_enrollment_description()).
1566: '"'."\n";
1567: foreach my $item ('shortdesc','non_html_notes') {
1568: next if (! exists($chosen_output->{$item}));
1569: print $outputfile
1570: '"'.&Apache::loncommon::csv_translate($chosen_output->{$item}).'"'.
1571: "\n";
1572: }
1573: #
1574: # Print out the headings
1575: my $sequence_row = '';
1576: my $resource_row = undef;
1577: foreach my $field (&get_student_fields_to_show()) {
1578: $sequence_row .='"",';
1579: $resource_row .= '"'.&Apache::loncommon::csv_translate($field).'",';
1580: }
1581: foreach my $seq (@sequences) {
1582: $sequence_row .= '"'.
1583: &Apache::loncommon::csv_translate($seq->compTitle).'",';
1584: my $count = 0;
1585: if ($chosen_output->{'every_problem'}) {
1586: foreach my $res (&get_resources($navmap,$seq)) {
1587: if (scalar(@{$res->parts}) < 1) {
1588: next;
1589: }
1590: foreach my $part (@{$res->parts}) {
1591: $resource_row .= '"'.
1592: &Apache::loncommon::csv_translate
1593: ($res->compTitle.', Part '.$res->part_display($part)).'",';
1594: $count++;
1595: }
1596: }
1597: }
1598: $sequence_row.='"",'x$count;
1599: if ($chosen_output->{'sequence_sum'}) {
1600: if($chosen_output->{'correct'}) {
1601: $resource_row .= '"sum",';
1602: } else {
1603: $resource_row .= '"score",';
1604: }
1605: }
1606: if ($chosen_output->{'sequence_max'}) {
1607: $sequence_row.= '"",';
1608: $resource_row .= '"maximum possible",';
1609: }
1610: }
1611: if ($chosen_output->{'grand_total'}) {
1612: $sequence_row.= '"",';
1613: $resource_row.= '"Total",';
1614: }
1615: if ($chosen_output->{'grand_maximum'}) {
1616: $sequence_row.= '"",';
1617: $resource_row.= '"Maximum",';
1618: }
1619: chomp($sequence_row);
1620: chomp($resource_row);
1621: print $outputfile $sequence_row."\n";
1622: print $outputfile $resource_row."\n";
1623: return;
1624: }
1625:
1626: sub csv_outputstudent {
1627: my ($r,$student) = @_;
1628: if ($request_aborted || ! defined($navmap) || ! defined($outputfile)) {
1629: return;
1630: }
1631: my $Str = '';
1632: #
1633: # Output student fields
1634: my @to_show = &get_student_fields_to_show();
1635: foreach my $field (@to_show) {
1636: my $value = $student->{$field};
1637: if ($field eq 'comments') {
1638: $value = &Apache::lonmsg::retrieve_instructor_comments
1639: ($student->{'username'},$student->{'domain'});
1640: }
1641: $Str .= '"'.&Apache::loncommon::csv_translate($value).'",';
1642: }
1643: #
1644: # Get student assessment data
1645: my %StudentsData;
1646: my @tmp = &Apache::loncoursedata::get_current_state($student->{'username'},
1647: $student->{'domain'},
1648: undef,
1649: $env{'request.course.id'});
1650: if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
1651: %StudentsData = @tmp;
1652: }
1653: #
1654: # Output performance data
1655: my $total = 0;
1656: my $maximum = 0;
1657: foreach my $seq (@sequences) {
1658: my ($performance,$performance_length,$score,$seq_max,$rawdata);
1659: if ($chosen_output->{'tries'}){
1660: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1661: &student_tries_on_sequence($student,\%StudentsData,
1662: $navmap,$seq,'no');
1663: } else {
1664: ($performance,$performance_length,$score,$seq_max,$rawdata) =
1665: &student_performance_on_sequence($student,\%StudentsData,
1666: $navmap,$seq,'no');
1667: }
1668: if ($chosen_output->{'every_problem'}) {
1669: if ($chosen_output->{'correct'}) {
1670: $score = 0;
1671: # Deal with number of parts correct data
1672: $Str .= '"'.join('","',( map { if ($_>0) {
1673: $score += 1;
1674: 1;
1675: } else {
1676: 0;
1677: }
1678: } @$rawdata)).'",';
1679: } else {
1680: $Str .= '"'.join('","',(@$rawdata)).'",';
1681: }
1682: }
1683: if ($chosen_output->{'sequence_sum'}) {
1684: $Str .= '"'.$score.'",';
1685: }
1686: if ($chosen_output->{'sequence_max'}) {
1687: $Str .= '"'.$seq_max.'",';
1688: }
1689: $total+=$score;
1690: $maximum += $seq_max;
1691: }
1692: if ($chosen_output->{'grand_total'}) {
1693: $Str .= '"'.$total.'",';
1694: }
1695: if ($chosen_output->{'grand_maximum'}) {
1696: $Str .= '"'.$maximum.'",';
1697: }
1698: chop($Str);
1699: $Str .= "\n";
1700: print $outputfile $Str;
1701: #
1702: # Update the progress window
1703: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last student');
1704: return;
1705: }
1706:
1707: sub csv_finish {
1708: my ($r) = @_;
1709: if ($request_aborted || ! defined($navmap) || ! defined($outputfile)) {
1710: return;
1711: }
1712: close($outputfile);
1713: #
1714: my $c = $r->connection();
1715: return if ($c->aborted());
1716: #
1717: # Close the progress window
1718: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1719: #
1720: # Tell the user where to get their csv file
1721: $r->print('<br />'.
1722: '<a href="'.$filename.'">'.&mt('Your csv file.').'</a>'."\n");
1723: $r->rflush();
1724: return;
1725:
1726: }
1727:
1728: }
1729:
1730: #######################################################
1731: #######################################################
1732:
1733: =pod
1734:
1735: =item &StudentTriesOnSequence()
1736:
1737: Inputs:
1738:
1739: =over 4
1740:
1741: =item $student
1742:
1743: =item $studentdata Hash ref to all student data
1744:
1745: =item $seq Hash ref, the sequence we are working on
1746:
1747: =item $links if defined we will output links to each resource.
1748:
1749: =back
1750:
1751: =cut
1752:
1753: #######################################################
1754: #######################################################
1755: sub student_tries_on_sequence {
1756: my ($student,$studentdata,$navmap,$seq,$links) = @_;
1757: $links = 'no' if (! defined($links));
1758: my $Str = '';
1759: my ($sum,$max) = (0,0);
1760: my $performance_length = 0;
1761: my @TriesData = ();
1762: my $tries;
1763: my $hasdata = 0; # flag - true if the student has any data on the sequence
1764: foreach my $resource (&get_resources($navmap,$seq)) {
1765: my $resource_data = $studentdata->{$resource->symb};
1766: my $value = '';
1767: foreach my $partnum (@{$resource->parts()}) {
1768: $tries = undef;
1769: $max++;
1770: $performance_length++;
1771: my $symbol = ' '; # default to space
1772: #
1773: my $awarded = 0;
1774: if (exists($resource_data->{'resource.'.$partnum.'.awarded'})) {
1775: $awarded = $resource_data->{'resource.'.$partnum.'.awarded'};
1776: $awarded = 0 if (! $awarded);
1777: }
1778: #
1779: my $status = '';
1780: if (exists($resource_data->{'resource.'.$partnum.'.solved'})) {
1781: $status = $resource_data->{'resource.'.$partnum.'.solved'};
1782: }
1783: #
1784: my $tries = 0;
1785: if(exists($resource_data->{'resource.'.$partnum.'.tries'})) {
1786: $tries = $resource_data->{'resource.'.$partnum.'.tries'};
1787: $hasdata =1;
1788: }
1789: #
1790: if ($awarded > 0) {
1791: # The student has gotten the problem correct to some degree
1792: if ($status eq 'excused') {
1793: $symbol = 'x';
1794: $max--;
1795: } elsif ($status eq 'correct_by_override') {
1796: $symbol = '+';
1797: $sum++;
1798: } elsif ($tries > 0) {
1799: if ($tries > 9) {
1800: $symbol = '*';
1801: } else {
1802: $symbol = $tries;
1803: }
1804: $sum++;
1805: } else {
1806: $symbol = '+';
1807: $sum++;
1808: }
1809: } else {
1810: # The student has the problem incorrect or it is ungraded
1811: if ($status eq 'excused') {
1812: $symbol = 'x';
1813: $max--;
1814: } elsif ($status eq 'incorrect_by_override') {
1815: $symbol = '-';
1816: } elsif ($status eq 'ungraded_attempted') {
1817: $symbol = 'u';
1818: } elsif ($status eq 'incorrect_attempted' ||
1819: $tries > 0) {
1820: $symbol = '.';
1821: } else {
1822: # Problem is wrong and has not been attempted.
1823: $symbol=' ';
1824: }
1825: }
1826: #
1827: if (! defined($tries)) {
1828: $tries = 0;
1829: }
1830: if ($status =~ /^(incorrect|ungraded)/) {
1831: # Bug 3390: show '-' for tries on incorrect problems
1832: # (csv & excel only)
1833: push(@TriesData,-$tries);
1834: } else {
1835: push (@TriesData,$tries);
1836: }
1837: #
1838: if ( ($links eq 'yes' && $symbol ne ' ') ||
1839: ($links eq 'all')) {
1840: if (length($symbol) > 1) {
1841: &Apache::lonnet::logthis('length of symbol "'.$symbol.'" > 1');
1842: }
1843: $symbol = '<a href="/adm/grades'.
1844: '?symb='.&Apache::lonnet::escape($resource->symb).
1845: '&student='.$student->{'username'}.
1846: '&userdom='.$student->{'domain'}.
1847: '&command=submission">'.$symbol.'</a>';
1848: }
1849: $value .= $symbol;
1850: }
1851: $Str .= $value;
1852: }
1853: if ($seq->randompick()) {
1854: $max = $seq->randompick();
1855: }
1856: if (! $hasdata && $sum == 0) {
1857: $sum = ' ';
1858: }
1859: return ($Str,$performance_length,$sum,$max,\@TriesData);
1860: }
1861:
1862: #######################################################
1863: #######################################################
1864:
1865: =pod
1866:
1867: =item &student_performance_on_sequence
1868:
1869: Inputs:
1870:
1871: =over 4
1872:
1873: =item $student
1874:
1875: =item $studentdata Hash ref to all student data
1876:
1877: =item $seq Hash ref, the sequence we are working on
1878:
1879: =item $links if defined we will output links to each resource.
1880:
1881: =back
1882:
1883: =cut
1884:
1885: #######################################################
1886: #######################################################
1887: sub student_performance_on_sequence {
1888: my ($student,$studentdata,$navmap,$seq,$links) = @_;
1889: $links = 'no' if (! defined($links));
1890: my $Str = ''; # final result string
1891: my ($score,$max) = (0,0);
1892: my $performance_length = 0;
1893: my $symbol;
1894: my @ScoreData = ();
1895: my $partscore;
1896: my $hasdata = 0; # flag, 0 if there were no submissions on the sequence
1897: foreach my $resource (&get_resources($navmap,$seq)) {
1898: my $symb = $resource->symb;
1899: my $resource_data = $studentdata->{$symb};
1900: foreach my $part (@{$resource->parts()}) {
1901: $partscore = undef;
1902: my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
1903: $symb,
1904: $student->{'domain'},
1905: $student->{'username'},
1906: $student->{'section'});
1907: if (!defined($weight) || ($weight eq '')) {
1908: $weight=1;
1909: }
1910: #
1911: $max += $weight; # see the 'excused' branch below...
1912: $performance_length++; # one character per part
1913: $symbol = ' '; # default to space
1914: #
1915: my $awarded;
1916: if (exists($resource_data->{'resource.'.$part.'.awarded'})) {
1917: $awarded = $resource_data->{'resource.'.$part.'.awarded'};
1918: $awarded = 0 if (! $awarded);
1919: $hasdata = 1;
1920: }
1921: #
1922: $partscore = $weight*$awarded;
1923: if (! defined($awarded)) {
1924: $partscore = undef;
1925: }
1926: $score += $partscore;
1927: $symbol = $partscore;
1928: if (abs($symbol - sprintf("%.0f",$symbol)) < 0.001) {
1929: $symbol = sprintf("%.0f",$symbol);
1930: }
1931: if (length($symbol) > 1) {
1932: $symbol = '*';
1933: }
1934: if (exists($resource_data->{'resource.'.$part.'.solved'}) &&
1935: $resource_data->{'resource.'.$part.'.solved'} ne '') {
1936: my $status = $resource_data->{'resource.'.$part.'.solved'};
1937: if ($status eq 'excused') {
1938: $symbol = 'x';
1939: $max -= $weight; # Do not count 'excused' problems.
1940: } elsif ($status eq 'ungraded_attempted') {
1941: $symbol = 'u';
1942: }
1943: $hasdata = 1;
1944: } elsif ($resource_data->{'resource.'.$part.'.award'} eq 'DRAFT') {
1945: $symbol = 'd';
1946: $hasdata = 1;
1947: } elsif (!exists($resource_data->{'resource.'.$part.'.awarded'})){
1948: # Unsolved. Did they try?
1949: if (exists($resource_data->{'resource.'.$part.'.tries'})){
1950: $symbol = '.';
1951: $hasdata = 1;
1952: } else {
1953: $symbol = ' ';
1954: }
1955: }
1956: #
1957: if (! defined($partscore)) {
1958: $partscore = $symbol;
1959: }
1960: push (@ScoreData,$partscore);
1961: #
1962: if ( ($links eq 'yes' && $symbol ne ' ') || ($links eq 'all')) {
1963: $symbol = '<a href="/adm/grades'.
1964: '?symb='.&Apache::lonnet::escape($symb).
1965: '&student='.$student->{'username'}.
1966: '&userdom='.$student->{'domain'}.
1967: '&command=submission">'.$symbol.'</a>';
1968: }
1969: $Str .= $symbol;
1970: }
1971: }
1972: if (! $hasdata && $score == 0) {
1973: $score = ' ';
1974: }
1975: return ($Str,$performance_length,$score,$max,\@ScoreData);
1976: }
1977:
1978: #######################################################
1979: #######################################################
1980:
1981: =pod
1982:
1983: =item &CreateLegend()
1984:
1985: This function returns a formatted string containing the legend for the
1986: chart. The legend describes the symbols used to represent grades for
1987: problems.
1988:
1989: =cut
1990:
1991: #######################################################
1992: #######################################################
1993: sub CreateLegend {
1994: my $Str = "<p><pre>".
1995: " digit score or number of tries to get correct ".
1996: " * correct by student in more than 9 tries\n".
1997: " + correct by hand grading or override\n".
1998: " - incorrect by override\n".
1999: " . incorrect attempted\n".
2000: " u ungraded attempted\n".
2001: " d draft answer saved but not submitted\n".
2002: " not attempted (blank field)\n".
2003: " x excused".
2004: "</pre><p>";
2005: return $Str;
2006: }
2007:
2008: #######################################################
2009: #######################################################
2010:
2011: =pod
2012:
2013: =back
2014:
2015: =cut
2016:
2017: #######################################################
2018: #######################################################
2019:
2020: 1;
2021:
2022: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>