--- loncom/interface/lonuserutils.pm	2007/12/01 03:48:20	1.6
+++ loncom/interface/lonuserutils.pm	2007/12/05 19:11:37	1.10
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.6 2007/12/01 03:48:20 albertel Exp $
+# $Id: lonuserutils.pm,v 1.10 2007/12/05 19:11:37 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);
 
 ###############################################################
 ###############################################################
@@ -1357,6 +1358,8 @@ sub print_userlist {
                     &gather_userinfo($context,$format,\%userlist,$indexhash,
                                      \%userinfo,\%allusers);
                 } else {
+                    $r->print('<input type="hidden" name="phase" value="'.
+                              $env{'form.phase'}.'" /></form>');
                     return;
                 }
             }
@@ -1380,17 +1383,18 @@ sub print_userlist {
         # Print out the available choices
         my $usercount;
         if ($env{'form.action'} eq 'modifystudent') {
-            ($usercount) = &show_users_list($r,$context,'view','modify',
+            ($usercount) = &show_users_list($r,$context,'view',$permission,
                                  $env{'form.Status'},\%userlist,$keylist);
         } else {
             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
-                               'aboutme',$env{'form.Status'},\%userlist,$keylist);
+                               $permission,$env{'form.Status'},\%userlist,$keylist);
         }
         if (!$usercount) {
             $r->print('<br />'.&mt('There are no users matching the search criteria.')); 
         }
     }
-    $r->print('</form>');
+    $r->print('<input type="hidden" name="phase" value="'.
+              $env{'form.phase'}.'" /></form>');
 }
 
 sub list_submit_button {
@@ -1492,7 +1496,8 @@ sub courses_selector {
     my $format_reply;
     my $jscript = '';
 
-    my $totcodes =
+    my $totcodes = 0;
+    $totcodes =
         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                                                       $cdom,$totcodes);
     if ($totcodes > 0) {
@@ -1694,7 +1699,7 @@ sub process_date_info {
 }
 
 sub show_users_list {
-    my ($r,$context,$mode,$linkto,$statusmode,$userlist,$keylist)=@_;
+    my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist)=@_;
     #
     # Variables for excel output
     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
@@ -1747,6 +1752,29 @@ END
     }
     unless ($mode eq 'autoenroll') {
         $r->print(<<END);
+
+<script type="text/javascript" language="Javascript">
+
+function username_display_launch(username,domain) {
+    var target;
+    for (var i=0; i<document.studentform.usernamelink.length; i++) {
+        if (document.studentform.usernamelink[i].checked) {
+            target = document.studentform.usernamelink[i].value;
+        }
+    }
+    if (target == 'modify') {
+        document.studentform.srchterm.value=username;
+        document.studentform.srchdomain.value=domain;
+        document.studentform.phase.value='get_user_info';
+        document.studentform.action.value = 'singleuser';
+        document.studentform.submit();
+    }
+    else {
+        document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
+    }
+}
+</script>
+
 <input type="hidden" name="state" value="$env{'form.state'}" />
 END
     }
@@ -1770,6 +1798,9 @@ END
                        'clicker'    => "clicker id",
                        'photo'      => "photo",
                        'extent'     => "extent",
+                       'link'       => "Behavior of username links",
+                       'aboutme'    => "Display a user's personal page",
+                       'modify'     => "Modify a user's information",
                       );
     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
         $lt{'extent'} = &mt('Course(s): description, section(s), status');
@@ -1811,13 +1842,33 @@ END
     my $output;
     if ($mode eq 'html' || $mode eq 'view') {
         $r->print(<<END);
-<input type="hidden" name="sname"  value="" />
-<input type="hidden" name="sdom"   value="" />
+<input type="hidden" name="srchby"  value="uname" />
+<input type="hidden" name="srchin"   value="dom" />
+<input type="hidden" name="srchtype" value="exact" />
+<input type="hidden" name="srchterm" value="" />
+<input type="hidden" name="srchdomain" value="" />
 END
-        if ($linkto eq 'aboutme') {
-            $output = &mt("Select a user name to view the user's personal page.");
-        } elsif ($linkto eq 'modify') {
-            $output = &mt("Select a user name to modify the user's information");
+         if ($mode ne 'autoenroll') {
+             $output = '<p>';
+             my @linkdests = ('aboutme');
+             if ($permission->{'cusr'}) {
+                 push (@linkdests,'modify');
+                 $output .= '<span class="LC_nobreak">'.$lt{'link'}.':&nbsp;';
+                 my $usernamelink = $env{'form.usernamelink'};
+                 if ($usernamelink eq '') {
+                     $usernamelink = 'aboutme';
+                 }
+                 foreach my $item (@linkdests) {
+                     my $checkedstr = '';
+                     if ($item eq $usernamelink) {
+                         $checkedstr = ' checked="checked" ';
+                     }
+                     $output .= '<label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.'>&nbsp;'.$lt{$item}.'</label>&nbsp;&nbsp;';
+                 }
+                 $output .= '</span><br />';
+             } else {
+                 $output .= &mt("Click on a username to view the user's personal page.").'<br />';
+             }
         }
         $output .= "\n<p>\n".
                   &Apache::loncommon::start_data_table().
@@ -1998,22 +2049,12 @@ END
         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
             $r->print(&Apache::loncommon::start_data_table_row());
             $r->print("<td>$rowcount</td>\n");
-            if ($linkto eq 'aboutme') {
-                $in{'username'} = 
-                    &Apache::loncommon::aboutmewrapper($in{'username'},
-                                                       $in{'username'},
-                                                       $in{'domain'});
-            } elsif ($linkto eq 'modify') {
-                $in{'username'} = '<a href="'.
-                          "javascript:document.studentform.sname.value='".
-                           $in{'username'}.
-                           "';document.studentform.sdom.value='".$in{'domain'}.
-                           "';document.studentform.state.value='selected".
-                           "';document.studentform.submit();".'">'.
-                           $in{'username'}."</a>\n";
-            }
             foreach my $item (@cols) {
-                $r->print('<td>'.$in{$item}.'</td>'."\n");
+                if ($item eq 'username') {
+                    $r->print('<td>'.&print_username_link($permission,\%in).'</td>');
+                } else {
+                    $r->print('<td>'.$in{$item}.'</td>'."\n");
+                }
             }
             if ($context eq 'course') {
                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
@@ -2094,6 +2135,21 @@ END
     }
 }
 
+sub print_username_link {
+    my ($permission,$in) = @_;
+    my $output;
+    if (!$permission->{'cusr'}) {
+        $output = &Apache::loncommon::aboutmewrapper($in->{'username'},
+                                                     $in->{'username'},
+                                                     $in->{'domain'});
+    } else {
+        $output = '<a href="javascript:username_display_launch('.
+                  "'$in->{'username'}','$in->{'domain'}'".')" />'.
+                  $in->{'username'}.'</a>';
+    }
+    return $output;
+}
+
 sub role_type_names {
     my %lt = &Apache::lonlocal::texthash (
                          'domain'             => 'Domain Roles',
@@ -2872,15 +2928,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.");
@@ -2927,5 +3005,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;