Jump to content

Recommended Posts

Posted

#!/usr/bin/perl
#moodle email script
#dedicated to all junior IT techs who get all the shitty jobs
#this script dedicated my senior tech, BRUVA!
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Getopt::Long;
use Crypt::SSLeay;
my %options;
#define your options here
%options = ( 'vle' => 'vle.broughton.lancsngfl.ac.uk',
  'emaildomain' => '@bbec.lancs.sch.uk',
   );
GetOptions(\%options,     'user=s',
                         'pass=s',
                         'url=s',
		  'newuser=s',
		  'newpass=s',
		  'createuser',
                       );

#create useragent object
my $ua = LWP::UserAgent->new( cookie_jar => ({}) );
$ua->env_proxy();
my $return = $ua->request(POST "http://login.vle.cleo.net.uk/1.8.2/?vle=$options{vle}",
			[ username => $options{user},
			  password => $options{pass},
			  testcookies => 0,
			  submit => 'Login',
			],
		);
my ($token) = $return->content =~ /token=([0-9a-f]+)\'/;
push @{ $ua->requests_redirectable }, 'POST';
$return = $ua->request(GET "http://login.vle.cleo.net.uk/content.php?"
		    ."url=http://$options{vle}/login/secure_login.php?token=$token");
#check if login succeeded
$return = $ua->request(GET "http://$options{vle}");
die "Login failed" unless $return->content =~ /User Admin/;
my ($sesskey) = $return->content =~ /sesskey=(\S+)\"/;

#edituser($options{newuser}, $options{newpass}, $sesskey) if $options{createuser};
my @uids = grabbyquery($sesskey, 'cleo');
for my $userid (@uids) {
print $userid . "\n";
}

sub edituser {
my ($username, $password, $sesskey) = @_;
my ($lastname, $firstname) = $username =~ /([0-9a-z]+)([a-z])$/;
my $uid = finduid($username) || '-1';
my $return = $ua->request(POST 'http://$options{vle}/user/editadvanced.php',
			Content_Type => 'form-data',
			Content => [
			username => $username,
			newpassword => $password,
			preference_auth_forcepasswordchange => 1,
			firstname => $firstname,
			lastname => $lastname,
			email => $username . $options{email},
			city => 'Preston',
			country => 'GB',
			MAX_FILE_SIZE => 31457280,
			id => $uid,
			course => 1,
			mform_showadvanced_last => 0,
			sesskey => $sesskey,
			_qf__user_editadvanced_form => 1,
			auth => 'manual',
			maildisplay => 2,
			emailstop => 0,
			mailformat => 1,
			preference_mailcharset => 0,
			maildigest => 0,
			autosubscribe => 1,
			trackforums => 0,
			htmleditor => 1,
			ajax => 0,
			screenreader => 0,
			lang => 'en_utf8', 
			],
			);
}

sub finduid {
my $username = shift;
my $return = $ua->request(GET "http://$options{vle}/admin/user.php?search=$username");
my ($uid) = $return->content =~ /\.\.\/user\/view.php\?id=([0-9]+)/;
return $uid;
}

sub deluser {
my ($username, $sesskey) = @_;
my $uid = finduid($username);
my $return = $ua->request(GET "http://$options{vle}/admin/user.php?delete=$uid&sesskey=$sesskey");
my ($verify) = $return->content =~ /confirm" value="([a-z0-9]+)"/;
print "$verify\n";
$return = $ua->request(POST 'http://$options{vle}/admin/user.php',
		Content => [
				delete => $uid,
				confirm => $verify,
				sesskey => $sesskey,
			   ],
		);
}

sub grabbyquery {
my ($sesskey, $search) = @_;
my $return = $ua->request(GET "http://$options{vle}/admin/user.php?sort=firstname&
		       dir=ASC&perpage=30&firstinitial=&lastinitial=&$search=cleo&page=0");
my $hits = $return->content =~ />([0-9]+) \//;
$return = $ua->request(GET "http://$options{vle}/admin/user.php?sort=firstname&
		    dir=ASC&perpage=$hits&firstinitial=&lastinitial=&search=$search&page=0");
my @uids = $return->content =~ /\.\.\/user\/view.php\?id=([0-9]+)/g;
return @uids;
}

 

Just another quickie, I wrote this one this yesterday. Its just a simple moodle framework that can do password resets, delete users.

 

its pretty self explanatory and you have to do a bit of work with the code to make it do what you want.

 

the grabby() sub is nice cos you can then edit users via a search of the moodle user base...

 

the reason i scripted it up is because cleo does not give us any decent database access. this means i cannot fix an error they made (alot of our users are given the @cleo.void.net.uk email which is lame, with this i can easily update all the users to their correct email.

 

i know the answer is to host our own moodle but thats not viable atm, so cleo if you read this, PLEASE GIVE US PHPMYADMIN ACCESS!

 

have phun! :)

Posted

Very interesting - I think the moodle team need to give you a job :)

 

Why don't you just use the normal user account/bulk upload facility for password/email changes?

 

regards

 

Simon

Posted

Yeah, we've done that for a while..the problem arises when you need to delete users...e.g. at the end of the year when you need to remove a yeargroup.

By default the SIMS upload utility creates those users with the broken email address's, at least from what i remember..

 

Don't forget it also saves alot of time by making it easy to add a user in a single command, delete a user or reset the password..(it was also fun ;))

 

anyway, any improvments, suggestions are welcomed (i know the code quality is a little shoddy, it needs tidying up :) )

 

also, id love a job at cleo...if it paid better that is (which im sure it would) ;)

Posted

Thanks for the info Geoff, i dont tend to keep much of an interest in the moodle CVS :p

 

but like i say, its just a framework, there is much more possibilities than just the bulk delete..iv combined it with the lgfl script too so now i can create both users at once...now for AD intergration ;)

 

Have any other idea's i could add to it anyone? Geoff?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...