Annotation of loncom/interface/lonsource.pm, revision 1.4

1.1       taceyjo1    1: # The LearningOnline Network with CAPA
1.2       albertel    2: # Souce Code handler
1.1       taceyjo1    3: #
1.4     ! taceyjo1    4: # $Id: lonsource.pm,v 1.3 2004/06/18 15:13:07 www Exp $
1.1       taceyjo1    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: 
                     31: package Apache::lonsource;
                     32: 
                     33: use strict;
                     34: use Apache::lonnet();
                     35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon();
                     37: use Apache::lonsequence();
                     38: use Apache::Constants qw(:common :http);
                     39: use Apache::lonmeta;
                     40: use Apache::File;
                     41: use Apache::lonlocal;
                     42: use HTML::Entities;
                     43: 
                     44: sub make_link {
1.4     ! taceyjo1   45:     my ($filename, $listname) = @_;
1.2       albertel   46:     my $sourcelink = "http://".$ENV{'SERVER_NAME'}.
1.4     ! taceyjo1   47: 	"/adm/source/?filename=".$filename."&listname=".$listname;
1.2       albertel   48: 
                     49:     return $sourcelink;
1.1       taceyjo1   50: }
                     51: 
                     52: sub stage_2 {
1.4     ! taceyjo1   53: 	my ($r, $filename, $author, $listname) = @_;
        !            54: 	$filename = $filename;
        !            55: 	&Apache::loncommon::content_type($r,'text/html');
        !            56: 	my ($uname, $udom) = &Apache::loncacc::constructaccess('/~'.$author.'/',$r->dir_config('lonDefDomain'));
        !            57: 	$r->send_http_header;
        !            58: 	$r->print('<html><head><title>LON-CAPA Move source to construction space</title>');
        !            59: 	$r->print(&Apache::loncommon::bodytag('Problem source code moving operation'));
        !            60: 	$r->print("Please enter the directory that you would like the source code to go into, a default has also been provided <br />");
        !            61: 	$r->print("Also note, the path is in reference to the root of your construction space, and new directories will be automatically created. <br /><br />");
        !            62: 	$r->print('		    <form name="copy" action="/adm/source/" target="_parent" method="post">
        !            63: 	<input type="hidden" name="filename" value="'.$filename.'" />
        !            64: 	<input type="hidden" name="listname" value="'.$listname.'" />
        !            65: 	<input type="hidden" name="action" value="copy_stage" />
        !            66: 	<input type="text" name="newpath" value="/shared_source/'.$author.'" />&nbsp;
        !            67: 	<input type="submit" value="Copy" />
        !            68: 	</form>');
        !            69: 	return OK;
        !            70: }
        !            71: 
        !            72: 
        !            73: sub copy_stage {
        !            74:     my ($r, $filename, $listname, $newpath) = @_;
        !            75:     my $role;
        !            76:     my $domain;
        !            77:     my $author_name;
        !            78:     #Figure out if we are author or co-author
        !            79:     if($ENV{'request.role'} =~ m|ca.|) { 
        !            80:     	($role, $domain, $author_name) = split(/\//,$ENV{'request.role'});
        !            81:     } else {
        !            82:     	$role = "au.";
        !            83:     	$domain = $ENV{'user.domain'};
        !            84:     	$author_name = $ENV{'user.name'};
        !            85:     	}
        !            86:     
        !            87:     my $path_to_new_file = '/home/'.$author_name.'/public_html/'.$newpath.'/'.$listname;
        !            88:     #Just checking again for access as we want to make sure that it is really ok now that we have the real path
        !            89:     my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file,$domain);
        !            90:     unless (($uname) && ($udom)) {
        !            91: 	 return HTTP_NOT_ACCEPTABLE;
        !            92:     }
        !            93:     &Apache::loncommon::content_type($r,'text/html');
        !            94:     $r->send_http_header;
        !            95:     $r->print('<html><head><title>LON-CAPA Move source to construction space</title>');
        !            96: 	 $r->print(&Apache::loncommon::bodytag('Copying Source'));
        !            97: 	 my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
        !            98: 	 $r->print($result);	
        !            99: 	 if(($result) && ($result =~ m|published|) ) {
        !           100: 	 		&delete_file($r, $author_name, $newpath, $filename, $path_to_new_file, '1');
        !           101: 	 } elsif(($result) && ($result =~ m|exists!|)) {
        !           102: 	 		&confirm($r, $author_name, $newpath, $filename, $path_to_new_file);
        !           103: 	 } else { 
        !           104: 	 		&copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
        !           105: 	 }
        !           106: 	
        !           107: return OK;	
        !           108: 	
        !           109: }
        !           110: 
        !           111: sub confirm {
        !           112: 	my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
        !           113: 	$r->print("<b>Press delete to remove file and replace it with a copy of the source you are viewing</b><br /><br />");
        !           114: 	$r->print('		    <form name="delete_confirm" action="/adm/source/" target="_parent" method="post">
        !           115: 		      <input type="hidden" name="filename" value="'.$filename.'" />
        !           116: 		      <input type="hidden" name="path" value="'.$path_to_new_file.'" />
        !           117: 		      <input type="hidden" name="author" value="'.$author_name.'" />
        !           118: 		      <input type="hidden" name="newpath" value="'.$newpath.'" />
        !           119: 		      <input type="hidden" name="action" value="delete_confirm" />
        !           120: 	            
        !           121: 	              <input type="submit" value="Delete" />
        !           122: 		    </form>');
        !           123: }
        !           124: 
        !           125: sub delete_file {
        !           126: 	my ($r, $author_name, $newpath, $filename, $path_to_new_file, $type) = @_;
        !           127: 	if($type eq '1') { 
        !           128: 		$r->print("<b>Cannot delete non-obsolete published file</b><br />Please
        !           129: 					use the code view in previous window to use shared code<br /><br />");
        !           130: 		$r->print('<input type="button" value="Close Window" name="close" onClick="window.close()">');
        !           131: 	} else {
        !           132: 		if(-e $path_to_new_file) {
        !           133: 	   unless(unlink($path_to_new_file)) {
        !           134: 			$r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
        !           135: 			return 0;
        !           136: 	   }
        !           137: 	} else {
        !           138: 	    $r->print('<p> '.&mt('No such file').'. </p></form>');
        !           139: 	    return 0;
        !           140: 	  }
        !           141: 	  &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
        !           142: 	}
1.1       taceyjo1  143: }
                    144: 
1.4     ! taceyjo1  145: sub copy_file {
        !           146: 	my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
        !           147: 	$r->print("<b>Creating directories</b>");
        !           148: 	my $path = '/home/'.$author_name.'/public_html/';
        !           149: 	my @directories = split(/\//,$newpath);
        !           150: 	foreach my $now_checking (@directories) { 
        !           151: 		if($now_checking ne '') {
        !           152: 			$path = $path.'/'.$now_checking;
        !           153: 			if(-e $path) {} #More moving along, isn't recursion fun'
        !           154: 			else {
        !           155: 				unless(mkdir($path, 02770)) {
        !           156: 					$r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
        !           157: 					return 0;
        !           158: 				}
        !           159: 				unless(chmod(02770, ($path))) {
        !           160: 					$r->print('<font color="red"> '.&mt('Error').': '.$!.'</font>');
        !           161: 					return 0;
        !           162: 				}
        !           163: 			}
        !           164: 		} else { } #Just move along
        !           165: 	}
        !           166: 	$r->print("<br /><b>Copying File</b>");
        !           167: 	my $problem_filename = "/home/httpd/html".$filename;
        !           168: 	my $file_output = &Apache::lonnet::getfile($problem_filename);
        !           169: 	my $fs=Apache::File->new(">$path_to_new_file");
        !           170: 	if (defined($fs)) {
        !           171: 		print $fs $file_output;
        !           172: 	}
        !           173: 	$r->print("<br /><br />");
        !           174: 	$r->print('<input type="button" value="Close Window" name="close" onClick="window.close()">');
        !           175: 	#Some 1.3'ish feature is to include the derivative feature, will go here..'
        !           176: }	
        !           177: 
1.1       taceyjo1  178: sub print_item {
1.4     ! taceyjo1  179: 	my ($r, $filename) = @_;
        !           180: 	$filename = "/home/httpd/html".$filename;
        !           181: 	&Apache::lonnet::logthis("print_item filename = $filename");
        !           182: 	my $file_output = &Apache::lonnet::getfile($filename);
        !           183: 	my $count=0;
        !           184: 	my $maxlength=-1;
        !           185: 	foreach (split ("\n", $file_output)) {
        !           186: 		$count+=int(length($_)/79);
        !           187: 		$count++;
        !           188: 		if (length($_) > $maxlength) { $maxlength = length($_); }
        !           189: 	}
        !           190: 	my $rows = $count;
        !           191: 	my $cols = $maxlength;
        !           192: 	$r->print('<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
        !           193: 	&HTML::Entities::encode($file_output,'<>&"').'</textarea>');
        !           194: 	return OK;
1.1       taceyjo1  195: 	
                    196: }
                    197: 
                    198: 
                    199: sub handler { 
1.2       albertel  200:     my $r=shift;
1.3       www       201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.4     ! taceyjo1  202: 					    ['filename','listname']);
1.3       www       203:     my $filename = $ENV{'form.filename'};
1.4     ! taceyjo1  204:     my $listname = $ENV{'form.listname'};
1.3       www       205:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
                    206:     if ($source ne 'open') {
1.4     ! taceyjo1  207: 	 	$ENV{'user.error.msg'}="$filename:cre:1:1:Source code not available";
        !           208: 	 	return HTTP_NOT_ACCEPTABLE;
1.3       www       209:     }
1.4     ! taceyjo1  210:     if ((!&Apache::lonnet::allowed('cre',$filename)) ||
        !           211: 		(!&Apache::lonnet::allowed('bre',$filename))) {
        !           212: 		$ENV{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
        !           213: 		return HTTP_NOT_ACCEPTABLE;
1.3       www       214:     }
                    215:     if ($ENV{'form.action'} eq 'stage2') { 
1.4     ! taceyjo1  216:     	my $author = &Apache::lonnet::metadata($filename,'authorspace');
        !           217:     	$author =~ m|@|;
        !           218:     	$author = $`; #This just tells who the author name is for later processing.
        !           219: 		&stage_2($r, $ENV{'form.filename'}, $author, $listname);
        !           220:     } elsif($ENV{'form.action'} eq 'copy_stage') {
        !           221:     		&copy_stage($r, $filename,$ENV{'form.listname'},$ENV{'form.newpath'});
        !           222:    	} elsif($ENV{'form.action'} eq 'delete_confirm') {
        !           223:    		&Apache::loncommon::content_type($r,'text/html');
        !           224: 			$r->send_http_header;
        !           225:     		&delete_file($r, $ENV{'form.author'}, $ENV{'form.newpath'}, $ENV{'form.filename'}, $ENV{'form.path'}, '0');
        !           226:    	} else {
        !           227: 			&Apache::loncommon::content_type($r,'text/html');
        !           228: 			$r->send_http_header;
        !           229: 			$r->print('		    <form name="copy" action="/adm/source/" target="_parent" method="post">
1.1       taceyjo1  230: 				<input type="button" value="Close Window" name="close" onClick="window.close()">
                    231: 		      <input type="hidden" name="filename" value="'.$filename.'" />
1.4     ! taceyjo1  232: 		      <input type="hidden" name="listname" value="'.$listname.'" />
1.1       taceyjo1  233: 		      <input type="hidden" name="action" value="stage2" />
                    234: 	              <input type="submit" value="Copy to CSTR" />
                    235: 		    </form>');
1.4     ! taceyjo1  236: 			$r->print('<hr />');
        !           237: 			&print_item($r, $ENV{'form.filename'});
        !           238:     	  }
1.2       albertel  239:     return OK;
1.1       taceyjo1  240: }
                    241: 
                    242: 1;
                    243: 	
1.2       albertel  244: 	

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