[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm

raeburn raeburn@source.lon-capa.org
Tue, 04 Aug 2009 19:53:42 -0000


raeburn		Tue Aug  4 19:53:42 2009 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  - Javascript functions: getFormIdByName() and getIndexByName() moved from &coursebrowser_javascript() to &javascript_index_functions() to facilitate reuse.
  - New routines: selectuser_link() and &userbrowser_javascript() added to provide "Select User" pop-up similar to "Select Course" popup.
  - Use pick_box tabling for directory search form elements in &user_picker().
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.875 loncom/interface/loncommon.pm:1.876
--- loncom/interface/loncommon.pm:1.875	Mon Aug  3 16:47:53 2009
+++ loncom/interface/loncommon.pm	Tue Aug  4 19:53:42 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.875 2009/08/03 16:47:53 bisitz Exp $
+# $Id: loncommon.pm,v 1.876 2009/08/04 19:53:42 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -484,26 +484,16 @@
 sub coursebrowser_javascript {
     my ($domainfilter,$sec_element,$formname)=@_;
     my $crs_or_grp_alert = &mt('Please select the type of LON-CAPA entity - Course or Community - for which you wish to add/modify a user role');
-   my $output = '
+    my $id_functions = &javascript_index_functions();
+    my $output = '
 <script type="text/javascript" language="JavaScript">
 // <![CDATA[
     var stdeditbrowser;'."\n";
-   $output .= <<"ENDSTDBRW";
+
+    $output .= <<"ENDSTDBRW";
     function opencrsbrowser(formname,uname,udom,desc,extra_element,multflag,crstype) {
         var url = '/adm/pickcourse?';
-        var domainfilter = '';
-        var formid = getFormIdByName(formname);
-        if (formid > -1) {
-            var domid = getIndexByName(formid,udom);
-            if (domid > -1) {
-                if (document.forms[formid].elements[domid].type == 'select-one') {
-                    domainfilter=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
-                }
-                if (document.forms[formid].elements[domid].type == 'hidden') {
-                    domainfilter=document.forms[formid].elements[domid].value;
-                }
-            }
-        }
+        var domainfilter = getDomainFromSelectbox(formname,udom);
         if (domainfilter != null) {
            if (domainfilter != '') {
                url += 'domainfilter='+domainfilter+'&';
@@ -555,24 +545,7 @@
         stdeditbrowser = open(url,title,options,'1');
         stdeditbrowser.focus();
     }
-
-    function getFormIdByName(formname) {
-        for (var i=0;i<document.forms.length;i++) {
-            if (document.forms[i].name == formname) {
-                return i;
-            }
-        }
-        return -1; 
-    }
-
-    function getIndexByName(formid,item) {
-        for (var i=0;i<document.forms[formid].elements.length;i++) {
-            if (document.forms[formid].elements[i].name == item) {
-                return i;
-            }
-        }
-        return -1;
-    }
+$id_functions
 ENDSTDBRW
     if ($sec_element ne '') {
         $output .= &setsec_javascript($sec_element,$formname);
@@ -583,6 +556,101 @@
     return $output;
 }
 
+sub javascript_index_functions {
+    return <<"ENDJS";
+
+function getFormIdByName(formname) {
+    for (var i=0;i<document.forms.length;i++) {
+        if (document.forms[i].name == formname) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+function getIndexByName(formid,item) {
+    for (var i=0;i<document.forms[formid].elements.length;i++) {
+        if (document.forms[formid].elements[i].name == item) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+function getDomainFromSelectbox(formname,udom) {
+    var userdom;
+    var formid = getFormIdByName(formname);
+    if (formid > -1) {
+        var domid = getIndexByName(formid,udom);
+        if (domid > -1) {
+            if (document.forms[formid].elements[domid].type == 'select-one') {
+                userdom=document.forms[formid].elements[domid].options[document.forms[formid].elements[domid].selectedIndex].value;
+            }
+            if (document.forms[formid].elements[domid].type == 'hidden') {
+                userdom=document.forms[formid].elements[domid].value;
+            }
+        }
+    }
+    return userdom;
+}
+
+ENDJS
+
+}
+
+sub userbrowser_javascript {
+    my $id_functions = &javascript_index_functions();
+    return <<"ENDUSERBRW";
+
+function openuserbrowser(formname,uname,udom,ulast,ufirst,uemail,hideudom) {
+    var url = '/adm/pickuser?';
+    var userdom = getDomainFromSelectbox(formname,udom);
+    if (userdom != null) {
+       if (userdom != '') {
+           url += 'srchdom='+userdom+'&';
+       }
+    }
+    url += 'form=' + formname + '&unameelement='+uname+
+                                '&udomelement='+udom+
+                                '&ulastelement='+ulast+
+                                '&ufirstelement='+ufirst+
+                                '&uemailelement='+uemail+
+                                '&hideudomelement='+hideudom;
+    var title = 'User_Browser';
+    var options = 'scrollbars=1,resizable=1,menubar=0';
+    options += ',width=700,height=600';
+    var stdeditbrowser = open(url,title,options,'1');
+    stdeditbrowser.focus();
+}
+
+function fix_domain (formname,udom,origdom) {
+    var formid = getFormIdByName(formname);
+    if (formid > -1) {
+        var domid = getIndexByName(formid,udom);
+        var hidedomid = getIndexByName(formid,origdom);
+        if (hidedomid > -1) {
+            var fixeddom = document.forms[formid].elements[hidedomid].value;
+            if (domid > -1) {
+                var slct = document.forms[formid].elements[domid];
+                if (slct.type == 'select-one') {
+                    var i;
+                    for (i=0;i<slct.length;i++) {
+                        if (slct.options[i].value==fixeddom) { slct.selectedIndex=i; }
+                    }
+                }
+                if (slct.type == 'hidden') {
+                    slct.value = fixeddom;
+                }
+            }
+        }
+    }
+    return;
+}
+
+$id_functions
+ENDUSERBRW
+}
+
 sub setsec_javascript {
     my ($sec_element,$formname) = @_;
     my $setsections = qq|
@@ -644,6 +712,12 @@
           &mt('Select Author').'</a>';
 }
 
+sub selectuser_link {
+    my ($form,$unameelem,$domelem,$lastelem,$firstelem,$emailelem,$hdomelem,$linktext) = @_;
+    return '<a href="javascript:openuserbrowser('."'$form','$unameelem','$domelem',".
+           "'$lastelem','$firstelem','$emailelem','$hdomelem'".');">'.$linktext.'</a>';
+}
+
 sub check_uncheck_jscript {
     my $jscript = <<"ENDSCRT";
 function checkAll(field) {
@@ -7619,24 +7693,20 @@
 
 $new_user_create
 
-<table>
- <tr>
-  <td>$lt{'doma'}:</td>
-  <td>$domform</td>
-  </td>
- </tr>
- <tr>
-  <td>$lt{'usr'}:</td>
-  <td>$srchbysel
-      $srchtypesel 
-      <input type="text" size="15" name="srchterm" value="$srchterm" />
-      $srchinsel 
-  </td>
- </tr>
-</table>
-<br />
 END_BLOCK
 
+    $output .= &Apache::lonhtmlcommon::start_pick_box().
+               &Apache::lonhtmlcommon::row_title($lt{'doma'}).
+               $domform.
+               &Apache::lonhtmlcommon::row_closure().
+               &Apache::lonhtmlcommon::row_title($lt{'usr'}).
+               $srchbysel.
+               $srchtypesel. 
+               '<input type="text" size="15" name="srchterm" value="'.$srchterm.'" />'.
+               $srchinsel.
+               &Apache::lonhtmlcommon::row_closure(1). 
+               &Apache::lonhtmlcommon::end_pick_box().
+               '<br />';
     return $output;
 }