--- loncom/interface/loncoursedata.pm 2016/08/14 16:13:21 1.200 +++ loncom/interface/loncoursedata.pm 2016/08/16 00:46:58 1.201 @@ -1,6 +1,6 @@ # The LearningOnline Network with CAPA # -# $Id: loncoursedata.pm,v 1.200 2016/08/14 16:13:21 raeburn Exp $ +# $Id: loncoursedata.pm,v 1.201 2016/08/16 00:46:58 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -1423,14 +1423,62 @@ sub ensure_current_sections { $updatetime = $tableinfo{'Update_time'}; } if ((!defined($updatetime)) || ($modifiedtime > $updatetime)) { - if (&Apache::lonmysql::drop_table($student_table)) { - if (&init_dbs($courseid)) { - return "error creating $student_table\n"; - } else { - &populate_student_table($courseid); + &update_student_table($cdom,$cnum); + } + return; +} + +sub update_student_table { + my ($cdom,$cnum) = @_; + return unless (($cdom ne '') && ($cnum ne '')); + my (%roster,%sqldata); + my $classlist = &get_classlist($cdom,$cnum); + while (my ($student,$data) = each (%$classlist)) { + my ($section,$start,$end) = ($data->[&CL_SECTION()], + $data->[&CL_START()], + $data->[&CL_END()]); + if ($section eq '' || $section =~ /^\s*$/) { + $section = 'none'; + } + if ($start eq '') { $start = 0; } + if ($end eq '') { $end = 0; } + $roster{$student}{'section'} = $section; + $roster{$student}{'start'} = $start; + $roster{$student}{'end'} = $end; + } + my $dbh = &Apache::lonmysql::get_dbh(); + my $statement = "SELECT student_id,student,section,start,end FROM $student_table"; + my $sth = $dbh->prepare($statement); + $sth->execute(); + if ($sth->err()) { + &Apache::lonnet::logthis("Unable to execute MySQL request:"); + &Apache::lonnet::logthis("\n".$statement."\n"); + &Apache::lonnet::logthis("error is:".$sth->errstr()); + return undef; + } + foreach my $row (@{$sth->fetchall_arrayref}) { + my ($id,$student,$section,$start,$end) = (@$row); + if (ref($roster{$student}) eq 'HASH') { + if (($roster{$student}{'section'} ne $section) || + ($roster{$student}{'start'} ne $start) || + ($roster{$student}{'end'} ne $end)) { + $sqldata{$id} = { + section => $roster{$student}{'section'}, + start => $roster{$student}{'start'}, + end => $roster{$student}{'end'}, + }; } } } + $sth->finish(); + if (keys(%sqldata)) { + foreach my $id (sort { $a <=> $b } keys(%sqldata)) { + my $request = "UPDATE $student_table SET section='$sqldata{$id}{section}'". + ", start='$sqldata{$id}{start}'". + ", end='$sqldata{$id}{end}' WHERE student_id='$id'"; + $dbh->do($request); + } + } return; }