Annotation of loncom/interface/statistics/lonsurveyreports.pm, revision 1.15

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: #
1.15    ! albertel    3: # $Id: lonsurveyreports.pm,v 1.14 2006/08/18 15:15:38 raeburn Exp $
1.1       matthew     4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: package Apache::lonsurveyreports;
                     28: 
                     29: use strict;
1.8       albertel   30: use Apache::lonnet;
1.1       matthew    31: use Apache::loncommon();
                     32: use Apache::lonhtmlcommon();
                     33: use Apache::loncoursedata();
                     34: use Apache::lonstatistics;
                     35: use Apache::lonlocal;
                     36: use Apache::lonstathelpers;
1.4       matthew    37: use Spreadsheet::WriteExcel;
1.1       matthew    38: use HTML::Entities();
                     39: use Time::Local();
1.13      www        40: use lib '/home/httpd/lib/perl/';
                     41: use LONCAPA;
                     42:  
1.1       matthew    43: 
1.7       matthew    44: my @SubmitButtons = (
                     45:                      { name => 'break'},
                     46:                      { name => 'PrevProblem',
1.1       matthew    47:                        text => 'Previous Survey' },
                     48:                      { name => 'NextProblem',
                     49:                        text => 'Next Survey' },
1.7       matthew    50:                      { name => 'SelectAnother',
                     51:                        text => 'Choose a different Survey' },
1.1       matthew    52:                      { name => 'break'},
                     53:                      { name => 'Generate',
                     54:                        text => 'Generate Report'},
                     55:                      );
                     56: 
                     57: sub BuildSurveyReportsPage {
                     58:     my ($r,$c)=@_;
                     59:     #
                     60:     my %Saveable_Parameters = ('Status' => 'scalar',
                     61:                                'Section' => 'array',
                     62:                                'NumPlots' => 'scalar',
                     63:                                );
                     64:     &Apache::loncommon::store_course_settings('survey_reports',
                     65:                                               \%Saveable_Parameters);
                     66:     &Apache::loncommon::restore_course_settings('survey_resports',
                     67:                                                 \%Saveable_Parameters);
                     68:     #
                     69:     &Apache::lonstatistics::PrepareClasslist();
                     70:     #
                     71:     $r->print(&CreateInterface());
                     72:     #
                     73:     my @Students = @Apache::lonstatistics::Students;
                     74:     #
                     75:     if (@Students < 1) {
                     76:         $r->print('<h2>There are no students in the sections selected</h2>');
                     77:     }
                     78:     #
                     79:     my @CacheButtonHTML = 
                     80:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
                     81:     $r->rflush();
                     82:     #
1.8       albertel   83:     if (exists($env{'form.problemchoice'}) && 
                     84:         ! exists($env{'form.SelectAnother'})) {
1.7       matthew    85:         $r->print('&nbsp;'x3);
1.1       matthew    86:         foreach my $button (@SubmitButtons) {
                     87:             if ($button->{'name'} eq 'break') {
                     88:                 $r->print("<br />\n");
                     89:             } else {
                     90:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
                     91:                           'value="'.&mt($button->{'text'}).'" />');
                     92:                 $r->print('&nbsp;'x5);
                     93:             }
                     94:         }
                     95:         foreach my $html (@CacheButtonHTML) {
                     96:             $r->print($html.('&nbsp;'x5));
                     97:         }
                     98:         #
                     99:         $r->print('<hr />');
1.5       matthew   100:         $r->print('<h4>'.
1.7       matthew   101:                   &Apache::lonlocal::locallocaltime(time).','.
1.5       matthew   102:                   &Apache::lonstatistics::section_and_enrollment_description().
                    103:                   '</h4>');
1.1       matthew   104:         $r->rflush();
                    105:         #
                    106:         # Determine which problem we are to analyze
                    107:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
1.8       albertel  108:             ($env{'form.problemchoice'});
1.1       matthew   109:         #
1.3       matthew   110:         my ($navmap,$prev,$curr,$next) = 
1.1       matthew   111:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
                    112:                                                         '.',
                    113:                                                         'part_survey',
                    114:                                                         );
1.8       albertel  115:         if (exists($env{'form.PrevProblem'}) && defined($prev)) {
1.1       matthew   116:             $current_problem = $prev;
1.8       albertel  117:         } elsif (exists($env{'form.NextProblem'}) && defined($next)) {
1.1       matthew   118:             $current_problem = $next;
                    119:         } else {
                    120:             $current_problem = $curr;
                    121:         }
                    122:         #
                    123:         # Store the current problem choice and send it out in the form
1.8       albertel  124:         $env{'form.problemchoice'} = 
1.1       matthew   125:             &Apache::lonstathelpers::make_target_id($current_problem);
                    126:         $r->print('<input type="hidden" name="problemchoice" value="'.
1.8       albertel  127:                   $env{'form.problemchoice'}.'" />');
1.1       matthew   128:         #
                    129:         if (! defined($current_problem->{'resource'})) {
                    130:             $r->print('resource is undefined');
                    131:         } else {
                    132:             my $resource = $current_problem->{'resource'};
1.3       matthew   133:             $r->print('<h1>'.$resource->compTitle.'</h1>');
                    134:             $r->print('<h3>'.$resource->src.'</h3>');
1.8       albertel  135:             if ($env{'form.renderprob'} eq 'true') {
1.6       matthew   136:                 $r->print(&Apache::lonstathelpers::render_resource($resource));
                    137:             }
1.1       matthew   138:             $r->rflush();
                    139:             my %Data = &Apache::lonstathelpers::get_problem_data
1.3       matthew   140:                 ($resource->src);
1.7       matthew   141:             &compile_student_answers($r,$current_problem,\%Data,\@Students);
1.8       albertel  142:             if ($env{'form.output'} eq 'HTML' || 
                    143:                 ! defined($env{'form.output'})) {
1.4       matthew   144:                 &make_HTML_report($r,$current_problem,\%Data,\@Students);
1.8       albertel  145:             } elsif ($env{'form.output'} eq 'Excel') {
1.4       matthew   146:                 &make_Excel_report($r,$current_problem,\%Data,\@Students);
1.8       albertel  147:             } elsif ($env{'form.output'} eq 'TXT') {
1.7       matthew   148:                 &make_text_report($r,$current_problem,\%Data,\@Students);
1.4       matthew   149:             }
1.1       matthew   150:         }
                    151:         $r->print('<hr />');
                    152:     } else {
                    153:         $r->print('<input type="submit" name="Generate" value="'.
                    154:                   &mt('Generate Survey Report').'" />');
                    155:         $r->print('&nbsp;'x5);
                    156:         $r->print('<h3>'.&mt('Please select a Survey to analyze').'</h3>');
                    157:         $r->print(&SurveyProblemSelector());
                    158:     }
                    159: }
                    160: 
                    161: ##########################################################
                    162: ##########################################################
                    163: ##
                    164: ## SurveyProblemSelector
                    165: ##
                    166: ##########################################################
                    167: ##########################################################
                    168: sub SurveyProblemSelector {
                    169:     my $Str = '';
                    170:     my @SurveyProblems;
1.3       matthew   171:     my ($navmap,@sequences) = 
                    172:         &Apache::lonstatistics::selected_sequences_with_assessments('all');
                    173:     foreach my $seq (@sequences) {
                    174:         my @resources = &Apache::lonstathelpers::get_resources($navmap,$seq);
                    175:         foreach my $res (@resources) {
                    176:             foreach my $part (@{$res->parts}) {
                    177:                 if ($res->is_survey($part)) {
1.1       matthew   178:                     push(@SurveyProblems,{res=>$res,seq=>$seq,part=>$part});
                    179:                     last;
                    180:                 }
                    181:             }
                    182:         }
                    183:     }
                    184:     if (! scalar(@SurveyProblems)) {
                    185:         $Str = '<h1>'.
                    186:             &mt('There are no survey problems in this course').
                    187:             '</h1>'.$/;
                    188:         return $Str;
                    189:     }
                    190:     $Str .= '<table>'.$/;
                    191:     $Str .= '<tr>'.'<td></td>'.
1.3       matthew   192:         '<th>'.&mt('Survey').'</th>'.
1.1       matthew   193:         '</tr>'.$/;
1.3       matthew   194:     my $id;
1.1       matthew   195:     foreach my $problem (@SurveyProblems) {
1.3       matthew   196:         $id++;
1.1       matthew   197:         my $value = &Apache::lonstathelpers::make_target_id
1.3       matthew   198:             ({symb=>$problem->{'res'}->symb,
1.1       matthew   199:               part=>$problem->{'part'},
                    200:               respid=>undef,
                    201:               resptype=>undef});
                    202:         my $checked = '';
1.8       albertel  203:         if ($env{'form.problemchoice'} eq $value) {
1.1       matthew   204:             $checked = 'checked ';
                    205:         }
1.3       matthew   206:         my $link = $problem->{'res'}->src.
1.13      www       207:             '?symb='.&escape($problem->{'res'}->symb);
1.3       matthew   208:         $Str .= '<tr><td>'.
                    209:             '<input type="radio" name="problemchoice" id="'.$id.'" '.
1.1       matthew   210:                    'value="'.$value.'" '.$checked.'/>'.'</td>'.
1.3       matthew   211:             '<td><nobr>'.
1.10      albertel  212:             '<label for="'.$id.'">'.$problem->{'res'}->compTitle.'('.$problem->{'seq'}->compTitle.')'.'</label>'.
1.3       matthew   213:             ('&nbsp;'x2).
                    214:             qq{<a target="preview" href="$link">view</a>}.'</td></tr>'.$/;
1.1       matthew   215:     }
                    216:     $Str .= '</table>';
                    217:     return $Str;
                    218: }
                    219: 
                    220: #########################################################
                    221: #########################################################
                    222: ##
                    223: ## Compile Student Answers
                    224: ##
                    225: #########################################################
                    226: #########################################################
1.7       matthew   227: sub compile_student_answers {
                    228:     my ($r,$problem,$ProblemData,$Students) = @_;
1.1       matthew   229:     my $resource = $problem->{'resource'};
1.7       matthew   230:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                    231:         ($r,'Processing Student Submissions',
                    232:          'Processing Student Submissions', 
                    233:          scalar(@$Students),'inline',undef,'Statistics','stats_status');
1.1       matthew   234:     foreach my $student (@$Students) {
1.3       matthew   235:         foreach my $partid (@{$resource->parts}) {
                    236:             my @response_ids   = $resource->responseIds($partid);
                    237:             my @response_types = $resource->responseType($partid);
                    238:             for (my $i=0;$i<=$#response_ids;$i++) {
                    239:                 my $respid = $response_ids[$i];
                    240:                 my $resptype = $response_types[$i];
1.1       matthew   241:                 my $results = 
                    242:                     &Apache::loncoursedata::get_response_data_by_student
1.3       matthew   243:                     ($student,$resource->symb,$respid);
1.1       matthew   244:                 next if (! defined($results) || ref($results) ne 'ARRAY' || 
                    245:                          ref($results->[0]) ne 'ARRAY');
                    246:                 my $student_response = 
                    247:                     $results->[0]->[&Apache::loncoursedata::RDs_submission()];
                    248:                 $problem->{'responsedata'}->{$partid}->{$respid}->{'_count'}++;
                    249:                 my $data = $problem->{'responsedata'}->{$partid}->{$respid};
1.3       matthew   250:                 if ($resptype =~ /^(option|match)$/) {
                    251:                     my @responses = split('&',$student_response);
                    252:                     foreach my $response (@responses) {
                    253:                         my ($foilid,$option) = 
                    254:                             map { 
1.13      www       255:                                 &unescape($_); 
1.3       matthew   256:                             } split('=',$response);
                    257:                         $data->{'foil_count'}->{$foilid}++;
                    258:                         $data->{'foil_responses'}->{$foilid}->{$option}++;
                    259:                     }
                    260:                 } elsif ($resptype =~ /^(radiobutton)$/) {
1.13      www       261:                     my ($foil,$value) = map { &unescape($_); } split('=',$student_response);
1.1       matthew   262:                     $value += 1;  # explicitly increment it...
                    263:                     $data->{'foil_responses'}->{$foil}++;
                    264:                     $data->{'foil_values'}->{$value}++;
                    265:                     if (! exists($data->{'map'}->{$value})) {
                    266:                         $data->{'map'}->{$value} = $foil;
1.15    ! albertel  267:                         $data->{'map_fv'}->{$foil} = $value;
1.1       matthew   268:                     }
                    269:                 } else {
                    270:                     # Variable stuff (essays, raw numbers, strings) go here
                    271:                     push(@{$data->{'responses'}},$student_response);
                    272:                 }
                    273:             }
                    274:         }
1.7       matthew   275:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                    276:                                                  'last student');
                    277:     }
                    278:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    279:     return;
                    280: }
                    281: 
                    282: 
                    283: #########################################################
                    284: #########################################################
                    285: ##
                    286: ## make_text_report
                    287: ##
                    288: #########################################################
                    289: #########################################################
                    290: sub make_text_report {
                    291:     my ($r,$problem,$problem_data,$students) = @_;
                    292:     my ($file,$filename) = &Apache::loncommon::create_text_file($r,'txt');
                    293:     if (! defined($file)) { return '';}
                    294:     $r->print('<script>'.
                    295:               'window.document.Statistics.stats_status.value="'.
                    296:               &mt('Building text document.').
                    297:               '";</script>');
                    298:     my $resource = $problem->{'resource'};
                    299:     print $file $resource->compTitle.$/;
                    300:     print $file &Apache::lonstatistics::section_and_enrollment_description().
                    301:         ' '.&mt('Generated on [_1]',&Apache::lonlocal::locallocaltime(time)).
                    302:         $/;
                    303:     my $something_has_been_output = 0;
                    304:     foreach my $partid (@{$resource->parts}) {
                    305:         my @response_ids   = $resource->responseIds($partid);
                    306:         my @response_types = $resource->responseType($partid);
                    307:         for (my $i=0;$i<=$#response_ids;$i++) {
                    308:             my $respid   = $response_ids[$i];
                    309:             my $resptype = $response_types[$i];
                    310:             my $data = $problem->{'responsedata'}->{$partid}->{$respid};
                    311:             if (exists($data->{'responses'}) && 
                    312:                 ref($data->{'responses'}) eq 'ARRAY') {
                    313:                 # Essay type response
                    314:                 print $file ('-'x40).$/;
                    315:                 print $file 
                    316:                     $resource->part_display($partid).', '.$respid.':'.$resptype.$/;
                    317:                 foreach my $submission (@{$data->{'responses'}}) {
                    318:                     print $file ('-'x20).$/;
                    319:                     $submission =~ s/(\\r\\n|\\n)/\n/g;
                    320:                     $submission =~ s/\\(\'|\"|\`)/$1/g;
                    321:                     print $file $submission.$/.$/;
                    322:                     $something_has_been_output=1;
                    323:                 }
                    324:             }
                    325:         }
1.1       matthew   326:     }
1.7       matthew   327:     close($file);
                    328:     if($something_has_been_output) {
                    329:         $r->print('<p><a href="'.$filename.'">'.
                    330:                   &mt('Your text file.').
                    331:                   '</a></p>'."\n");
                    332:         $r->print('<script>'.
                    333:                   'window.document.Statistics.stats_status.value="'.
                    334:                &mt('Done compiling text file.  See link below to download.').
                    335:                   '";</script>');
                    336:     } else {
                    337:         $r->print('<h2>'.
                    338:                   &mt("There is no essay or string response data to output for this survey.").'</h2>');
                    339:     }
                    340:     $r->rflush();
1.1       matthew   341:     return;
                    342: }
                    343: 
1.7       matthew   344: 
1.1       matthew   345: #########################################################
                    346: #########################################################
                    347: ##
1.4       matthew   348: ## make_Excel_report
                    349: ##
                    350: #########################################################
                    351: #########################################################
                    352: sub make_Excel_report {
                    353:     my ($r,$problem,$problem_data,$students) = @_;
                    354:     my ($workbook,$filename,$format) = &Apache::loncommon::create_workbook($r);
                    355:     if (! defined($workbook)) { return '';}
                    356:     $r->print('<script>'.
                    357:               'window.document.Statistics.stats_status.value="'.
                    358:               &mt('Building spreadsheet.').
                    359:               '";</script>');
                    360:     my $worksheet  = $workbook->addworksheet('Survey Reports');
                    361:     #
                    362:     my $rows_output=0;
                    363:     $worksheet->write($rows_output++,0,
1.8       albertel  364:                     $env{'course.'.$env{'request.course.id'}.'.description'},
1.4       matthew   365:                       $format->{'h1'});
                    366:     $rows_output++;
                    367:     #
                    368:     my $resource = $problem->{'resource'};
                    369:     $worksheet->write($rows_output++,0,$resource->compTitle,$format->{'h2'});
                    370:     foreach my $partid (@{$resource->parts}) {
                    371:         my @response_ids   = $resource->responseIds($partid);
                    372:         my @response_types = $resource->responseType($partid);
                    373:         for (my $i=0;$i<=$#response_ids;$i++) {
                    374:             my $respid   = $response_ids[$i];
                    375:             my $resptype = $response_types[$i];
                    376:             my $data = $problem->{'responsedata'}->{$partid}->{$respid};
                    377:             my $cols_output=0;
                    378:             $worksheet->write($rows_output,$cols_output++,
                    379:                               $resource->part_display($partid),$format->{'h3'});
                    380:             $worksheet->write($rows_output,$cols_output++,
                    381:                               'Response '.$respid.', '.$resptype,
                    382:                               $format->{'h3'});
                    383:             $rows_output++;
                    384:             if (exists($data->{'responses'}) && 
                    385:                 ref($data->{'responses'}) eq 'ARRAY') {
                    386:                 my $warned_about_size = 0;
                    387:                 foreach my $data (@{$data->{'responses'}}) {
                    388:                     if (length($data) > 255 && ! $warned_about_size) {
                    389:                         $r->print('<p>'.
                    390:                                   &mt('[_1]:[_2] responses to  [_3] may be too long to fit Excel spreadsheet.',
                    391:                                       $resource->compTitle,
                    392:                                       $resource->part_display($partid),
                    393:                                       $respid).
                    394:                                   '</p>');
                    395:                         $r->rflush();
                    396:                         $warned_about_size=1;
                    397:                     }
                    398:                     $worksheet->write($rows_output++,0,$data);
                    399:                 }
                    400:             } elsif (exists($data->{'foil_count'}) && 
                    401:                      exists($data->{'foil_responses'})) {
                    402:                 my $respdata = $problem_data->{$partid.'.'.$respid};
                    403:                 my @rowdata = ('Foil Name','Foil Text','Option',
                    404:                                'Frequency');
                    405:                 $worksheet->write_row($rows_output++,0,
                    406:                                       \@rowdata,$format->{'h4'});
                    407:                 #
1.15    ! albertel  408: 		my @foils = sort(keys(%{$respdata->{'_Foils'}}));
1.4       matthew   409:                 foreach my $foilid (@foils) {
                    410:                     my $foil_count = $data->{'foil_count'}->{$foilid};
                    411:                     my $foiltext = $respdata->{'_Foils'}->{$foilid}->{'text'};
                    412:                     my $foilname = $respdata->{'_Foils'}->{$foilid}->{'name'};
                    413:                     $foiltext = &HTML::Entities::decode($foilname);
                    414:                     my $cols_output=0;
                    415:                     $worksheet->write($rows_output,$cols_output++,$foilname);
                    416:                     $worksheet->write($rows_output,$cols_output++,$foiltext);  
                    417:                     my $option_start_col = $cols_output;
                    418:                     #
                    419:                     foreach my $option (sort(@{$respdata->{'_Options'}})){
                    420:                         $cols_output= $option_start_col;
                    421:                         $worksheet->write($rows_output,$cols_output++,
                    422:                                           $option);
                    423:                         my $count=
                    424:                             $data->{'foil_responses'}->{$foilid}->{$option};
                    425:                         $worksheet->write($rows_output,$cols_output++,$count);
                    426:                         $rows_output++;
                    427:                     }
                    428:                 }
                    429:             } elsif (exists($data->{'_count'})      && 
                    430:                      exists($data->{'foil_values'}) && 
                    431:                      exists($data->{'map'})) {
                    432:                 my $respdata = $problem_data->{$partid.'.'.$respid};
                    433:                 my @rowdata = ('Foil Name','Foil Text','Frequency');
                    434:                 $worksheet->write_row($rows_output++,0,
                    435:                                       \@rowdata,$format->{'h4'});
1.15    ! albertel  436: 		my @foils = sort(keys(%{$respdata->{'_Foils'}}));
        !           437:                 foreach my $foilid (@foils) {
1.4       matthew   438:                     undef(@rowdata);
1.15    ! albertel  439:                     my $value = $data->{'map_fv'}->{$foilid};
1.4       matthew   440:                     push(@rowdata,$respdata->{'_Foils'}->{$foilid}->{'name'});
                    441:                     push(@rowdata,$respdata->{'_Foils'}->{$foilid}->{'text'});
                    442:                     push(@rowdata,$data->{'foil_values'}->{$value});
                    443:                     $worksheet->write_row($rows_output++,0,\@rowdata);
                    444:                 }
                    445:             }  
                    446:             $rows_output++;
                    447:         }    #response ids
                    448:     }    # partids
                    449:     $workbook->close();
                    450:     $r->print('<p><a href="'.$filename.'">'.
                    451:               &mt('Your Excel spreadsheet.').
                    452:               '</a></p>'."\n");
                    453:     $r->print('<script>'.
                    454:               'window.document.Statistics.stats_status.value="'.
                    455:               &mt('Done compiling spreadsheet.  See link below to download.').
                    456:               '";</script>');
                    457:     $r->rflush();
                    458:     return;
                    459: }
                    460: 
                    461: #########################################################
                    462: #########################################################
                    463: ##
1.1       matthew   464: ## make_HTML_report
                    465: ##
                    466: #########################################################
                    467: #########################################################
                    468: sub make_HTML_report {
                    469:     my ($r,$problem,$ProblemData,$Students) = @_;
                    470:     my $resource = $problem->{'resource'};
1.3       matthew   471:     foreach my $partid (@{$resource->parts}) {
                    472:         my @response_ids   = $resource->responseIds($partid);
                    473:         my @response_types = $resource->responseType($partid);
                    474:         for (my $i=0;$i<=$#response_ids;$i++) {
1.1       matthew   475:             my $Str = '<table>'.$/;
1.3       matthew   476:             my $respid   = $response_ids[$i];
                    477:             my $resptype = $response_types[$i];
1.1       matthew   478:             my $data = $problem->{'responsedata'}->{$partid}->{$respid};
1.3       matthew   479:             if (! defined($data) || ref($data) ne 'HASH') {
                    480:                 next;
                    481:             }
1.1       matthew   482:             # Debugging code
                    483: #            $Str .= '<tr>'.
                    484: #                '<td>'.$partid.'</td>'.
                    485: #                '<td>'.$respid.'</td>'.
                    486: #                '<td>'.$resptype.'</td>'.
                    487: #                '</tr>'.$/;
                    488:             $Str .= '<tr>'.
                    489:                 '<td><b>'.&mt('Total').'</b></td>'.
                    490:                 '<td>'.$data->{'_count'}.'</td>'.
1.9       albertel  491:                 '<td>'.&mt('Part [_1], Response [_2]',
                    492: 			   $resource->part_display($partid),$respid).'</td>'.
1.3       matthew   493:                 '</tr>';
1.1       matthew   494:             if (exists($data->{'responses'}) && 
                    495:                 ref($data->{'responses'}) eq 'ARRAY') {
                    496:                 &randomize_array($data->{'responses'});
                    497:                 foreach my $response (@{$data->{'responses'}}) {
                    498:                     $response =~ s/\\r\\n/\n/g;
                    499:                     $response =~ s/\\'/'/g;
                    500:                     $response =~ s/\\"/"/g;
                    501:                     $Str .= '<tr>'.
                    502:                         '<td colspan="3"><pre>'.
                    503:                         &HTML::Entities::encode($response,'<>&').
                    504:                         '</pre><hr /></td>'.
                    505:                         '</tr>'.$/;
                    506:                 }
1.3       matthew   507:             } elsif (exists($data->{'foil_count'}) && 
                    508:                      exists($data->{'foil_responses'})) {
                    509:                 $Str.='<tr><td colspan="3">'.
                    510:                     '<table><tr>';
                    511:                 my $tmp = '<th>'.join('</th><th>',
                    512:                                       (&mt('Foil Name'),
                    513:                                        &mt('Foil Text'),
                    514:                                        &mt('Option'),
                    515:                                        &mt('Frequency'),
                    516:                                        &mt('Percent'))).'</th></tr>';
1.15    ! albertel  517: 		my @foils = sort(keys(%{$ProblemData->{$partid.'.'.$respid}->{'_Foils'}}));
1.3       matthew   518:                 foreach my $foilid (@foils) {
                    519:                     my $prob_data = $ProblemData->{$partid.'.'.$respid};
                    520:                     my $foil_count = $data->{'foil_count'}->{$foilid};
                    521:                     my $foiltext = $prob_data->{'_Foils'}->{$foilid}->{'text'};
                    522:                     my $foilname = $prob_data->{'_Foils'}->{$foilid}->{'name'};
                    523:                     my $rowspan = scalar(@{$prob_data->{'_Options'}});
                    524:                     my $preamble = '<tr>'.
                    525:                         '<td valign="top" rowspan="'.$rowspan.'">'.
                    526:                             $foilname.'</td>'.
                    527:                         '<td valign="top" rowspan="'.$rowspan.'">'.
                    528:                             $foiltext.'</td>';
                    529:                     foreach my $option (sort(@{$prob_data->{'_Options'}})){
                    530:                         my $count = 
                    531:                             $data->{'foil_responses'}->{$foilid}->{$option};
                    532:                         $tmp .= $preamble.
                    533:                             '<td>'.$option.'</td>'.
                    534:                             '<td align="right">'.$count.'</td>'.
                    535:                             '<td align="right">'.
                    536:                                 sprintf('%.2f',100*$count/$foil_count).'%'.
                    537:                             '</td></tr>'.$/;
                    538:                         $preamble = '<tr>';
                    539:                     }
                    540:                 }
                    541:                 $Str.=$tmp.'</table></td></tr>';
1.1       matthew   542:             } elsif (exists($data->{'_count'})      && 
                    543:                      exists($data->{'foil_values'}) && 
                    544:                      exists($data->{'map'})) {
                    545:                 # This is an option or radiobutton survey response
                    546:                 my $total = $data->{'_count'};
                    547:                 my $sum = 0;
                    548:                 my $tmp;
1.15    ! albertel  549: 		my @foils = sort(keys(%{$ProblemData->{$partid.'.'.$respid}
        !           550: 					            ->{'_Foils'}}));
        !           551:                 foreach my $foilid (@foils) {
        !           552:                     my $value = $data->{'map_fv'}->{$foilid};
1.1       matthew   553:                     my $count = $data->{'foil_values'}->{$value};
                    554:                     my $foiltext = $ProblemData->{$partid.'.'.$respid}->{'_Foils'}->{$foilid}->{'text'};
                    555:                     my $foilname = $ProblemData->{$partid.'.'.$respid}->{'_Foils'}->{$foilid}->{'name'};
                    556:                     $tmp .= '<tr>'.
                    557:                         '<td>'.$foilname.'</td>'.
                    558:                         '<td>'.$foiltext.'</td>'.
                    559:                         '<td align="right">'.$count.'</td>'.
                    560:                         '<td align="right">'.
                    561:                             sprintf("%.2f",$count/$total*100).'%</td>'.
                    562:                         '</tr>'.$/;
                    563:                 }
                    564:                 $Str .= '<tr>'.
                    565:                     '<th>'.&mt('Foil Name').'</th>'.
                    566:                     '<th>'.&mt('Text').'</th>'.
                    567:                     '<th>'.&mt('Freq').'</th>'.
                    568:                     '<th>'.&mt('Percent').'</th>'.
                    569:                     '</tr>'.$/.
                    570:                     $tmp;
                    571:             }
                    572:             $Str.= '</table><hr />';
                    573:             $r->print($Str);
                    574:             $r->rflush();
                    575:         }
                    576:     }
                    577:     return;
                    578: }
                    579: 
                    580: sub randomize_array {
                    581:     # Fisher Yates shuffle, lifted from p 121 of "The Perl Cookbook"
                    582:     my ($array) = @_;
                    583:     for (my $i=scalar(@$array);--$i;) {
                    584:         my $j = int(rand($i+1));
                    585:         next if ($i == $j);
                    586:         @$array[$i,$j]=@$array[$j,$i];
                    587:     }
                    588: }
                    589: 
                    590: #########################################################
                    591: #########################################################
                    592: ##
                    593: ##   Generic Interface Routines
                    594: ##
                    595: #########################################################
                    596: #########################################################
                    597: sub CreateInterface {
                    598:     ##
                    599:     ## Environment variable initialization
                    600:     my $Str = '';
1.4       matthew   601:     my $output_selector = '<select name="output" size="5">'.$/;
1.8       albertel  602:     if (! exists($env{'form.output'})) {
                    603:         $env{'form.output'} = 'HTML';
1.4       matthew   604:     }
                    605:     foreach my $output_format ( {name=>'HTML',text=>&mt("HTML") },
1.7       matthew   606:                                 {name=>'Excel',text=>&mt("Excel") },
                    607:                                 {name=>'TXT',text=>&mt("Text (essays only)") },
                    608:                                 ) {
1.4       matthew   609:         $output_selector.='<option value="'.$output_format->{'name'}.'"';
1.8       albertel  610:         if ($env{'form.output'} eq $output_format->{'name'}) {
1.4       matthew   611:             $output_selector.=' selected';
                    612:         }
                    613:         $output_selector.= '>'.$output_format->{'text'}.'</option>'.$/;
                    614:     }
                    615:     $output_selector .= '</select>'.$/;
1.11      albertel  616:     $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
1.1       matthew   617:     $Str .= '<p>';
                    618:     $Str .= '<table cellspacing="5">'."\n";
                    619:     $Str .= '<tr>';
1.6       matthew   620:     $Str .= '<th>'.&mt('Sections').'</th>';
1.12      raeburn   621:     $Str .= '<th>'.&mt('Groups').'</th>';
1.14      raeburn   622:     $Str .= '<th>'.&mt('Access Status').'</th>';
1.6       matthew   623:     $Str .= '<th>'.&mt('Output Format').'</th>';
                    624:     $Str .= '<th>'.'&nbsp;'.'</th>';
1.1       matthew   625:     $Str .= '</tr>'."\n";
                    626:     #
                    627:     $Str .= '<tr><td align="center">'."\n";
                    628:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
                    629:     $Str .= '</td>';
                    630:     #
1.12      raeburn   631:     $Str .= '<td align="center">'."\n";
                    632:     $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
                    633:     $Str .= '</td>';
                    634:     #
1.1       matthew   635:     $Str .= '<td align="center">';
                    636:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
                    637:     $Str .= '</td>';
                    638:     #
1.4       matthew   639:     $Str .= '<td align="center">'.$output_selector.'</td>';
                    640:     #
1.6       matthew   641:     # Render problem checkbox
                    642:     my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
1.8       albertel  643:     if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
1.6       matthew   644:         $prob_checkbox .= 'checked ';
                    645:     }
                    646:     $prob_checkbox .= 'value="true" />';
                    647:     $Str .= '<td align="right" valign="top">'.
                    648:         '<label><b>'.
                    649:         &mt('Show problem [_1]',$prob_checkbox).'</b></label><br />'.
                    650:         '<label><b>'.
                    651:         '</td>';
                    652:     #
1.1       matthew   653:     $Str .= '</tr>'."\n";
                    654:     $Str .= '</table>'."\n";
                    655:     #
                    656:     $Str .= '<nobr>'.&mt('Status: [_1]',
                    657:                          '<input type="text" '.
                    658:                          'name="stats_status" size="60" value="" />').
                    659:             '</nobr>'.'</p>';    
                    660:     ##
                    661:     return $Str;
                    662: }
                    663: 
                    664: 1;
                    665: 
                    666: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>