--- loncom/interface/lonuserutils.pm 2007/12/12 23:59:41 1.15 +++ loncom/interface/lonuserutils.pm 2007/12/21 20:34:26 1.21 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Utility functions for managing LON-CAPA user accounts # -# $Id: lonuserutils.pm,v 1.15 2007/12/12 23:59:41 raeburn Exp $ +# $Id: lonuserutils.pm,v 1.21 2007/12/21 20:34:26 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -46,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 @@ -236,8 +235,8 @@ sub domain_roles_select { } elsif ($roletype eq 'author') { @roles = &construction_space_roles(); } else { - @roles = &course_roles('domain'); - unshift(@roles,'cr'); + my $custom = 1; + @roles = &course_roles('domain',undef,$custom); } my $order = ['Any',@roles]; $select_menus{$roletype}->{'order'} = $order; @@ -772,7 +771,7 @@ sub forceid_change { ############################################################### ############################################################### sub print_upload_manager_form { - my ($r,$context) = @_; + my ($r,$context,$permission) = @_; my $firstLine; my $datatoken; if (!$env{'form.datatoken'}) { @@ -962,11 +961,10 @@ sub make_dates_default { my ($startdate,$enddate,$context) = @_; my $result = ''; if ($context eq 'course') { - my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'}; - my $crs = $env{'course.'.$env{'request.course.id'}.'.num'}; + my ($cnum,$cdom) = &get_course_identity(); my $put_result = &Apache::lonnet::put('environment', {'default_enrollment_start_date'=>$startdate, - 'default_enrollment_end_date' =>$enddate},$dom,$crs); + 'default_enrollment_end_date' =>$enddate},$cdom,$cnum); if ($put_result eq 'ok') { $result .= &mt('Set default start and end dates for course'). '
'."\n"; @@ -1048,7 +1046,8 @@ sub default_role_selector { sub default_course_roles { my ($context,$checkpriv,%customroles) = @_; my $output; - my @roles = &course_roles($context,$checkpriv); + my $custom = 1; + my @roles = &course_roles($context,$checkpriv,$custom); foreach my $role (@roles) { my $plrole=&Apache::lonnet::plaintext($role); $output .= ' '; @@ -1066,7 +1065,7 @@ sub default_course_roles { sub construction_space_roles { my ($checkpriv) = @_; - my @allroles = ('ca','aa'); + my @allroles = &roles_by_context('author'); my @roles; if ($checkpriv) { foreach my $role (@allroles) { @@ -1082,7 +1081,7 @@ sub construction_space_roles { sub domain_roles { my ($checkpriv) = @_; - my @allroles = ('dc','li','dg','au','sc'); + my @allroles = &roles_by_context('domain'); my @roles; if ($checkpriv) { foreach my $role (@allroles) { @@ -1097,8 +1096,8 @@ sub domain_roles { } sub course_roles { - my ($context,$checkpriv) = @_; - my @allroles = ('st','ta','ep','in','cc'); + my ($context,$checkpriv,$custom) = @_; + my @allroles = &roles_by_context('course',$custom); my @roles; if ($context eq 'domain') { @roles = @allroles; @@ -1128,17 +1127,18 @@ sub course_roles { sub curr_role_permissions { my ($context,$setting,$checkpriv) = @_; + my $custom = 1; my @roles; if ($context eq 'author') { @roles = &construction_space_roles($checkpriv); } elsif ($context eq 'domain') { if ($setting eq 'course') { - @roles = &course_roles($context,$checkpriv); + @roles = &course_roles($context,$checkpriv,$custom); } else { @roles = &domain_roles($checkpriv); } } elsif ($context eq 'course') { - @roles = &course_roles($context,$checkpriv); + @roles = &course_roles($context,$checkpriv,$custom); } return @roles; } @@ -1287,8 +1287,7 @@ sub print_userlist { } } my $cid =$env{'request.course.id'}; - my $cdom=$env{'course.'.$cid.'.domain'}; - my $cnum=$env{'course.'.$cid.'.num'}; + my ($cnum,$cdom) = &get_course_identity($cid); my $showroles; if ($env{'form.showrole'} ne 'Any') { $showroles = [$env{'form.showrole'}]; @@ -1303,7 +1302,7 @@ sub print_userlist { my (%cstr_roles,%dom_roles); if ($context eq 'author') { # List co-authors and assistant co-authors - my @possroles = ('ca','aa'); + my @possroles = &roles_by_context($context); %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef, \@statuses,\@possroles); &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo, @@ -1328,7 +1327,7 @@ sub print_userlist { } else { my @possroles; if ($env{'form.showrole'} eq 'Any') { - @possroles = ('ca','aa'); + my @possroles = &roles_by_context($context); } else { @possroles = ($env{'form.showrole'}); } @@ -1351,14 +1350,13 @@ sub print_userlist { foreach my $cid (keys(%courses)) { my %coursehash = &Apache::lonnet::coursedescription($cid,{'one_time' => 1}); - my $cdom = $coursehash{'domain'}; - my $cnum = $coursehash{'num'}; + my ($cnum,$cdom,$cdesc) = &get_course_identity($cid); next if ($cnum eq '' || $cdom eq ''); - my $cdesc = $coursehash{'description'}; + my $custom = 1; my (@roles,@sections,%access,%users,%userdata, %statushash); if ($env{'form.showrole'} eq 'Any') { - @roles = &course_roles($context); + @roles = &course_roles($context,undef,$custom); unshift(@roles,'cr'); } else { @roles = ($env{'form.showrole'}); @@ -1760,20 +1758,22 @@ sub show_users_list { my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers); if ($context eq 'course') { $cid=$env{'request.course.id'}; - $cdom = $env{'course.'.$cid.'.domain'}; - $cnum = $env{'course.'.$cid.'.num'}; + ($cnum,$cdom) = &get_course_identity($cid); ($classgroups) = &Apache::loncoursedata::get_group_memberships( $userlist,$keylist,$cdom,$cnum); - if (! exists($env{'form.displayphotos'})) { - $env{'form.displayphotos'} = 'off'; - } - $displayphotos = $env{'form.displayphotos'}; - if (! exists($env{'form.displayclickers'})) { - $env{'form.displayclickers'} = 'off'; - } - $displayclickers = $env{'form.displayclickers'}; - if ($env{'course.'.$cid.'.internal.showphoto'}) { - $r->print(' + if ($mode eq 'autoenroll') { + $env{'form.showrole'} = 'st'; + } else { + if (! exists($env{'form.displayphotos'})) { + $env{'form.displayphotos'} = 'off'; + } + $displayphotos = $env{'form.displayphotos'}; + if (! exists($env{'form.displayclickers'})) { + $env{'form.displayclickers'} = 'off'; + } + $displayclickers = $env{'form.displayclickers'}; + if ($env{'course.'.$cid.'.internal.showphoto'}) { + $r->print(' - '); - } - $r->print(<print(< END + } } - unless ($mode eq 'autoenroll') { + if ($mode ne 'autoenroll') { my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript(); my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox"); my $singconfirm = &mt(' for a single user'); @@ -1919,49 +1920,52 @@ END } elsif ($env{'form.showrole'} ne 'Any') { $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'}); } - my $results_description = &results_header_row($rolefilter,$statusmode, - $context,$permission); - $r->print(''.$results_description.'
'); + my $results_description; + if ($mode ne 'autoenroll') { + $results_description = &results_header_row($rolefilter,$statusmode, + $context,$permission); + $r->print(''.$results_description.'
'); + } my ($output,$actionselect); - if ($mode eq 'html' || $mode eq 'view') { - if ($permission->{'cusr'}) { - $actionselect = &select_actions($context,$setting,$statusmode); - } - $r->print(<{'cusr'}) { + $actionselect = &select_actions($context,$setting,$statusmode); + } + $r->print(< END - if ($mode ne 'autoenroll') { - $output = '

'; - 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 .= '  '; - } - $output .= '
'; - } else { - $output .= &mt("Click on a username to view the user's personal page.").'
'; - } - if ($actionselect) { - $output .= <<"END"; + $output = '

'; + 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 .= '  '; + } + $output .= '
'; + } else { + $output .= &mt("Click on a username to view the user's personal page.").'
'; + } + if ($actionselect) { + $output .= <<"END"; $lt{'ac'}: $actionselect

  END - } + } } $output .= "\n

\n". &Apache::loncommon::start_data_table(). @@ -1980,7 +1984,7 @@ END $output .= "$lt{$item}\n"; } my %role_types = &role_type_names(); - if ($context eq 'course') { + if ($context eq 'course' && $mode ne 'autoenroll') { if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') { # Clicker display on or off? my %clicker_options = &Apache::lonlocal::texthash( @@ -2014,8 +2018,8 @@ END ' '."\n"; } } - $output .= &Apache::loncommon::end_data_table_header_row(); } + $output .= &Apache::loncommon::end_data_table_header_row(); # Done with the HTML header line } elsif ($mode eq 'csv') { # @@ -2151,32 +2155,54 @@ END } if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') { $r->print(&Apache::loncommon::start_data_table_row()); - $r->print("$rowcount\n"); my $checkval; - if ($mode ne 'autoenroll' && $actionselect) { - $checkval = $user; - if ($context eq 'course') { - if ($role eq 'st') { - $checkval .= ':st'; - } - $checkval .= ':'.$in{'section'}; - if ($role eq 'st') { - $checkval .= ':'.$in{'type'}.':'.$in{'lockedtype'}; + if ($mode eq 'autoenroll') { + my $cellentry; + if ($in{'type'} eq 'auto') { + $cellentry = ''.&mt('auto').' '; + $autocount ++; + } else { + $cellentry = '
'.&mt('manual').'
'; + $manualcount ++; + if ($in{'lockedtype'}) { + $cellentry .= ''; + $unlockcount ++; + } else { + $cellentry .= ''; + $lockcount ++; } + $cellentry .= '
'; + } + $r->print("$cellentry\n"); + } else { + $r->print("$rowcount\n"); + $checkval; + if ($actionselect) { + $checkval = $user; + if ($context eq 'course') { + if ($role eq 'st') { + $checkval .= ':st'; + } + $checkval .= ':'.$in{'section'}; + if ($role eq 'st') { + $checkval .= ':'.$in{'type'}.':'.$in{'lockedtype'}; + } + } + $r->print(''); } - $r->print(''); } foreach my $item (@cols) { if ($item eq 'username') { - $r->print(''.&print_username_link($permission,\%in).''); - } elsif (($item eq 'start' || $item eq 'end') && ($mode ne 'autoeroll') && ($actionselect)) { + $r->print(''.&print_username_link($mode,$permission, + \%in).''); + } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) { $r->print(''.$in{$item}.''."\n"); } else { $r->print(''.$in{$item}.''."\n"); } } - if ($context eq 'course') { + if (($context eq 'course') && ($mode ne 'autoenroll')) { if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') { if ($displayclickers eq 'on') { my $clickers = @@ -2256,9 +2282,11 @@ END } sub print_username_link { - my ($permission,$in) = @_; + my ($mode,$permission,$in) = @_; my $output; - if (!$permission->{'cusr'}) { + if ($mode eq 'autoenroll') { + $output = $in->{'username'}; + } elsif (!$permission->{'cusr'}) { $output = &Apache::loncommon::aboutmewrapper($in->{'username'}, $in->{'username'}, $in->{'domain'}); @@ -2395,7 +2423,7 @@ ENDTWO } sub date_section_selector { - my ($context) = @_; + my ($context,$permission) = @_; my $callingform = $env{'form.callingform'}; my $formname = 'dateselect'; my $groupslist = &get_groupslist(); @@ -2474,14 +2502,14 @@ END $starttime = time; } $date_items = &date_setting_table($starttime,undef,$context, - $env{'form.bulkaction'}); + $env{'form.bulkaction'},$formname, + $permission); } $output .= '

'.$headertext.'

'. '
'."\n". $date_items; if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') { - my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; - my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; + my ($cnum,$cdom) = &get_course_identity(); my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum); my $info; @@ -2509,17 +2537,8 @@ END $info = ''; } my $sections_select .= &course_sections(\%sections_count,$env{'form.showrole'}); - my $secbox = '

'.&Apache::lonhtmlcommon::start_pick_box()."\n". - &Apache::lonhtmlcommon::row_title(&mt('New section to assign'),'LC_oddrow_value')."\n". - ''."\n". - '
'.&mt('Existing sections')."\n". - '
'.$sections_select.'
'. - &mt('New section').'
'."\n". - ''."\n". - ''."\n". - '
'."\n". - &Apache::lonhtmlcommon::row_closure(1)."\n". - &Apache::lonhtmlcommon::end_pick_box().'

'; + my $rowtitle = &mt('New section to assign'); + my $secbox = §ion_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context); $output .= $info.$secbox; } $output .= '

'. @@ -2529,6 +2548,36 @@ END return $output; } +sub section_picker { + my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode) = @_; + my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum); + my $sections_select .= &course_sections(\%sections_count,$role); + my $secbox = '

'.&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 .= ''."\n". + '
'.&mt('Existing sections')."\n". + '
'.$sections_select.'
'. + &mt('New section').'
'."\n". + ''."\n". + ''."\n". + '
'."\n"; + } else { + $secbox .= ''. + $env{'request.course.sec'}; + } + $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n". + &Apache::lonhtmlcommon::end_pick_box().'

'; + return $secbox; +} + sub results_header_row { my ($rolefilter,$statusmode,$context,$permission) = @_; my ($description,$showfilter); @@ -2640,8 +2689,9 @@ sub results_header_row { ################################################# ################################################# sub show_drop_list { - my ($r,$classlist,$keylist,$nosort)=@_; + my ($r,$classlist,$keylist,$nosort,$permission)=@_; my $cid=$env{'request.course.id'}; + my ($cnum,$cdom) = &get_course_identity($cid); if (! exists($env{'form.sortby'})) { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['sortby']); @@ -2650,26 +2700,17 @@ sub show_drop_list { if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) { $sortby = 'username'; } - my $cdom = $env{'course.'.$cid.'.domain'}; - my $cnum = $env{'course.'.$cid,'.num'}; my ($classgroups) = &Apache::loncoursedata::get_group_memberships( $classlist,$keylist,$cdom,$cnum); # my $action = "drop"; + my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript(); $r->print(< -

@@ -2684,9 +2725,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(<   $lt{'usrn'} $lt{'dom'} @@ -2696,32 +2737,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 @@ -2768,6 +2809,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"); @@ -2827,7 +2875,7 @@ sub print_first_users_upload_form { # ================================================= Drop/Add from uploaded file sub upfile_drop_add { - my ($r,$context) = @_; + my ($r,$context,$permission) = @_; &Apache::loncommon::load_tmp_file($r); my @userdata=&Apache::loncommon::upfile_record_sep(); if($env{'form.noFirstLine'}){shift(@userdata);} @@ -3278,9 +3326,10 @@ sub user_change_result { } # ========================================================= Menu Phase Two Drop -sub print_expire_menu { - my ($r,$context) = @_; - $r->print("

".&mt("Expire Users' Roles")."

"); +sub print_drop_menu { + my ($r,$context,$permission) = @_; + $r->print('

'.&mt("Drop Students").'

'."\n". + ''."\n"); my $cid=$env{'request.course.id'}; my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist(); if (! defined($classlist)) { @@ -3288,11 +3337,11 @@ sub print_expire_menu { return; } # Print out the available choices - &show_drop_list($r,$classlist,$keylist); + &show_drop_list($r,$classlist,$keylist,$permission); + $r->print(''. &Apache::loncommon::end_page()); return; } - # ================================================================== Phase four sub update_user_list { @@ -3308,12 +3357,18 @@ sub update_user_list { my %result_text = ( ok => { 'revoke' => 'Revoked', 'delete' => 'Deleted', 'reenable' => 'Re-enabled', - 'activate' => 'Activated', + 'activate' => 'Activated', + 'chgdates' => 'Changed Access Dates for', + 'chgsec' => 'Changed section for', + 'drop' => 'Dropped', }, error => {'revoke' => 'revoking', 'delete' => 'deleting', 'reenable' => 're-enabling', 'activate' => 'activating', + 'chgdates' => 'changing access dates for', + 'chgsec' => 'changing section for', + 'drop' => 'dropping', }, ); my ($startdate,$enddate); @@ -3323,7 +3378,18 @@ sub update_user_list { foreach my $item (@changelist) { my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections, $scopestem); - if ($context eq 'course') { + if ($choice eq 'drop') { + ($uname,$udom,$sec) = split(/:/,$item,-1); + $role = 'st'; + $cid = $env{'request.course.id'}; + $scopestem = '/'.$cid; + $scopestem =~s/\_/\//g; + if ($sec eq '') { + $scope = $scopestem; + } else { + $scope = $scopestem.'/'.$sec; + } + } elsif ($context eq 'course') { ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1); $cid = $env{'request.course.id'}; $scopestem = '/'.$cid; @@ -3356,8 +3422,14 @@ sub update_user_list { my ($uid,$first,$middle,$last,$gene,$sec); my $start = $env{'form.'.$item.'_start'}; my $end = $env{'form.'.$item.'_end'}; - # revoke or delete user role - if ($choice eq 'revoke') { + if ($choice eq 'drop') { + # drop students + $end = $now; + $type = 'manual'; + $result = + &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid); + } elsif ($choice eq 'revoke') { + # revoke or delete user role $end = $now; if ($role eq 'st') { $result = @@ -3459,19 +3531,26 @@ sub update_user_list { } } } + my $extent = $scope; + if ($choice eq 'drop' || $context eq 'course') { + my ($cnum,$cdom,$cdesc) = &get_course_identity($cid); + if ($cdesc) { + $extent = $cdesc; + } + } if ($result eq 'ok' || $result eq 'ok:') { $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]", - $plrole,$scope,$uname.':'.$udom).'
'); + $plrole,$extent,$uname.':'.$udom).'
'); $count++; } else { $r->print( &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]:[_4]", - $plrole,$scope,$uname.':'.$udom,$result).'
'); + $plrole,$extent,$uname.':'.$udom,$result).'
'); } } $r->print('

'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,users].",$count).'

'); if ($count > 0) { - if ($choice eq 'revoke') { + 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 @@ -3723,5 +3802,329 @@ sub can_create_user { return $cancreate; } +sub can_modify_userinfo { + my ($context,$dom,$fields,$userroles) = @_; + my %domconfig = + &Apache::lonnet::get_dom('configuration',['usermodification'], + $dom); + my %canmodify; + if (ref($fields) eq 'ARRAY') { + foreach my $field (@{$fields}) { + $canmodify{$field} = 0; + if (&Apache::lonnet::allowed('mau',$dom)) { + $canmodify{$field} = 1; + } else { + if (ref($domconfig{'usermodification'}) eq 'HASH') { + if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') { + if (ref($userroles) eq 'ARRAY') { + foreach my $role (@{$userroles}) { + my $testrole; + if ($role =~ /^cr\//) { + $testrole = 'cr'; + } else { + $testrole = $role; + } + if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') { + if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) { + $canmodify{$field} = 1; + last; + } + } + } + } else { + foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) { + if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') { + if ($domconfig{'usermodification'}{$context}{$key}{$field}) { + $canmodify{$field} = 1; + last; + } + } + } + } + } + } elsif ($context eq 'course') { + if (ref($userroles) eq 'ARRAY') { + if (grep(/^st$/,@{$userroles})) { + $canmodify{$field} = 1; + } + } else { + $canmodify{$field} = 1; + } + } + } + } + } + return %canmodify; +} + +sub check_usertype { + my ($dom,$uname,$rules) = @_; + my $usertype; + if (ref($rules) eq 'HASH') { + my @user_rules = keys(%{$rules}); + if (@user_rules > 0) { + my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules); + if (keys(%rule_check) > 0) { + $usertype = 'unofficial'; + foreach my $item (keys(%rule_check)) { + if ($rule_check{$item}) { + $usertype = 'official'; + last; + } + } + } + } + } + return $usertype; +} + +sub roles_by_context { + my ($context,$custom) = @_; + my @allroles; + if ($context eq 'course') { + @allroles = ('st','ad','ta','ep','in','cc'); + if ($custom) { + push(@allroles,'cr'); + } + } elsif ($context eq 'author') { + @allroles = ('ca','aa'); + } elsif ($context eq 'domain') { + @allroles = ('li','dg','sc','au','dc'); + } + return @allroles; +} + +sub get_permission { + my ($context,$roles) = @_; + my %permission; + if ($context eq 'course') { + my $custom = 1; + my @allroles = &roles_by_context($context,$custom); + foreach my $role (@allroles) { + if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) { + $permission{'cusr'} = 1; + last; + } + } + if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) { + $permission{'custom'} = 1; + } + if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) { + $permission{'view'} = 1; + } + if (!$permission{'view'}) { + my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'}; + $permission{'view'} = &Apache::lonnet::allowed('vcl',$scope); + if ($permission{'view'}) { + $permission{'view_section'} = $env{'request.course.sec'}; + } + } + if (!$permission{'cusr'}) { + if ($env{'request.course.sec'} ne '') { + my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'}; + $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope)); + if ($permission{'cusr'}) { + $permission{'cusr_section'} = $env{'request.course.sec'}; + } + } + } + if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) { + $permission{'grp_manage'} = 1; + } + } elsif ($context eq 'author') { + $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'}); + $permission{'view'} = $permission{'cusr'}; + } else { + my @allroles = &roles_by_context($context); + foreach my $role (@allroles) { + if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) { $permission{'cusr'} = 1; + last; + } + } + if (!$permission{'cusr'}) { + if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) { + $permission{'cusr'} = 1; + } + } + if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) { + $permission{'custom'} = 1; + } + $permission{'view'} = $permission{'cusr'}; + } + my $allowed = 0; + foreach my $perm (values(%permission)) { + if ($perm) { $allowed=1; last; } + } + return (\%permission,$allowed); +} + +# ==================================================== Figure out author access + +sub authorpriv { + my ($auname,$audom)=@_; + unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname)) + || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; } return 1; +} + +sub get_course_identity { + my ($cid) = @_; + my ($cnum,$cdom,$cdesc); + if ($cid eq '') { + $cid = $env{'request.course.id'} + } + if ($cid ne '') { + $cnum = $env{'course.'.$cid.'.num'}; + $cdom = $env{'course.'.$cid.'.domain'}; + $cdesc = $env{'course.'.$cid.'.description'}; + if ($cnum eq '' || $cdom eq '') { + my %coursehash = + &Apache::lonnet::coursedescription($cid,{'one_time' => 1}); + $cdom = $coursehash{'domain'}; + $cnum = $coursehash{'num'}; + $cdesc = $coursehash{'description'}; + } + } + return ($cnum,$cdom,$cdesc); +} + +sub dc_setcourse_js { + my ($formname,$mode) = @_; + my $dc_setcourse_code; + my $cctext = &Apache::lonnet::plaintext('cc'); + my %alerts = §ioncheck_alerts(); + my $role = 'role'; + if ($mode eq 'upload') { + $role = 'courserole'; + } + $dc_setcourse_code = (<<"SCRIPTTOP"); +function setCourse() { + var course = document.$formname.dccourse.value; + if (course != "") { + if (document.$formname.dcdomain.value != document.$formname.origdom.value) { + alert("$alerts{'curd'}"); + return; + } + var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value + var section=""; + var numsections = 0; + var newsecs = new Array(); + for (var i=0; i 1)) { + alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.") + return; + } + for (var j=0; j 0)) { + alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}."); + section = ""; + } +SCRIPTTOP + if ($mode ne 'upload') { + $dc_setcourse_code .= (<<"ENDSCRIPT"); + var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole + var numcourse = getIndex(document.$formname.dccourse); + if (numcourse == "-1") { + alert("$alerts{'thwa'}"); + return; + } + else { + document.$formname.elements[numcourse].name = "act"+coursename; + var numnewsec = getIndex(document.$formname.newsec); + if (numnewsec != "-1") { + document.$formname.elements[numnewsec].name = "sec"+coursename; + document.$formname.elements[numnewsec].value = section; + } + var numstart = getIndex(document.$formname.start); + if (numstart != "-1") { + document.$formname.elements[numstart].name = "start"+coursename; + } + var numend = getIndex(document.$formname.end); + if (numend != "-1") { + document.$formname.elements[numend].name = "end"+coursename + } + } + } + document.$formname.submit(); +} + +ENDSCRIPT + } else { + $dc_setcourse_code .= " + document.$formname.sections.value = section; + } + return 'ok'; +} +"; + } + $dc_setcourse_code .= (<<"ENDSCRIPT"); + + function getIndex(caller) { + for (var i=0;i 'You must select a course in the current domain', + inea => 'In each course, each user may only have one student role at a time', + youh => 'You had selected', + sect => 'sections', + plsm => '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', + plsc => 'Please choose a different section name', + mayt => 'may not be used as the name for a section, as it is the name of a course group', + secn => 'Section names and group names must be distinct', + secd => 'Section designations do not apply to ', + role => 'roles', + accr => 'role will be added with access to all sections', + thwa => 'There was a problem with your course selection' + ); + return %alerts; +} + + 1;