[LON-CAPA-cvs] cvs: loncom(version_2_9_X) /interface loncreateuser.pm

raeburn raeburn@source.lon-capa.org
Tue, 19 Jan 2010 22:41:02 -0000


This is a MIME encoded message

--raeburn1263940862
Content-Type: text/plain

raeburn		Tue Jan 19 22:41:02 2010 EDT

  Modified files:              (Branch: version_2_9_X)
    /loncom/interface	loncreateuser.pm 
  Log:
  - Backport 1.324.
  
  
--raeburn1263940862
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20100119224102.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.295.2.23 loncom/interface/loncreateuser.pm:1.295.2.24
--- loncom/interface/loncreateuser.pm:1.295.2.23	Tue Jan 19 21:29:50 2010
+++ loncom/interface/loncreateuser.pm	Tue Jan 19 22:41:02 2010
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.295.2.23 2010/01/19 21:29:50 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.295.2.24 2010/01/19 22:41:02 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -51,13 +51,14 @@
 Assistant", "Course Coordinator", and other such roles are really just
 collection of privileges that are useful in many circumstances.
 
-Creating custom roles can be done by the Domain Coordinator through
-the Create User functionality. That screen will show all privileges
-that can be assigned to users. For a complete list of privileges,
-please see C</home/httpd/lonTabs/rolesplain.tab>.
+Custom roles can be defined by a Domain Coordinator, Course Coordinator
+or Community Coordinator via the Manage User functionality.
+The custom role editor screen will show all privileges which can be
+assigned to users. For a complete list of privileges, please see 
+C</home/httpd/lonTabs/rolesplain.tab>.
 
-Custom role definitions are stored in the C<roles.db> file of the role
-author.
+Custom role definitions are stored in the C<roles.db> file of the creator
+of the role.
 
 =cut
 
@@ -485,6 +486,11 @@
         '// ]]>'."\n".
         '</script>'."\n";
 
+    my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
+    if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
+        && (&Apache::lonnet::allowed('mcr','/'))) {
+        $jscript .= &customrole_javascript();
+    }
     my %loaditems = (
                 'onload' => "javascript:setFormElements(document.$formtoset)",
                     );
@@ -510,9 +516,6 @@
     }
     my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management',
                                                      $helpitem);
-    my %existingroles=&Apache::lonuserutils::my_custom_roles();
-    my $choice=&Apache::loncommon::select_form('make new role','rolename',
-		('make new role' => 'Generate new role ...',%existingroles));
     my %lt=&Apache::lonlocal::texthash(
                     'srst' => 'Search for a user and enroll as a student',
                     'srme' => 'Search for a user and enroll as a member',
@@ -520,22 +523,47 @@
                     'srad' => 'Search for a user and modify/add user information or roles',
 		    'usr'  => "Username",
                     'dom'  => "Domain",
-                    'ecrp' => "Edit Custom Role Privileges",
-                    'nr'   => "Name of Role",
+                    'ecrp' => "Define or Edit Custom Role",
+                    'nr'   => "role name",
                     'cre'  => "Next",
 				       );
     $r->print($start_page."\n".$crumbs);
     if ($env{'form.action'} eq 'custom') {
         if (&Apache::lonnet::allowed('mcr','/')) {
-            $r->print(<<ENDCUSTOM);
-<form action="/adm/createuser" method="post" name="docustom">
-<input type="hidden" name="action" value="$env{'form.action'}" />
-<input type="hidden" name="phase" value="selected_custom_edit" />
-<h3>$lt{'ecrp'}</h3>
-$choice $lt{'nr'}: <input type="text" size="15" name="newrolename" /><br />
-<input name="customeditor" type="submit" value="$lt{'cre'}" />
-</form>
-ENDCUSTOM
+            my $newroletext = &mt('Define new custom role:');
+            $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
+                      '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
+                      '<input type="hidden" name="phase" value="selected_custom_edit" />'.
+                      '<h3>'.$lt{'ecrp'}.'</h3>'.
+                      &Apache::loncommon::start_data_table().
+                      &Apache::loncommon::start_data_table_row().
+                      '<td>');
+            if (keys(%existingroles) > 0) {
+                $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
+            } else {
+                $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
+            }
+            $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
+                      &Apache::loncommon::end_data_table_row());
+            if (keys(%existingroles) > 0) {
+                $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
+                          '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
+                          &mt('View/Modify existing role:').'</b></label></td>'.
+                          '<td align="center"><br />'.
+                          '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
+                          '<option value="" selected="selected">'.
+                          &mt('Select'));
+                foreach my $role (sort(keys(%existingroles))) {
+                    $r->print('<option value="'.$role.'">'.$role.'</option>');
+                }
+                $r->print('</select>'.
+                          '</td>'.
+                          &Apache::loncommon::end_data_table_row());
+            }
+            $r->print(&Apache::loncommon::end_data_table().'<p>'.
+                      '<input name="customeditor" type="submit" value="'.
+                      $lt{'cre'}.'" /></p>'.
+                      '</form>');
         }
     } else {
         my $actiontext = $lt{'srad'};
@@ -555,6 +583,44 @@
     $r->print(&Apache::loncommon::end_page());
 }
 
+sub customrole_javascript {
+    my $js = <<"END";
+<script type="text/javascript">
+// <![CDATA[
+
+function setCustomFields() {
+    if (document.docustom.customroleaction.length > 0) {
+        for (var i=0; i<document.docustom.customroleaction.length; i++) {
+            if (document.docustom.customroleaction[i].checked) {
+                if (document.docustom.customroleaction[i].value == 'new') {
+                    document.docustom.rolename.selectedIndex = 0;
+                } else {
+                    document.docustom.newrolename.value = '';
+                }
+            }
+        }
+    }
+    return;
+}
+
+function setCustomAction(caller) {
+    if (document.docustom.customroleaction.length > 0) {
+        for (var i=0; i<document.docustom.customroleaction.length; i++) {
+            if (document.docustom.customroleaction[i].value == caller) {
+                document.docustom.customroleaction[i].checked = true;
+            }
+        }
+    }
+    setCustomFields();
+    return;
+}
+
+// ]]>
+</script>
+END
+    return $js;
+}
+
 sub entry_form {
     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype) = @_;
     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
@@ -3472,18 +3538,27 @@
 
 sub custom_role_editor {
     my ($r) = @_;
-    my $rolename=$env{'form.rolename'};
-
-    if ($rolename eq 'make new role') {
-	$rolename=$env{'form.newrolename'};
+    my $action = $env{'form.customroleaction'};
+    my $rolename;
+    if ($action eq 'new') {
+        $rolename=$env{'form.newrolename'};
+    } else {
+        $rolename=$env{'form.rolename'};
     }
 
     $rolename=~s/[^A-Za-z0-9]//gs;
-
     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
 	&print_username_entry_form($r);
         return;
     }
+    my ($crstype,$context);
+    if ($env{'request.course.id'}) {
+        $crstype = &Apache::loncommon::course_type();
+        $context = 'course';
+    } else {
+        $context = 'domain';
+        $crstype = $env{'form.templatecrstype'};
+    }
 # ------------------------------------------------------- What can be assigned?
     my %full=();
     my %courselevel=();
@@ -3501,6 +3576,9 @@
 	$body_top .= &mt('Existing Role').' "';
 # ------------------------------------------------- Get current role privileges
 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
+        if ($crstype eq 'Community') {
+            $syspriv =~ s/bre\&S//;
+        }
     } else {
 	$body_top .= &mt('New Role').' "';
 	$roledef='';
@@ -3542,25 +3620,54 @@
     my $head_script = "\n";
     $head_script .= '<script type="text/javascript">'."\n".
                     '// <![CDATA['."\n";
-    my $crstype;
-    if ($env{'request.course.id'}) {
-        $crstype = &Apache::loncommon::course_type();
+    my @template_roles = ("in","ta","ep");
+    if ($context eq 'domain') {
+        push(@template_roles,"ad");
     }
-    my @template_roles = ("in","ta","ep","st");
+    push(@template_roles,"st");
     if ($crstype eq 'Community') {
         unshift(@template_roles,'co');
     } else {
         unshift(@template_roles,'cc');
     }
     foreach my $role (@template_roles) {
-        $head_script .= &make_script_template($role);
+        $head_script .= &make_script_template($role,$crstype);
         $button_code .= &make_button_code($role,$crstype).' ';
     }
+    my $context_code;
+    if ($context eq 'domain') {
+        my $checkedCommunity = '';
+        my $checkedCourse = ' checked="checked"';
+        if ($env{'form.templatecrstype'} eq 'Community') {
+            $checkedCommunity = $checkedCourse;
+            $checkedCourse = '';
+        }
+        $context_code = '<label>'.
+                        '<input type="radio" name="templatecrstype" value="Course"'.$checkedCourse.' onclick="this.form.submit();">'.
+                        &mt('Course').
+                        '</label>'.('&nbsp;' x2).
+                        '<label>'.
+                        '<input type="radio" name="templatecrstype" value="Community"'.$checkedCommunity.' onclick="this.form.submit();">'.
+                        &mt('Community').
+                        '</label>'.
+                        '</fieldset>'.
+                        '<input type="hidden" name="customroleaction" value="'.
+                        $action.'" />';
+        if ($env{'form.customroleaction'} eq 'new') {
+            $context_code .= '<input type="hidden" name="newrolename" value="'.
+                             $rolename.'" />';
+        } else {
+            $context_code .= '<input type="hidden" name="rolename" value="'.
+                             $rolename.'" />';
+        }
+        $context_code .= '<input type="hidden" name="action" value="custom" />'.
+                         '<input type="hidden" name="phase" value="selected_custom_edit" />';
+    }
     $head_script .= "\n".$jsback."\n".
                     '// ]]>'."\n".
                     '</script>'."\n";
     $r->print(&Apache::loncommon::start_page('Custom Role Editor',$head_script));
-   &Apache::lonhtmlcommon::add_breadcrumb
+    &Apache::lonhtmlcommon::add_breadcrumb
      ({href=>"javascript:backPage(document.form1,'pickrole','')",
        text=>"Pick custom role",
        faq=>282,bug=>'Instructor Interface',},
@@ -3577,13 +3684,21 @@
                     'dml'  => "Domain Level",
                     'ssl'  => "System Level");
 
-    $r->print('<div>'
+    $r->print('<div class="LC_left_float">'
              .'<form action=""><fieldset>'
              .'<legend>'.&mt('Select a Template').'</legend>'
              .$button_code
-             .'</fieldset></form>'
-             .'</div>'
-    );
+             .'</fieldset></form></div>');
+    if ($context_code) {
+        $r->print('<div class="LC_left_float">'
+                 .'<form action="/adm/createuser" method="post"><fieldset>'
+                 .'<legend>'.&mt('Context').'</legend>'
+                 .$context_code
+                 .'</form>'
+                 .'</div>'
+        );
+    }
+    $r->print('<br clear="all" />');
 
     $r->print(<<ENDCCF);
 <form name="form1" method="post">
@@ -3604,11 +3719,15 @@
     '</td><td>'.
     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d"'.
     ($domainlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
-    '</td><td>'.
-    ($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
-    ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
-    '</td>'.
-             &Apache::loncommon::end_data_table_row());
+    '</td><td>');
+        if ($priv eq 'bre' && $crstype eq 'Community') {
+            $r->print('&nbsp;');
+        } else {
+            $r->print($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
+                      ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;');
+        }
+        $r->print('</td>'.
+                  &Apache::loncommon::end_data_table_row());
     }
     $r->print(&Apache::loncommon::end_data_table().
    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
@@ -3620,7 +3739,7 @@
 }
 # --------------------------------------------------------
 sub make_script_template {
-    my ($role) = @_;
+    my ($role,$crstype) = @_;
     my %full_c=();
     my %full_d=();
     my %full_s=();
@@ -3634,6 +3753,7 @@
         $full_d{$priv}=1;
     }
     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
+        next if (($crstype eq 'Community') && ($item eq 'bre&S'));
         my ($priv,$restrict)=split(/\&/,$item);
         $full_s{$priv}=1;
     }
@@ -5734,7 +5854,8 @@
             }
         }
         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
-            foreach my $cust (sort keys %customroles) {
+            foreach my $cust (sort(keys(%customroles))) {
+                next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                                             $cust,\%sections_count,\%lt);

--raeburn1263940862--