File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.96: download - view: text, annotated - select for diffs
Thu Feb 12 22:23:30 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
lonstatistics: Removed use of lonhtmlcommon::Title.
lonhtmlcommon: Removed unused statistics subroutines.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstatistics.pm,v 1.96 2004/02/12 22:23:30 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: # (Navigate problems for statistical reports
   28: #
   29: ###
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: lonstatistics
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Main handler for statistics and chart.
   40: 
   41: =over 4
   42: 
   43: =cut
   44: 
   45: package Apache::lonstatistics;
   46: 
   47: use strict;
   48: use Apache::Constants qw(:common :http);
   49: use vars qw(
   50:     @FullClasslist 
   51:     @Students
   52:     @Sections 
   53:     @SelectedSections
   54:     %StudentData
   55:     @StudentDataOrder
   56:     @SelectedStudentData
   57:     $top_map 
   58:     @Sequences 
   59:     @SelectedMaps
   60:     @Assessments
   61:     $enrollment_status);
   62: 
   63: use Apache::lonnet();
   64: use Apache::lonhomework;
   65: use Apache::loncommon;
   66: use Apache::loncoursedata;
   67: use Apache::lonhtmlcommon;
   68: use Apache::lonproblemanalysis();
   69: use Apache::lonsubmissiontimeanalysis();
   70: use Apache::loncorrectproblemplot();
   71: use Apache::lonproblemstatistics();
   72: use Apache::lonstudentassessment();
   73: use Apache::lonpercentage;
   74: use Apache::lonmysql;
   75: use Apache::lonlocal;
   76: use Time::HiRes;
   77: 
   78: #######################################################
   79: #######################################################
   80: 
   81: =pod
   82: 
   83: =item Package Variables
   84: 
   85: =item @FullClasslist The full classlist
   86: 
   87: =item @Students The students we are concerned with for this invocation
   88: 
   89: =item @Sections The sections available in this class
   90: 
   91: =item $curr_student The student currently being examined
   92: 
   93: =item $prev_student The student previous in the classlist
   94: 
   95: =item $next_student The student next in the classlist
   96: 
   97: =over
   98: 
   99: =cut 
  100: 
  101: #######################################################
  102: #######################################################
  103: #
  104: # Classlist variables
  105: #
  106: my $curr_student;
  107: my $prev_student;
  108: my $next_student;
  109: 
  110: #######################################################
  111: #######################################################
  112: 
  113: =pod
  114: 
  115: =item &clear_classlist_variables()
  116: 
  117: undef the following package variables:
  118: 
  119: =over
  120: 
  121: =item @FullClasslist
  122: 
  123: =item @Students
  124: 
  125: =item @Sections
  126: 
  127: =item @SelectedSections
  128: 
  129: =item %StudentData
  130: 
  131: =item @StudentDataOrder
  132: 
  133: =item @SelectedStudentData
  134: 
  135: =item $curr_student
  136: 
  137: =item $prev_student
  138: 
  139: =item $next_student
  140: 
  141: =back
  142: 
  143: =cut
  144: 
  145: #######################################################
  146: #######################################################
  147: sub clear_classlist_variables {
  148:     undef(@FullClasslist);
  149:     undef(@Students);
  150:     undef(@Sections);
  151:     undef(@SelectedSections);
  152:     undef(%StudentData);
  153:     undef(@SelectedStudentData);
  154:     undef($curr_student);
  155:     undef($prev_student);
  156:     undef($next_student);
  157: }
  158: 
  159: #######################################################
  160: #######################################################
  161: 
  162: =pod
  163: 
  164: =item &PrepareClasslist()
  165: 
  166: Build up the classlist information.  The classlist information is kept in
  167: the following package variables:
  168: 
  169: =over
  170: 
  171: =item @FullClasslist
  172: 
  173: =item @Students
  174: 
  175: =item @Sections
  176: 
  177: =item @SelectedSections
  178: 
  179: =item %StudentData
  180: 
  181: =item @SelectedStudentData
  182: 
  183: =item $curr_student
  184: 
  185: =item $prev_student
  186: 
  187: =item $next_student
  188: 
  189: =back
  190: 
  191: $curr_student, $prev_student, and $next_student may not be defined, depending
  192: upon the calling context.
  193: 
  194: =cut
  195: 
  196: #######################################################
  197: #######################################################
  198: sub PrepareClasslist {
  199:     my %Sections;
  200:     &clear_classlist_variables();
  201:     #
  202:     # Retrieve the classlist
  203:     my $cid  = $ENV{'request.course.id'};
  204:     my $cdom = $ENV{'course.'.$cid.'.domain'};
  205:     my $cnum = $ENV{'course.'.$cid.'.num'};
  206:     my ($classlist,$field_names) = &Apache::loncoursedata::get_classlist($cid,
  207:                                                                   $cdom,$cnum);
  208:     if (exists($ENV{'form.Section'})) {
  209:         if (ref($ENV{'form.Section'})) {
  210:             @SelectedSections = @{$ENV{'form.Section'}};
  211:         } elsif ($ENV{'form.Section'} !~ /^\s*$/) {
  212:             @SelectedSections = ($ENV{'form.Section'});
  213:         }
  214:     }
  215:     @SelectedSections = ('all') if (! @SelectedSections);
  216:     foreach (@SelectedSections) {
  217:         if ($_ eq 'all') {
  218:             @SelectedSections = ('all');
  219:         }
  220:     }
  221:     #
  222:     # Deal with instructors with restricted section access
  223:     if ($ENV{'request.course.sec'} !~ /^\s*$/) {
  224:         @SelectedSections = ($ENV{'request.course.sec'});
  225:     }
  226:     #
  227:     # Set up %StudentData
  228:     @StudentDataOrder = qw/fullname username domain id section status/;
  229:     foreach my $field (@StudentDataOrder) {
  230:         $StudentData{$field}->{'title'} = $field;
  231:         $StudentData{$field}->{'base_width'} = length($field);
  232:         $StudentData{$field}->{'width'} = 
  233:                                $StudentData{$field}->{'base_width'};
  234:     }
  235:     #
  236:     # get the status requested
  237:     $enrollment_status = 'Active';
  238:     $enrollment_status = $ENV{'form.Status'} if (exists($ENV{'form.Status'}));
  239:     #
  240:     # Process the classlist
  241:     while (my ($student,$student_data) = each (%$classlist)) {
  242:         my $studenthash = ();
  243:         for (my $i=0; $i< scalar(@$field_names);$i++) {
  244:             my $field = $field_names->[$i];
  245:             # Store the data
  246:             $studenthash->{$field}=$student_data->[$i];
  247:             # Keep track of the width of the fields
  248:             next if (! exists($StudentData{$field}));
  249:             my $length = length($student_data->[$i]);
  250:             if ($StudentData{$field}->{'width'} < $length) {
  251:                 $StudentData{$field}->{'width'} = $length; 
  252:             }
  253:         }
  254:         push (@FullClasslist,$studenthash);
  255:         #
  256:         # Build up a list of sections
  257:         my $section = $studenthash->{'section'};
  258:         if (! defined($section) || $section =~/^\s*$/ || $section == -1) {
  259:             $studenthash->{'section'} = 'none';
  260:             $section = $studenthash->{'section'};
  261:         }
  262:         $Sections{$section}++;
  263:         #
  264:         # Only put in the list those students we are interested in
  265:         foreach my $sect (@SelectedSections) {
  266:             if ( (($sect eq 'all') || 
  267:                   ($section eq $sect)) &&
  268:                  (($studenthash->{'status'} eq $enrollment_status) || 
  269:                   ($enrollment_status eq 'Any')) 
  270:                  ){
  271:                 push (@Students,$studenthash);
  272:                 last;
  273:             }
  274:         }
  275:     }
  276:     #
  277:     # Put the consolidated section data in the right place
  278:     if ($ENV{'request.course.sec'} !~ /^\s*$/) {
  279:         @Sections = ($ENV{'request.course.sec'});
  280:     } else {
  281:         @Sections = sort {$a cmp $b} keys(%Sections);
  282:         unshift(@Sections,'all'); # Put 'all' at the front of the list
  283:     }
  284:     #
  285:     # Sort the Students
  286:     my $sortby = 'fullname';
  287:     $sortby = $ENV{'form.sort'} if (exists($ENV{'form.sort'}));
  288:     my @TmpStudents = sort { $a->{$sortby} cmp $b->{$sortby} ||
  289:                              $a->{'fullname'} cmp $b->{'fullname'} } @Students;
  290:     @Students = @TmpStudents;
  291:     # 
  292:     # Now deal with that current student thing....
  293:     $curr_student = undef;
  294:     if (exists($ENV{'form.SelectedStudent'})) {
  295:         my ($current_uname,$current_dom) = 
  296:             split(':',$ENV{'form.SelectedStudent'});
  297:         my $i;
  298:         for ($i = 0; $i<=$#Students; $i++) {
  299:             next if (($Students[$i]->{'username'} ne $current_uname) || 
  300:                      ($Students[$i]->{'domain'}   ne $current_dom));
  301:             $curr_student = $Students[$i];
  302:             last; # If we get here, we have our student.
  303:         }
  304:         if (defined($curr_student)) {
  305:             if ($i == 0) {
  306:                 $prev_student = undef;
  307:             } else {
  308:                 $prev_student = $Students[$i-1];
  309:             }
  310:             if ($i == $#Students) {
  311:                 $next_student = undef;
  312:             } else {
  313:                 $next_student = $Students[$i+1];
  314:             }
  315:         }
  316:     }
  317:     #
  318:     if (exists($ENV{'form.StudentData'})) {
  319:         if (ref($ENV{'form.StudentData'}) eq 'ARRAY') {
  320:             @SelectedStudentData = @{$ENV{'form.StudentData'}};
  321:         } else {
  322:             @SelectedStudentData = ($ENV{'form.StudentData'});
  323:         }
  324:     } else {
  325:         @SelectedStudentData = ('username');
  326:     }
  327:     foreach (@SelectedStudentData) {
  328:         if ($_ eq 'all') {
  329:             @SelectedStudentData = ('all');
  330:             last;
  331:         }
  332:     }
  333:     #
  334:     return;
  335: }
  336: 
  337: 
  338: #######################################################
  339: #######################################################
  340: 
  341: =pod
  342: 
  343: =item get_students
  344: 
  345: Returns a list of the selected students
  346: 
  347: =cut
  348: 
  349: #######################################################
  350: #######################################################
  351: sub get_students {
  352:     if (! @Students) {
  353:         &PrepareClasslist()
  354:     }
  355:     return @Students;
  356: }
  357: 
  358: #######################################################
  359: #######################################################
  360: 
  361: =pod
  362: 
  363: =item &current_student()
  364: 
  365: Returns a pointer to a hash containing data about the currently
  366: selected student.
  367: 
  368: =cut
  369: 
  370: #######################################################
  371: #######################################################
  372: sub current_student { 
  373:     return $curr_student;
  374: }
  375: 
  376: #######################################################
  377: #######################################################
  378: 
  379: =pod
  380: 
  381: =item &previous_student()
  382: 
  383: Returns a pointer to a hash containing data about the student prior
  384: in the list of students.  Or something.  
  385: 
  386: =cut
  387: 
  388: #######################################################
  389: #######################################################
  390: sub previous_student { 
  391:     return $prev_student;
  392: }
  393: 
  394: #######################################################
  395: #######################################################
  396: 
  397: =pod
  398: 
  399: =item &next_student()
  400: 
  401: Returns a pointer to a hash containing data about the next student
  402: to be viewed.
  403: 
  404: =cut
  405: 
  406: #######################################################
  407: #######################################################
  408: sub next_student { 
  409:     return $next_student;
  410: }
  411: 
  412: #######################################################
  413: #######################################################
  414: 
  415: =pod
  416: 
  417: =item &clear_sequence_variables()
  418: 
  419: =cut
  420: 
  421: #######################################################
  422: #######################################################
  423: sub clear_sequence_variables {
  424:     undef($top_map);
  425:     undef(@Sequences);
  426:     undef(@Assessments);
  427: }
  428: 
  429: #######################################################
  430: #######################################################
  431: 
  432: =pod
  433: 
  434: =item &SetSelectedMaps($elementname)
  435: 
  436: Sets the @SelectedMaps array from $ENV{'form.'.$elementname};
  437: 
  438: =cut
  439: 
  440: #######################################################
  441: #######################################################
  442: sub SetSelectedMaps {
  443:     my $elementname = shift;
  444:     if (exists($ENV{'form.'.$elementname})) {
  445:         if (ref($ENV{'form.'.$elementname})) {
  446:             @SelectedMaps = @{$ENV{'form.'.$elementname}};
  447:         } else {
  448:             @SelectedMaps = ($ENV{'form.'.$elementname});
  449:         }
  450:     } else {
  451:         @SelectedMaps = ('all');
  452:     }
  453: }
  454: 
  455: 
  456: #######################################################
  457: #######################################################
  458: 
  459: =pod
  460: 
  461: =item &Sequences_with_Assess()
  462: 
  463: Returns an array containing the subset of @Sequences which contain
  464: assessments.
  465: 
  466: =cut
  467: 
  468: #######################################################
  469: #######################################################
  470: sub Sequences_with_Assess {
  471:     my @Sequences_to_Show;
  472:     foreach my $map_symb (@SelectedMaps) {
  473:         foreach my $sequence (@Sequences) {
  474:             next if ($sequence->{'symb'} ne $map_symb && $map_symb ne 'all');
  475:             next if ($sequence->{'num_assess'} < 1);
  476:             push (@Sequences_to_Show,$sequence);
  477:         }
  478:     }
  479:     return @Sequences_to_Show;
  480: }
  481: 
  482: #######################################################
  483: #######################################################
  484: 
  485: =pod
  486: 
  487: =item &PrepareCourseData($r)
  488: 
  489: =cut
  490: 
  491: #######################################################
  492: #######################################################
  493: sub PrepareCourseData {
  494:     my ($r) = @_;
  495:     &clear_sequence_variables();
  496:     my ($top,$sequences,$assessments) = 
  497:         &Apache::loncoursedata::get_sequence_assessment_data();
  498:     if (! defined($top) || ! ref($top)) {
  499:         # There has been an error, better report it
  500:         &Apache::lonnet::logthis('top is undefined');
  501:         return;
  502:     }
  503:     $top_map = $top if (ref($top));
  504:     @Sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
  505:     @Assessments = @{$assessments} if (ref($assessments) eq 'ARRAY');
  506:     return;
  507: }
  508: 
  509: #######################################################
  510: #######################################################
  511: 
  512: =pod
  513: 
  514: =item &log_sequence($sequence,$recursive,$padding)
  515: 
  516: Write data about the sequence to a logfile.  If $recursive is not
  517: undef the data is written recursively.  $padding is used for recursive
  518: calls.
  519: 
  520: =cut
  521: 
  522: #######################################################
  523: #######################################################
  524: sub log_sequence {
  525:     my ($seq,$recursive,$padding) = @_;
  526:     $padding = '' if (! defined($padding));
  527:     if (ref($seq) ne 'HASH') {
  528:         &Apache::lonnet::logthis('log_sequence passed bad sequnce');
  529:         return;
  530:     }
  531:     &Apache::lonnet::logthis($padding.'sequence '.$seq->{'title'});
  532:     while (my($key,$value) = each(%$seq)) {
  533:         next if ($key eq 'contents');
  534:         if (ref($value) eq 'ARRAY') {
  535:             for (my $i=0;$i< scalar(@$value);$i++) {
  536:                 &Apache::lonnet::logthis($padding.$key.'['.$i.']='.
  537:                                          $value->[$i]);
  538:             }
  539:         } else {
  540:             &Apache::lonnet::logthis($padding.$key.'='.$value);
  541:         }
  542:     }
  543:     if (defined($recursive)) {
  544:         &Apache::lonnet::logthis($padding.'-'x20);
  545:         &Apache::lonnet::logthis($padding.'contains:');
  546:         foreach my $item (@{$seq->{'contents'}}) {
  547:             if ($item->{'type'} eq 'container') {
  548:                 &log_sequence($item,$recursive,$padding.'    ');
  549:             } else {
  550:                 &Apache::lonnet::logthis($padding.'title = '.$item->{'title'});
  551:                 while (my($key,$value) = each(%$item)) {
  552:                     next if ($key eq 'title');
  553:                     if (ref($value) eq 'ARRAY') {
  554:                         for (my $i=0;$i< scalar(@$value);$i++) {
  555:                             &Apache::lonnet::logthis($padding.$key.'['.$i.']='.
  556:                                                      $value->[$i]);
  557:                         }
  558:                     } else {
  559:                         &Apache::lonnet::logthis($padding.$key.'='.$value);
  560:                     }
  561:                 }
  562:             }
  563:         }
  564:         &Apache::lonnet::logthis($padding.'end contents of '.$seq->{'title'});
  565:         &Apache::lonnet::logthis($padding.'-'x20);
  566:     }
  567:     return;
  568: }
  569: 
  570: ##############################################
  571: ##############################################
  572: 
  573: =pod 
  574: 
  575: =item &StudentDataSelect($elementname,$status,$numvisible,$selected)
  576: 
  577: Returns html for a selection box allowing the user to choose one (or more) 
  578: of the fields of student data available (fullname, username, id, section, etc)
  579: 
  580: =over 4
  581: 
  582: =item $elementname The name of the HTML form element
  583: 
  584: =item $status 'multiple' or 'single' selection box
  585: 
  586: =item $numvisible The number of options to be visible
  587: 
  588: =back
  589: 
  590: =cut
  591: 
  592: ##############################################
  593: ##############################################
  594: sub StudentDataSelect {
  595:     my ($elementname,$status,$numvisible)=@_;
  596:     if ($numvisible < 1) {
  597:         return;
  598:     }
  599:     #
  600:     # Build the form element
  601:     my $Str = "\n";
  602:     $Str .= '<select name="'.$elementname.'" ';
  603:     if ($status ne 'single') {
  604:         $Str .= 'multiple="true" ';
  605:     }
  606:     $Str .= 'size="'.$numvisible.'" >'."\n";
  607:     #
  608:     # Deal with 'all'
  609:     $Str .= '    <option value="all" ';
  610:     foreach (@SelectedStudentData) {
  611:         if ($_ eq 'all') {
  612:             $Str .= 'selected ';
  613:             last;
  614:         }
  615:     }
  616:     $Str .= ">all</option>\n";
  617:     #
  618:     # Loop through the student data fields
  619:     foreach my $item (@StudentDataOrder) {
  620:         $Str .= '    <option value="'.$item.'" ';
  621:         foreach (@SelectedStudentData) {
  622:             if ($item eq $_ ) {
  623:                 $Str .= 'selected ';
  624:                 last;
  625:             }
  626:         }
  627:         $Str .= '>'.$item."</option>\n";
  628:     }
  629:     $Str .= "</select>\n";
  630:     return $Str;
  631: }
  632: 
  633: ##############################################
  634: ##############################################
  635: 
  636: =pod 
  637: 
  638: =item &MapSelect($elementname,$status,$numvisible,$restriction) 
  639: 
  640: Returns html for a selection box allowing the user to choose one (or more) 
  641: of the sequences in the course.  The values of the sequences are the symbs.
  642: If the top sequence is selected, the value 'top' will result.
  643: 
  644: =over 4
  645: 
  646: =item $elementname The name of the HTML form element
  647: 
  648: =item $status 'multiple' or 'single' selection box
  649: 
  650: =item $numvisible The number of options to be visible
  651: 
  652: =item $restriction Code reference to subroutine which returns true or 
  653: false.  The code must expect a reference to a sequence data structure.
  654: 
  655: =back
  656: 
  657: =cut
  658: 
  659: ##############################################
  660: ##############################################
  661: sub MapSelect {
  662:     my ($elementname,$status,$numvisible,$restriction)=@_;
  663:     if ($numvisible < 1) {
  664:         return;
  665:     }
  666:     #
  667:     # Set up array of selected items
  668:     &SetSelectedMaps($elementname);
  669:     #
  670:     # Set up the restriction call
  671:     if (! defined($restriction)) {
  672:         $restriction = sub { 1; };
  673:     }
  674:     #
  675:     # Build the form element
  676:     my $Str = "\n";
  677:     $Str .= '<select name="'.$elementname.'" ';
  678:     if ($status ne 'single') {
  679:         $Str .= 'multiple="true" ';
  680:     }
  681:     $Str .= 'size="'.$numvisible.'" >'."\n";
  682:     #
  683:     # Deal with 'all'
  684:     foreach (@SelectedMaps) {
  685:         if ($_ eq 'all') {
  686:             @SelectedMaps = ('all');
  687:             last;
  688:         }
  689:     }
  690:     #
  691:     # Put in option for 'all'
  692:     $Str .= '    <option value="all" ';
  693:     foreach (@SelectedMaps) {
  694:         if ($_ eq 'all') {
  695:             $Str .= 'selected ';
  696:             last;
  697:         }
  698:     }
  699:     $Str .= ">all</option>\n";
  700:     #
  701:     # Loop through the sequences
  702:     foreach my $seq (@Sequences) {
  703:         next if (! $restriction->($seq));
  704:         $Str .= '    <option value="'.$seq->{'symb'}.'" ';
  705:         foreach (@SelectedMaps) {
  706:             if ($seq->{'symb'} eq $_) {
  707:                 $Str .= 'selected ';
  708:                 last;
  709:             }
  710:         }
  711:         $Str .= '>'.$seq->{'title'}."</option>\n";
  712:     }
  713:     $Str .= "</select>\n";
  714:     return $Str;
  715: }
  716: 
  717: ##############################################
  718: ##############################################
  719: 
  720: =pod 
  721: 
  722: =item &SectionSelect($elementname,$status,$numvisible) 
  723: 
  724: Returns html for a selection box allowing the user to choose one (or more) 
  725: of the sections in the course.  
  726: 
  727: Uses the package variables @Sections and @SelectedSections
  728: =over 4
  729: 
  730: =item $elementname The name of the HTML form element
  731: 
  732: =item $status 'multiple' or 'single' selection box
  733: 
  734: =item $numvisible The number of options to be visible
  735: 
  736: =back
  737: 
  738: =cut
  739: 
  740: ##############################################
  741: ##############################################
  742: sub SectionSelect {
  743:     my ($elementname,$status,$numvisible)=@_;
  744:     if ($numvisible < 1) {
  745:         return;
  746:     }
  747:     #
  748:     # Make sure we have the data we need to continue
  749:     if (! @Sections) {
  750:         &PrepareClasslist()
  751:     }
  752:     #
  753:     # Build the form element
  754:     my $Str = "\n";
  755:     $Str .= '<select name="'.$elementname.'" ';
  756:     if ($status ne 'single') {
  757:         $Str .= 'multiple="true" ';
  758:     }
  759:     $Str .= 'size="'.$numvisible.'" >'."\n";
  760:     #
  761:     # Loop through the sequences
  762:     foreach my $s (@Sections) {
  763:         $Str .= '    <option value="'.$s.'" ';
  764:         foreach (@SelectedSections) {
  765:             if ($s eq $_) {
  766:                 $Str .= 'selected ';
  767:                 last;
  768:             }
  769:         }
  770:         $Str .= '>'.$s."</option>\n";
  771:     }
  772:     $Str .= "</select>\n";
  773:     return $Str;
  774: }
  775: 
  776: #######################################################
  777: #######################################################
  778: 
  779: =pod
  780: 
  781: =item &CreateAndParseOutputSelector()
  782: 
  783: Construct a selection list of options for output and parse output selections.
  784: 
  785: =cut
  786: 
  787: #######################################################
  788: #######################################################
  789: sub OutputDescriptions {
  790:     my (@OutputOptions) = @_;
  791:     my $Str = '';
  792:     $Str .= "<h2>Output Modes</h2>\n";
  793:     $Str .= "<dl>\n";
  794:     foreach my $outputmode (@OutputOptions) {
  795: 	$Str .="    <dt>".$outputmode->{'name'}."</dt>\n";
  796: 	$Str .="        <dd>".$outputmode->{'description'}."</dd>\n";
  797:     }
  798:     $Str .= "</dl>\n";
  799:     return $Str;
  800: }
  801: 
  802: sub CreateAndParseOutputSelector {
  803:     my ($elementname,$default,@OutputOptions) = @_;
  804:     my $output_mode;
  805:     my $show;
  806:     my $Str = '';
  807:     #
  808:     # Format for output options is 'mode, restrictions';
  809:     my $selected = $default;
  810:     if (exists($ENV{'form.'.$elementname})) {
  811:         if (ref($ENV{'form.'.$elementname} eq 'ARRAY')) {
  812:             $selected = $ENV{'form.'.$elementname}->[0];
  813:         } else {
  814:             $selected = $ENV{'form.'.$elementname};
  815:         }
  816:     }
  817:     #
  818:     # Set package variables describing output mode
  819:     $output_mode = 'html';
  820:     $show        = 'all';
  821:     foreach my $option (@OutputOptions) {
  822:         next if ($option->{'value'} ne $selected);
  823:         $output_mode = $option->{'mode'};
  824:         $show        = $option->{'show'};
  825:     }
  826:     #
  827:     # Build the form element
  828:     $Str = qq/<select size="5" name="$elementname">/;
  829:     foreach my $option (@OutputOptions) {
  830:         if (exists($option->{'special'}) && 
  831:             $option->{'special'} =~ /do not show/) {
  832:             next;
  833:         }
  834:         $Str .= "\n".'    <option value="'.$option->{'value'}.'"';
  835:         $Str .= " selected " if ($option->{'value'} eq $selected);
  836:         $Str .= ">".&mt($option->{'name'})."<\/option>";
  837:     }
  838:     $Str .= "\n</select>";
  839:     return ($Str,$output_mode,$show);
  840: }
  841: 
  842: ###############################################
  843: ###############################################
  844: 
  845: =pod 
  846: 
  847: =item &Gather_Student_Data()
  848: 
  849: Ensures all student data is up to date.
  850: 
  851: =cut
  852: 
  853: ###############################################
  854: ###############################################
  855: sub Gather_Student_Data {
  856:     my ($r) = @_;
  857:     my $c = $r->connection();
  858:     #
  859:     &Apache::loncoursedata::clear_internal_caches();
  860:     #
  861:     my @Sequences = &Apache::lonstatistics::Sequences_with_Assess();
  862:     #
  863:     my @Students = @Apache::lonstatistics::Students;
  864:     #
  865:     # Open the progress window
  866:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  867:         ($r,'Statistics Compilation Status',
  868:          'Statistics Compilation Progress', scalar(@Students));
  869:     #
  870:     while (my $student = shift @Students) {
  871:         return if ($c->aborted());
  872:         my ($status,undef) = &Apache::loncoursedata::ensure_current_data
  873:             ($student->{'username'},$student->{'domain'},
  874:              $ENV{'request.course.id'});
  875:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  876:                                                  &mt('last student'));
  877:     }
  878:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  879:     $r->rflush();
  880: }
  881: 
  882: ###############################################
  883: ###############################################
  884: 
  885: =pod 
  886: 
  887: =item &Gather_Full_Student_Data()
  888: 
  889: Ensures all student data is up to date.
  890: 
  891: =cut
  892: 
  893: ###############################################
  894: ###############################################
  895: sub Gather_Full_Student_Data {
  896:     my ($r) = @_;
  897:     my $c = $r->connection();
  898:     #
  899:     &Apache::loncoursedata::clear_internal_caches();
  900:     #
  901:     my @Students = @Apache::lonstatistics::Students;
  902:     #
  903:     # Open the progress window
  904:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  905:         ($r,&mt('Student Data Compilation Status'),
  906:          &mt('Student Data Compilation Progress'), scalar(@Students));
  907:     #
  908:     while (my $student = shift @Students) {
  909:         return if ($c->aborted());
  910:         my ($status,undef) = &Apache::loncoursedata::ensure_current_full_data
  911:             ($student->{'username'},$student->{'domain'},
  912:              $ENV{'request.course.id'});
  913:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  914:                                                  &mt('last student'));
  915:     }
  916:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  917:     $r->rflush();
  918: }
  919: 
  920: ##################################################
  921: ##################################################
  922: sub DisplayClasslist {
  923:     my ($r)=@_;
  924:     #
  925:     my @Fields = ('fullname','username','domain','id','section');
  926:     #
  927:     my $Str='';
  928:     if (! @Students) {
  929:         if ($SelectedSections[0] eq 'all') { 
  930:             if (lc($ENV{'form.Status'}) eq 'any') {
  931:                 $Str .= '<h2>There are no students in the course.</h2>';
  932:             } elsif (lc($ENV{'form.Status'}) eq 'active') {
  933:                 $Str .= '<h2>There are no currently enrolled students in '.
  934:                     'the course.</h2>';
  935:             } elsif (lc($ENV{'form.Status'}) eq 'expired') {
  936:                 $Str .= '<h2>There are no previously enrolled '.
  937:                     'students in the course.</h2>';
  938:             }
  939:         } else { 
  940:             my $sections;
  941:             if (@SelectedSections == 1) {
  942:                 $sections = 'section '.$SelectedSections[0];
  943:             } elsif (@SelectedSections > 2) {
  944:                 $sections = 'sections '.join(', ',@SelectedSections);
  945:                 $sections =~ s/, ([^,])*$/, and $1/;
  946:             } else {
  947:                 $sections = 'sections '.join(' and ',@SelectedSections);
  948:             }
  949:             if (lc($ENV{'form.Status'}) eq 'any') {
  950:                 $Str .= '<h2>There are no students in '.$sections.'.</h2>';
  951:             } elsif (lc($ENV{'form.Status'}) eq 'active') {
  952:                 $Str .= '<h2>There are no currently enrolled students '.
  953:                     'in '.$sections.'.</h2>';
  954:             } elsif (lc($ENV{'form.Status'}) eq 'expired') {
  955:                 $Str .= '<h2>There are no previously enrolled students '.
  956:                     'in '.$sections.'.</h2>';
  957:             }
  958:         }
  959:         $Str.= '<a href="/adm/statistics?reportSelected=student_assessment">'.
  960:             'Return to the chart.</a>';
  961:         $r->print($Str);
  962:         $r->rflush();
  963:         return;
  964:     }
  965: 
  966:     # "Click" is asinine but it is probably not my place to change the world.
  967:     $Str .= '<h2>Click on a students name or username to view their chart</h2>';
  968:     $Str .= '<table border="0"><tr><td bgcolor="#777777">'."\n";
  969:     $Str .= '<table border="0" cellpadding="3"><tr bgcolor="#e6ffff">'."\n";
  970:     foreach my $field (@Fields) {
  971:         $Str .= '<th><a href="/adm/statistics?reportSelected=classlist&sort='.$field.'">'.$field.
  972:             '</a></th>';
  973:     }
  974:     $Str .= '</tr>'."\n";
  975:     #
  976:     my $alternate = 0;
  977:     foreach my $student (@Students) { # @Students is a package variable
  978:         my $sname = $student->{'username'}.':'.$student->{'domain'};
  979:         if($alternate) {
  980:             $Str .= '<tr bgcolor="#ffffe6">';
  981:         } else {
  982:             $Str .= '<tr bgcolor="#ffffc6">';
  983:         }
  984:         $alternate = ($alternate + 1) % 2;
  985:         #
  986:         foreach my $field (@Fields) {
  987:             $Str .= '<td>';
  988:             if ($field eq 'fullname' || $field eq 'username') {
  989:                 $Str .= '<a href="/adm/statistics?reportSelected=';
  990:                 $Str .= &Apache::lonnet::escape('student_assessment');
  991:                 $Str .= '&sort='.&Apache::lonnet::escape($ENV{'form.sort'});
  992:                 $Str .= '&SelectedStudent=';
  993:                 $Str .= &Apache::lonnet::escape($sname).'">';
  994:                 $Str .= $student->{$field}.'&nbsp';
  995:                 $Str .= '</a>';
  996:             } else {
  997:                 $Str .= $student->{$field};
  998:             }
  999:             $Str .= '</td>';
 1000:         }
 1001:         $Str .= "</tr>\n";
 1002:     }
 1003:     $Str .= '</table></td></tr></table>'."\n";
 1004:     #
 1005:     $r->print($Str);
 1006:     $r->rflush();
 1007:     #
 1008:     return;
 1009: }
 1010: 
 1011: ##############################################
 1012: ##############################################
 1013: sub CreateMainMenu {
 1014:     #
 1015:     # Define menu data
 1016:     my @reports = ({ internal_name => 'problem_statistics',
 1017:                      name => &mt('Overall Problem Statistics'),
 1018:                      short_description => 
 1019:     &mt('Student performance statistics on all problems.'),
 1020:                  },
 1021:                    { internal_name => 'problem_analysis',
 1022:                      name => &mt('Detailed Problem Analysis'),
 1023:                      short_description => 
 1024:     &mt('Detailed statistics and graphs of student performance on problems.'),
 1025:                  },
 1026:                    { internal_name => 'submissiontime_analysis',
 1027:                      name => &mt('Submission Time Plots'),
 1028:                      short_description => 
 1029:     &mt('Display and analysis of submission times on assessments.'),
 1030:                  },
 1031:                    { internal_name => 'correct_problems_plot',
 1032:                      name => &mt('Correct Problems Plot'),
 1033:                      short_description => 
 1034:     &mt('Display a histogram of student performance in the course.'),
 1035:                  },
 1036: #                   { internal_name => 'student_assessment',
 1037: #                     name => &mt('Problem Status Chart'),
 1038: #                     short_description => 
 1039: #    &mt('Brief view of each students performance in course.'),
 1040: #                 },
 1041:                    # 'percentage'  => 'Correct-problems Plot',
 1042:                    # 'activitylog' => 'Activity Log',
 1043:                    );
 1044:     #
 1045:     # Create the menu
 1046:     my $Str;
 1047:     $Str .= '<h1>'.&mt('Please select a report to generate').'</h1>';
 1048:     foreach my $reportdata (@reports) {
 1049:         $Str .='    <h3><a href="/adm/statistics?reportSelected='.
 1050:             $reportdata->{'internal_name'}.'" >'.
 1051:             $reportdata->{'name'}."</a></h3>\n";
 1052:         $Str .= '    '.('&nbsp;'x8).$reportdata->{'short_description'}.
 1053:             "\n";
 1054:     }
 1055:     $Str .="</dl>\n";
 1056:     #
 1057:     return $Str;
 1058: }
 1059: 
 1060: ##############################################
 1061: ##############################################
 1062: sub handler {
 1063:     my $r=shift;
 1064:     my $c = $r->connection();
 1065:     #
 1066:     # Check for overloading
 1067:     my $loaderror=&Apache::lonnet::overloaderror($r);
 1068:     if ($loaderror) { return $loaderror; }
 1069:     $loaderror=
 1070:        &Apache::lonnet::overloaderror($r,
 1071:          $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
 1072:     if ($loaderror) { return $loaderror; }
 1073:     #
 1074:     # Check for access
 1075:     if (! &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
 1076:         $ENV{'user.error.msg'}=
 1077:             $r->uri.":vgr:0:0:Cannot view grades for complete course";
 1078:         if (! &Apache::lonnet::allowed('vgr',
 1079:                       $ENV{'request.course.id'}.'/'.$ENV{'request.course.sec'})) {
 1080:             $ENV{'user.error.msg'}=
 1081:                 $r->uri.":vgr:0:0:Cannot view grades with given role";
 1082:             return HTTP_NOT_ACCEPTABLE;
 1083:         }
 1084:     }
 1085:     #
 1086:     # Send the header
 1087:     &Apache::loncommon::no_cache($r);
 1088:     &Apache::loncommon::content_type($r,'text/html');
 1089:     $r->send_http_header;
 1090:     if ($r->header_only) { return OK; }
 1091:     #
 1092:     # Extract form elements from query string
 1093:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1094:                                             ['sort','reportSelected',
 1095:                                              'SelectedStudent']);
 1096:     #
 1097:     # Give the LON-CAPA page header
 1098:     $r->print('<html><head><title>'.
 1099:               &mt('Course Statistics and Charts').
 1100:               "</title></head>\n".
 1101:               &Apache::loncommon::bodytag('Course Statistics and Charts')."\n".
 1102: 	      &Apache::loncommon::help_open_faq(139).
 1103: 	      &Apache::loncommon::help_open_bug('Statistics and Charts'));
 1104:     $r->rflush();
 1105:     # 
 1106:     # Either print out a menu for them or send them to a report
 1107:     if (! exists($ENV{'form.reportSelected'}) || 
 1108:         $ENV{'form.reportSelected'} eq '') {
 1109:         $r->print(&CreateMainMenu());
 1110:     } else {
 1111:     #
 1112:         if (! &Apache::lonmysql::verify_sql_connection()) {
 1113:             my $serveradmin = $r->dir_config('lonAdmEMail');
 1114:             $r->print('<h2><font color="Red">'.
 1115:                       &mt('Unable to connect to database!').
 1116:                       '</font></h2>');
 1117:             $r->print('<p>'.
 1118:                       &mt('Please notify the server administrator ').
 1119:                       '<b>'.$serveradmin.'</b></p>');
 1120:             $r->print('<p>'.
 1121:                       &mt('Course Statistics and Charts cannot be '.
 1122:                           'retrieved until the database is restarted.  '.
 1123:                           'Your data is intact but cannot be displayed '.
 1124:                           'at this time.').'</p>');
 1125:             $r->print('</body></html>');
 1126:             return;
 1127:         }
 1128:         #
 1129:         # Clean out the caches
 1130:         if (exists($ENV{'form.ClearCache'})) {
 1131:             &Apache::loncoursedata::delete_caches($ENV{'requres.course.id'});
 1132:         }
 1133:         #
 1134:         # Begin form output
 1135:         $r->print('<form name="Statistics" ');
 1136:         $r->print('method="post" action="/adm/statistics">');
 1137:         $r->rflush();
 1138:         #
 1139:         my $GoToPage = $ENV{'form.reportSelected'};
 1140:         #
 1141:         # Set up the statistics and chart environment
 1142:         &PrepareCourseData($r);
 1143:         #
 1144:         $r->print('<input type="hidden" name="reportSelected" value="'.
 1145:                   $GoToPage.'">');
 1146:         if($GoToPage eq 'activitylog') {
 1147: #        &Apache::lonproblemstatistics::Activity();
 1148:         } elsif($GoToPage eq 'problem_statistics') {
 1149:             &Apache::lonproblemstatistics::BuildProblemStatisticsPage($r,$c);
 1150:         } elsif($GoToPage eq 'problem_analysis') {
 1151:             &Apache::lonproblemanalysis::BuildProblemAnalysisPage($r,$c);
 1152:         } elsif($GoToPage eq 'submissiontime_analysis') {
 1153:             &Apache::lonsubmissiontimeanalysis::BuildSubmissionTimePage($r,$c);
 1154:         } elsif($GoToPage eq 'correct_problems_plot') {
 1155:             &Apache::loncorrectproblemplot::BuildCorrectProblemsPage($r,$c);
 1156:         } elsif($GoToPage eq 'student_assessment') {
 1157:             &Apache::lonstudentassessment::BuildStudentAssessmentPage($r,$c);
 1158:         } elsif($GoToPage eq 'DoDiffGraph' || $GoToPage eq 'PercentWrongGraph') {
 1159: #        &Apache::lonproblemstatistics::BuildGraphicChart($r,$c);
 1160:         } elsif($GoToPage eq 'Correct-problems Plot') {
 1161:             #	&Apache::lonpercentage::BuildPercentageGraph($r,$c);
 1162:         }
 1163:         #
 1164:         $r->print("</form>\n");
 1165:     }
 1166:     $r->print("</body>\n</html>\n");
 1167:     $r->rflush();
 1168:     #
 1169:     return OK;
 1170: }
 1171: 
 1172: 1;
 1173: 
 1174: #######################################################
 1175: #######################################################
 1176: 
 1177: =pod
 1178: 
 1179: =back
 1180: 
 1181: =cut
 1182: 
 1183: #######################################################
 1184: #######################################################
 1185: 
 1186: __END__
 1187: 

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