--- loncom/interface/loncoursedata.pm 2004/02/02 19:50:33 1.113 +++ loncom/interface/loncoursedata.pm 2004/07/06 15:50:03 1.137 @@ -1,6 +1,6 @@ # The LearningOnline Network with CAPA # -# $Id: loncoursedata.pm,v 1.113 2004/02/02 19:50:33 matthew Exp $ +# $Id: loncoursedata.pm,v 1.137 2004/07/06 15:50:03 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -182,7 +182,7 @@ sub get_sequence_assessment_data { next; } next if (! ref($curRes)); - next if (! $curRes->is_problem());# && !$curRes->randomout); + next if (! $curRes->is_problem() && $curRes->src() !~ /\.survey$/); # Okay, from here on out we only deal with assessments $title = $curRes->title(); $title =~ s/\:/\&\#058;/g; @@ -199,6 +199,7 @@ sub get_sequence_assessment_data { my @Ids = $curRes->responseIds($part); $partdata{$part}->{'ResponseTypes'}= \@Responses; $partdata{$part}->{'ResponseIds'} = \@Ids; + $partdata{$part}->{'Survey'} = $curRes->is_survey($part); # Count how many responses of each type there are in this part foreach (@Responses) { $partdata{$part}->{$_}++; @@ -475,6 +476,13 @@ about both the response and part data. 'transaction', and 'timestamp'. The primary key is based on the first 3 columns. +=item $weight_table + +The weight table holds the weight for the problems used in the class. +Whereas the weight of a problem can vary by section and student the data +here is applied to the class as a whole. +Columns: 'symb_id','part_id','response_id','weight'. + =back =back @@ -518,6 +526,7 @@ my $parameters_table; my $fulldump_response_table; my $fulldump_part_table; my $fulldump_timestamp_table; +my $weight_table; my @Tables; ################################################ @@ -539,12 +548,14 @@ store student data. ################################################ ################################################ sub init_dbs { - my $courseid = shift; + my ($courseid,$drop) = @_; &setup_table_names($courseid); # # Drop any of the existing tables - foreach my $table (@Tables) { - &Apache::lonmysql::drop_table($table); + if ($drop) { + foreach my $table (@Tables) { + &Apache::lonmysql::drop_table($table); + } } # # Note - changes to this table must be reflected in the code that @@ -572,7 +583,7 @@ sub init_dbs { restrictions => 'NOT NULL', auto_inc => 'yes', }, { name => 'part', - type => 'VARCHAR(100)', + type => 'VARCHAR(100) BINARY', restrictions => 'NOT NULL'}, ], 'PRIMARY KEY' => ['part (100)'], @@ -587,16 +598,16 @@ sub init_dbs { restrictions => 'NOT NULL', auto_inc => 'yes', }, { name => 'student', - type => 'VARCHAR(100)', + type => 'VARCHAR(100) BINARY', restrictions => 'NOT NULL UNIQUE'}, { name => 'section', - type => 'VARCHAR(100)', + type => 'VARCHAR(100) BINARY', restrictions => 'NOT NULL'}, { name => 'status', - type => 'VARCHAR(15)', + type => 'VARCHAR(15) BINARY', restrictions => 'NOT NULL'}, { name => 'classification', - type => 'varchar(100)', }, + type => 'VARCHAR(100) BINARY', }, { name => 'updatetime', type => 'INT UNSIGNED'}, { name => 'fullupdatetime', @@ -621,14 +632,14 @@ sub init_dbs { type => 'MEDIUMINT UNSIGNED', restrictions => 'NOT NULL' }, { name => 'part', - type => 'VARCHAR(100)', + type => 'VARCHAR(100) BINARY', restrictions => 'NOT NULL'}, { name => 'solved', type => 'TINYTEXT' }, { name => 'tries', type => 'SMALLINT UNSIGNED' }, { name => 'awarded', - type => 'TINYTEXT' }, + type => 'REAL' }, { name => 'award', type => 'TINYTEXT' }, { name => 'awarddetail', @@ -663,7 +674,7 @@ sub init_dbs { { name => 'award', type => 'TINYTEXT' }, { name => 'awarded', - type => 'TINYTEXT' }, + type => 'REAL' }, { name => 'previous', type => 'SMALLINT UNSIGNED' }, # { name => 'regrader', @@ -701,7 +712,7 @@ sub init_dbs { { name => 'awarddetail', type => 'TINYTEXT' }, # { name => 'message', -# type => 'CHAR' }, +# type => 'CHAR BINARY'}, { name => 'response_specific', type => 'TINYTEXT' }, { name => 'response_specific_value', @@ -740,7 +751,6 @@ sub init_dbs { { columns=>['transaction'] }, ], }; - # my $parameters_table_def = { id => $parameters_table, @@ -760,6 +770,22 @@ sub init_dbs { 'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'], }; # + my $weight_table_def = { + id => $weight_table, + permanent => 'no', + columns => [{ name => 'symb_id', + type => 'MEDIUMINT UNSIGNED', + restrictions => 'NOT NULL' }, + { name => 'part_id', + type => 'MEDIUMINT UNSIGNED', + restrictions => 'NOT NULL' }, + { name => 'weight', + type => 'REAL', + restrictions => 'NOT NULL' }, + ], + 'PRIMARY KEY' => ['symb_id','part_id'], + }; + # # Create the tables my $tableid; $tableid = &Apache::lonmysql::create_table($symb_table_def); @@ -816,6 +842,12 @@ sub init_dbs { &Apache::lonmysql::get_error()); return 9; } + $tableid = &Apache::lonmysql::create_table($weight_table_def); + if (! defined($tableid)) { + &Apache::lonnet::logthis("error creating weight_table: ". + &Apache::lonmysql::get_error()); + return 10; + } return 0; } @@ -1052,6 +1084,7 @@ sub populate_student_table { } # &setup_table_names($courseid); + &init_dbs($courseid,0); my $dbh = &Apache::lonmysql::get_dbh(); my $request = 'INSERT IGNORE INTO '.$student_table. "(student,section,status) VALUES "; @@ -1077,7 +1110,6 @@ sub populate_student_table { return; } - ################################################ ################################################ @@ -1235,11 +1267,7 @@ sub update_full_student_data { # However, there is one wrinkle: submissions which end in # and odd number of '\' cause insert errors to occur. # Best trap this somehow... - $value =~ s/\'/\\\'/g; - my ($offensive_string) = ($value =~ /(\\+)$/); - if (length($offensive_string) % 2) { - $value =~ s/\\$/\\\\/; - } + $value = $dbh->quote($value); } if ($field eq 'submissiongrading' || $field eq 'molecule') { @@ -1552,7 +1580,8 @@ sub ensure_tables_are_set_up { my @CurrentTable = &Apache::lonmysql::tables_in_db(); my ($found_symb,$found_student,$found_part, $found_performance,$found_parameters,$found_fulldump_part, - $found_fulldump_response,$found_fulldump_timestamp); + $found_fulldump_response,$found_fulldump_timestamp, + $found_weight); foreach (@CurrentTable) { $found_symb = 1 if ($_ eq $symb_table); $found_student = 1 if ($_ eq $student_table); @@ -1562,13 +1591,14 @@ sub ensure_tables_are_set_up { $found_fulldump_part = 1 if ($_ eq $fulldump_part_table); $found_fulldump_response = 1 if ($_ eq $fulldump_response_table); $found_fulldump_timestamp = 1 if ($_ eq $fulldump_timestamp_table); + $found_weight = 1 if ($_ eq $weight_table); } - if (!$found_symb || - !$found_student || !$found_part || - !$found_performance || !$found_parameters || + if (!$found_symb || + !$found_student || !$found_part || + !$found_performance || !$found_parameters || !$found_fulldump_part || !$found_fulldump_response || - !$found_fulldump_timestamp ) { - if (&init_dbs($courseid)) { + !$found_fulldump_timestamp || !$found_weight ) { + if (&init_dbs($courseid,1)) { return 'error'; } } @@ -1871,12 +1901,14 @@ populated and all local caching variable properly. This means you need to call &ensure_current_data for the students you are concerned with prior to calling this routine. -Inputs: $students, $symb, $part, $courseid +Inputs: $Sections, $status, $symb, $part, $courseid, $starttime, $endtime =over 4 -=item $students is an array of hash references. -Each hash must contain at least the 'username' and 'domain' of a student. +=item $Sections Array ref containing section names for students. +'all' is allowed to be the first (and only) item in the array. + +=item $status String describing the status of students =item $symb is the symb for the problem. @@ -1884,6 +1916,9 @@ Each hash must contain at least the 'use =item $courseid is the course id, of course! +=item $starttime and $endtime are unix times which to use to limit +the statistical data. + =back Outputs: See the code for up to date information. A hash reference is @@ -1919,7 +1954,7 @@ able to answer it correctly. ################################################ ################################################ sub get_problem_statistics { - my ($students,$symb,$part,$courseid) = @_; + my ($Sections,$status,$symb,$part,$courseid,$starttime,$endtime) = @_; return if (! defined($symb) || ! defined($part)); $courseid = $ENV{'request.course.id'} if (! defined($courseid)); # @@ -1931,88 +1966,119 @@ sub get_problem_statistics { my $dbh = &Apache::lonmysql::get_dbh(); return undef if (! defined($dbh)); # + # Clean out the table $dbh->do('DROP TABLE '.$stats_table); # May return an error my $request = - 'CREATE TEMPORARY TABLE '.$stats_table. - ' SELECT student_id,solved,award,awarded,tries FROM '.$performance_table. - ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id; - if (defined($students)) { + 'CREATE TEMPORARY TABLE '.$stats_table.' '. + 'SELECT a.student_id,a.solved,a.award,a.awarded,a.tries '. + 'FROM '.$performance_table.' AS a '; + # + # See if we need to include some requirements on the students + if ((defined($Sections) && lc($Sections->[0]) ne 'all') || + (defined($status) && lc($status) ne 'any')) { + $request .= 'NATURAL LEFT JOIN '.$student_table.' AS b '; + } + $request .= ' WHERE a.symb_id='.$symb_id.' AND a.part_id='.$part_id; + # + # Limit the students included to those specified + if (defined($Sections) && lc($Sections->[0]) ne 'all') { $request .= ' AND ('. - join(' OR ', map {'student_id='. - &get_student_id($_->{'username'}, - $_->{'domain'}) - } @$students + join(' OR ', map { "b.section='".$_."'" } @$Sections ).')'; } -# &Apache::lonnet::logthis($request); + if (defined($status) && lc($status) ne 'any') { + $request .= " AND b.status='".$status."'"; + } + # + # Limit by starttime and endtime + my $time_requirements = undef; + if (defined($starttime)) { + $time_requirements .= 'a.timestamp>='.$starttime; + if (defined($endtime)) { + $time_requirements .= ' AND a.timestamp<='.$endtime; + } + } elsif (defined($endtime)) { + $time_requirements .= 'a.timestamp<='.$endtime; + } + if (defined($time_requirements)) { + $request .= ' AND '.$time_requirements; + } + # + # Finally, execute the request to create the temporary table $dbh->do($request); -# &Apache::lonnet::logthis('request = '.$/.$request); - $request = 'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) '. + # + # Collect the first suite of statistics + $request = 'SELECT COUNT(*),SUM(tries),'. + 'AVG(tries),STD(tries) '. 'FROM '.$stats_table; - my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request + my ($num,$tries,$mean,$STD) = &execute_SQL_request ($dbh,$request); -# &Apache::lonnet::logthis('request = '.$/.$request); + # + $request = 'SELECT MAX(tries),MIN(tries) FROM '.$stats_table. + ' WHERE awarded>0'; + if (defined($time_requirements)) { + $request .= ' AND '.$time_requirements; + } + my ($max,$min) = &execute_SQL_request($dbh,$request); + # $request = 'SELECT SUM(awarded) FROM '.$stats_table; + if (defined($time_requirements)) { + $request .= ' AND '.$time_requirements; + } my ($Solved) = &execute_SQL_request($dbh,$request); -# &Apache::lonnet::logthis('request = '.$/.$request); + # $request = 'SELECT SUM(awarded) FROM '.$stats_table. " WHERE solved='correct_by_override'"; -# &Apache::lonnet::logthis('request = '.$/.$request); + if (defined($time_requirements)) { + $request .= ' AND '.$time_requirements; + } my ($solved) = &execute_SQL_request($dbh,$request); -# $Solved = int($Solved); -# $solved = int($solved); + # + $Solved -= $solved; # $num = 0 if (! defined($num)); $tries = 0 if (! defined($tries)); - $mod = 0 if (! defined($mod)); + $max = 0 if (! defined($max)); + $min = 0 if (! defined($min)); $STD = 0 if (! defined($STD)); - $Solved = 0 if (! defined($Solved)); + $Solved = 0 if (! defined($Solved) || $Solved < 0); $solved = 0 if (! defined($solved)); # + # Compute the more complicated statistics my $DegOfDiff = 'nan'; $DegOfDiff = 1-($Solved)/$tries if ($tries>0); - + # my $SKEW = 'nan'; my $wrongpercent = 0; + my $numwrong = 'nan'; if ($num > 0) { ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('. 'POWER(tries - '.$STD.',3)'. '))/'.$num.' FROM '.$stats_table); - $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10; + $numwrong = $num-$Solved; + $wrongpercent=int(10*100*$numwrong/$num)/10; } # -# $dbh->do('DROP TABLE '.$stats_table); # May return an error - # - # Store in metadata - # - if ($num) { - my %storestats=(); - - my $urlres=(&Apache::lonnet::decode_symb($symb))[2]; - - $storestats{$courseid.'___'.$urlres.'___timestamp'}=time; - $storestats{$courseid.'___'.$urlres.'___stdno'}=$num; - $storestats{$courseid.'___'.$urlres.'___avetries'}=$mean; - $storestats{$courseid.'___'.$urlres.'___difficulty'}=$DegOfDiff; - - $urlres=~/^(\w+)\/(\w+)/; - &Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2); - } + # Drop the temporary table + $dbh->do('DROP TABLE '.$stats_table); # May return an error # # Return result - # return { num_students => $num, tries => $tries, - max_tries => $mod, + max_tries => $max, + min_tries => $min, mean_tries => $mean, std_tries => $STD, skew_tries => $SKEW, num_solved => $Solved, num_override => $solved, + num_wrong => $numwrong, per_wrong => $wrongpercent, deg_of_diff => $DegOfDiff }; } +## +## This is a helper for get_statistics sub execute_SQL_request { my ($dbh,$request)=@_; # &Apache::lonnet::logthis($request); @@ -2025,6 +2091,399 @@ sub execute_SQL_request { return (); } +###################################################### +###################################################### + +=pod + +=item &populate_weight_table + +=cut + +###################################################### +###################################################### +sub populate_weight_table { + my ($courseid) = @_; + if (! defined($courseid)) { + $courseid = $ENV{'request.course.id'}; + } + # + &setup_table_names($courseid); + my ($top,$sequences,$assessments) = get_sequence_assessment_data(); + if (! defined($top) || ! ref($top)) { + # There has been an error, better report it + &Apache::lonnet::logthis('top is undefined'); + return; + } + # Since we use lonnet::EXT to retrieve problem weights, + # to ensure current data we must clear the caches out. + &Apache::lonnet::clear_EXT_cache_status(); + my $dbh = &Apache::lonmysql::get_dbh(); + my $request = 'INSERT IGNORE INTO '.$weight_table. + "(symb_id,part_id,weight) VALUES "; + my $weight; + foreach my $res (@$assessments) { + my $symb_id = &get_symb_id($res->{'symb'}); + foreach my $part (@{$res->{'parts'}}) { + my $part_id = &get_part_id($part); + $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight', + $res->{'symb'}, + undef,undef,undef); + if (!defined($weight) || ($weight eq '')) { + $weight=1; + } + $request .= "('".$symb_id."','".$part_id."','".$weight."'),"; + } + } + $request =~ s/(,)$//; +# &Apache::lonnet::logthis('request = '.$/.$request); + $dbh->do($request); + if ($dbh->err()) { + &Apache::lonnet::logthis("error ".$dbh->errstr(). + " occured executing \n". + $request); + } + return; +} + +########################################################## +########################################################## + +=pod + +=item &limit_by_start_end_times + +Build SQL WHERE condition which limits the data collected by the start +and end times provided + +Inputs: $starttime, $endtime, $table + +Returns: $time_limits + +=cut + +########################################################## +########################################################## +sub limit_by_start_end_time { + my ($starttime,$endtime,$table) = @_; + my $time_requirements = undef; + if (defined($starttime)) { + $time_requirements .= $table.".timestamp>='".$starttime."'"; + if (defined($endtime)) { + $time_requirements .= " AND ".$table.".timestamp<='".$endtime."'"; + } + } elsif (defined($endtime)) { + $time_requirements .= $table.".timestamp<='".$endtime."'"; + } + return $time_requirements; +} + +########################################################## +########################################################## + +=pod + +=item &limit_by_section_and_status + +Build SQL WHERE condition which limits the data collected by section and +student status. + +Inputs: $Sections (array ref) + $enrollment (string: 'any', 'expired', 'active') + $tablename The name of the table that holds the student data + +Returns: $student_requirements,$enrollment_requirements + +=cut + +########################################################## +########################################################## +sub limit_by_section_and_status { + my ($Sections,$enrollment,$tablename) = @_; + my $student_requirements = undef; + if ( (defined($Sections) && $Sections->[0] ne 'all')) { + $student_requirements = '('. + join(' OR ', map { $tablename.".section='".$_."'" } @$Sections + ).')'; + } + # + my $enrollment_requirements=undef; + if (defined($enrollment) && $enrollment ne 'Any') { + $enrollment_requirements = $tablename.".status='".$enrollment."'"; + } + return ($student_requirements,$enrollment_requirements); +} + +###################################################### +###################################################### + +=pod + +=item rank_students_by_scores_on_resources + +Inputs: + $resources: array ref of hash ref. Each hash ref needs key 'symb'. + $Sections: array ref of sections to include, + $enrollment: string, + $courseid (may be omitted) + +Returns; An array of arrays. The sub arrays contain a student name and +their score on the resources. + +=cut + +###################################################### +###################################################### +sub RNK_student { return 0; }; +sub RNK_score { return 1; }; + +sub rank_students_by_scores_on_resources { + my ($resources,$Sections,$enrollment,$courseid,$starttime,$endtime) = @_; + return if (! defined($resources) || ! ref($resources) eq 'ARRAY'); + if (! defined($courseid)) { + $courseid = $ENV{'request.course.id'}; + } + # + &setup_table_names($courseid); + my $dbh = &Apache::lonmysql::get_dbh(); + my ($section_limits,$enrollment_limits)= + &limit_by_section_and_status($Sections,$enrollment,'b'); + my $symb_limits = '('.join(' OR ',map {'a.symb_id='.&get_symb_id($_); + } @$resources + ).')'; + my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a'); + my $request = 'SELECT b.student,SUM(a.awarded*w.weight) AS score FROM '. + $performance_table.' AS a '. + 'NATURAL LEFT JOIN '.$weight_table.' AS w '. + 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '. + 'WHERE '; + if (defined($section_limits)) { + $request .= $section_limits.' AND '; + } + if (defined($enrollment_limits)) { + $request .= $enrollment_limits.' AND '; + } + if (defined($time_limits)) { + $request .= $time_limits.' AND '; + } + if ($symb_limits ne '()') { + $request .= $symb_limits.' AND '; + } + $request =~ s/( AND )$//; # Remove extra conjunction + $request =~ s/( WHERE )$//; # In case there were no limits placed on it + $request .= ' GROUP BY a.student_id ORDER BY score'; + #&Apache::lonnet::logthis('request = '.$/.$request); + my $sth = $dbh->prepare($request); + $sth->execute(); + my $rows = $sth->fetchall_arrayref(); + return ($rows); +} + +######################################################## +######################################################## + +=pod + +=item &get_sum_of_scores + +Inputs: $resource (hash ref, needs {'symb'} key), +$part, (the part id), +$students (array ref, contents of array are scalars holding 'sname:sdom'), +$courseid + +Returns: the sum of the score on the problem part over the students and the + maximum possible value for the sum (taken from the weight table). + +=cut + +######################################################## +######################################################## +sub get_sum_of_scores { + my ($resource,$part,$students,$courseid,$starttime,$endtime) = @_; + if (! defined($courseid)) { + $courseid = $ENV{'request.course.id'}; + } + # + &setup_table_names($courseid); + my $dbh = &Apache::lonmysql::get_dbh(); + my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a'); + my $request = 'SELECT SUM(a.awarded*w.weight),SUM(w.weight) FROM '. + $performance_table.' AS a '. + 'NATURAL LEFT JOIN '.$weight_table.' AS w '; + $request .= 'WHERE a.symb_id='.&get_symb_id($resource->{'symb'}). + ' AND a.part_id='.&get_part_id($part); + if (defined($time_limits)) { + $request .= ' AND '.$time_limits; + } + if (defined($students)) { + $request .= ' AND ('. + join(' OR ',map {'a.student_id='.&get_student_id(split(':',$_)); + } @$students). + ')'; + } + my $sth = $dbh->prepare($request); + $sth->execute(); + my $rows = $sth->fetchrow_arrayref(); + if ($dbh->err) { + &Apache::lonnet::logthis('error = '.$dbh->errstr()); + return (undef,undef); + } + return ($rows->[0],$rows->[1]); +} + +######################################################## +######################################################## + +=pod + +=item &score_stats + +Inputs: $Sections, $enrollment, $symbs, $starttime, + $endtime, $courseid + +$Sections, $enrollment, $starttime, $endtime, and $courseid are the same as +elsewhere in this module. +$symbs is an array ref of symbs + +Returns: minimum, maximum, mean, s.d., number of students, and maximum + possible of student scores on the given resources + +=cut + +######################################################## +######################################################## +sub score_stats { + my ($Sections,$enrollment,$symbs,$starttime,$endtime,$courseid)=@_; + if (! defined($courseid)) { + $courseid = $ENV{'request.course.id'}; + } + # + &setup_table_names($courseid); + my $dbh = &Apache::lonmysql::get_dbh(); + # + my ($section_limits,$enrollment_limits)= + &limit_by_section_and_status($Sections,$enrollment,'b'); + my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a'); + my @Symbids = map { &get_symb_id($_); } @{$symbs}; + # + my $stats_table = $courseid.'_problem_stats'; + my $symb_restriction = join(' OR ',map {'a.symb_id='.$_;} @Symbids); + my $request = 'DROP TABLE '.$stats_table; + $dbh->do($request); + $request = + 'CREATE TEMPORARY TABLE '.$stats_table.' '. + 'SELECT a.student_id,'. + 'SUM(a.awarded*w.weight) AS score FROM '. + $performance_table.' AS a '. + 'NATURAL LEFT JOIN '.$weight_table.' AS w '. + 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '. + 'WHERE ('.$symb_restriction.')'; + if ($time_limits) { + $request .= ' AND '.$time_limits; + } + if ($section_limits) { + $request .= ' AND '.$section_limits; + } + if ($enrollment_limits) { + $request .= ' AND '.$enrollment_limits; + } + $request .= ' GROUP BY a.student_id'; +# &Apache::lonnet::logthis('request = '.$/.$request); + my $sth = $dbh->prepare($request); + $sth->execute(); + $request = + 'SELECT AVG(score),STD(score),MAX(score),MIN(score),COUNT(score) '. + 'FROM '.$stats_table; + my ($ave,$std,$max,$min,$count) = &execute_SQL_request($dbh,$request); +# &Apache::lonnet::logthis('request = '.$/.$request); + + $request = 'SELECT SUM(weight) FROM '.$weight_table. + ' WHERE ('.$symb_restriction.')'; + my ($max_possible) = &execute_SQL_request($dbh,$request); + # &Apache::lonnet::logthis('request = '.$/.$request); + return($min,$max,$ave,$std,$count,$max_possible); +} + + +######################################################## +######################################################## + +=pod + +=item &count_stats + +Inputs: $Sections, $enrollment, $symbs, $starttime, + $endtime, $courseid + +$Sections, $enrollment, $starttime, $endtime, and $courseid are the same as +elsewhere in this module. +$symbs is an array ref of symbs + +Returns: minimum, maximum, mean, s.d., and number of students + of the number of items correct on the given resources + +=cut + +######################################################## +######################################################## +sub count_stats { + my ($Sections,$enrollment,$symbs,$starttime,$endtime,$courseid)=@_; + if (! defined($courseid)) { + $courseid = $ENV{'request.course.id'}; + } + # + &setup_table_names($courseid); + my $dbh = &Apache::lonmysql::get_dbh(); + # + my ($section_limits,$enrollment_limits)= + &limit_by_section_and_status($Sections,$enrollment,'b'); + my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a'); + my @Symbids = map { &get_symb_id($_); } @{$symbs}; + # + my $stats_table = $courseid.'_problem_stats'; + my $symb_restriction = join(' OR ',map {'a.symb_id='.$_;} @Symbids); + my $request = 'DROP TABLE '.$stats_table; + $dbh->do($request); + $request = + 'CREATE TEMPORARY TABLE '.$stats_table.' '. + 'SELECT a.student_id,'. + 'COUNT(a.award) AS count FROM '. + $performance_table.' AS a '. + 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '. + 'WHERE ('.$symb_restriction.')'. + " AND a.award!='INCORRECT_ATTEMPTED'"; + if ($time_limits) { + $request .= ' AND '.$time_limits; + } + if ($section_limits) { + $request .= ' AND '.$section_limits; + } + if ($enrollment_limits) { + $request .= ' AND '.$enrollment_limits; + } + $request .= ' GROUP BY a.student_id'; +# &Apache::lonnet::logthis('request = '.$/.$request); + my $sth = $dbh->prepare($request); + $sth->execute(); + $request = + 'SELECT AVG(count),STD(count),MAX(count),MIN(count),COUNT(count) '. + 'FROM '.$stats_table; + my ($ave,$std,$max,$min,$count) = &execute_SQL_request($dbh,$request); +# &Apache::lonnet::logthis('request = '.$/.$request); + return($min,$max,$ave,$std,$count); +} + +###################################################### +###################################################### + +=pod + +=item get_student_data + +=cut + +###################################################### +###################################################### sub get_student_data { my ($students,$courseid) = @_; $courseid = $ENV{'request.course.id'} if (! defined($courseid)); @@ -2064,17 +2523,28 @@ sub RD_tries { return 5; } sub RD_sname { return 6; } sub get_response_data { - my ($students,$symb,$response,$courseid) = @_; + my ($Sections,$enrollment,$symb,$response,$courseid) = @_; return undef if (! defined($symb) || ! defined($response)); $courseid = $ENV{'request.course.id'} if (! defined($courseid)); # &setup_table_names($courseid); my $symb_id = &get_symb_id($symb); + if (! defined($symb_id)) { + &Apache::lonnet::logthis('Unable to find symb for '.$symb.' in '.$courseid); + return undef; + } my $response_id = &get_part_id($response); + if (! defined($response_id)) { + &Apache::lonnet::logthis('Unable to find id for '.$response.' in '.$courseid); + return undef; + } # my $dbh = &Apache::lonmysql::get_dbh(); return undef if (! defined($dbh)); + # + my ($student_requirements,$enrollment_requirements) = + &limit_by_section_and_status($Sections,$enrollment,'d'); my $request = 'SELECT '. 'a.student_id, a.awarddetail, a.response_specific_value, '. 'a.submission, b.timestamp, c.tries, d.student '. @@ -2089,13 +2559,15 @@ sub get_response_data { 'ON a.student_id=d.student_id '. 'WHERE '. 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id; - if (defined($students)) { - $request .= ' AND ('. - join(' OR ', map {'a.student_id='. - &get_student_id($_->{'username'}, - $_->{'domain'}) - } @$students - ).')'; + if (defined($student_requirements) || defined($enrollment_requirements)) { + $request .= ' AND '; + if (defined($student_requirements)) { + $request .= $student_requirements.' AND '; + } + if (defined($enrollment_requirements)) { + $request .= $enrollment_requirements.' AND '; + } + $request =~ s/( AND )$//; } $request .= ' ORDER BY b.timestamp'; # &Apache::lonnet::logthis("request =\n".$request); @@ -2107,8 +2579,69 @@ sub get_response_data { } my $dataset = $sth->fetchall_arrayref(); if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) { + # Clear the \'s from around the submission + for (my $i =0;$i[$i]->[3] =~ s/(\'$|^\')//g; + } + return $dataset; + } +} + + +sub RDs_awarddetail { return 3; } +sub RDs_submission { return 2; } +sub RDs_timestamp { return 1; } +sub RDs_tries { return 0; } +sub RDs_awarded { return 4; } + +sub get_response_data_by_student { + my ($student,$symb,$response,$courseid) = @_; + return undef if (! defined($symb) || + ! defined($response)); + $courseid = $ENV{'request.course.id'} if (! defined($courseid)); + # + &setup_table_names($courseid); + my $symb_id = &get_symb_id($symb); + my $response_id = &get_part_id($response); + # + my $student_id = &get_student_id($student->{'username'}, + $student->{'domain'}); + # + my $dbh = &Apache::lonmysql::get_dbh(); + return undef if (! defined($dbh)); + my $request = 'SELECT '. + 'c.tries, b.timestamp, a.submission, a.awarddetail, e.awarded '. + 'FROM '.$fulldump_response_table.' AS a '. + 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '. + 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '. + 'a.transaction = b.transaction '. + 'LEFT JOIN '.$fulldump_part_table.' AS c '. + 'ON a.symb_id=c.symb_id AND a.student_id=c.student_id AND '. + 'a.part_id=c.part_id AND a.transaction = c.transaction '. + 'LEFT JOIN '.$student_table.' AS d '. + 'ON a.student_id=d.student_id '. + 'LEFT JOIN '.$performance_table.' AS e '. + 'ON a.symb_id=e.symb_id AND a.part_id=e.part_id AND '. + 'a.student_id=e.student_id AND c.tries=e.tries '. + 'WHERE '. + 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id. + ' AND a.student_id='.$student_id.' ORDER BY b.timestamp'; +# &Apache::lonnet::logthis("request =\n".$request); + my $sth = $dbh->prepare($request); + $sth->execute(); + if ($dbh->err) { + &Apache::lonnet::logthis('error = '.$dbh->errstr()); + return undef; + } + my $dataset = $sth->fetchall_arrayref(); + if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) { + # Clear the \'s from around the submission + for (my $i =0;$i[$i]->[2] =~ s/(\'$|^\')//g; + } return $dataset; } + return undef; # error occurred } sub RT_student_id { return 0; } @@ -2164,50 +2697,78 @@ sub get_response_time_data { =pod -=item &get_student_scores($Students,$Symbs,$courseid) +=item &get_student_scores($Sections,$Symbs,$enrollment,$courseid) =cut ################################################ ################################################ sub get_student_scores { - my ($Students,$Symbs,$courseid) = @_; + my ($Sections,$Symbs,$enrollment,$courseid,$starttime,$endtime) = @_; $courseid = $ENV{'request.course.id'} if (! defined($courseid)); &setup_table_names($courseid); my $dbh = &Apache::lonmysql::get_dbh(); return (undef) if (! defined($dbh)); my $tmptable = $courseid.'_temp_'.time; - my ($symb_requirements,$student_requirements); + # + my $symb_requirements; if (defined($Symbs) && @$Symbs) { $symb_requirements = '('. - join(' OR ', map{ "(symb_id='".&get_symb_id($_->{'symb'}). - "' AND part_id='".&get_part_id($_->{'part'}). - "')" + join(' OR ', map{ "(a.symb_id='".&get_symb_id($_->{'symb'}). + "' AND a.part_id='".&get_part_id($_->{'part'}). + "')" } @$Symbs).')'; } - if (defined($Students)) { + # + my $student_requirements; + if ( (defined($Sections) && $Sections->[0] ne 'all')) { $student_requirements = '('. - join(' OR ', map {'student_id='. - &get_student_id($_->{'username'}, - $_->{'domain'}) - } @$Students + join(' OR ', map { "b.section='".$_."'" } @$Sections ).')'; } + # + my $enrollment_requirements=undef; + if (defined($enrollment) && $enrollment ne 'Any') { + $enrollment_requirements = "b.status='".$enrollment."'"; + } + # + my $time_requirements = undef; + if (defined($starttime)) { + $time_requirements .= "a.timestamp>='".$starttime."'"; + if (defined($endtime)) { + $time_requirements .= " AND a.timestamp<='".$endtime."'"; + } + } elsif (defined($endtime)) { + $time_requirements .= "a.timestamp<='".$endtime."'"; + } + ## + ## my $request = 'CREATE TEMPORARY TABLE IF NOT EXISTS '.$tmptable. - ' SELECT student_id,SUM(awarded) AS score FROM '.$performance_table; - if (defined($symb_requirements) || defined($student_requirements)) { + ' SELECT a.student_id,SUM(a.awarded) AS score FROM '. + $performance_table.' AS a '; + if (defined($student_requirements) || defined($enrollment_requirements)) { + $request .= ' NATURAL LEFT JOIN '.$student_table.' AS b '; + } + if (defined($symb_requirements) || + defined($student_requirements) || + defined($enrollment_requirements) ) { $request .= ' WHERE '; - if (defined($symb_requirements)) { - $request .= $symb_requirements; - if (defined($student_requirements)) { - $request .= ' AND '.$student_requirements; - } - } elsif (defined($student_requirements)) { - $request .= $student_requirements; - } } - $request .= ' GROUP BY student_id'; - &Apache::lonnet::logthis("request = \n".$request); + if (defined($symb_requirements)) { + $request .= $symb_requirements.' AND '; + } + if (defined($student_requirements)) { + $request .= $student_requirements.' AND '; + } + if (defined($enrollment_requirements)) { + $request .= $enrollment_requirements.' AND '; + } + if (defined($time_requirements)) { + $request .= $time_requirements.' AND '; + } + $request =~ s/ AND $//; # Strip of the trailing ' AND '. + $request .= ' GROUP BY a.student_id'; +# &Apache::lonnet::logthis("request = \n".$request); my $sth = $dbh->prepare($request); $sth->execute(); if ($dbh->err) { @@ -2274,6 +2835,7 @@ sub setup_table_names { $fulldump_part_table = $base_id.'_'.'partdata'; $fulldump_response_table = $base_id.'_'.'responsedata'; $fulldump_timestamp_table = $base_id.'_'.'timestampdata'; + $weight_table = $base_id.'_'.'weight'; # @Tables = ( $symb_table, @@ -2284,6 +2846,7 @@ sub setup_table_names { $fulldump_part_table, $fulldump_response_table, $fulldump_timestamp_table, + $weight_table, ); return; } @@ -2324,7 +2887,7 @@ $ENV{'course.'.$cid.'.domain'}, and $ENV Returns a reference to a hash which contains: keys '$sname:$sdom' - values [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type] + values [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type,$lockedtype] The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used as indices into the returned list to future-proof clients against @@ -2344,6 +2907,7 @@ sub CL_SECTION { return 5; } sub CL_FULLNAME { return 6; } sub CL_STATUS { return 7; } sub CL_TYPE { return 8; } +sub CL_LOCKEDTYPE { return 9; } sub get_classlist { my ($cid,$cdom,$cnum) = @_; @@ -2360,9 +2924,9 @@ sub get_classlist { } my ($sname,$sdom) = split(/:/,$student); my @Values = split(/:/,$info); - my ($end,$start,$id,$section,$fullname,$type); + my ($end,$start,$id,$section,$fullname,$type,$lockedtype); if (@Values > 2) { - ($end,$start,$id,$section,$fullname,$type) = @Values; + ($end,$start,$id,$section,$fullname,$type,$lockedtype) = @Values; } else { # We have to get the data ourselves ($end,$start) = @Values; $section = &Apache::lonnet::getsection($sdom,$sname,$cid); @@ -2399,11 +2963,11 @@ sub get_classlist { $status='Active'; } $classlist{$student} = - [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type]; + [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type,$lockedtype]; } if (wantarray()) { return (\%classlist,['domain','username','end','start','id', - 'section','fullname','status','type']); + 'section','fullname','status','type','lockedtype']); } else { return \%classlist; }