File:  [LON-CAPA] / loncom / misc / refresh_courseids_db.pl
Revision 1.6: download - view: text, annotated - select for diffs
Fri Jul 30 17:46:12 2010 UTC (13 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Improved checking for changes in required LON-CAPA version for a course.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: #
    4: # $Id: refresh_courseids_db.pl,v 1.6 2010/07/30 17:46:12 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #################################################
   29: 
   30: =pod
   31: 
   32: =head1 NAME
   33: 
   34: refresh_courseids_db.pl
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: refresh_courseids_db.pl is run on a library server and gathers 
   39: course information for each course for which the current server is
   40: the home server.  Entries (excluding last access time) for each course 
   41: in nohist_courseids.db are updated.   
   42: 
   43: =head1 DESCRIPTION
   44: 
   45: refresh_courseids_db.pl will update course information, apart 
   46: from last access time, in nohist_courseids.db, using course data   
   47: from each course's environment.db file.
   48: 
   49: =cut
   50: 
   51: #################################################
   52: 
   53: use strict;
   54: use lib '/home/httpd/lib/perl/';
   55: use Apache::lonnet;
   56: use Apache::loncommon;
   57: use Apache::lonuserstate;
   58: use Apache::loncoursedata;
   59: use Apache::lonnavmaps;
   60: use LONCAPA qw(:DEFAULT :match);
   61: 
   62: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
   63: 
   64: use vars qw( %checkparms %checkresponsetypes %checkcrstypes %anonsurvey );
   65: 
   66: #  Make sure this process is running from user=www
   67: my $wwwid=getpwnam('www');
   68: if ($wwwid!=$<) {
   69:     my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
   70:     my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
   71:     system("echo 'User ID mismatch. refresh_courseids_db.pl must be run as user www.' |\
   72:  mail -s '$subj' $emailto > /dev/null");
   73:     exit 1;
   74: }
   75: #
   76: # Let people know we are running
   77: open(my $fh,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/refreshcourseids_db.log');
   78: print $fh "==== refresh_courseids_db.pl Run ".localtime()."====\n";
   79: 
   80: my @domains = sort(&Apache::lonnet::current_machine_domains());
   81: 
   82: &build_release_hashes();
   83: $env{'allowed.bre'} = 'F';
   84: 
   85: foreach my $dom (@domains) {
   86:     my %courseshash;
   87:     my @ids=&Apache::lonnet::current_machine_ids();
   88:     my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.');
   89:     my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   90:     my %domdesign = &Apache::loncommon::get_domainconf($dom);
   91:     my $autoassign = $domdesign{$dom.'.autoassign.co-owners'};
   92:     &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,$autoassign,$fh);
   93:     foreach my $lonhost (keys(%courseshash)) {
   94:         if (ref($courseshash{$lonhost}) eq 'HASH') {
   95:             if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
   96:                 print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
   97:             } else {
   98:                 print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
   99:             }
  100:         }
  101:     }
  102: }
  103: 
  104: delete($env{'allowed.bre'});
  105: 
  106: ## Finished!
  107: print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
  108: close($fh);
  109: 
  110: sub recurse_courses {
  111:     my ($cdom,$dir,$depth,$courseshash,$currhash,$autoassign,$fh) = @_;
  112:     next unless (ref($currhash) eq 'HASH');
  113:     if (-d $dir) {
  114:         opendir(DIR,$dir);
  115:         my @contents = grep(!/^\./,readdir(DIR));
  116:         closedir(DIR);
  117:         $depth ++;
  118:         foreach my $item (@contents) {
  119:             if ($depth < 4) {
  120:                 &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,
  121:                                  $currhash,$autoassign,$fh);
  122:             } elsif ($item =~ /^$match_courseid$/) {
  123:                 my $cnum = $item;
  124:                 my $cid = $cdom.'_'.$cnum;
  125:                 unless (ref($currhash->{$cid}) eq 'HASH') {
  126:                     my $is_course = 0;
  127:                     if (-e "$dir/$cnum/passwd") {
  128:                         if (open(my $pwfh,"<$dir/$cnum/passwd")) {
  129:                             while (<$pwfh>) {
  130:                                 if (/^none:/) {
  131:                                     $is_course = 1;
  132:                                     last;
  133:                                 }
  134:                             } 
  135:                         }
  136:                     }
  137:                     next unless ($is_course);
  138:                     my @stats = stat("$dir/$cnum/passwd");
  139:                     print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
  140:                 }
  141:                 my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
  142:                 my %changes = ();
  143:                 my $crstype = $courseinfo{'type'};
  144:                 if ($crstype eq '') {
  145:                     if ($cnum =~ /^$match_community$/) {
  146:                         $crstype = 'Community';
  147:                     } else {
  148:                         $crstype = 'Course';
  149:                     }
  150:                     $changes{'type'} = $crstype;
  151:                 }
  152:                 my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
  153:                 my $owner = $courseinfo{'internal.courseowner'};
  154:                 my (%roleshash,$gotcc,$reqdmajor,$reqdminor);
  155:                 if ($owner eq '') {
  156:                     %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
  157:                     $gotcc = 1;
  158:                     if (keys(%roleshash) == 1) {
  159:                         foreach my $key (keys(%roleshash)) {
  160:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
  161:                                 $owner = $1;
  162:                                 $changes{'internal.courseowner'} = $owner;
  163:                             }
  164:                         }
  165:                     }
  166:                 } elsif ($owner !~ /:/) {
  167:                     if ($owner =~ /^$match_username$/) {
  168:                         my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
  169:                         unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
  170:                             $owner .= ':'.$cdom;
  171:                             $changes{'internal.courseowner'} = $owner;
  172:                         }
  173:                     }
  174:                 }
  175:                 my $created = $courseinfo{'internal.created'};
  176:                 my $creator = $courseinfo{'internal.creator'};
  177:                 my $creationcontext = $courseinfo{'internal.creationcontext'};
  178:                 my $inst_code = $courseinfo{'internal.coursecode'};
  179:                 $inst_code = '' if (!defined($inst_code));
  180:                 $owner = '' if (!defined($owner));
  181:                 if ($created eq '') {
  182:                     if (ref($currhash->{$cid}) eq 'HASH') {
  183:                         $created = $currhash->{$cid}{'created'};
  184:                         $creator = $currhash->{$cid}{'creator'};
  185:                         $creationcontext = $currhash->{$cid}{'context'};
  186:                         unless ($created eq '') {
  187:                             $changes{'internal.created'} = $created;
  188:                         }
  189:                         if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
  190:                              $changes{'internal.creator'} = $creator;
  191:                         }
  192:                         unless ($creationcontext eq '') {
  193:                             $changes{'internal.creationcontext'} = $creationcontext;
  194:                         }
  195:                     }
  196:                     if ($created eq '') {
  197:                         if (-e "$dir/$cnum/passwd") {
  198:                             my @stats = stat("$dir/$cnum/passwd");
  199:                             $created = $stats[9];
  200:                         }
  201:                         my %lastaccess = 
  202:                             &Apache::lonnet::courselastaccess($cdom,$cnum);
  203:                         if ($lastaccess{$cid}) {
  204:                             if ($created eq '') {
  205:                                 $created = $lastaccess{$cid};
  206:                             } elsif ($lastaccess{$cid} < $created) {
  207:                                 $created = $lastaccess{$cid};
  208:                             }
  209:                         }
  210:                         unless ($created eq '') {
  211:                             $changes{'internal.created'} = $created;
  212:                         }
  213:                     }
  214:                 }
  215:                 
  216:                 $env{'request.course.id'} = $cdom.'_'.$cnum;
  217:                 $env{'request.role'} = 'cc./'.$cdom.'/'.$cnum;
  218:                 &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  219: 
  220:                 # check all parameters
  221:                 ($reqdmajor,$reqdminor) = &parameter_constraints($cnum,$cdom);
  222: 
  223:                 # check course type
  224:                 ($reqdmajor,$reqdminor) = &coursetype_constraints($cnum,$cdom,$crstype,
  225:                                                                  $reqdmajor,
  226:                                                                  $reqdminor);
  227:                 # check course contents
  228:                 ($reqdmajor,$reqdminor) = &coursecontent_constraints($cnum,$cdom,
  229:                                                                      $reqdmajor,
  230:                                                                      $reqdminor);
  231:                 delete($env{'request.course.id'});
  232:                 delete($env{'request.role'});
  233: 
  234:                 unless ($chome eq 'no_host') {
  235:                     $courseshash->{$chome}{$cid} = {
  236:                         description => $courseinfo{'description'},
  237:                         inst_code   => $inst_code,
  238:                         owner       => $owner,
  239:                         type        => $crstype,
  240:                     };
  241:                     if ($creator ne '') {
  242:                         $courseshash->{$chome}{$cid}{'creator'} = $creator;
  243:                     }
  244:                     if ($created ne '') {
  245:                         $courseshash->{$chome}{$cid}{'created'} = $created;
  246:                     }
  247:                     if ($creationcontext ne '') {
  248:                         $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
  249:                     }
  250:                     if (($inst_code ne '') && ($autoassign)) {
  251:                         unless ($gotcc) {
  252:                             %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
  253:                         }
  254:                         my @currcoowners;
  255:                         my @newcoowners;
  256:                         if ($courseinfo{'internal.co-owners'} ne '') {
  257:                             @currcoowners = split(',',$courseinfo{'internal.co-owners'});
  258:                         }
  259:                         foreach my $key (keys(%roleshash)) {
  260:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
  261:                                 my $cc = $1;
  262:                                 unless ($cc eq $owner) {
  263:                                     my ($result,$desc) = &Apache::lonnet::auto_validate_instcode($cnum,$cdom,$inst_code,$cc);
  264:                                     if ($result eq 'valid') {
  265:                                         if (@newcoowners > 0) {
  266:                                             unless (grep(/^\Q$cc\E$/,@newcoowners)) { 
  267:                                                 push(@newcoowners,$cc);
  268:                                             }
  269:                                         } else {
  270:                                             push(@newcoowners,$cc);
  271:                                         }
  272:                                     }
  273:                                 }
  274:                             }
  275:                         }
  276:                         my @diffs = &Apache::loncommon::compare_arrays(\@currcoowners,\@newcoowners);
  277:                         if (@diffs > 0) {
  278:                             if (@newcoowners > 0) {
  279:                                 $changes{'internal.co-owners'} = join(',',@newcoowners);
  280:                                 $courseshash->{$chome}{$cid}{'co-owners'} = $changes{'internal.co-owners'};
  281:                             } else {
  282:                                 if ($courseinfo{'internal.co-owners'} ne '') {
  283:                                     if (&Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum) eq 'ok') {
  284:                                         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";
  285:                                     }
  286:                                 } else {
  287:                                     print $fh "Error occurred when updating co-ownership in course's environment.db for ".$cdom."_".$cnum."\n";
  288:                                 }
  289:                             }
  290:                         } elsif (@currcoowners > 0) {
  291:                             $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
  292:                         }
  293:                     } elsif ($courseinfo{'internal.co-owners'} ne '') {
  294:                         $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
  295:                     }
  296:                     foreach my $item ('categories','cloners','hidefromcat') {
  297:                         if ($courseinfo{$item} ne '') {
  298:                             $courseshash->{$chome}{$cid}{$item} = $courseinfo{$item}; 
  299:                         }
  300:                     }
  301:                     foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
  302:                         if ($courseinfo{'internal.'.$item} ne '') {
  303:                             $courseshash->{$chome}{$cid}{$item} =
  304:                                 $courseinfo{'internal.'.$item};
  305:                         }
  306:                     }
  307:                     if ($reqdmajor eq '' && $reqdminor eq '') {
  308:                         if ($courseinfo{'internal.releaserequired'} ne '') {
  309:                             $changes{'internal.releaserequired'} = '';
  310:                         }
  311:                     } else {
  312:                         my $releasereq =  $reqdmajor.'.'.$reqdminor;
  313:                         $courseshash->{$chome}{$cid}{'releaserequired'} = $releasereq;
  314:                         if ($courseinfo{'internal.releaserequired'} eq '') {
  315:                             $changes{'internal.releaserequired'} = $releasereq;
  316:                         } else {
  317:                             if ($courseinfo{'internal.releaserequired'} ne $releasereq) {
  318:                         
  319:                                 $changes{'internal.releaserequired'} = $releasereq;
  320:                             }
  321:                         }
  322:                     }
  323:                     if (keys(%changes)) {
  324:                         if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
  325:                             print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
  326:                             foreach my $key (sort(keys(%changes))) {
  327:                                 print $fh "$key => $changes{$key} ";
  328:                             }
  329:                             print $fh "\n";
  330:                         } else {
  331:                             print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
  332:                         }
  333:                     }
  334:                 }
  335:             }
  336:         }
  337:     }
  338:     return;
  339: }
  340: 
  341: sub parameter_constraints {
  342:     my ($cnum,$cdom) = @_;
  343:     my ($reqdmajor,$reqdminor);
  344:     my $resourcedata=&read_paramdata($cnum,$cdom);
  345:     if (ref($resourcedata) eq 'HASH') {
  346:         foreach my $key (keys(%{$resourcedata})) { 
  347:             foreach my $item (keys(%checkparms)) {
  348:                 if ($key =~ /(\Q$item\E)$/) {
  349:                     if (ref($checkparms{$item}) eq 'ARRAY') {
  350:                         my $value = $resourcedata->{$key};
  351:                         if (grep(/^\Q$value\E$/,@{$checkparms{$item}})) {
  352:                             my ($major,$minor) = split(/\./,$Apache::lonnet::needsrelease{'parameter:'.$item.':'.$value});
  353:                             ($reqdmajor,$reqdminor) = 
  354:                                 &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
  355:                         }
  356:                     }
  357:                 }
  358:             }
  359:         }
  360:     }
  361:     return ($reqdmajor,$reqdminor);
  362: }
  363: 
  364: sub coursetype_constraints {
  365:     my ($cnum,$cdom,$crstype,$reqdmajor,$reqdminor) = @_;
  366:     if (defined($checkcrstypes{$crstype})) {
  367:         my ($major,$minor) = split(/\./,$checkcrstypes{$crstype});
  368:         ($reqdmajor,$reqdminor) = 
  369:             &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
  370:     }
  371:     return ($reqdmajor,$reqdminor);
  372: }
  373: 
  374: sub coursecontent_constraints {
  375:     my ($cnum,$cdom,$reqdmajor,$reqdminor) = @_;
  376:     my $navmap = Apache::lonnavmaps::navmap->new();
  377:     if (defined($navmap)) {
  378:         my %resourcetracker =  &Apache::lonnet::dump('nohist_resourcetracker',
  379:                                                      $cdom,$cnum);
  380:         my %allresponses;
  381:         my $anonsurv_subm;
  382:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
  383:             my %responses = $res->responseTypes();
  384:             foreach my $key (keys(%responses)) {
  385:                 next unless(exists($checkresponsetypes{$key}));
  386:                 $allresponses{$key} += $responses{$key};
  387:             }
  388:             my @parts = @{$res->parts()};
  389:             my $symb = $res->symb();
  390:             foreach my $part (@parts) {
  391:                 if (exists($resourcetracker{$symb."\0".$part."\0anonymous"})) {
  392:                     $anonsurv_subm = 1;
  393:                 }
  394:             }
  395:         }
  396:         foreach my $key (keys(%allresponses)) {
  397:             my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
  398:             ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
  399:         }
  400:         if ($anonsurv_subm) {
  401:             ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($anonsurvey{major},
  402:                                           $anonsurvey{minor},$reqdmajor,$reqdminor);
  403:         }
  404:     }
  405:     return ($reqdmajor,$reqdminor);
  406: }
  407: 
  408: sub update_reqd_loncaparev {
  409:     my ($major,$minor,$reqdmajor,$reqdminor) = @_;
  410:     if (($major ne '' && $major !~ /\D/) & ($minor ne '' && $minor !~ /\D/)) {
  411:         if ($reqdmajor eq '' || $reqdminor eq '') {
  412:             $reqdmajor = $major;
  413:             $reqdminor = $minor;
  414:         } elsif (($major > $reqdmajor) ||
  415:             ($major == $reqdmajor && $minor > $reqdminor))  {
  416:             $reqdmajor = $major;
  417:             $reqdminor = $minor;
  418:         }
  419:     }
  420:     return ($reqdmajor,$reqdminor);
  421: }
  422: 
  423: sub read_paramdata {
  424:     my ($cnum,$dom)=@_;
  425:     my $resourcedata=&Apache::lonnet::get_courseresdata($cnum,$dom);
  426:     my $classlist=&Apache::loncoursedata::get_classlist();
  427:     foreach my $student (keys(%{$classlist})) {
  428:         if ($student =~/^($LONCAPA::match_username)\:($LONCAPA::match_domain)$/) {
  429:             my ($tuname,$tudom)=($1,$2);
  430:             my $useropt=&Apache::lonnet::get_userresdata($tuname,$tudom);
  431:             foreach my $userkey (keys(%{$useropt})) {
  432:                 if ($userkey=~/^$env{'request.course.id'}/) {
  433:                     my $newkey=$userkey;
  434:                     $newkey=~s/^($env{'request.course.id'}\.)/$1\[useropt\:$tuname\:$tudom\]\./;
  435:                     $$resourcedata{$newkey}=$$useropt{$userkey};
  436:                 }
  437:             }
  438:          }
  439:     }
  440:     return $resourcedata;
  441: }
  442: 
  443: sub build_release_hashes {
  444:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
  445:         my ($item,$name,$value) = split(/:/,$key);
  446:         if ($item eq 'parameter') {
  447:             if (ref($checkparms{$name}) eq 'ARRAY') {
  448:                 unless(grep(/^\Q$name\E$/,@{$checkparms{$name}})) {
  449:                     push(@{$checkparms{$name}},$value);
  450:                 }
  451:             } else {
  452:                 push(@{$checkparms{$name}},$value);
  453:             }
  454:         } elsif ($item eq 'resourcetag') {
  455:             if ($name eq 'responsetype') {
  456:                 $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
  457:             }
  458:         } elsif ($item eq 'course') {
  459:             if ($name eq 'crstype') {
  460:                 $checkcrstypes{$value} = $Apache::lonnet::needsrelease{$key};
  461:             }
  462:         }
  463:     }
  464:     ($anonsurvey{major},$anonsurvey{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:anonsurvey'});
  465:     return;
  466: }
  467: 

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