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

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

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