--- loncom/interface/lonuserutils.pm 2010/01/14 18:06:14 1.111
+++ loncom/interface/lonuserutils.pm 2012/02/08 19:35:20 1.136.6.2
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Utility functions for managing LON-CAPA user accounts
#
-# $Id: lonuserutils.pm,v 1.111 2010/01/14 18:06:14 bisitz Exp $
+# $Id: lonuserutils.pm,v 1.136.6.2 2012/02/08 19:35:20 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -47,7 +47,8 @@ sub modifystudent {
# this one. If $csec is defined, drop them from all other sections of
# this course and add them to section $csec
my ($cnum,$cdom) = &get_course_identity($courseid);
- my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
+ my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
+ my %roles = &Apache::lonnet::dump('roles',$udom,$unam,'.',undef,$extra);
my ($tmp) = keys(%roles);
# Bail out if we were unable to get the students roles
return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
@@ -295,7 +296,8 @@ sub hidden_input {
}
sub print_upload_manager_header {
- my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype)=@_;
+ my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype,
+ $can_assign)=@_;
my $javascript;
#
if (! exists($env{'form.upfile_associate'})) {
@@ -309,9 +311,9 @@ sub print_upload_manager_header {
}
}
if ($env{'form.upfile_associate'} eq 'reverse') {
- $javascript=&upload_manager_javascript_reverse_associate();
+ $javascript=&upload_manager_javascript_reverse_associate($can_assign);
} else {
- $javascript=&upload_manager_javascript_forward_associate();
+ $javascript=&upload_manager_javascript_forward_associate($can_assign);
}
#
# Deal with restored settings
@@ -340,7 +342,8 @@ sub print_upload_manager_header {
."
\n");
$r->print('
'.
&mt('Identify fields in uploaded list')."
\n");
- $r->print(&mt('Enter as many fields as you can. The system will inform you and bring you back to this page, if the data selected are insufficient to add users.')." \n");
+ $r->print(&mt('Enter as many fields as you can.').' '.
+ &mt('The system will inform you and bring you back to this page, [_1]if the data selected are insufficient to add users.',' ')." \n");
$r->print(&hidden_input('action','upload').
&hidden_input('state','got_file').
&hidden_input('associate','').
@@ -532,6 +535,7 @@ END
if (message!='') {
message+='\\n';
}
+ message+='$alert{'section'}';
}
if (foundemail==0) {
if (message!='') {
@@ -585,6 +589,46 @@ END
###############################################################
###############################################################
sub upload_manager_javascript_forward_associate {
+ my ($can_assign) = @_;
+ my ($auth_update,$numbuttons,$argreset);
+ if (ref($can_assign) eq 'HASH') {
+ if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
+ $argreset .= " vf.krbarg.value='';\n";
+ $numbuttons ++ ;
+ }
+ if ($can_assign->{'int'}) {
+ $argreset .= " vf.intarg.value='';\n";
+ $numbuttons ++;
+ }
+ if ($can_assign->{'loc'}) {
+ $argreset .= " vf.locarg.value='';\n";
+ $numbuttons ++;
+ }
+ if (!$can_assign->{'int'}) {
+ my $warning = &mt('You may not specify an initial password for each user, as this is only available when new users use LON-CAPA internal authentication.\n').
+ &mt('Your current role does not have rights to create users with that authentication type.');
+ $auth_update = <<"END";
+ // Currently the initial password field is only supported for internal auth
+ // (see bug 6368).
+ if (nw==9) {
+ eval('vf.f'+tf+'.selectedIndex=0;')
+ alert('$warning');
+ }
+END
+ } elsif ($numbuttons > 1) {
+ $auth_update = <<"END";
+ // If we set the password, make the password form below correspond to
+ // the new value.
+ if (nw==9) {
+ changed_radio('int',document.studentform);
+ set_auth_radio_buttons('int',document.studentform);
+$argreset
+ }
+
+END
+ }
+ }
+
return(<{'krb4'} || $can_assign->{'krb5'}) {
+ $argreset .= " vf.krbarg.value='';\n";
+ $numbuttons ++ ;
+ }
+ if ($can_assign->{'int'}) {
+ $argreset .= " vf.intarg.value='';\n";
+ $numbuttons ++;
+ }
+ if ($can_assign->{'loc'}) {
+ $argreset .= " vf.locarg.value='';\n";
+ $numbuttons ++;
+ }
+ if (!$can_assign->{'int'}) {
+ my $warning = &mt('You may not specify an initial password, as this is only available when new users use LON-CAPA internal authentication.\n').
+ &mt('Your current role does not have rights to create users with that authentication type.');
+ $auth_update = <<"END";
+ // Currently the initial password field is only supported for internal auth
+ // (see bug 6368).
+ if (tf==8 && nw!=0) {
+ eval('vf.f'+tf+'.selectedIndex=0;')
+ alert('$warning');
+ }
+END
+ } elsif ($numbuttons > 1) {
+ $auth_update = <<"END";
+ // initial password specified, pick internal authentication
+ if (tf==8 && nw!=0) {
+ changed_radio('int',document.studentform);
+ set_auth_radio_buttons('int',document.studentform);
+$argreset
+ }
+
+END
+ }
+ }
+
return(<=2) && (tf<=5) && (nw!=0)) {
eval('vf.f1.selectedIndex=0;')
}
- // intial password specified, pick internal authentication
- if (tf==8 && nw!=0) {
- changed_radio('int',document.studentform);
- set_auth_radio_buttons('int',document.studentform);
- vf.krbarg.value='';
- vf.intarg.value='';
- vf.locarg.value='';
- }
+ $auth_update
}
function clearpwd(vf) {
@@ -978,8 +1048,9 @@ sub print_upload_manager_form {
my ($krbdef,$krbdefdom) =
&Apache::loncommon::get_kerberos_defaults($defdom);
#
+ my ($authnum,%can_assign) = &Apache::loncommon::get_assignable_auth($defdom);
&print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
- $permission,$crstype);
+ $permission,$crstype,\%can_assign);
my $i;
my $keyfields;
if ($total>=0) {
@@ -1472,9 +1543,11 @@ sub print_userlist {
$title = &mt('Select Communities');
$warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
}
+ my @standardnames = &Apache::loncommon::get_standard_codeitems();
my $courseform =
&Apache::lonhtmlcommon::course_selection($formname,$numcodes,
- $codetitles,$idlist,$idlist_titles,$crstype);
+ $codetitles,$idlist,$idlist_titles,$crstype,
+ \@standardnames);
$r->print('
'.&Apache::lonhtmlcommon::start_pick_box()."\n".
&Apache::lonhtmlcommon::start_pick_box()."\n".
&Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
@@ -1732,7 +1805,7 @@ sub section_group_filter {
}
if (@options > 0) {
my $currsel;
- $markup = ''."\n";
+ $markup = '
\n");
if ($counts{'role'} > 0) {
@@ -4393,10 +4551,9 @@ sub print_drop_menu {
# ================================================================== 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;
if ($context eq 'course') {
$crstype = &Apache::loncommon::course_type();
}
@@ -4411,7 +4568,7 @@ sub update_user_list {
'reenable' => 'Re-enabled',
'activate' => 'Activated',
'chgdates' => 'Changed Access Dates for',
- 'chgsec' => 'Changed section for',
+ 'chgsec' => 'Changed section(s) for',
'drop' => 'Dropped',
},
error => {'revoke' => 'revoking',
@@ -4428,8 +4585,9 @@ sub update_user_list {
($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';
@@ -4528,14 +4686,23 @@ sub update_user_list {
} 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,
@@ -4562,13 +4729,23 @@ sub update_user_list {
} else {
if ($sec eq '') {
$nochg = 1;
- } else {
+ $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);
@@ -4577,22 +4754,58 @@ sub update_user_list {
$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) {
+ 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.',''.$showsingle.'');
+ } 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.',''.$showsingle.'');
+ }
+ $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);
+
}
}
}
}
+ unless ($role eq 'st') {
+ unless ($showsecs) {
+ my @tolist = sort(@shownew,@retained);
+ if ($keepnosection) {
+ push(@tolist,&mt('No section'));
+ }
+ $showsecs = join(', ',@tolist);
+ }
+ }
}
}
my $extent = $scope;
@@ -4603,13 +4816,48 @@ sub update_user_list {
}
}
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).' ');
- $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,''.
+ &Apache::loncommon::plainname($uname,$udom).
+ '').' ');
+ if ($sec eq '') {
+ $r->print(&mt('[_1]No section[_2] - [_3]','','',$dates));
+ } else {
+ $r->print(&mt('Section(s): [_1] - [_2]',
+ ''.$showsecs.'',$dates));
+ }
+ $r->print(' ');
+ } else {
+ $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
+ ''.&Apache::loncommon::plainname($uname,$udom).'',
+ ''.$showsecs.'',$dates).' ');
+ $count ++;
+ }
+ if ($warn_singlesec) {
+ $r->print('
'.$warn_singlesec.'
');
+ }
+ } elsif ($choice eq 'chgdates') {
+ $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent,
+ ''.&Apache::loncommon::plainname($uname,$udom).'',
+ $dates).' ');
+ $count ++;
+ } else {
+ $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
+ ''.&Apache::loncommon::plainname($uname,$udom).'').
+ ' ');
+ $count ++;
+ }
} else {
$r->print(
- &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]: [_4].",
- $plrole,$extent,$uname.':'.$udom,$result).' ');
+ &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
+ $plrole,$extent,
+ ''.&Apache::loncommon::plainname($uname,$udom).'',
+ $result).' ');
}
}
$r->print(''."\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)});
@@ -4825,7 +5094,7 @@ sub setsections_javascript {
accr => 'A course coordinator role will be added with access to all sections.',
acor => 'A coordinator role will be added with access to all sections',
inea => 'In each course, each user may only have one student role at a time.',
- inec => 'In each community, each user may only have on member role at a time.',
+ inco => 'In each community, each user may only have one member role at a time.',
youh => 'You had selected ',
secs => 'sections.',
plmo => 'Please modify your selections so they include no more than one section.',
@@ -4833,16 +5102,21 @@ sub setsections_javascript {
plch => 'Please choose a different section name.',
mnot => 'may not be used as a section name, as it is the name of a course group.',
secn => 'Section names and group names must be distinct. Please choose a different section name.',
+ nonw => 'Section names may only contain letters or numbers.',
);
$setsection_js .= <<"ENDSECCODE";
function setSections(formname,crstype) {
var re1 = /^currsec_/;
+ var re2 =/\\W/;
+ var trimleading = /^\\s+/;
+ var trimtrailing = /\\s+\$/;
var groups = new Array($groupslist);
for (var i=0;i 0) {
- if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
- sections = sections + "," + formname.elements[i+1].value;
- }
- }
- else {
- sections = formname.elements[i+1].value;
- }
- var newsecs = formname.elements[i+1].value;
- var numsplit;
+ var newsecs = formname.elements[num+1].value;
+ var validsecs = new Array();
+ var validsecstr = '';
+ var badsecs = new Array();
if (newsecs != null && newsecs != "") {
- numsplit = newsecs.split(/,/g);
- numsec = numsec + numsplit.length;
+ var numsplit;
+ if (newsecs.indexOf(',') == -1) {
+ numsplit = new Array(newsecs);
+ } else {
+ numsplit = newsecs.split(/,/g);
+ }
+ for (var m=0; m 0) {
+ alert("$alerts{'nonw'}\\n$alerts{'plch'}");
+ return;
+ }
+ numsec = numsec + validsecs.length;
}
-
if ((role == 'st') && (numsec > 1)) {
if (crstype == 'Community') {
alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
@@ -4893,25 +5189,36 @@ function setSections(formname,crstype) {
alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
}
return;
- }
- else {
- if (numsplit != null) {
- for (var j=0; j 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;
+ if ((ref($got_rules) eq 'HASH') && (ref($curr_rules) eq 'HASH')) {
+ if (!$got_rules->{$dom}) {
+ my %domconfig = &Apache::lonnet::get_dom('configuration',
+ ['usercreation'],$dom);
+ if (ref($domconfig{'usercreation'}) eq 'HASH') {
+ foreach my $item ('username','id') {
+ if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
+ $curr_rules->{$dom}{$item} =
+ $domconfig{'usercreation'}{$item.'_rule'};
+ }
+ }
+ }
+ $got_rules->{$dom} = 1;
+ }
+ if (ref($rules) eq 'HASH') {
+ my @user_rules;
+ if (ref($curr_rules->{$dom}{'username'}) eq 'ARRAY') {
+ foreach my $rule (keys(%{$rules})) {
+ if (grep(/^\Q$rule\E/,@{$curr_rules->{$dom}{'username'}})) {
+ push(@user_rules,$rule);
+ }
+ }
+ }
+ 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;
+ }
}
}
}