-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocallib.php
454 lines (390 loc) · 17.3 KB
/
locallib.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local libraries.
*
* @package local_ciabinitialsprofilepics
* @copyright 2018 Paul Vaughan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/local/ciabinitialsprofilepics/vendor/autoload.php');
use Intervention\Image\ImageManager;
// O or 1. TODO: config option. Probably.
define('CIABINITIALSPROFILEPICS_BG_ALPHA', 0);
// This can be 'gd' or 'imagick', but Moodle already requires gd, so use that.
define('CIABINITIALSPROFILEPICS_DRIVER', 'gd');
// Default size.
define('CIABINITIALSPROFILEPICS_SIZE', 200);
// Shapes options.
define('CIABINITIALSPROFILEPICS_SHAPES_CIRCLE', 'circle');
define('CIABINITIALSPROFILEPICS_SHAPES_SQUARE', 'square');
define('CIABINITIALSPROFILEPICS_SHAPES_ROUNDEDSQUARE', 'rounded_square');
define('CIABINITIALSPROFILEPICS_SHAPES_UPSLASH', 'upslash');
define('CIABINITIALSPROFILEPICS_SHAPES_DOWNSLASH', 'downslash');
define('CIABINITIALSPROFILEPICS_SHAPES_LEFTSLASH', 'leftslash');
define('CIABINITIALSPROFILEPICS_SHAPES_RIGHTSLASH', 'rightslash');
define('CIABINITIALSPROFILEPICS_SHAPES_HEXAGON_HORIZONTAL', 'hexagon_horizontal');
define('CIABINITIALSPROFILEPICS_SHAPES_HEXAGON_VERTICAL', 'hexagon_vertical');
define('CIABINITIALSPROFILEPICS_SHAPES_STAR', 'star');
define('CIABINITIALSPROFILEPICS_SHAPES', [
CIABINITIALSPROFILEPICS_SHAPES_CIRCLE,
CIABINITIALSPROFILEPICS_SHAPES_SQUARE,
CIABINITIALSPROFILEPICS_SHAPES_ROUNDEDSQUARE,
CIABINITIALSPROFILEPICS_SHAPES_UPSLASH,
CIABINITIALSPROFILEPICS_SHAPES_DOWNSLASH,
CIABINITIALSPROFILEPICS_SHAPES_LEFTSLASH,
CIABINITIALSPROFILEPICS_SHAPES_RIGHTSLASH,
CIABINITIALSPROFILEPICS_SHAPES_HEXAGON_HORIZONTAL,
CIABINITIALSPROFILEPICS_SHAPES_HEXAGON_VERTICAL,
CIABINITIALSPROFILEPICS_SHAPES_STAR,
]);
// If it's not a circle, it's square, or a rounded square.
define('CIABINITIALSPROFILEPICS_SHAPE', CIABINITIALSPROFILEPICS_SHAPES_SQUARE);
// Font to use.
define('CIABINITIALSPROFILEPICS_FONT', 'opensans-regular.ttf');
// Colours.
define('CIABINITIALSPROFILEPICS_COLOURS', [
// Default generic colours.
"#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50",
"#f1c40f", "#e67e22", "#e74c3c", "#dce0e1", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d",
]);
// Colours used to darken and lighten other colours by layering on top.
define('CIABINITIALSPROFILEPICS_COLOUR_DARKEN', [0, 0, 0, .3]);
define('CIABINITIALSPROFILEPICS_COLOUR_LIGHTEN', [255, 255, 255, .2]);
// Pool of letters (and numbers, symbols as appropriate) for testing and if no initials supplied.
define('CIABINITIALSPROFILEPICS_INITIALSPOOL', 'ABCDE12345!?#@¥£€$¢₡₢₣₤₥₦₧₨₩₪₫₭₮₯₹·');
// Font sizes.
define('CIABINITIALSPROFILEPICS_FONTSIZE', [
'1.4' => get_string('extralarge', 'local_ciabinitialsprofilepics'),
'1.2' => get_string('large', 'local_ciabinitialsprofilepics'),
'1.0' => get_string('medium', 'local_ciabinitialsprofilepics'),
'0.8' => get_string('small', 'local_ciabinitialsprofilepics'),
]);
// Font alpha-transparency.
define('CIABINITIALSPROFILEPICS_FONTALPHA', [
'0.2' => '20%',
'0.3' => '30%',
'0.4' => '40%',
'0.5' => '50%',
'0.6' => '60%',
'0.7' => '70%',
'0.8' => '80%',
]);
/**
* Checks for the existence of a profile picture for the user.
* @param core\event\user_created or core\event\user_updated event
* @return bool
*/
function ciabinitialsprofilepics_profile_picture_exists($event) : bool {
global $CFG, $DB;
// If not enabled, bail.
if (get_config('local_ciabinitialsprofilepics', 'enabled') == '0') {
return true;
}
require_once($CFG->libdir . '/gdlib.php');
$user = $DB->get_record('user', ['id' => $event->relateduserid]);
if ($user->picture == 0) {
return ciabinitialsprofilepics_create_and_save_to_profile($user);
}
return true;
}
/**
* Takes the user object and creates an image from the user's initials, saves it to their profile.
* @param object user Moodle user object.
* @return bool
*/
function ciabinitialsprofilepics_create_and_save_to_profile($user) : bool {
global $CFG, $DB, $usernew;
$initials = ciabinitialsprofilepics_get_initials_from_user($user);
// TODO: config option.
$shape = get_config('local_ciabinitialsprofilepics', 'shape');
$colour = ciabinitialsprofilepics_get_working_colour($initials);
$size = 500;
$fontsize = get_config('local_ciabinitialsprofilepics', 'fontsize');
$fontalpha = get_config('local_ciabinitialsprofilepics', 'fontalpha');
// Initials, shape, colour, size, fontsize, fontalpha.
$canvas = ciabinitialsprofilepics_generate_profile_pic($initials, $shape, $colour, $size, $fontsize, $fontalpha);
$tempfile = ciabinitialsprofilepics_save_to_disk($canvas, $user);
$newpicture = (int) process_new_icon(context_user::instance($user->id, MUST_EXIST), 'user', 'icon', 0, $tempfile);
$DB->set_field('user', 'picture', $newpicture, ['id' => $user->id]);
$user->picture = $newpicture;
return true;
}
/**
* Performs the action of saving the canvas object to disk, temporarily.
* @param object canvas Canvas image object.
* @param object user User object.
* @return path to temporary file and folder.
*/
function ciabinitialsprofilepics_save_to_disk($canvas, $user) {
$tempfile = make_request_directory() . $user->id . '.png';
$canvas->save($tempfile);
return $tempfile;
}
/**
* Creates an image and returns it as 'data-url' to be directly displayed on-screen.
* @param array initials An array containing two or more elements which are each a single-character string.
* @param string shape One of 'circle', 'square', or 'roundedsquare' at the time of writing.
* @param string colour Valid hex colour string with leading '#', e.g. '#f51' or '#3fd7dd'.
* @param int size Size in pixels of image width and height.
* @param float fontsize A multiplication factor for font size.
* @param float fontalpha Alpha-transparency.
* @return string 'data-url' encoded image.
*/
function ciabinitialsprofilepics_create_and_dump_onscreen(
array $initials = null,
string $shape = null,
string $colour = null,
int $size = null,
float $fontsize = null,
float $fontalpha = null
) : string {
$canvas = ciabinitialsprofilepics_generate_profile_pic($initials, $shape, $colour, $size, $fontsize, $fontalpha);
return (string) $canvas->encode('data-url');
}
/**
* Takes a Moodle user object and creates/returns an array of single letters.
* @param object user User object for the created/updated user.
* @return array Initials.
* TODO: potentially limit this to two initials only.
*/
function ciabinitialsprofilepics_get_initials_from_user($user) : array {
$names = explode(' ', fullname($user));
$initials = [];
foreach ($names as $name) {
$encoding = mb_detect_encoding($name);
$initials[] = mb_strtoupper(mb_substr($name, 0, 1), $encoding);
}
return $initials;
}
/**
* Generates a profile image from the user's initials.
* @param array initials An array containing two or more elements which are each a single-character string.
* @param string shape One of 'circle', 'square', or 'rounded_square' at the time of writing.
* @param string colour Valid hex colour string with leading '#', e.g. '#f51' or '#3fd7dd'.
* @param int size Size in pixels of image width and height
* @param float fontsize A multiplication factor for font size.
* @param float fontalpha Alpha-transparency.
* @return Intervention image object
*/
function ciabinitialsprofilepics_generate_profile_pic(
array $initials = null,
string $shape = null,
string $colour = null,
int $size = null,
float $fontsize = null,
float $fontalpha = null
) {
// Should have all decent params passed in, so do brief sanity checks only.
$initials = $initials ?? ciabinitialsprofilepics_get_random_initials();
$shape = $shape ?? CIABINITIALSPROFILEPICS_SHAPE;
$colour = $colour ?? ciabinitialsprofilepics_get_random_colour();
$size = $size ?? CIABINITIALSPROFILEPICS_SIZE;
$fontsize = $fontsize ?? 1.4;
$fontalpha = $fontalpha ?? 0.2;
global $CFG;
$image = new ImageManager(['driver' => CIABINITIALSPROFILEPICS_DRIVER]);
$hsize = (int) ($size / 2);
if ($shape == CIABINITIALSPROFILEPICS_SHAPES_CIRCLE) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$canvas->circle($size - 1, $size / 2, $size / 2, function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_ROUNDEDSQUARE) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$canvas->circle($hsize, $hsize / 2, $hsize / 2, function ($draw) use ($colour) {
$draw->background($colour);
});
$canvas->circle($hsize, $size - ($hsize / 2) - 1, $hsize / 2, function ($draw) use ($colour) {
$draw->background($colour);
});
$canvas->circle($hsize, $size - ($hsize / 2), $size - ($hsize / 2), function ($draw) use ($colour) {
$draw->background($colour);
});
$canvas->circle($hsize, ($hsize / 2), $size - ($hsize / 2), function ($draw) use ($colour) {
$draw->background($colour);
});
$rsize = $hsize / 2;
$points = [
$rsize, 0,
$size - $rsize, 0,
$size, $rsize,
$size, $size - $rsize,
$size - $rsize, $size,
$rsize, $size,
0, $size - $rsize,
0, $rsize
];
$canvas->polygon($points, function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_SQUARE) {
$canvas = $image->canvas($size, $size, $colour);
// If we want a border here, I think we'll need to make the canvas transparent and draw a square.
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_UPSLASH) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$canvas->polygon([0, $hsize, $size, 0, $size, $hsize, 0, $size], function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_DOWNSLASH) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$canvas->polygon([0, 0, $size, $hsize, $size, $size, 0, $hsize], function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_LEFTSLASH) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$canvas->polygon([0, 0, $hsize, 0, $size, $size, $hsize, $size], function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_RIGHTSLASH) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$canvas->polygon([$hsize, 0, $size, 0, $hsize, $size, 0, $size], function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_HEXAGON_HORIZONTAL) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$theta = 0; // Hexagon with flat sides at the top and bottom.
$points = 6;
for ($j = 0; $j <= $points - 1; $j++) {
$x[$j] = (int) ($hsize * cos(2 * pi() * $j / $points + $theta) + $hsize);
$y[$j] = (int) ($hsize * sin(2 * pi() * $j / $points + $theta) + $hsize);
}
$points = [
$x[0], $y[0],
$x[1], $y[1],
$x[2], $y[2],
$x[3], $y[3],
$x[4], $y[4],
$x[5], $y[5],
];
$canvas->polygon($points, function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_HEXAGON_VERTICAL) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
$theta = pi() / 2; // Hexagon with flat sides at the sides.
$points = 6;
for ($j = 0; $j <= $points - 1; $j++) {
$x[$j] = (int) ($hsize * cos(2 * pi() * $j / $points + $theta) + $hsize);
$y[$j] = (int) ($hsize * sin(2 * pi() * $j / $points + $theta) + $hsize);
}
$points = [
$x[0], $y[0],
$x[1], $y[1],
$x[2], $y[2],
$x[3], $y[3],
$x[4], $y[4],
$x[5], $y[5],
];
$canvas->polygon($points, function ($draw) use ($colour) {
$draw->background($colour);
});
} else if ($shape == CIABINITIALSPROFILEPICS_SHAPES_STAR) {
$canvas = $image->canvas($size, $size, [255, 255, 255, CIABINITIALSPROFILEPICS_BG_ALPHA]);
// TODO: how to make the star point 'up' properly.
$theta = 2.2;
$points = [];
$numpoints = 5;
// Generates points.
for ($j = 0; $j <= $numpoints - 1; $j++) {
$points[] = (int) ($hsize * cos(2 * pi() * $j / $numpoints + $theta) + $hsize);
$points[] = (int) ($hsize * sin(2 * pi() * $j / $numpoints + $theta) + $hsize);
}
$canvas->polygon([$points[0], $points[1], $points[4], $points[5], $points[8], $points[9], $points[2], $points[3], $points[6], $points[7]], function ($draw) use ($colour) {
$draw->background($colour);
});
// Fill in the centre.
$canvas->fill($colour, $hsize, $hsize);
} else {
$canvas = $image->canvas($size, $size, $colour);
}
// Draw the text.
for ($j = 0; $j <= count($initials) - 1; $j++) {
// For now, hard-coded limit of 2. TODO: future development to use more.
if ($j == 2) {
break;
}
$canvas->text($initials[$j], ($size * .35) * ($j + 1), ($size * .5), function ($font) use ($size, $fontsize, $fontalpha) {
$font->file(__DIR__ . '/fonts/' . CIABINITIALSPROFILEPICS_FONT);
$font->size($size * $fontsize);
$font->color([255, 255, 255, $fontalpha]);
$font->valign('middle');
$font->align('center');
$font->angle(-15);
});
}
// Resizing.
if ($size != $size) {
$canvas->resize($size, $size);
}
return $canvas;
}
/**
* Gets a random colour from the pool of colours.
*/
function ciabinitialsprofilepics_get_random_colour() {
return CIABINITIALSPROFILEPICS_COLOURS[rand(0, count(CIABINITIALSPROFILEPICS_COLOURS) - 1)];
}
/**
* Gets random initials.
* @param int number The number of initials required; defaults to 2.
*/
function ciabinitialsprofilepics_get_random_initials(int $number = null) {
$number = $number ?? 2;
$len = strlen(CIABINITIALSPROFILEPICS_INITIALSPOOL) - 1;
$out = [];
for ($j = 1; $j <= $number; $j++) {
$out[] = substr(CIABINITIALSPROFILEPICS_INITIALSPOOL, rand(0, $len), 1);
}
return $out;
}
/**
* Gets the working colour choice, depending on settings.
* @param array initials User's initials.
*/
function ciabinitialsprofilepics_get_working_colour(array $initials = null) {
$initials = $initials ?? ['A', 'Z'];
// Forced to a specific colour.
if ($forcecolour = get_config('local_ciabinitialsprofilepics', 'forcecolour')) {
$colour = $forcecolour;
} else if (get_config('local_ciabinitialsprofilepics', 'randomcolour')) {
// Random choice.
$colour = ciabinitialsprofilepics_get_random_colour();
} else {
// One based on the first letter of your names.
$colour = ciabinitialsprofilepics_get_colour_from_initials($initials);
}
return $colour;
}
/**
* Gets the colour based on the user's initials.
* @param array initials User's initials.
*/
function ciabinitialsprofilepics_get_colour_from_initials(array $initials = null) {
$initials = $initials ?? ['A', 'Z'];
$colour = false;
$charindex = ord($initials[0]);
$colourindex = $charindex % count(CIABINITIALSPROFILEPICS_COLOURS);
if ($colourindex >= 0) {
$colour = CIABINITIALSPROFILEPICS_COLOURS[$colourindex];
}
// If no colour selected for some reason, pick a random one.
if (!$colour) {
$colour = ciabinitialsprofilepics_get_random_colour();
}
return $colour;
}