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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 07 Jul 2008 23:33:49 -0000


This is a MIME encoded message

--raeburn1215473629
Content-Type: text/plain

raeburn		Mon Jul  7 19:33:49 2008 EDT

  Modified files:              
    /loncom/interface	loncreateuser.pm 
  Log:
  - Adding user types allowed to self-enroll from a domain now a one step process.
  - Javascript added to automatically toggle radio buttons for "Any user in any 
    domain" and "Delete" or "Activate" checkboxes for specific domains.
  - "Any user in any domain" in separate table separated from table for specific domains, with word: "Or" to separate the tables.
  - Validation javascript added to alert user if "Activate" and "User types" are inconcisitent.
  - Default when adding an additional specific domain, is to enable self-enrollment for all user types for that domain. 
  
  
--raeburn1215473629
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080707193349.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.248 loncom/interface/loncreateuser.pm:1.249
--- loncom/interface/loncreateuser.pm:1.248	Mon Jul  7 12:42:26 2008
+++ loncom/interface/loncreateuser.pm	Mon Jul  7 19:33:49 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.248 2008/07/07 16:42:26 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.249 2008/07/07 23:33:49 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3178,8 +3178,179 @@
     my $groupslist = &Apache::lonuserutils::get_groupslist();
     my $setsec_js = 
         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
+    my %alerts = &Apache::lonlocal::texthash(
+        acto => 'Activation of self-enrollment was selected for the following domain(s)',
+        butn => 'but no user types have been checked.',
+        wilf => "Please uncheck 'activate' or check at least one type.",
+    );
+    my $selfenroll_js = <<"ENDSCRIPT";
+function update_types(caller,num) {
+    var delidx = getIndexByName('selfenroll_delete');
+    var actidx = getIndexByName('selfenroll_activate');
+    if (caller == 'selfenroll_all') {
+        var selall;
+        for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
+            if (document.$formname.selfenroll_all[i].checked) {
+                selall = document.$formname.selfenroll_all[i].value;
+            }
+        }
+        if (selall == 1) {
+            if (delidx != -1) {
+                if (document.$formname.selfenroll_delete.length) {
+                    for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
+                        document.$formname.selfenroll_delete[j].checked = true;
+                    }
+                } else {
+                    document.$formname.elements[delidx].checked = true;
+                }
+            }
+            if (actidx != -1) {
+                if (document.$formname.selfenroll_activate.length) {
+                    for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
+                        document.$formname.selfenroll_activate[j].checked = false;
+                    }
+                } else {
+                    document.$formname.elements[actidx].checked = false;
+                }
+            }
+            document.$formname.selfenroll_newdom.selectedIndex = 0; 
+        }
+    }
+    if (caller == 'selfenroll_activate') {
+        if (document.$formname.selfenroll_activate.length) {
+            for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
+                if (document.$formname.selfenroll_activate[j].value == num) {
+                    if (document.$formname.selfenroll_activate[j].checked) {
+                        for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
+                            if (document.$formname.selfenroll_all[i].value == '1') {
+                                document.$formname.selfenroll_all[i].checked = false;
+                            }
+                            if (document.$formname.selfenroll_all[i].value == '0') {
+                                document.$formname.selfenroll_all[i].checked = true;
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
+                if (document.$formname.selfenroll_all[i].value == '1') {
+                    document.$formname.selfenroll_all[i].checked = false;
+                }
+                if (document.$formname.selfenroll_all[i].value == '0') {
+                    document.$formname.selfenroll_all[i].checked = true;
+                }
+            }
+        }
+    }
+    if (caller == 'selfenroll_delete') {
+        if (document.$formname.selfenroll_delete.length) {
+            for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
+                if (document.$formname.selfenroll_delete[j].value == num) {
+                    if (document.$formname.selfenroll_delete[j].checked) {
+                        var delindex = getIndexByName('selfenroll_types_'+num);
+                        if (delindex != -1) { 
+                            if (document.$formname.elements[delindex].length) {
+                                for (var k=0; k<document.$formname.elements[delindex].length; k++) {
+                                    document.$formname.elements[delindex][k].checked = false;
+                                }
+                            } else {
+                                document.$formname.elements[delindex].checked = false;
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            if (document.$formname.selfenroll_delete.checked) {
+                var delindex = getIndexByName('selfenroll_types_'+num);
+                if (delindex != -1) {
+                    if (document.$formname.elements[delindex].length) {
+                        for (var k=0; k<document.$formname.elements[delindex].length; k++) {
+                            document.$formname.elements[delindex][k].checked = false;
+                        }
+                    } else {
+                        document.$formname.elements[delindex].checked = false;
+                    }
+                }
+            }
+        }
+    }
+    return;
+}
+
+function validate_types(form) {
+    var needaction = new Array();
+    var countfail = 0;
+    var actidx = getIndexByName('selfenroll_activate');
+    if (actidx != -1) {
+        if (document.$formname.selfenroll_activate.length) {
+            for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
+                var num = document.$formname.selfenroll_activate[j].value;
+                if (document.$formname.selfenroll_activate[j].checked) {
+                    countfail = check_types(num,countfail,needaction)
+                }
+            }
+        } else {
+            if (document.$formname.selfenroll_activate.checked) {
+                var num = document.enrollstudent.selfenroll_activate.value;
+                countfail = check_types(num,countfail,needaction)
+            }
+        }
+    }
+    if (countfail > 0) {
+        var msg = "$alerts{'acto'}\\n";
+        var loopend = needaction.length -1;
+        if (loopend > 0) {
+            for (var m=0; m<loopend; m++) {
+                msg += needaction[m]+", ";
+            }
+        }
+        msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
+        alert(msg);
+        return; 
+    }
+    setSections(form);
+}
+
+function check_types(num,countfail,needaction) {
+    var typeidx = getIndexByName('selfenroll_types_'+num);
+    var count = 0;
+    if (typeidx != -1) {
+        if (document.$formname.elements[typeidx].length) {
+            for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
+                if (document.$formname.elements[typeidx][k].checked) {
+                    count ++;
+                }
+            }
+        } else {
+            if (document.$formname.elements[typeidx].checked) {
+                count ++;
+            }
+        }
+        if (count == 0) {
+            var domidx = getIndexByName('selfenroll_dom_'+num);
+            if (domidx != -1) {
+                var domname = document.$formname.elements[domidx].value;
+                needaction[countfail] = domname;
+                countfail ++;
+            }
+        }
+    }
+    return countfail;
+}
+
+function getIndexByName(item) {
+    for (var i=0;i<document.$formname.elements.length;i++) {
+        if (document.$formname.elements[i].name == item) {
+            return i;
+        }
+    }
+    return -1;
+}
+ENDSCRIPT
     my $output = '<script type="text/javascript">'."\n".
-                 $setsec_js."\n".
+                 $setsec_js."\n".$selfenroll_js."\n".
                  '</script>'."\n".
                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n".
                  '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
@@ -3208,14 +3379,20 @@
                 if ($curr_types eq '*') {
                     $output .= ' checked="checked" '; 
                 }
-                $output .= '/>'.&mt('Yes').'</label>&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
+                $output .= 'onchange="javascript:update_types('.
+                           "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
+                           '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
                 if ($curr_types ne '*') {
                     $output .= ' checked="checked" ';
                 }
-                $output .= '/>'.&mt('No').'</label></td>'
-                           .&Apache::loncommon::end_data_table_row();
+                $output .= ' onchange="javascript:update_types('.
+                           "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
+                           &Apache::loncommon::end_data_table_row().
+                           &Apache::loncommon::end_data_table().
+                           &mt('Or').'<br />'.
+                           &Apache::loncommon::start_data_table();
                 my %currdoms;
-                if (($curr_types eq '') && ($env{'form.selfenroll_newdom'} eq '')) {
+                if ($curr_types eq '') {
                     $output .= &new_selfenroll_dom_row($cdom,'0');
                 } elsif ($curr_types ne '*') {
                     my @entries = split(/;/,$curr_types);
@@ -3224,33 +3401,27 @@
                             my ($currdom,$typestr) = split(/:/,$entry);
                             $currdoms{$currdom} = 1;
                             my $domdesc = &Apache::lonnet::domain($currdom);
-                            my @currinsttypes = split(/,/,$typestr);
+                            my @currinsttypes = split(',',$typestr);
                             $output .= &Apache::loncommon::start_data_table_row()
                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                                        .'" value="'.$currdom.'" /></span><br />'
                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
-                                       .'name="selfenroll_delete" value="'.$num.'" />'
+                                       .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
                                        .&mt('Delete').'</label></span></td>';
-                            $output .= '<td valign="top">'.&mt('User types:').'<br />'
+                            $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                                        .&Apache::loncommon::end_data_table_row();
                             $num ++;
                         }
                     }
                 }
-                if ($env{'form.selfenroll_newdom'} ne '') {
-                    if (!defined($currdoms{$env{'form.selfenroll_newdom'}})) {
-                        $output .= &new_selfenroll_dom_row($env{'form.selfenroll_newdom'},$num); 
-                        $num ++;
-                    }
-                }
-                my $add_domtitle = &mt('Additional domain:');
+                my $add_domtitle = &mt('Users in additional domain:');
                 if ($curr_types eq '*') { 
-                    $add_domtitle = &mt('Specific domain:');
+                    $add_domtitle = &mt('Users in specific domain:');
                 } elsif ($curr_types eq '') {
-                    $add_domtitle = &mt('Other domain:');
+                    $add_domtitle = &mt('Users in other domain:');
                 }
                 $output .= &Apache::loncommon::start_data_table_row()
                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
@@ -3331,7 +3502,7 @@
     }
     $output .= &Apache::lonhtmlcommon::end_pick_box().
                '<br /><input type="button" name="selfenrollconf" value="'
-               .&mt('Save changes').'" onclick="setSections(this.form);" />'
+               .&mt('Save changes').'" onclick="validate_types(this.form);" />'
                .'<input type="hidden" name="action" value="selfenroll" /></form>';
     $r->print($output);
     return;
@@ -3345,7 +3516,12 @@
         $output .= &Apache::loncommon::start_data_table_row()
                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
-                   .'" value="'.$newdom.'" /></span></td>';
+                   .'" value="'.$newdom.'" /></span><br />'
+                   .'<span class="LC_nobreak"><label><input type="checkbox" '
+                   .'name="selfenroll_activate" value="'.$num.'" '
+                   .'onchange="javascript:update_types('
+                   ."'selfenroll_activate','$num'".');" />'
+                   .&mt('Activate').'</label></span></td>';
         my @currinsttypes;
         $output .= '<td>'.&mt('User types:').'<br />'
                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
@@ -3375,9 +3551,13 @@
                            $type.'" ';
                 if (ref($currinsttypes) eq 'ARRAY') {
                     if (@{$currinsttypes} > 0) {
-                        if (grep(/^\Q$type\E$/,@{$currinsttypes})) {
+                        if (grep(/^any$/,@{$currinsttypes})) {
+                            $output .= 'checked="checked"';
+                        } elsif (grep(/^\Q$type\E$/,@{$currinsttypes})) {
                             $output .= 'checked="checked"';
                         }
+                    } else {
+                        $output .= 'checked="checked"';
                     }
                 }
                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
@@ -3387,13 +3567,21 @@
         if (($count > 0) && ($count%$numinrow == 0)) {
             $output .= '</tr><tr>';
         }
-        $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'" ';
+        $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
         if (ref($currinsttypes) eq 'ARRAY') {
             if (@{$currinsttypes} > 0) {
-                if (grep(/^other$/,@{$currinsttypes})) {
-                    $output .= 'checked="checked" ';
+                if (grep(/^any$/,@{$currinsttypes})) { 
+                    $output .= ' checked="checked"';
+                } elsif ($othervalue eq 'other') {
+                    if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
+                        $output .= ' checked="checked"';
+                    }
                 }
+            } else {
+                $output .= ' checked="checked"';
             }
+        } else {
+            $output .= ' checked="checked"';
         }
         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
     }
@@ -4315,26 +4503,57 @@
                     } else {
                         next;
                     }
-                } elsif ($curr_types eq '') {
-                    my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_0');
-                    if (@types > 0) {
-                        $changes{'internal.selfenroll_types'} = $env{'form.selfenroll_dom_0'}.':'.join(',',@types);
-                    }
                 } else {
+                    my %currdoms;
                     my @entries = split(/;/,$curr_types);
-                    my $num = 0;
-                    my @latesttypes;
                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
+                    my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
                     my $newnum = 0;
-                    for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {
-                        if (!grep(/^$j$/,@deletedoms)) {
-                            my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
+                    my @latesttypes;
+                    foreach my $num (@activations) {
+                        my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
+                        if (@types > 0) {
                             @types = sort(@types);
                             my $typestr = join(',',@types);
-                            $latesttypes[$newnum] = $env{'form.selfenroll_dom_'.$j}.':'.$typestr;
+                            my $typedom = $env{'form.selfenroll_dom_'.$num};
+                            $latesttypes[$newnum] = $typedom.':'.$typestr;
+                            $currdoms{$typedom} = 1;
                             $newnum ++;
                         }
                     }
+                    for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {                        if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
+                            my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
+                            if (@types > 0) {
+                                @types = sort(@types);
+                                my $typestr = join(',',@types);
+                                my $typedom = $env{'form.selfenroll_dom_'.$j};
+                                $latesttypes[$newnum] = $typedom.':'.$typestr;
+                                $currdoms{$typedom} = 1;
+                                $newnum ++;
+                            }
+                        }
+                    }
+                    if ($env{'form.selfenroll_newdom'} ne '') {
+                        my $typedom = $env{'form.selfenroll_newdom'};
+                        if ((!defined($currdoms{$typedom})) && 
+                            (&Apache::lonnet::domain($typedom) ne '')) {
+                            my $typestr;
+                            my ($othertitle,$usertypes,$types) = 
+                                &Apache::loncommon::sorted_inst_types($typedom);
+                            my $othervalue = 'any';
+                            if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
+                                if (@{$types} > 0) {
+                                    $othervalue = 'other';
+                                    $typestr = join(',',(@{$types},$othervalue));
+                                }
+                                $typestr = $othervalue;
+                            } else {
+                                $typestr = $othervalue;
+                            } 
+                            $latesttypes[$newnum] = $typedom.':'.$typestr;
+                            $newnum ++ ;
+                        }
+                    }
                     my $selfenroll_types = join(';',@latesttypes);
                     if ($selfenroll_types ne $curr_types) {
                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
@@ -4442,13 +4661,10 @@
                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
             }
         } else {
-            $r->print(&mt('No changes were needed to the existing self-enrollment settings in this course.'));
+            $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
         }
     } else {
-        $r->print(&mt('No changes were needed to the existing self-enrollment settings in this course.'));
-    }
-    if ($env{'form.selfenroll_newdom'} ne '') {
-        &print_selfenroll_menu($r,$context,$permission);
+        $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
     }
     return;
 }

--raeburn1215473629--