Annotation of loncom/interface/statistics/lonproblemstatistics.pm, revision 1.104

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.104   ! albertel    3: # $Id: lonproblemstatistics.pm,v 1.103 2005/03/27 05:08:03 matthew Exp $
1.1       stredwic    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: #
1.47      matthew    29: ###############################################
                     30: ###############################################
                     31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
                     36: lonproblemstatistics
                     37: 
                     38: =head1 SYNOPSIS
                     39: 
                     40: Routines to present problem statistics to instructors via tables,
                     41: Excel files, and plots.
                     42: 
                     43: =over 4
                     44: 
                     45: =cut
                     46: 
                     47: ###############################################
                     48: ###############################################
1.1       stredwic   49: 
1.36      minaeibi   50: package Apache::lonproblemstatistics;
1.1       stredwic   51: 
                     52: use strict;
1.104   ! albertel   53: use Apache::lonnet;
1.62      matthew    54: use Apache::loncommon();
1.1       stredwic   55: use Apache::lonhtmlcommon;
                     56: use Apache::loncoursedata;
1.41      matthew    57: use Apache::lonstatistics;
1.84      matthew    58: use LONCAPA::lonmetadata();
1.59      matthew    59: use Apache::lonlocal;
1.44      matthew    60: use Spreadsheet::WriteExcel;
1.70      matthew    61: use Apache::lonstathelpers();
1.71      matthew    62: use Time::HiRes;
1.73      matthew    63: 
                     64: my @StatsArray;
1.79      matthew    65: my %SeqStat;    # keys are symbs, values are hash refs
1.73      matthew    66: 
1.59      matthew    67: ##
                     68: ## Localization notes:
                     69: ##
                     70: ## in @Fields[0]->{'long_title'} is placed in Excel files and is used as the
                     71: ## header for plots created with Graph.pm, both of which more than likely do
                     72: ## not support localization.
                     73: ##
1.79      matthew    74: #
                     75: #
                     76: ##
                     77: ## Description of Field attributes
                     78: ##
                     79: ## Attribute     Required   Value       Meaning or Use
                     80: ##
                     81: ## name            yes      any scalar  Used to uniquely identify field
                     82: ## title           yes      any scalar  This is what the user sees to identify
                     83: ##                                      the field.  Passed through &mt().
                     84: ## long_title      yes      any scalar  Used as graph heading and in excel
                     85: ##                                      output.  NOT translated
                     86: ## align           no    (left|right|center)  HTML cell contents alignment
                     87: ## color           yes      html color  HTML cell background color
                     88: ##                                      used to visually group statistics
                     89: ## special         no          (link)   Indicates a link, target is name.link
                     90: ##                                      Currently set in &get_statistics()
                     91: ## graphable       no      (yes|no)     Can a bar graph of the field be 
                     92: ##                                      produced?
                     93: ## sortable        no      (yes|no)     Should a sort link be put in the
                     94: ##                                      column header?
                     95: ## selectable      yes     (yes|no)     Can the column be removed from the
                     96: ##                                      statistics display?
                     97: ## selected        yes     (yes|no)     Is the column selected by default?
                     98: ##
1.85      matthew    99: ## format          no      sprintf format string
                    100: ##
                    101: ## excel_format    no      excel format type 
                    102: ##                               (see &Apache::loncommon::define_excel_formats
1.49      matthew   103: my @Fields = (
                    104:            { name => 'problem_num',
                    105:              title => 'P#',
                    106:              align => 'right',
1.76      matthew   107:              color => '#FFFFE6',
                    108:              selectable => 'no',
1.80      matthew   109:              defaultselected => 'yes',
1.76      matthew   110:            },
1.49      matthew   111:            { name   => 'container',
1.51      matthew   112:              title  => 'Sequence or Folder',
1.49      matthew   113:              align  => 'left',
                    114:              color  => '#FFFFE6',
1.76      matthew   115:              sortable => 'yes',
                    116:              selectable => 'no',
1.80      matthew   117:              defaultselected => 'yes',
1.76      matthew   118:            },
1.49      matthew   119:            { name   => 'title',
                    120:              title  => 'Title',
                    121:              align  => 'left',
                    122:              color  => '#FFFFE6',
                    123:              special  => 'link',
1.76      matthew   124:              sortable => 'yes', 
                    125:              selectable => 'no',
1.80      matthew   126:              defaultselected => 'yes',
1.76      matthew   127:            },
1.49      matthew   128:            { name   => 'part', 
                    129:              title  => 'Part',
                    130:              align  => 'left',
1.55      matthew   131:              color  => '#FFFFE6',
1.76      matthew   132:              selectable => 'no',
1.80      matthew   133:              defaultselected => 'yes',
1.76      matthew   134:            },
1.49      matthew   135:            { name   => 'num_students',
                    136:              title  => '#Stdnts',
                    137:              align  => 'right',
                    138:              color  => '#EEFFCC',
                    139:              format => '%d',
                    140:              sortable  => 'yes',
                    141:              graphable => 'yes',
1.76      matthew   142:              long_title => 'Number of Students Attempting Problem',
                    143:              selectable => 'yes',
1.80      matthew   144:              defaultselected => 'yes',
1.76      matthew   145:            },
1.49      matthew   146:            { name   => 'tries',
                    147:              title  => 'Tries',
                    148:              align  => 'right',
                    149:              color  => '#EEFFCC',
                    150:              format => '%d',
                    151:              sortable  => 'yes',
                    152:              graphable => 'yes',
1.76      matthew   153:              long_title => 'Total Number of Tries',
                    154:              selectable => 'yes',
1.80      matthew   155:              defaultselected => 'yes',
1.76      matthew   156:            },
1.49      matthew   157:            { name   => 'max_tries',
                    158:              title  => 'Max Tries',
                    159:              align  => 'right',
                    160:              color  => '#DDFFFF',
                    161:              format => '%d',
                    162:              sortable  => 'yes',
                    163:              graphable => 'yes',
1.76      matthew   164:              long_title => 'Maximum Number of Tries',
                    165:              selectable => 'yes',
1.80      matthew   166:              defaultselected => 'yes',
1.76      matthew   167:            },
1.73      matthew   168:            { name   => 'min_tries',
                    169:              title  => 'Min Tries',
                    170:              align  => 'right',
                    171:              color  => '#DDFFFF',
                    172:              format => '%d',
                    173:              sortable  => 'yes',
                    174:              graphable => 'yes',
1.76      matthew   175:              long_title => 'Minumum Number of Tries',
                    176:              selectable => 'yes',
1.80      matthew   177:              defaultselected => 'yes',
1.76      matthew   178:            },
1.49      matthew   179:            { name   => 'mean_tries',
                    180:              title  => 'Mean Tries',
                    181:              align  => 'right',
                    182:              color  => '#DDFFFF',
                    183:              format => '%5.2f',
                    184:              sortable  => 'yes',
                    185:              graphable => 'yes',
1.76      matthew   186:              long_title => 'Average Number of Tries',
                    187:              selectable => 'yes',
1.80      matthew   188:              defaultselected => 'yes',
1.76      matthew   189:            },
1.49      matthew   190:            { name   => 'std_tries',
                    191:              title  => 'S.D. tries',
                    192:              align  => 'right',
                    193:              color  => '#DDFFFF',
                    194:              format => '%5.2f',
                    195:              sortable  => 'yes',
                    196:              graphable => 'yes',
1.76      matthew   197:              long_title => 'Standard Deviation of Number of Tries',
                    198:              selectable => 'yes',
1.80      matthew   199:              defaultselected => 'yes',
1.76      matthew   200:            },
1.49      matthew   201:            { name   => 'skew_tries',
                    202:              title  => 'Skew Tries',
                    203:              align  => 'right',
                    204:              color  => '#DDFFFF',
                    205:              format => '%5.2f',
                    206:              sortable  => 'yes',
                    207:              graphable => 'yes',
1.76      matthew   208:              long_title => 'Skew of Number of Tries',
                    209:              selectable => 'yes',
1.80      matthew   210:              defaultselected => 'no',
1.76      matthew   211:            },
1.49      matthew   212:            { name   => 'num_solved',
                    213:              title  => '#YES',
                    214:              align  => 'right',
                    215:              color  => '#FFDDDD',
1.63      matthew   216:              format => '%4.1f',#             format => '%d',
1.49      matthew   217:              sortable  => 'yes',
                    218:              graphable => 'yes',
1.76      matthew   219:              long_title => 'Number of Students able to Solve',
1.77      matthew   220:              selectable => 'yes',
1.80      matthew   221:              defaultselected => 'yes',
1.76      matthew   222:            },
1.49      matthew   223:            { name   => 'num_override',
                    224:              title  => '#yes',
                    225:              align  => 'right',
                    226:              color  => '#FFDDDD',
1.63      matthew   227:              format => '%4.1f',#             format => '%d',
1.49      matthew   228:              sortable  => 'yes',
                    229:              graphable => 'yes',
1.76      matthew   230:              long_title => 'Number of Students given Override',
                    231:              selectable => 'yes',
1.80      matthew   232:              defaultselected => 'yes',
1.76      matthew   233:            },
1.95      matthew   234:            { name   => 'tries_per_correct',
                    235:              title  => 'tries/correct',
                    236:              align  => 'right',
                    237:              color  => '#FFDDDD',
                    238:              format => '%4.1f',
                    239:              sortable  => 'yes',
                    240:              graphable => 'yes',
                    241:              long_title => 'Tries per Correct Answer',
                    242:              selectable => 'yes',
                    243:              defaultselected => 'yes',
                    244:            },
1.73      matthew   245:            { name   => 'num_wrong',
                    246:              title  => '#Wrng',
1.49      matthew   247:              align  => 'right',
1.73      matthew   248:              color  => '#FFDDDD',
1.49      matthew   249:              format => '%4.1f',
                    250:              sortable  => 'yes',
                    251:              graphable => 'yes',
1.93      matthew   252:              long_title => 'Number of students whose final answer is wrong',
                    253:              selectable => 'yes',
                    254:              defaultselected => 'yes',
                    255:            },
                    256:            { name   => 'per_wrong',
                    257:              title  => '%Wrng',
                    258:              align  => 'right',
                    259:              color  => '#FFDDDD',
                    260:              format => '%4.1f',
                    261:              sortable  => 'yes',
                    262:              graphable => 'yes',
1.76      matthew   263:              long_title => 'Percent of students whose final answer is wrong',
                    264:              selectable => 'yes',
1.80      matthew   265:              defaultselected => 'yes',
1.76      matthew   266:            },
1.73      matthew   267:            { name   => 'deg_of_diff',
                    268:              title  => 'DoDiff',
                    269:              align  => 'right',
                    270:              color  => '#FFFFE6',
                    271:              format => '%5.2f',
                    272:              sortable  => 'yes',
                    273:              graphable => 'yes',
                    274:              long_title => 'Degree of Difficulty'.
1.76      matthew   275:                            '[ 1 - ((#YES+#yes) / Tries) ]',
                    276:              selectable => 'yes',
1.80      matthew   277:              defaultselected => 'yes',
1.76      matthew   278:            },
1.71      matthew   279:            { name   => 'deg_of_disc',
1.73      matthew   280:              title  => 'DoDisc',
1.71      matthew   281:              align  => 'right',
                    282:              color  => '#FFFFE6',
                    283:              format => '%4.2f',
                    284:              sortable  => 'yes',
                    285:              graphable => 'yes',
1.76      matthew   286:              long_title => 'Degree of Discrimination',
                    287:              selectable => 'yes',
1.89      matthew   288:              defaultselected => 'yes',
1.76      matthew   289:            },
1.85      matthew   290: ##   duedate included for research purposes.  Commented out most of the time.
                    291: #           { name => 'duedate',
                    292: #             title => 'Due Date',
                    293: #             align => 'left',
                    294: #             color => '#FFFFFF',
                    295: #             sortable => 'yes',
                    296: #             graphable => 'no',
                    297: #             long_title => 'Due date of resource for instructor',
                    298: #             selectable => 'no',
                    299: #             defaultselected => 'yes',
                    300: #            },
                    301: ##   opendate included for research purposes.  Commented out most of the time.
                    302: #           { name => 'opendate',
                    303: #             title => 'Open Date',
                    304: #             align => 'left',
                    305: #             color => '#FFFFFF',
                    306: #             sortable => 'yes',
                    307: #             graphable => 'no',
                    308: #             long_title => 'date resource became answerable',
                    309: #             selectable => 'no',
                    310: #             defaultselected => 'yes',
                    311: #            },
                    312: ##   symb included for research purposes.  Commented out most of the time.
                    313: #           { name => 'symb',
                    314: #             title => 'Symb',
                    315: #             align => 'left',
                    316: #             color => '#FFFFFF',
                    317: #             sortable => 'yes',
                    318: #             graphable => 'no',
                    319: #             long_title => 'Unique LON-CAPA identifier for problem',
                    320: #             selectable => 'no',
                    321: #             defaultselected => 'yes',
                    322: #            },
1.86      matthew   323: ##   resptypes included for research purposes.  Commented out most of the time.
                    324: #           { name => 'resptypes',
                    325: #             title => 'Response Types',
                    326: #             align => 'left',
                    327: #             color => '#FFFFFF',
                    328: #             sortable => 'no',
                    329: #             graphable => 'no',
                    330: #             long_title => 'Response Types used in this problem',
                    331: #             selectable => 'no',
                    332: #             defaultselected => 'yes',
                    333: #            },
1.94      matthew   334: ##   maxtries included for research purposes.  Commented out most of the time.
                    335: #           { name => 'maxtries',
                    336: #             title => 'Maxtries',
                    337: #             align => 'left',
                    338: #             color => '#FFFFFF',
                    339: #             sortable => 'no',
                    340: #             graphable => 'no',
                    341: #             long_title => 'Maximum number of tries',
                    342: #             selectable => 'no',
                    343: #             defaultselected => 'yes',
                    344: #            },
                    345: ##   hinttries included for research purposes.  Commented out most of the time.
                    346: #           { name => 'hinttries',
                    347: #             title => 'hinttries',
                    348: #             align => 'left',
                    349: #             color => '#FFFFFF',
                    350: #             sortable => 'no',
                    351: #             graphable => 'no',
                    352: #             long_title => 'Number of tries before a hint appears',
                    353: #             selectable => 'no',
                    354: #             defaultselected => 'yes',
                    355: #            },
1.98      matthew   356: #
                    357: ##   problem weight for instructor
                    358:            { name => 'weight',
                    359:              title => 'weight',
                    360:              align => 'right',
                    361:              color => '#FFFFFF',
                    362:              sortable => 'no',
                    363:              graphable => 'no',
                    364:              long_title => 'Problem weight (for instructor)',
1.100     matthew   365:              selectable => 'yes',
1.98      matthew   366:              defaultselected => 'yes',
                    367:             },
1.49      matthew   368: );
                    369: 
1.79      matthew   370: my @SeqFields = (
                    371:            { name   => 'title',
                    372:              title  => 'Sequence',
                    373:              align  => 'left',
                    374:              color  => '#FFFFE6',
                    375:              special  => 'no',
                    376:              sortable => 'no', 
                    377:              selectable => 'yes',
1.80      matthew   378:              defaultselected => 'no',
1.79      matthew   379:            },
                    380:            { name   => 'items',
                    381:              title  => '#Items',
                    382:              align  => 'right',
                    383:              color  => '#FFFFE6',
                    384:              format => '%4d',
                    385:              sortable  => 'no',
                    386:              graphable => 'no',
                    387:              long_title => 'Number of Items in Sequence',
                    388:              selectable => 'yes',
1.80      matthew   389:              defaultselected => 'no',
1.79      matthew   390:            },
                    391:            { name   => 'scoremean',
                    392:              title  => 'Score Mean',
                    393:              align  => 'right',
                    394:              color  => '#FFFFE6',
                    395:              format => '%4.2f',
                    396:              sortable  => 'no',
                    397:              graphable => 'no',
                    398:              long_title => 'Mean Sequence Score',
                    399:              selectable => 'yes',
1.80      matthew   400:              defaultselected => 'no',
1.79      matthew   401:            },
                    402:            { name   => 'scorestd',
                    403:              title  => 'Score STD',
                    404:              align  => 'right',
                    405:              color  => '#FFFFE6',
                    406:              format => '%4.2f',
                    407:              sortable  => 'no',
                    408:              graphable => 'no',
                    409:              long_title => 'Standard Deviation of Sequence Scores',
                    410:              selectable => 'yes',
1.80      matthew   411:              defaultselected => 'no',
1.79      matthew   412:            },
                    413:            { name   => 'scoremax',
                    414:              title  => 'Score Max',
                    415:              align  => 'right',
                    416:              color  => '#FFFFE6',
                    417:              format => '%4.2f',
                    418:              sortable  => 'no',
                    419:              graphable => 'no',
                    420:              long_title => 'Maximum Sequence Score',
                    421:              selectable => 'yes',
1.80      matthew   422:              defaultselected => 'no',
1.79      matthew   423:            },
                    424:            { name   => 'scoremin',
                    425:              title  => 'Score Min',
                    426:              align  => 'right',
                    427:              color  => '#FFFFE6',
                    428:              format => '%4.2f',
                    429:              sortable  => 'no',
                    430:              graphable => 'no',
                    431:              long_title => 'Minumum Sequence Score',
                    432:              selectable => 'yes',
1.80      matthew   433:              defaultselected => 'no',
1.79      matthew   434:            },
                    435:            { name   => 'scorecount',
                    436:              title  => 'Score N',
                    437:              align  => 'right',
                    438:              color  => '#FFFFE6',
                    439:              format => '%4d',
                    440:              sortable  => 'no',
                    441:              graphable => 'no',
                    442:              long_title => 'Number of Students in score computations',
                    443:              selectable => 'yes',
1.80      matthew   444:              defaultselected => 'no',
1.79      matthew   445:            },
                    446:            { name   => 'countmean',
                    447:              title  => 'Count Mean',
                    448:              align  => 'right',
                    449:              color  => '#FFFFFF',
                    450:              format => '%4.2f',
                    451:              sortable  => 'no',
                    452:              graphable => 'no',
                    453:              long_title => 'Mean Sequence Score',
                    454:              selectable => 'yes',
1.80      matthew   455:              defaultselected => 'no',
1.79      matthew   456:            },
                    457:            { name   => 'countstd',
                    458:              title  => 'Count STD',
                    459:              align  => 'right',
                    460:              color  => '#FFFFFF',
                    461:              format => '%4.2f',
                    462:              sortable  => 'no',
                    463:              graphable => 'no',
                    464:              long_title => 'Standard Deviation of Sequence Scores',
                    465:              selectable => 'yes',
1.80      matthew   466:              defaultselected => 'no',
1.79      matthew   467:            },
                    468:            { name   => 'countmax',
                    469:              title  => 'Count Max',
                    470:              align  => 'right',
                    471:              color  => '#FFFFFF',
                    472:              format => '%4.2f',
                    473:              sortable  => 'no',
                    474:              graphable => 'no',
                    475:              long_title => 'Maximum Number of Correct Problems',
                    476:              selectable => 'yes',
1.80      matthew   477:              defaultselected => 'no',
1.79      matthew   478:            },
                    479:            { name   => 'countmin',
                    480:              title  => 'Count Min',
                    481:              align  => 'right',
                    482:              color  => '#FFFFFF',
                    483:              format => '%4.2f',
                    484:              sortable  => 'no',
                    485:              graphable => 'no',
                    486:              long_title => 'Minumum Number of Correct Problems',
                    487:              selectable => 'yes',
1.80      matthew   488:              defaultselected => 'no',
1.79      matthew   489:            },
                    490:            { name   => 'count',
                    491:              title  => 'Count N',
                    492:              align  => 'right',
                    493:              color  => '#FFFFFF',
                    494:              format => '%4d',
                    495:              sortable  => 'no',
                    496:              graphable => 'no',
                    497:              long_title => 'Number of Students in score computations',
                    498:              selectable => 'yes',
1.80      matthew   499:              defaultselected => 'no',
1.79      matthew   500:            },
                    501:            { name   => 'KR-21',
                    502:              title  => 'KR-21',
                    503:              align  => 'right',
                    504:              color  => '#FFAAAA',
                    505:              format => '%4.2f',
                    506:              sortable  => 'no',
                    507:              graphable => 'no',
                    508:              long_title => 'KR-21 reliability statistic',
                    509:              selectable => 'yes',
1.80      matthew   510:              defaultselected => 'no',
1.79      matthew   511:            },           
                    512: );
                    513: 
1.76      matthew   514: my %SelectedFields;
                    515: 
                    516: sub parse_field_selection {
                    517:     #
                    518:     # Pull out the defaults
1.104   ! albertel  519:     if (! defined($env{'form.fieldselections'})) {
        !           520:         $env{'form.fieldselections'} = [];
1.76      matthew   521:         foreach my $field (@Fields) {
                    522:             next if ($field->{'selectable'} ne 'yes');
1.80      matthew   523:             if ($field->{'defaultselected'} eq 'yes') {
1.104   ! albertel  524:                 push(@{$env{'form.fieldselections'}},$field->{'name'});
1.76      matthew   525:             }
                    526:         }
                    527:     }
                    528:     #
1.80      matthew   529:     # Make sure the data we are plotting is there
                    530:     my %NeededFields;
1.104   ! albertel  531:     if (exists($env{'form.plot'}) && $env{'form.plot'} ne '' &&
        !           532:         $env{'form.plot'} ne 'none') {
        !           533:         if ($env{'form.plot'} eq 'degrees') {
1.80      matthew   534:             $NeededFields{'deg_of_diff'}++;
                    535:             $NeededFields{'deg_of_disc'}++;
1.104   ! albertel  536:         } elsif ($env{'form.plot'} eq 'tries statistics') {
1.80      matthew   537:             $NeededFields{'mean_tries'}++;
                    538:             $NeededFields{'std_tries'}++;
                    539:             $NeededFields{'problem_num'}++;
                    540:         } else {
1.104   ! albertel  541:             $NeededFields{$env{'form.plot'}}++;
1.80      matthew   542:         }
                    543:     }
                    544:     #
1.76      matthew   545:     # This should not happen, but in case it does...
1.104   ! albertel  546:     if (ref($env{'form.fieldselections'}) ne 'ARRAY') {
        !           547:         $env{'form.fieldselections'} = [$env{'form.fieldselections'}];
1.76      matthew   548:     }
                    549:     #
                    550:     # Set the field data and the selected fields (for easier checking)
                    551:     undef(%SelectedFields);
                    552:     foreach my $field (@Fields) {
1.80      matthew   553:         if ($field->{'selectable'} ne 'yes') {
                    554:             $field->{'selected'} = 'yes';
                    555:         } else {
                    556:             $field->{'selected'} = 'no';
                    557:         }
                    558:         if (exists($NeededFields{$field->{'name'}})) {
                    559:             $field->{'selected'} = 'yes';
                    560:             $SelectedFields{$field->{'name'}}++;
                    561:         }
1.104   ! albertel  562:         foreach my $selection (@{$env{'form.fieldselections'}}) {
1.76      matthew   563:             if ($selection eq $field->{'name'} || $selection eq 'all') {
                    564:                 $field->{'selected'} = 'yes';
                    565:                 $SelectedFields{$field->{'name'}}++;
                    566:             }
                    567:         }
                    568:     }
1.82      matthew   569:     #
                    570:     # Always show all the sequence statistics (for now)
                    571:     foreach my $field (@SeqFields) {
                    572:         $field->{'selected'} = 'yes';
                    573:     }
1.76      matthew   574:     return;
                    575: }
                    576: 
                    577: sub field_selection_input {
                    578:     my $Str = '<select name="fieldselections" multiple size="5">'."\n";
                    579:     $Str .= '<option value="all">all</option>'."\n";
                    580:     foreach my $field (@Fields) {
                    581:         next if ($field->{'selectable'} ne 'yes');
                    582:         $Str .= '    <option value="'.$field->{'name'}.'" ';
                    583:         if ($field->{'selected'} eq 'yes') {
                    584:             $Str .= 'selected ';
                    585:         }
                    586:         $Str .= '>'.$field->{'title'}.'</option>'."\n";
                    587:     }
                    588:     $Str .= "</select>\n";
                    589: }
                    590: 
1.47      matthew   591: ###############################################
                    592: ###############################################
                    593: 
                    594: =pod 
                    595: 
                    596: =item &CreateInterface()
                    597: 
                    598: Create the main intereface for the statistics page.  Allows the user to
                    599: select sections, maps, and output.
                    600: 
                    601: =cut
1.1       stredwic  602: 
1.47      matthew   603: ###############################################
                    604: ###############################################
1.41      matthew   605: sub CreateInterface {
1.87      matthew   606:     my ($r) = @_;
1.80      matthew   607:     #
1.76      matthew   608:     &parse_field_selection();
1.80      matthew   609:     #
1.41      matthew   610:     my $Str = '';
1.67      matthew   611:     $Str .= &Apache::lonhtmlcommon::breadcrumbs
1.69      matthew   612:         (undef,'Overall Problem Statistics','Statistics_Overall_Key');
1.41      matthew   613:     $Str .= '<table cellspacing="5">'."\n";
                    614:     $Str .= '<tr>';
1.59      matthew   615:     $Str .= '<td align="center"><b>'.&mt('Sections').'</b></td>';
                    616:     $Str .= '<td align="center"><b>'.&mt('Enrollment Status').'</b></td>';
                    617:     $Str .= '<td align="center"><b>'.&mt('Sequences and Folders').'</b></td>';
1.76      matthew   618:     $Str .= '<td align="center"><b>'.&mt('Statistics').'</b></td>';
1.70      matthew   619:     $Str .= '<td rowspan="2">'.
                    620:         &Apache::lonstathelpers::limit_by_time_form().'</td>';
1.41      matthew   621:     $Str .= '</tr>'."\n";
                    622:     #
                    623:     $Str .= '<tr><td align="center">'."\n";
                    624:     $Str .= &Apache::lonstatistics::SectionSelect('Section','multiple',5);
1.50      matthew   625:     $Str .= '</td><td align="center">';
                    626:     $Str .= &Apache::lonhtmlcommon::StatusOptions(undef,undef,5);
1.41      matthew   627:     $Str .= '</td><td align="center">';
                    628:     #
1.97      matthew   629:     $Str .= &Apache::lonstatistics::map_select('Maps','multiple,all',5);
1.76      matthew   630:     $Str .= '</td><td>'.&field_selection_input();
1.41      matthew   631:     $Str .= '</td></tr>'."\n";
                    632:     $Str .= '</table>'."\n";
1.87      matthew   633:     #
                    634:     $Str .= '<p>'.&mt('Status: [_1]',
                    635:                          '<input type="text" '.
                    636:                          'name="stats_status" size="60" value="" />'
                    637:                          ).
                    638:                          '</nobr></p>';
                    639:     #
1.59      matthew   640:     $Str .= '<input type="submit" name="GenerateStatistics" value="'.
                    641:         &mt('Generate Statistics').'" />';
1.54      matthew   642:     $Str .= '&nbsp;'x5;
1.73      matthew   643:     $Str .= 'Plot '.&plot_dropdown().('&nbsp;'x10);
1.87      matthew   644:     #
1.73      matthew   645:     return $Str;
1.41      matthew   646: }
1.25      stredwic  647: 
1.41      matthew   648: ###############################################
                    649: ###############################################
1.28      stredwic  650: 
1.47      matthew   651: =pod 
                    652: 
                    653: =item &BuildProblemStatisticsPage()
                    654: 
                    655: Main interface to problem statistics.
                    656: 
                    657: =cut
                    658: 
1.41      matthew   659: ###############################################
                    660: ###############################################
1.97      matthew   661: my $navmap;
                    662: my @sequences;
                    663: 
1.41      matthew   664: sub BuildProblemStatisticsPage {
                    665:     my ($r,$c)=@_;
1.97      matthew   666:     undef($navmap);
                    667:     undef(@sequences);
1.61      matthew   668:     #
                    669:     my %Saveable_Parameters = ('Status' => 'scalar',
                    670:                                'statsoutputmode' => 'scalar',
                    671:                                'Section' => 'array',
                    672:                                'StudentData' => 'array',
1.77      matthew   673:                                'Maps' => 'array',
                    674:                                'fieldselections'=> 'array');
1.61      matthew   675:     &Apache::loncommon::store_course_settings('statistics',
                    676:                                               \%Saveable_Parameters);
                    677:     &Apache::loncommon::restore_course_settings('statistics',
                    678:                                                 \%Saveable_Parameters);
                    679:     #
                    680:     &Apache::lonstatistics::PrepareClasslist();
1.41      matthew   681:     #
1.73      matthew   682:     # Clear the package variables
                    683:     undef(@StatsArray);
1.79      matthew   684:     undef(%SeqStat);
1.71      matthew   685:     #
1.73      matthew   686:     # Finally let the user know we are here
1.87      matthew   687:     my $interface = &CreateInterface($r);
1.57      matthew   688:     $r->print($interface);
1.104   ! albertel  689:     $r->print('<input type="hidden" name="sortby" value="'.$env{'form.sortby'}.
1.41      matthew   690:               '" />');
1.73      matthew   691:     #
1.87      matthew   692:     my @CacheButtonHTML = 
                    693:         &Apache::lonstathelpers::manage_caches($r,'Statistics','stats_status');
                    694:     my $Str;
                    695:     foreach my $html (@CacheButtonHTML) {
                    696:         $Str.=$html.('&nbsp;'x5);
                    697:     }
                    698:     #
                    699:     $r->print($Str);
1.104   ! albertel  700:     if (! exists($env{'form.firstrun'})) {
1.73      matthew   701:         $r->print('<h3>'.
                    702:                   &mt('Press "Generate Statistics" when you are ready.').
                    703:                   '</h3><p>'.
                    704:                   &mt('It may take some time to update the student data '.
                    705:                       'for the first analysis.  Future analysis this session '.
                    706:                       ' will not have this delay.').
                    707:                   '</p>');
1.41      matthew   708:         return;
1.28      stredwic  709:     }
1.73      matthew   710:     $r->rflush();
1.41      matthew   711:     #
1.73      matthew   712:     # This probably does not need to be done each time we are called, but
                    713:     # it does not slow things down noticably.
                    714:     &Apache::loncoursedata::populate_weight_table();
1.75      matthew   715:     #
1.99      matthew   716:     ($navmap,@sequences) = 
                    717:         &Apache::lonstatistics::selected_sequences_with_assessments();
                    718:     if (! ref($navmap)) {
                    719:         $r->print('<h1>'.&mt('A course-wide error occured.').'</h1>'.
                    720:                   '<h3>'.$navmap.'</h3>');
                    721:         return;
                    722:     }
1.104   ! albertel  723:     if (exists($env{'form.Excel'})) {
1.102     matthew   724:         $r->print('<h4>'.
                    725:                   &Apache::lonstatistics::section_and_enrollment_description().
                    726:                   '</h4>');
1.73      matthew   727:         &Excel_output($r);
1.87      matthew   728:     } else { 
                    729:         $r->print('<input type="submit" name="Excel" value="'.
                    730:                   &mt('Produce Excel Output').'" />'.'&nbsp;'x5);
                    731:         $r->rflush();
1.102     matthew   732:         $r->print('<h4>'.
                    733:                   &Apache::lonstatistics::section_and_enrollment_description().
                    734:                   '</h4>');
1.75      matthew   735:         my $count = 0;
1.97      matthew   736:         foreach my $seq (@sequences) {
                    737:             my @resources = 
                    738:                 &Apache::lonstathelpers::get_resources($navmap,$seq);
                    739:             $count += scalar(@resources);
1.75      matthew   740:         }
                    741:         if ($count > 10) {
                    742:             $r->print('<h2>'.
                    743:                       &mt('Compiling statistics for [_1] problems',$count).
                    744:                       '</h2>');
                    745:             if ($count > 30) {
                    746:                 $r->print('<h3>'.&mt('This will take some time.').'</h3>');
                    747:             }
                    748:             $r->rflush();
                    749:         }
                    750:         #
1.104   ! albertel  751:         my $sortby = $env{'form.sortby'};
1.73      matthew   752:         $sortby = 'container' if (! defined($sortby) || $sortby =~ /^\s*$/);
1.104   ! albertel  753:         my $plot = $env{'form.plot'};
1.75      matthew   754:         if ($plot eq '' || $plot eq 'none') {
                    755:             undef($plot);
                    756:         }
1.73      matthew   757:         if ($sortby eq 'container' && ! defined($plot)) {
1.79      matthew   758:             &output_sequence_statistics($r);
1.73      matthew   759:             &output_html_by_sequence($r);
                    760:         } else {
                    761:             if (defined($plot)) {
                    762:                 &make_plot($r,$plot);
                    763:             }
                    764:             &output_html_stats($r);
1.79      matthew   765:             &output_sequence_statistics($r);
1.73      matthew   766:         }
                    767:     }
                    768:     return;
                    769: }
                    770: 
1.79      matthew   771: sub output_sequence_statistics {
                    772:     my ($r) = @_;
                    773:     my $c=$r->connection();
1.90      matthew   774:     $r->print('<h2>'.&mt('Sequence Statistics').
                    775: 	      &Apache::loncommon::help_open_topic('Statistics_Sequence').
                    776: 	      '</h2>');
1.79      matthew   777:     $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
                    778:               '<table border="0" cellpadding="3">'."\n".
                    779:               '<tr bgcolor="#FFFFE6">');
                    780:     $r->print(&sequence_html_header());
1.97      matthew   781:     foreach my $seq (@sequences) { 
1.79      matthew   782:         last if ($c->aborted);
                    783:         &compute_sequence_statistics($seq);
                    784:         $r->print(&sequence_html_output($seq));
                    785:     }
                    786:     $r->print('</table>');
                    787:     $r->print('</table>');
                    788:     $r->rflush();
                    789:     return;
                    790: }
                    791: 
                    792: 
1.73      matthew   793: ##########################################################
                    794: ##########################################################
                    795: ##
                    796: ## HTML output routines
                    797: ##
                    798: ##########################################################
                    799: ##########################################################
                    800: sub output_html_by_sequence {
                    801:     my ($r) = @_;
                    802:     my $c = $r->connection();
                    803:     $r->print(&html_preamble());
1.41      matthew   804:     #
1.97      matthew   805:     foreach my $seq (@sequences) {
1.73      matthew   806:         last if ($c->aborted);
1.97      matthew   807:         $r->print("<h3>".$seq->compTitle."</h3>".
1.73      matthew   808:                   '<table border="0"><tr><td bgcolor="#777777">'."\n".
                    809:                   '<table border="0" cellpadding="3">'."\n".
                    810:                   '<tr bgcolor="#FFFFE6">'.
                    811:                   &statistics_table_header('no container')."</tr>\n");
                    812:         my @Data = &compute_statistics_on_sequence($seq);
                    813:         foreach my $data (@Data) {
                    814:             $r->print('<tr>'.&statistics_html_table_data($data,
                    815:                                                          'no container').
                    816:                       "</tr>\n");
1.70      matthew   817:         }
1.73      matthew   818:         $r->print('</table>'."\n".'</table>'."\n");
1.41      matthew   819:         $r->rflush();
1.28      stredwic  820:     }
1.41      matthew   821:     return;
                    822: }
1.21      stredwic  823: 
1.73      matthew   824: sub output_html_stats {
                    825:     my ($r)=@_;
                    826:     &compute_all_statistics($r);
                    827:     $r->print(&html_preamble());
1.104   ! albertel  828:     &sort_data($env{'form.sortby'});
1.73      matthew   829:     #
                    830:     my $count=0;
                    831:     foreach my $data (@StatsArray) {
                    832:         if ($count++ % 50 == 0) {
                    833:             $r->print("</table>\n</table>\n");
                    834:             $r->print('<table border="0"><tr><td bgcolor="#777777">'."\n".
                    835:                       '<table border="0" cellpadding="3">'."\n".
                    836:                       '<tr bgcolor="#FFFFE6">'.
                    837:                       '<tr bgcolor="#FFFFE6">'.
                    838:                       &statistics_table_header().
                    839:                       "</tr>\n");
                    840:         }
                    841:         $r->print('<tr>'.&statistics_html_table_data($data)."</tr>\n");
                    842:     }
                    843:     $r->print("</table>\n</table>\n");
                    844:     return;
                    845: }
1.47      matthew   846: 
1.73      matthew   847: sub html_preamble {
                    848:     my $Str='';
                    849:     $Str .= "<h2>".
1.104   ! albertel  850:         $env{'course.'.$env{'request.course.id'}.'.description'}.
1.73      matthew   851:         "</h2>\n";
                    852:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
                    853:     if (defined($starttime) || defined($endtime)) {
                    854:         # Inform the user what the time limits on the data are.
                    855:         $Str .= '<h3>'.&mt('Statistics on submissions from [_1] to [_2]',
                    856:                            &Apache::lonlocal::locallocaltime($starttime),
                    857:                            &Apache::lonlocal::locallocaltime($endtime)
                    858:                            ).'</h3>';
                    859:     }
                    860:     $Str .= "<h3>".&mt('Compiled on [_1]',
                    861:                        &Apache::lonlocal::locallocaltime(time))."</h3>";
                    862:     return $Str;
                    863: }
1.47      matthew   864: 
                    865: 
1.44      matthew   866: ###############################################
                    867: ###############################################
1.73      matthew   868: ##
                    869: ## Misc HTML output routines
                    870: ##
                    871: ###############################################
                    872: ###############################################
                    873: sub statistics_html_table_data {
                    874:     my ($data,$options) = @_;
                    875:     my $row = '';
                    876:     foreach my $field (@Fields) {
                    877:         next if ($options =~ /no $field->{'name'}/);
1.76      matthew   878:         next if ($field->{'selected'} ne 'yes');
1.73      matthew   879:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
                    880:         if (exists($field->{'align'})) {
                    881:             $row .= ' align="'.$field->{'align'}.'"';
1.41      matthew   882:             }
1.73      matthew   883:         $row .= '>';
                    884:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
                    885:             $row .= '<a href="'.$data->{$field->{'name'}.'.link'}.'">';
1.41      matthew   886:         }
1.92      matthew   887:         if (exists($field->{'format'}) && $data->{$field->{'name'}} !~ /[A-Z]/i) {
1.73      matthew   888:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
                    889:         } else {
                    890:             $row .= $data->{$field->{'name'}};
                    891:         }
                    892:         if (exists($field->{'special'}) && $field->{'special'} eq 'link') {
                    893:             $row.= '</a>';
                    894:         }
                    895:         $row .= '</td>';
1.21      stredwic  896:     }
1.73      matthew   897:     return $row;
1.41      matthew   898: }
1.25      stredwic  899: 
1.73      matthew   900: sub statistics_table_header {
                    901:     my ($options) = @_;
                    902:     my $header_row;
                    903:     foreach my $field (@Fields) {
                    904:         next if ($options =~ /no $field->{'name'}/);
1.76      matthew   905:         next if ($field->{'selected'} ne 'yes');
1.73      matthew   906:         $header_row .= '<th>';
                    907:         if (exists($field->{'sortable'}) && $field->{'sortable'} eq 'yes') {
                    908:             $header_row .= '<a href="javascript:'.
                    909:                 'document.Statistics.sortby.value='."'".$field->{'name'}."'".
                    910:                     ';document.Statistics.submit();">';
                    911:         }
                    912:         $header_row .= &mt($field->{'title'});
                    913:         if ($options =~ /sortable/) {
                    914:             $header_row.= '</a>';
                    915:         }
                    916:         if ($options !~ /no plots/        && 
                    917:             exists($field->{'graphable'}) && 
                    918:             $field->{'graphable'} eq 'yes') {
                    919:             $header_row.=' (';
                    920:             $header_row .= '<a href="javascript:'.
                    921:                 "document.Statistics.plot.value='$field->{'name'}'".
                    922:                     ';document.Statistics.submit();">';
                    923:             $header_row .= &mt('plot').'</a>)';
                    924:         }
                    925:         $header_row .= '</th>';
                    926:     }
                    927:     return $header_row;
                    928: }
1.26      stredwic  929: 
1.79      matthew   930: sub sequence_html_header {
                    931:     my $Str .= '<tr>';
                    932:     foreach my $field (@SeqFields) {
                    933: #        next if ($field->{'selected'} ne 'yes');
                    934:         $Str .= '<th bgcolor="'.$field->{'color'}.'"';
                    935:         $Str .= '>'.$field->{'title'}.'</th>';
                    936:     }
                    937:     $Str .= '</tr>';
                    938:     return $Str;
                    939: }
                    940: 
                    941: 
                    942: sub sequence_html_output {
                    943:     my ($seq) = @_;
1.97      matthew   944:     my $data = $SeqStat{$seq->symb};
1.79      matthew   945:     my $row = '<tr>';
                    946:     foreach my $field (@SeqFields) {
1.82      matthew   947:         next if ($field->{'selected'} ne 'yes');
1.79      matthew   948:         $row .= '<td bgcolor="'.$field->{'color'}.'"';
                    949:         if (exists($field->{'align'})) {
                    950:             $row .= ' align="'.$field->{'align'}.'"';
                    951:         }
                    952:         $row .= '>';
                    953:         if (exists($field->{'format'})) {
                    954:             $row .= sprintf($field->{'format'},$data->{$field->{'name'}});
                    955:         } else {
                    956:             $row .= $data->{$field->{'name'}};
                    957:         }
                    958:         $row .= '</td>';
                    959:     }
                    960:     $row .= '</tr>'."\n";
                    961:     return $row;
                    962: }
                    963: 
1.73      matthew   964: ####################################################
                    965: ####################################################
                    966: ##
                    967: ##    Plotting Routines
                    968: ##
                    969: ####################################################
                    970: ####################################################
                    971: sub make_plot {
                    972:     my ($r,$plot) = @_;
                    973:     &compute_all_statistics($r);
1.104   ! albertel  974:     &sort_data($env{'form.sortby'});
1.73      matthew   975:     if ($plot eq 'degrees') {
                    976:         &degrees_plot($r);
1.74      matthew   977:     } elsif ($plot eq 'tries statistics') {
                    978:         &tries_data_plot($r);
1.73      matthew   979:     } else {
                    980:         &make_single_stat_plot($r,$plot);
                    981:     }
                    982:     return;
                    983: }
1.47      matthew   984: 
1.73      matthew   985: sub make_single_stat_plot {
                    986:     my ($r,$datafield) = @_;
1.41      matthew   987:     #
1.73      matthew   988:     my $title; my $yaxis;
                    989:     foreach my $field (@Fields) {
                    990:         next if ($field->{'name'} ne $datafield);
                    991:         $title = $field->{'long_title'};
                    992:         $yaxis = $field->{'title'};
                    993:         last;
                    994:     }
                    995:     if ($title eq '' || $yaxis eq '') {
                    996:         # datafield is something we do not know enough about to plot
                    997:         $r->print('<h3>'.
                    998:                   &mt('Unable to plot the requested statistic.').
                    999:                   '</h3>');
                   1000:         return;
1.49      matthew  1001:     }
                   1002:     #
1.73      matthew  1003:     # Build up the data sets to plot
                   1004:     my @Labels; 
                   1005:     my @Data;
                   1006:     my $max = 1;
                   1007:     foreach my $data (@StatsArray) {
                   1008:         push(@Labels,$data->{'problem_num'});
                   1009:         push(@Data,$data->{$datafield});
                   1010:         if ($data->{$datafield}>$max) {
                   1011:             $max = $data->{$datafield};
                   1012:         }
                   1013:     }
                   1014:     foreach (1,2,3,4,5,10,15,20,25,40,50,75,100,150,200,250,300,500,600,750,
                   1015:              1000,1500,2000,2500,3000,3500,4000,5000,7500,10000,15000,20000) {
                   1016:         if ($max <= $_) {
                   1017:             $max = $_;
                   1018:             last;
1.42      matthew  1019:         }
                   1020:     }
1.73      matthew  1021:     if ($max > 20000) {
                   1022:         $max = 10000*(int($max/10000)+1);
1.42      matthew  1023:     }
1.73      matthew  1024:     #
                   1025:     $r->print("<p>".&Apache::loncommon::DrawBarGraph($title,
                   1026:                                                      'Problem Number',
                   1027:                                                      $yaxis,
                   1028:                                                      $max,
                   1029:                                                      undef, # colors
                   1030:                                                      \@Labels,
                   1031:                                                      \@Data)."</p>\n");
                   1032:     return;
                   1033: }
                   1034: 
                   1035: sub degrees_plot {
                   1036:     my ($r)=@_;
                   1037:     my $count = scalar(@StatsArray);
                   1038:     my $width = 50 + 10*$count;
                   1039:     $width = 300 if ($width < 300);
                   1040:     my $height = 300;
                   1041:     my $plot = '';
                   1042:     my $ymax = 0;
                   1043:     my $ymin = 0;
                   1044:     my @Disc; my @Diff; my @Labels;    
                   1045:     foreach my $data (@StatsArray) {
                   1046:         push(@Labels,$data->{'problem_num'});
                   1047:         my $disc = $data->{'deg_of_disc'};
                   1048:         my $diff = $data->{'deg_of_diff'};
                   1049:         push(@Disc,$disc);
                   1050:         push(@Diff,$diff);
                   1051:         #
                   1052:         $ymin = $disc if ($ymin > $disc);
                   1053:         $ymin = $diff if ($ymin > $diff);
                   1054:         $ymax = $disc if ($ymax < $disc);
                   1055:         $ymax = $diff if ($ymax < $diff);
                   1056:     }
                   1057:     #
                   1058:     # Make sure we show relevant information.
                   1059:     if ($ymin < 0) {
                   1060:         if (abs($ymin) < 0.05) {
                   1061:             $ymin = 0;
                   1062:         } else {
                   1063:             $ymin = -1;
1.42      matthew  1064:         }
                   1065:     }
1.73      matthew  1066:     if ($ymax > 0) {
                   1067:         if (abs($ymax) < 0.05) {
                   1068:             $ymax = 0;
1.42      matthew  1069:         } else {
1.73      matthew  1070:             $ymax = 1;
1.42      matthew  1071:         }
1.43      matthew  1072:     }
1.49      matthew  1073:     #
1.73      matthew  1074:     my $xmax = $Labels[-1];
                   1075:     if ($xmax > 50) {
                   1076:         if ($xmax % 10 != 0) {
                   1077:             $xmax = 10 * (int($xmax/10)+1);
                   1078:         }
                   1079:     } else {
                   1080:         if ($xmax % 5 != 0) {
                   1081:             $xmax = 5 * (int($xmax/5)+1);
1.49      matthew  1082:         }
1.26      stredwic 1083:     }
1.41      matthew  1084:     #
1.73      matthew  1085:     my $discdata .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1086:                     '<data>'.join(',',@Disc).'</data>'.$/;
                   1087:     #
                   1088:     my $diffdata .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1089:                     '<data>'.join(',',@Diff).'</data>'.$/;
                   1090:     #
1.81      matthew  1091:     my $title = 'Degree of Discrimination\nand Degree of Difficulty';
                   1092:     if ($xmax > 50) {
                   1093:         $title = 'Degree of Discrimination and Degree of Difficulty';
                   1094:     }
                   1095:     #
1.73      matthew  1096:     $plot=<<"END";
                   1097: <gnuplot 
                   1098:     texfont="10"
                   1099:     fgcolor="x000000"
                   1100:     plottype="Cartesian"
                   1101:     font="large"
                   1102:     grid="on"
                   1103:     align="center"
                   1104:     border="on"
                   1105:     transparent="on"
1.81      matthew  1106:     alttag="Degree of Discrimination and Degree of Difficulty Plot"
1.73      matthew  1107:     samples="100"
                   1108:     bgcolor="xffffff"
                   1109:     height="$height"
                   1110:     width="$width">
                   1111:     <key 
                   1112:         pos="top right"
                   1113:         title=""
                   1114:         box="off" />
1.81      matthew  1115:     <title>$title</title>
1.73      matthew  1116:     <axis xmin="0" ymin="$ymin" xmax="$xmax" ymax="$ymax" color="x000000" />
                   1117:     <xlabel>Problem Number</xlabel>
                   1118:     <curve 
                   1119:         linestyle="linespoints" 
                   1120:         name="DoDisc" 
                   1121:         pointtype="0" 
                   1122:         color="x000000">
                   1123:         $discdata
                   1124:     </curve>
                   1125:     <curve 
                   1126:         linestyle="linespoints" 
                   1127:         name="DoDiff" 
                   1128:         pointtype="0" 
                   1129:         color="xFF0000">
                   1130:         $diffdata
                   1131:     </curve>
                   1132: </gnuplot>
                   1133: END
                   1134:     my $plotresult = 
                   1135:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
                   1136:     $r->print($plotresult);
1.41      matthew  1137:     return;
1.42      matthew  1138: }
                   1139: 
1.74      matthew  1140: sub tries_data_plot {
                   1141:     my ($r)=@_;
                   1142:     my $count = scalar(@StatsArray);
                   1143:     my $width = 50 + 10*$count;
                   1144:     $width = 300 if ($width < 300);
                   1145:     my $height = 300;
                   1146:     my $plot = '';
                   1147:     my @STD;  my @Mean; my @Max; my @Min;
                   1148:     my @Labels;
                   1149:     my $ymax = 5;
                   1150:     foreach my $data (@StatsArray) {
                   1151:         my $max = $data->{'mean_tries'} + $data->{'std_tries'};
                   1152:         $ymax = $max if ($ymax < $max);
                   1153:         $ymax = $max if ($ymax < $max);
                   1154:         push(@Labels,$data->{'problem_num'});
                   1155:         push(@STD,$data->{'std_tries'});
                   1156:         push(@Mean,$data->{'mean_tries'});
                   1157:     }
                   1158:     #
                   1159:     # Make sure we show relevant information.
                   1160:     my $xmax = $Labels[-1];
                   1161:     if ($xmax > 50) {
                   1162:         if ($xmax % 10 != 0) {
                   1163:             $xmax = 10 * (int($xmax/10)+1);
                   1164:         }
                   1165:     } else {
                   1166:         if ($xmax % 5 != 0) {
                   1167:             $xmax = 5 * (int($xmax/5)+1);
                   1168:         }
                   1169:     }
                   1170:     $ymax = int($ymax)+1+2;
                   1171:     #
                   1172:     my $std_data .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1173:                     '<data>'.join(',',@Mean).'</data>'.$/;
                   1174:     #
                   1175:     my $std_error_data .= '<data>'.join(',',@Labels).'</data>'.$/.
                   1176:                           '<data>'.join(',',@Mean).'</data>'.$/.
                   1177:                           '<data>'.join(',',@STD).'</data>'.$/;
                   1178:     #
1.81      matthew  1179:     my $title = 'Mean and S.D. of Tries';
                   1180:     if ($xmax > 25) {
                   1181:         $title = 'Mean and Standard Deviation of Tries';
                   1182:     }
                   1183:     #
1.74      matthew  1184:     $plot=<<"END";
                   1185: <gnuplot 
                   1186:     texfont="10"
                   1187:     fgcolor="x000000"
                   1188:     plottype="Cartesian"
                   1189:     font="large"
                   1190:     grid="on"
                   1191:     align="center"
                   1192:     border="on"
                   1193:     transparent="on"
1.81      matthew  1194:     alttag="Mean and S.D of Tries Plot"
1.74      matthew  1195:     samples="100"
                   1196:     bgcolor="xffffff"
                   1197:     height="$height"
                   1198:     width="$width">
1.81      matthew  1199:     <title>$title</title>
1.74      matthew  1200:     <axis xmin="0" ymin="0" xmax="$xmax" ymax="$ymax" color="x000000" />
                   1201:     <xlabel>Problem Number</xlabel>
1.81      matthew  1202:     <ylabel>Number of Tries</ylabel>
1.74      matthew  1203:     <curve 
                   1204:         linestyle="yerrorbars"
                   1205:         name="S.D. Tries" 
                   1206:         pointtype="1" 
                   1207:         color="x666666">
                   1208:         $std_error_data
                   1209:     </curve>
                   1210:     <curve 
                   1211:         linestyle="points"
                   1212:         name="Mean Tries" 
                   1213:         pointtype="1" 
                   1214:         color="xCC4444">
                   1215:         $std_data
                   1216:     </curve>
                   1217: </gnuplot>
                   1218: END
                   1219:     my $plotresult = 
                   1220:         '<p>'.&Apache::lonxml::xmlparse($r,'web',$plot).'</p>'.$/;
                   1221:     $r->print($plotresult);
                   1222:     return;
                   1223: }
                   1224: 
1.73      matthew  1225: sub plot_dropdown {
                   1226:     my $current = '';
                   1227:     #
1.104   ! albertel 1228:     if (defined($env{'form.plot'})) {
        !          1229:         $current = $env{'form.plot'};
1.73      matthew  1230:     }
                   1231:     #
                   1232:     my @Additional_Plots = (
                   1233:                             { graphable=>'yes',
                   1234:                               name => 'degrees',
1.81      matthew  1235:                               title => 'Difficulty Indexes' },
1.74      matthew  1236:                             { graphable=>'yes',
                   1237:                               name => 'tries statistics',
1.81      matthew  1238:                               title => 'Tries Statistics' });
1.73      matthew  1239:     #
                   1240:     my $Str= "\n".'<select name="plot" size="1">';
                   1241:     $Str .= '<option name="none"></option>'."\n";
                   1242:     $Str .= '<option name="none2">none</option>'."\n";
1.81      matthew  1243:     foreach my $field (@Additional_Plots,@Fields) {
1.73      matthew  1244:         if (! exists($field->{'graphable'}) ||
                   1245:             $field->{'graphable'} ne 'yes') {
                   1246:             next;
                   1247:         }
                   1248:         $Str .= '<option value="'.$field->{'name'}.'"';
                   1249:         if ($field->{'name'} eq $current) {
                   1250:             $Str .= ' selected ';
                   1251:         }
                   1252:         $Str.= '>'.&mt($field->{'title'}).'</option>'."\n";
                   1253:     }
                   1254:     $Str .= '</select>'."\n";
                   1255:     return $Str;
                   1256: }
                   1257: 
1.41      matthew  1258: ###############################################
                   1259: ###############################################
1.73      matthew  1260: ##
                   1261: ## Excel output routines
                   1262: ##
1.41      matthew  1263: ###############################################
                   1264: ###############################################
1.73      matthew  1265: sub Excel_output {
1.44      matthew  1266:     my ($r) = @_;
1.73      matthew  1267:     $r->print('<h2>'.&mt('Preparing Excel Spreadsheet').'</h2>');
                   1268:     ##
                   1269:     ## Compute the statistics
                   1270:     &compute_all_statistics($r);
                   1271:     my $c = $r->connection;
                   1272:     return if ($c->aborted());
1.101     matthew  1273:     #
                   1274:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
1.73      matthew  1275:     ##
                   1276:     ## Create the excel workbook
1.101     matthew  1277:     my ($excel_workbook,$filename,$format) =
                   1278:         &Apache::loncommon::create_workbook($r);
                   1279:     return if (! defined($excel_workbook));
1.44      matthew  1280:     #
                   1281:     # Add a worksheet
1.104   ! albertel 1282:     my $sheetname = $env{'course.'.$env{'request.course.id'}.'.description'};
1.44      matthew  1283:     if (length($sheetname) > 31) {
                   1284:         $sheetname = substr($sheetname,0,31);
                   1285:     }
1.73      matthew  1286:     my $excel_sheet = $excel_workbook->addworksheet(
                   1287:         &Apache::loncommon::clean_excel_name($sheetname));
                   1288:     ##
                   1289:     ## Begin creating excel sheet
                   1290:     ##
                   1291:     my ($rows_output,$cols_output) = (0,0);
1.44      matthew  1292:     #
                   1293:     # Put the course description in the header
                   1294:     $excel_sheet->write($rows_output,$cols_output++,
1.104   ! albertel 1295:                    $env{'course.'.$env{'request.course.id'}.'.description'},
1.82      matthew  1296:                         $format->{'h1'});
1.44      matthew  1297:     $cols_output += 3;
                   1298:     #
                   1299:     # Put a description of the sections listed
                   1300:     my $sectionstring = '';
1.82      matthew  1301:     $excel_sheet->write($rows_output,$cols_output++,
1.103     matthew  1302:                         &Apache::lonstatistics::section_and_enrollment_description('plaintext'),
1.82      matthew  1303:                         $format->{'h3'});
1.102     matthew  1304:     $cols_output += scalar(&Apache::lonstatistics::get_selected_sections());
1.44      matthew  1305:     #
1.70      matthew  1306:     # Time restrictions
                   1307:     my $time_string;
                   1308:     if (defined($starttime)) {
                   1309:         # call localtime but not lonlocal:locallocaltime because excel probably
                   1310:         # cannot handle localized text.  Probably.
                   1311:         $time_string .= 'Data collected from '.localtime($time_string);
                   1312:         if (defined($endtime)) {
                   1313:             $time_string .= ' to '.localtime($endtime);
                   1314:         }
                   1315:         $time_string .= '.';
                   1316:     } elsif (defined($endtime)) {
                   1317:         # See note above about lonlocal:locallocaltime
                   1318:         $time_string .= 'Data collected before '.localtime($endtime).'.';
                   1319:     }
1.82      matthew  1320:     if (defined($time_string)) {
                   1321:         $excel_sheet->write($rows_output,$cols_output++,$time_string);
                   1322:         $cols_output+= 5;
                   1323:     }
1.70      matthew  1324:     #
1.44      matthew  1325:     # Put the date in there too
                   1326:     $excel_sheet->write($rows_output,$cols_output++,
                   1327:                         'Compiled on '.localtime(time));
                   1328:     #
                   1329:     $rows_output++; 
                   1330:     $cols_output=0;
1.82      matthew  1331:     ##
                   1332:     ## Sequence Statistics
                   1333:     ## 
                   1334:     &write_headers($excel_sheet,$format,\$rows_output,\$cols_output,
                   1335:                    \@SeqFields);
1.97      matthew  1336:     foreach my $seq (@sequences) {
                   1337:         my $data = $SeqStat{$seq->symb};
1.82      matthew  1338:         $cols_output=0;
                   1339:         foreach my $field (@SeqFields) {
                   1340:             next if ($field->{'selected'} ne 'yes');
1.85      matthew  1341:             my $fieldformat = undef;
                   1342:             if (exists($field->{'excel_format'})) {
                   1343:                 $fieldformat = $format->{$field->{'excel_format'}};
                   1344:             }
1.55      matthew  1345:             $excel_sheet->write($rows_output,$cols_output++,
1.85      matthew  1346:                                 $data->{$field->{'name'}},$fieldformat);
1.55      matthew  1347:         }
1.82      matthew  1348:         $rows_output++;
                   1349:         $cols_output=0;
1.55      matthew  1350:     }
1.82      matthew  1351:     ##
                   1352:     ## Resource Statistics
                   1353:     ##
1.55      matthew  1354:     $rows_output++;
                   1355:     $cols_output=0;
1.82      matthew  1356:     &write_headers($excel_sheet,$format,\$rows_output,\$cols_output,
                   1357:                    \@Fields);
                   1358:     #
1.73      matthew  1359:     foreach my $data (@StatsArray) {
                   1360:         $cols_output=0;
                   1361:         foreach my $field (@Fields) {
1.76      matthew  1362:             next if ($field->{'selected'} ne 'yes');
1.73      matthew  1363:             next if ($field->{'name'} eq 'problem_num');
1.85      matthew  1364:             my $fieldformat = undef;
                   1365:             if (exists($field->{'excel_format'})) {
                   1366:                 $fieldformat = $format->{$field->{'excel_format'}};
                   1367:             }
1.73      matthew  1368:             $excel_sheet->write($rows_output,$cols_output++,
1.85      matthew  1369:                                 $data->{$field->{'name'}},$fieldformat);
1.44      matthew  1370:         }
1.73      matthew  1371:         $rows_output++;
1.82      matthew  1372:         $cols_output=0;
1.44      matthew  1373:     }
                   1374:     #
                   1375:     $excel_workbook->close();
1.73      matthew  1376:     #
1.44      matthew  1377:     # Tell the user where to get their excel file
                   1378:     $r->print('<br />'.
1.59      matthew  1379:               '<a href="'.$filename.'">'.
                   1380:               &mt('Your Excel Spreadsheet').'</a>'."\n");
1.44      matthew  1381:     $r->rflush();
                   1382:     return;
                   1383: }
                   1384: 
1.82      matthew  1385: ##
                   1386: ## &write_headers
                   1387: ##
                   1388: sub write_headers {
                   1389:     my ($excel_sheet,$format,$rows_output,$cols_output,$Fields) = @_;
                   1390:     ##
                   1391:     ## First the long titles
                   1392:     foreach my $field (@{$Fields}) {
                   1393:         next if ($field->{'name'} eq 'problem_num');
                   1394:         next if ($field->{'selected'} ne 'yes');
                   1395:         if (exists($field->{'long_title'})) {
                   1396:             $excel_sheet->write($$rows_output,${$cols_output},
                   1397:                                 $field->{'long_title'},
                   1398:                                 $format->{'bold'});
                   1399:         } else {
                   1400:             $excel_sheet->write($$rows_output,${$cols_output},'');
                   1401:         }
                   1402:         ${$cols_output}+= 1;
                   1403:     }
                   1404:     ${$cols_output} =0;
                   1405:     ${$rows_output}+=1;
                   1406:     ##
                   1407:     ## Then the short titles
                   1408:     foreach my $field (@{$Fields}) {
                   1409:         next if ($field->{'selected'} ne 'yes');
                   1410:         next if ($field->{'name'} eq 'problem_num');
                   1411:         # Use english for excel as I am not sure how well excel handles 
                   1412:         # other character sets....
                   1413:         $excel_sheet->write($$rows_output,$$cols_output,
                   1414:                             $field->{'title'},
                   1415:                             $format->{'bold'});
                   1416:         $$cols_output+=1;
                   1417:     }
                   1418:     ${$cols_output} =0;
                   1419:     ${$rows_output}+=1;
                   1420:     return;
                   1421: }
                   1422: 
1.73      matthew  1423: ##################################################
                   1424: ##################################################
                   1425: ##
                   1426: ## Statistics Gathering and Manipulation Routines
                   1427: ##
                   1428: ##################################################
                   1429: ##################################################
                   1430: sub compute_statistics_on_sequence {
                   1431:     my ($seq) = @_;
                   1432:     my @Data;
1.97      matthew  1433:     foreach my $res (&Apache::lonstathelpers::get_resources($navmap,$seq)) {
                   1434:         foreach my $part (@{$res->parts}) {
                   1435:             next if ($res->is_survey($part));
1.73      matthew  1436:             #
                   1437:             # This is where all the work happens
                   1438:             my $data = &get_statistics($seq,$res,$part,scalar(@StatsArray)+1);
                   1439:             push (@Data,$data);
                   1440:             push (@StatsArray,$data);
1.49      matthew  1441:         }
1.26      stredwic 1442:     }
1.73      matthew  1443:     return @Data;
1.41      matthew  1444: }
1.26      stredwic 1445: 
1.73      matthew  1446: sub compute_all_statistics {
                   1447:     my ($r) = @_;
                   1448:     if (@StatsArray > 0) {
                   1449:         # Assume we have already computed the statistics
                   1450:         return;
                   1451:     }
                   1452:     my $c = $r->connection;
1.97      matthew  1453:     foreach my $seq (@sequences) {
1.73      matthew  1454:         last if ($c->aborted);
1.82      matthew  1455:         &compute_sequence_statistics($seq);
1.73      matthew  1456:         &compute_statistics_on_sequence($seq);
1.49      matthew  1457:     }
                   1458: }
                   1459: 
1.73      matthew  1460: sub sort_data {
                   1461:     my ($sortkey) = @_;
                   1462:     return if (! @StatsArray);
1.45      matthew  1463:     #
1.73      matthew  1464:     # Sort the data
                   1465:     my $sortby = undef;
1.49      matthew  1466:     foreach my $field (@Fields) {
1.73      matthew  1467:         if ($sortkey eq $field->{'name'}) {
                   1468:             $sortby = $field->{'name'};
1.45      matthew  1469:         }
1.26      stredwic 1470:     }
1.73      matthew  1471:     if (! defined($sortby) || $sortby eq '' || $sortby eq 'problem_num') {
                   1472:         $sortby = 'container';
                   1473:     }
                   1474:     if ($sortby ne 'container') {
                   1475:         # $sortby is already defined, so we can charge ahead
                   1476:         if ($sortby =~ /^(title|part)$/i) {
                   1477:             # Alpha comparison
                   1478:             @StatsArray = sort {
                   1479:                 lc($a->{$sortby}) cmp lc($b->{$sortby}) ||
                   1480:                 lc($a->{'title'}) cmp lc($b->{'title'}) ||
                   1481:                 lc($a->{'part'}) cmp lc($b->{'part'});
                   1482:             } @StatsArray;
1.24      stredwic 1483:         } else {
1.73      matthew  1484:             # Numerical comparison
                   1485:             @StatsArray = sort {
                   1486:                 my $retvalue = 0;
                   1487:                 if ($b->{$sortby} eq 'nan') {
                   1488:                     if ($a->{$sortby} ne 'nan') {
                   1489:                         $retvalue = -1;
                   1490:                     } else {
                   1491:                         $retvalue = 0;
                   1492:                     }
                   1493:                 }
                   1494:                 if ($a->{$sortby} eq 'nan') {
                   1495:                     if ($b->{$sortby} ne 'nan') {
                   1496:                         $retvalue = 1;
                   1497:                     }
                   1498:                 }
                   1499:                 if ($retvalue eq '0') {
                   1500:                     $retvalue = $b->{$sortby} <=> $a->{$sortby}     ||
                   1501:                             lc($a->{'title'}) <=> lc($b->{'title'}) ||
                   1502:                             lc($a->{'part'})  <=> lc($b->{'part'});
                   1503:                 }
                   1504:                 $retvalue;
                   1505:             } @StatsArray;
1.24      stredwic 1506:         }
                   1507:     }
1.45      matthew  1508:     #
1.73      matthew  1509:     # Renumber the data set
                   1510:     my $count;
                   1511:     foreach my $data (@StatsArray) {
                   1512:         $data->{'problem_num'} = ++$count;
                   1513:     }
1.24      stredwic 1514:     return;
1.48      matthew  1515: }
                   1516: 
1.70      matthew  1517: ########################################################
                   1518: ########################################################
                   1519: 
                   1520: =pod
                   1521: 
                   1522: =item &get_statistics()
                   1523: 
                   1524: Wrapper routine from the call to loncoursedata::get_problem_statistics.  
1.73      matthew  1525: Calls lonstathelpers::get_time_limits() to limit the data set by time
                   1526: and &compute_discrimination_factor
1.70      matthew  1527: 
                   1528: Inputs: $sequence, $resource, $part, $problem_num
                   1529: 
                   1530: Returns: Hash reference with statistics data from 
                   1531: loncoursedata::get_problem_statistics.
                   1532: 
                   1533: =cut
                   1534: 
                   1535: ########################################################
                   1536: ########################################################
1.48      matthew  1537: sub get_statistics {
1.49      matthew  1538:     my ($sequence,$resource,$part,$problem_num) = @_;
1.48      matthew  1539:     #
1.70      matthew  1540:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
1.97      matthew  1541:     my $symb = $resource->symb;
1.104   ! albertel 1542:     my $courseid = $env{'request.course.id'};
1.48      matthew  1543:     #
1.49      matthew  1544:     my $data = &Apache::loncoursedata::get_problem_statistics
1.102     matthew  1545:                         ([&Apache::lonstatistics::get_selected_sections()],
1.66      matthew  1546:                          $Apache::lonstatistics::enrollment_status,
1.70      matthew  1547:                          $symb,$part,$courseid,$starttime,$endtime);
1.85      matthew  1548:     $data->{'symb'}        = $symb;
1.49      matthew  1549:     $data->{'part'}        = $part;
                   1550:     $data->{'problem_num'} = $problem_num;
1.97      matthew  1551:     $data->{'container'}   = $sequence->compTitle;
                   1552:     $data->{'title'}       = $resource->compTitle;
                   1553:     $data->{'title.link'}  = $resource->src.'?symb='.
                   1554:         &Apache::lonnet::escape($resource->symb);
1.49      matthew  1555:     #
1.76      matthew  1556:     if ($SelectedFields{'deg_of_disc'}) {
                   1557:         $data->{'deg_of_disc'} = 
                   1558:             &compute_discrimination_factor($resource,$part,$sequence);
                   1559:     }
1.83      matthew  1560:     #
                   1561:     # Store in metadata if computations were done for all students
1.84      matthew  1562:     if ($data->{'num_students'} > 1) {
1.102     matthew  1563:         my @Sections = &Apache::lonstatistics::get_selected_sections();
1.84      matthew  1564:         my $sections = '"'.join(' ',@Sections).'"';
                   1565:         $sections =~ s/&+/_/g;  # Ensure no special characters
                   1566:         $data->{'sections'}=$sections;
1.104   ! albertel 1567:         $data->{'course'} = $env{'request.course.id'};
1.97      matthew  1568:         my $urlres=(&Apache::lonnet::decode_symb($resource->symb))[2];
1.84      matthew  1569:         $data->{'urlres'}=$urlres;
                   1570:         my %storestats = 
                   1571:             &LONCAPA::lonmetadata::dynamic_metadata_storage($data);
1.83      matthew  1572:         my ($dom,$user) = $urlres=~/^(\w+)\/(\w+)/; 
                   1573:         &Apache::lonnet::put('nohist_resevaldata',\%storestats,$dom,$user);
                   1574:     }
1.85      matthew  1575:     #
1.95      matthew  1576:     $data->{'tries_per_correct'} = $data->{'tries'} / 
                   1577:         ($data->{'num_solved'}+0.1);
                   1578:     #
1.85      matthew  1579:     # Get the due date for research purposes (commented out most of the time)
                   1580: #    $data->{'duedate'} = 
                   1581: #        &Apache::lonnet::EXT('resource.'.$part.'.duedate',$symb);
                   1582: #    $data->{'opendate'} = 
                   1583: #        &Apache::lonnet::EXT('resource.'.$part.'.opendate',$symb);
1.94      matthew  1584: #    $data->{'maxtries'} = 
                   1585: #        &Apache::lonnet::EXT('resource.'.$part.'.maxtries',$symb);
                   1586: #    $data->{'hinttries'} =
                   1587: #        &Apache::lonnet::EXT('resource.'.$part.'.hinttries',$symb);
1.98      matthew  1588:     $data->{'weight'} =
                   1589:         &Apache::lonnet::EXT('resource.'.$part.'.weight',$symb);
1.86      matthew  1590: #    $data->{'resptypes'} = join(',',@{$resource->{'partdata'}->{$part}->{'ResponseTypes'}});
1.49      matthew  1591:     return $data;
1.71      matthew  1592: }
                   1593: 
                   1594: ###############################################
                   1595: ###############################################
                   1596: 
                   1597: =pod
                   1598: 
                   1599: =item &compute_discrimination_factor()
                   1600: 
                   1601: Inputs: $Resource, $Sequence
                   1602: 
                   1603: Returns: integer between -1 and 1
                   1604: 
                   1605: =cut
                   1606: 
                   1607: ###############################################
                   1608: ###############################################
                   1609: sub compute_discrimination_factor {
1.97      matthew  1610:     my ($resource,$part,$seq) = @_;
                   1611:     my $symb = $resource->symb;
1.71      matthew  1612:     my @Resources;
1.97      matthew  1613:     foreach my $res (&Apache::lonstathelpers::get_resources($navmap,$seq)){
                   1614:         next if ($res->symb eq $symb);
                   1615:         push (@Resources,$res->symb);
1.71      matthew  1616:     }
                   1617:     #
                   1618:     # rank
1.83      matthew  1619:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
1.71      matthew  1620:     my $ranking = 
                   1621:         &Apache::loncoursedata::rank_students_by_scores_on_resources
                   1622:         (\@Resources,
1.102     matthew  1623:          [&Apache::lonstatistics::get_selected_sections()],
1.83      matthew  1624:          $Apache::lonstatistics::enrollment_status,undef,
                   1625:          $starttime,$endtime);
1.71      matthew  1626:     #
                   1627:     # compute their percent scores on the problems in the sequence,
                   1628:     my $number_to_grab = int(scalar(@{$ranking})/4);
                   1629:     my $num_students = scalar(@{$ranking});
                   1630:     my @BottomSet = map { $_->[&Apache::loncoursedata::RNK_student()]; 
                   1631:                       } @{$ranking}[0..$number_to_grab];
                   1632:     my @TopSet    = 
                   1633:         map { 
                   1634:             $_->[&Apache::loncoursedata::RNK_student()]; 
                   1635:           } @{$ranking}[($num_students-$number_to_grab)..($num_students-1)];
1.91      matthew  1636:     if (! @BottomSet || (@BottomSet == 1 && $BottomSet[0] eq '') ||
                   1637:         ! @TopSet    || (@TopSet    == 1 && $TopSet[0]    eq '')) {
                   1638:         return 'nan';
                   1639:     }
1.71      matthew  1640:     my ($bottom_sum,$bottom_max) = 
1.97      matthew  1641:         &Apache::loncoursedata::get_sum_of_scores($symb,$part,\@BottomSet,
1.83      matthew  1642:                                                   undef,$starttime,$endtime);
1.71      matthew  1643:     my ($top_sum,$top_max) = 
1.97      matthew  1644:         &Apache::loncoursedata::get_sum_of_scores($symb,$part,\@TopSet,
1.83      matthew  1645:                                                   undef,$starttime,$endtime);
1.71      matthew  1646:     my $deg_of_disc;
                   1647:     if ($top_max == 0 || $bottom_max==0) {
                   1648:         $deg_of_disc = 'nan';
                   1649:     } else {
                   1650:         $deg_of_disc = ($top_sum/$top_max) - ($bottom_sum/$bottom_max);
                   1651:     }
                   1652:     #&Apache::lonnet::logthis('    '.$top_sum.'/'.$top_max.
                   1653:     #                         ' - '.$bottom_sum.'/'.$bottom_max);
                   1654:     return $deg_of_disc;
1.1       stredwic 1655: }
1.12      minaeibi 1656: 
1.45      matthew  1657: ###############################################
                   1658: ###############################################
1.79      matthew  1659: ##
                   1660: ## Compute KR-21
                   1661: ##
                   1662: ## To compute KR-21, you need the following information:
                   1663: ##
                   1664: ## K=the number of items in your test
                   1665: ## M=the mean score on the test
                   1666: ## s=the standard deviation of the scores on your test 
                   1667: ##
                   1668: ## then:
                   1669: ## 
                   1670: ## KR-21 rk= [K/(K-1)] * [1- (M*(K-M))/(K*s^2))]
                   1671: ##
                   1672: ###############################################
                   1673: ###############################################
                   1674: sub compute_sequence_statistics {
                   1675:     my ($seq) = @_;
1.97      matthew  1676:     my $symb = $seq->symb;
1.79      matthew  1677:     my @Resources;
1.97      matthew  1678:     my $part_count;
                   1679:     foreach my $res (&Apache::lonstathelpers::get_resources($navmap,$seq)) {
                   1680:         push (@Resources,$res->symb);
                   1681:         $part_count += scalar(@{$res->parts});
1.79      matthew  1682:     }
                   1683:     my ($starttime,$endtime) = &Apache::lonstathelpers::get_time_limits();
                   1684:     #
                   1685:     # First compute statistics based on student scores
                   1686:     my ($smin,$smax,$sMean,$sSTD,$scount,$sMAX) = 
                   1687:         &Apache::loncoursedata::score_stats
1.102     matthew  1688:                     ([&Apache::lonstatistics::get_selected_sections()],
1.79      matthew  1689:                      $Apache::lonstatistics::enrollment_status,
                   1690:                      \@Resources,$starttime,$endtime,undef);
1.97      matthew  1691:     $SeqStat{$symb}->{'title'}  = $seq->compTitle;
1.79      matthew  1692:     $SeqStat{$symb}->{'scoremax'}  = $smax;
                   1693:     $SeqStat{$symb}->{'scoremin'}  = $smin;
                   1694:     $SeqStat{$symb}->{'scoremean'} = $sMean;
                   1695:     $SeqStat{$symb}->{'scorestd'}  = $sSTD;
                   1696:     $SeqStat{$symb}->{'scorecount'} = $scount;
                   1697:     $SeqStat{$symb}->{'max_possible'} = $sMAX;
                   1698:     #
                   1699:     # Compute statistics based on the number of correct problems
                   1700:     # 'correct' is taken to mean 
                   1701:     my ($cmin,$cmax,$cMean,$cSTD,$ccount)=
                   1702:         &Apache::loncoursedata::count_stats
1.102     matthew  1703:         ([&Apache::lonstatistics::get_selected_sections()],
1.79      matthew  1704:          $Apache::lonstatistics::enrollment_status,
                   1705:          \@Resources,$starttime,$endtime,undef);
1.97      matthew  1706:     my $K = $part_count;
1.79      matthew  1707:     my $kr_21;
                   1708:     if ($K > 1 && $cSTD > 0) {
                   1709:         $kr_21 =  ($K/($K-1)) * (1 - $cMean*($K-$cMean)/($K*$cSTD**2));
                   1710:     } else {
                   1711:         $kr_21 = 'nan';
                   1712:     }
                   1713:     $SeqStat{$symb}->{'countmax'} = $cmax;
                   1714:     $SeqStat{$symb}->{'countmin'} = $cmin;
                   1715:     $SeqStat{$symb}->{'countstd'} = $cSTD;
1.82      matthew  1716:     $SeqStat{$symb}->{'countmean'} = $cMean;
1.79      matthew  1717:     $SeqStat{$symb}->{'count'} = $ccount;
                   1718:     $SeqStat{$symb}->{'items'} = $K;
                   1719:     $SeqStat{$symb}->{'KR-21'}=$kr_21;
                   1720:     return;
                   1721: }
                   1722: 
                   1723: 
1.47      matthew  1724: 
                   1725: =pod 
                   1726: 
1.73      matthew  1727: =item ProblemStatisticsLegend
                   1728: 
                   1729: =over 4
                   1730: 
                   1731: =item #Stdnts
                   1732: Total number of students attempted the problem.
                   1733: 
                   1734: =item Tries
                   1735: Total number of tries for solving the problem.
1.59      matthew  1736: 
1.73      matthew  1737: =item Max Tries
                   1738: Largest number of tries for solving the problem by a student.
                   1739: 
                   1740: =item Mean
                   1741: Average number of tries. [ Tries / #Stdnts ]
                   1742: 
                   1743: =item #YES
                   1744: Number of students solved the problem correctly.
                   1745: 
                   1746: =item #yes
                   1747: Number of students solved the problem by override.
                   1748: 
                   1749: =item %Wrong
                   1750: Percentage of students who tried to solve the problem 
                   1751: but is still incorrect. [ 100*((#Stdnts-(#YES+#yes))/#Stdnts) ]
                   1752: 
                   1753: =item DoDiff
                   1754: Degree of Difficulty of the problem.  
                   1755: [ 1 - ((#YES+#yes) / Tries) ]
                   1756: 
                   1757: =item S.D.
                   1758: Standard Deviation of the tries.  
                   1759: [ sqrt(sum((Xi - Mean)^2)) / (#Stdnts-1) 
                   1760: where Xi denotes every student\'s tries ]
                   1761: 
                   1762: =item Skew.
                   1763: Skewness of the students tries.
                   1764: [(sqrt( sum((Xi - Mean)^3) / #Stdnts)) / (S.D.^3)]
                   1765: 
                   1766: =item Dis.F.
                   1767: Discrimination Factor: A Standard for evaluating the 
                   1768: problem according to a Criterion<br>
                   1769: 
                   1770: =item [Criterion to group students into %27 Upper Students - 
                   1771: and %27 Lower Students]
                   1772: 1st Criterion for Sorting the Students: 
                   1773: Sum of Partial Credit Awarded / Total Number of Tries
                   1774: 2nd Criterion for Sorting the Students: 
                   1775: Total number of Correct Answers / Total Number of Tries
                   1776: 
                   1777: =item Disc.
                   1778: Number of Students had at least one discussion.
                   1779: 
                   1780: =back
1.47      matthew  1781: 
                   1782: =cut
1.73      matthew  1783: 
                   1784: ############################################################
                   1785: ############################################################
1.4       minaeibi 1786: 
1.1       stredwic 1787: 1;
                   1788: __END__

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