File:  [LON-CAPA] / loncom / interface / statistics / lonsurveyreports.pm
Revision 1.25: download - view: text, annotated - select for diffs
Mon Jan 17 00:19:41 2011 UTC (13 years, 4 months ago) by www
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
Add statistics and reports under "grading"

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

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