--- 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('Settings for assigning roles:').'

'."\n". &mt('Pick the action to take on roles for these users:').'
      '; } - if ($context eq 'construction_space') { + if ($context eq 'author') { $Str .= '

'.&mt('Default role')."

\n". &mt('Choose the role to assign to users without one specified in the uploaded file'); } elsif ($context eq 'course') { @@ -637,8 +723,12 @@ sub print_upload_manager_footer { my ($options,$cb_script,$coursepick) = &default_role_selector($context,'defaultrole',1); if ($context eq 'domain') { $Str .= ''.&mt('Domain Level').'
'.$options.'

'.&mt('Course Level').'
'.$cb_script.$coursepick; - } else { + } elsif ($context eq 'author') { $Str .= $options; + } else { + $Str .= '
'.&mt('role').': '. + $options.' '. + ''.&mt('section').': 
'; } if ($context eq 'course') { $Str .= "

".&mt('Full Update')."

\n". @@ -646,12 +736,9 @@ sub print_upload_manager_footer { ' '.&mt('Full update (also print list of users not enrolled anymore)'). "

\n"; } - $Str .= "

".&mt('ID/Student Number')."

\n"; - $Str .= "

\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 .= '