--- loncom/misc/refresh_courseids_db.pl 2010/03/14 23:17:54 1.1 +++ loncom/misc/refresh_courseids_db.pl 2010/03/15 20:13:14 1.2 @@ -1,7 +1,7 @@ #!/usr/bin/perl # The LearningOnline Network # -# $Id: refresh_courseids_db.pl,v 1.1 2010/03/14 23:17:54 raeburn Exp $ +# $Id: refresh_courseids_db.pl,v 1.2 2010/03/15 20:13:14 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -78,7 +78,9 @@ foreach my $dom (@domains) { my @ids=&Apache::lonnet::current_machine_ids(); my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.'); my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom; - &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,$fh); + my %domdesign = &Apache::loncommon::get_domainconf($dom); + my $autoassign = $domdesign{$dom.'.autoassign.co-owners'}; + &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,$autoassign,$fh); foreach my $lonhost (keys(%courseshash)) { if (ref($courseshash{$lonhost}) eq 'HASH') { if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') { @@ -95,7 +97,7 @@ print $fh "==== refresh_courseids.db com close($fh); sub recurse_courses { - my ($cdom,$dir,$depth,$courseshash,$currhash,$fh) = @_; + my ($cdom,$dir,$depth,$courseshash,$currhash,$autoassign,$fh) = @_; next unless (ref($currhash) eq 'HASH'); if (-d $dir) { opendir(DIR,$dir); @@ -104,7 +106,8 @@ sub recurse_courses { $depth ++; foreach my $item (@contents) { if ($depth < 4) { - &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,$currhash,$fh); + &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash, + $currhash,$autoassign,$fh); } elsif ($item =~ /^$match_courseid$/) { my $cnum = $item; my $cid = $cdom.'_'.$cnum; @@ -137,8 +140,10 @@ sub recurse_courses { } my $chome = &Apache::lonnet::homeserver($cnum,$cdom); my $owner = $courseinfo{'internal.courseowner'}; + my (%roleshash,$gotcc); if ($owner eq '') { - my %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1); + %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1); + $gotcc = 1; if (keys(%roleshash) == 1) { foreach my $key (keys(%roleshash)) { if ($key =~ /^($match_username\:$match_domain)\:cc$/) { @@ -163,11 +168,10 @@ sub recurse_courses { $inst_code = '' if (!defined($inst_code)); $owner = '' if (!defined($owner)); if ($created eq '') { - my %currdump = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.'); - if (ref($currdump{$cid}) eq 'HASH') { - $created = $currdump{$cid}{'created'}; - $creator = $currdump{$cid}{'creator'}; - $creationcontext = $currdump{$cid}{'context'}; + if (ref($currhash->{$cid}) eq 'HASH') { + $created = $currhash->{$cid}{'created'}; + $creator = $currhash->{$cid}{'creator'}; + $creationcontext = $currhash->{$cid}{'context'}; unless ($created eq '') { $changes{'internal.created'} = $created; } @@ -204,9 +208,6 @@ sub recurse_courses { owner => $owner, type => $crstype, }; - if ($courseinfo{'internal.co-owners'} ne '') { - $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'}; - } if ($creator ne '') { $courseshash->{$chome}{$cid}{'creator'} = $creator; } @@ -216,16 +217,62 @@ sub recurse_courses { if ($creationcontext ne '') { $courseshash->{$chome}{$cid}{'context'} = $creationcontext; } + if (($inst_code ne '') && ($autoassign)) { + unless ($gotcc) { + %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1); + } + my @currcoowners; + my @newcoowners; + if ($courseinfo{'internal.co-owners'} ne '') { + @currcoowners = split(',',$courseinfo{'internal.co-owners'}); + } + foreach my $key (keys(%roleshash)) { + if ($key =~ /^($match_username\:$match_domain)\:cc$/) { + my $cc = $1; + unless ($cc eq $owner) { + my ($result,$desc) = &Apache::lonnet::auto_validate_instcode($cnum,$cdom,$inst_code,$cc); + if ($result eq 'valid') { + if (@newcoowners > 0) { + unless (grep(/^\Q$cc\E$/,@newcoowners)) { + push(@newcoowners,$cc); + } + } else { + push(@newcoowners,$cc); + } + } + } + } + } + my @diffs = &Apache::loncommon::compare_arrays(\@currcoowners,\@newcoowners); + if (@diffs > 0) { + if (@newcoowners > 0) { + $changes{'internal.co-owners'} = join(',',@newcoowners); + $courseshash->{$chome}{$cid}{'co-owners'} = $changes{'internal.co-owners'}; + } else { + if ($courseinfo{'internal.co-owners'} ne '') { + if (&Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum) eq 'ok') { + print $fh "Former co-owner(s): $courseinfo{'internal.co-owners'} for official course: $inst_code (".$cdom."_".$cnum.") no longer active CCs, co-ownership status deleted.\n"; + } + } else { + print $fh "Error occurred when updating co-ownership in course's environment.db for ".$cdom."_".$cnum."\n"; + } + } + } elsif (@currcoowners > 0) { + $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'}; + } + } elsif ($courseinfo{'internal.co-owners'} ne '') { + $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'}; + } if (keys(%changes)) { - if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') { - print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: "; - foreach my $key (sort(keys(%changes))) { - print $fh "$key => $changes{$key} "; - } - print $fh "\n"; - } else { - print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n"; - } + if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') { + print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: "; + foreach my $key (sort(keys(%changes))) { + print $fh "$key => $changes{$key} "; + } + print $fh "\n"; + } else { + print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n"; + } } } } @@ -234,3 +281,5 @@ sub recurse_courses { return; } + +