File:  [LON-CAPA] / loncom / interface / lonclonecourse.pm
Revision 1.3: download - view: text, annotated - select for diffs
Thu Aug 2 01:34:07 2007 UTC (16 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD
- remove debug spew

    1: # The LearningOnline Network
    2: # routines for clone a course
    3: #
    4: # $Id: lonclonecourse.pm,v 1.3 2007/08/02 01:34:07 albertel 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: package Apache::lonclonecourse;
   31: use LONCAPA;
   32: use Apache::lonnet;
   33: 
   34: # ================================================ Get course directory listing
   35: 
   36: my @output=();
   37: 
   38: sub crsdirlist {
   39:     my ($courseid,$which)=@_;
   40:     @output=();
   41:     return &innercrsdirlist($courseid,$which);
   42: }
   43: 
   44: sub innercrsdirlist {
   45:     my ($courseid,$which,$path)=@_;
   46:     my $dirptr=16384;
   47:     unless ($which) { $which=''; } else { $which.='/'; }
   48:     unless ($path)  { $path=''; } else { $path.='/'; }
   49:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   50:     my @listing=&Apache::lonnet::dirlist
   51: 	($which,$crsdata{'domain'},$crsdata{'num'},
   52: 	 &propath($crsdata{'domain'},$crsdata{'num'}));
   53:     foreach (@listing) {
   54: 	unless ($_=~/^\./) {
   55: 	    my @unpackline = split (/\&/,$_);
   56: 	    if ($unpackline[3]&$dirptr) {
   57: # is a directory, recurse
   58: 		&innercrsdirlist($courseid,$which.$unpackline[0],
   59: 				            $path.$unpackline[0]);
   60: 	    } else { 
   61: # is a file, put into output
   62: 		push (@output,$path.$unpackline[0]);
   63: 	    }
   64: 	}
   65:     }
   66:     return @output;
   67: }
   68: 
   69: # ============================================================= Read a userfile
   70: 
   71: sub readfile {
   72:     my ($courseid,$which)=@_;
   73:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   74:     my $file = &Apache::lonnet::getfile('/uploaded/'.$crsdata{'domain'}.'/'.
   75: 				      $crsdata{'num'}.'/'.$which);
   76:     return $file;
   77: }
   78: 
   79: # ============================================================ Write a userfile
   80: 
   81: sub writefile {
   82:     (my $courseid, my $which,$env{'form.output'})=@_;
   83:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   84:     my $data = &Apache::lonnet::finishuserfileupload(
   85: 					  $crsdata{'num'},$crsdata{'domain'},
   86: 					  'output',$which);
   87:     return $data;
   88: }
   89: 
   90: # ===================================================================== Rewrite
   91: 
   92: sub rewritefile {
   93:     my ($contents,%rewritehash)=@_;
   94:     foreach my $pattern (keys(%rewritehash)) {
   95: 	my $new=$rewritehash{$pattern};
   96: 	$contents=~s/\Q$pattern\E/$new/gs;
   97:     }
   98:     return $contents;
   99: }
  100: 
  101: # ============================================================= Copy a userfile
  102: 
  103: sub copyfile {
  104:     my ($origcrsid,$newcrsid,$which)=@_;
  105:     unless ($which=~/\.sequence$/) {
  106: 	return &writefile($newcrsid,$which,
  107: 		      &readfile($origcrsid,$which));
  108:     } else {
  109: 	my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  110: 	my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  111: 	return &writefile($newcrsid,$which,
  112: 		 &rewritefile(
  113:                      &readfile($origcrsid,$which),
  114: 	    (
  115:        '/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
  116:     => '/uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/',
  117:        '/public/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
  118:     => '/public/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/'
  119:             )));
  120:     }
  121: }
  122: 
  123: # =============================================================== Copy a dbfile
  124: 
  125: sub copydb {
  126:     my ($origcrsid,$newcrsid,$which)=@_;
  127:     $which=~s/\.db$//;
  128:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  129:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  130:     my %data=&Apache::lonnet::dump
  131: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
  132:     foreach my $key (keys(%data)) {
  133: 	if ($key=~/^internal./) { delete($data{$key}); }
  134:     }
  135:     return &Apache::lonnet::put
  136: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
  137: }
  138: 
  139: # ========================================================== Copy resourcesdata
  140: 
  141: sub copyresourcedb {
  142:     my ($origcrsid,$newcrsid)=@_;
  143:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  144:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  145:     my %data=&Apache::lonnet::dump
  146: 	('resourcedata',$origcrsdata{'domain'},$origcrsdata{'num'});
  147:     $origcrsid=~s/^\///;
  148:     $origcrsid=~s/\//\_/;
  149:     $newcrsid=~s/^\///;
  150:     $newcrsid=~s/\//\_/;
  151:     my %newdata=();
  152:     undef %newdata;
  153:     my $startdate=$data{$origcrsid.'.0.opendate'};
  154:     if (!$startdate) {
  155: 	# now global start date for assements try the enrollment start
  156: 	my %start=&Apache::lonnet::get('environment',
  157: 				   ['default_enrollment_start_date'],
  158: 				   $origcrsdata{'domain'},$origcrsdata{'num'});
  159: 
  160: 	$startdate = $start{'default_enrollment_start_date'};
  161:     }
  162:     my $today=time;
  163:     my $delta=0;
  164:     if ($startdate) {
  165: 	my $oneday=60*60*24;
  166: 	$delta=$today-$startdate;
  167: 	$delta=int($delta/$oneday)*$oneday;
  168:     }
  169: # ugly retro fix for broken version of types
  170:     foreach (keys %data) {
  171: 	if ($_=~/\wtype$/) {
  172: 	    my $newkey=$_;
  173: 	    $newkey=~s/type$/\.type/;
  174: 	    $data{$newkey}=$data{$_};
  175: 	    delete $data{$_};
  176: 	}
  177:     }
  178: # adjust symbs
  179:     my $pattern='uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/';
  180:     my $new=    'uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/';
  181:     foreach (keys %data) {
  182: 	if ($_=~/\Q$pattern\E/) {
  183: 	    my $newkey=$_;
  184: 	    $newkey=~s/\Q$pattern\E/$new/;
  185: 	    $data{$newkey}=$data{$_};
  186: 	    delete $data{$_};
  187: 	}
  188:     }
  189: # adjust dates
  190:     foreach (keys %data) {
  191: 	my $thiskey=$_;
  192: 	$thiskey=~s/^$origcrsid/$newcrsid/;
  193: 	$newdata{$thiskey}=$data{$_};
  194: 	if ($data{$_.'.type'}=~/^date_(start|end)$/) {
  195: 	    if ($delta > 0) {
  196: 		$newdata{$thiskey}=$newdata{$thiskey}+$delta;
  197: 	    } else {
  198: 		# no delta, it's unlikely we want the old dates and times
  199: 		delete($newdata{$thiskey});
  200: 		delete($newdata{$thiskey.'.type'});
  201: 	    }
  202: 	}
  203:     }
  204:     return &Apache::lonnet::put
  205: 	('resourcedata',\%newdata,$newcrsdata{'domain'},$newcrsdata{'num'});
  206: }
  207: 
  208: # ========================================================== Copy all userfiles
  209: 
  210: sub copyuserfiles {
  211:     my ($origcrsid,$newcrsid)=@_;
  212:     foreach (&crsdirlist($origcrsid,'userfiles')) {
  213: 	if ($_ !~m|^scantron_|) {
  214: 	    &copyfile($origcrsid,$newcrsid,$_);
  215: 	}
  216:     }
  217: }
  218: # ========================================================== Copy all userfiles
  219: 
  220: sub copydbfiles {
  221:     my ($origcrsid,$newcrsid)=@_;
  222: 
  223:     my ($origcrs_discussion) = ($origcrsid=~m|^/(.*)|);
  224:     $origcrs_discussion=~s|/|_|g;
  225:     foreach (&crsdirlist($origcrsid)) {
  226: 	if ($_=~/\.db$/) {
  227: 	    unless 
  228:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate|resourcedata|\Q$origcrs_discussion\E|slots|slot_reservations|gradingqueue|reviewqueue|CODEs|groupmembership)/) {
  229: 		 &copydb($origcrsid,$newcrsid,$_);
  230: 	     }
  231: 	}
  232:     }
  233: }
  234: 
  235: # ======================================================= Copy all course files
  236: 
  237: sub copycoursefiles {
  238:     my ($origcrsid,$newcrsid)=@_;
  239:     &copyuserfiles($origcrsid,$newcrsid);
  240:     &copydbfiles($origcrsid,$newcrsid);
  241:     &copyresourcedb($origcrsid,$newcrsid);
  242: }
  243: 
  244: 1;

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