-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.php
144 lines (124 loc) · 5.99 KB
/
settings.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
defined('MOODLE_INTERNAL') || die;
if ($hassiteconfig) {
// Kreiramo glavnu kategoriju za MailChimp Sync
$ADMIN->add('localplugins', new admin_category('local_mailchimpsync_category', get_string('pluginname', 'local_mailchimpsync')));
// Glavna stranica postavki
$settings = new admin_settingpage('local_mailchimpsync_settings', get_string('settings', 'local_mailchimpsync'));
if ($ADMIN->fulltree) {
// API Key
$settings->add(new admin_setting_configtext(
'local_mailchimpsync/apikey',
get_string('apikey', 'local_mailchimpsync'),
get_string('apikey_desc', 'local_mailchimpsync'),
'',
PARAM_TEXT
));
$api_key = get_config('local_mailchimpsync', 'apikey');
$lists = array();
$merge_fields = array();
if (!empty($api_key)) {
$cache = \cache::make('local_mailchimpsync', 'apicalls');
$lists = $cache->get('lists');
if ($lists === false) {
$api = new \local_mailchimpsync\api();
$lists = $api->get_lists();
$cache->set('lists', $lists);
}
// Only add these settings if API key is set and lists are retrieved
if (!empty($lists)) {
// Default List
$settings->add(new admin_setting_configtext(
'local_mailchimpsync/default_list_id',
get_string('default_list_id', 'local_mailchimpsync'),
get_string('default_list_id_desc', 'local_mailchimpsync'),
'',
PARAM_TEXT
));
// Cohort to List Mapping
$cohorts = $DB->get_records_menu('cohort', null, 'name', 'id, name');
if (!empty($cohorts)) {
foreach ($cohorts as $cohort_id => $cohort_name) {
$settings->add(new admin_setting_configselect(
"local_mailchimpsync/cohort_{$cohort_id}_list",
get_string('cohort_list', 'local_mailchimpsync', $cohort_name),
get_string('cohort_list_desc', 'local_mailchimpsync'),
'',
$lists
));
}
}
// Field Mapping
$userfields = [
'firstname' => get_string('firstname'),
'lastname' => get_string('lastname'),
'email' => get_string('email'),
'city' => get_string('city'),
'country' => get_string('country'),
'lang' => get_string('language'),
'timezone' => get_string('timezone'),
'phone1' => get_string('phone'),
'phone2' => get_string('phone2'),
];
// Add custom profile fields
$custom_fields = $DB->get_records('user_info_field', null, '', 'id, name, shortname');
foreach ($custom_fields as $field) {
$userfields['profile_field_'.$field->shortname] = $field->name;
}
// MailChimp merge fields
if (!empty($lists)) {
$first_list_id = array_keys($lists)[0];
$merge_fields = $cache->get('merge_fields_'.$first_list_id);
if ($merge_fields === false) {
$api = new \local_mailchimpsync\api();
$merge_fields = $api->get_merge_fields($first_list_id);
$cache->set('merge_fields_'.$first_list_id, $merge_fields);
}
}
$merge_field_options = array();
foreach ($merge_fields as $field) {
$merge_field_options[$field['tag']] = $field['name'];
}
$settings->add(new admin_setting_configmultiselect(
'local_mailchimpsync/field_mapping',
get_string('field_mapping', 'local_mailchimpsync'),
get_string('field_mapping_desc', 'local_mailchimpsync'),
['FNAME' => 'firstname', 'LNAME' => 'lastname', 'EMAIL' => 'email'],
array_combine(array_keys($userfields), array_keys($userfields))
));
$settings->add(new admin_setting_configcheckbox(
'local_mailchimpsync/use_sync_field',
get_string('use_sync_field', 'local_mailchimpsync'),
get_string('use_sync_field_desc', 'local_mailchimpsync'),
0
));
$custom_fields = $DB->get_records_menu('user_info_field', null, '', 'id, name');
$settings->add(new admin_setting_configselect(
'local_mailchimpsync/sync_field',
get_string('sync_field', 'local_mailchimpsync'),
get_string('sync_field_desc', 'local_mailchimpsync'),
'',
$custom_fields
));
}
}
}
// Dodajemo glavnu stranicu postavki u kategoriju
$ADMIN->add('local_mailchimpsync_category', $settings);
// Dodajemo ostale stranice u kategoriju
$ADMIN->add('local_mailchimpsync_category', new admin_externalpage(
'local_mailchimpsync_sync',
get_string('sync_page_title', 'local_mailchimpsync'),
new moodle_url('/local/mailchimpsync/admin/sync.php')
));
$ADMIN->add('local_mailchimpsync_category', new admin_externalpage(
'local_mailchimpsync_stats',
get_string('stats_page_title', 'local_mailchimpsync'),
new moodle_url('/local/mailchimpsync/stats.php')
));
$ADMIN->add('local_mailchimpsync_category', new admin_externalpage(
'local_mailchimpsync_privacy',
get_string('privacy_policy', 'local_mailchimpsync'),
new moodle_url('/local/mailchimpsync/privacy_policy.php')
));
}