--- loncom/interface/loncreateuser.pm	2003/07/18 13:45:14	1.60
+++ loncom/interface/loncreateuser.pm	2003/07/20 00:39:01	1.65
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.60 2003/07/18 13:45:14 www Exp $
+# $Id: loncreateuser.pm,v 1.65 2003/07/20 00:39:01 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,28 +25,6 @@
 #
 # http://www.lon-capa.org/
 #
-# (Create a course
-# (My Desk
-#
-# (Internal Server Error Handler
-#
-# (Login Screen
-# 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
-# 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
-#
-# YEAR=2001
-# 3/1/1 Gerd Kortemeyer)
-#
-# 3/1 Gerd Kortemeyer)
-#
-# 2/14 Gerd Kortemeyer)
-#
-# 2/14,2/17,2/19,2/20,2/21,2/22,2/23,3/2,3/17,3/24,04/12 Gerd Kortemeyer
-# April Guy Albertelli
-# 05/10,10/16 Gerd Kortemeyer 
-# 02/11/02 Matthew Hall
-#
-# $Id: loncreateuser.pm,v 1.60 2003/07/18 13:45:14 www Exp $
 ###
 
 package Apache::loncreateuser;
@@ -90,7 +68,7 @@ sub my_custom_roles {
     my %rolehash=&Apache::lonnet::dump('roles');
     foreach (keys %rolehash) {
 	if ($_=~/^rolesdef\_(\w+)$/) {
-	    $returnhash{$_}=$_;
+	    $returnhash{$1}=$1;
 	}
     }
     return %returnhash;
@@ -302,13 +280,16 @@ 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 (keys(%rolesdump)) {
+	   foreach my $area (sort keys(%rolesdump)) {
                next if ($area =~ /^rolesdef/);
                my $role = $rolesdump{$area};
                my $thisrole=$area;
                $area =~ s/\_\w\w$//;
                my ($role_code,$role_end_time,$role_start_time) = 
                    split(/_/,$role);
+# Is this a custom role? Get role owner and title.
+	       my ($croleudom,$croleuname,$croletitle)=
+	           ($role_code=~/^cr\/(\w+)\/(\w+)\/(\w+)$/);
                my $bgcol='ffffff';
                my $allowed=0;
                my $delallowed=0;
@@ -334,10 +315,17 @@ END
                        (&Apache::lonnet::allowed('dro',$ccdomain))) {
                        $delallowed=1;
                    }
+# - custom role. Needs more info, too
+		   if ($croletitle) {
+		       if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
+			   $allowed=1;
+			   $thisrole.='.'.$role_code;
+		       }
+		   }
                    # Compute the background color based on $area
                    $bgcol=$1.'_'.$2;
-                   $bgcol=~s/[^8-9b-e]//g;
-                   $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',0,6);
+                   $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;
                    }
@@ -369,7 +357,7 @@ END
                    }
                }
                my $row = '';
-               $row.='<tr bgcolor=#"'.$bgcol.'"><td>';
+               $row.='<tr bgcolor="#'.$bgcol.'"><td>';
                my $active=1;
                $active=0 if (($role_end_time) && ($now>$role_end_time));
                if (($active) && ($allowed)) {
@@ -387,7 +375,14 @@ END
                } else {
                    $row.='&nbsp;';
                }
-               $row.= '</td><td>'.&Apache::lonnet::plaintext($role_code).
+	       my $plaintext='';
+	       unless ($croletitle) {
+		   $plaintext=&Apache::lonnet::plaintext($role_code);
+	       } else {
+	           $plaintext=
+		"Customrole '$croletitle' defined by $croleuname\@$croleudom";
+	       }
+               $row.= '</td><td>'.$plaintext.
                       '</td><td>'.$area.
                       '</td><td>'.($role_start_time?localtime($role_start_time)
                                                    : '&nbsp;' ).
@@ -757,10 +752,11 @@ END
 	next if (! $ENV{$_});
 	# Revoke roles
 	if ($_=~/^form\.rev/) {
-	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_]+)$/) {
+	    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;
@@ -774,6 +770,14 @@ END
 	                     $ENV{'course.'.$cid.'.home'}).'</b><br>');
 		}
 	    } 
+	    if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
+# Revoke custom role
+		$r->print(
+                '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/) {
 	    if ($_=~/^form\.del\:([^\_]+)\_([^\_]+)$/) {
 	        $r->print('Deleting '.$2.' in '.$1.': '.
@@ -793,7 +797,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
 
@@ -808,7 +834,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(
@@ -841,14 +867,16 @@ 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(
                               $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
                               $url,$2,$end,$start)
 			  .'</b><br>');
-	    }
+	    } else {
+		$r->print('<p>ERROR: Unknown command <tt>'.$_.'</tt></p><br>');
+            }
 	} 
     } # End of foreach (keys(%ENV))
     $r->print('</body></html>');
@@ -864,7 +892,7 @@ sub custom_role_editor {
 	$rolename=$ENV{'form.newrolename'};
     }
 
-    $rolename=~s/\W//gs;
+    $rolename=~s/[^A-Za-z0-9]//gs;
 
     unless ($rolename) {
 	&print_username_entry_form($r);
@@ -873,11 +901,16 @@ sub custom_role_editor {
 
     $r->print(&Apache::loncommon::bodytag(
                      'Create Users, Change User Privileges').'<h2>');
+    my $syspriv='';
+    my $dompriv='';
+    my $coursepriv='';
     my ($rdummy,$roledef)=
 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 # ------------------------------------------------------- Does this role exist?
     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 	$r->print('Existing Role "');
+# ------------------------------------------------- Get current role privileges
+	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
     } else {
 	$r->print('New Role "');
 	$roledef='';
@@ -886,26 +919,129 @@ sub custom_role_editor {
 # ------------------------------------------------------- What can be assigned?
     my %full=();
     my %courselevel=();
+    my %courselevelcurrent=();
     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 	my ($priv,$restrict)=split(/\&/,$_);
         unless ($restrict) { $restrict='F'; }
         $courselevel{$priv}=$restrict;
+        if ($coursepriv=~/\:$priv/) {
+	    $courselevelcurrent{$priv}=1;
+	}
 	$full{$priv}=1;
     }
     my %domainlevel=();
+    my %domainlevelcurrent=();
     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 	my ($priv,$restrict)=split(/\&/,$_);
         unless ($restrict) { $restrict='F'; }
         $domainlevel{$priv}=$restrict;
+        if ($dompriv=~/\:$priv/) {
+	    $domainlevelcurrent{$priv}=1;
+	}
+	$full{$priv}=1;
+    }
+    my %systemlevel=();
+    my %systemlevelcurrent=();
+    foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
+	my ($priv,$restrict)=split(/\&/,$_);
+        unless ($restrict) { $restrict='F'; }
+        $systemlevel{$priv}=$restrict;
+        if ($syspriv=~/\:$priv/) {
+	    $systemlevelcurrent{$priv}=1;
+	}
 	$full{$priv}=1;
     }
-    $r->print('<table border="2"><tr><th>Privilege</th><th>Course Level</th><th>Domain Level</th></tr>');
+    $r->print(<<ENDCCF);
+<form method="post">
+<input type="hidden" name="phase" value="set_custom_roles" />
+<input type="hidden" name="rolename" value="$rolename" />
+<table border="2">
+<tr><th>Privilege</th><th>Course Level</th><th>Domain Level</th>
+<th>System Level</th></tr>
+ENDCCF
     foreach (sort keys %full) {
 	$r->print('<tr><td>'.&Apache::lonnet::plaintext($_).'</td><td>'.
-		  $courselevel{$_}.'</td><td>'.$domainlevel{$_}.'</td></tr>');
+    ($courselevel{$_}?'<input type="checkbox" name="'.$_.':c" '.
+    ($courselevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
+    '</td><td>'.
+    ($domainlevel{$_}?'<input type="checkbox" name="'.$_.':d" '.
+    ($domainlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
+    '</td><td>'.
+    ($systemlevel{$_}?'<input type="checkbox" name="'.$_.':s" '.
+    ($systemlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
+    '</td></tr>');
     }
-    $r->print('</table>');
-    $r->print('Not yet implemented.');
+    $r->print(
+   '<table><input type="submit" value="Define Role" /></form></body></html>');
+}
+
+# ---------------------------------------------------------- Call to definerole
+sub set_custom_role {
+    my $r=shift;
+
+    my $rolename=$ENV{'form.rolename'};
+
+    $rolename=~s/[^A-Za-z0-9]//gs;
+
+    unless ($rolename) {
+	&print_username_entry_form($r);
+        return;
+    }
+
+    $r->print(&Apache::loncommon::bodytag(
+                     'Create Users, Change User Privileges').'<h2>');
+    my ($rdummy,$roledef)=
+			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
+# ------------------------------------------------------- Does this role exist?
+    if (($rdummy ne 'con_lost') && ($roledef ne '')) {
+	$r->print('Existing Role "');
+    } else {
+	$r->print('New Role "');
+	$roledef='';
+    }
+    $r->print($rolename.'"</h2>');
+# ------------------------------------------------------- What can be assigned?
+    my $sysrole='';
+    my $domrole='';
+    my $courole='';
+
+    foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
+	my ($priv,$restrict)=split(/\&/,$_);
+        unless ($restrict) { $restrict=''; }
+        if ($ENV{'form.'.$priv.':c'}) {
+	    $courole.=':'.$_;
+	}
+    }
+
+    foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
+	my ($priv,$restrict)=split(/\&/,$_);
+        unless ($restrict) { $restrict=''; }
+        if ($ENV{'form.'.$priv.':d'}) {
+	    $domrole.=':'.$_;
+	}
+    }
+
+    foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
+	my ($priv,$restrict)=split(/\&/,$_);
+        unless ($restrict) { $restrict=''; }
+        if ($ENV{'form.'.$priv.':s'}) {
+	    $sysrole.=':'.$_;
+	}
+    }
+    $r->print('<br />Defining Role: '.
+	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
+    if ($ENV{'request.course.id'}) {
+        my $url='/'.$ENV{'request.course.id'};
+        $url=~s/\_/\//g;
+	$r->print('<br />Assigning Role to Self: '.
+	      &Apache::lonnet::assigncustomrole($ENV{'user.domain'},
+						$ENV{'user.name'},
+						$url,
+						$ENV{'user.domain'},
+						$ENV{'user.name'},
+						$rolename));
+    }
+    $r->print('</body></html>');
 }
 
 # ================================================================ Main Handler
@@ -935,6 +1071,8 @@ sub handler {
            &update_user_data($r);
        } elsif ($ENV{'form.phase'} eq 'selected_custom_edit') {
            &custom_role_editor($r);
+       } elsif ($ENV{'form.phase'} eq 'set_custom_roles') {
+	   &set_custom_role($r);
        }
    } else {
       $ENV{'user.error.msg'}=
@@ -948,6 +1086,10 @@ sub handler {
 sub course_level_table {
     my %inccourses = @_;
     my $table = '';
+# Custom Roles?
+
+    my %customroles=&my_custom_roles();
+
     foreach (sort( keys(%inccourses))) {
 	my $thiscourse=$_;
 	my $protectedcourse=$_;
@@ -956,8 +1098,8 @@ sub course_level_table {
 	my $area=$coursedata{'description'};
 	if (!defined($area)) { $area='Unavailable course: '.$_; }
 	my $bgcol=$thiscourse;
-	$bgcol=~s/[^8-9b-e]//g;
-	$bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',0,6);
+	$bgcol=~s/[^7-9a-e]//g;
+	$bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
 	foreach  ('st','ta','ep','ad','in','cc') {
 	    if (&Apache::lonnet::allowed('c'.$_,$thiscourse)) {
 		my $plrole=&Apache::lonnet::plaintext($_);
@@ -987,6 +1129,26 @@ ENDTIMEENTRY
                 $table.= "</tr>\n";
             }
         }
+        foreach (sort keys %customroles) {
+	    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_$customrole"></td>
+<td>$plrole</td>
+<td>$area</td>
+<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_$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_$customrole.value,'end_$customrole','cu.pres','dateset')">Set End Date</a></td></tr>
+ENDENTRY
+           }
+	}
     }
     return '' if ($table eq ''); # return nothing if there is nothing 
                                  # in the table