File:  [LON-CAPA] / loncom / misc / refresh_courseids_db.pl
Revision 1.8: download - view: text, annotated - select for diffs
Sun Aug 22 21:14:54 2010 UTC (13 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC1, HEAD
- Only check courses with last access within past two days when updating
  LON-CAPA release required in nohist_courseids.db.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: #
    4: # $Id: refresh_courseids_db.pl,v 1.8 2010/08/22 21:14:54 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: my @ids=&Apache::lonnet::current_machine_ids();
   82: 
   83: &build_release_hashes();
   84: $env{'allowed.bre'} = 'F';
   85: 
   86: foreach my $dom (@domains) {
   87:     my %courseshash;
   88:     my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.');
   89:     my %lastaccess = &Apache::lonnet::courselastaccess($dom,undef,\@ids);
   90:     my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   91:     my %domdesign = &Apache::loncommon::get_domainconf($dom);
   92:     my $autoassign = $domdesign{$dom.'.autoassign.co-owners'};
   93:     &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,\%lastaccess,$autoassign,$fh);
   94:     foreach my $lonhost (keys(%courseshash)) {
   95:         if (ref($courseshash{$lonhost}) eq 'HASH') {
   96:             if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
   97:                 print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
   98:             } else {
   99:                 print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
  100:             }
  101:         }
  102:     }
  103: }
  104: 
  105: delete($env{'allowed.bre'});
  106: 
  107: ## Finished!
  108: print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
  109: close($fh);
  110: 
  111: sub recurse_courses {
  112:     my ($cdom,$dir,$depth,$courseshash,$currhash,$lastaccess,$autoassign,$fh) = @_;
  113:     next unless (ref($currhash) eq 'HASH');
  114:     if (-d $dir) {
  115:         opendir(DIR,$dir);
  116:         my @contents = grep(!/^\./,readdir(DIR));
  117:         closedir(DIR);
  118:         $depth ++;
  119:         foreach my $item (@contents) {
  120:             if ($depth < 4) {
  121:                 &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,
  122:                                  $currhash,$lastaccess,$autoassign,$fh);
  123:             } elsif ($item =~ /^$match_courseid$/) {
  124:                 my $cnum = $item;
  125:                 my $cid = $cdom.'_'.$cnum;
  126:                 unless (ref($currhash->{$cid}) eq 'HASH') {
  127:                     my $is_course = 0;
  128:                     if (-e "$dir/$cnum/passwd") {
  129:                         if (open(my $pwfh,"<$dir/$cnum/passwd")) {
  130:                             while (<$pwfh>) {
  131:                                 if (/^none:/) {
  132:                                     $is_course = 1;
  133:                                     last;
  134:                                 }
  135:                             } 
  136:                         }
  137:                     }
  138:                     next unless ($is_course);
  139:                     my @stats = stat("$dir/$cnum/passwd");
  140:                     print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
  141:                 }
  142:                 my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
  143:                 my %changes = ();
  144:                 my $crstype = $courseinfo{'type'};
  145:                 if ($crstype eq '') {
  146:                     if ($cnum =~ /^$match_community$/) {
  147:                         $crstype = 'Community';
  148:                     } else {
  149:                         $crstype = 'Course';
  150:                     }
  151:                     $changes{'type'} = $crstype;
  152:                 }
  153:                 my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
  154:                 my $owner = $courseinfo{'internal.courseowner'};
  155:                 my $twodaysago = time - 172800;
  156:                 my (%roleshash,$gotcc,$reqdmajor,$reqdminor);
  157:                 if ($owner eq '') {
  158:                     %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
  159:                     $gotcc = 1;
  160:                     if (keys(%roleshash) == 1) {
  161:                         foreach my $key (keys(%roleshash)) {
  162:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
  163:                                 $owner = $1;
  164:                                 $changes{'internal.courseowner'} = $owner;
  165:                             }
  166:                         }
  167:                     }
  168:                 } elsif ($owner !~ /:/) {
  169:                     if ($owner =~ /^$match_username$/) {
  170:                         my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
  171:                         unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
  172:                             $owner .= ':'.$cdom;
  173:                             $changes{'internal.courseowner'} = $owner;
  174:                         }
  175:                     }
  176:                 }
  177:                 my $created = $courseinfo{'internal.created'};
  178:                 my $creator = $courseinfo{'internal.creator'};
  179:                 my $creationcontext = $courseinfo{'internal.creationcontext'};
  180:                 my $inst_code = $courseinfo{'internal.coursecode'};
  181:                 my $releaserequired = $courseinfo{'internal.releaserequired'};
  182:                 $inst_code = '' if (!defined($inst_code));
  183:                 $owner = '' if (!defined($owner));
  184:                 if ($created eq '') {
  185:                     if (ref($currhash->{$cid}) eq 'HASH') {
  186:                         $created = $currhash->{$cid}{'created'};
  187:                         $creator = $currhash->{$cid}{'creator'};
  188:                         $creationcontext = $currhash->{$cid}{'context'};
  189:                         unless ($created eq '') {
  190:                             $changes{'internal.created'} = $created;
  191:                         }
  192:                         if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
  193:                              $changes{'internal.creator'} = $creator;
  194:                         }
  195:                         unless ($creationcontext eq '') {
  196:                             $changes{'internal.creationcontext'} = $creationcontext;
  197:                         }
  198:                     }
  199:                     if ($created eq '') {
  200:                         if (-e "$dir/$cnum/passwd") {
  201:                             my @stats = stat("$dir/$cnum/passwd");
  202:                             $created = $stats[9];
  203:                         }
  204:                         if ($lastaccess->{$cid}) {
  205:                             if ($created eq '') {
  206:                                 $created = $lastaccess->{$cid};
  207:                             } elsif ($lastaccess->{$cid} < $created) {
  208:                                 $created = $lastaccess->{$cid};
  209:                             }
  210:                         }
  211:                         unless ($created eq '') {
  212:                             $changes{'internal.created'} = $created;
  213:                         }
  214:                     }
  215:                 }
  216:                  
  217:                 if (($chome ne '')  && ($lastaccess->{$cid} > $twodaysago)) {
  218:                     $env{'request.course.id'} = $cdom.'_'.$cnum;
  219:                     $env{'request.role'} = 'cc./'.$cdom.'/'.$cnum;
  220:                     &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  221: 
  222:                     # check all parameters
  223:                     ($reqdmajor,$reqdminor) = &parameter_constraints($cnum,$cdom);
  224: 
  225:                     # check course type
  226:                     ($reqdmajor,$reqdminor) = &coursetype_constraints($cnum,$cdom,$crstype,
  227:                                                                       $reqdmajor,
  228:                                                                       $reqdminor);
  229:                     # check course contents
  230:                     ($reqdmajor,$reqdminor) = &coursecontent_constraints($cnum,$cdom,
  231:                                                                          $reqdmajor,
  232:                                                                          $reqdminor);
  233:                     delete($env{'request.course.id'});
  234:                     delete($env{'request.role'});
  235:                 } elsif ($releaserequired) {
  236:                     ($reqdmajor,$reqdminor) = split(/\./,$releaserequired);
  237:                 }
  238: 
  239:                 unless ($chome eq 'no_host') {
  240:                     $courseshash->{$chome}{$cid} = {
  241:                         description => $courseinfo{'description'},
  242:                         inst_code   => $inst_code,
  243:                         owner       => $owner,
  244:                         type        => $crstype,
  245:                     };
  246:                     if ($creator ne '') {
  247:                         $courseshash->{$chome}{$cid}{'creator'} = $creator;
  248:                     }
  249:                     if ($created ne '') {
  250:                         $courseshash->{$chome}{$cid}{'created'} = $created;
  251:                     }
  252:                     if ($creationcontext ne '') {
  253:                         $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
  254:                     }
  255:                     if (($inst_code ne '') && ($autoassign)) {
  256:                         unless ($gotcc) {
  257:                             %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
  258:                         }
  259:                         my @currcoowners;
  260:                         my @newcoowners;
  261:                         if ($courseinfo{'internal.co-owners'} ne '') {
  262:                             @currcoowners = split(',',$courseinfo{'internal.co-owners'});
  263:                         }
  264:                         foreach my $key (keys(%roleshash)) {
  265:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
  266:                                 my $cc = $1;
  267:                                 unless ($cc eq $owner) {
  268:                                     my ($result,$desc) = &Apache::lonnet::auto_validate_instcode($cnum,$cdom,$inst_code,$cc);
  269:                                     if ($result eq 'valid') {
  270:                                         if (@newcoowners > 0) {
  271:                                             unless (grep(/^\Q$cc\E$/,@newcoowners)) { 
  272:                                                 push(@newcoowners,$cc);
  273:                                             }
  274:                                         } else {
  275:                                             push(@newcoowners,$cc);
  276:                                         }
  277:                                     }
  278:                                 }
  279:                             }
  280:                         }
  281:                         my @diffs = &Apache::loncommon::compare_arrays(\@currcoowners,\@newcoowners);
  282:                         if (@diffs > 0) {
  283:                             if (@newcoowners > 0) {
  284:                                 $changes{'internal.co-owners'} = join(',',@newcoowners);
  285:                                 $courseshash->{$chome}{$cid}{'co-owners'} = $changes{'internal.co-owners'};
  286:                             } else {
  287:                                 if ($courseinfo{'internal.co-owners'} ne '') {
  288:                                     if (&Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum) eq 'ok') {
  289:                                         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";
  290:                                     }
  291:                                 } else {
  292:                                     print $fh "Error occurred when updating co-ownership in course's environment.db for ".$cdom."_".$cnum."\n";
  293:                                 }
  294:                             }
  295:                         } elsif (@currcoowners > 0) {
  296:                             $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
  297:                         }
  298:                     } elsif ($courseinfo{'internal.co-owners'} ne '') {
  299:                         $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
  300:                     }
  301:                     foreach my $item ('categories','cloners','hidefromcat') {
  302:                         if ($courseinfo{$item} ne '') {
  303:                             $courseshash->{$chome}{$cid}{$item} = $courseinfo{$item}; 
  304:                         }
  305:                     }
  306:                     foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
  307:                         if ($courseinfo{'internal.'.$item} ne '') {
  308:                             $courseshash->{$chome}{$cid}{$item} =
  309:                                 $courseinfo{'internal.'.$item};
  310:                         }
  311:                     }
  312:                     if ($reqdmajor eq '' && $reqdminor eq '') {
  313:                         if ($courseinfo{'internal.releaserequired'} ne '') {
  314:                             $changes{'internal.releaserequired'} = '';
  315:                         }
  316:                     } else {
  317:                         my $releasereq =  $reqdmajor.'.'.$reqdminor;
  318:                         $courseshash->{$chome}{$cid}{'releaserequired'} = $releasereq;
  319:                         if ($courseinfo{'internal.releaserequired'} eq '') {
  320:                             $changes{'internal.releaserequired'} = $releasereq;
  321:                         } else {
  322:                             if ($courseinfo{'internal.releaserequired'} ne $releasereq) {
  323:                         
  324:                                 $changes{'internal.releaserequired'} = $releasereq;
  325:                             }
  326:                         }
  327:                     }
  328:                     if (keys(%changes)) {
  329:                         if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
  330:                             print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
  331:                             foreach my $key (sort(keys(%changes))) {
  332:                                 print $fh "$key => $changes{$key} ";
  333:                             }
  334:                             print $fh "\n";
  335:                         } else {
  336:                             print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
  337:                         }
  338:                     }
  339:                 }
  340:             }
  341:         }
  342:     }
  343:     return;
  344: }
  345: 
  346: sub parameter_constraints {
  347:     my ($cnum,$cdom) = @_;
  348:     my ($reqdmajor,$reqdminor);
  349:     my $resourcedata=&read_paramdata($cnum,$cdom);
  350:     if (ref($resourcedata) eq 'HASH') {
  351:         foreach my $key (keys(%{$resourcedata})) { 
  352:             foreach my $item (keys(%checkparms)) {
  353:                 if ($key =~ /(\Q$item\E)$/) {
  354:                     if (ref($checkparms{$item}) eq 'ARRAY') {
  355:                         my $value = $resourcedata->{$key};
  356:                         if (grep(/^\Q$value\E$/,@{$checkparms{$item}})) {
  357:                             my ($major,$minor) = split(/\./,$Apache::lonnet::needsrelease{'parameter:'.$item.':'.$value});
  358:                             ($reqdmajor,$reqdminor) = 
  359:                                 &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
  360:                         }
  361:                     }
  362:                 }
  363:             }
  364:         }
  365:     }
  366:     return ($reqdmajor,$reqdminor);
  367: }
  368: 
  369: sub coursetype_constraints {
  370:     my ($cnum,$cdom,$crstype,$reqdmajor,$reqdminor) = @_;
  371:     if (defined($checkcrstypes{$crstype})) {
  372:         my ($major,$minor) = split(/\./,$checkcrstypes{$crstype});
  373:         ($reqdmajor,$reqdminor) = 
  374:             &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
  375:     }
  376:     return ($reqdmajor,$reqdminor);
  377: }
  378: 
  379: sub coursecontent_constraints {
  380:     my ($cnum,$cdom,$reqdmajor,$reqdminor) = @_;
  381:     my $navmap = Apache::lonnavmaps::navmap->new();
  382:     if (defined($navmap)) {
  383:         my %anonsubmissions =  &Apache::lonnet::dump('nohist_anonsurveys',
  384:                                                      $cdom,$cnum);
  385:         my %allresponses;
  386:         my $anonsurv_subm;
  387:         foreach my $res ($navmap->retrieveResources(undef,sub { $_[0]->is_problem() },1,0)) {
  388:             my %responses = $res->responseTypes();
  389:             foreach my $key (keys(%responses)) {
  390:                 next unless(exists($checkresponsetypes{$key}));
  391:                 $allresponses{$key} += $responses{$key};
  392:             }
  393:             my @parts = @{$res->parts()};
  394:             my $symb = $res->symb();
  395:             foreach my $part (@parts) {
  396:                 if (exists($anonsubmissions{$symb."\0".$part})) {
  397:                     $anonsurv_subm = 1;
  398:                 }
  399:             }
  400:         }
  401:         foreach my $key (keys(%allresponses)) {
  402:             my ($major,$minor) = split(/\./,$checkresponsetypes{$key});
  403:             ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($major,$minor,$reqdmajor,$reqdminor);
  404:         }
  405:         if ($anonsurv_subm) {
  406:             ($reqdmajor,$reqdminor) = &update_reqd_loncaparev($anonsurvey{major},
  407:                                           $anonsurvey{minor},$reqdmajor,$reqdminor);
  408:         }
  409:     }
  410:     return ($reqdmajor,$reqdminor);
  411: }
  412: 
  413: sub update_reqd_loncaparev {
  414:     my ($major,$minor,$reqdmajor,$reqdminor) = @_;
  415:     if (($major ne '' && $major !~ /\D/) & ($minor ne '' && $minor !~ /\D/)) {
  416:         if ($reqdmajor eq '' || $reqdminor eq '') {
  417:             $reqdmajor = $major;
  418:             $reqdminor = $minor;
  419:         } elsif (($major > $reqdmajor) ||
  420:             ($major == $reqdmajor && $minor > $reqdminor))  {
  421:             $reqdmajor = $major;
  422:             $reqdminor = $minor;
  423:         }
  424:     }
  425:     return ($reqdmajor,$reqdminor);
  426: }
  427: 
  428: sub read_paramdata {
  429:     my ($cnum,$dom)=@_;
  430:     my $resourcedata=&Apache::lonnet::get_courseresdata($cnum,$dom);
  431:     my $classlist=&Apache::loncoursedata::get_classlist();
  432:     foreach my $student (keys(%{$classlist})) {
  433:         if ($student =~/^($LONCAPA::match_username)\:($LONCAPA::match_domain)$/) {
  434:             my ($tuname,$tudom)=($1,$2);
  435:             my $useropt=&Apache::lonnet::get_userresdata($tuname,$tudom);
  436:             foreach my $userkey (keys(%{$useropt})) {
  437:                 if ($userkey=~/^$env{'request.course.id'}/) {
  438:                     my $newkey=$userkey;
  439:                     $newkey=~s/^($env{'request.course.id'}\.)/$1\[useropt\:$tuname\:$tudom\]\./;
  440:                     $$resourcedata{$newkey}=$$useropt{$userkey};
  441:                 }
  442:             }
  443:          }
  444:     }
  445:     return $resourcedata;
  446: }
  447: 
  448: sub build_release_hashes {
  449:     foreach my $key (keys(%Apache::lonnet::needsrelease)) {
  450:         my ($item,$name,$value) = split(/:/,$key);
  451:         if ($item eq 'parameter') {
  452:             if (ref($checkparms{$name}) eq 'ARRAY') {
  453:                 unless(grep(/^\Q$name\E$/,@{$checkparms{$name}})) {
  454:                     push(@{$checkparms{$name}},$value);
  455:                 }
  456:             } else {
  457:                 push(@{$checkparms{$name}},$value);
  458:             }
  459:         } elsif ($item eq 'resourcetag') {
  460:             if ($name eq 'responsetype') {
  461:                 $checkresponsetypes{$value} = $Apache::lonnet::needsrelease{$key}
  462:             }
  463:         } elsif ($item eq 'course') {
  464:             if ($name eq 'crstype') {
  465:                 $checkcrstypes{$value} = $Apache::lonnet::needsrelease{$key};
  466:             }
  467:         }
  468:     }
  469:     ($anonsurvey{major},$anonsurvey{minor}) = split(/\./,$Apache::lonnet::needsrelease{'parameter:type:anonsurvey'});
  470:     return;
  471: }
  472: 

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