--- loncom/interface/loncoursedata.pm 2003/04/11 15:14:25 1.68 +++ loncom/interface/loncoursedata.pm 2003/08/27 21:34:24 1.85 @@ -1,6 +1,6 @@ # The LearningOnline Network with CAPA # -# $Id: loncoursedata.pm,v 1.68 2003/04/11 15:14:25 matthew Exp $ +# $Id: loncoursedata.pm,v 1.85 2003/08/27 21:34:24 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -104,11 +104,14 @@ sub get_sequence_assessment_data { my $fn=$ENV{'request.course.fn'}; ## ## use navmaps - my $navmap = Apache::lonnavmaps::navmap->new($fn.".db", - $fn."_parms.db",1,0); + my $navmap = Apache::lonnavmaps::navmap->new(); if (!defined($navmap)) { return 'Can not open Coursemap'; } + # We explicity grab the top level map because I am not sure we + # are pulling it from the iterator. + my $top_level_map = $navmap->getById('0.0'); + # my $iterator = $navmap->getIterator(undef, undef, undef, 1); my $curRes = $iterator->next(); # Top level sequence ## @@ -118,8 +121,9 @@ sub get_sequence_assessment_data { ## resources. This means we have to start out with something to look ## at. my $title = $ENV{'course.'.$ENV{'request.course.id'}.'.description'}; - my $symb = 'top'; - my $src = 'not applicable'; + my $symb = $top_level_map->symb(); + my $src = $top_level_map->src(); + my $randompick = $top_level_map->randompick(); # my @Sequences; my @Assessments; @@ -130,29 +134,44 @@ sub get_sequence_assessment_data { type => 'container', num_assess => 0, num_assess_parts => 0, - contents => [], }; + contents => [], + randompick => $randompick, + }; push (@Sequences,$top); push (@Nested_Sequences, $top); # # We need to keep track of which sequences contain homework problems # + my $previous_too; my $previous; - $curRes = $iterator->next(); # BEGIN_MAP - $curRes = $iterator->next(); # The first item in the top level map. while (scalar(@Nested_Sequences)) { + $previous_too = $previous; $previous = $curRes; $curRes = $iterator->next(); my $currentmap = $Nested_Sequences[-1]; # Last one on the stack if ($curRes == $iterator->BEGIN_MAP()) { + if (! ref($previous)) { + $previous = $previous_too; + } + if (! ref($previous)) { + next; + } # get the map itself, instead of BEGIN_MAP $title = $previous->title(); + $title =~ s/\:/\&\#058;/g; $symb = $previous->symb(); $src = $previous->src(); + # pick up the filename if there is no title available + if (! defined($title) || $title eq '') { + ($title) = ($src=~/\/([^\/]*)$/); + } + $randompick = $previous->randompick(); my $newmap = { title => $title, src => $src, symb => $symb, type => 'container', num_assess => 0, + randompick => $randompick, contents => [], }; push (@{$currentmap->{'contents'}},$newmap); # this is permanent @@ -168,6 +187,7 @@ sub get_sequence_assessment_data { next if (! $curRes->is_problem());# && !$curRes->randomout); # Okay, from here on out we only deal with assessments $title = $curRes->title(); + $title =~ s/\:/\&\#058;/g; $symb = $curRes->symb(); $src = $curRes->src(); my $parts = $curRes->parts(); @@ -211,6 +231,39 @@ sub LoadDiscussion { return \%Discuss; } +################################################ +################################################ + +=pod + +=item &GetUserName(username,userdomain) + +Returns a hash with the following entries: + 'firstname', 'middlename', 'lastname', 'generation', and 'fullname' + + 'fullname' is the result of &Apache::loncoursedata::ProcessFullName. + +=cut + +################################################ +################################################ +sub GetUserName { + my ($username,$userdomain) = @_; + $username = $ENV{'user.name'} if (! defined($username)); + $userdomain = $ENV{'user.domain'} if (! defined($username)); + my %userenv = &Apache::lonnet::get('environment', + ['firstname','middlename','lastname','generation'], + $userdomain,$username); + $userenv{'fullname'} = &ProcessFullName($userenv{'lastname'}, + $userenv{'generation'}, + $userenv{'firstname'}, + $userenv{'middlename'}); + return %userenv; +} + +################################################ +################################################ + =pod =item &ProcessFullName() @@ -221,6 +274,8 @@ is Lastname generation, firstname middle =cut +################################################ +################################################ sub ProcessFullName { my ($lastname, $generation, $firstname, $middlename)=@_; my $Str = ''; @@ -422,6 +477,13 @@ sub init_dbs { my $courseid = shift; &setup_table_names($courseid); # + # Drop any of the existing tables + foreach my $table ($symb_table,$part_table,$student_table, + $updatetime_table,$performance_table, + $parameters_table) { + &Apache::lonmysql::drop_table($table); + } + # # Note - changes to this table must be reflected in the code that # stores the data (calls &Apache::lonmysql::store_row with this table # id @@ -464,6 +526,8 @@ sub init_dbs { { name => 'student', type => 'VARCHAR(100)', restrictions => 'NOT NULL'}, + { name => 'classification', + type => 'varchar(100)', }, ], 'PRIMARY KEY' => ['student (100)'], 'KEY' => [{ columns => ['student_id']},], @@ -494,6 +558,9 @@ sub init_dbs { { name => 'part_id', type => 'MEDIUMINT UNSIGNED', restrictions => 'NOT NULL' }, + { name => 'part', + type => 'VARCHAR(100)', + restrictions => 'NOT NULL'}, { name => 'solved', type => 'TINYTEXT' }, { name => 'tries', @@ -581,6 +648,36 @@ sub init_dbs { =pod +=item &delete_caches() + +=cut + +################################################ +################################################ +sub delete_caches { + my $courseid = shift; + $courseid = $ENV{'request.course.id'} if (! defined($courseid)); + # + &setup_table_names($courseid); + # + my $dbh = &Apache::lonmysql::get_dbh(); + foreach my $table ($symb_table,$part_table,$student_table, + $updatetime_table,$performance_table, + $parameters_table ){ + my $command = 'DROP TABLE '.$table.';'; + $dbh->do($command); + if ($dbh->err) { + &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr); + } + } + return; +} + +################################################ +################################################ + +=pod + =item &get_part_id() Get the MySQL id of a problem part string. @@ -748,7 +845,7 @@ sub get_student_id { $have_read_student_table = 1; } if (! exists($ids_by_student{$student})) { - &Apache::lonmysql::store_row($student_table,[undef,$student]); + &Apache::lonmysql::store_row($student_table,[undef,$student,undef]); undef(%ids_by_student); my @Result = &Apache::lonmysql::get_rows($student_table); foreach (@Result) { @@ -826,7 +923,7 @@ sub update_student_data { $sname.':'.$sdom.' in course '.$courseid. ':'.$tmp[0]); $returnstatus = 'error getting data'; - return $returnstatus; + return ($returnstatus,undef); } if (scalar(@tmp) < 1) { return ('no data',undef); @@ -850,7 +947,7 @@ sub update_student_data { my $num_parameters = 0; my $store_performance_command = 'INSERT INTO '.$performance_table. ' VALUES '."\n"; - return 'error' if (! defined($dbh)); + return ('error',undef) if (! defined($dbh)); while (my ($current_symb,$param_hash) = each(%student_data)) { # # make sure the symb is set up properly @@ -862,7 +959,8 @@ sub update_student_data { if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) { $newstring = "('".join("','", $symb_id,$student_id, - $parameter,$value)."'),\n"; + $parameter)."',". + $dbh->quote($value)."),\n"; $num_parameters ++; if ($newstring !~ /''/) { $store_parameters_command .= $newstring; @@ -881,12 +979,12 @@ sub update_student_data { my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'}; my $timestamp = $param_hash->{'timestamp'}; # - $solved = '' if (! defined($awarded)); + $solved = '' if (! defined($solved)); $tries = '' if (! defined($tries)); $awarded = '' if (! defined($awarded)); $award = '' if (! defined($award)); $awarddetail = '' if (! defined($awarddetail)); - $newstring = "('".join("','",$symb_id,$student_id,$part_id, + $newstring = "('".join("','",$symb_id,$student_id,$part_id,$part, $solved,$tries,$awarded,$award, $awarddetail,$timestamp)."'),\n"; $store_performance_command .= $newstring; @@ -903,14 +1001,14 @@ sub update_student_data { &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr()); &Apache::lonnet::logthis('command = '.$store_parameters_command); $returnstatus = 'error: unable to insert parameters into database'; - return $returnstatus,\%student_data; + return ($returnstatus,\%student_data); } $dbh->do($store_performance_command); if ($dbh->err()) { &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr()); &Apache::lonnet::logthis('command = '.$store_performance_command); $returnstatus = 'error: unable to insert performance into database'; - return $returnstatus,\%student_data; + return ($returnstatus,\%student_data); } $elapsed += Time::HiRes::time - $start; # @@ -966,7 +1064,7 @@ sub ensure_current_data { !$found_student || !$found_part || !$found_performance || !$found_parameters) { if (&init_dbs($courseid)) { - return 'error'; + return ('error',undef); } } # @@ -1030,12 +1128,12 @@ sub get_student_data_from_performance_ca # my $dbh = &Apache::lonmysql::get_dbh(); my $request = "SELECT ". - "d.symb,c.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,". + "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,". "a.timestamp "; if (defined($student)) { $request .= "FROM $student_table AS b ". "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ". - "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ". +# "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ". "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ". "WHERE student='$student'"; if (defined($symb) && $symb ne '') { @@ -1044,7 +1142,7 @@ sub get_student_data_from_performance_ca } elsif (defined($symb) && $symb ne '') { $request .= "FROM $symb_table as d ". "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ". - "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ". +# "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ". "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ". "WHERE symb='".$dbh->quote($symb)."'"; } @@ -1122,14 +1220,24 @@ sub get_current_state { return () if (! defined($sname) || ! defined($sdom)); # my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid); - # - if (defined($data)) { - if (defined($symb)) { - return %{$data->{$symb}}; - } else { - return %$data; - } - } elsif ($status eq 'no data') { +# &Apache::lonnet::logthis +# ('sname = '.$sname. +# ' domain = '.$sdom. +# ' status = '.$status. +# ' data is '.(defined($data)?'defined':'undefined')); +# while (my ($symb,$hash) = each(%$data)) { +# &Apache::lonnet::logthis($symb."\n----------------------------------"); +# while (my ($key,$value) = each (%$hash)) { +# &Apache::lonnet::logthis(" ".$key." = ".$value); +# } +# } + # + if (defined($data) && defined($symb) && ref($data->{$symb})) { + return %{$data->{$symb}}; + } elsif (defined($data) && ! defined($symb) && ref($data)) { + return %$data; + } + if ($status eq 'no data') { return (); } else { if ($status ne 'okay' && $status ne '') { @@ -1270,6 +1378,25 @@ sub get_problem_statistics { } # $dbh->do('DROP TABLE '.$stats_table); # May return an error + # + # Store in metadata + # + if ($num) { + my %storestats=(); + + my $urlres=(split(/\_\_\_/,$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); + } + # + # Return result + # return { num_students => $num, tries => $tries, max_tries => $mod, @@ -1279,7 +1406,7 @@ sub get_problem_statistics { num_solved => $Solved, num_override => $solved, per_wrong => $wrongpercent, - deg_of_diff => $DegOfDiff } + deg_of_diff => $DegOfDiff }; } sub execute_SQL_request {