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

1.1       albertel    1: # The LearningOnline Network
                      2: # routines for clone a course
                      3: #
1.5     ! www         4: # $Id: lonclonecourse.pm,v 1.4 2008/01/18 17:51:18 raeburn Exp $
1.1       albertel    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)=@_;
1.2       albertel   94:     foreach my $pattern (keys(%rewritehash)) {
                     95: 	my $new=$rewritehash{$pattern};
                     96: 	$contents=~s/\Q$pattern\E/$new/gs;
1.1       albertel   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'}.'/'
1.4       raeburn   118:     => '/public/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/',
                    119:        '/adm/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
                    120:     => '/adm/'.$newcrsdata{'domain'}.'/'.$newcrsdata{'num'}.'/',
1.1       albertel  121:             )));
                    122:     }
                    123: }
                    124: 
                    125: # =============================================================== Copy a dbfile
                    126: 
                    127: sub copydb {
                    128:     my ($origcrsid,$newcrsid,$which)=@_;
                    129:     $which=~s/\.db$//;
                    130:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
                    131:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
                    132:     my %data=&Apache::lonnet::dump
                    133: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
                    134:     foreach my $key (keys(%data)) {
                    135: 	if ($key=~/^internal./) { delete($data{$key}); }
                    136:     }
                    137:     return &Apache::lonnet::put
                    138: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
                    139: }
                    140: 
                    141: # ========================================================== Copy resourcesdata
                    142: 
                    143: sub copyresourcedb {
                    144:     my ($origcrsid,$newcrsid)=@_;
                    145:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
                    146:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
                    147:     my %data=&Apache::lonnet::dump
                    148: 	('resourcedata',$origcrsdata{'domain'},$origcrsdata{'num'});
                    149:     $origcrsid=~s/^\///;
                    150:     $origcrsid=~s/\//\_/;
                    151:     $newcrsid=~s/^\///;
                    152:     $newcrsid=~s/\//\_/;
                    153:     my %newdata=();
                    154:     undef %newdata;
                    155:     my $startdate=$data{$origcrsid.'.0.opendate'};
                    156:     if (!$startdate) {
                    157: 	# now global start date for assements try the enrollment start
                    158: 	my %start=&Apache::lonnet::get('environment',
                    159: 				   ['default_enrollment_start_date'],
                    160: 				   $origcrsdata{'domain'},$origcrsdata{'num'});
                    161: 
                    162: 	$startdate = $start{'default_enrollment_start_date'};
                    163:     }
                    164: # ugly retro fix for broken version of types
                    165:     foreach (keys %data) {
                    166: 	if ($_=~/\wtype$/) {
                    167: 	    my $newkey=$_;
                    168: 	    $newkey=~s/type$/\.type/;
                    169: 	    $data{$newkey}=$data{$_};
                    170: 	    delete $data{$_};
                    171: 	}
                    172:     }
                    173: # adjust symbs
                    174:     my $pattern='uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/';
                    175:     my $new=    'uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/';
                    176:     foreach (keys %data) {
1.2       albertel  177: 	if ($_=~/\Q$pattern\E/) {
1.1       albertel  178: 	    my $newkey=$_;
1.2       albertel  179: 	    $newkey=~s/\Q$pattern\E/$new/;
1.1       albertel  180: 	    $data{$newkey}=$data{$_};
                    181: 	    delete $data{$_};
                    182: 	}
                    183:     }
                    184:     return &Apache::lonnet::put
                    185: 	('resourcedata',\%newdata,$newcrsdata{'domain'},$newcrsdata{'num'});
                    186: }
                    187: 
                    188: # ========================================================== Copy all userfiles
                    189: 
                    190: sub copyuserfiles {
                    191:     my ($origcrsid,$newcrsid)=@_;
                    192:     foreach (&crsdirlist($origcrsid,'userfiles')) {
                    193: 	if ($_ !~m|^scantron_|) {
                    194: 	    &copyfile($origcrsid,$newcrsid,$_);
                    195: 	}
                    196:     }
                    197: }
                    198: # ========================================================== Copy all userfiles
                    199: 
                    200: sub copydbfiles {
                    201:     my ($origcrsid,$newcrsid)=@_;
                    202: 
                    203:     my ($origcrs_discussion) = ($origcrsid=~m|^/(.*)|);
                    204:     $origcrs_discussion=~s|/|_|g;
                    205:     foreach (&crsdirlist($origcrsid)) {
                    206: 	if ($_=~/\.db$/) {
                    207: 	    unless 
                    208:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate|resourcedata|\Q$origcrs_discussion\E|slots|slot_reservations|gradingqueue|reviewqueue|CODEs|groupmembership)/) {
                    209: 		 &copydb($origcrsid,$newcrsid,$_);
                    210: 	     }
                    211: 	}
                    212:     }
                    213: }
                    214: 
                    215: # ======================================================= Copy all course files
                    216: 
                    217: sub copycoursefiles {
                    218:     my ($origcrsid,$newcrsid)=@_;
                    219:     &copyuserfiles($origcrsid,$newcrsid);
                    220:     &copydbfiles($origcrsid,$newcrsid);
                    221:     &copyresourcedb($origcrsid,$newcrsid);
                    222: }
                    223: 
                    224: 1;

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