Annotation of loncom/interface/loncoursedata.pm, revision 1.82

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.82    ! bowersj2    3: # $Id: loncoursedata.pm,v 1.81 2003/07/14 13:10:39 matthew Exp $
1.1       stredwic    4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: ###
                     28: 
                     29: =pod
                     30: 
                     31: =head1 NAME
                     32: 
                     33: loncoursedata
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
1.22      stredwic   37: Set of functions that download and process student and course information.
1.1       stredwic   38: 
                     39: =head1 PACKAGES USED
                     40: 
                     41:  Apache::Constants qw(:common :http)
                     42:  Apache::lonnet()
1.22      stredwic   43:  Apache::lonhtmlcommon
1.1       stredwic   44:  HTML::TokeParser
                     45:  GDBM_File
                     46: 
                     47: =cut
                     48: 
                     49: package Apache::loncoursedata;
                     50: 
                     51: use strict;
                     52: use Apache::Constants qw(:common :http);
                     53: use Apache::lonnet();
1.13      stredwic   54: use Apache::lonhtmlcommon;
1.57      matthew    55: use Time::HiRes;
                     56: use Apache::lonmysql;
1.1       stredwic   57: use HTML::TokeParser;
                     58: use GDBM_File;
                     59: 
                     60: =pod
                     61: 
                     62: =head1 DOWNLOAD INFORMATION
                     63: 
1.22      stredwic   64: This section contains all the functions that get data from other servers 
                     65: and/or itself.
1.1       stredwic   66: 
                     67: =cut
                     68: 
1.50      matthew    69: ####################################################
                     70: ####################################################
1.45      matthew    71: 
                     72: =pod
                     73: 
                     74: =item &get_sequence_assessment_data()
                     75: 
                     76: AT THIS TIME THE USE OF THIS FUNCTION IS *NOT* RECOMMENDED
                     77: 
                     78: Use lonnavmaps to build a data structure describing the order and 
                     79: assessment contents of each sequence in the current course.
                     80: 
                     81: The returned structure is a hash reference. 
                     82: 
1.61      matthew    83: { title => 'title',
                     84:   symb  => 'symb',
                     85:   src   => '/s/o/u/r/c/e',
1.45      matthew    86:   type  => (container|assessment),
1.50      matthew    87:   num_assess   => 2,               # only for container
1.45      matthew    88:   parts        => [11,13,15],      # only for assessment
1.50      matthew    89:   response_ids => [12,14,16],      # only for assessment
                     90:   contents     => [........]       # only for container
1.45      matthew    91: }
                     92: 
1.50      matthew    93: $hash->{'contents'} is a reference to an array of hashes of the same structure.
                     94: 
                     95: Also returned are array references to the sequences and assessments contained
                     96: in the course.
1.49      matthew    97: 
1.45      matthew    98: 
                     99: =cut
                    100: 
1.50      matthew   101: ####################################################
                    102: ####################################################
1.45      matthew   103: sub get_sequence_assessment_data {
                    104:     my $fn=$ENV{'request.course.fn'};
                    105:     ##
                    106:     ## use navmaps
1.82    ! bowersj2  107:     my $navmap = Apache::lonnavmaps::navmap->new(1,0);
1.45      matthew   108:     if (!defined($navmap)) {
                    109:         return 'Can not open Coursemap';
                    110:     }
1.75      matthew   111:     # We explicity grab the top level map because I am not sure we
                    112:     # are pulling it from the iterator.
                    113:     my $top_level_map = $navmap->getById('0.0');
                    114:     #
1.45      matthew   115:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
1.61      matthew   116:     my $curRes = $iterator->next(); # Top level sequence
1.45      matthew   117:     ##
                    118:     ## Prime the pump 
                    119:     ## 
                    120:     ## We are going to loop until we run out of sequences/pages to explore for
                    121:     ## resources.  This means we have to start out with something to look
                    122:     ## at.
1.76      matthew   123:     my $title = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.75      matthew   124:     my $symb  = $top_level_map->symb();
                    125:     my $src   = $top_level_map->src();
                    126:     my $randompick = $top_level_map->randompick();
1.45      matthew   127:     #
1.49      matthew   128:     my @Sequences; 
                    129:     my @Assessments;
1.45      matthew   130:     my @Nested_Sequences = ();   # Stack of sequences, keeps track of depth
                    131:     my $top = { title    => $title,
1.52      matthew   132:                 src      => $src,
1.45      matthew   133:                 symb     => $symb,
                    134:                 type     => 'container',
                    135:                 num_assess => 0,
1.53      matthew   136:                 num_assess_parts => 0,
1.75      matthew   137:                 contents   => [], 
                    138:                 randompick => $randompick,
                    139:             };
1.49      matthew   140:     push (@Sequences,$top);
1.45      matthew   141:     push (@Nested_Sequences, $top);
                    142:     #
                    143:     # We need to keep track of which sequences contain homework problems
                    144:     # 
1.78      matthew   145:     my $previous_too;
1.52      matthew   146:     my $previous;
1.45      matthew   147:     while (scalar(@Nested_Sequences)) {
1.78      matthew   148:         $previous_too = $previous;
1.50      matthew   149:         $previous = $curRes;
1.45      matthew   150:         $curRes = $iterator->next();
                    151:         my $currentmap = $Nested_Sequences[-1]; # Last one on the stack
                    152:         if ($curRes == $iterator->BEGIN_MAP()) {
1.78      matthew   153:             if (! ref($previous)) {
                    154:                 $previous = $previous_too;
                    155:             }
                    156:             if (! ref($previous)) {
                    157:                 next;
                    158:             }
1.45      matthew   159:             # get the map itself, instead of BEGIN_MAP
1.51      matthew   160:             $title = $previous->title();
                    161:             $symb  = $previous->symb();
                    162:             $src   = $previous->src();
1.81      matthew   163:             # pick up the filename if there is no title available
                    164:             if (! defined($title) || $title eq '') {
                    165:                 ($title) = ($src=~/\/([^\/]*)$/);
                    166:             }
1.75      matthew   167:             $randompick = $previous->randompick();
1.45      matthew   168:             my $newmap = { title    => $title,
                    169:                            src      => $src,
                    170:                            symb     => $symb,
                    171:                            type     => 'container',
                    172:                            num_assess => 0,
1.75      matthew   173:                            randompick => $randompick,
1.45      matthew   174:                            contents   => [],
                    175:                        };
                    176:             push (@{$currentmap->{'contents'}},$newmap); # this is permanent
1.49      matthew   177:             push (@Sequences,$newmap);
1.45      matthew   178:             push (@Nested_Sequences, $newmap); # this is a stack
                    179:             next;
                    180:         }
                    181:         if ($curRes == $iterator->END_MAP()) {
                    182:             pop(@Nested_Sequences);
                    183:             next;
                    184:         }
                    185:         next if (! ref($curRes));
1.50      matthew   186:         next if (! $curRes->is_problem());# && !$curRes->randomout);
1.45      matthew   187:         # Okay, from here on out we only deal with assessments
                    188:         $title = $curRes->title();
                    189:         $symb  = $curRes->symb();
                    190:         $src   = $curRes->src();
                    191:         my $parts = $curRes->parts();
                    192:         my $assessment = { title => $title,
                    193:                            src   => $src,
                    194:                            symb  => $symb,
                    195:                            type  => 'assessment',
1.53      matthew   196:                            parts => $parts,
                    197:                            num_parts => scalar(@$parts),
1.45      matthew   198:                        };
1.49      matthew   199:         push(@Assessments,$assessment);
1.45      matthew   200:         push(@{$currentmap->{'contents'}},$assessment);
                    201:         $currentmap->{'num_assess'}++;
1.53      matthew   202:         $currentmap->{'num_assess_parts'}+= scalar(@$parts);
1.45      matthew   203:     }
1.58      matthew   204:     $navmap->untieHashes();
1.49      matthew   205:     return ($top,\@Sequences,\@Assessments);
1.45      matthew   206: }
1.50      matthew   207: 
1.4       stredwic  208: sub LoadDiscussion {
1.13      stredwic  209:     my ($courseID)=@_;
1.5       minaeibi  210:     my %Discuss=();
                    211:     my %contrib=&Apache::lonnet::dump(
                    212:                 $courseID,
                    213:                 $ENV{'course.'.$courseID.'.domain'},
                    214:                 $ENV{'course.'.$courseID.'.num'});
                    215: 				 
                    216:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
                    217: 
1.4       stredwic  218:     foreach my $temp(keys %contrib) {
                    219: 	if ($temp=~/^version/) {
                    220: 	    my $ver=$contrib{$temp};
                    221: 	    my ($dummy,$prb)=split(':',$temp);
                    222: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
                    223: 		my $name=$contrib{"$idx:$prb:sendername"};
1.5       minaeibi  224: 		$Discuss{"$name:$prb"}=$idx;	
1.4       stredwic  225: 	    }
                    226: 	}
                    227:     }       
1.5       minaeibi  228: 
                    229:     return \%Discuss;
1.1       stredwic  230: }
                    231: 
1.71      matthew   232: ################################################
                    233: ################################################
                    234: 
                    235: =pod
                    236: 
                    237: =item &GetUserName(username,userdomain)
                    238: 
                    239: Returns a hash with the following entries:
                    240:    'firstname', 'middlename', 'lastname', 'generation', and 'fullname'
                    241: 
                    242:    'fullname' is the result of &Apache::loncoursedata::ProcessFullName.
                    243: 
                    244: =cut
                    245: 
                    246: ################################################
                    247: ################################################
                    248: sub GetUserName {
                    249:     my ($username,$userdomain) = @_;
                    250:     $username = $ENV{'user.name'} if (! defined($username));
                    251:     $userdomain = $ENV{'user.domain'} if (! defined($username));
                    252:     my %userenv = &Apache::lonnet::get('environment',
                    253:                            ['firstname','middlename','lastname','generation'],
                    254:                                        $userdomain,$username);
                    255:     $userenv{'fullname'} = &ProcessFullName($userenv{'lastname'},
                    256:                                             $userenv{'generation'},
                    257:                                             $userenv{'firstname'},
                    258:                                             $userenv{'middlename'});
                    259:     return %userenv;
                    260: }
                    261: 
                    262: ################################################
                    263: ################################################
                    264: 
1.1       stredwic  265: =pod
                    266: 
                    267: =item &ProcessFullName()
                    268: 
                    269: Takes lastname, generation, firstname, and middlename (or some partial
                    270: set of this data) and returns the full name version as a string.  Format
                    271: is Lastname generation, firstname middlename or a subset of this.
                    272: 
                    273: =cut
                    274: 
1.71      matthew   275: ################################################
                    276: ################################################
1.1       stredwic  277: sub ProcessFullName {
                    278:     my ($lastname, $generation, $firstname, $middlename)=@_;
                    279:     my $Str = '';
                    280: 
1.34      matthew   281:     # Strip whitespace preceeding & following name components.
                    282:     $lastname   =~ s/(\s+$|^\s+)//g;
                    283:     $generation =~ s/(\s+$|^\s+)//g;
                    284:     $firstname  =~ s/(\s+$|^\s+)//g;
                    285:     $middlename =~ s/(\s+$|^\s+)//g;
                    286: 
1.1       stredwic  287:     if($lastname ne '') {
1.34      matthew   288: 	$Str .= $lastname;
                    289: 	$Str .= ' '.$generation if ($generation ne '');
                    290: 	$Str .= ',';
                    291:         $Str .= ' '.$firstname  if ($firstname ne '');
                    292:         $Str .= ' '.$middlename if ($middlename ne '');
1.1       stredwic  293:     } else {
1.34      matthew   294:         $Str .= $firstname      if ($firstname ne '');
                    295:         $Str .= ' '.$middlename if ($middlename ne '');
                    296:         $Str .= ' '.$generation if ($generation ne '');
1.1       stredwic  297:     }
                    298: 
                    299:     return $Str;
                    300: }
                    301: 
1.46      matthew   302: ################################################
                    303: ################################################
                    304: 
                    305: =pod
                    306: 
1.47      matthew   307: =item &make_into_hash($values);
                    308: 
                    309: Returns a reference to a hash as described by $values.  $values is
                    310: assumed to be the result of 
1.57      matthew   311:     join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47      matthew   312: 
                    313: This is a helper function for get_current_state.
                    314: 
                    315: =cut
                    316: 
                    317: ################################################
                    318: ################################################
                    319: sub make_into_hash {
                    320:     my $values = shift;
                    321:     my %tmp = map { &Apache::lonnet::unescape($_); }
                    322:                                            split(':',$values);
                    323:     return \%tmp;
                    324: }
                    325: 
                    326: 
                    327: ################################################
                    328: ################################################
                    329: 
                    330: =pod
                    331: 
1.57      matthew   332: =head1 LOCAL DATA CACHING SUBROUTINES
                    333: 
                    334: The local caching is done using MySQL.  There is no fall-back implementation
                    335: if MySQL is not running.
                    336: 
                    337: The programmers interface is to call &get_current_state() or some other
                    338: primary interface subroutine (described below).  The internals of this 
                    339: storage system are documented here.
                    340: 
                    341: There are six tables used to store student performance data (the results of
                    342: a dumpcurrent).  Each of these tables is created in MySQL with a name of
                    343: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate 
                    344: for the table.  The tables and their purposes are described below.
                    345: 
                    346: Some notes before we get started.
                    347: 
                    348: Each table must have a PRIMARY KEY, which is a column or set of columns which
                    349: will serve to uniquely identify a row of data.  NULL is not allowed!
                    350: 
                    351: INDEXes work best on integer data.
                    352: 
                    353: JOIN is used to combine data from many tables into one output.
                    354: 
                    355: lonmysql.pm is used for some of the interface, specifically the table creation
                    356: calls.  The inserts are done in bulk by directly calling the database handler.
                    357: The SELECT ... JOIN statement used to retrieve the data does not have an
                    358: interface in lonmysql.pm and I shudder at the thought of writing one.
                    359: 
                    360: =head3 Table Descriptions
                    361: 
                    362: =over 4
                    363: 
                    364: =item $symb_table
                    365: 
                    366: The symb_table has two columns.  The first is a 'symb_id' and the second
                    367: is the text name for the 'symb' (limited to 64k).  The 'symb_id' is generated
                    368: automatically by MySQL so inserts should be done on this table with an
                    369: empty first element.  This table has its PRIMARY KEY on the 'symb_id'.
                    370: 
                    371: =item $part_table
                    372: 
                    373: The part_table has two columns.  The first is a 'part_id' and the second
                    374: is the text name for the 'part' (limited to 100 characters).  The 'part_id' is
                    375: generated automatically by MySQL so inserts should be done on this table with
                    376: an empty first element.  This table has its PRIMARY KEY on the 'part' (100
                    377: characters) and a KEY on 'part_id'.
                    378: 
                    379: =item $student_table
                    380: 
                    381: The student_table has two columns.  The first is a 'student_id' and the second
                    382: is the text description of the 'student' (typically username:domain) (less
                    383: than 100 characters).  The 'student_id' is automatically generated by MySQL.
                    384: The use of the name 'student_id' is loaded, I know, but this ID is used ONLY 
                    385: internally to the MySQL database and is not the same as the students ID 
                    386: (stored in the students environment).  This table has its PRIMARY KEY on the
                    387: 'student' (100 characters).
                    388: 
                    389: =item $updatetime_table
                    390: 
                    391: The updatetime_table has two columns.  The first is 'student' (100 characters,
                    392: typically username:domain).  The second is 'updatetime', which is an unsigned
                    393: integer, NOT a MySQL date.  This table has its PRIMARY KEY on 'student' (100
                    394: characters).
                    395: 
                    396: =item $performance_table
                    397: 
                    398: The performance_table has 9 columns.  The first three are 'symb_id', 
                    399: 'student_id', and 'part_id'.  These comprise the PRIMARY KEY for this table
                    400: and are directly related to the $symb_table, $student_table, and $part_table
                    401: described above.  MySQL does better indexing on numeric items than text,
                    402: so we use these three "index tables".  The remaining columns are
                    403: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
                    404: These are either the MySQL type TINYTEXT or various integers ('tries' and 
                    405: 'timestamp').  This table has KEYs of 'student_id' and 'symb_id'.
                    406: For use of this table, see the functions described below.
                    407: 
                    408: =item $parameters_table
                    409: 
                    410: The parameters_table holds the data that does not fit neatly into the
                    411: performance_table.  The parameters table has four columns: 'symb_id',
                    412: 'student_id', 'parameter', and 'value'.  'symb_id', 'student_id', and
                    413: 'parameter' comprise the PRIMARY KEY for this table.  'parameter' is 
                    414: limited to 255 characters.  'value' is limited to 64k characters.
                    415: 
                    416: =back
                    417: 
                    418: =head3 Important Subroutines
                    419: 
                    420: Here is a brief overview of the subroutines which are likely to be of 
                    421: interest:
                    422: 
                    423: =over 4
                    424: 
                    425: =item &get_current_state(): programmers interface.
                    426: 
                    427: =item &init_dbs(): table creation
                    428: 
                    429: =item &update_student_data(): data storage calls
                    430: 
                    431: =item &get_student_data_from_performance_cache(): data retrieval
                    432: 
                    433: =back
                    434: 
                    435: =head3 Main Documentation
                    436: 
                    437: =over 4
                    438: 
                    439: =cut
                    440: 
                    441: ################################################
                    442: ################################################
                    443: 
                    444: ################################################
                    445: ################################################
                    446: {
                    447: 
                    448: my $current_course ='';
                    449: my $symb_table;
                    450: my $part_table;
                    451: my $student_table;
                    452: my $updatetime_table;
                    453: my $performance_table;
                    454: my $parameters_table;
                    455: 
                    456: ################################################
                    457: ################################################
                    458: 
                    459: =pod
                    460: 
                    461: =item &init_dbs()
                    462: 
                    463: Input: course id
                    464: 
                    465: Output: 0 on success, positive integer on error
                    466: 
                    467: This routine issues the calls to lonmysql to create the tables used to
                    468: store student data.
                    469: 
                    470: =cut
                    471: 
                    472: ################################################
                    473: ################################################
                    474: sub init_dbs {
                    475:     my $courseid = shift;
                    476:     &setup_table_names($courseid);
                    477:     #
1.73      matthew   478:     # Drop any of the existing tables
                    479:     foreach my $table ($symb_table,$part_table,$student_table,
                    480:                        $updatetime_table,$performance_table,
                    481:                        $parameters_table) {
                    482:         &Apache::lonmysql::drop_table($table);
                    483:     }
                    484:     #
1.57      matthew   485:     # Note - changes to this table must be reflected in the code that 
                    486:     # stores the data (calls &Apache::lonmysql::store_row with this table
                    487:     # id
                    488:     my $symb_table_def = {
                    489:         id => $symb_table,
                    490:         permanent => 'no',
                    491:         columns => [{ name => 'symb_id',
                    492:                       type => 'MEDIUMINT UNSIGNED',
                    493:                       restrictions => 'NOT NULL',
                    494:                       auto_inc     => 'yes', },
                    495:                     { name => 'symb',
                    496:                       type => 'MEDIUMTEXT',
                    497:                       restrictions => 'NOT NULL'},
                    498:                     ],
                    499:         'PRIMARY KEY' => ['symb_id'],
                    500:     };
                    501:     #
                    502:     my $part_table_def = {
                    503:         id => $part_table,
                    504:         permanent => 'no',
                    505:         columns => [{ name => 'part_id',
                    506:                       type => 'MEDIUMINT UNSIGNED',
                    507:                       restrictions => 'NOT NULL',
                    508:                       auto_inc     => 'yes', },
                    509:                     { name => 'part',
                    510:                       type => 'VARCHAR(100)',
                    511:                       restrictions => 'NOT NULL'},
                    512:                     ],
                    513:         'PRIMARY KEY' => ['part (100)'],
                    514:         'KEY' => [{ columns => ['part_id']},],
                    515:     };
                    516:     #
                    517:     my $student_table_def = {
                    518:         id => $student_table,
                    519:         permanent => 'no',
                    520:         columns => [{ name => 'student_id',
                    521:                       type => 'MEDIUMINT UNSIGNED',
                    522:                       restrictions => 'NOT NULL',
                    523:                       auto_inc     => 'yes', },
                    524:                     { name => 'student',
                    525:                       type => 'VARCHAR(100)',
                    526:                       restrictions => 'NOT NULL'},
                    527:                     ],
                    528:         'PRIMARY KEY' => ['student (100)'],
                    529:         'KEY' => [{ columns => ['student_id']},],
                    530:     };
                    531:     #
                    532:     my $updatetime_table_def = {
                    533:         id => $updatetime_table,
                    534:         permanent => 'no',
                    535:         columns => [{ name => 'student',
                    536:                       type => 'VARCHAR(100)',
                    537:                       restrictions => 'NOT NULL UNIQUE',},
                    538:                     { name => 'updatetime',
                    539:                       type => 'INT UNSIGNED',
                    540:                       restrictions => 'NOT NULL' },
                    541:                     ],
                    542:         'PRIMARY KEY' => ['student (100)'],
                    543:     };
                    544:     #
                    545:     my $performance_table_def = {
                    546:         id => $performance_table,
                    547:         permanent => 'no',
                    548:         columns => [{ name => 'symb_id',
                    549:                       type => 'MEDIUMINT UNSIGNED',
                    550:                       restrictions => 'NOT NULL'  },
                    551:                     { name => 'student_id',
                    552:                       type => 'MEDIUMINT UNSIGNED',
                    553:                       restrictions => 'NOT NULL'  },
                    554:                     { name => 'part_id',
                    555:                       type => 'MEDIUMINT UNSIGNED',
                    556:                       restrictions => 'NOT NULL' },
1.73      matthew   557:                     { name => 'part',
                    558:                       type => 'VARCHAR(100)',
                    559:                       restrictions => 'NOT NULL'},                    
1.57      matthew   560:                     { name => 'solved',
                    561:                       type => 'TINYTEXT' },
                    562:                     { name => 'tries',
                    563:                       type => 'SMALLINT UNSIGNED' },
                    564:                     { name => 'awarded',
                    565:                       type => 'TINYTEXT' },
                    566:                     { name => 'award',
                    567:                       type => 'TINYTEXT' },
                    568:                     { name => 'awarddetail',
                    569:                       type => 'TINYTEXT' },
                    570:                     { name => 'timestamp',
                    571:                       type => 'INT UNSIGNED'},
                    572:                     ],
                    573:         'PRIMARY KEY' => ['symb_id','student_id','part_id'],
                    574:         'KEY' => [{ columns=>['student_id'] },
                    575:                   { columns=>['symb_id'] },],
                    576:     };
                    577:     #
                    578:     my $parameters_table_def = {
                    579:         id => $parameters_table,
                    580:         permanent => 'no',
                    581:         columns => [{ name => 'symb_id',
                    582:                       type => 'MEDIUMINT UNSIGNED',
                    583:                       restrictions => 'NOT NULL'  },
                    584:                     { name => 'student_id',
                    585:                       type => 'MEDIUMINT UNSIGNED',
                    586:                       restrictions => 'NOT NULL'  },
                    587:                     { name => 'parameter',
                    588:                       type => 'TINYTEXT',
                    589:                       restrictions => 'NOT NULL'  },
                    590:                     { name => 'value',
                    591:                       type => 'MEDIUMTEXT' },
                    592:                     ],
                    593:         'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
                    594:     };
                    595:     #
                    596:     # Create the tables
                    597:     my $tableid;
                    598:     $tableid = &Apache::lonmysql::create_table($symb_table_def);
                    599:     if (! defined($tableid)) {
                    600:         &Apache::lonnet::logthis("error creating symb_table: ".
                    601:                                  &Apache::lonmysql::get_error());
                    602:         return 1;
                    603:     }
                    604:     #
                    605:     $tableid = &Apache::lonmysql::create_table($part_table_def);
                    606:     if (! defined($tableid)) {
                    607:         &Apache::lonnet::logthis("error creating part_table: ".
                    608:                                  &Apache::lonmysql::get_error());
                    609:         return 2;
                    610:     }
                    611:     #
                    612:     $tableid = &Apache::lonmysql::create_table($student_table_def);
                    613:     if (! defined($tableid)) {
                    614:         &Apache::lonnet::logthis("error creating student_table: ".
                    615:                                  &Apache::lonmysql::get_error());
                    616:         return 3;
                    617:     }
                    618:     #
                    619:     $tableid = &Apache::lonmysql::create_table($updatetime_table_def);
                    620:     if (! defined($tableid)) {
                    621:         &Apache::lonnet::logthis("error creating updatetime_table: ".
                    622:                                  &Apache::lonmysql::get_error());
                    623:         return 4;
                    624:     }
                    625:     #
                    626:     $tableid = &Apache::lonmysql::create_table($performance_table_def);
                    627:     if (! defined($tableid)) {
                    628:         &Apache::lonnet::logthis("error creating preformance_table: ".
                    629:                                  &Apache::lonmysql::get_error());
                    630:         return 5;
                    631:     }
                    632:     #
                    633:     $tableid = &Apache::lonmysql::create_table($parameters_table_def);
                    634:     if (! defined($tableid)) {
                    635:         &Apache::lonnet::logthis("error creating parameters_table: ".
                    636:                                  &Apache::lonmysql::get_error());
                    637:         return 6;
                    638:     }
                    639:     return 0;
1.70      matthew   640: }
                    641: 
                    642: ################################################
                    643: ################################################
                    644: 
                    645: =pod
                    646: 
                    647: =item &delete_caches()
                    648: 
                    649: =cut
                    650: 
                    651: ################################################
                    652: ################################################
                    653: sub delete_caches {
                    654:     my $courseid = shift;
                    655:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                    656:     #
                    657:     &setup_table_names($courseid);
                    658:     #
                    659:     my $dbh = &Apache::lonmysql::get_dbh();
                    660:     foreach my $table ($symb_table,$part_table,$student_table,
                    661:                        $updatetime_table,$performance_table,
                    662:                        $parameters_table ){
                    663:         my $command = 'DROP TABLE '.$table.';';
                    664:         $dbh->do($command);
                    665:         if ($dbh->err) {
                    666:             &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
                    667:         }
                    668:     }
                    669:     return;
1.57      matthew   670: }
                    671: 
                    672: ################################################
                    673: ################################################
                    674: 
                    675: =pod
                    676: 
                    677: =item &get_part_id()
                    678: 
                    679: Get the MySQL id of a problem part string.
                    680: 
                    681: Input: $part
                    682: 
                    683: Output: undef on error, integer $part_id on success.
                    684: 
                    685: =item &get_part()
                    686: 
                    687: Get the string describing a part from the MySQL id of the problem part.
                    688: 
                    689: Input: $part_id
                    690: 
                    691: Output: undef on error, $part string on success.
                    692: 
                    693: =cut
                    694: 
                    695: ################################################
                    696: ################################################
                    697: 
1.61      matthew   698: my $have_read_part_table = 0;
1.57      matthew   699: my %ids_by_part;
                    700: my %parts_by_id;
                    701: 
                    702: sub get_part_id {
                    703:     my ($part) = @_;
1.61      matthew   704:     $part = 0 if (! defined($part));
                    705:     if (! $have_read_part_table) {
                    706:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    707:         foreach (@Result) {
                    708:             $ids_by_part{$_->[1]}=$_->[0];
                    709:         }
                    710:         $have_read_part_table = 1;
                    711:     }
1.57      matthew   712:     if (! exists($ids_by_part{$part})) {
                    713:         &Apache::lonmysql::store_row($part_table,[undef,$part]);
                    714:         undef(%ids_by_part);
                    715:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    716:         foreach (@Result) {
                    717:             $ids_by_part{$_->[1]}=$_->[0];
                    718:         }
                    719:     }
                    720:     return $ids_by_part{$part} if (exists($ids_by_part{$part}));
                    721:     return undef; # error
                    722: }
                    723: 
                    724: sub get_part {
                    725:     my ($part_id) = @_;
                    726:     if (! exists($parts_by_id{$part_id})  || 
                    727:         ! defined($parts_by_id{$part_id}) ||
                    728:         $parts_by_id{$part_id} eq '') {
                    729:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    730:         foreach (@Result) {
                    731:             $parts_by_id{$_->[0]}=$_->[1];
                    732:         }
                    733:     }
                    734:     return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
                    735:     return undef; # error
                    736: }
                    737: 
                    738: ################################################
                    739: ################################################
                    740: 
                    741: =pod
                    742: 
                    743: =item &get_symb_id()
                    744: 
                    745: Get the MySQL id of a symb.
                    746: 
                    747: Input: $symb
                    748: 
                    749: Output: undef on error, integer $symb_id on success.
                    750: 
                    751: =item &get_symb()
                    752: 
                    753: Get the symb associated with a MySQL symb_id.
                    754: 
                    755: Input: $symb_id
                    756: 
                    757: Output: undef on error, $symb on success.
                    758: 
                    759: =cut
                    760: 
                    761: ################################################
                    762: ################################################
                    763: 
1.61      matthew   764: my $have_read_symb_table = 0;
1.57      matthew   765: my %ids_by_symb;
                    766: my %symbs_by_id;
                    767: 
                    768: sub get_symb_id {
                    769:     my ($symb) = @_;
1.61      matthew   770:     if (! $have_read_symb_table) {
                    771:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    772:         foreach (@Result) {
                    773:             $ids_by_symb{$_->[1]}=$_->[0];
                    774:         }
                    775:         $have_read_symb_table = 1;
                    776:     }
1.57      matthew   777:     if (! exists($ids_by_symb{$symb})) {
                    778:         &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
                    779:         undef(%ids_by_symb);
                    780:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    781:         foreach (@Result) {
                    782:             $ids_by_symb{$_->[1]}=$_->[0];
                    783:         }
                    784:     }
                    785:     return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
                    786:     return undef; # error
                    787: }
                    788: 
                    789: sub get_symb {
                    790:     my ($symb_id) = @_;
                    791:     if (! exists($symbs_by_id{$symb_id})  || 
                    792:         ! defined($symbs_by_id{$symb_id}) ||
                    793:         $symbs_by_id{$symb_id} eq '') {
                    794:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    795:         foreach (@Result) {
                    796:             $symbs_by_id{$_->[0]}=$_->[1];
                    797:         }
                    798:     }
                    799:     return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
                    800:     return undef; # error
                    801: }
                    802: 
                    803: ################################################
                    804: ################################################
                    805: 
                    806: =pod
                    807: 
                    808: =item &get_student_id()
                    809: 
                    810: Get the MySQL id of a student.
                    811: 
                    812: Input: $sname, $dom
                    813: 
                    814: Output: undef on error, integer $student_id on success.
                    815: 
                    816: =item &get_student()
                    817: 
                    818: Get student username:domain associated with the MySQL student_id.
                    819: 
                    820: Input: $student_id
                    821: 
                    822: Output: undef on error, string $student (username:domain) on success.
                    823: 
                    824: =cut
                    825: 
                    826: ################################################
                    827: ################################################
                    828: 
1.61      matthew   829: my $have_read_student_table = 0;
1.57      matthew   830: my %ids_by_student;
                    831: my %students_by_id;
                    832: 
                    833: sub get_student_id {
                    834:     my ($sname,$sdom) = @_;
                    835:     my $student = $sname.':'.$sdom;
1.61      matthew   836:     if (! $have_read_student_table) {
                    837:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    838:         foreach (@Result) {
                    839:             $ids_by_student{$_->[1]}=$_->[0];
                    840:         }
                    841:         $have_read_student_table = 1;
                    842:     }
1.57      matthew   843:     if (! exists($ids_by_student{$student})) {
                    844:         &Apache::lonmysql::store_row($student_table,[undef,$student]);
                    845:         undef(%ids_by_student);
                    846:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    847:         foreach (@Result) {
                    848:             $ids_by_student{$_->[1]}=$_->[0];
                    849:         }
                    850:     }
                    851:     return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
                    852:     return undef; # error
                    853: }
                    854: 
                    855: sub get_student {
                    856:     my ($student_id) = @_;
                    857:     if (! exists($students_by_id{$student_id})  || 
                    858:         ! defined($students_by_id{$student_id}) ||
                    859:         $students_by_id{$student_id} eq '') {
                    860:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    861:         foreach (@Result) {
                    862:             $students_by_id{$_->[0]}=$_->[1];
                    863:         }
                    864:     }
                    865:     return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
                    866:     return undef; # error
                    867: }
                    868: 
                    869: ################################################
                    870: ################################################
                    871: 
                    872: =pod
                    873: 
                    874: =item &update_student_data()
                    875: 
                    876: Input: $sname, $sdom, $courseid
                    877: 
                    878: Output: $returnstatus, \%student_data
                    879: 
                    880: $returnstatus is a string describing any errors that occured.  'okay' is the
                    881: default.
                    882: \%student_data is the data returned by a call to lonnet::currentdump.
                    883: 
                    884: This subroutine loads a students data using lonnet::currentdump and inserts
                    885: it into the MySQL database.  The inserts are done on two tables, 
                    886: $performance_table and $parameters_table.  $parameters_table holds the data 
                    887: that is not included in $performance_table.  See the description of 
                    888: $performance_table elsewhere in this file.  The INSERT calls are made
                    889: directly by this subroutine, not through lonmysql because we do a 'bulk'
                    890: insert which takes advantage of MySQLs non-SQL compliant INSERT command to 
                    891: insert multiple rows at a time.  If anything has gone wrong during this
                    892: process, $returnstatus is updated with a description of the error and
                    893: \%student_data is returned.  
                    894: 
                    895: Notice we do not insert the data and immediately query it.  This means it
                    896: is possible for there to be data returned this first time that is not 
                    897: available the second time.  CYA.
                    898: 
                    899: =cut
                    900: 
                    901: ################################################
                    902: ################################################
                    903: sub update_student_data {
                    904:     my ($sname,$sdom,$courseid) = @_;
                    905:     #
1.60      matthew   906:     # Set up database names
                    907:     &setup_table_names($courseid);
                    908:     #
1.57      matthew   909:     my $student_id = &get_student_id($sname,$sdom);
                    910:     my $student = $sname.':'.$sdom;
                    911:     #
                    912:     my $returnstatus = 'okay';
                    913:     #
                    914:     # Download students data
                    915:     my $time_of_retrieval = time;
                    916:     my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
                    917:     if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
                    918:         &Apache::lonnet::logthis('error getting data for '.
                    919:                                  $sname.':'.$sdom.' in course '.$courseid.
                    920:                                  ':'.$tmp[0]);
                    921:         $returnstatus = 'error getting data';
1.79      matthew   922:         return ($returnstatus,undef);
1.57      matthew   923:     }
                    924:     if (scalar(@tmp) < 1) {
                    925:         return ('no data',undef);
                    926:     }
                    927:     my %student_data = @tmp;
                    928:     #
                    929:     # Remove all of the students data from the table
1.60      matthew   930:     my $dbh = &Apache::lonmysql::get_dbh();
                    931:     $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
                    932:              $student_id);
                    933:     $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
                    934:              $student_id);
1.57      matthew   935:     #
                    936:     # Store away the data
                    937:     #
                    938:     my $starttime = Time::HiRes::time;
                    939:     my $elapsed = 0;
                    940:     my $rows_stored;
                    941:     my $store_parameters_command  = 'INSERT INTO '.$parameters_table.
1.60      matthew   942:         ' VALUES '."\n";
1.61      matthew   943:     my $num_parameters = 0;
1.57      matthew   944:     my $store_performance_command = 'INSERT INTO '.$performance_table.
1.60      matthew   945:         ' VALUES '."\n";
1.79      matthew   946:     return ('error',undef) if (! defined($dbh));
1.57      matthew   947:     while (my ($current_symb,$param_hash) = each(%student_data)) {
                    948:         #
                    949:         # make sure the symb is set up properly
                    950:         my $symb_id = &get_symb_id($current_symb);
                    951:         #
                    952:         # Load data into the tables
1.63      matthew   953:         while (my ($parameter,$value) = each(%$param_hash)) {
1.57      matthew   954:             my $newstring;
1.63      matthew   955:             if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.57      matthew   956:                 $newstring = "('".join("','",
                    957:                                        $symb_id,$student_id,
1.69      matthew   958:                                        $parameter)."',".
                    959:                                            $dbh->quote($value)."),\n";
1.61      matthew   960:                 $num_parameters ++;
1.57      matthew   961:                 if ($newstring !~ /''/) {
                    962:                     $store_parameters_command .= $newstring;
                    963:                     $rows_stored++;
                    964:                 }
                    965:             }
                    966:             next if ($parameter !~ /^resource\.(.*)\.solved$/);
                    967:             #
                    968:             my $part = $1;
                    969:             my $part_id = &get_part_id($part);
                    970:             next if (!defined($part_id));
                    971:             my $solved  = $value;
                    972:             my $tries   = $param_hash->{'resource.'.$part.'.tries'};
                    973:             my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
                    974:             my $award   = $param_hash->{'resource.'.$part.'.award'};
                    975:             my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
                    976:             my $timestamp = $param_hash->{'timestamp'};
1.60      matthew   977:             #
1.74      matthew   978:             $solved      = '' if (! defined($solved));
1.57      matthew   979:             $tries       = '' if (! defined($tries));
                    980:             $awarded     = '' if (! defined($awarded));
                    981:             $award       = '' if (! defined($award));
                    982:             $awarddetail = '' if (! defined($awarddetail));
1.73      matthew   983:             $newstring = "('".join("','",$symb_id,$student_id,$part_id,$part,
1.57      matthew   984:                                    $solved,$tries,$awarded,$award,
1.63      matthew   985:                                    $awarddetail,$timestamp)."'),\n";
1.57      matthew   986:             $store_performance_command .= $newstring;
                    987:             $rows_stored++;
                    988:         }
                    989:     }
                    990:     chop $store_parameters_command;
1.60      matthew   991:     chop $store_parameters_command;
                    992:     chop $store_performance_command;
1.57      matthew   993:     chop $store_performance_command;
                    994:     my $start = Time::HiRes::time;
1.61      matthew   995:     $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57      matthew   996:     if ($dbh->err()) {
                    997:         &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61      matthew   998:         &Apache::lonnet::logthis('command = '.$store_parameters_command);
1.57      matthew   999:         $returnstatus = 'error: unable to insert parameters into database';
1.79      matthew  1000:         return ($returnstatus,\%student_data);
1.57      matthew  1001:     }
                   1002:     $dbh->do($store_performance_command);
                   1003:     if ($dbh->err()) {
                   1004:         &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61      matthew  1005:         &Apache::lonnet::logthis('command = '.$store_performance_command);
1.57      matthew  1006:         $returnstatus = 'error: unable to insert performance into database';
1.79      matthew  1007:         return ($returnstatus,\%student_data);
1.57      matthew  1008:     }
                   1009:     $elapsed += Time::HiRes::time - $start;
                   1010:     #
                   1011:     # Set the students update time
                   1012:     &Apache::lonmysql::replace_row($updatetime_table,
                   1013:                                    [$student,$time_of_retrieval]);
                   1014:     return ($returnstatus,\%student_data);
                   1015: }
                   1016: 
                   1017: ################################################
                   1018: ################################################
                   1019: 
                   1020: =pod
                   1021: 
                   1022: =item &ensure_current_data()
                   1023: 
                   1024: Input: $sname, $sdom, $courseid
                   1025: 
                   1026: Output: $status, $data
                   1027: 
                   1028: This routine ensures the data for a given student is up to date.  It calls
                   1029: &init_dbs() if the tables do not exist.  The $updatetime_table is queried
                   1030: to determine the time of the last update.  If the students data is out of
                   1031: date, &update_student_data() is called.  The return values from the call
                   1032: to &update_student_data() are returned.
                   1033: 
                   1034: =cut
                   1035: 
                   1036: ################################################
                   1037: ################################################
                   1038: sub ensure_current_data {
                   1039:     my ($sname,$sdom,$courseid) = @_;
                   1040:     my $status = 'okay';   # return value
                   1041:     #
1.61      matthew  1042:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   1043:     # 
                   1044:     # Clean out package variables
1.57      matthew  1045:     &setup_table_names($courseid);
                   1046:     #
                   1047:     # if the tables do not exist, make them
                   1048:     my @CurrentTable = &Apache::lonmysql::tables_in_db();
                   1049:     my ($found_symb,$found_student,$found_part,$found_update,
                   1050:         $found_performance,$found_parameters);
                   1051:     foreach (@CurrentTable) {
                   1052:         $found_symb        = 1 if ($_ eq $symb_table);
                   1053:         $found_student     = 1 if ($_ eq $student_table);
                   1054:         $found_part        = 1 if ($_ eq $part_table);
                   1055:         $found_update      = 1 if ($_ eq $updatetime_table);
                   1056:         $found_performance = 1 if ($_ eq $performance_table);
                   1057:         $found_parameters  = 1 if ($_ eq $parameters_table);
                   1058:     }
                   1059:     if (!$found_symb        || !$found_update || 
                   1060:         !$found_student     || !$found_part   ||
                   1061:         !$found_performance || !$found_parameters) {
                   1062:         if (&init_dbs($courseid)) {
1.79      matthew  1063:             return ('error',undef);
1.57      matthew  1064:         }
                   1065:     }
                   1066:     #
                   1067:     # Get the update time for the user
                   1068:     my $updatetime = 0;
1.60      matthew  1069:     my $modifiedtime = &Apache::lonnet::GetFileTimestamp
                   1070:         ($sdom,$sname,$courseid.'.db',
                   1071:          $Apache::lonnet::perlvar{'lonUsersDir'});
1.57      matthew  1072:     #
                   1073:     my $student = $sname.':'.$sdom;
                   1074:     my @Result = &Apache::lonmysql::get_rows($updatetime_table,
                   1075:                                              "student ='$student'");
                   1076:     my $data = undef;
                   1077:     if (@Result) {
                   1078:         $updatetime = $Result[0]->[1];
                   1079:     }
                   1080:     if ($modifiedtime > $updatetime) {
                   1081:         ($status,$data) = &update_student_data($sname,$sdom,$courseid);
                   1082:     }
                   1083:     return ($status,$data);
                   1084: }
                   1085: 
                   1086: ################################################
                   1087: ################################################
                   1088: 
                   1089: =pod
                   1090: 
                   1091: =item &get_student_data_from_performance_cache()
                   1092: 
                   1093: Input: $sname, $sdom, $symb, $courseid
                   1094: 
                   1095: Output: hash reference containing the data for the given student.
                   1096: If $symb is undef, all the students data is returned.
                   1097: 
                   1098: This routine is the heart of the local caching system.  See the description
                   1099: of $performance_table, $symb_table, $student_table, and $part_table.  The
                   1100: main task is building the MySQL request.  The tables appear in the request
                   1101: in the order in which they should be parsed by MySQL.  When searching
                   1102: on a student the $student_table is used to locate the 'student_id'.  All
                   1103: rows in $performance_table which have a matching 'student_id' are returned,
                   1104: with data from $part_table and $symb_table which match the entries in
                   1105: $performance_table, 'part_id' and 'symb_id'.  When searching on a symb,
                   1106: the $symb_table is processed first, with matching rows grabbed from 
                   1107: $performance_table and filled in from $part_table and $student_table in
                   1108: that order.  
                   1109: 
                   1110: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite 
                   1111: interesting, especially if you play with the order the tables are listed.  
                   1112: 
                   1113: =cut
                   1114: 
                   1115: ################################################
                   1116: ################################################
                   1117: sub get_student_data_from_performance_cache {
                   1118:     my ($sname,$sdom,$symb,$courseid)=@_;
                   1119:     my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61      matthew  1120:     &setup_table_names($courseid);
1.57      matthew  1121:     #
                   1122:     # Return hash
                   1123:     my $studentdata;
                   1124:     #
                   1125:     my $dbh = &Apache::lonmysql::get_dbh();
                   1126:     my $request = "SELECT ".
1.73      matthew  1127:         "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63      matthew  1128:             "a.timestamp ";
1.57      matthew  1129:     if (defined($student)) {
                   1130:         $request .= "FROM $student_table AS b ".
                   1131:             "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
1.73      matthew  1132: #            "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57      matthew  1133:             "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
                   1134:                 "WHERE student='$student'";
                   1135:         if (defined($symb) && $symb ne '') {
1.67      matthew  1136:             $request .= " AND d.symb=".$dbh->quote($symb);
1.57      matthew  1137:         }
                   1138:     } elsif (defined($symb) && $symb ne '') {
                   1139:         $request .= "FROM $symb_table as d ".
                   1140:             "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
1.73      matthew  1141: #            "LEFT JOIN $part_table    AS c ON c.part_id = a.part_id ".
1.57      matthew  1142:             "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
                   1143:                 "WHERE symb='".$dbh->quote($symb)."'";
                   1144:     }
                   1145:     my $starttime = Time::HiRes::time;
                   1146:     my $rows_retrieved = 0;
                   1147:     my $sth = $dbh->prepare($request);
                   1148:     $sth->execute();
                   1149:     if ($sth->err()) {
                   1150:         &Apache::lonnet::logthis("Unable to execute MySQL request:");
                   1151:         &Apache::lonnet::logthis("\n".$request."\n");
                   1152:         &Apache::lonnet::logthis("error is:".$sth->errstr());
                   1153:         return undef;
                   1154:     }
                   1155:     foreach my $row (@{$sth->fetchall_arrayref}) {
                   1156:         $rows_retrieved++;
1.63      matthew  1157:         my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) = 
1.57      matthew  1158:             (@$row);
                   1159:         my $base = 'resource.'.$part;
                   1160:         $studentdata->{$symb}->{$base.'.solved'}  = $solved;
                   1161:         $studentdata->{$symb}->{$base.'.tries'}   = $tries;
                   1162:         $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
                   1163:         $studentdata->{$symb}->{$base.'.award'}   = $award;
                   1164:         $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
                   1165:         $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67      matthew  1166:     }
                   1167:     if (defined($symb) && $symb ne '') {
                   1168:         $studentdata = $studentdata->{$symb};
1.57      matthew  1169:     }
                   1170:     return $studentdata;
                   1171: }
                   1172: 
                   1173: ################################################
                   1174: ################################################
                   1175: 
                   1176: =pod
                   1177: 
                   1178: =item &get_current_state()
                   1179: 
                   1180: Input: $sname,$sdom,$symb,$courseid
                   1181: 
                   1182: Output: Described below
1.46      matthew  1183: 
1.47      matthew  1184: Retrieve the current status of a students performance.  $sname and
1.46      matthew  1185: $sdom are the only required parameters.  If $symb is undef the results
1.47      matthew  1186: of an &Apache::lonnet::currentdump() will be returned.  
1.46      matthew  1187: If $courseid is undef it will be retrieved from the environment.
                   1188: 
                   1189: The return structure is based on &Apache::lonnet::currentdump.  If
                   1190: $symb is unspecified, all the students data is returned in a hash of
                   1191: the form:
                   1192: ( 
                   1193:   symb1 => { param1 => value1, param2 => value2 ... },
                   1194:   symb2 => { param1 => value1, param2 => value2 ... },
                   1195: )
                   1196: 
                   1197: If $symb is specified, a hash of 
                   1198: (
                   1199:   param1 => value1, 
                   1200:   param2 => value2,
                   1201: )
                   1202: is returned.
                   1203: 
1.57      matthew  1204: If no data is found for $symb, or if the student has no performance data,
1.46      matthew  1205: an empty list is returned.
                   1206: 
                   1207: =cut
                   1208: 
                   1209: ################################################
                   1210: ################################################
                   1211: sub get_current_state {
1.47      matthew  1212:     my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
                   1213:     #
1.46      matthew  1214:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47      matthew  1215:     #
1.61      matthew  1216:     return () if (! defined($sname) || ! defined($sdom));
                   1217:     #
1.57      matthew  1218:     my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77      matthew  1219: #    &Apache::lonnet::logthis
                   1220: #        ('sname = '.$sname.
                   1221: #         ' domain = '.$sdom.
                   1222: #         ' status = '.$status.
                   1223: #         ' data is '.(defined($data)?'defined':'undefined'));
1.73      matthew  1224: #    while (my ($symb,$hash) = each(%$data)) {
                   1225: #        &Apache::lonnet::logthis($symb."\n----------------------------------");
                   1226: #        while (my ($key,$value) = each (%$hash)) {
                   1227: #            &Apache::lonnet::logthis("   ".$key." = ".$value);
                   1228: #        }
                   1229: #    }
1.47      matthew  1230:     #
1.79      matthew  1231:     if (defined($data) && defined($symb) && ref($data->{$symb})) {
                   1232:         return %{$data->{$symb}};
                   1233:     } elsif (defined($data) && ! defined($symb) && ref($data)) {
                   1234:         return %$data;
                   1235:     } 
                   1236:     if ($status eq 'no data') {
1.57      matthew  1237:         return ();
                   1238:     } else {
                   1239:         if ($status ne 'okay' && $status ne '') {
                   1240:             &Apache::lonnet::logthis('status = '.$status);
1.47      matthew  1241:             return ();
                   1242:         }
1.57      matthew  1243:         my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
                   1244:                                                       $symb,$courseid);
                   1245:         return %$returnhash if (defined($returnhash));
1.46      matthew  1246:     }
1.57      matthew  1247:     return ();
1.61      matthew  1248: }
                   1249: 
                   1250: ################################################
                   1251: ################################################
                   1252: 
                   1253: =pod
                   1254: 
                   1255: =item &get_problem_statistics()
                   1256: 
                   1257: Gather data on a given problem.  The database is assumed to be 
                   1258: populated and all local caching variables are assumed to be set
                   1259: properly.  This means you need to call &ensure_current_data for
                   1260: the students you are concerned with prior to calling this routine.
                   1261: 
                   1262: Inputs: $students, $symb, $part, $courseid
                   1263: 
1.64      matthew  1264: =over 4
                   1265: 
                   1266: =item $students is an array of hash references.  
                   1267: Each hash must contain at least the 'username' and 'domain' of a student.
                   1268: 
                   1269: =item $symb is the symb for the problem.
                   1270: 
                   1271: =item $part is the part id you need statistics for
                   1272: 
                   1273: =item $courseid is the course id, of course!
                   1274: 
                   1275: =back
                   1276: 
1.66      matthew  1277: Outputs: See the code for up to date information.  A hash reference is
                   1278: returned.  The hash has the following keys defined:
1.64      matthew  1279: 
                   1280: =over 4
                   1281: 
1.66      matthew  1282: =item num_students The number of students attempting the problem
                   1283:       
                   1284: =item tries The total number of tries for the students
                   1285:       
                   1286: =item max_tries The maximum number of tries taken
                   1287:       
                   1288: =item mean_tries The average number of tries
                   1289:       
                   1290: =item num_solved The number of students able to solve the problem
                   1291:       
                   1292: =item num_override The number of students whose answer is 'correct_by_override'
                   1293:       
                   1294: =item deg_of_diff The degree of difficulty of the problem
                   1295:       
                   1296: =item std_tries The standard deviation of the number of tries
                   1297:       
                   1298: =item skew_tries The skew of the number of tries
1.64      matthew  1299: 
1.66      matthew  1300: =item per_wrong The number of students attempting the problem who were not
                   1301: able to answer it correctly.
1.64      matthew  1302: 
                   1303: =back
                   1304: 
1.61      matthew  1305: =cut
                   1306: 
                   1307: ################################################
                   1308: ################################################
                   1309: sub get_problem_statistics {
                   1310:     my ($students,$symb,$part,$courseid) = @_;
                   1311:     return if (! defined($symb) || ! defined($part));
                   1312:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   1313:     #
                   1314:     my $symb_id = &get_symb_id($symb);
                   1315:     my $part_id = &get_part_id($part);
                   1316:     my $stats_table = $courseid.'_problem_stats';
                   1317:     #
                   1318:     my $dbh = &Apache::lonmysql::get_dbh();
                   1319:     return undef if (! defined($dbh));
                   1320:     #
                   1321:     # A) Number of Students attempting problem
                   1322:     # B) Total number of tries of students attempting problem
                   1323:     # C) Mod (largest number of tries for solving the problem)
                   1324:     # D) Mean (average number of tries for solving the problem)
                   1325:     # E) Number of students to solve the problem
                   1326:     # F) Number of students to solve the problem by override
                   1327:     # G) Number of students unable to solve the problem
                   1328:     # H) Degree of difficulty : 1-(E+F)/B
                   1329:     # I) Standard deviation of number of tries
                   1330:     # J) Skew of tries: sqrt(sum(Xi-D)^3)/A
                   1331:     #
                   1332:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
                   1333:     my $request = 
                   1334:         'CREATE TEMPORARY TABLE '.$stats_table.
                   1335:             ' SELECT student_id,solved,award,tries FROM '.$performance_table.
                   1336:                 ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id;
1.64      matthew  1337:     if (defined($students)) {
                   1338:         $request .= ' AND ('.
                   1339:             join(' OR ', map {'student_id='.
                   1340:                                   &get_student_id($_->{'username'},
                   1341:                                                   $_->{'domain'})
                   1342:                                   } @$students
                   1343:                  ).')';
                   1344:     }
1.61      matthew  1345: #    &Apache::lonnet::logthis($request);
                   1346:     $dbh->do($request);
                   1347:     my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request
                   1348:         ($dbh,
                   1349:          'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) FROM '.
                   1350:          $stats_table);
                   1351:     my ($Solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1352:                                         $stats_table.
                   1353:                                         " WHERE solved='correct_by_student'");
                   1354:     my ($solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1355:                                         $stats_table.
                   1356:                                         " WHERE solved='correct_by_override'");
                   1357:     $num    = 0 if (! defined($num));
                   1358:     $tries  = 0 if (! defined($tries));
                   1359:     $mod    = 0 if (! defined($mod));
                   1360:     $STD    = 0 if (! defined($STD));
                   1361:     $Solved = 0 if (! defined($Solved));
                   1362:     $solved = 0 if (! defined($solved));
                   1363:     #
                   1364:     my $DegOfDiff = 'nan';
1.66      matthew  1365:     $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.61      matthew  1366: 
                   1367:     my $SKEW = 'nan';
1.66      matthew  1368:     my $wrongpercent = 0;
1.61      matthew  1369:     if ($num > 0) {
                   1370:         ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
                   1371:                                      'POWER(tries - '.$STD.',3)'.
                   1372:                                      '))/'.$num.' FROM '.$stats_table);
1.66      matthew  1373:         $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
1.61      matthew  1374:     }
                   1375:     #
                   1376:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
1.81      matthew  1377:     #
                   1378:     # Store in metadata
                   1379:     #
1.80      www      1380:     if ($num) {
                   1381: 	my %storestats=();
                   1382: 
                   1383:         my $urlres=(split(/\_\_\_/,$symb))[2];
                   1384: 
                   1385: 	$storestats{$courseid.'___'.$urlres.'___timestamp'}=time;       
                   1386: 	$storestats{$courseid.'___'.$urlres.'___stdno'}=$num;
                   1387: 	$storestats{$courseid.'___'.$urlres.'___avetries'}=$mean;	   
                   1388: 	$storestats{$courseid.'___'.$urlres.'___difficulty'}=$DegOfDiff;
                   1389: 
                   1390: 	$urlres=~/^(\w+)\/(\w+)/; 
                   1391: 	&Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2); 
                   1392:     }
1.81      matthew  1393:     #
                   1394:     # Return result
                   1395:     #
1.66      matthew  1396:     return { num_students => $num,
                   1397:              tries        => $tries,
                   1398:              max_tries    => $mod,
                   1399:              mean_tries   => $mean,
                   1400:              std_tries    => $STD,
                   1401:              skew_tries   => $SKEW,
                   1402:              num_solved   => $Solved,
                   1403:              num_override => $solved,
                   1404:              per_wrong    => $wrongpercent,
1.81      matthew  1405:              deg_of_diff  => $DegOfDiff };
1.61      matthew  1406: }
                   1407: 
                   1408: sub execute_SQL_request {
                   1409:     my ($dbh,$request)=@_;
                   1410: #    &Apache::lonnet::logthis($request);
                   1411:     my $sth = $dbh->prepare($request);
                   1412:     $sth->execute();
                   1413:     my $row = $sth->fetchrow_arrayref();
                   1414:     if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
                   1415:         return @$row;
                   1416:     }
                   1417:     return ();
                   1418: }
                   1419: 
                   1420: 
                   1421: ################################################
                   1422: ################################################
                   1423: 
                   1424: =pod
                   1425: 
                   1426: =item &setup_table_names()
                   1427: 
                   1428: input: course id
                   1429: 
                   1430: output: none
                   1431: 
                   1432: Cleans up the package variables for local caching.
                   1433: 
                   1434: =cut
                   1435: 
                   1436: ################################################
                   1437: ################################################
                   1438: sub setup_table_names {
                   1439:     my ($courseid) = @_;
                   1440:     if (! defined($courseid)) {
                   1441:         $courseid = $ENV{'request.course.id'};
                   1442:     }
                   1443:     #
                   1444:     if (! defined($current_course) || $current_course ne $courseid) {
                   1445:         # Clear out variables
                   1446:         $have_read_part_table = 0;
                   1447:         undef(%ids_by_part);
                   1448:         undef(%parts_by_id);
                   1449:         $have_read_symb_table = 0;
                   1450:         undef(%ids_by_symb);
                   1451:         undef(%symbs_by_id);
                   1452:         $have_read_student_table = 0;
                   1453:         undef(%ids_by_student);
                   1454:         undef(%students_by_id);
                   1455:         #
                   1456:         $current_course = $courseid;
                   1457:     }
                   1458:     #
                   1459:     # Set up database names
                   1460:     my $base_id = $courseid;
                   1461:     $symb_table        = $base_id.'_'.'symb';
                   1462:     $part_table        = $base_id.'_'.'part';
                   1463:     $student_table     = $base_id.'_'.'student';
                   1464:     $updatetime_table  = $base_id.'_'.'updatetime';
                   1465:     $performance_table = $base_id.'_'.'performance';
                   1466:     $parameters_table  = $base_id.'_'.'parameters';
                   1467:     return;
1.3       stredwic 1468: }
1.1       stredwic 1469: 
1.35      matthew  1470: ################################################
                   1471: ################################################
                   1472: 
                   1473: =pod
                   1474: 
1.57      matthew  1475: =back
                   1476: 
                   1477: =item End of Local Data Caching Subroutines
                   1478: 
                   1479: =cut
                   1480: 
                   1481: ################################################
                   1482: ################################################
                   1483: 
                   1484: 
                   1485: }
                   1486: ################################################
                   1487: ################################################
                   1488: 
                   1489: =pod
                   1490: 
                   1491: =head3 Classlist Subroutines
                   1492: 
1.35      matthew  1493: =item &get_classlist();
                   1494: 
                   1495: Retrieve the classist of a given class or of the current class.  Student
                   1496: information is returned from the classlist.db file and, if needed,
                   1497: from the students environment.
                   1498: 
                   1499: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
                   1500: and course number, respectively).  Any omitted arguments will be taken 
                   1501: from the current environment ($ENV{'request.course.id'},
                   1502: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
                   1503: 
                   1504: Returns a reference to a hash which contains:
                   1505:  keys    '$sname:$sdom'
1.54      bowersj2 1506:  values  [$sdom,$sname,$end,$start,$id,$section,$fullname,$status]
                   1507: 
                   1508: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
                   1509: as indices into the returned list to future-proof clients against
                   1510: changes in the list order.
1.35      matthew  1511: 
                   1512: =cut
                   1513: 
                   1514: ################################################
                   1515: ################################################
1.54      bowersj2 1516: 
                   1517: sub CL_SDOM     { return 0; }
                   1518: sub CL_SNAME    { return 1; }
                   1519: sub CL_END      { return 2; }
                   1520: sub CL_START    { return 3; }
                   1521: sub CL_ID       { return 4; }
                   1522: sub CL_SECTION  { return 5; }
                   1523: sub CL_FULLNAME { return 6; }
                   1524: sub CL_STATUS   { return 7; }
1.35      matthew  1525: 
                   1526: sub get_classlist {
                   1527:     my ($cid,$cdom,$cnum) = @_;
                   1528:     $cid = $cid || $ENV{'request.course.id'};
                   1529:     $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
                   1530:     $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57      matthew  1531:     my $now = time;
1.35      matthew  1532:     #
                   1533:     my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
                   1534:     while (my ($student,$info) = each(%classlist)) {
1.60      matthew  1535:         if ($student =~ /^(con_lost|error|no_such_host)/i) {
                   1536:             &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
                   1537:             return undef;
                   1538:         }
1.35      matthew  1539:         my ($sname,$sdom) = split(/:/,$student);
                   1540:         my @Values = split(/:/,$info);
                   1541:         my ($end,$start,$id,$section,$fullname);
                   1542:         if (@Values > 2) {
                   1543:             ($end,$start,$id,$section,$fullname) = @Values;
                   1544:         } else { # We have to get the data ourselves
                   1545:             ($end,$start) = @Values;
1.37      matthew  1546:             $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35      matthew  1547:             my %info=&Apache::lonnet::get('environment',
                   1548:                                           ['firstname','middlename',
                   1549:                                            'lastname','generation','id'],
                   1550:                                           $sdom, $sname);
                   1551:             my ($tmp) = keys(%info);
                   1552:             if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   1553:                 $fullname = 'not available';
                   1554:                 $id = 'not available';
1.38      matthew  1555:                 &Apache::lonnet::logthis('unable to retrieve environment '.
                   1556:                                          'for '.$sname.':'.$sdom);
1.35      matthew  1557:             } else {
                   1558:                 $fullname = &ProcessFullName(@info{qw/lastname generation 
                   1559:                                                        firstname middlename/});
                   1560:                 $id = $info{'id'};
                   1561:             }
1.36      matthew  1562:             # Update the classlist with this students information
                   1563:             if ($fullname ne 'not available') {
                   1564:                 my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
                   1565:                 my $reply=&Apache::lonnet::cput('classlist',
                   1566:                                                 {$student => $enrolldata},
                   1567:                                                 $cdom,$cnum);
                   1568:                 if ($reply !~ /^(ok|delayed)/) {
                   1569:                     &Apache::lonnet::logthis('Unable to update classlist for '.
                   1570:                                              'student '.$sname.':'.$sdom.
                   1571:                                              ' error:'.$reply);
                   1572:                 }
                   1573:             }
1.35      matthew  1574:         }
                   1575:         my $status='Expired';
                   1576:         if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
                   1577:             $status='Active';
                   1578:         }
                   1579:         $classlist{$student} = 
                   1580:             [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
                   1581:     }
                   1582:     if (wantarray()) {
                   1583:         return (\%classlist,['domain','username','end','start','id',
                   1584:                              'section','fullname','status']);
                   1585:     } else {
                   1586:         return \%classlist;
                   1587:     }
                   1588: }
                   1589: 
1.1       stredwic 1590: # ----- END HELPER FUNCTIONS --------------------------------------------
                   1591: 
                   1592: 1;
                   1593: __END__
1.36      matthew  1594: 
1.35      matthew  1595: 

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