--- loncom/interface/loncommon.pm	2007/08/18 00:01:37	1.565
+++ loncom/interface/loncommon.pm	2007/08/28 18:32:36	1.572
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.565 2007/08/18 00:01:37 albertel Exp $
+# $Id: loncommon.pm,v 1.572 2007/08/28 18:32:36 banghart Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -721,24 +721,20 @@ sub help_open_topic {
 
     my $template = "";
     my $link;
-
+    
     $topic=~s/\W/\_/g;
 
-    if (!$stayOnPage)
-    {
+    if (!$stayOnPage) {
 	$link = "javascript:void(open('/adm/help/${filename}.hlp', 'Help_for_$topic', 'menubar=0,toolbar=1,scrollbars=1,width=$width,height=$height,resizable=yes'))";
-    }
-    else
-    {
+    } else {
 	$link = "/adm/help/${filename}.hlp";
     }
 
     # Add the text
-    if ($text ne "")
-    {
+    if ($text ne "") {
 	$template .= 
-  "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
-  "<td bgcolor='#5555FF'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
+            "<table bgcolor='#3333AA' cellspacing='1' cellpadding='1' border='0'><tr>".
+            "<td bgcolor='#5555FF'><a target=\"_top\" href=\"$link\"><font color='#FFFFFF' size='2'>$text</font></a>";
     }
 
     # Add the graphic
@@ -805,14 +801,10 @@ sub help_open_menu {
     my ($topic,$component_help,$faq,$bug,$stayOnPage,$width,$height,$text) 
 	= @_;    
     $stayOnPage = 0 if (not defined $stayOnPage);
-    # formerly only used pop-up help (stayOnPage = 0)
+    # only use pop-up help (stayOnPage == 0)
     # if environment.remote is on (using remote control UI)
-    # if ($env{'browser.interface'} eq 'textual' ||
-    #	$env{'environment.remote'} eq 'off' ) {
-    #   $stayOnPage=1;
-    #}
-    # Now making pop-up help the default even with remote control
-    if ($env{'browser.interface'} eq 'textual') {
+    if ($env{'browser.interface'} eq 'textual' ||
+    	$env{'environment.remote'} eq 'off' ) {
         $stayOnPage=1;
     }
     my $output;
@@ -834,15 +826,13 @@ sub help_open_menu {
 
 sub top_nav_help {
     my ($text) = @_;
-
     $text = &mt($text);
-
-    my $stayOnPage = 
+    my $stay_on_page = 
 	($env{'browser.interface'}  eq 'textual' ||
 	 $env{'environment.remote'} eq 'off' );
-    my $link=  ($stayOnPage) ? "javascript:helpMenu('display')"
+    my $link = ($stay_on_page) ? "javascript:helpMenu('display')"
 	                     : "javascript:helpMenu('open')";
-    my $banner_link = &update_help_link(undef,undef,undef,undef,$stayOnPage);
+    my $banner_link = &update_help_link(undef,undef,undef,undef,$stay_on_page);
 
     my $title = &mt('Get help');
 
@@ -5609,7 +5599,7 @@ sub get_secgrprole_info {
 }
 
 sub user_picker {
-    my ($dom,$srch,$forcenewuser) = @_;
+    my ($dom,$srch,$forcenewuser,$caller) = @_;
     my $currdom = $dom;
     my %curr_selected = (
                         srchin => 'dom',
@@ -5642,6 +5632,15 @@ sub user_picker {
                     'instd'     => 'in institutional directory',
                     'exact'     => 'is',
                     'contains'  => 'contains',
+                    'begins'    => 'begins with',
+                    'youm'      => "You must include some text to search for.",
+                    'thte'      => "The text you are searching for must contain at least two characters when using a 'begins' type search.",
+                    'thet'      => "The text you are searching for must contain at least three characters when using a 'contains' type search.",
+                    'yomc'      => "You must choose a domain when using an institutional directory search.",
+                    'ymcd'      => "You must choose a domain when using a domain search.",
+                    'whus'      => "When using searching by last,first you must include a comma as separator between last name and first name.",
+                    'whse'      => "When searching by last,first you must include at least one character in the first name.",
+                     'thfo'     => "The following need to be corrected before the search can be run:",
                                        );
     my $domform = &select_dom_form($currdom,'srchdomain',1,1);
     my $srchinsel = ' <select name="srchin">';
@@ -5677,7 +5676,7 @@ sub user_picker {
     $srchbysel .= "\n  </select>\n";
 
     my $srchtypesel = ' <select name="srchtype">';
-    foreach my $option ('exact','contains') {
+    foreach my $option ('exact','begins','contains') {
         if ($curr_selected{'srchtype'} eq $option) {
             $srchtypesel .= '
    <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
@@ -5691,29 +5690,29 @@ sub user_picker {
     my ($newuserscript,$new_user_create);
 
     if ($forcenewuser) {
-	$new_user_create = '<p> <input type="submit" name="forcenew" value="'.&HTML::Entities::encode(&mt('Make new user "[_1]"',$srchterm),'<>&"').'" onclick="javascript:setSearch(\'1\');" /> </p>';
+	$new_user_create = '<p> <input type="submit" name="forcenew" value="'.&HTML::Entities::encode(&mt('Make new user "[_1]"',$srchterm),'<>&"').'" onclick="javascript:setSearch(\'1\','.$caller.');" /> </p>';
         $newuserscript = <<"ENDSCRIPT";
 
-function setSearch(createnew) {
+function setSearch(createnew,callingForm) {
     if (createnew == 1) {
-        for (var i=0; i<document.crtuser.srchby.length; i++) {
-            if (document.crtuser.srchby.options[i].value == 'uname') {
-                document.crtuser.srchby.selectedIndex = i;
+        for (var i=0; i<callingForm.srchby.length; i++) {
+            if (callingForm.srchby.options[i].value == 'uname') {
+                callingForm.srchby.selectedIndex = i;
             }
         }
-        for (var i=0; i<document.crtuser.srchin.length; i++) {
-            if ( document.crtuser.srchin.options[i].value == 'dom') {
-		document.crtuser.srchin.selectedIndex = i;
+        for (var i=0; i<callingForm.srchin.length; i++) {
+            if ( callingForm.srchin.options[i].value == 'dom') {
+		callingForm.srchin.selectedIndex = i;
             }
         }
-        for (var i=0; i<document.crtuser.srchtype.length; i++) {
-            if (document.crtuser.srchtype.options[i].value == 'exact') {
-                document.crtuser.srchtype.selectedIndex = i;
+        for (var i=0; i<callingForm.srchtype.length; i++) {
+            if (callingForm.srchtype.options[i].value == 'exact') {
+                callingForm.srchtype.selectedIndex = i;
             }
         }
-        for (var i=0; i<document.crtuser.srchdomain.length; i++) {
-            if (document.crtuser.srchdomain.options[i].value == '$env{'request.role.domain'}') {
-                document.crtuser.srchdomain.selectedIndex = i;
+        for (var i=0; i<callingForm.srchdomain.length; i++) {
+            if (callingForm.srchdomain.options[i].value == '$env{'request.role.domain'}') {
+                callingForm.srchdomain.selectedIndex = i;
             }
         }
     }
@@ -5724,62 +5723,69 @@ ENDSCRIPT
 
     my $output = <<"END_BLOCK";
 <script type="text/javascript">
-function validateEntry() {
+function validateEntry(callingForm) {
 
     var checkok = 1;
     var srchin;
-    for (var i=0; i<document.crtuser.srchin.length; i++) {
-	if ( document.crtuser.srchin[i].checked ) {
-	    srchin = document.crtuser.srchin[i].value;
+    for (var i=0; i<callingForm.srchin.length; i++) {
+	if ( callingForm.srchin[i].checked ) {
+	    srchin = callingForm.srchin[i].value;
 	}
     }
 
-    var srchtype = document.crtuser.srchtype.options[document.crtuser.srchtype.selectedIndex].value;
-    var srchby = document.crtuser.srchby.options[document.crtuser.srchby.selectedIndex].value;
-    var srchdomain = document.crtuser.srchdomain.options[document.crtuser.srchdomain.selectedIndex].value;
-    var srchterm =  document.crtuser.srchterm.value;
-    var srchin = document.crtuser.srchin.options[document.crtuser.srchin.selectedIndex].value;
+    var srchtype = callingForm.srchtype.options[callingForm.srchtype.selectedIndex].value;
+    var srchby = callingForm.srchby.options[callingForm.srchby.selectedIndex].value;
+    var srchdomain = callingForm.srchdomain.options[callingForm.srchdomain.selectedIndex].value;
+    var srchterm =  callingForm.srchterm.value;
+    var srchin = callingForm.srchin.options[callingForm.srchin.selectedIndex].value;
     var msg = "";
 
     if (srchterm == "") {
         checkok = 0;
-        msg += "You must include some text to search for.\\n";
+        msg += "$lt{'youm'}\\n";
+    }
+
+    if (srchtype== 'begins') {
+        if (srchterm.length < 2) {
+            checkok = 0;
+            msg += "$lt{'thte'}\\n";
+        }
     }
 
     if (srchtype== 'contains') {
         if (srchterm.length < 3) {
             checkok = 0;
-            msg += "The text you are searching for must contain at least three characters when using a 'contains' type search.\\n";
+            msg += "$lt{'thet'}\\n";
         }
     }
     if (srchin == 'instd') {
         if (srchdomain == '') {
             checkok = 0;
-            msg += "You must choose a domain when using an institutional directory search.\\n";
+            msg += "$lt{'yomc'}\\n";
         }
     }
     if (srchin == 'dom') {
         if (srchdomain == '') {
             checkok = 0;
-            msg += "You must choose a domain when using a domain search.\\n";
+            msg += "$lt{'ymcd'}\\n";
         }
     }
     if (srchby == 'lastfirst') {
         if (srchterm.indexOf(",") == -1) {
             checkok = 0;
-            msg += "When using searching by last,first you must include a comma as separator between last name and first name.\\n";
+            msg += "$lt{'whus'}\\n";
         }
         if (srchterm.indexOf(",") == srchterm.length -1) {
             checkok = 0;
-            msg += "When searching by last,first you must include at least one character in the first name.\\n";
+            msg += "$lt{'whse'}\\n";
         }
     }
     if (checkok == 0) {
-        alert("The following need to be corrected before the search can be run:\\n"+msg);
+        alert("$lt{'thfo'}\\n"+msg);
         return;
     }
     if (checkok == 1) {
-        document.crtuser.submit();
+        callingForm.submit();
     }
 }
 
@@ -6969,6 +6975,40 @@ sub commit_studentrole {
 ############################################################
 ############################################################
 
+sub check_clone {
+    my ($args) = @_;
+    my $cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
+    my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
+    my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
+    my $clonemsg;
+    my $can_clone = 0;
+
+    if ($clonehome eq 'no_host') {
+	$clonemsg = &mt('Attempting to clone non-existing [_1]',
+			$args->{'crstype'});
+    } else {
+	my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
+	if ($env{'request.role.domain'} eq $args->{'clonedomain'}) {
+	    $can_clone = 1;
+	} else {
+	    my %clonehash = &Apache::lonnet::get('environment',['cloners'],
+						 $args->{'clonedomain'},$args->{'clonecourse'});
+	    my @cloners = split(/,/,$clonehash{'cloners'});
+	    my %roleshash =
+		&Apache::lonnet::get_my_roles($args->{'ccuname'},
+					      $args->{'ccdomain'},'userroles',['active'],['cc'],
+					      [$args->{'clonedomain'}]);
+	    if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':cc'}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
+		$can_clone = 1;
+	    } else {
+		$clonemsg = &mt('The new course was not cloned from an existing course because the new course owner ([_1]) does not have cloning rights in the existing course ([_2]).',$args->{'ccuname'}.':'.$args->{'ccdomain'},$clonedesc{'description'});
+	    }
+	}
+    }
+
+    return ($can_clone, $clonemsg, $cloneid, $clonehome);
+}
+
 sub construct_course {
     my ($args,$logmsg,$courseid,$crsudom,$crsunum,$udom,$uname,$context) = @_;
     my $outcome;
@@ -6976,6 +7016,23 @@ sub construct_course {
     if ($context eq 'auto') {
         $linefeed = "\n";
     }
+
+#
+# Are we cloning?
+#
+    my ($can_clone, $clonemsg, $cloneid, $clonehome);
+    if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
+	($can_clone, $clonemsg, $cloneid, $clonehome) = &check_clone($args);
+	if ($context ne 'auto') {
+	    $clonemsg = '<span class="LC_error">'.$clonemsg.'</span>';
+	}
+	$outcome .= $clonemsg.$linefeed;
+
+        if (!$can_clone) {
+	    return (0,$outcome);
+	}
+    }
+
 #
 # Open course
 #
@@ -6996,81 +7053,39 @@ sub construct_course {
     # if anyone ever decides to not show this, and Utils::Course::new
     # will need to be suitably modified.
     $outcome .= &mt('New LON-CAPA [_1] ID: [_2]',$crstype,$$courseid).$linefeed;
-
 #
 # Check if created correctly
 #
     ($$crsudom,$$crsunum)= &LONCAPA::split_courseid($$courseid);
     my $crsuhome=&Apache::lonnet::homeserver($$crsunum,$$crsudom);
     $outcome .= &mt('Created on').': '.$crsuhome.$linefeed;
+
 #
-# Are we cloning?
-#
-    my $cloneid='';
-    if (($args->{'clonecourse'}) && ($args->{'clonedomain'})) {
-        my $can_clone = 0;
-	$cloneid='/'.$args->{'clonedomain'}.'/'.$args->{'clonecourse'};
-        my ($clonecrsudom,$clonecrsunum)= &LONCAPA::split_courseid($cloneid);
-	my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
-        my $clonemsg;
-	if ($clonehome eq 'no_host') {
-            $clonemsg = &mt('Attempting to clone non-existing [_1]',$crstype);
-            if ($context eq 'auto') {
-                $outcome .= $clonemsg;
-            } else {
-	        $outcome .= '<font color="red">'.$clonemsg.'</font>';
-            }
-            $outcome .= $linefeed;
-	} else {
-            my %clonedesc = &Apache::lonnet::coursedescription($cloneid,{'one_time' => 1});
-            if ($env{'request.role.domain'} eq $args->{'form.clonedomain'}) {
-                $can_clone = 1;
-            } else {
-                my %clonehash = &Apache::lonnet::get('environment',['cloners'],
-                            $args->{'clonedomain'},$args->{'clonecourse'});
-                my @cloners = split(/,/,$clonehash{'cloners'});
-                my %roleshash =
-                    &Apache::lonnet::get_my_roles($args->{'ccuname'},
-                        $args->{'ccdomain'},'userroles',['active'],['cc'],
-                        [$args->{'clonedomain'}]);
-                if (($roleshash{$args->{'clonecourse'}.':'.$args->{'clonedomain'}.':cc'}) || (grep(/^\Q$args->{'ccuname'}\E:\Q$args->{'ccdomain'}\E$/,@cloners))) {
-                    $can_clone = 1;
-                } else {
-                    $clonemsg = &mt('The new course was not cloned from an existing course because the course owner ([_1]) does not have cloning rights in the existing course ([_2]).',$args->{'ccuname'}.':'.$args->{'ccdomain'},$clonedesc{'description'});
-                    if ($context eq 'auto') {
-                        $outcome .= $clonemsg;
-                    } else {
-                        $outcome .= '<font color="red">'.$clonemsg.'</font>';
-                    }
-                    $outcome .= $linefeed;
-                }
-            }
-        }
-        if ($can_clone) {
-	    $clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
-            if ($context eq 'auto') {
-                $outcome = $clonemsg;
-            } else { 
-                $outcome .= '<font color="green">'.$clonemsg.'</font>';
-            }
-            $outcome .= $linefeed;
-	    my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
+# Do the cloning
+#   
+    if ($can_clone && $cloneid) {
+	$clonemsg = &mt('Cloning [_1] from [_2]',$crstype,$clonehome);
+	if ($context ne 'auto') {
+	    $clonemsg = '<span class="LC_success">'.$clonemsg.'</span>';
+	}
+	$outcome .= $clonemsg.$linefeed;
+	my %oldcenv=&Apache::lonnet::dump('environment',$$crsudom,$$crsunum);
 # Copy all files
-	    &Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid);
+	&Apache::lonclonecourse::copycoursefiles($cloneid,$$courseid);
 # Restore URL
-	    $cenv{'url'}=$oldcenv{'url'};
+	$cenv{'url'}=$oldcenv{'url'};
 # Restore title
-	    $cenv{'description'}=$oldcenv{'description'};
+	$cenv{'description'}=$oldcenv{'description'};
 # restore grading mode
-	    if (defined($oldcenv{'grading'})) {
-		$cenv{'grading'}=$oldcenv{'grading'};
-	    }
-# Mark as cloned
-	    $cenv{'clonedfrom'}=$cloneid;
-	    delete($cenv{'default_enrollment_start_date'});
-	    delete($cenv{'default_enrollment_end_date'});
+	if (defined($oldcenv{'grading'})) {
+	    $cenv{'grading'}=$oldcenv{'grading'};
 	}
+# Mark as cloned
+	$cenv{'clonedfrom'}=$cloneid;
+	delete($cenv{'default_enrollment_start_date'});
+	delete($cenv{'default_enrollment_end_date'});
     }
+
 #
 # Set environment (will override cloned, if existing)
 #
@@ -7178,7 +7193,7 @@ sub construct_course {
                            ' ('.$lt{'adby'}.')';
         if ($context eq 'auto') {
             $outcome .= $badclass_msg.$linefeed;
-            $outcome .= '<font color="red">'.$badclass_msg.$linefeed.'<ul>'."\n";
+            $outcome .= '<div class="LC_warning">'.$badclass_msg.$linefeed.'<ul>'."\n";
             foreach my $item (@badclasses) {
                 if ($context eq 'auto') {
                     $outcome .= " - $item\n";
@@ -7189,7 +7204,7 @@ sub construct_course {
             if ($context eq 'auto') {
                 $outcome .= $linefeed;
             } else {
-                $outcome .= "</ul><br /><br /></font>\n";
+                $outcome .= "</ul><br /><br /></div>\n";
             }
         } 
     }
@@ -7211,7 +7226,7 @@ sub construct_course {
             if ($context eq 'auto') {
                 $outcome .= $krb_msg;
             } else {
-                $outcome .= '<font color="red" size="+1">'.$krb_msg.'</font>';
+                $outcome .= '<span class="LC_error">'.$krb_msg.'</span>';
             }
             $outcome .= $linefeed;
         }
@@ -7309,7 +7324,8 @@ sub construct_course {
 	if ($errtext) { $fatal=2; }
         $outcome .= ($fatal?$errtext:'write ok').$linefeed;
     }
-    return $outcome;
+
+    return (1,$outcome);
 }
 
 ############################################################