--- loncom/interface/lonuserutils.pm	2007/12/26 13:55:32	1.35
+++ loncom/interface/lonuserutils.pm	2008/01/02 08:01:22	1.40
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.35 2007/12/26 13:55:32 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.40 2008/01/02 08:01:22 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -76,7 +76,7 @@ sub modifystudent {
         }
     }
     if ($result eq '') {
-        $result = 'Unable to find section for this student';
+        $result = &mt('Unable to find section for this student');
     } else {
         $result =~ s/(ok:)+/ok/g;
     }
@@ -333,17 +333,12 @@ sub print_upload_manager_header {
 sub javascript_validations {
     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
         $context,$groupslist)=@_;
-
     my %param = (
                   kerb_def_dom => $krbdefdom,
                   curr_authtype => $curr_authtype,
                 );
-    if ($mode eq 'createuser') {
-        $param{'formname'} = 'cu';
-    } elsif ($mode eq 'upload') {
+    if ($mode eq 'upload') {
         $param{'formname'} = 'studentform';
-    } elsif ($mode eq 'singlestudent') {
-        $param{'formname'} = 'cu';
     } elsif ($mode eq 'createcourse') {
         $param{'formname'} = 'ccrs';
     } elsif ($mode eq 'modifycourse') {
@@ -367,7 +362,7 @@ sub javascript_validations {
                 }
             } elsif ($context eq 'domain') {
                 $setsection_call = 'setCourse()';
-                $setsections_js = &dc_setcourse_js($param{'formname'},$mode);
+                $setsections_js = &dc_setcourse_js($param{'formname'},$mode,$context);
             }
             $finish = "  var checkSec = $setsection_call\n".
                       "  if (checkSec == 'ok') {\n".
@@ -389,8 +384,7 @@ sub javascript_validations {
          role     => 'The optional role field was not specified.',
          continue => 'Continue adding users?',
          );
-
-    my $function_name =(<<END);
+    my $function_name = <<"END";
 $setsections_js
 
 function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
@@ -456,7 +450,7 @@ END
     foundatype=1;
     if (current.argfield == null || current.argfield == '') {
         var alertmsg = '';
-        switch (current.value) {
+        switch (current.radiovalue) {
             case 'krb':
                 alertmsg = '$alert{'krb'}';
                 break;
@@ -521,14 +515,8 @@ END
 }
 END
     }
-    my $result = $function_name;
-    if ( ($mode eq 'upload') || ($mode eq 'createcourse') || ($mode eq 'modifycourse')  ) {
-        $result .= $auth_checks;
-    }
-    $result .= $optional_checks."\n".$section_checks;
-    if ( ($mode eq 'upload') || ($mode eq 'createcourse') || ($mode eq 'modifycourse')  ) {
-        $result .= $authheader;
-    }
+    my $result = $function_name.$auth_checks.$optional_checks."\n".
+                 $section_checks.$authheader;
     return $result;
 }
 ###############################################################
@@ -761,8 +749,7 @@ sub print_upload_manager_footer {
                                      $permission,$context,'upload');
         $Str .= $secbox."<h3>".&mt('Full Update')."</h3>\n".
                 '<p><label><input type="checkbox" name="fullup" value="yes">'.
-                ' '.&mt('Display students with current/future access, who are  not in the uploaded file.').'<br />'.&mt('Students selected from this list can be dropped.');
-                "</label></p>\n";
+                ' '.&mt('Display students with current/future access who are not in the uploaded file.').'</label><br />'.&mt('Students selected from this list can be dropped.').'</p>'."\n";
     }
     if ($context eq 'course' || $context eq 'domain') {
         $Str .= &forceid_change($context);
@@ -1342,8 +1329,9 @@ sub print_userlist {
             $showroles = undef;
         }
         my $withsec = 1;
+        my $hidepriv = 1;
         my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
-                                    \@statuses,$showroles,undef,$withsec);
+                                    \@statuses,$showroles,undef,$withsec,$hidepriv);
         &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                          \%advrolehash,$permission);
     } else {
@@ -1394,7 +1382,8 @@ sub print_userlist {
             } elsif ($env{'form.roletype'} eq 'course') {
                 if ($env{'form.coursepick'}) {
                     my %courses = &process_coursepick();
-                    my %allusers; 
+                    my %allusers;
+                    my $hidepriv = 1;
                     foreach my $cid (keys(%courses)) {
                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                         next if ($cnum eq '' || $cdom eq '');
@@ -1412,7 +1401,7 @@ sub print_userlist {
                         foreach my $type (@statuses) {
                             $access{$type} = $type;
                         }
-                        &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash);
+                        &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
                         foreach my $user (keys(%userdata)) {
                             next if (ref($userinfo{$user}) eq 'HASH');
                             foreach my $item ('fullname','id') {
@@ -1918,7 +1907,19 @@ END
         my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
         my $singconfirm = &mt(' for a single user?');
         my $multconfirm = &mt(' for multiple users?');
-        my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode); 
+        my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
+        my %lt = &Apache::lonlocal::texthash( 
+              acwi => 'Access will be set to start immediately',
+              asyo => 'as you did not select an end date in the pop-up window',
+              accw => 'Access will be set to continue indefinitely',
+              asyd => 'as you did not select an end date in the pop-up window',
+              sewi => "Sections will be switched to 'No section'",
+              ayes => "as you either selected the 'No section' option",
+              oryo => 'or you did not select a section in the pop-up window',
+              arol => 'A role with no section will be added',
+              swbs => 'Sections will be switched to:',
+              rwba => 'Roles will be added for section(s):',
+        );
         $r->print(<<END);
 
 <script type="text/javascript" language="Javascript">
@@ -1944,12 +1945,47 @@ function verify_action (field) {
     } 
     else {
         var message = document.studentform.bulkaction[document.studentform.bulkaction.selectedIndex].text;
+        var choice = document.studentform.bulkaction[document.studentform.bulkaction.selectedIndex].value;
         if (numchecked == 1) { 
             message += singconf;
         } 
         else {
             message += multconf; 
         }
+        if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
+            var datemsg = '';
+            if ((document.studentform.startdate_month.value == '') && 
+                (document.studentform.startdate_day.value  == '') &&
+                (document.studentform.startdate_year.value == '')) {
+                datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
+            }
+            if ((document.studentform.enddate_month.value == '') &&
+                (document.studentform.enddate_day.value  == '') &&
+                (document.studentform.enddate_year.value == '')) {
+                datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
+            }
+            if (datemsg != '') {
+                message += "\\n"+datemsg;
+            }
+        }
+        if (choice == 'chgsec') {
+            var rolefilter = document.studentform.showrole.options[document.studentform.showrole.selectedIndex].value;
+            var retained =  document.studentform.retainsec.value;
+            var secshow = document.studentform.newsecs.value;
+            if (secshow == '') {
+                if (rolefilter == 'st' || retained == 0 || retained == "") {
+                    message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n"; 
+                } else {
+                    message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
+                }
+            } else {
+                if (rolefilter == 'st' || retained == 0 || retained == "") {
+                    message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
+                } else {
+                    message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
+                }
+            }
+        }
         if (confirm(message)) {
             document.studentform.phase.value = 'bulkchange';
             document.studentform.submit();
@@ -2377,7 +2413,6 @@ END
                 $r->print("<td>$cellentry</td>\n");
             } else {
                 $r->print("<td>$rowcount</td>\n");
-                $checkval;
                 if ($actionselect) {
                     my $showcheckbox;
                     if ($role =~ /^cr\//) {
@@ -2677,7 +2712,15 @@ function saveselections(formname) {
 END
     if ($env{'form.bulkaction'} eq 'chgsec') {
         $output .= <<"END";
-        opener.document.$callingform.retainsec.value = formname.retainsec.value;
+        if (formname.retainsec.length > 1) {  
+            for (var i=0; i<formname.retainsec.length; i++) {
+                if (formname.retainsec[i].checked == true) {
+                    opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
+                }
+            }
+        } else {
+            opener.document.$callingform.retainsec.value = formname.retainsec.value;
+        }
         setSections(formname);
         if (seccheck == 'ok') {
             opener.document.$callingform.newsecs.value = formname.sections.value;
@@ -3951,7 +3994,11 @@ sub update_user_list {
                         if (@newsecs == 0) {
                             $result = &mt('No change in section assignment (none)');
                             $nochg = 1;
-                        }
+                        } else {
+                            $revresult =
+                                &Apache::lonnet::revokerole($udom,$uname,
+                                                            $scope,$role);
+                        } 
                     } else {
                         if (@newsecs > 0) {
                             if (grep(/^\Q$sec\E$/,@newsecs)) {
@@ -4170,7 +4217,7 @@ sub get_groupslist {
 }
 
 sub setsections_javascript {
-    my ($formname,$groupslist,$mode) = @_;
+    my ($formname,$groupslist,$mode,$checkauth) = @_;
     my ($checkincluded,$finish,$rolecode,$setsection_js);
     if ($mode eq 'upload') {
         $checkincluded = 'formname.name == "'.$formname.'"';
@@ -4178,12 +4225,26 @@ sub setsections_javascript {
         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
     } elsif ($formname eq 'cu') {
         $checkincluded = 'formname.elements[i-1].checked == true';
-        $finish = 'formname.submit()';
+        if ($checkauth) {
+            $finish = "var authcheck = auth_check();\n".
+                      "   if (authcheck == 'ok') {\n".
+                      "       formname.submit();\n".
+                      "   }\n";
+        } else {
+            $finish = 'formname.submit()';
+        }
         $rolecode = "var match = str.split('_');
                 var role = match[3];\n";
     } elsif ($formname eq 'enrollstudent') {
         $checkincluded = 'formname.name == "'.$formname.'"';
-        $finish = 'formname.submit()';
+        if ($checkauth) {
+            $finish = "var authcheck = auth_check();\n".
+                      "   if (authcheck == 'ok') {\n".
+                      "       formname.submit();\n".
+                      "   }\n";
+        } else {
+            $finish = 'formname.submit()';
+        }
         $rolecode = "var match = str.split('_');
                 var role = match[1];\n";
     } else {
@@ -4520,15 +4581,19 @@ sub get_course_identity {
 }
 
 sub dc_setcourse_js {
-    my ($formname,$mode) = @_;
-    my $dc_setcourse_code;
+    my ($formname,$mode,$context) = @_;
+    my ($dc_setcourse_code,$authen_check);
     my $cctext = &Apache::lonnet::plaintext('cc');
     my %alerts = &sectioncheck_alerts();
     my $role = 'role';
     if ($mode eq 'upload') {
         $role = 'courserole';
+    } else {
+        $authen_check = &verify_authen($formname,$context);
     }
     $dc_setcourse_code = (<<"SCRIPTTOP");
+$authen_check
+
 function setCourse() {
     var course = document.$formname.dccourse.value;
     if (course != "") {
@@ -4613,9 +4678,11 @@ SCRIPTTOP
             }
         }
     }
-    document.$formname.submit();
+    var authcheck = auth_check();
+    if (authcheck == 'ok') {
+        document.$formname.submit();
+    }
 }
-
 ENDSCRIPT
     } else {
         $dc_setcourse_code .=  "
@@ -4636,6 +4703,66 @@ ENDSCRIPT
         return -1;
     }
 ENDSCRIPT
+    return $dc_setcourse_code;
+}
+
+sub verify_authen {
+    my ($formname,$context) = @_;
+    my %alerts = &authcheck_alerts();
+    my $finish = "return 'ok';";
+    if ($context eq 'author') {
+        $finish = "document.$formname.submit();";
+    }
+    my $outcome = <<"ENDSCRIPT";
+
+function auth_check() {
+    var logintype;
+    if (document.$formname.login.length) {
+        if (document.$formname.login.length > 0) {
+            var loginpicked = 0;
+            for (var i=0; i<document.$formname.login.length; i++) {
+                if (document.$formname.login[i].checked == true) {
+                    loginpicked = 1;
+                    logintype = document.$formname.login[i].value;
+                }
+            }
+            if (loginpicked == 0) {
+                alert("$alerts{'authen'}");
+                return;
+            }
+        }
+    } else {
+        logintype = document.$formname.login.value;
+    }
+    if (logintype == 'nochange') {
+        return 'ok';
+    }
+    var argpicked = document.$formname.elements[logintype+'arg'].value;
+    if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
+        var alertmsg = '';
+        switch (logintype) {
+            case 'krb':
+                alertmsg = '$alerts{'krb'}';
+                break;
+            case 'int':
+                alertmsg = '$alerts{'ipass'}';
+            case 'fsys':
+                alertmsg = '$alerts{'ipass'}';
+                break;
+            case 'loc':
+                alertmsg = '';
+                break;
+            default:
+                alertmsg = '';
+        }
+        if (alertmsg != '') {
+            alert(alertmsg);
+            return;
+        }
+    }
+    $finish
+}
+ENDSCRIPT
 }
 
 sub sectioncheck_alerts {
@@ -4657,5 +4784,15 @@ sub sectioncheck_alerts {
     return %alerts;
 }
 
+sub authcheck_alerts {
+    my %alerts = 
+        &Apache::lonlocal::texthash(
+                    authen => 'You must choose an authentication type.',
+                    krb    => 'You need to specify the Kerberos domain.',
+                    ipass  => 'You need to specify the initial password.',
+        );
+    return %alerts;
+}
+
 1;