File:  [LON-CAPA] / loncom / interface / statistics / lonsurveyreports.pm
Revision 1.22: download - view: text, annotated - select for diffs
Fri Dec 12 05:03:39 2008 UTC (15 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_99_1, version_2_7_99_0, bz5969, bz2851, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox
- Unbalanced HTML tags.
- Eliminate unecessary concatenation.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonsurveyreports.pm,v 1.22 2008/12/12 05:03:39 raeburn Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    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;
   30: use Apache::lonnet;
   31: use Apache::loncommon();
   32: use Apache::lonhtmlcommon();
   33: use Apache::loncoursedata();
   34: use Apache::lonstatistics;
   35: use Apache::lonlocal;
   36: use Apache::lonstathelpers;
   37: use Spreadsheet::WriteExcel;
   38: use HTML::Entities();
   39: use Time::Local();
   40: use lib '/home/httpd/lib/perl/';
   41: use LONCAPA;
   42:  
   43: 
   44: my @SubmitButtons = (
   45:                      { name => 'break'},
   46:                      { name => 'PrevProblem',
   47:                        text => 'Previous Survey' },
   48:                      { name => 'NextProblem',
   49:                        text => 'Next Survey' },
   50:                      { name => 'SelectAnother',
   51:                        text => 'Choose a different Survey' },
   52:                      { name => 'break'},
   53:                      { name => 'Generate',
   54:                        text => 'Generate Report'},
   55:                      );
   56: 
   57: sub BuildSurveyReportsPage {
   58:     my ($r,$c)=@_;
   59:     #
   60:     my %Saveable_Parameters = ('Status' => 'scalar',
   61:                                'Section' => 'array',
   62:                                'NumPlots' => 'scalar',
   63:                                );
   64:     &Apache::loncommon::store_course_settings('survey_reports',
   65:                                               \%Saveable_Parameters);
   66:     &Apache::loncommon::restore_course_settings('survey_resports',
   67:                                                 \%Saveable_Parameters);
   68:     #
   69:     &Apache::lonstatistics::PrepareClasslist();
   70:     #
   71:     $r->print(&CreateInterface());
   72:     #
   73:     my @Students = @Apache::lonstatistics::Students;
   74:     #
   75:     if (@Students < 1) {
   76:         $r->print('<p class="LC_warning">'.&mt('There are no students in the sections selected.').'</p>');
   77:     }
   78:     #
   79:     my @CacheButtonHTML = 
   80:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
   81:     $r->rflush();
   82:     #
   83:     if (exists($env{'form.problemchoice'}) && 
   84:         ! exists($env{'form.SelectAnother'})) {
   85:         $r->print('&nbsp;'x3);
   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 />');
  100:         $r->print('<h4>'.
  101:                   &Apache::lonlocal::locallocaltime(time).','.
  102:                   &Apache::lonstatistics::section_and_enrollment_description().
  103:                   '</h4>');
  104:         $r->rflush();
  105:         #
  106:         # Determine which problem we are to analyze
  107:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
  108:             ($env{'form.problemchoice'});
  109:         #
  110:         my ($navmap,$prev,$curr,$next) = 
  111:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  112:                                                         '.',
  113:                                                         'part_survey',
  114:                                                         );
  115:         if (exists($env{'form.PrevProblem'}) && defined($prev)) {
  116:             $current_problem = $prev;
  117:         } elsif (exists($env{'form.NextProblem'}) && defined($next)) {
  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
  124:         $env{'form.problemchoice'} = 
  125:             &Apache::lonstathelpers::make_target_id($current_problem);
  126:         $r->print('<input type="hidden" name="problemchoice" value="'.
  127:                   $env{'form.problemchoice'}.'" />');
  128:         #
  129:         if (! defined($current_problem->{'resource'})) {
  130:             $r->print('resource is undefined');
  131:         } else {
  132:             my $resource = $current_problem->{'resource'};
  133:             $r->print('<h1>'.$resource->compTitle.'</h1>');
  134:             $r->print('<h3>'.$resource->src.'</h3>');
  135:             if ($env{'form.renderprob'} eq 'true') {
  136:                 $r->print('<hr />'
  137:                          .&Apache::lonstathelpers::render_resource($resource)
  138:                          .'<hr />'
  139:                 );
  140:             }
  141:             $r->rflush();
  142:             my %Data = &Apache::lonstathelpers::get_problem_data
  143:                 ($resource->src);
  144:             &compile_student_answers($r,$current_problem,\%Data,\@Students);
  145:             if ($env{'form.output'} eq 'HTML' || 
  146:                 ! defined($env{'form.output'})) {
  147:                 &make_HTML_report($r,$current_problem,\%Data,\@Students);
  148:             } elsif ($env{'form.output'} eq 'Excel') {
  149:                 &make_Excel_report($r,$current_problem,\%Data,\@Students);
  150:             } elsif ($env{'form.output'} eq 'TXT') {
  151:                 &make_text_report($r,$current_problem,\%Data,\@Students);
  152:             }
  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;
  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)) {
  181:                     push(@SurveyProblems,{res=>$res,seq=>$seq,part=>$part});
  182:                     last;
  183:                 }
  184:             }
  185:         }
  186:     }
  187:     if (! scalar(@SurveyProblems)) {
  188:         $Str = '<p class="LC_warning">'.
  189:             &mt('There are no survey problems in this course.').
  190:             '</p>'.$/;
  191:         return $Str;
  192:     }
  193:     $Str .= '<table>'.$/;
  194:     $Str .= '<tr>'.'<td></td>'.
  195:         '<th>'.&mt('Survey').'</th>'.
  196:         '</tr>'.$/;
  197:     my $id;
  198:     foreach my $problem (@SurveyProblems) {
  199:         $id++;
  200:         my $value = &Apache::lonstathelpers::make_target_id
  201:             ({symb=>$problem->{'res'}->symb,
  202:               part=>$problem->{'part'},
  203:               respid=>undef,
  204:               resptype=>undef});
  205:         my $checked = '';
  206:         if ($env{'form.problemchoice'} eq $value) {
  207:             $checked = 'checked="checked" ';
  208:         }
  209:         my $link = $problem->{'res'}->link.
  210:             '?symb='.&escape($problem->{'res'}->shown_symb);
  211:         $Str .= '<tr><td>'.
  212:             '<input type="radio" name="problemchoice" id="'.$id.'" '.
  213:                    'value="'.$value.'" '.$checked.'/>'.'</td>'.
  214:             '<td><span class="LC_nobreak">'.
  215:             '<label for="'.$id.'">'.$problem->{'res'}->compTitle.' ('.$problem->{'seq'}->compTitle.')'.'</label>'.
  216:             ('&nbsp;'x2).
  217:             '<a target="preview" href="'.$link.'">'.&mt('View survey').'</a></span></td></tr>'.$/;
  218:     }
  219:     $Str .= '</table>';
  220:     return $Str;
  221: }
  222: 
  223: #########################################################
  224: #########################################################
  225: ##
  226: ## Compile Student Answers
  227: ##
  228: #########################################################
  229: #########################################################
  230: sub compile_student_answers {
  231:     my ($r,$problem,$ProblemData,$Students) = @_;
  232:     my $resource = $problem->{'resource'};
  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');
  237:     foreach my $student (@$Students) {
  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];
  244:                 my $results = 
  245:                     &Apache::loncoursedata::get_response_data_by_student
  246:                     ($student,$resource->symb,$respid);
  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};
  253:                 if ($resptype =~ /^(option|match)$/) {
  254:                     my @responses = split('&',$student_response);
  255:                     foreach my $response (@responses) {
  256:                         my ($foilid,$option) = 
  257:                             map { 
  258:                                 &unescape($_); 
  259:                             } split('=',$response);
  260:                         $data->{'foil_count'}->{$foilid}++;
  261:                         $data->{'foil_responses'}->{$foilid}->{$option}++;
  262:                     }
  263:                 } elsif ($resptype =~ /^(radiobutton)$/) {
  264:                     my ($foil,$value) = map { &unescape($_); } split('=',$student_response);
  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;
  270:                         $data->{'map_fv'}->{$foil} = $value;
  271:                     }
  272:                 } else {
  273:                     # Variable stuff (essays, raw numbers, strings) go here
  274:                     push(@{$data->{'responses'}},$student_response);
  275:                 }
  276:             }
  277:         }
  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:         }
  329:     }
  330:     close($file);
  331:     if($something_has_been_output) {
  332:         $r->print('<p class="LC_info"><a href="'.$filename.'">'.
  333:                   &mt('Your text file').
  334:                   '</a></p>'."\n");
  335:         $r->print('<script>'.
  336:                   'window.document.Statistics.stats_status.value="'.
  337:                &mt('Done compiling text file. See link below to download.').
  338:                   '";</script>');
  339:     } else {
  340:         $r->print('<p class="LC_warning">'
  341:                  .&mt('There is no essay or string response data to output for this survey.')
  342:                  .'</p>');
  343:     }
  344:     $r->rflush();
  345:     return;
  346: }
  347: 
  348: 
  349: #########################################################
  350: #########################################################
  351: ##
  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,
  368:                     $env{'course.'.$env{'request.course.id'}.'.description'},
  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) {
  393:                         $r->print('<p class="LC_warning">'.
  394:                                   &mt('[_1]:[_2] responses to [_3] may be too long to fit Excel spreadsheet.',
  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:                 #
  412: 		my @foils = sort(keys(%{$respdata->{'_Foils'}}));
  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'});
  440: 		my @foils = sort(keys(%{$respdata->{'_Foils'}}));
  441:                 foreach my $foilid (@foils) {
  442:                     undef(@rowdata);
  443:                     my $value = $data->{'map_fv'}->{$foilid};
  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();
  454:     $r->print('<p class="LC_info"><a href="'.$filename.'">'.
  455:               &mt('Your Excel spreadsheet.').
  456:               '</a></p>'."\n");
  457:     $r->print('<script>'.
  458:               'window.document.Statistics.stats_status.value="'.
  459:               &mt('Done compiling spreadsheet. See link below to download.').
  460:               '";</script>');
  461:     $r->rflush();
  462:     return;
  463: }
  464: 
  465: #########################################################
  466: #########################################################
  467: ##
  468: ## make_HTML_report
  469: ##
  470: #########################################################
  471: #########################################################
  472: sub make_HTML_report {
  473:     my ($r,$problem,$ProblemData,$Students) = @_;
  474:     my $resource = $problem->{'resource'};
  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++) {
  479:             my $Str = '<table>'.$/;
  480:             my $respid   = $response_ids[$i];
  481:             my $resptype = $response_types[$i];
  482:             my $data = $problem->{'responsedata'}->{$partid}->{$respid};
  483:             if (! defined($data) || ref($data) ne 'HASH') {
  484:                 next;
  485:             }
  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>'.
  495:                 '<td>'.&mt('Part [_1], Response [_2]',
  496: 			   $resource->part_display($partid),$respid).'</td>'.
  497:                 '</tr>';
  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:                 }
  511:             } elsif (exists($data->{'foil_count'}) && 
  512:                      exists($data->{'foil_responses'})) {
  513:                 $Str.='<tr>'
  514:                     .'<td colspan="3">'
  515:                     .&Apache::loncommon::start_data_table()
  516:                     .&Apache::loncommon::start_data_table_header_row();
  517:                 my $tmp = '<th>'.join('</th><th>',
  518:                                       (&mt('Foil Name'),
  519:                                        &mt('Foil Text'),
  520:                                        &mt('Option'),
  521:                                        &mt('Frequency'),
  522:                                        &mt('Percent'))).'</th>'
  523:                           .&Apache::loncommon::end_data_table_header_row();
  524: 		my @foils = sort(keys(%{$ProblemData->{$partid.'.'.$respid}->{'_Foils'}}));
  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'}});
  531:                     my $preamble = &Apache::loncommon::start_data_table_row().
  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).'%'.
  544:                             '</td>'.&Apache::loncommon::end_data_table_row().$/;
  545:                         $preamble = &Apache::loncommon::continue_data_table_row(); #&Apache::loncommon::start_data_table_row();
  546:                     }
  547:                 }
  548:                 $Str.=$tmp.&Apache::loncommon::end_data_table()
  549:                      .'</td></tr>';
  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;
  557: 		my @foils = sort(keys(%{$ProblemData->{$partid.'.'.$respid}
  558: 					            ->{'_Foils'}}));
  559:                 foreach my $foilid (@foils) {
  560:                     my $value = $data->{'map_fv'}->{$foilid};
  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'};
  564:                     $tmp .= &Apache::loncommon::start_data_table_row().
  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>'.
  570:                         &Apache::loncommon::end_data_table_row().$/;
  571:                 }
  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>';
  584:             }
  585:             $Str.= '</table>';
  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 = '';
  614:     my $output_selector = '<select name="output" size="5">'.$/;
  615:     if (! exists($env{'form.output'})) {
  616:         $env{'form.output'} = 'HTML';
  617:     }
  618:     foreach my $output_format ( {name=>'HTML',text=>&mt("HTML") },
  619:                                 {name=>'Excel',text=>&mt("Excel") },
  620:                                 {name=>'TXT',text=>&mt("Text (essays only)") },
  621:                                 ) {
  622:         $output_selector.='<option value="'.$output_format->{'name'}.'"';
  623:         if ($env{'form.output'} eq $output_format->{'name'}) {
  624:             $output_selector.=' selected="selected"';
  625:         }
  626:         $output_selector.= '>'.$output_format->{'text'}.'</option>'.$/;
  627:     }
  628:     $output_selector .= '</select>'.$/;
  629:     $Str .= &Apache::lonhtmlcommon::breadcrumbs('Student Submission Reports');
  630:     $Str .= '<p>';
  631:     $Str .= &Apache::loncommon::start_data_table();
  632:     $Str .= &Apache::loncommon::start_data_table_header_row();
  633:     $Str .= '<th>'.&mt('Sections').'</th>';
  634:     $Str .= '<th>'.&mt('Groups').'</th>';
  635:     $Str .= '<th>'.&mt('Access Status').'</th>';
  636:     $Str .= '<th>'.&mt('Output Format').'</th>';
  637:     $Str .= '<th>'.&mt('Options').'</th>';
  638:     $Str .= &Apache::loncommon::end_data_table_header_row();
  639:     #
  640:     $Str .= &Apache::loncommon::start_data_table_row();
  641:     $Str .= '<td align="center">'."\n";
  642:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  643:     $Str .= '</td>';
  644:     #
  645:     $Str .= '<td align="center">'."\n";
  646:     $Str .= &Apache::lonstatistics::GroupSelect('Group','multiple',5);
  647:     $Str .= '</td>';
  648:     #
  649:     $Str .= '<td align="center">';
  650:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  651:     $Str .= '</td>';
  652:     #
  653:     $Str .= '<td align="center">'.$output_selector.'</td>';
  654:     #
  655:     # Render problem checkbox
  656:     my $prob_checkbox = '<input type="checkbox" name="renderprob" ';
  657:     if (exists($env{'form.renderprob'}) && $env{'form.renderprob'} eq 'true') {
  658:         $prob_checkbox .= 'checked="checked" ';
  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:     #
  667:     $Str .= &Apache::loncommon::end_data_table_row();
  668:     $Str .= &Apache::loncommon::end_data_table();
  669:     #
  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>';
  676:     ##
  677:     return $Str;
  678: }
  679: 
  680: 1;
  681: 
  682: __END__

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