Annotation of loncom/interface/lonclonecourse.pm, revision 1.1

1.1     ! albertel    1: # The LearningOnline Network
        !             2: # routines for clone a course
        !             3: #
        !             4: # $Id: loncreatecourse.pm,v 1.93.2.4 2006/07/20 22:10:22 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:     &Apache::lonnet::logthis("gor $data $crsdata{'num'} $crsdata{'domain'}");
        !            88:     return $data;
        !            89: }
        !            90: 
        !            91: # ===================================================================== Rewrite
        !            92: 
        !            93: sub rewritefile {
        !            94:     my ($contents,%rewritehash)=@_;
        !            95:     foreach (keys %rewritehash) {
        !            96: 	my $pattern=$_;
        !            97: 	$pattern=~s/(\W)/\\$1/gs;
        !            98: 	my $new=$rewritehash{$_};
        !            99: 	$contents=~s/$pattern/$new/gs;
        !           100:     }
        !           101:     return $contents;
        !           102: }
        !           103: 
        !           104: # ============================================================= Copy a userfile
        !           105: 
        !           106: sub copyfile {
        !           107:     my ($origcrsid,$newcrsid,$which)=@_;
        !           108:     unless ($which=~/\.sequence$/) {
        !           109: 	return &writefile($newcrsid,$which,
        !           110: 		      &readfile($origcrsid,$which));
        !           111:     } else {
        !           112: 	my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
        !           113: 	my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
        !           114: 	return &writefile($newcrsid,$which,
        !           115: 		 &rewritefile(
        !           116:                      &readfile($origcrsid,$which),
        !           117: 	    (
        !           118:        '/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
        !           119:     => '/uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/',
        !           120:        '/public/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
        !           121:     => '/public/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/'
        !           122:             )));
        !           123:     }
        !           124: }
        !           125: 
        !           126: # =============================================================== Copy a dbfile
        !           127: 
        !           128: sub copydb {
        !           129:     my ($origcrsid,$newcrsid,$which)=@_;
        !           130:     $which=~s/\.db$//;
        !           131:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
        !           132:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
        !           133:     my %data=&Apache::lonnet::dump
        !           134: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
        !           135:     foreach my $key (keys(%data)) {
        !           136: 	if ($key=~/^internal./) { delete($data{$key}); }
        !           137:     }
        !           138:     return &Apache::lonnet::put
        !           139: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
        !           140: }
        !           141: 
        !           142: # ========================================================== Copy resourcesdata
        !           143: 
        !           144: sub copyresourcedb {
        !           145:     my ($origcrsid,$newcrsid)=@_;
        !           146:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
        !           147:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
        !           148:     my %data=&Apache::lonnet::dump
        !           149: 	('resourcedata',$origcrsdata{'domain'},$origcrsdata{'num'});
        !           150:     $origcrsid=~s/^\///;
        !           151:     $origcrsid=~s/\//\_/;
        !           152:     $newcrsid=~s/^\///;
        !           153:     $newcrsid=~s/\//\_/;
        !           154:     my %newdata=();
        !           155:     undef %newdata;
        !           156:     my $startdate=$data{$origcrsid.'.0.opendate'};
        !           157:     if (!$startdate) {
        !           158: 	# now global start date for assements try the enrollment start
        !           159: 	my %start=&Apache::lonnet::get('environment',
        !           160: 				   ['default_enrollment_start_date'],
        !           161: 				   $origcrsdata{'domain'},$origcrsdata{'num'});
        !           162: 
        !           163: 	$startdate = $start{'default_enrollment_start_date'};
        !           164:     }
        !           165:     my $today=time;
        !           166:     my $delta=0;
        !           167:     if ($startdate) {
        !           168: 	my $oneday=60*60*24;
        !           169: 	$delta=$today-$startdate;
        !           170: 	$delta=int($delta/$oneday)*$oneday;
        !           171:     }
        !           172: # ugly retro fix for broken version of types
        !           173:     foreach (keys %data) {
        !           174: 	if ($_=~/\wtype$/) {
        !           175: 	    my $newkey=$_;
        !           176: 	    $newkey=~s/type$/\.type/;
        !           177: 	    $data{$newkey}=$data{$_};
        !           178: 	    delete $data{$_};
        !           179: 	}
        !           180:     }
        !           181: # adjust symbs
        !           182:     my $pattern='uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/';
        !           183:     $pattern=~s/(\W)/\\$1/gs;
        !           184:     my $new=    'uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/';
        !           185:     foreach (keys %data) {
        !           186: 	if ($_=~/$pattern/) {
        !           187: 	    my $newkey=$_;
        !           188: 	    $newkey=~s/$pattern/$new/;
        !           189: 	    $data{$newkey}=$data{$_};
        !           190: 	    delete $data{$_};
        !           191: 	}
        !           192:     }
        !           193: # adjust dates
        !           194:     foreach (keys %data) {
        !           195: 	my $thiskey=$_;
        !           196: 	$thiskey=~s/^$origcrsid/$newcrsid/;
        !           197: 	$newdata{$thiskey}=$data{$_};
        !           198: 	if ($data{$_.'.type'}=~/^date_(start|end)$/) {
        !           199: 	    if ($delta > 0) {
        !           200: 		$newdata{$thiskey}=$newdata{$thiskey}+$delta;
        !           201: 	    } else {
        !           202: 		# no delta, it's unlikely we want the old dates and times
        !           203: 		delete($newdata{$thiskey});
        !           204: 		delete($newdata{$thiskey.'.type'});
        !           205: 	    }
        !           206: 	}
        !           207:     }
        !           208:     return &Apache::lonnet::put
        !           209: 	('resourcedata',\%newdata,$newcrsdata{'domain'},$newcrsdata{'num'});
        !           210: }
        !           211: 
        !           212: # ========================================================== Copy all userfiles
        !           213: 
        !           214: sub copyuserfiles {
        !           215:     my ($origcrsid,$newcrsid)=@_;
        !           216:     foreach (&crsdirlist($origcrsid,'userfiles')) {
        !           217: 	if ($_ !~m|^scantron_|) {
        !           218: 	    &copyfile($origcrsid,$newcrsid,$_);
        !           219: 	}
        !           220:     }
        !           221: }
        !           222: # ========================================================== Copy all userfiles
        !           223: 
        !           224: sub copydbfiles {
        !           225:     my ($origcrsid,$newcrsid)=@_;
        !           226: 
        !           227:     my ($origcrs_discussion) = ($origcrsid=~m|^/(.*)|);
        !           228:     $origcrs_discussion=~s|/|_|g;
        !           229:     foreach (&crsdirlist($origcrsid)) {
        !           230: 	if ($_=~/\.db$/) {
        !           231: 	    unless 
        !           232:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate|resourcedata|\Q$origcrs_discussion\E|slots|slot_reservations|gradingqueue|reviewqueue|CODEs|groupmembership)/) {
        !           233: 		 &copydb($origcrsid,$newcrsid,$_);
        !           234: 	     }
        !           235: 	}
        !           236:     }
        !           237: }
        !           238: 
        !           239: # ======================================================= Copy all course files
        !           240: 
        !           241: sub copycoursefiles {
        !           242:     my ($origcrsid,$newcrsid)=@_;
        !           243:     &copyuserfiles($origcrsid,$newcrsid);
        !           244:     &copydbfiles($origcrsid,$newcrsid);
        !           245:     &copyresourcedb($origcrsid,$newcrsid);
        !           246: }
        !           247: 
        !           248: 1;

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