File:  [LON-CAPA] / loncom / misc / refresh_courseids_db.pl
Revision 1.19: download - view: text, annotated - select for diffs
Sat Jun 7 19:13:51 2014 UTC (9 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_1, version_2_11_0, HEAD
- Routines moved from refresh_courseids_db.pl to lonrelrequtils.pm
  to facilitate re-use: parameter_constraints(), coursetype_constraints(),
  commblock_constraints(), coursecontent_constraints(), read_paramdata(),
  update_reqd_loncaparev().
- Clean-up handler phase for lonblockingmenu.pm and courseprefs.pm
  (query string contains phase=releaseinfo) includes call to update
  LON-CAPA version requirement in course's environment.db and course's
  record in domain's nohost_courseids.db
- Display of details for LON-CAPA version requirement in course now
  includes any requirements based on blocking (timer trigger, printout
  or content access).

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: #
    4: # $Id: refresh_courseids_db.pl,v 1.19 2014/06/07 19:13:51 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 Apache::lonrelrequtils;
   61: use LONCAPA qw(:DEFAULT :match);
   62: 
   63: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
   64: 
   65: #  Make sure this process is running from user=www
   66: my $wwwid=getpwnam('www');
   67: if ($wwwid!=$<) {
   68:     my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
   69:     my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
   70:     system("echo 'User ID mismatch. refresh_courseids_db.pl must be run as user www.' |\
   71:  mail -s '$subj' $emailto > /dev/null");
   72:     exit 1;
   73: }
   74: #
   75: # Let people know we are running
   76: open(my $fh,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/refreshcourseids_db.log');
   77: print $fh "==== refresh_courseids_db.pl Run ".localtime()."====\n";
   78: 
   79: my @domains = sort(&Apache::lonnet::current_machine_domains());
   80: my @ids=&Apache::lonnet::current_machine_ids();
   81: 
   82: &Apache::lonrelrequtils::init_global_hashes();
   83: my $globals_set = 1;
   84: 
   85: $env{'allowed.bre'} = 'F';
   86: 
   87: foreach my $dom (@domains) {
   88:     $env{'user.domain'} = $dom;
   89:     $env{'user.name'} = &Apache::lonnet::get_domainconfiguser($dom);
   90:     my %courseshash;
   91:     my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.');
   92:     my %lastaccess = &Apache::lonnet::courselastaccess($dom,undef,\@ids);
   93:     my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   94:     my %domdesign = &Apache::loncommon::get_domainconf($dom);
   95:     my $autoassign = $domdesign{$dom.'.autoassign.co-owners'};
   96:     &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,\%lastaccess,$autoassign,$fh);
   97:     foreach my $lonhost (keys(%courseshash)) {
   98:         if (ref($courseshash{$lonhost}) eq 'HASH') {
   99:             if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
  100:                 print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
  101:             } else {
  102:                 print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
  103:             }
  104:         }
  105:     }
  106:     delete($env{'user.name'});
  107:     delete($env{'user.domain'});
  108: }
  109: 
  110: delete($env{'allowed.bre'});
  111: 
  112: ## Finished!
  113: print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
  114: close($fh);
  115: 
  116: sub recurse_courses {
  117:     my ($cdom,$dir,$depth,$courseshash,$currhash,$lastaccess,$autoassign,$fh) = @_;
  118:     next unless (ref($currhash) eq 'HASH');
  119:     if (-d $dir) {
  120:         opendir(DIR,$dir);
  121:         my @contents = grep(!/^\./,readdir(DIR));
  122:         closedir(DIR);
  123:         $depth ++;
  124:         foreach my $item (@contents) {
  125:             if ($depth < 4) {
  126:                 &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,
  127:                                  $currhash,$lastaccess,$autoassign,$fh);
  128:             } elsif ($item =~ /^$match_courseid$/) {
  129:                 my $cnum = $item;
  130:                 my $cid = $cdom.'_'.$cnum;
  131:                 unless (ref($currhash->{$cid}) eq 'HASH') {
  132:                     my $is_course = 0;
  133:                     if (-e "$dir/$cnum/passwd") {
  134:                         if (open(my $pwfh,"<$dir/$cnum/passwd")) {
  135:                             while (<$pwfh>) {
  136:                                 if (/^none:/) {
  137:                                     $is_course = 1;
  138:                                     last;
  139:                                 }
  140:                             } 
  141:                         }
  142:                     }
  143:                     next unless ($is_course);
  144:                     my @stats = stat("$dir/$cnum/passwd");
  145:                     print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
  146:                 }
  147:                 my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
  148:                 my %changes = ();
  149:                 my $crstype = $courseinfo{'type'};
  150:                 if ($crstype eq '') {
  151:                     if ($cnum =~ /^$match_community$/) {
  152:                         $crstype = 'Community';
  153:                     } else {
  154:                         $crstype = 'Course';
  155:                     }
  156:                     $changes{'type'} = $crstype;
  157:                 }
  158:                 my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
  159:                 my $owner = $courseinfo{'internal.courseowner'};
  160:                 my $twodaysago = time - 172800;
  161:                 my (%roleshash,$gotcc,$reqdmajor,$reqdminor);
  162:                 if ($owner eq '') {
  163:                     %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
  164:                     $gotcc = 1;
  165:                     if (keys(%roleshash) == 1) {
  166:                         foreach my $key (keys(%roleshash)) {
  167:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
  168:                                 $owner = $1;
  169:                                 $changes{'internal.courseowner'} = $owner;
  170:                             }
  171:                         }
  172:                     }
  173:                 } elsif ($owner !~ /:/) {
  174:                     if ($owner =~ /^$match_username$/) {
  175:                         my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
  176:                         unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
  177:                             $owner .= ':'.$cdom;
  178:                             $changes{'internal.courseowner'} = $owner;
  179:                         }
  180:                     }
  181:                 }
  182:                 my $created = $courseinfo{'internal.created'};
  183:                 my $creator = $courseinfo{'internal.creator'};
  184:                 my $creationcontext = $courseinfo{'internal.creationcontext'};
  185:                 my $inst_code = $courseinfo{'internal.coursecode'};
  186:                 my $releaserequired = $courseinfo{'internal.releaserequired'};
  187:                 my $uniquecode = $courseinfo{'internal.uniquecode'};
  188:                 $inst_code = '' if (!defined($inst_code));
  189:                 $owner = '' if (!defined($owner));
  190:                 $uniquecode = '' if (!defined($uniquecode));
  191:                 if ($created eq '') {
  192:                     if (ref($currhash->{$cid}) eq 'HASH') {
  193:                         $created = $currhash->{$cid}{'created'};
  194:                         $creator = $currhash->{$cid}{'creator'};
  195:                         $creationcontext = $currhash->{$cid}{'context'};
  196:                         unless ($created eq '') {
  197:                             $changes{'internal.created'} = $created;
  198:                         }
  199:                         if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
  200:                              $changes{'internal.creator'} = $creator;
  201:                         }
  202:                         unless ($creationcontext eq '') {
  203:                             $changes{'internal.creationcontext'} = $creationcontext;
  204:                         }
  205:                     }
  206:                     if ($created eq '') {
  207:                         if (-e "$dir/$cnum/passwd") {
  208:                             my @stats = stat("$dir/$cnum/passwd");
  209:                             $created = $stats[9];
  210:                         }
  211:                         if ($lastaccess->{$cid}) {
  212:                             if ($created eq '') {
  213:                                 $created = $lastaccess->{$cid};
  214:                             } elsif ($lastaccess->{$cid} < $created) {
  215:                                 $created = $lastaccess->{$cid};
  216:                             }
  217:                         }
  218:                         unless ($created eq '') {
  219:                             $changes{'internal.created'} = $created;
  220:                         }
  221:                     }
  222:                 }
  223:                  
  224:                 if (($chome ne '')  && ($lastaccess->{$cid} > $twodaysago)) {
  225:                     $env{'request.course.id'} = $cdom.'_'.$cnum;
  226:                     $env{'request.role'} = 'cc./'.$cdom.'/'.$cnum;
  227: 
  228:                     my $readmap = 1;
  229:                     ($reqdmajor,$reqdminor) = &Apache::lonrelrequtils::get_release_req($cnum,$cdom,
  230:                                                                                        $crstype,$readmap,
  231:                                                                                        $globals_set);
  232:                     delete($env{'request.course.id'});
  233:                     delete($env{'request.role'});
  234:                 } elsif ($releaserequired) {
  235:                     ($reqdmajor,$reqdminor) = split(/\./,$releaserequired);
  236:                 }
  237: 
  238:                 unless ($chome eq 'no_host') {
  239:                     if (($lastaccess->{$cid} eq '') ||
  240:                         ($lastaccess->{$cid} > $twodaysago)) {
  241:                         my $contentchange;
  242:                         if ($courseinfo{'internal.created'} eq '') {
  243:                             $contentchange = &last_map_update($cnum,$cdom);
  244:                         } else {
  245:                             unless ($courseinfo{'internal.created'} > $lastaccess->{$cid}) {
  246:                                 $contentchange = &last_map_update($cnum,$cdom);
  247:                             }
  248:                         }
  249:                         if (($contentchange) && ($contentchange > $courseinfo{'internal.contentchange'})) {
  250:                             $changes{'internal.contentchange'} = $contentchange;
  251:                         }
  252:                     }
  253:                     $courseshash->{$chome}{$cid} = {
  254:                         description => $courseinfo{'description'},
  255:                         inst_code   => $inst_code,
  256:                         owner       => $owner,
  257:                         type        => $crstype,
  258:                     };
  259:                     if ($creator ne '') {
  260:                         $courseshash->{$chome}{$cid}{'creator'} = $creator;
  261:                     }
  262:                     if ($created ne '') {
  263:                         $courseshash->{$chome}{$cid}{'created'} = $created;
  264:                     }
  265:                     if ($creationcontext ne '') {
  266:                         $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
  267:                     }
  268:                     if (($inst_code ne '') && ($autoassign)) {
  269:                         unless ($gotcc) {
  270:                             %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
  271:                         }
  272:                         my @currcoowners;
  273:                         my @newcoowners;
  274:                         if ($courseinfo{'internal.co-owners'} ne '') {
  275:                             @currcoowners = split(',',$courseinfo{'internal.co-owners'});
  276:                         }
  277:                         foreach my $key (keys(%roleshash)) {
  278:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
  279:                                 my $cc = $1;
  280:                                 unless ($cc eq $owner) {
  281:                                     my ($result,$desc) = &Apache::lonnet::auto_validate_instcode($cnum,$cdom,$inst_code,$cc);
  282:                                     if ($result eq 'valid') {
  283:                                         if (@newcoowners > 0) {
  284:                                             unless (grep(/^\Q$cc\E$/,@newcoowners)) { 
  285:                                                 push(@newcoowners,$cc);
  286:                                             }
  287:                                         } else {
  288:                                             push(@newcoowners,$cc);
  289:                                         }
  290:                                     }
  291:                                 }
  292:                             }
  293:                         }
  294:                         my @diffs = &Apache::loncommon::compare_arrays(\@currcoowners,\@newcoowners);
  295:                         if (@diffs > 0) {
  296:                             if (@newcoowners > 0) {
  297:                                 $changes{'internal.co-owners'} = join(',',@newcoowners);
  298:                                 $courseshash->{$chome}{$cid}{'co-owners'} = $changes{'internal.co-owners'};
  299:                             } else {
  300:                                 if ($courseinfo{'internal.co-owners'} ne '') {
  301:                                     if (&Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum) eq 'ok') {
  302:                                         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";
  303:                                     }
  304:                                 } else {
  305:                                     print $fh "Error occurred when updating co-ownership in course's environment.db for ".$cdom."_".$cnum."\n";
  306:                                 }
  307:                             }
  308:                         } elsif (@currcoowners > 0) {
  309:                             $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
  310:                         }
  311:                     } elsif ($courseinfo{'internal.co-owners'} ne '') {
  312:                         $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
  313:                     }
  314:                     foreach my $item ('categories','cloners','hidefromcat') {
  315:                         if ($courseinfo{$item} ne '') {
  316:                             $courseshash->{$chome}{$cid}{$item} = $courseinfo{$item}; 
  317:                         }
  318:                     }
  319:                     foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date','uniquecode') {
  320:                         if ($courseinfo{'internal.'.$item} ne '') {
  321:                             $courseshash->{$chome}{$cid}{$item} =
  322:                                 $courseinfo{'internal.'.$item};
  323:                         }
  324:                     }
  325:                     if ($reqdmajor eq '' && $reqdminor eq '') {
  326:                         if ($courseinfo{'internal.releaserequired'} ne '') {
  327:                             $changes{'internal.releaserequired'} = '';
  328:                         }
  329:                     } else {
  330:                         my $releasereq =  $reqdmajor.'.'.$reqdminor;
  331:                         $courseshash->{$chome}{$cid}{'releaserequired'} = $releasereq;
  332:                         if ($courseinfo{'internal.releaserequired'} eq '') {
  333:                             $changes{'internal.releaserequired'} = $releasereq;
  334:                         } else {
  335:                             if ($courseinfo{'internal.releaserequired'} ne $releasereq) {
  336:                         
  337:                                 $changes{'internal.releaserequired'} = $releasereq;
  338:                             }
  339:                         }
  340:                     }
  341:                     if (keys(%changes)) {
  342:                         if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
  343:                             print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
  344:                             foreach my $key (sort(keys(%changes))) {
  345:                                 print $fh "$key => $changes{$key} ";
  346:                             }
  347:                             print $fh "\n";
  348:                         } else {
  349:                             print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
  350:                         }
  351:                     }
  352:                 }
  353:             }
  354:         }
  355:     }
  356:     return;
  357: }
  358: 
  359: sub last_map_update {
  360:     my ($cnum,$cdom) = @_;
  361:     my $lastupdate = 0;
  362:     my $path = &LONCAPA::propath($cdom,$cnum);
  363:     if (-d "$path/userfiles") {
  364:         if (opendir(my $dirh, "$path/userfiles")) {
  365:             my @maps = grep(/^default_?\d*\.(?:sequence|page)$/,readdir($dirh));
  366:             foreach my $map (@maps) {
  367:                 my $mtime = (stat("$path/userfiles/$map"))[9];
  368:                 if ($mtime > $lastupdate) {
  369:                     $lastupdate = $mtime;
  370:                 }
  371:             }
  372:         }
  373:     }
  374:     return $lastupdate;
  375: }
  376: 

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