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

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

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