File:  [LON-CAPA] / loncom / interface / lonstatistics.pm
Revision 1.139: download - view: text, annotated - select for diffs
Fri Feb 22 18:19:38 2008 UTC (16 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, HEAD
- Top level sequence was being included twice.

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

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