--- loncom/interface/lonuserutils.pm	2007/11/15 21:24:51	1.5
+++ loncom/interface/lonuserutils.pm	2007/12/05 17:53:13	1.9
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.5 2007/11/15 21:24:51 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.9 2007/12/05 17:53:13 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -35,7 +35,8 @@ use Apache::lonnet;
 use Apache::loncommon();
 use Apache::lonhtmlcommon;
 use Apache::lonlocal;
-use LONCAPA();
+use Apache::longroup;
+use LONCAPA qw(:DEFAULT :match);
 
 ###############################################################
 ###############################################################
@@ -159,7 +160,7 @@ sub propagate_id_change {
 
 sub update_classlist {
     my ($cdom,$cnum,$udom,$uname,$user) = @_;
-    my ($uid,$fullname,$classlistentry);
+    my ($uid,$classlistentry);
     my $fullname =
         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
                                      $user->{'lastname'},$user->{'generation'},
@@ -1325,7 +1326,7 @@ sub print_userlist {
                         my $cnum = $coursehash{'num'};
                         my $cdesc = $coursehash{'description'};
                         my (@roles,@sections,%access,%users,%userdata,
-                            %users,%statushash);
+                            %statushash);
                         if ($env{'form.showrole'} eq 'Any') {
                             @roles = &course_roles($context);
                             unshift(@roles,'cr');
@@ -1489,11 +1490,11 @@ sub courses_selector {
     my %idnums = ();
     my %idlist_titles = ();
     my $caller = 'global';
-    my $totcodes = 0;
     my $format_reply;
     my $jscript = '';
 
-    my $totcodes =
+    my $totcodes = 0;
+    $totcodes =
         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                                                       $cdom,$totcodes);
     if ($totcodes > 0) {
@@ -2873,15 +2874,37 @@ sub expire_user_list {
     $r->print('<p>'.&mt('Re-enrollment will re-activate data.')) if ($count);
 }
 
-sub section_check_js {
-    my $groupslist;
-    my %curr_groups = &Apache::longroup::coursegroups();
-    if (%curr_groups) {
-        $groupslist = join('","',sort(keys(%curr_groups)));
+sub classlist_drop {
+    my ($scope,$uname,$udom,$now,$action) = @_;
+    my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
+    my $cid=$cdom.'_'.$cnum;
+    my $user = $uname.':'.$udom;
+    if ($action eq 'drop') {
+        if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
+            my $result =
+                &Apache::lonnet::cput('classlist',
+                                      { $user => $now },
+                                      $env{'course.'.$cid.'.domain'},
+                                      $env{'course.'.$cid.'.num'});
+            return &mt('Drop from classlist: [_1]',
+                       '<b>'.$result.'</b>').'<br />';
+        }
     }
+}
+
+sub active_student_roles {
+    my ($cnum,$cdom,$uname,$udom) = @_;
+    my %roles =
+        &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
+                                      ['future','active'],['st']);
+    return exists($roles{"$cnum:$cdom:st"});
+}
+
+sub section_check_js {
+    my $groupslist= &get_groupslist();
     return <<"END";
 function validate(caller) {
-    var groups = new Array("$groupslist");
+    var groups = new Array($groupslist);
     var secname = caller.value;
     if ((secname == 'all') || (secname == 'none')) {
         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
@@ -2928,5 +2951,144 @@ sub set_login {
     return $response;
 }
 
+sub course_sections {
+    my ($sections_count,$role) = @_;
+    my $output = '';
+    my @sections = (sort {$a <=> $b} keys %{$sections_count});
+    if (scalar(@sections) == 1) {
+        $output = '<select name="currsec_'.$role.'" >'."\n".
+                  '  <option value="">Select</option>'."\n".
+                  '  <option value="">No section</option>'."\n".
+                  '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
+    } else {
+        $output = '<select name="currsec_'.$role.'" ';
+        my $multiple = 4;
+        if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
+        $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
+        foreach my $sec (@sections) {
+            $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
+        }
+    }
+    $output .= '</select>';
+    return $output;
+}
+
+sub get_groupslist {
+    my $groupslist;
+    my %curr_groups = &Apache::longroup::coursegroups();
+    if (%curr_groups) {
+        $groupslist = join('","',sort(keys(%curr_groups)));
+        $groupslist = '"'.$groupslist.'"';
+    }
+    return $groupslist;
+}
+
+sub setsections_javascript {
+    my ($form,$groupslist) = @_;
+    my ($checkincluded,$finish,$roleplace,$setsection_js);
+    if ($form eq 'cu') {
+        $checkincluded = 'formname.elements[i-1].checked == true';
+        $finish = 'formname.submit()';
+        $roleplace = 3;
+    } else {
+        $checkincluded = 'formname.name == "'.$form.'"';
+        $finish = "seccheck = 'ok';";
+        $roleplace = 1;
+        $setsection_js = "var seccheck = 'alert';";
+    }
+    my %alerts = &Apache::lonlocal::texthash(
+                    secd => 'Section designations do not apply to Course Coordinator roles.',
+                    accr => 'A course coordinator role will be added with access to all sections.',
+                    inea => 'In each course, each user may only have one student role at a time.',
+                    youh => 'You had selected ',
+                    secs => 'sections.',
+                    plmo => 'Please modify your selections so they include no more than one section.',
+                    mayn => 'may not be used as the name for a section, as it is a reserved word.',
+                    plch => 'Please choose a different section name.',
+                    mnot => 'may not be used as a section name, as it is the name of a course group.',
+                    secn => 'Section names and group names must be distinct. Please choose a different section name.',
+                 );
+    $setsection_js .= <<"ENDSECCODE";
+
+function setSections(formname) {
+    var re1 = /^currsec_/;
+    var groups = new Array($groupslist);
+    for (var i=0;i<formname.elements.length;i++) {
+        var str = formname.elements[i].name;
+        var checkcurr = str.match(re1);
+        if (checkcurr != null) {
+            if ($checkincluded) {
+                var match = str.split('_');
+                var role = match[$roleplace];
+                if (role == 'cc') {
+                    alert("$alerts{'secd'}\\n$alerts{'accr'}");
+                }
+                else {
+                    var sections = '';
+                    var numsec = 0;
+                    var sections;
+                    for (var j=0; j<formname.elements[i].length; j++) {
+                        if (formname.elements[i].options[j].selected == true ) {
+                            if (formname.elements[i].options[j].value != "") {
+                                if (numsec == 0) {
+                                    if (formname.elements[i].options[j].value != "") {
+                                        sections = formname.elements[i].options[j].value;
+                                        numsec ++;
+                                    }
+                                }
+                                else {
+                                    sections = sections + "," +  formname.elements[i].options[j].value
+                                    numsec ++;
+                                }
+                            }
+                        }
+                    }
+                    if (numsec > 0) {
+                        if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
+                            sections = sections + "," +  formname.elements[i+1].value;
+                        }
+                    }
+                    else {
+                        sections = formname.elements[i+1].value;
+                    }
+                    var newsecs = formname.elements[i+1].value;
+                    var numsplit;
+                    if (newsecs != null && newsecs != "") {
+                        numsplit = newsecs.split(/,/g);
+                        numsec = numsec + numsplit.length;
+                    }
+
+                    if ((role == 'st') && (numsec > 1)) {
+                        alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
+                        return;
+                    }
+                    else {
+                        if (numsplit != null) {
+                            for (var j=0; j<numsplit.length; j++) {
+                                if ((numsplit[j] == 'all') ||
+                                    (numsplit[j] == 'none')) {
+                                    alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
+                                    return;
+                                }
+                                for (var k=0; k<groups.length; k++) {
+                                    if (numsplit[j] == groups[k]) {
+                                        alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
+                                        return;
+                                    }
+                                }
+                            }
+                        }
+                        formname.elements[i+2].value = sections;
+                    }
+                }
+            }
+        }
+    }
+    $finish
+}
+ENDSECCODE
+    return $setsection_js;
+}
+
 1;