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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 12 May 2008 17:52:15 -0000


This is a MIME encoded message

--raeburn1210614735
Content-Type: text/plain

raeburn		Mon May 12 13:52:15 2008 EDT

  Modified files:              
    /loncom/interface	lonuserutils.pm loncreateuser.pm 
  Log:
  Bug 5176 Tracking enrollment changes.
  lonuserutils
   - $context passed as an additional arg to a number of routines so it propagates to lonnet::modify_student_enrollment() and lonnet::assignrole()
  
  loncreateuser
   - &print_userchangelogs_display() displays role change history in a course
   - &role_display_filter() and &rolechg_contexts() used by print_userchangelogs()
   - View Login History and View Change Logs added to User Management main menu
  
  Work in progress.
  
  
--raeburn1210614735
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080512135215.txt"

Index: loncom/interface/lonuserutils.pm
diff -u loncom/interface/lonuserutils.pm:1.53 loncom/interface/lonuserutils.pm:1.54
--- loncom/interface/lonuserutils.pm:1.53	Fri May  9 13:55:25 2008
+++ loncom/interface/lonuserutils.pm	Mon May 12 13:52:08 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.53 2008/05/09 17:55:25 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.54 2008/05/12 17:52:08 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -925,8 +925,15 @@
 
 
 sub get_dates_from_form {
-    my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
-    my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate');
+    my ($startname,$endname) = @_;
+    if ($startname eq '') {
+        $startname = 'startdate';
+    }
+    if ($endname eq '') {
+        $endname = 'enddate';
+    }
+    my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
+    my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
     if ($env{'form.no_end_date'}) {
         $enddate = 0;
     }
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.238 loncom/interface/loncreateuser.pm:1.239
--- loncom/interface/loncreateuser.pm:1.238	Tue Mar 11 22:45:06 2008
+++ loncom/interface/loncreateuser.pm	Mon May 12 13:52:09 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.238 2008/03/12 02:45:06 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.239 2008/05/12 17:52:09 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2041,13 +2041,14 @@
                       &mt('Contact your <a href="[_1]">helpdesk</a> for more information.',"javascript:helpMenu('display')").'<br />');
         }
         $r->print($no_forceid_alert.
-                  &Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts, \%curr_rules));
+                  &Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules));
     }
     if ($env{'form.action'} eq 'singlestudent') {
-        &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser);
-        $r->print('<p><a href="javascript:backPage(document.userupdate)">'.                  &mt('Enroll Another Student').'</a></p>');
+        &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context);
+        $r->print('<p><a href="javascript:backPage(document.userupdate)">'.
+                  &mt('Enroll Another Student').'</a></p>');
     } else {
-        my @rolechanges = &update_roles($r);
+        my @rolechanges = &update_roles($r,$context);
         if ($namechanged) {
             if ($context eq 'course') {
                 if (@userroles > 0) {
@@ -2075,7 +2076,7 @@
 }
 
 sub update_roles {
-    my ($r) = @_;
+    my ($r,$context) = @_;
     my $now=time;
     my @rolechanges;
     my %disallowed;
@@ -2091,7 +2092,7 @@
 		my $result =
 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
 						$env{'form.ccuname'},
-						$scope,$role);
+						$scope,$role,'','',$context);
 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
 		if ($role eq 'st') {
@@ -2110,7 +2111,7 @@
 		$r->print(&mt('Revoking custom role:').
                       ' '.$4.' by '.$3.':'.$2.' in '.$1.': <b>'.
                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
-				  $env{'form.ccuname'},$1,$2,$3,$4).
+				  $env{'form.ccuname'},$1,$2,$3,$4,'','',$context).
 		'</b><br />');
                 if (!grep(/^cr$/,@rolechanges)) {
                     push(@rolechanges,'cr');
@@ -2123,7 +2124,8 @@
 		my $result =
 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
 						$env{'form.ccuname'},
-						$scope,$role,$now,0,1);
+						$scope,$role,$now,0,1,'',
+                                                $context);
 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
 			      '<b>'.$result.'</b>').'<br />');
 		if ($role eq 'st') {
@@ -2176,7 +2178,8 @@
                     }
                 } else {
 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
-                               $env{'form.ccuname'},$url,$role,0,$now);
+                               $env{'form.ccuname'},$url,$role,0,$now,'','',
+                               $context);
 		    $output = &mt('Re-enabling [_1] in [_2]: <b>[_3]</b>',
 			      $role,$url,$result).'<br />';
 		}
@@ -2332,7 +2335,7 @@
 }
 
 sub enroll_single_student {
-    my ($r,$uhome,$amode,$genpwd,$now,$newuser) = @_;
+    my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context) = @_;
     $r->print('<h3>'.&mt('Enrolling Student').'</h3>');
 
     # Remove non alphanumeric values from section
@@ -2347,7 +2350,7 @@
             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
             $env{'form.cmiddlename'},$env{'form.clastname'},
             $env{'form.generation'},$env{'form.sections'},$enddate,
-            $startdate,'manual',undef,$env{'request.course.id'});
+            $startdate,'manual',undef,$env{'request.course.id'},'',$context);
     if ($enroll_result =~ /^ok/) {
         $r->print(&mt('<b>[_1]</b> enrolled',$env{'form.ccuname'}.':'.$env{'form.ccdomain'}));
         if ($env{'form.sections'} ne '') {
@@ -2989,6 +2992,15 @@
             &update_selfenroll_config($r,$context);
         }
         $r->print(&Apache::loncommon::end_page());
+    } elsif ($env{'form.action'} eq 'changelogs') {
+        $r->print(&header());
+        &Apache::lonhtmlcommon::add_breadcrumb
+            ({href=>'/adm/createuser?action=changelogs',
+              text=>"User Management Logs"});
+        $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Changes',
+                                                      'Course_User_Logs'));
+            &print_userchangelogs_display($r,$context,$permission);
+        $r->print(&Apache::loncommon::end_page());        
     } else {
         $r->print(&header());
         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
@@ -3109,6 +3121,16 @@
                  help => 'Course_Manage_Group',
                  permission => $permission->{'grp_manage'},
                  url => '/adm/coursegroups?refpage=cusr',
+               },
+               { text => 'View Log-in History',
+                 help => 'Course_User_Logins',
+                 action => 'logins',
+                 permission => $permission->{'cusr'},
+               },
+               { text => 'View Change Logs',
+                 help => 'Course_User_Logs',
+                 action => 'changelogs',
+                 permission => $permission->{'cusr'},
                });
         push(@menu,@courselinks);
     }
@@ -3264,6 +3286,235 @@
     return $output;
 }
 
+sub print_userchangelogs_display {
+    my ($r,$context,$permission) = @_;
+    my $formname = 'roleslog';
+    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    my %roleslog=&Apache::lonnet::dump('nohist_rolelog',$cdom,$cnum);
+    if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
+
+    $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">');
+    my %saveable_parameters = ('show' => 'scalar',);
+    &Apache::loncommon::store_course_settings('roles_log',
+                                              \%saveable_parameters);
+    &Apache::loncommon::restore_course_settings('roles_log',
+                                                \%saveable_parameters);
+    # set defaults
+    my $now = time();
+    my $defstart = $now - (7*24*3600); #7 days ago 
+    my %defaults = (
+                     page               => '1',
+                     show               => '10',
+                     role               => 'any',
+                     chgcontext         => 'any',
+                     rolelog_start_date => $defstart,
+                     rolelog_end_date   => $now,
+                   );
+    my $more_records = 0;
+
+    # set current
+    my %curr;
+    foreach my $item ('show','page','role','chgcontext') {
+        $curr{$item} = $env{'form.'.$item};
+    }
+    my ($startdate,$enddate) = 
+        &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
+    $curr{'rolelog_start_date'} = $startdate;
+    $curr{'rolelog_end_date'} = $enddate;
+    foreach my $key (keys(%defaults)) {
+        if ($curr{$key} eq '') {
+            $curr{$key} = $defaults{$key};
+        }
+    }
+    my (%whodunit,%changed);
+    $r->print(&role_display_filter($formname,$cdom,$cnum,\%curr));
+    my $showntablehdr = 0;
+    my $tablehdr = &Apache::loncommon::start_data_table().
+                   &Apache::loncommon::start_data_table_header_row().
+                   '<th>&nbsp;</th><th>'.&mt('When').'</th><th>'.&mt('Who made the change').
+                   '</th><th>'.&mt('Changed User').'</th><th>'.&mt('Role').'</th><th>'.&mt('Section').'</th><th>'.
+                   &mt('Context').'</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
+                   &Apache::loncommon::end_data_table_header_row();
+    my ($minshown,$maxshown);
+    my $minshown = 1;
+    my $count = 0;
+    if ($curr{'show'} ne &mt('all')) { 
+        $maxshown = $curr{'page'} * $curr{'show'};
+        if ($curr{'page'} > 1) {
+            $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
+        }
+    }
+    foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
+        next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
+                 ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
+        if ($curr{'show'} ne &mt('all')) {
+            if ($count >= $curr{'page'} * $curr{'show'}) {
+                $more_records = 1;
+                last;
+            }
+        }
+        if ($curr{'role'} ne 'any') {
+            next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
+        }
+        if ($curr{'chgcontext'} ne 'any') {
+            if ($curr{'chgcontext'} eq 'selfenroll') {
+                next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
+            } else {
+                next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
+            }
+        }
+        $count ++;
+        next if ($count < $minshown);
+        if (!$showntablehdr) {
+            $r->print($tablehdr);
+            $showntablehdr = 1;
+        }
+        if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
+            $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
+                &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
+        }
+        if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
+            $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
+                &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
+        }
+        my $sec = $roleslog{$id}{'logentry'}{'section'};
+        if ($sec eq '') {
+            $sec = &mt('None');
+        }
+        my ($rolestart,$roleend);
+        if ($roleslog{$id}{'delflag'}) {
+            $rolestart = &mt('deleted');
+            $roleend = &mt('deleted');
+        } else {
+            $rolestart = $roleslog{$id}{'logentry'}{'start'};
+            $roleend = $roleslog{$id}{'logentry'}{'end'};
+            if ($rolestart eq '' || $rolestart == 0) {
+                $rolestart = &mt('No start date'); 
+            } else {
+                $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
+            }
+            if ($roleend eq '' || $roleend == 0) { 
+                $roleend = &mt('No end date');
+            } else {
+                $roleend = &Apache::lonlocal::locallocaltime($roleend);
+            }
+        }
+        my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
+        if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
+            $chgcontext = 'selfenroll';
+        }
+        my %lt = &rolechg_contexts();
+        if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
+            $chgcontext = $lt{$chgcontext};
+        }
+        $r->print(&Apache::loncommon::start_data_table_row().'<td>'.$count.'</td><td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td><td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td><td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td><td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'}).'</td><td>'.$sec.'</td><td>'.$chgcontext.'</td><td>'.$rolestart.'</td><td>'.$roleend.'</td>'.&Apache::loncommon::end_data_table_row()."\n");
+    }
+    if ($showntablehdr) {
+        $r->print(&Apache::loncommon::end_data_table().'<br />');
+        if (($curr{'page'} > 1) || ($more_records)) {
+            $r->print('<table><tr>');
+            if ($curr{'page'} > 1) {
+                $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
+            }
+            if ($more_records) {
+                $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
+            }
+            $r->print('</tr></table>');
+            $r->print(<<"ENDSCRIPT");
+<script type="text/javascript">
+function chgPage(caller) {
+    if (caller == 'previous') {
+        document.$formname.page.value --;
+    }
+    if (caller == 'next') {
+        document.$formname.page.value ++;
+    }
+    document.$formname.submit(); 
+    return;
+}
+</script>
+ENDSCRIPT
+        }
+    } else {
+        $r->print(&mt('There are no records to display'));
+    }
+    $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'.
+              '<input type="hidden" name="action" value="changelogs" /></form>');
+    return;
+}
+
+sub role_display_filter {
+    my ($formname,$cdom,$cnum,$curr) = @_;
+    my $context = 'course';
+    my $nolink = 1;
+    my $output = '<table><tr><td valign="top">'.
+                 '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b><br />'.
+                 &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
+                                              (&mt('all'),5,10,20,50,100,1000,10000)).
+                 '</td><td>&nbsp;&nbsp;</td>';
+    my $startform =
+        &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
+                                            $curr->{'rolelog_start_date'},undef,
+                                            undef,undef,undef,undef,undef,undef,$nolink);
+    my $endform =
+        &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
+                                            $curr->{'rolelog_end_date'},undef,
+                                            undef,undef,undef,undef,undef,undef,$nolink);
+    my %lt = &rolechg_contexts();
+    $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br /><table><tr><td>'.&mt('After:').
+               '</td><td>'.$startform.'</td></tr><tr><td>'.&mt('Before:').'</td><td>'.
+               $endform.'</td></tr></table></td><td>&nbsp;&nbsp;</td>'.
+               '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
+               '<select name="role"><option value="any"';
+    if ($curr->{'role'} eq 'any') {
+        $output .= ' selected="selected"';
+    }
+    $output .=  '>'.&mt('Any').'</option>'."\n";
+    my @roles = &Apache::lonuserutils::course_roles($context,undef,1);
+    foreach my $role (@roles) {
+        my $plrole;
+        if ($role eq 'cr') {
+            $plrole = &mt('Custom Role');
+        } else {
+            $plrole=&Apache::lonnet::plaintext($role);
+        }
+        my $selstr = '';
+        if ($role eq $curr->{'role'}) {
+            $selstr = ' selected="selected"';
+        }
+        $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
+    }
+    $output .= '</select></td><td>&nbsp;&nbsp;</td><td valign="top"><b>'.
+               &mt('Context:').'</b><br /><select name="chgcontext">';
+    foreach my $chgtype ('any','auto','updatenow','createcourse','course','domain','selfenroll') {
+        my $selstr = '';
+        if ($curr->{'chgcontext'} eq $chgtype) {
+            $output .= $selstr = ' selected="selected"';
+        }
+        if (($chgtype eq 'auto') || ($chgtype eq 'updatenow')) {
+            next if (!&Apache::lonnet::auto_run($cnum,$cdom));
+        }
+        $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
+    } 
+    $output .= '</select></td><td>&nbsp;&nbsp;</td><td valign="middle"><input type="submit" value="'.
+               &mt('Update Display').'" /></tr></table><hr noshade><br />';
+    return $output;
+}
+
+sub rolechg_contexts {
+    my %lt = &Apache::lonlocal::texthash (
+                                             any          => 'Any',
+                                             auto         => 'Automated enrollment',
+                                             updatenow    => 'Roster Update',
+                                             createcourse => 'Course Creation',
+                                             course       => 'User Management in course',
+                                             domain       => 'User Management in domain',
+                                             selfenroll   => 'Self-enrolled', 
+                                         );
+    return %lt;
+}
+
 #-------------------------------------------------- functions for &phase_two
 sub user_search_result {
     my ($context,$srch) = @_;

--raeburn1210614735--