File:  [LON-CAPA] / loncom / interface / statistics / lonsurveyreports.pm
Revision 1.2: download - view: text, annotated - select for diffs
Tue Jul 6 15:56:42 2004 UTC (19 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, HEAD
Added output of part and response ids.  Removed bogus 'mean' value from
output in optionresponse case.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonsurveyreports.pm,v 1.2 2004/07/06 15:56:42 matthew 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 HTML::Entities();
   38: use Time::Local();
   39: 
   40: my @SubmitButtons = ({ name => 'PrevProblem',
   41:                        text => 'Previous Survey' },
   42:                      { name => 'NextProblem',
   43:                        text => 'Next Survey' },
   44:                      { name => 'break'},
   45:                      { name => 'SelectAnother',
   46:                        text => 'Choose a different Survey Problem' },
   47:                      { name => 'Generate',
   48:                        text => 'Generate Report'},
   49:                      );
   50: 
   51: sub BuildSurveyReportsPage {
   52:     my ($r,$c)=@_;
   53:     #
   54:     my %Saveable_Parameters = ('Status' => 'scalar',
   55:                                'Section' => 'array',
   56:                                'NumPlots' => 'scalar',
   57:                                );
   58:     &Apache::loncommon::store_course_settings('survey_reports',
   59:                                               \%Saveable_Parameters);
   60:     &Apache::loncommon::restore_course_settings('survey_resports',
   61:                                                 \%Saveable_Parameters);
   62:     #
   63:     &Apache::lonstatistics::PrepareClasslist();
   64:     #
   65:     $r->print(&CreateInterface());
   66:     #
   67:     my @Students = @Apache::lonstatistics::Students;
   68:     #
   69:     if (@Students < 1) {
   70:         $r->print('<h2>There are no students in the sections selected</h2>');
   71:     }
   72:     #
   73:     my @CacheButtonHTML = 
   74:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
   75:     $r->rflush();
   76:     #
   77:     if (exists($ENV{'form.problemchoice'}) && 
   78:         ! exists($ENV{'form.SelectAnother'})) {
   79:         foreach my $button (@SubmitButtons) {
   80:             if ($button->{'name'} eq 'break') {
   81:                 $r->print("<br />\n");
   82:             } else {
   83:                 $r->print('<input type="submit" name="'.$button->{'name'}.'" '.
   84:                           'value="'.&mt($button->{'text'}).'" />');
   85:                 $r->print('&nbsp;'x5);
   86:             }
   87:         }
   88:         foreach my $html (@CacheButtonHTML) {
   89:             $r->print($html.('&nbsp;'x5));
   90:         }
   91:         #
   92:         $r->print('<hr />');
   93:         $r->rflush();
   94:         #
   95:         # Determine which problem we are to analyze
   96:         my $current_problem = &Apache::lonstathelpers::get_target_from_id
   97:             ($ENV{'form.problemchoice'});
   98:         #
   99:         my ($prev,$curr,$next) = 
  100:             &Apache::lonstathelpers::get_prev_curr_next($current_problem,
  101:                                                         '.',
  102:                                                         'part_survey',
  103:                                                         );
  104:         if (exists($ENV{'form.PrevProblem'}) && defined($prev)) {
  105:             $current_problem = $prev;
  106:         } elsif (exists($ENV{'form.NextProblem'}) && defined($next)) {
  107:             $current_problem = $next;
  108:         } else {
  109:             $current_problem = $curr;
  110:         }
  111:         #
  112:         # Store the current problem choice and send it out in the form
  113:         $ENV{'form.problemchoice'} = 
  114:             &Apache::lonstathelpers::make_target_id($current_problem);
  115:         $r->print('<input type="hidden" name="problemchoice" value="'.
  116:                   $ENV{'form.problemchoice'}.'" />');
  117:         #
  118:         if (! defined($current_problem->{'resource'})) {
  119:             $r->print('resource is undefined');
  120:         } else {
  121:             my $resource = $current_problem->{'resource'};
  122:             $r->print('<h1>'.$resource->{'title'}.'</h1>');
  123:             $r->print('<h3>'.$resource->{'src'}.'</h3>');
  124:             $r->print(&Apache::lonstathelpers::render_resource($resource));
  125:             $r->rflush();
  126:             my %Data = &Apache::lonstathelpers::get_problem_data
  127:                 ($resource->{'src'});
  128:             &make_HTML_report($r,$current_problem,\%Data,\@Students);
  129:         }
  130:         $r->print('<hr />');
  131:     } else {
  132:         $r->print('<input type="submit" name="Generate" value="'.
  133:                   &mt('Generate Survey Report').'" />');
  134:         $r->print('&nbsp;'x5);
  135:         $r->print('<h3>'.&mt('Please select a Survey to analyze').'</h3>');
  136:         $r->print(&SurveyProblemSelector());
  137:     }
  138: }
  139: 
  140: ##########################################################
  141: ##########################################################
  142: ##
  143: ## SurveyProblemSelector
  144: ##
  145: ##########################################################
  146: ##########################################################
  147: sub SurveyProblemSelector {
  148:     my $Str = '';
  149:     my @SurveyProblems;
  150:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess('all')) {
  151:         next if ($seq->{'num_assess'}<1);
  152:         foreach my $res (@{$seq->{'contents'}}) {
  153:             next if ($res->{'type'} ne 'assessment');
  154:             foreach my $part (@{$res->{'parts'}}) {
  155:                 if ($res->{'partdata'}->{$part}->{'Survey'}) {
  156:                     push(@SurveyProblems,{res=>$res,seq=>$seq,part=>$part});
  157:                     last;
  158:                 }
  159:             }
  160:         }
  161:     }
  162:     if (! scalar(@SurveyProblems)) {
  163:         $Str = '<h1>'.
  164:             &mt('There are no survey problems in this course').
  165:             '</h1>'.$/;
  166:         return $Str;
  167:     }
  168:     $Str .= '<table>'.$/;
  169:     $Str .= '<tr>'.'<td></td>'.
  170:         '<th>'.&mt('Sequence').'</th>'.
  171:         '<th>'.&mt('Problem').'</th>'.
  172:         '</tr>'.$/;
  173:     foreach my $problem (@SurveyProblems) {
  174:         my $value = &Apache::lonstathelpers::make_target_id
  175:             ({symb=>$problem->{'res'}->{'symb'},
  176:               part=>$problem->{'part'},
  177:               respid=>undef,
  178:               resptype=>undef});
  179:         my $checked = '';
  180:         if ($ENV{'form.problemchoice'} eq $value) {
  181:             $checked = 'checked ';
  182:         }
  183:         $Str .= '<tr>'.'<td>'.
  184:             '<input type="radio" name="problemchoice" '.
  185:                    'value="'.$value.'" '.$checked.'/>'.'</td>'.
  186:             '<td>'.$problem->{'seq'}->{'title'}.'</td>'.
  187:             '<td>'.$problem->{'res'}->{'title'}.'</td>'.
  188:             '</tr>'.$/;
  189:     }
  190:     $Str .= '</table>';
  191:     return $Str;
  192: }
  193: 
  194: #########################################################
  195: #########################################################
  196: ##
  197: ## Compile Student Answers
  198: ##
  199: #########################################################
  200: #########################################################
  201: sub Compile_Student_Answers {
  202:     my ($problem,$ProblemData,$Students) = @_;
  203:     my $resource = $problem->{'resource'};
  204:     foreach my $student (@$Students) {
  205:         foreach my $partid (@{$resource->{'parts'}}) {
  206:             my $partdata = $resource->{'partdata'}->{$partid};
  207:             for (my $i=0;$i<=@{$partdata->{'ResponseIds'}};$i++) {
  208:                 my $respid = $partdata->{'ResponseIds'}->[$i];
  209:                 my $resptype = $partdata->{'ResponseTypes'}->[$i];
  210:                 my $results = 
  211:                     &Apache::loncoursedata::get_response_data_by_student
  212:                     ($student,$resource->{'symb'},$respid);
  213:                 next if (! defined($results) || ref($results) ne 'ARRAY' || 
  214:                          ref($results->[0]) ne 'ARRAY');
  215:                 my $student_response = 
  216:                     $results->[0]->[&Apache::loncoursedata::RDs_submission()];
  217:                 $problem->{'responsedata'}->{$partid}->{$respid}->{'_count'}++;
  218:                 my $data = $problem->{'responsedata'}->{$partid}->{$respid};
  219:                 if ($resptype =~ /^(radiobutton|optionresponse)$/) {
  220:                     # Restricted response type can be categorized.
  221:                     #
  222:                     # Assume responses were not randomized and the order 
  223:                     # represents their value.  This is probably a dumb thing
  224:                     # to do...
  225:                     #
  226:                     my ($foil,$value) = split('=',$student_response);
  227:                     $value += 1;  # explicitly increment it...
  228:                     $data->{'foil_responses'}->{$foil}++;
  229:                     $data->{'foil_values'}->{$value}++;
  230:                     if (! exists($data->{'map'}->{$value})) {
  231:                         $data->{'map'}->{$value} = $foil;
  232:                     }
  233:                 } else {
  234:                     # Variable stuff (essays, raw numbers, strings) go here
  235:                     push(@{$data->{'responses'}},$student_response);
  236:                 }
  237:             }
  238:         }
  239:     }
  240:     return;
  241: }
  242: 
  243: #########################################################
  244: #########################################################
  245: ##
  246: ## make_HTML_report
  247: ##
  248: #########################################################
  249: #########################################################
  250: sub make_HTML_report {
  251:     my ($r,$problem,$ProblemData,$Students) = @_;
  252:     &Compile_Student_Answers($problem,$ProblemData,$Students);
  253:     # &output_hash('',$ProblemData);
  254:     my $resource = $problem->{'resource'};
  255:     foreach my $partid (@{$resource->{'parts'}}) {
  256:         my $partdata = $resource->{'partdata'}->{$partid};
  257:         for (my $i=0;$i<=@{$partdata->{'ResponseIds'}};$i++) {
  258:             my $Str = '<table>'.$/;
  259:             my $respid = $partdata->{'ResponseIds'}->[$i];
  260:             my $resptype = $partdata->{'ResponseTypes'}->[$i];
  261:             my $data = $problem->{'responsedata'}->{$partid}->{$respid};
  262:             next if (! defined($data) || ref($data) ne 'HASH');
  263:             # Debugging code
  264: #            $Str .= '<tr>'.
  265: #                '<td>'.$partid.'</td>'.
  266: #                '<td>'.$respid.'</td>'.
  267: #                '<td>'.$resptype.'</td>'.
  268: #                '</tr>'.$/;
  269:             $Str .= '<tr>'.
  270:                 '<td><b>'.&mt('Total').'</b></td>'.
  271:                 '<td>'.$data->{'_count'}.'</td>'.
  272:                 '<td>'.&mt('Part [_1], Response [_2]',$partid,$respid).'</td>'.
  273:                 '</tr>'.$/;
  274:             if (exists($data->{'responses'}) && 
  275:                 ref($data->{'responses'}) eq 'ARRAY') {
  276:                 &randomize_array($data->{'responses'});
  277:                 foreach my $response (@{$data->{'responses'}}) {
  278:                     $response =~ s/\\r\\n/\n/g;
  279:                     $response =~ s/\\'/'/g;
  280:                     $response =~ s/\\"/"/g;
  281:                     $Str .= '<tr>'.
  282:                         '<td colspan="3"><pre>'.
  283:                         &HTML::Entities::encode($response,'<>&').
  284:                         '</pre><hr /></td>'.
  285:                         '</tr>'.$/;
  286:                 }
  287:             } elsif (exists($data->{'_count'})      && 
  288:                      exists($data->{'foil_values'}) && 
  289:                      exists($data->{'map'})) {
  290:                 # This is an option or radiobutton survey response
  291:                 my $total = $data->{'_count'};
  292:                 my $sum = 0;
  293:                 my $tmp;
  294:                 foreach my $value (sort(keys(%{$data->{'foil_values'}}))) {
  295:                     my $count = $data->{'foil_values'}->{$value};
  296:                     my $foilid = $data->{'map'}->{$value};
  297:                     my $foiltext = $ProblemData->{$partid.'.'.$respid}->{'_Foils'}->{$foilid}->{'text'};
  298:                     my $foilname = $ProblemData->{$partid.'.'.$respid}->{'_Foils'}->{$foilid}->{'name'};
  299:                     $sum = $value * $data->{'foil_values'}->{$value};
  300:                     $tmp .= '<tr>'.
  301:                         '<td>'.$foilname.'</td>'.
  302:                         '<td>'.$foiltext.'</td>'.
  303:                         '<td align="right">'.$count.'</td>'.
  304:                         '<td align="right">'.
  305:                             sprintf("%.2f",$count/$total*100).'%</td>'.
  306:                         '</tr>'.$/;
  307:                 }
  308:                 $Str .= '<tr>'.
  309:                     '<th>'.&mt('Foil Name').'</th>'.
  310:                     '<th>'.&mt('Text').'</th>'.
  311:                     '<th>'.&mt('Freq').'</th>'.
  312:                     '<th>'.&mt('Percent').'</th>'.
  313:                     '</tr>'.$/.
  314:                     $tmp;
  315:             }
  316:             $Str.= '</table><hr />';
  317:             $r->print($Str);
  318:             $r->rflush();
  319:         }
  320:     }
  321:     return;
  322: }
  323: 
  324: sub randomize_array {
  325:     # Fisher Yates shuffle, lifted from p 121 of "The Perl Cookbook"
  326:     my ($array) = @_;
  327:     for (my $i=scalar(@$array);--$i;) {
  328:         my $j = int(rand($i+1));
  329:         next if ($i == $j);
  330:         @$array[$i,$j]=@$array[$j,$i];
  331:     }
  332: }
  333: 
  334: #########################################################
  335: #########################################################
  336: ##
  337: ##   Generic Interface Routines
  338: ##
  339: #########################################################
  340: #########################################################
  341: sub CreateInterface {
  342:     ##
  343:     ## Environment variable initialization
  344:     my $Str = '';
  345:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
  346:         (undef,'Student Submission Reports');
  347:     $Str .= '<p>';
  348:     $Str .= '<table cellspacing="5">'."\n";
  349:     $Str .= '<tr>';
  350:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
  351:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
  352:     $Str .= '</tr>'."\n";
  353:     #
  354:     $Str .= '<tr><td align="center">'."\n";
  355:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
  356:     $Str .= '</td>';
  357:     #
  358:     $Str .= '<td align="center">';
  359:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
  360:     $Str .= '</td>';
  361:     #
  362:     $Str .= '</tr>'."\n";
  363:     $Str .= '</table>'."\n";
  364:     #
  365:     $Str .= '<nobr>'.&mt('Status: [_1]',
  366:                          '<input type="text" '.
  367:                          'name="stats_status" size="60" value="" />').
  368:             '</nobr>'.'</p>';    
  369:     ##
  370:     return $Str;
  371: }
  372: 
  373: 
  374: 
  375: 1;
  376: 
  377: __END__

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