--- loncom/interface/lonuserutils.pm 2007/11/10 22:18:09 1.4
+++ loncom/interface/lonuserutils.pm 2007/12/21 15:22:01 1.19
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Utility functions for managing LON-CAPA user accounts
#
-# $Id: lonuserutils.pm,v 1.4 2007/11/10 22:18:09 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.19 2007/12/21 15:22:01 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);
###############################################################
###############################################################
@@ -45,8 +46,7 @@ sub modifystudent {
# if $csec is undefined, drop the student from all the courses matching
# this one. If $csec is defined, drop them from all other sections of
# this course and add them to section $csec
- my $cdom = $env{'course.'.$courseid.'.domain'};
- my $cnum = $env{'course.'.$courseid.'.num'};
+ my ($cnum,$cdom) = &get_course_identity($courseid);
my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
my ($tmp) = keys(%roles);
# Bail out if we were unable to get the students roles
@@ -85,28 +85,39 @@ sub modifystudent {
sub modifyuserrole {
my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
$first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
- $end,$start) = @_;
- my ($scope,$userresult,$authresult,$roleresult);
+ $end,$start,$checkid) = @_;
+ my ($scope,$userresult,$authresult,$roleresult,$idresult);
if ($setting eq 'course' || $context eq 'course') {
$scope = '/'.$cid;
$scope =~ s/\_/\//g;
if ($role ne 'cc' && $sec ne '') {
$scope .='/'.$sec;
}
- } elsif ($setting eq 'domain') {
+ } elsif ($context eq 'domain') {
$scope = '/'.$env{'request.role.domain'}.'/';
- } elsif ($setting eq 'construction_space') {
+ } elsif ($context eq 'author') {
$scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
}
if ($context eq 'domain') {
my $uhome = &Apache::lonnet::homeserver($uname,$udom);
if ($uhome ne 'no_host') {
- if (($changeauth) && (&Apache::lonnet::allowed('mau',$udom))) {
+ if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
($umode eq 'localauth')) {
$authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
}
}
+ if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
+ ($env{'form.recurseid'}) && ($checkid)) {
+ my %userupdate = (
+ lastname => $last,
+ middlename => $middle,
+ firstname => $first,
+ generation => $gene,
+ id => $uid,
+ );
+ $idresult = &propagate_id_change($uname,$udom,\%userupdate);
+ }
}
}
$userresult =
@@ -114,14 +125,83 @@ sub modifyuserrole {
$middle,$last,$gene,$forceid,$desiredhome,
$email,$role,$start,$end);
if ($userresult eq 'ok') {
- if ($role ne '') {
+ if ($role ne '') {
$roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
$role,$end,$start);
}
}
- return ($userresult,$authresult,$roleresult);
+ return ($userresult,$authresult,$roleresult,$idresult);
+}
+
+sub propagate_id_change {
+ my ($uname,$udom,$user) = @_;
+ my (@types,@roles);
+ @types = ('active','future');
+ @roles = ('st');
+ my $idresult;
+ my %roleshash = &Apache::lonnet::get_my_roles($uname,
+ $udom,'userroles',\@types,\@roles);
+ my %args = (
+ one_time => 1,
+ );
+ foreach my $item (keys(%roleshash)) {
+ my ($cnum,$cdom,$role) = split(/:/,$item);
+ my ($start,$end) = split(/:/,$roleshash{$item});
+ if (&Apache::lonnet::is_course($cdom,$cnum)) {
+ my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
+ my %coursehash =
+ &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
+ my $cdesc = $coursehash{'description'};
+ if ($cdesc eq '') {
+ $cdesc = $cdom.'_'.$cnum;
+ }
+ if ($result eq 'ok') {
+ $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'
'."\n";
+ } else {
+ $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'
'."\n";
+ }
+ }
+ }
+ return $idresult;
+}
+
+sub update_classlist {
+ my ($cdom,$cnum,$udom,$uname,$user) = @_;
+ my ($uid,$classlistentry);
+ my $fullname =
+ &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
+ $user->{'lastname'},$user->{'generation'},
+ 'lastname');
+ my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
+ $cdom,$cnum);
+ my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
+ my $ididx=&Apache::loncoursedata::CL_ID() - 2;
+ my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
+ for (my $i=0; $i<@classinfo; $i++) {
+ if ($i == $ididx) {
+ if (defined($user->{'id'})) {
+ $classlistentry .= $user->{'id'}.':';
+ } else {
+ $classlistentry .= $classinfo[$i].':';
+ }
+ } elsif ($i == $nameidx) {
+ $classlistentry .= $fullname.':';
+ } else {
+ $classlistentry .= $classinfo[$i].':';
+ }
+ }
+ $classlistentry =~ s/:$//;
+ my $reply=&Apache::lonnet::cput('classlist',
+ {"$uname:$udom" => $classlistentry},
+ $cdom,$cnum);
+ if (($reply eq 'ok') || ($reply eq 'delayed')) {
+ return 'ok';
+ } else {
+ return 'error: '.$reply;
+ }
}
+
###############################################################
###############################################################
# build a role type and role selection form
@@ -130,7 +210,7 @@ sub domain_roles_select {
# domain context
#
# Role types
- my @roletypes = ('domain','construction_space','course');
+ my @roletypes = ('domain','author','course');
my %lt = &role_type_names();
#
# build up the menu information to be passed to
@@ -152,22 +232,28 @@ sub domain_roles_select {
my @roles;
if ($roletype eq 'domain') {
@roles = &domain_roles();
- } elsif ($roletype eq 'construction_space') {
+ } elsif ($roletype eq 'author') {
@roles = &construction_space_roles();
} else {
- @roles = &course_roles('domain');
+ my $custom = 1;
+ @roles = &course_roles('domain',undef,$custom);
}
my $order = ['Any',@roles];
$select_menus{$roletype}->{'order'} = $order;
foreach my $role (@roles) {
- $select_menus{$roletype}->{'select2'}->{$role} =
- &Apache::lonnet::plaintext($role);
+ if ($role eq 'cr') {
+ $select_menus{$roletype}->{'select2'}->{$role} =
+ &mt('Custom role');
+ } else {
+ $select_menus{$roletype}->{'select2'}->{$role} =
+ &Apache::lonnet::plaintext($role);
+ }
}
$select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
}
my $result = &Apache::loncommon::linked_select_forms
('studentform',(' 'x3).&mt('Role: '),$env{'form.roletype'},
- 'roletype','showrole',\%select_menus,['domain','construction_space','course']);
+ 'roletype','showrole',\%select_menus,['domain','author','course']);
return $result;
}
@@ -572,7 +658,7 @@ sub print_upload_manager_footer {
my $formname;
if ($context eq 'course') {
$formname = 'document.studentform';
- } elsif ($context eq 'construction_space') {
+ } elsif ($context eq 'author') {
$formname = 'document.studentform';
} elsif ($context eq 'domain') {
$formname = 'document.studentform';
@@ -623,7 +709,7 @@ sub print_upload_manager_footer {
$Str .= '
'.&mt('role').': '. + $options.' | '. + ''.&mt('section').': |
\n".'
'."\n".
- &mt('(only do if you know what you are doing.)')."
\n"; + if ($context eq 'course' || $context eq 'domain') { + $Str .= &forceid_change($context); + } $Str .= '
\n".'
'."\n".
+ &mt('(only do if you know what you are doing.)')."
\n";
+ if ($context eq 'domain') {
+ $output .= '
'.&list_submit_button(&mt('Update Display')). "\n
\n"); + if ($env{'form.coursepick'}) { + $r->print('';
+ my @linkdests = ('aboutme');
+ if ($permission->{'cusr'}) {
+ push (@linkdests,'modify');
+ $output .= ''.$lt{'link'}.': ';
+ my $usernamelink = $env{'form.usernamelink'};
+ if ($usernamelink eq '') {
+ $usernamelink = 'aboutme';
+ }
+ foreach my $item (@linkdests) {
+ my $checkedstr = '';
+ if ($item eq $usernamelink) {
+ $checkedstr = ' checked="checked" ';
+ }
+ $output .= '
';
+ } else {
+ $output .= &mt("Click on a username to view the user's personal page.").'
';
+ }
+ if ($actionselect) {
+ $output .= <<"END";
+$lt{'ac'}: $actionselect
+ 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"); + } } $output .= "\n
\n". &Apache::loncommon::start_data_table(). @@ -1729,15 +1975,16 @@ END
'.&mt('manual').' | |
'.&Apache::lonhtmlcommon::start_pick_box()."\n"; + if ($mode eq 'upload') { + my ($options,$cb_script,$coursepick) = + &default_role_selector($context,1); + $secbox .= &Apache::lonhtmlcommon::row_title('role','LC_oddrow_value'). + $options. &Apache::lonhtmlcommon::row_closure(1)."\n"; + } + $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n"; + if ($env{'request.course.sec'} eq '') { + $secbox .= '
'.&mt('Existing sections')."\n".
+ ' '.$sections_select.' | '.
+ &mt('New section').' '."\n". + ''."\n". + ''."\n". + ' |
@@ -2146,9 +2733,9 @@ my %lt=&Apache::lonlocal::texthash('usrn
'groups' => "active groups",
);
if ($nosort) {
- $r->print(&Apache::loncommon::start_data_table());
+ $r->print(&Apache::loncommon::start_data_table().
+ &Apache::loncommon::start_data_table_header_row());
$r->print(< \n");
- } elsif ($context eq 'construction_space') {
+ } elsif ($context eq 'author') {
$r->print(' \n");
} else {
$r->print(' \n");
@@ -2420,6 +3014,7 @@ sub upfile_drop_add {
# Get information about course groups
%curr_groups = &Apache::longroup::coursegroups();
}
+ my (%curr_rules,%got_rules,%alerts);
# Get new users list
foreach (@userdata) {
my %entries=&Apache::loncommon::record_sep($_);
@@ -2451,7 +3046,7 @@ sub upfile_drop_add {
$entries{$fields{'username'}},$fname,$mname,$lname,$gen).
'');
} else {
- my $username = $entries{$fields{'username'}};
+ my $username = $entries{$fields{'username'}};
my $sec;
if ($context eq 'course' || $setting eq 'course') {
# determine section number
@@ -2522,6 +3117,50 @@ sub upfile_drop_add {
\$lname,\$gen,\$sec,\$role) {
$$_ =~ s/(\s+$|^\s+)//g;
}
+ # check against rules
+ my $checkid = 0;
+ my $newuser = 0;
+ my (%rulematch,%inst_results,%idinst_results);
+ my $uhome=&Apache::lonnet::homeserver($username,$domain);
+ if ($uhome eq 'no_host') {
+ $checkid = 1;
+ $newuser = 1;
+ my $checkhash;
+ my $checks = { 'username' => 1 };
+ $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
+ &Apache::loncommon::user_rule_check($checkhash,$checks,
+ \%alerts,\%rulematch,\%inst_results,\%curr_rules,
+ \%got_rules);
+ if (ref($alerts{'username'}) eq 'HASH') {
+ if (ref($alerts{'username'}{$domain}) eq 'HASH') {
+ next if ($alerts{'username'}{$domain}{$username});
+ }
+ }
+ } else {
+# FIXME check if user info can be updated.
+ }
+ if ($id ne '') {
+ if (!$newuser) {
+ my %idhash = &Apache::lonnet::idrget($domain,($username));
+ if ($idhash{$username} ne $id) {
+ $checkid = 1;
+ }
+ }
+ if ($checkid) {
+ my $checkhash;
+ my $checks = { 'id' => 1 };
+ $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser,
+ 'id' => $id };
+ &Apache::loncommon::user_rule_check($checkhash,$checks,
+ \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
+ \%got_rules);
+ if (ref($alerts{'id'}) eq 'HASH') {
+ if (ref($alerts{'id'}{$domain}) eq 'HASH') {
+ next if ($alerts{'id'}{$domain}{$id});
+ }
+ }
+ }
+ }
if ($password || $env{'form.login'} eq 'loc') {
my ($userresult,$authresult,$roleresult);
if ($role eq 'st') {
@@ -2540,7 +3179,7 @@ sub upfile_drop_add {
$id,$amode,$password,$fname,
$mname,$lname,$gen,$sec,
$env{'form.forceid'},$desiredhost,
- $email,$role,$enddate,$startdate);
+ $email,$role,$enddate,$startdate,$checkid);
}
$flushc =
&user_change_result($r,$userresult,$authresult,
@@ -2551,7 +3190,7 @@ sub upfile_drop_add {
$r->print(' \n".&mt('Processed [_1] user(s).',$counts{'user'}).
"
$lt{'usrn'}
$lt{'dom'}
@@ -2158,32 +2745,32 @@ my %lt=&Apache::lonlocal::texthash('usrn
$lt{'start'}
$lt{'end'}
$lt{'groups'}
-
END
-
+ $r->print(&Apache::loncommon::end_data_table_header_row());
} else {
- $r->print(&Apache::loncommon::start_data_table());
+ $r->print(&Apache::loncommon::start_data_table().
+ &Apache::loncommon::start_data_table_header_row());
$r->print(<
+
- $lt{'usrn'}
+ $lt{'usrn'}
- $lt{'dom'}
+ $lt{'dom'}
- ID
+ ID
- $lt{'sn'}
+ $lt{'sn'}
- $lt{'sec'}
+ $lt{'sec'}
- $lt{'start'}
+ $lt{'start'}
- $lt{'end'}
+ $lt{'end'}
- $lt{'groups'}
+ $lt{'groups'}
-
END
+ $r->print(&Apache::loncommon::end_data_table_header_row());
}
#
# Sort the students
@@ -2230,6 +2817,13 @@ END
}
my $status = $sdata->[$index{'status'}];
next if ($status ne 'Active');
+ if ($env{'request.course.sec'} ne '') {
+ if ($section ne $env{'request.course.sec'}) {
+ next;
+ }
+ }
+ my $studentkey = $student.':'.$section;
+ my $startitem = '';
#
$r->print(&Apache::loncommon::start_data_table_row());
$r->print(<<"END");
@@ -2379,7 +2973,7 @@ sub upfile_drop_add {
} elsif ($setting eq 'course') {
$defaultrole = $env{'form.courserole'};
}
- } elsif ($context eq 'construction_space') {
+ } elsif ($context eq 'author') {
$defaultrole = $env{'form.defaultrole'};
}
if ($context eq 'domain' && $setting eq 'course') {
@@ -2402,7 +2996,7 @@ sub upfile_drop_add {
#######################################
if ($context eq 'course') {
$r->print(''.&mt('Enrolling Users')."
\n'.&mt('Updating Co-authors')."
\n'.&mt('Adding/Modifying Users')."
\n
'.
&mt('[_1]: Unable to enroll. No password specified.',$username)
);
- } elsif ($context eq 'construction_space') {
+ } elsif ($context eq 'author') {
$r->print('
'.
&mt('[_1]: Unable to add co-author. No password specified.',$username)
);
@@ -2565,9 +3204,7 @@ sub upfile_drop_add {
}
} # end of foreach (@userdata)
# Flush the course logs so reverse user roles immediately updated
- if ($context eq 'course' || ($context eq 'domain' && $setting eq 'course')) {
- &Apache::lonnet::flushcourselogs();
- }
+ &Apache::lonnet::flushcourselogs();
$r->print("
'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,users].",$count).'
'); + if ($count > 0) { + if ($choice eq 'revoke' || $choice eq 'drop') { + $r->print(''.&mt('Re-enabling will re-activate data for the role.
')); + } + # Flush the course logs so reverse user roles immediately updated + &Apache::lonnet::flushcourselogs(); + } + if ($env{'form.makedatesdefault'}) { + if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') { + $r->print(&make_dates_default($startdate,$enddate)); } } - $r->print(''.&mt('Dropped [_1] user(s).',$count).'
'); - $r->print(''.&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]',
+ ''.$result.'').'
';
+ }
}
+}
+
+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.");
@@ -2749,5 +3648,436 @@ 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 = '