[LON-CAPA-cvs] cvs: loncom(version_2_9_X) /interface lonuserutils.pm

raeburn raeburn@source.lon-capa.org
Wed, 24 Mar 2010 03:09:41 -0000


This is a MIME encoded message

--raeburn1269400181
Content-Type: text/plain

raeburn		Wed Mar 24 03:09:41 2010 EDT

  Modified files:              (Branch: version_2_9_X)
    /loncom/interface	lonuserutils.pm 
  Log:
  - Backport 1.116, 1.117, 1.118.
  
  
--raeburn1269400181
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20100324030941.txt"

Index: loncom/interface/lonuserutils.pm
diff -u loncom/interface/lonuserutils.pm:1.97.2.15 loncom/interface/lonuserutils.pm:1.97.2.16
--- loncom/interface/lonuserutils.pm:1.97.2.15	Tue Feb  2 06:05:03 2010
+++ loncom/interface/lonuserutils.pm	Wed Mar 24 03:09:41 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.97.2.15 2010/02/02 06:05:03 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.97.2.16 2010/03/24 03:09:41 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1562,7 +1562,8 @@
                         }
                     }
                 }
-            } elsif ($env{'form.roletype'} eq 'course') {
+            } elsif (($env{'form.roletype'} eq 'course') ||
+                     ($env{'form.roletype'} eq 'community')) {
                 if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
                     my %courses = &process_coursepick();
                     my %allusers;
@@ -2302,7 +2303,7 @@
     if ($env{'form.showrole'} eq 'cr') {
         $rolefilter = &mt('custom');  
     } elsif ($env{'form.showrole'} ne 'Any') {
-        $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
+        $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
     }
     my $results_description;
     if ($mode ne 'autoenroll') {
@@ -2402,7 +2403,7 @@
             if ($env{'form.userwin'}) {
                 $checkwin = ' checked="checked"';
             }
-            $output .= '</td><td valign="top"><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
+            $output .= '</td><td valign="top" style="border-left: 1px solid;><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
         }
         $output .= "\n".'<div class="LC_clear_float_footer">&nbsp;</div>'."\n".
                   &Apache::loncommon::start_data_table().
@@ -2954,7 +2955,6 @@
                          'author' => 'Co-Author Roles',
                          'course' => 'Course Roles',
                          'community' => 'Community Roles',
-
              );
     return %lt;
 }
@@ -3173,8 +3173,8 @@
     my %lt = &Apache::lonlocal::texthash (
                  chac => 'Access dates to apply for selected users',
                  chse => 'Changes in section affiliation to apply to selected users',
-                 fors => 'For student roles changing the section, will result in a section switch as students may only be in one section of a course at a time.',
-                 forn => 'For a role in a course that is not a student role, a user may have roles in more than one section of a course at a time.',
+                 fors => 'For student roles, changing the section will result in a section switch as students may only be in one section of a course at a time.',
+                 forn => 'For a course role that is not "student", users may have roles in more than one section at a time.',
                  reta => "Retain each user's current section affiliations?", 
                  dnap => '(Does not apply to student roles).',
             );
@@ -3193,14 +3193,13 @@
                                           $permission,$crstype);
     }
     $output .= '<h3>'.$headertext.'</h3>'.
-               '<form name="'.$formname.'" method="post">'."\n".
+               '<form name="'.$formname.'" method="post" action="">'."\n".
                 $date_items;
     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
         my ($cnum,$cdom) = &get_course_identity();
-        my $crstype = &Apache::loncommon::course_type();
         if ($crstype eq 'Community') {
-            $lt{'fors'} = &mt('For member roles changing the section, will result in a section switch as members may only be in one section of a community at a time.');
-            $lt{'forn'} = &mt('For a role in a community that is not a member role, a user may have roles in more than one section of a community at a time.');
+            $lt{'fors'} = &mt('For member roles, changing the section will result in a section switch, as members may only be in one section of a community at a time.');
+            $lt{'forn'} = &mt('For a community role that is not "member", users may have roles in more than one section at a time.');
             $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
         }
         my $info;
@@ -3241,7 +3240,7 @@
     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype) = @_;
     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
     my $sections_select .= &course_sections(\%sections_count,$role);
-    my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
+    my $secbox = '<div>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
     if ($mode eq 'upload') {
         my ($options,$cb_script,$coursepick) =
             &default_role_selector($context,1,$crstype);
@@ -3254,7 +3253,7 @@
                    '<td align="center">'.&mt('Existing sections')."\n".
                    '<br />'.$sections_select.'</td><td align="center">'.
                    &mt('New section').'<br />'."\n".
-                   '<input type="text" name="newsec" size="15" />'."\n".
+                   '<input type="text" name="newsec" size="15" value="" />'."\n".
                    '<input type="hidden" name="sections" value="" />'."\n".
                    '</td></tr></table>'."\n";
     } else {
@@ -3263,7 +3262,7 @@
                    $env{'request.course.sec'};
     }
     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
-               &Apache::lonhtmlcommon::end_pick_box().'</p>';
+               &Apache::lonhtmlcommon::end_pick_box().'</div>';
     return $secbox;
 }
 
@@ -3404,7 +3403,7 @@
                 if ($rolefilter eq 'Any') {
                     $description .= &mt('All users with co-author roles in domain',$showfilter);
                 } else {
-                    $description .= &mt('All co-authors in domain  with [_1] roles',$rolefilter);
+                    $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
                 }
             }
         } elsif (($env{'form.roletype'} eq 'course') || 
@@ -3662,7 +3661,6 @@
     $str .= '<input type="hidden" name="action" value="upload" />';
     $str .= '<input type="hidden" name="state"  value="got_file" />';
 
-
     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
 
     # Excel and CSV Help
@@ -4388,7 +4386,7 @@
 # ================================================================== Phase four
 
 sub update_user_list {
-    my ($r,$context,$setting,$choice) = @_;
+    my ($r,$context,$setting,$choice,$crstype) = @_;
     my $now = time;
     my $count=0;
     my $crstype;
@@ -4406,7 +4404,7 @@
                                    'reenable' => 'Re-enabled',
                                    'activate' => 'Activated',
                                    'chgdates' => 'Changed Access Dates for',
-                                   'chgsec'   => 'Changed section for',
+                                   'chgsec'   => 'Changed section(s) for',
                                    'drop'     => 'Dropped',
                                  },
                         error => {'revoke'    => 'revoking',
@@ -4423,8 +4421,9 @@
         ($startdate,$enddate) = &get_dates_from_form();
     }
     foreach my $item (@changelist) {
-        my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
-            $scopestem);
+        my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,
+            @sections,$scopestem,$singlesec,$showsecs,$warn_singlesec,
+            $nothingtodo,$keepnosection);
         if ($choice eq 'drop') {
             ($uname,$udom,$sec) = split(/:/,$item,-1);
             $role = 'st';
@@ -4523,14 +4522,23 @@
             } elsif ($choice eq 'chgsec') {
                 my (@newsecs,$revresult,$nochg,@retained);
                 if (($role ne 'cc') && ($role ne 'co')) {
-                    @newsecs = split(/,/,$env{'form.newsecs'});
+                    my @secs = sort(split(/,/,$env{'form.newsecs'}));
+                    if (@secs) {
+                        my %curr_groups = &Apache::longroup::coursegroups();
+                        foreach my $sec (@secs) {
+                            next if (($sec =~ /\W/) || ($sec eq 'none') ||
+                            (exists($curr_groups{$sec})));
+                            push(@newsecs,$sec);
+                        }
+                    }
                 }
                 # remove existing section if not to be retained.   
-                if (!$env{'form.retainsec'}) {
+                if (!$env{'form.retainsec'} || ($role eq 'st')) {
                     if ($sec eq '') {
                         if (@newsecs == 0) {
-                            $result = &mt('No change in section assignment (none)');
+                            $result = 'ok';
                             $nochg = 1;
+                            $nothingtodo = 1;
                         } else {
                             $revresult =
                                 &Apache::lonnet::revokerole($udom,$uname,
@@ -4557,13 +4565,23 @@
                 } else {
                     if ($sec eq '') {
                         $nochg = 1;
+                        $keepnosection = 1;
                     } else { 
                         push(@retained,$sec);
                     }
                 }
                 # add new sections
+                my (@diffs,@shownew);
+                if (@retained) {
+                    @diffs = &Apache::loncommon::compare_arrays(\@retained,\@newsecs);
+                } else {
+                    @diffs = @newsecs;
+                }
                 if (@newsecs == 0) {
-                    if (!$nochg) {
+                    if ($nochg) {
+                        $result = 'ok';
+                        $nothingtodo = 1;
+                    } else {
                         if ($role eq 'st') {
                             $result = 
                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context);
@@ -4572,15 +4590,41 @@
                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
                         }
                     }
+                    $showsecs = &mt('No section');
+                } elsif (@diffs == 0) {
+                    $result = 'ok';
+                    $nothingtodo = 1;
                 } else {
                     foreach my $newsec (@newsecs) { 
                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                             if ($role eq 'st') {
                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context);
+                                if (@newsecs > 1) {
+                                    my $showsingle;
+                                    if ($newsec eq '') {
+                                        $showsingle = &mt('No section');
+                                    } else {
+                                        $showsingle = $newsec;
+                                    }
+                                    if ($crstype eq 'Community') {
+                                        $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each community member may only be in one section at a time.','<i>'.$showsingle.'</i>');
+                                    } else {
+                                        $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each student may only be in one section of a course at a time.','<i>'.$showsingle.'</i>');
+                                    }
+                                    $showsecs = $showsingle;
+                                    last;
+                                } else {
+                                    if ($newsec eq '') {
+                                        $showsecs = &mt('No section');
+                                    } else {
+                                        $showsecs = $newsec;
+                                    }
+                                }
                             } else {
                                 my $newscope = $scopestem;
                                 if ($newsec ne '') {
                                    $newscope .= '/'.$newsec;
+                                   push(@shownew,$newsec);
                                 }
                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                                                         $newscope,$role,$end,$start);
@@ -4588,6 +4632,15 @@
                         }
                     }
                 }
+                unless ($role eq 'st') {
+                    unless ($showsecs) {
+                        my @tolist = sort(@shownew,@retained);
+                        if ($keepnosection) {
+                            push(@tolist,&mt('No section'));
+                        }
+                        $showsecs = join(', ',@tolist);
+                    }
+                }
             }
         }
         my $extent = $scope;
@@ -4598,13 +4651,48 @@
             }
         }
         if ($result eq 'ok' || $result eq 'ok:') {
-            $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
-                          $plrole,$extent,$uname.':'.$udom).'<br />');
-            $count++;
+            my $dates;
+            if (($choice eq 'chgsec') || ($choice eq 'chgdates')) {
+                $dates = &dates_feedback($start,$end,$now);
+            }
+            if ($choice eq 'chgsec') {
+                if ($nothingtodo) {
+                    $r->print(&mt("Section assignment for role of '[_1]' in [_2] for '[_3]' unchanged.",$plrole,$extent,'<i>'.
+                          &Apache::loncommon::plainname($uname,$udom).
+                          '</i>').' ');
+                    if ($sec eq '') {
+                        $r->print(&mt('[_1]No section[_2] - [_3]','<b>','</b>',$dates));
+                    } else {
+                        $r->print(&mt('Section(s): [_1] - [_2]',
+                                      '<b>'.$showsecs.'</b>',$dates));
+                    }
+                    $r->print('<br />');
+                } else {
+                    $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
+                        '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
+                        '<b>'.$showsecs.'</b>',$dates).'<br />');
+                   $count ++;
+               }
+               if ($warn_singlesec) {
+                   $r->print('<div class="LC_warning">'.$warn_singlesec.'</div>');
+               }
+            } elsif ($choice eq 'chgdates') {
+                $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent,
+                      '<i>'.&Apache::loncommon::plainname($uname.':'.$udom).'</i>',
+                      $dates).'<br />');
+               $count ++;
+            } else {
+                $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
+                      '<i>'.&Apache::loncommon::plainname($uname.':'.$udom).'</i>').
+                          '<br />');
+                $count ++;
+            }
         } else {
             $r->print(
-                &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]: [_4].",
-                    $plrole,$extent,$uname.':'.$udom,$result).'<br />');
+                &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
+                    $plrole,$extent,
+                    '<i>'.&Apache::loncommon::plainname($uname.':'.$udom).'</i>',
+                    $result).'<br />');
         }
     }
     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
@@ -4620,7 +4708,7 @@
             }
         }
     }
-    $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,no users].",$count).'</b></p>');
+    $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} for [quant,_1,user role,user roles,no user roles].",$count).'</b></p>');
     if ($count > 0) {
         if ($choice eq 'revoke' || $choice eq 'drop') {
             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
@@ -4640,6 +4728,27 @@
     $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
 }
 
+sub dates_feedback {
+    my ($start,$end,$now) = @_;
+    my $dates;
+    if ($start < $now) {
+        if ($end == 0) {
+            $dates .= &mt('role(s) active now; no end date');
+        } elsif ($end > $now) {
+            $dates = &mt('role(s) active now; ends [_1].',&Apache::lonlocal::locallocaltime($end));
+        } else {
+            $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
+        }
+     } else {
+        if ($end == 0 || $end > $now) {
+            $dates = &mt('future role(s); starts: [_1].',&Apache::lonlocal::locallocaltime($start));
+        } else {
+            $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
+        }
+    }
+    return $dates;
+}
+
 sub classlist_drop {
     my ($scope,$uname,$udom,$now) = @_;
     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
@@ -4858,7 +4967,7 @@
                     for (var j=0; j<formname.elements[num].length; j++) {
                         if (formname.elements[num].options[j].selected == true ) {
                             var addsec = formname.elements[num].options[j].value;
-                            if (addsec != "") {
+                            if ((addsec != "") && (addsec != null)) {
                                 fromexisting.push(addsec);
                                 if (numsec == 0) {
                                     sections = addsec;
@@ -4880,8 +4989,8 @@
                         } else {
                             numsplit = newsecs.split(/,/g);
                         }
-                        for (var i=0; i<numsplit.length; i++) {
-                            var newsec = numsplit[i];
+                        for (var m=0; m<numsplit.length; m++) {
+                            var newsec = numsplit[m];
                             newsec = newsec.replace(trimleading,'');
                             newsec = newsec.replace(trimtrailing,'');
                             if (re2.test(newsec) == true) {
@@ -4890,8 +4999,8 @@
                                 if (newsec != '') {
                                     var isnew = 1;
                                     if (fromexisting != null) {
-                                        for (var m=0; m<fromexisting.length; m++) {
-                                            if (newsec == fromexisting[m]) {
+                                        for (var n=0; n<fromexisting.length; n++) {
+                                            if (newsec == fromexisting[n]) {
                                                 isnew = 0;
                                             }
                                         }
@@ -4938,10 +5047,10 @@
                         }
                     }
                     if ((validsecstr != '') && (validsecstr != null)) {
-                        if (numsec > 0) {
-                            sections = sections + "," + validsecstr;
-                        } else {
+                        if ((sections == '') || (sections == null)) {
                             sections = validsecstr;
+                        } else {
+                            sections = sections + "," + validsecstr;
                         }
                     }
                     formname.elements[num+2].value = sections;

--raeburn1269400181--