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

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

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