--- loncom/interface/loncreateuser.pm	2003/07/19 21:44:51	1.64
+++ loncom/interface/loncreateuser.pm	2003/09/17 17:30:10	1.67
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.64 2003/07/19 21:44:51 www Exp $
+# $Id: loncreateuser.pm,v 1.67 2003/09/17 17:30:10 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -29,6 +29,36 @@
 
 package Apache::loncreateuser;
 
+=pod
+
+=head1 NAME
+
+Apache::loncreateuser - handler to create users and custom roles
+
+=head1 SYNOPSIS
+
+Apache::loncreateuser provides an Apache handler for creating users,
+    editing their login parameters, roles, and removing roles, and
+    also creating and assigning custom roles.
+
+=head1 OVERVIEW
+
+=head2 Custom Roles
+
+In LON-CAPA, roles are actually collections of privileges. "Teaching
+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 role definitions are stored in the C<roles.db> file of the role
+author.
+
+=cut
+
 use strict;
 use Apache::Constants qw(:common :http);
 use Apache::lonnet;
@@ -280,7 +310,10 @@ END
 <table border=2>
 <tr><th>Revoke</th><th>Delete</th><th>Role</th><th>Extent</th><th>Start</th><th>End</th>
 END
-	   foreach my $area (sort keys(%rolesdump)) {
+	   foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
+				    my $b1=join('_',(split('_',$b))[1,0]);
+				    return $a1 cmp $b1;
+				} keys(%rolesdump)) {
                next if ($area =~ /^rolesdef/);
                my $role = $rolesdump{$area};
                my $thisrole=$area;
@@ -755,8 +788,8 @@ END
 	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
 # Revoke standard role
 	        $r->print('Revoking '.$2.' in '.$1.': <b>'.
-                     &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
-                     $ENV{'form.ccuname'},$1,$2,$now).'</b><br>');
+                     &Apache::lonnet::revokerole($ENV{'form.ccdomain'},
+                     $ENV{'form.ccuname'},$1,$2).'</b><br>');
 		if ($2 eq 'st') {
 		    $1=~/^\/(\w+)\/(\w+)/;
 		    my $cid=$1.'_'.$2;
@@ -773,7 +806,9 @@ END
 	    if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
 # Revoke custom role
 		$r->print(
-                'Revoking custom role '.$4.' by '.$3.'\@'.$2.' in '.$1.': <b>'.
+                'Revoking custom role '.$4.' by '.$3.'@'.$2.' in '.$1.': <b>'.
+&Apache::lonnet::revokecustomrole($ENV{'form.ccdomain'},
+				  $ENV{'form.ccuname'},$1,$2,$3,$4).
 		'</b><br>');
 	    }
 	} elsif ($_=~/^form\.del/) {
@@ -795,7 +830,29 @@ END
 		}
 	    } 
 	} elsif ($_=~/^form\.act/) {
-	    if ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
+	    if 
+($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_cr_cr_([^\_]+)_(\w+)_([^\_]+)$/) {
+                # Activate a custom role
+		my $url='/'.$1.'/'.$2;
+		my $full=$1.'_'.$2.'_cr_cr_'.$3.'_'.$4.'_'.$5;
+		if ($ENV{'form.sec_'.$full}) {
+		    $url.='/'.$ENV{'form.sec_'.$full};
+		}
+
+		my $start = ( $ENV{'form.start_'.$full} ? 
+			      $ENV{'form.start_'.$full} : 
+			      $now );
+		my $end   = ( $ENV{'form.end_'.$full} ? 
+			      $ENV{'form.end_'.$full} :
+			      0 );
+
+    $r->print('Assigning custom role "'.$5.'" by '.$4.'@'.$3.' in '.$url.
+                         ($start?', starting '.localtime($start):'').
+                         ($end?', ending '.localtime($end):'').': <b>'.
+	      &Apache::lonnet::assigncustomrole(
+	$ENV{'form.ccdomain'},$ENV{'form.ccuname'},$url,$3,$4,$5,$end,$start).
+	      '</b><br>');
+	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
 		# Activate roles for sections with 3 id numbers
 		# set start, end times, and the url for the class
 
@@ -810,7 +867,7 @@ END
 		    $url.='/'.$ENV{'form.sec_'.$1.'_'.$2.'_'.$3};
 		}
 		# Assign the role and report it
-		$r->print('Assigning: '.$3.' in '.$url.
+		$r->print('Assigning '.$3.' in '.$url.
                          ($start?', starting '.localtime($start):'').
                          ($end?', ending '.localtime($end):'').': <b>'.
                           &Apache::lonnet::assignrole(
@@ -843,7 +900,7 @@ END
 			      0 );
 		my $url='/'.$1.'/';
 		# Assign the role and report it.
-		$r->print('Assigning: '.$2.' in '.$url.': '.
+		$r->print('Assigning '.$2.' in '.$url.': '.
                          ($start?', starting '.localtime($start):'').
                          ($end?', ending '.localtime($end):'').': <b>'.
                           &Apache::lonnet::assignrole(
@@ -1106,20 +1163,24 @@ ENDTIMEENTRY
             }
         }
         foreach (sort keys %customroles) {
-	    my $plrole=$_;
-	    $table .= <<ENDENTRY;
+	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
+		my $plrole=$_;
+                my $customrole=$protectedcourse.'_cr_cr_'.$ENV{'user.domain'}.
+		    '_'.$ENV{'user.name'}.'_'.$plrole;
+		$table .= <<ENDENTRY;
 <tr bgcolor="#$bgcol">
-<td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
+<td><input type="checkbox" name="act_$customrole"></td>
 <td>$plrole</td>
 <td>$area</td>
-<td><input type="text" size="5" name="sec_$protectedcourse\_$_"></td>
-<td><input type=hidden name="start_$protectedcourse\_$_" value=''>
+<td><input type="text" size="5" name="sec_$customrole"></td>
+<td><input type=hidden name="start_$customrole" value=''>
 <a href=
-"javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">Set Start Date</a></td>
-<td><input type=hidden name="end_$protectedcourse\_$_" value=''>
+"javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">Set Start Date</a></td>
+<td><input type=hidden name="end_$customrole" value=''>
 <a href=
-"javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">Set End Date</a></td></tr>
+"javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">Set End Date</a></td></tr>
 ENDENTRY
+           }
 	}
     }
     return '' if ($table eq ''); # return nothing if there is nothing