-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodle_rules.module
94 lines (83 loc) · 2.43 KB
/
moodle_rules.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require __DIR__ . '/vendor/autoload.php';
function moodle_rules_rules_action_info() {
$actions = [
'moodle_rules_action_insert_user_data' => [
'label' => t('Insert user and civicrm data to moodle'),
'group' => t('Moodle Tools'),
'parameter' => [
'id' => [
'type' => 'text',
'label' => 'The Drupal user\'s uid'
],
'user_name' => [
'type' => 'text',
'label' => 'The Drupal user\'s username'
],
'first_name' => [
'type' => 'text',
'label' => 'The Drupal user\'s first name in CiviCRM'
],
'last_name' => [
'type' => 'text',
'label' => 'The Drupal user\'s last name in CiviCRM'
],
'email' => [
'type' => 'text',
'label' => 'The Drupal user\'s email address'
]
]
]
];
return $actions;
}
function moodle_rules_action_insert_user_data($id, $username, $first_name, $last_name, $email) {
// Read settings from .env
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$user = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
$db = getenv('DB_NAME');
$users_table = getenv('DB_USERS_TABLE');
// Connect to Moodle DB.
$connection = new mysqli("127.0.0.1", $user, $password, $db);
// Check connection.
if (mysqli_connect_errno()) {
watchdog('moodle_rules', t('Couldn\'t connect to Moodle database. ' . mysqli_connect_error()), array(), WATCHDOG_ERROR);
return;
}
$query = "SELECT id,idnumber FROM $users_table WHERE idnumber = $id";
$row = NULL;
if ($result = $connection->query($query)) {
$row = $result->fetch_row();
$result->close();
}
if ($row == NULL) {
$query = "INSERT INTO $users_table
(idnumber, auth, confirmed, mnethostid, username, firstname, lastname, email, city, country)
VALUES
(?,?,?,?,?,?,?,?,?,?)";
$statement = $connection->prepare($query);
$statement->bind_param(
"ssssssssss",
$id,
$auth = 'drupalservices',
$confirmed = '1',
$mnethostid = '1',
$username,
$first_name,
$last_name,
$email,
$city = 'none',
$country = 'no'
);
try {
$statement->execute();
watchdog('moodle_rules', t("Trying to add $email to moodle..."), array(), WATCHDOG_INFO);
} catch (Exception $e) {
watchdog('moodle_rules', t('Something went wrong on query execution.'), array(), WATCHDOG_ERROR);
}
watchdog('moodle_rules', t("$email has been added to moodle."), array(), WATCHDOG_INFO);
}
mysqli_close($connection);
}