package IIfe7ed66bde3aa68899b7883eab4be165::mod_Moodle; use strict; use base 'JinPluginBaseExtScript'; use DBI; use Digest::MD5 qw(md5_hex); sub init { my $this = shift()->SUPER::init(@_); return $this->msg('error.missing_config') if grep { ! $this->{$_} } qw/dbhost dbname dbuser/; $this->{dbh} = DBI->connect("DBI:mysql:$this->{dbname}:$this->{dbhost}", $this->{dbuser}, $this->{dbpass}) or return $this->msg('error.connect_db', $DBI::errstr); my $sth = $this->sql('show tables like ?', "$this->{tblprefix}user"); return $this->msg('error.tables_not_found', $this->{tblprefix}) unless $sth->rows > 0; return; } sub addUser { my $this = shift; my $user = shift; my $script_uid = $this->_get_script_uid($user); if ($script_uid) { $this->updateUser($user); } else { my $sth = $this->sql("SELECT max(id) FROM $this->{tblprefix}user") or return; my $max_id = $sth->fetchrow; $max_id++; my $memberName = $this->getUsernameForUser($user); my $regOptions = { id => $max_id, auth => 'manual', confirmed => 1, mnethostid => 1, username => $memberName, password => md5_hex($user->{passwd}), firstname => $user->{fname}, lastname => $user->{lname}, email => $user->{email}, }; my @f = keys %$regOptions; $this->sql("INSERT INTO $this->{tblprefix}user SET " . join(',', map { "$_=?" } @f), map { $regOptions->{$_} } @f) or return; $script_uid = $this->_get_script_uid($user); $this->fixScriptUID($user, $script_uid); } if ($this->{assign_user_role}) { my $sth = $this->sql("SELECT id FROM $this->{tblprefix}role WHERE name=?", $this->{assign_user_role}) or return; if (my $roleid = $sth->fetchrow) { $this->sql("INSERT INTO $this->{tblprefix}role_assignments SET roleid=?, contextid=?, userid=?", $roleid, 1, $script_uid) or return; } } return $script_uid; } sub updateUser { my $this = shift; my $user = shift; if (my $script_uid = $this->_get_script_uid($user)) { my $memberName = $this->getUsernameForUser($user); $this->sql("UPDATE $this->{tblprefix}user SET username=?,password=?,firstname=?,lastname=?,email=? WHERE id=?", $memberName, md5_hex($user->{passwd}), $user->{fname}, $user->{lname}, $user->{email}, $script_uid) or return; $this->fixScriptUID($user, $script_uid); } else { $this->addUser($user); } return; } sub deleteUser { my $this = shift; my $user = shift; my $script_uid = $this->_get_script_uid($user) or return; my @queries = ( "DELETE FROM $this->{tblprefix}course_display WHERE userid=?", "DELETE FROM $this->{tblprefix}event WHERE userid=?", "DELETE FROM $this->{tblprefix}log WHERE userid=?", "DELETE FROM $this->{tblprefix}message_contacts WHERE userid=?", "DELETE FROM $this->{tblprefix}user_preferences WHERE userid=?", "DELETE FROM $this->{tblprefix}user_lastaccess WHERE userid=?", "DELETE FROM $this->{tblprefix}scale WHERE userid=?", "DELETE FROM $this->{tblprefix}scale_history WHERE userid=?", "DELETE FROM $this->{tblprefix}stats_user_daily WHERE userid=?", "DELETE FROM $this->{tblprefix}stats_user_weekly WHERE userid=?", "DELETE FROM $this->{tblprefix}stats_user_monthly WHERE userid=?", "DELETE FROM $this->{tblprefix}post WHERE userid=?", "DELETE FROM $this->{tblprefix}role_assignments WHERE userid=?", "DELETE FROM $this->{tblprefix}role_sortorder WHERE userid=?", "DELETE FROM $this->{tblprefix}user_info_data WHERE userid=?", "DELETE FROM $this->{tblprefix}mnet_enrol_assignments WHERE userid=?", "DELETE FROM $this->{tblprefix}mnet_log WHERE userid=?", "DELETE FROM $this->{tblprefix}mnet_session WHERE userid=?", "DELETE FROM $this->{tblprefix}events_queue WHERE userid=?", "DELETE FROM $this->{tblprefix}grade_grades WHERE userid=?", "DELETE FROM $this->{tblprefix}grade_grades_history WHERE userid=?", "DELETE FROM $this->{tblprefix}grade_import_values WHERE userid=?", "DELETE FROM $this->{tblprefix}tag WHERE userid=?", "DELETE FROM $this->{tblprefix}groups_members WHERE userid=?", "DELETE FROM $this->{tblprefix}user_private_key WHERE userid=?", "DELETE FROM $this->{tblprefix}webdav_locks WHERE userid=?", "DELETE FROM $this->{tblprefix}user WHERE id=?", ); for (@queries) { $this->sql($_, $script_uid) or return; } $this->fixScriptUID($user, undef); return; } sub suspendUser { my $this = shift; my $user = shift; $this->deleteUser($user); return; } sub getUsernameForUser { my $this = shift; my $user = shift; $user->{email}; } sub _get_script_uid { my $this = shift; my $user = shift; my $script_uid = $this->getScriptUID($user); if ($script_uid) { my $sth = $this->sql("SELECT id FROM $this->{tblprefix}user WHERE id=?", $script_uid) or return; $this->fixScriptUID($user, undef) unless $sth->rows > 0; } my $sth = $this->sql("SELECT id FROM $this->{tblprefix}user WHERE email=?", $user->{email}) or return; $script_uid = $sth->fetchrow_array if $sth->rows > 0; $script_uid; } 1; 