File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.125: download - view: text, annotated - select for diffs
Tue Oct 4 18:27:32 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove course_id as an argument to loncoursedata::get_classlist

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

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