--- loncom/interface/loncreateuser.pm 2005/06/19 00:41:32 1.105 +++ loncom/interface/loncreateuser.pm 2006/05/09 14:38:10 1.114 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Create a user # -# $Id: loncreateuser.pm,v 1.105 2005/06/19 00:41:32 www Exp $ +# $Id: loncreateuser.pm,v 1.114 2006/05/09 14:38:10 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -116,13 +116,15 @@ sub authorpriv { # =================================================================== Phase one sub print_username_entry_form { - my $r=shift; + my ($r) = @_; my $defdom=$env{'request.role.domain'}; my @domains = &Apache::loncommon::get_domains(); my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain'); - my $html=&Apache::lonxml::xmlbegin(); - my $bodytag =&Apache::loncommon::bodytag('Create Users, Change User Privileges').&Apache::loncommon::help_open_menu('',undef,undef,'',282,'Instructor Interface'); my $selscript=&Apache::loncommon::studentbrowser_javascript(); + my $start_page = + &Apache::loncommon::start_page('Create Users, Change User Privileges', + $selscript); + my $sellink=&Apache::loncommon::selectstudent_link ('crtuser','ccuname','ccdomain'); my %existingroles=&my_custom_roles(); @@ -137,15 +139,11 @@ sub print_username_entry_form { 'nr' => "Name of Role", 'cre' => "Custom Role Editor" ); + my $help = &Apache::loncommon::help_open_menu('',undef,undef,'',282,'Instructor Interface'); my $helpsiur=&Apache::loncommon::help_open_topic('Course_Change_Privileges'); my $helpecpr=&Apache::loncommon::help_open_topic('Course_Editing_Custom_Roles'); $r->print(<<"ENDDOCUMENT"); -$html -<head> -<title>The LearningOnline Network with CAPA</title> -$selscript -</head> -$bodytag +$start_page <form action="/adm/createuser" method="post" name="crtuser"> <input type="hidden" name="phase" value="get_user_info"> <h2>$lt{siur}$helpsiur</h2> @@ -156,14 +154,120 @@ $lt{'dom'}:</td><td>$domform</td></tr> </table> <input name="userrole" type="submit" value="$lt{usrr}" /> </form> +ENDDOCUMENT + if (&Apache::lonnet::allowed('mcr','/')) { + $r->print(<<ENDCUSTOM); <form action="/adm/createuser" method="post" name="docustom"> <input type="hidden" name="phase" value="selected_custom_edit"> <h2>$lt{'ecrp'}$helpecpr</h2> $lt{'nr'}: $choice <input type="text" size="15" name="newrolename" /><br /> <input name="customeditor" type="submit" value="$lt{'cre'}" /> -</body> -</html> -ENDDOCUMENT +</form> +ENDCUSTOM + } + $r->print(&Apache::loncommon::end_page()); +} + + +sub user_modification_js { + my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_; + + return <<END; +<script type="text/javascript" language="Javascript"> + + function pclose() { + parmwin=window.open("/adm/rat/empty.html","LONCAPAparms", + "height=350,width=350,scrollbars=no,menubar=no"); + parmwin.close(); + } + + $pjump_def + $dc_setcourse_code + + function dateset() { + eval("document.cu."+document.cu.pres_marker.value+ + ".value=document.cu.pres_value.value"); + pclose(); + } + + $nondc_setsection_code + + function setSections() { + var re1 = /^currsec_/; + var groups = new Array($groupslist); + for (var i=0;i<document.cu.elements.length;i++) { + var str = document.cu.elements[i].name; + var checkcurr = str.match(re1); + if (checkcurr != null) { + var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/; + if (document.cu.elements[i-1].checked == true) { + var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/; + match = re2.exec(str); + var role = match[1]; + if (role == 'cc') { + alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections."); + } + else { + var sections = ''; + var numsec = 0; + var sections; + for (var j=0; j<document.cu.elements[i].length; j++) { + if (document.cu.elements[i].options[j].selected == true ) { + if (document.cu.elements[i].options[j].value != "") { + if (numsec == 0) { + if (document.cu.elements[i].options[j].value != "") { + sections = document.cu.elements[i].options[j].value; + numsec ++; + } + } + else { + sections = sections + "," + document.cu.elements[i].options[j].value + numsec ++; + } + } + } + } + if (numsec > 0) { + if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) { + sections = sections + "," + document.cu.elements[i+1].value; + } + } + else { + sections = document.cu.elements[i+1].value; + } + var newsecs = document.cu.elements[i+1].value; + if (newsecs != null && newsecs != "") { + var numsplit = newsecs.split(/,/g); + numsec = numsec + numsplit.length; + } + if ((role == 'st') && (numsec > 1)) { + alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.") + return; + } + else { + for (var j=0; j<numsplit.length; j++) { + if ((numsplit[j] == 'all') || + (numsplit[j] == 'none')) { + alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name."); + return; + } + for (var k=0; k<groups.length; k++) { + if (numsplit[j] == groups[k]) { + alert("'"+numsplit[j]+"' may not be used as a section name, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name."); + return; + } + } + } + document.cu.elements[i+2].value = sections; + } + } + } + } + } + document.cu.submit(); + } +</script> +END } # =================================================================== Phase two @@ -196,10 +300,19 @@ sub print_user_modification_page { $ccdomain=~s/\W//g; my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition(); my $dc_setcourse_code = ''; - my $loaditem; + my $nondc_setsection_code = ''; + my %loaditem; + + my $groupslist; + my %curr_groups = &Apache::loncommon::coursegroups(); + if (%curr_groups) { + $groupslist = join('","',sort(keys(%curr_groups))); + $groupslist = '"'.$groupslist.'"'; + } + if ($env{'request.role'} =~ m-^dc\./(\w+)/$-) { my $dcdom = $1; - $loaditem = qq|OnLoad="document.cu.coursedesc.value=''"|; + $loaditem{'onload'} = "document.cu.coursedesc.value=''"; $dc_setcourse_code = <<"ENDSCRIPT"; function setCourse() { var course = document.cu.dccourse.value; @@ -239,6 +352,21 @@ sub print_user_modification_page { alert("In each course, each user may only have one student role at a time. You had selected "+numsections+" sections.\\nPlease modify your selections so they include no more than one section.") return; } + for (var j=0; j<numsplit.length; j++) { + if ((numsplit[j] == 'all') || (numsplit[j] == 'none')) { + alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name."); + return; + } + if (document.cu.groups.value != '') { + var groups = document.cu.groups.value.split(/,/g); + for (var k=0; k<groups.length; k++) { + if (numsplit[j] == groups[k]) { + alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name."); + return; + } + } + } + } if ((userrole == 'cc') && (numsections > 0)) { alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections."); section = ""; @@ -269,31 +397,11 @@ sub print_user_modification_page { return -1; } ENDSCRIPT - } - my $html=&Apache::lonxml::xmlbegin(); - my $dochead =<<"ENDDOCHEAD"; -$html -<head> -<title>The LearningOnline Network with CAPA</title> -<script type="text/javascript" language="Javascript"> - - function pclose() { - parmwin=window.open("/adm/rat/empty.html","LONCAPAparms", - "height=350,width=350,scrollbars=no,menubar=no"); - parmwin.close(); - } - - $pjump_def - $dc_setcourse_code - - function dateset() { - eval("document.cu."+document.cu.pres_marker.value+ - ".value=document.cu.pres_value.value"); - pclose(); - } - + } else { + $nondc_setsection_code = <<"ENDSECCODE"; function setSections() { var re1 = /^currsec_/; + var groups = new Array($groupslist); for (var i=0;i<document.cu.elements.length;i++) { var str = document.cu.elements[i].name; var checkcurr = str.match(re1); @@ -329,10 +437,10 @@ $html if (numsec > 0) { if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) { sections = sections + "," + document.cu.elements[i+1].value; - } + } } else { - sections = document.cu.elements[i+1].value; + sections = document.cu.elements[i+1].value; } var newsecs = document.cu.elements[i+1].value; if (newsecs != null && newsecs != "") { @@ -340,10 +448,23 @@ $html numsec = numsec + numsplit.length; } if ((role == 'st') && (numsec > 1)) { - alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.") + alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.") return; } - else { + else { + for (var j=0; j<numsplit.length; j++) { + if ((numsplit[j] == 'all') || + (numsplit[j] == 'none')) { + alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name."); + return; + } + for (var k=0; k<groups.length; k++) { + if (numsplit[j] == groups[k]) { + alert("'"+numsplit[j]+"' may not be used as a section name, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name."); + return; + } + } + } document.cu.elements[i+2].value = sections; } } @@ -352,11 +473,14 @@ $html } document.cu.submit(); } -</script> -</head> -ENDDOCHEAD - $r->print(&Apache::loncommon::bodytag( - 'Create Users, Change User Privileges',undef,$loaditem)); +ENDSECCODE + } + my $js = &user_modification_js($pjump_def,$dc_setcourse_code, + $nondc_setsection_code,$groupslist); + my $start_page = + &Apache::loncommon::start_page('Create Users, Change User Privileges', + $js,{'add_entries' => \%loaditem,}); + my $forminfo =<<"ENDFORMINFO"; <form action="/adm/createuser" method="post" name="cu"> <input type="hidden" name="phase" value="update_user_data"> @@ -398,7 +522,7 @@ ENDFORMINFO my $genhelp=&Apache::loncommon::help_open_topic('Generation'); &initialize_authen_forms(); $r->print(<<ENDNEWUSER); -$dochead +$start_page <h1>$lt{'cnu'}</h1> $forminfo <h2>$lt{'nu'} "$ccuname" $lt{'id'} $ccdomain</h2> @@ -439,7 +563,7 @@ ENDNEWUSER 'gen' => "generation" ); $r->print(<<ENDCHANGEUSER); -$dochead +$start_page <h1>$lt{'cup'}</h1> $forminfo <h2>$lt{'usr'} "$ccuname" $lt{'id'} "$ccdomain"</h2> @@ -542,7 +666,7 @@ END $bgcol=~s/[^7-9a-e]//g; $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6); if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) { - $carea.='<br />Section/Group: '.$3; + $carea.='<br />Section: '.$3; $sortkey.="\0$3"; } $area=$carea; @@ -661,7 +785,7 @@ END my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain); if ($currentauth=~/^krb(4|5):/) { $currentauth=~/^krb(4|5):(.*)/; - my $krbdefdom=$1; + my $krbdefdom=$2; my %param = ( formname => 'document.cu', kerb_def_dom => $krbdefdom ); @@ -859,7 +983,7 @@ ENDDROW $r->print(&course_level_table(%inccourses)); $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setSections()">'."\n"); } - $r->print("</form></body></html>"); + $r->print("</form>".&Apache::loncommon::end_page()); } # ================================================================= Phase Three @@ -869,22 +993,16 @@ sub update_user_data { $env{'form.ccdomain'}); # Error messages my $error = '<font color="#ff0000">'.&mt('Error').':</font>'; - my $end = '</body></html>'; - # Print header - my $html=&Apache::lonxml::xmlbegin(); - $r->print(<<ENDTHREEHEAD); -$html -<head> -<title>The LearningOnline Network with CAPA</title> -</head> -ENDTHREEHEAD + my $end = &Apache::loncommon::end_page(); + my $title; if (exists($env{'form.makeuser'})) { $title='Set Privileges for New User'; } else { $title='Modify User Privileges'; } - $r->print(&Apache::loncommon::bodytag($title)); + $r->print(&Apache::loncommon::start_page($title)); + my %disallowed; # Check Inputs if (! $env{'form.ccuname'} ) { $r->print($error.&mt('No login name specified').'.'.$end); @@ -1114,7 +1232,7 @@ END } if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) { # Revoke custom role - $r->print(&mt('Revoking custom role'). + $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). @@ -1137,7 +1255,7 @@ END &Apache::lonnet::escape($now.':'), $env{'course.'.$cid.'.home'}).'</b><br />'); } - } + } } elsif ($_=~/^form\.ren/) { my $udom = $env{'form.ccdomain'}; my $uname = $env{'form.ccuname'}; @@ -1165,7 +1283,7 @@ END $role,$url,$result).'<br />'; } $r->print($output); - } + } } elsif ($_=~/^form\.act/) { my $udom = $env{'form.ccdomain'}; my $uname = $env{'form.ccuname'}; @@ -1188,8 +1306,15 @@ END if ($num_sections == 0) { $r->print(&commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end)); } else { - foreach (sort {$a cmp $b} keys %sections) { - my $securl = $url.'/'.$_; + my %curr_groups = + &Apache::loncommon::coursegroups($one,$two); + foreach my $sec (sort {$a cmp $b} keys %sections) { + if (($sec eq 'none') || ($sec eq 'all') || + exists($curr_groups{$sec})) { + $disallowed{$sec} = $url; + next; + } + my $securl = $url.'/'.$sec; $r->print(&commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end)); } } @@ -1211,10 +1336,17 @@ END if ($num_sections == 0) { $r->print(&commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'')); } else { + my %curr_groups = + &Apache::loncommon::coursegroups($one,$two); my $emptysec = 0; foreach my $sec (sort {$a cmp $b} keys %sections) { $sec =~ s/\W//g; - if ($sec ne '') { + if ($sec ne '') { + if (($sec eq 'none') || ($sec eq 'all') || + exists($curr_groups{$sec})) { + $disallowed{$sec} = $url; + next; + } my $securl = $url.'/'.$sec; $r->print(&commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec)); } else { @@ -1257,12 +1389,20 @@ END } else { $r->print('<p>'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$_.'</tt></p><br />'); } - } + foreach my $key (sort(keys(%disallowed))) { + if (($key eq 'none') || ($key eq 'all')) { + $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key)); + } else { + $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is the name of a course group.',$key)); + } + $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />'); + } + } } # End of foreach (keys(%env)) # Flush the course logs so reverse user roles immediately updated &Apache::lonnet::flushcourselogs(); $r->print('<p><a href="/adm/createuser">Create/Modify Another User</a></p>'); - $r->print('</body></html>'); + $r->print(&Apache::loncommon::end_page()); } sub commit_customrole { @@ -1343,7 +1483,7 @@ sub commit_studentrole { } } else { $$logmsg .= "Incomplete course id defined. Addition of user $uname from domain $udom to course $one\_$two, section $sec not completed.$linefeed"; - $result = "Error: incomplete course id\n"; + $result = "error: incomplete course id\n"; } return $result; } @@ -1398,14 +1538,14 @@ sub custom_role_editor { return; } - $r->print(&Apache::loncommon::bodytag( - 'Create Users, Change User Privileges').'<h2>'); + $r->print(&Apache::loncommon::start_page('Custom Role Editor')); my $syspriv=''; my $dompriv=''; my $coursepriv=''; my ($rdummy,$roledef)= &Apache::lonnet::get('roles',["rolesdef_$rolename"]); # ------------------------------------------------------- Does this role exist? + $r->print('<h2>'); if (($rdummy ne 'con_lost') && ($roledef ne '')) { $r->print(&mt('Existing Role').' "'); # ------------------------------------------------- Get current role privileges @@ -1477,12 +1617,13 @@ ENDCCF '</td></tr>'); } $r->print( - '<table><input type="submit" value="'.&mt('Define Role').'" /></form></body></html>'); + '<table><input type="submit" value="'.&mt('Define Role').'" /></form>'. + &Apache::loncommon::end_page()); } # ---------------------------------------------------------- Call to definerole sub set_custom_role { - my $r=shift; + my ($r) = @_; my $rolename=$env{'form.rolename'}; @@ -1493,10 +1634,10 @@ sub set_custom_role { return; } - $r->print(&Apache::loncommon::bodytag( - 'Create Users, Change User Privileges').'<h2>'); + $r->print(&Apache::loncommon::start_page('Save Custom Role').'<h2>'); my ($rdummy,$roledef)= - &Apache::lonnet::get('roles',["rolesdef_$rolename"]); + &Apache::lonnet::get('roles',["rolesdef_$rolename"]); + # ------------------------------------------------------- Does this role exist? if (($rdummy ne 'con_lost') && ($roledef ne '')) { $r->print(&mt('Existing Role').' "'); @@ -1546,7 +1687,8 @@ sub set_custom_role { $env{'user.name'}, $rolename)); } - $r->print('</body></html>'); + $r->print('<p><a href="/adm/createuser">Create another role, or Create/Modify a user.</a></p>'); + $r->print(&Apache::loncommon::end_page()); } # ================================================================ Main Handler @@ -1603,7 +1745,7 @@ sub course_level_table { 'act' => "Activate", 'rol' => "Role", 'ext' => "Extent", - 'grs' => "Group/Section", + 'grs' => "Section", 'sta' => "Start", 'end' => "End" ); @@ -1752,7 +1894,7 @@ sub course_level_dc { 'crl' => "Course Level", 'crt' => "Course Title", 'rol' => "Role", - 'grs' => "Group/Section", + 'grs' => "Section", 'exs' => "Existing sections", 'new' => "Define new section", 'sta' => "Start", @@ -1781,7 +1923,8 @@ sub course_level_dc { ' <option value=""><--'.&mt('Pick course first').'</select></td>'. '<td> </td>'. '<td valign="top"> <b>'.$lt{'new'}.'</b><br />'. - '<input type="text" name="newsec" value="" /></td>'. + '<input type="text" name="newsec" value="" />'. + '<input type="hidden" name="groups" value="" /></td>'. '</tr></table></td>'; $otheritems .= <<ENDTIMEENTRY; <td><input type=hidden name="start" value=''>