[LON-CAPA-cvs] cvs: modules /albertel change_preference_setting.pl
albertel
lon-capa-cvs-allow@mail.lon-capa.org
Wed, 20 Jun 2007 20:16:15 -0000
albertel Wed Jun 20 16:16:15 2007 EDT
Added files:
/modules/albertel change_preference_setting.pl
Log:
- helper script to change the settings of a large numbe of users' preference options
Index: modules/albertel/change_preference_setting.pl
+++ modules/albertel/change_preference_setting.pl
#!/usr/bin/perl
#
# The LearningOnline Network
#
# change_preference_setting.pl - Remove a key from a db file.
#
# $Id: change_preference_setting.pl,v 1.1 2007/06/20 20:16:10 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#################################################
use strict;
use GDBM_File;
use lib '/home/httpd/lib/perl/';
use LONCAPA;
use Apache::lonnet;
use Getopt::Long;
#
# Set up configuration options
my ($help,$simulate,$user_list_file,$option_list_file,$verbose,$debug);
$verbose=0;
&GetOptions(
'help' => \$help,
'simulate' => \$simulate,
'userlist=s' => \$user_list_file,
'optionlist=s' => \$option_list_file,
'verbose=s' => \$verbose,
'debug' => \$debug,
);
if ($help) {
print <<"ENDHELP";
$0
Modify a preference setting for a list of users
Options:
-help Print this help
-simulate Do not modify the users.
-userlist=filename File containg list of usernames, one name per line.
Example: smith
or smith:msu
(if the domain is not specified, it will use the
machines default domain.)
-optionlist=filename File containg the options to set, format is
optionstring=value
Example: icons=iconsonly
-verbose=val Sets logging level, val must be a number
ENDHELP
exit 0;
}
&main();
sub say {
my ($what,$level) = @_;
$level ||= 1;
if ($level <= $verbose) {
print("$what\n");
}
}
sub main {
my %options;
open(my $option_file,$option_list_file)
|| die("Can't read from file: $option_list_file");
foreach my $line (<$option_file>) {
chomp($line);
my ($option,$value) = split('=',$line,2);
next if ($option !~ /\S/);
&say("will set $option to value $value",2);
$options{$option} = $value;
}
open(my $user_file,$user_list_file)
|| die("Can't read from file: $user_list_file");
my $successes=0;
foreach my $user (<$user_file>) {
chomp($user);
next if ($user !~ /\S/);
&say("Doing $user",3);
my ($uname, $udom) = split(':',$user,2);
$udom ||= $Apache::lonnet::perlvar{'lonDefDomain'};
&say("Doing use $uname : $udom",2);
my $result =
&Apache::lonnet::put('environment',\%options,$udom,$uname);
if ($result eq 'ok') {
$successes++;
&say("Result was $result",3);
} else {
&say("Unsuccessful result of ($result) when attmpting to change $uname:$udom",-1);
}
}
&say("Succeeded in changing $successes user's settings.",-1);
}
exit;