Completed
Push — gcconnex ( 3cc45a...fd60b7 )
by
unknown
29:08 queued 11:52
created

start.php ➔ get_time_data()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 4
nop 2
dl 0
loc 33
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
elgg_register_event_handler('init', 'system', 'gccollab_stats_init', 0);
4
5
function gccollab_stats_init() {
6
	elgg_register_page_handler('stats', 'stats_page_handler');
7
	if( strpos(elgg_get_site_entity()->name, 'collab') !== false ){
8
		elgg_register_plugin_hook_handler('public_pages', 'walled_garden', 'gccollab_stats_public_page');
9
	}
10
11
	elgg_ws_expose_function(
12
        "member.stats",
13
        "get_member_data",
14
        array(
15
        	"type" => array('type' => 'string', 'required' => true),
16
        	"lang" => array('type' => 'string', 'required' => false, 'default' => 'en')
17
        ),
18
        'Exposes member data for use with dashboard',
19
        'GET',
20
        false,
21
        false
22
	);
23
24
	elgg_ws_expose_function(
25
        "site.stats",
26
        "get_site_data",
27
        array(
28
        	"type" => array('type' => 'string', 'required' => true),
29
        	"lang" => array('type' => 'string', 'required' => false, 'default' => 'en')
30
        ),
31
        'Exposes site data for use with dashboard',
32
        'GET',
33
        false,
34
        false
35
	);
36
37
	elgg_ws_expose_function(
38
        "time.stats",
39
        "get_time_data",
40
        array(
41
        	"type" => array('type' => 'string', 'required' => true),
42
        	"lang" => array('type' => 'string', 'required' => false, 'default' => 'en')
43
        ),
44
        'Exposes entity data over time for use with dashboard',
45
        'GET',
46
        false,
47
        false
48
	);
49
}
50
51
function gccollab_stats_public_page($hook, $handler, $return, $params){
52
	$pages = array('stats');
53
	return array_merge($pages, $return);
54
}
55
56
function stats_page_handler($page) {
57
	$base = elgg_get_plugins_path() . 'gccollab_stats/pages/gccollab_stats';
58
	$page = (strpos(elgg_get_site_entity()->name, 'collab') !== false) ? 'gccollab' : 'gcconnex';
59
	require_once "$base/$page.php";
60
	return true;
61
}
62
63
function get_member_data($type, $lang) {
64
	$data = array();
65
	ini_set("memory_limit", -1);
66
	elgg_set_ignore_access(true);
67
	$dbprefix = elgg_get_config('dbprefix');
68
69
	switch($type) {
70
71
	case 'all':
72
		$users = elgg_get_entities(array(
73
			'type' => 'user',
74
			'limit' => 0
75
		));
76
77
		if ($lang == 'fr'){
78
			$users_types = array('federal' => 'féderal', 'academic' => 'milieu universitaire', 'student' => 'étudiant', 'provincial' => 'provincial', 'municipal' => 'municipale', 'international' => 'international', 'ngo' => 'ngo', 'community' => 'collectivité', 'business' => 'entreprise', 'media' => 'média', 'retired' => 'retraité(e)', 'other' => 'autre');
79
80
			foreach($users as $key => $obj){
81
				$data[$users_types[$obj->user_type]] = isset( $data[$users_types[$obj->user_type]] ) ? $data[$users_types[$obj->user_type]] + 1 : 1;
82
			}
83 View Code Duplication
		} else {
84
			foreach($users as $key => $obj){
85
				$data[$obj->user_type] = isset( $data[$obj->user_type] ) ? $data[$obj->user_type] + 1 : 1;
86
			}
87
		}
88
		break;
89
90
	case 'federal':
91
		$users = elgg_get_entities_from_metadata(array(
92
			'type' => 'user',
93
			'metadata_name_value_pairs' => array(
94
				array('name' => 'user_type', 'value' => 'federal'),
95
			),
96
			'limit' => 0
97
		));
98
99
		if ($lang == 'fr'){
100
			$deptObj = elgg_get_entities(array(
101
			   	'type' => 'object',
102
			   	'subtype' => 'federal_departments',
103
			));
104
			$depts = get_entity($deptObj[0]->guid);
105
			$federal_departments = json_decode($depts->federal_departments_fr, true);
106
107
			foreach($users as $key => $obj){
108
				$data[$federal_departments[$obj->federal]] = isset( $data[$federal_departments[$obj->federal]] ) ? $data[$federal_departments[$obj->federal]] + 1 : 1;
109
			}
110 View Code Duplication
		} else {
111
			foreach($users as $key => $obj){
112
				$data[$obj->federal] = isset( $data[$obj->federal] ) ? $data[$obj->federal] + 1 : 1;
113
			}
114
		}
115
		break;
116
117
	case 'academic':
118
		$users = elgg_get_entities_from_metadata(array(
119
			'type' => 'user',
120
			'metadata_name_value_pairs' => array(
121
				array('name' => 'user_type', 'value' => 'academic'),
122
			),
123
			'limit' => 0
124
		));
125
		foreach($users as $key => $obj){
126
			$data[$obj->institution]['total'] = isset( $data[$obj->institution]['total'] ) ? $data[$obj->institution]['total'] + 1 : 1;
127 View Code Duplication
			if($obj->university){
128
				$data[$obj->institution][$obj->university] = isset( $data[$obj->institution][$obj->university] ) ? $data[$obj->institution][$obj->university] + 1 : 1;
129
			}
130 View Code Duplication
			if($obj->college){
131
				$data[$obj->institution][$obj->college] = isset( $data[$obj->institution][$obj->college] ) ? $data[$obj->institution][$obj->college] + 1 : 1;
132
			}
133
		}
134
		break;
135
136
	case 'student':
137
		$users = elgg_get_entities_from_metadata(array(
138
			'type' => 'user',
139
			'metadata_name_value_pairs' => array(
140
				array('name' => 'user_type', 'value' => 'student'),
141
			),
142
			'limit' => 0
143
		));
144
		foreach($users as $key => $obj){
145
			$data[$obj->institution]['total'] = isset( $data[$obj->institution]['total'] ) ? $data[$obj->institution]['total'] + 1 : 1;
146 View Code Duplication
			if($obj->university){
147
				$data[$obj->institution][$obj->university] = isset( $data[$obj->institution][$obj->university] ) ? $data[$obj->institution][$obj->university] + 1 : 1;
148
			}
149 View Code Duplication
			if($obj->college){
150
				$data[$obj->institution][$obj->college] = isset( $data[$obj->institution][$obj->college] ) ? $data[$obj->institution][$obj->college] + 1 : 1;
151
			}
152 View Code Duplication
			if($obj->highschool){
153
				$data[$obj->institution][$obj->highschool] = isset( $data[$obj->institution][$obj->highschool] ) ? $data[$obj->institution][$obj->highschool] + 1 : 1;
154
			}
155
		}
156
		break;
157
158 View Code Duplication
	case 'university':
159
		$users = elgg_get_entities_from_metadata(array(
160
			'type' => 'user',
161
			'metadata_name_value_pairs' => array(
162
				array('name' => 'user_type', 'value' => 'academic'),
163
				array('name' => 'institution', 'value' => 'university'),
164
			),
165
			'limit' => 0
166
		));
167
		foreach($users as $key => $obj){
168
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
169
			$data[$obj->university] = isset( $data[$obj->university] ) ? $data[$obj->university] + 1 : 1;
170
		}
171
		break;
172
173 View Code Duplication
	case 'college':
174
		$users = elgg_get_entities_from_metadata(array(
175
			'type' => 'user',
176
			'metadata_name_value_pairs' => array(
177
				array('name' => 'user_type', 'value' => 'academic'),
178
				array('name' => 'institution', 'value' => 'college'),
179
			),
180
			'limit' => 0
181
		));
182
		foreach($users as $key => $obj){
183
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
184
			$data[$obj->college] = isset( $data[$obj->college] ) ? $data[$obj->college] + 1 : 1;
185
		}
186
		break;
187
188 View Code Duplication
	case 'highschool':
189
		$users = elgg_get_entities_from_metadata(array(
190
			'type' => 'user',
191
			'metadata_name_value_pairs' => array(
192
				array('name' => 'user_type', 'value' => 'student'),
193
				array('name' => 'institution', 'value' => 'highschool'),
194
			),
195
			'limit' => 0
196
		));
197
		foreach($users as $key => $obj){
198
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
199
			$data[$obj->highschool] = isset( $data[$obj->highschool] ) ? $data[$obj->highschool] + 1 : 1;
200
		}
201
		break;
202
203
	case 'provincial':
204
		$users = elgg_get_entities_from_metadata(array(
205
			'type' => 'user',
206
			'metadata_name_value_pairs' => array(
207
				array('name' => 'user_type', 'value' => 'provincial'),
208
			),
209
			'limit' => 0
210
		));
211
212
		if ($lang == 'fr'){
213
			$provObj = elgg_get_entities(array(
214
			   	'type' => 'object',
215
			   	'subtype' => 'provinces',
216
			));
217
			$provs = get_entity($provObj[0]->guid);
218
			$provincial_departments = json_decode($provs->provinces_fr, true);
219
220
			$minObj = elgg_get_entities(array(
221
			   	'type' => 'object',
222
			   	'subtype' => 'ministries',
223
			));
224
			$mins = get_entity($minObj[0]->guid);
225
			$ministries = json_decode($mins->ministries_fr, true);
226
227
			foreach($users as $key => $obj){
228
				$data[$provincial_departments[$obj->provincial]]['total'] = isset( $data[$provincial_departments[$obj->provincial]]['total'] ) ? $data[$provincial_departments[$obj->provincial]]['total'] + 1 : 1;
229
				$data[$provincial_departments[$obj->provincial]][$ministries[$obj->provincial][$obj->ministry]] = isset( $data[$provincial_departments[$obj->provincial]][$ministries[$obj->provincial][$obj->ministry]] ) ? $data[$provincial_departments[$obj->provincial]][$ministries[$obj->provincial][$obj->ministry]] + 1 : 1;
230
			}
231
		} else {
232
			foreach($users as $key => $obj){
233
				$data[$obj->provincial]['total'] = isset( $data[$obj->provincial]['total'] ) ? $data[$obj->provincial]['total'] + 1 : 1;
234
				$data[$obj->provincial][$obj->ministry] = isset( $data[$obj->provincial][$obj->ministry] ) ? $data[$obj->provincial][$obj->ministry] + 1 : 1;
235
			}
236
		}
237
		break;
238
239 View Code Duplication
	case 'municipal':
240
		$users = elgg_get_entities_from_metadata(array(
241
			'type' => 'user',
242
			'metadata_name_value_pairs' => array(
243
				array('name' => 'user_type', 'value' => 'municipal')
244
			),
245
			'limit' => 0
246
		));
247
		foreach($users as $key => $obj){
248
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
249
			$data[$obj->municipal] = isset( $data[$obj->municipal] ) ? $data[$obj->municipal] + 1 : 1;
250
		}
251
		break;
252
253 View Code Duplication
	case 'international':
254
		$users = elgg_get_entities_from_metadata(array(
255
			'type' => 'user',
256
			'metadata_name_value_pairs' => array(
257
				array('name' => 'user_type', 'value' => 'international')
258
			),
259
			'limit' => 0
260
		));
261
		foreach($users as $key => $obj){
262
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
263
			$data[$obj->international] = isset( $data[$obj->international] ) ? $data[$obj->international] + 1 : 1;
264
		}
265
		break;
266
267 View Code Duplication
	case 'ngo':
268
		$users = elgg_get_entities_from_metadata(array(
269
			'type' => 'user',
270
			'metadata_name_value_pairs' => array(
271
				array('name' => 'user_type', 'value' => 'ngo')
272
			),
273
			'limit' => 0
274
		));
275
		foreach($users as $key => $obj){
276
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
277
			$data[$obj->ngo] = isset( $data[$obj->ngo] ) ? $data[$obj->ngo] + 1 : 1;
278
		}
279
		break;
280
281 View Code Duplication
	case 'community':
282
		$users = elgg_get_entities_from_metadata(array(
283
			'type' => 'user',
284
			'metadata_name_value_pairs' => array(
285
				array('name' => 'user_type', 'value' => 'community')
286
			),
287
			'limit' => 0
288
		));
289
		foreach($users as $key => $obj){
290
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
291
			$data[$obj->community] = isset( $data[$obj->community] ) ? $data[$obj->community] + 1 : 1;
292
		}
293
		break;
294
295 View Code Duplication
	case 'business':
296
		$users = elgg_get_entities_from_metadata(array(
297
			'type' => 'user',
298
			'metadata_name_value_pairs' => array(
299
				array('name' => 'user_type', 'value' => 'business')
300
			),
301
			'limit' => 0
302
		));
303
		foreach($users as $key => $obj){
304
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
305
			$data[$obj->business] = isset( $data[$obj->business] ) ? $data[$obj->business] + 1 : 1;
306
		}
307
		break;
308
309 View Code Duplication
	case 'media':
310
		$users = elgg_get_entities_from_metadata(array(
311
			'type' => 'user',
312
			'metadata_name_value_pairs' => array(
313
				array('name' => 'user_type', 'value' => 'media')
314
			),
315
			'limit' => 0
316
		));
317
		foreach($users as $key => $obj){
318
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
319
			$data[$obj->media] = isset( $data[$obj->media] ) ? $data[$obj->media] + 1 : 1;
320
		}
321
		break;
322
323 View Code Duplication
	case 'retired':
324
		$users = elgg_get_entities_from_metadata(array(
325
			'type' => 'user',
326
			'metadata_name_value_pairs' => array(
327
				array('name' => 'user_type', 'value' => 'retired')
328
			),
329
			'limit' => 0
330
		));
331
		foreach($users as $key => $obj){
332
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
333
			$data[$obj->retired] = isset( $data[$obj->retired] ) ? $data[$obj->retired] + 1 : 1;
334
		}
335
		break;
336
337 View Code Duplication
	case 'other':
338
		$users = elgg_get_entities_from_metadata(array(
339
			'type' => 'user',
340
			'metadata_name_value_pairs' => array(
341
				array('name' => 'user_type', 'value' => 'other')
342
			),
343
			'limit' => 0
344
		));
345
		foreach($users as $key => $obj){
346
			$data['total'] = isset( $data['total'] ) ? $data['total'] + 1 : 1;
347
			$data[$obj->other] = isset( $data[$obj->other] ) ? $data[$obj->other] + 1 : 1;
348
		}
349
		break;
350
351
	case 'gcconnex':
352
	    $query = "SELECT msv.string as department, count(*) as count FROM {$dbprefix}users_entity u LEFT JOIN {$dbprefix}metadata md ON u.guid = md.entity_guid LEFT JOIN {$dbprefix}metastrings msn ON md.name_id = msn.id LEFT JOIN {$dbprefix}metastrings msv ON md.value_id = msv.id WHERE msn.string = 'department' GROUP BY department ORDER BY count DESC LIMIT 25";
353
		$departments = get_data($query);
354
355
		foreach($departments as $key => $obj){
356
			$data[$obj->department] = (int)$obj->count;
357
		}
358
		break;
359
360
	default:
361
		$data = "Please use one of the following `type` parameters: all, federal, academic, student, university, college, highschool, provincial, municipal, international, ngo, community, business, media, retired, other, gcconnex";
362
		break;
363
364
	}
365
366
    return $data;
367
}
368
369
function get_site_data($type, $lang) {
370
	$data = array();
371
	ini_set("memory_limit", -1);
372
	elgg_set_ignore_access(true);
373
	$dbprefix = elgg_get_config('dbprefix');
374
375
	switch($type) {
376
377
	case 'wireposts':
378
		$typeid = get_subtype_id('object', 'thewire');
379
380
		$query = "SELECT guid, time_created, owner_guid FROM {$dbprefix}entities WHERE type = 'object' AND subtype = {$typeid} AND enabled = 'yes'";
381
		$wireposts = get_data($query);
382
383
		foreach($wireposts as $key => $obj){
384
			$data[] = array($obj->time_created, "", $obj->owner_guid);
385
		}
386
		break;
387
388
	case 'blogposts':
389
		$typeid = get_subtype_id('object', 'blog');
390
391
		$query = "SELECT guid, time_created, owner_guid FROM {$dbprefix}entities WHERE type = 'object' AND subtype = {$typeid} AND enabled = 'yes'";
392
		$blogposts = get_data($query);
393
394
		foreach($blogposts as $key => $obj){
395
			$data[] = array($obj->time_created, "", "", $obj->owner_guid);
396
		}
397
		break;
398
399
	case 'comments':
400
		$typeid = get_subtype_id('object', 'comment');
401
402
		$query = "SELECT guid, time_created, owner_guid FROM {$dbprefix}entities WHERE type = 'object' AND subtype = {$typeid} AND enabled = 'yes'";
403
		$comments = get_data($query);
404
405
		foreach($comments as $key => $obj){
406
			$data[] = array($obj->time_created, "", $obj->owner_guid);
407
		}
408
		break;
409
410
	case 'groupscreated':
411
		$query = "SELECT guid, time_created, owner_guid FROM {$dbprefix}entities WHERE type = 'group' AND enabled = 'yes'";
412
		$groupscreated = get_data($query);
413
414
		foreach($groupscreated as $key => $obj){
415
			$data[] = array($obj->time_created, "", "", $obj->owner_guid);
416
		}
417
		break;
418
419
	case 'groupsjoined':
420
		$query = "SELECT * FROM {$dbprefix}entity_relationships WHERE relationship = 'member'";
421
		$groupsjoined = get_data($query);
422
423
		foreach($groupsjoined as $key => $obj){
424
			if ( $obj->time_created ){
425
				$data[] = array($obj->time_created, $obj->guid_one, $obj->guid_two);
426
			}
427
		}
428
		break;
429
430 View Code Duplication
	case 'likes':
431
		$likesID = elgg_get_metastring_id("likes");
432
433
		$query = "SELECT * FROM {$dbprefix}annotations WHERE name_id = $likesID";
434
		$likes = get_data($query);
435
436
		foreach($likes as $key => $obj){
437
			$data[] = array($obj->time_created, $obj->owner_guid, "");
438
		}
439
		break;
440
441 View Code Duplication
	case 'messages':
442
		$name_id = elgg_get_metastring_id("fromId");
443
444
		$query = "SELECT md.time_created as time_created, ms.string as sender_guid FROM {$dbprefix}metadata md 
445
					RIGHT JOIN {$dbprefix}metastrings ms ON ms.id = md.value_id
446
					RIGHT JOIN {$dbprefix}users_entity efrom ON ms.string = efrom.guid
447
					WHERE md.name_id = {$name_id}";
448
		$messages = get_data($query);
449
450
		foreach($messages as $key => $obj){
451
			$data[] = array($obj->time_created, "", $obj->sender_guid);
452
		}
453
		break;
454
455
	case 'optins':
456
		$optin_types = array(
457
			"opt_in_missions" => "missions:micro_mission",
458
			"opt_in_missionCreate" => "missions:micro_mission",
459
			"opt_in_swap" => "missions:job_swap",
460
			"opt_in_mentored" => "missions:mentoring",
461
			"opt_in_mentoring" => "missions:mentoring",
462
			"opt_in_shadowed" => "missions:job_shadowing",
463
			"opt_in_shadowing" => "missions:job_shadowing",
464
			"opt_in_jobshare" => "missions:job_sharing",
465
			"opt_in_pcSeek" => "missions:peer_coaching",
466
			"opt_in_pcCreate" => "missions:peer_coaching",
467
			"opt_in_ssSeek" => "missions:skill_sharing",
468
			"opt_in_ssCreate" => "missions:skill_sharing",
469
			"opt_in_rotation" => "missions:job_rotation",
470
			"opt_in_assignSeek" => "missions:assignment",
471
			"opt_in_assignCreate" => "missions:assignment",
472
			"opt_in_deploySeek" => "missions:deployment",
473
			"opt_in_deployCreate" => "missions:deployment",
474
			"opt_in_casual_seek" => "missions:casual",
475
			"opt_in_casual_create" => "missions:casual",
476
			"opt_in_student_seek" => "missions:student",
477
			"opt_in_student_create" => "missions:student",
478
			"opt_in_collaboration_seek" => "missions:collaboration",
479
			"opt_in_collaboration_create" => "missions:collaboration"
480
		);
481
482
		$yes = elgg_get_metastring_id('gcconnex_profile:opt:yes');
483
484
		$map = array();
485
		foreach ($optin_types as $optin_type => $index) {
486
			$map[$optin_type] = elgg_get_metastring_id($optin_type);
487
		}
488
489
		$query = "SELECT count(DISTINCT m.entity_guid) as num FROM elggmetadata m WHERE";
490
		foreach ($optin_types as $optin_type => $index) {
491
			$temp = $query . " m.name_id={$map[$optin_type]} AND m.value_id={$yes};";
492
			$result = get_data($temp)[0];
493
			$count = intval($result->num);
494
495
			if($count > 0){
496
				$string = elgg_echo($index, $lang);
497 View Code Duplication
				if(stripos($optin_type, 'create') !== false){
498
					$string .= " (" . elgg_echo("missions:offering", $lang) . ")";
499
				}
500 View Code Duplication
				if(stripos($optin_type, 'seek') !== false){
501
					$string .= " (" . elgg_echo("missions:seeking", $lang) . ")";
502
				}
503
				if($optin_type == "opt_in_mentored"){
504
					$string .= " (" . elgg_echo("gcconnex_profile:opt:mentored", $lang) . ")";
505
				}
506
				if($optin_type == "opt_in_mentoring"){
507
					$string .= " (" . elgg_echo("gcconnex_profile:opt:mentoring", $lang) . ")";
508
				}
509
				$data[$string] = $count;
510
			}
511
		}
512
		break;
513
514
	default:
515
		$data = "Please use one of the following `type` parameters: wireposts, blogposts, comments, groupscreated, groupsjoined, likes, messages, optins";
516
		break;
517
518
	}
519
520
    return $data;
521
}
522
523
function get_time_data($type, $lang) {
524
	$data = array();
525
	ini_set("memory_limit", -1);
526
	elgg_set_ignore_access(true);
527
	$dbprefix = elgg_get_config('dbprefix');
528
529
	switch($type) {
530
531
	case 'members':
532
		$query = "SELECT DISTINCT e.time_created AS time, count(*) AS count, date_format(from_unixtime(e.time_created),'%Y-%m-%d') AS date FROM {$dbprefix}entities e JOIN {$dbprefix}users_entity st ON e.guid = st.guid WHERE e.type = 'user' AND e.enabled = 'yes' GROUP BY date ORDER BY time";
533
		$data = get_data($query);
534
		break;
535
536
	case 'groups':
537
		$query = "SELECT DISTINCT e.time_created as time, count(*) as count, date_format(from_unixtime(e.time_created),'%Y-%m-%d') AS date FROM {$dbprefix}entities e WHERE type = 'group' AND e.enabled = 'yes' GROUP BY date ORDER BY time";
538
		$data = get_data($query);
539
		break;
540
541
	case 'opportunities':
542
		$typeid = get_subtype_id('object', 'mission');
543
544
		$query = "SELECT DISTINCT e.time_created AS time, count(*) AS count, date_format(from_unixtime(e.time_created),'%Y-%m-%d') AS date FROM {$dbprefix}entities e WHERE e.type = 'object' AND e.subtype = {$typeid} AND e.enabled = 'yes' GROUP BY date ORDER BY time";
545
		$data = get_data($query);
546
		break;
547
548
	default:
549
		$data = "Please use one of the following `type` parameters: members, groups, opportunities";
550
		break;
551
552
	}
553
554
	return $data;
555
}
556