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

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

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