-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgamify.lib.php
More file actions
563 lines (516 loc) · 19.7 KB
/
gamify.lib.php
File metadata and controls
563 lines (516 loc) · 19.7 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
//namespace gameme;
/* Get a list of gameme leaders in a course
* @$gamificaiton - a PHPGamifcations object
* @$leader_depth - the number of leaders to display
*/
function getLeaders($gamification, $leader_depth){
echo "<h3>"._AT('gm_leaders_top', $leader_depth)."</h3>";
$leaders = $gamification->getUsersPointsRanking($leader_depth);
$sql = "SELECT member_id FROM %scourses WHERE course_id = %d";
$instructor = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id']), TRUE);
$count = 0;
$leader_board = '<table
style="
width:95%;
border-top:1px solid #ded29e;
border-bottom:1px solid #ded29e;
text-align:center;"><tr><th>#</th><th>'._AT('gm_id').'</th><th>'._AT('gm_points').'</th><th>'._AT('gm_level').'</th></tr>';
if(!empty($leaders)){
foreach($leaders as $key=>$leader){
// don't display instructor on leader board
if($instructor['member_id'] == $leader->id_user){
if(show_instructor() == 1){
$login_name = getLoginName($leader->id_user);
$count++;
$leader_board .= '<tr>
<td>'.$count.'</td>
<td>'.$login_name.'</td>
<td>'.$leader->points.'</td>
<td>'.$leader->id_level.'</td>
</tr>'."\n";
}
} else if($instructor['member_id'] != $leader->id_user){
$login_name = getLoginName($leader->id_user);
$count++;
$leader_board .= '<tr>
<td>'.$count.'</td>
<td>'.$login_name.'</td>
<td>'.$leader->points.'</td>
<td>'.$leader->id_level.'</td>
</tr>'."\n";
}
}
}
$leader_board .='</table>';
echo $leader_board;
}
/*
*/
function getLoginName($mid){
$sql = "SELECT login FROM %smembers WHERE member_id=%d";
$member_login = queryDB($sql, array(TABLE_PREFIX, $mid), TRUE);
return $member_login['login'];
}
/*
*/
function yourPosition($mid){
$sql = "SELECT * FROM %sgm_user_scores WHERE course_id = %d ORDER BY points DESC";
$leaders_desc = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id']));
$sql = "SELECT member_id FROM %scourses WHERE course_id = %d";
$instructor = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id']), TRUE);
$count = 0;
foreach($leaders_desc as $leader){
// remove instructor from leader board
if($leader['id_user'] != $instructor['member_id']){
$count++;
if($leader['id_user'] == $_SESSION['member_id'] && !$_SESSION['is_admin']){
echo "<p>"._AT('gm_in_position' , $count)."</p>"."\n";
}
}
}
return $member_login['login'];
}
/*
*/
function showUserScores($gamification){
echo "<h3>"._AT('gm_levels_awarded')."</h3>";
$score = $gamification->getUserScores();
echo "<table class='data'>";
echo "<tr>
<th> </th>
<th>"._AT('gm_level')."</th>
<th>"._AT('gm_description')."</th>
</tr>"."\n";
echo showstars($score->getPoints());
echo "</table>";
}
/*
*/
function showUserScore($gamification){
$score = $gamification->getUserScores();
if($score->getPoints() == 0){
echo '<div style="text-align:center;">'._AT('gm_login_for_points').'</div>';
}
echo "<div style='
width:80%;
font-size:2em;
text-align:center;
margin-top:.5em;
margin-left:auto;
margin-right:auto;
border: 1px solid green;
box-shadow: .1em .1em .1em;#666666;
background-color: #e9fbe9;
border-radius:.3em;
padding:.2em;'
>"._AT('gm_points').": " . $score->getPoints() . " </div><br /><hr />";
}
/*
*/
function showUserLevels($gamification, $course_id)
{
$score = $gamification->getUserScores();
echo "<h3>"._AT('gm_your_levels_reached')."</h3>";
if ($score){
$sql = "SELECT `value` from %sgm_options WHERE `course_id`=%d AND `gm_option`='%s'";
if($level_max = queryDB($sql, array(TABLE_PREFIX, $course_id, "level_count"),TRUE)){
if($level_max['value'] >0){
$limit = " LIMIT ".$level_max['value'];
}
}
// Get default levels
$sql = "SELECT * FROM %sgm_levels WHERE course_id = %d AND points <= %d ORDER BY id asc $limit";
$levels = queryDB($sql, array(TABLE_PREFIX, 0, $score->getPoints()));
$level_ids = array();
foreach($levels as $level){
array_push($level_ids, $level['id']);
}
// Get customized course levels
$sql = "SELECT * FROM %sgm_levels WHERE course_id = %d AND points <= %d ORDER BY id asc $limit";
$course_levels = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id'], $score->getPoints()));
$course_level_ids = array();
foreach($course_levels as $course_level){
array_push($course_level_ids, $course_level['id']);
}
$this_levels = array();
foreach($course_levels as $course_level){
if(!in_array($course_level['id'], $this_levels)){
array_push($this_levels, $course_level);
}
}
foreach($levels as $level){
if(!in_array($level['id'], $course_level_ids)){
array_push($this_levels, $level);
}
}
//usort($this_levels, 'sortByOrder');
krsort($this_levels);
echo "<table class='data'>
<tr>
<th>"._AT('gm_level')."</th>
<th>"._AT('gm_title')."</th>
<th>"._AT('gm_description')."</th>
<th>"._AT('gm_points')."</th>
</tr>";
foreach($this_levels as $level){
echo "<tr>";
echo "<td>".showstar($level['points'])."</td>";
echo "<td>".$level['title']."</td>";
echo "<td>".$level['description']."</td>";
echo "<td>".$level['points']."</td>";
echo "</tr>"."\n";
}
echo "</table>";
}
}
/*
*/
function showUserLevel($gamification, $courseId){
$score = $gamification->getUserScores();
echo "<h3>"._AT('gm_levels_awarded')."</h3>
<div style='background-color:#f6f6f6; width:100%; padding:.2em;margin-left:auto;margin-right:auto;' >".showstars($score->getPoints())."</div>";
}
/*
*/
function showUserProgress($gamification, $courseId){
$score = $gamification->getUserScores();
return "<br >"._AT('gm_progress_to_next', $score->getProgress())."<hr />";
}
function sortByOrder($a, $b) {
return $a['points'] - $b['points'];
}
/*
* Strings stars together, laid out in Levels in the sidemenu block
* @param $points - the user's current points scored
*/
function showstars($points){
global $_base_href;
$sql = "SELECT `value` from %sgm_options WHERE `course_id`=%d AND `gm_option`='%s'";
if($level_max = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id'], "level_count"),TRUE)){
if($level_max['value'] >0){
$limit = " LIMIT ".$level_max['value'];
}
}
// Get default levels
$sql = "SELECT * FROM %sgm_levels WHERE course_id = %d AND points <= %d ORDER BY id asc $limit";
$levels = queryDB($sql, array(TABLE_PREFIX, 0, $points));
$level_ids = array();
foreach($levels as $level){
array_push($level_ids, $level['id']);
}
// Get customized course levels
$sql = "SELECT * FROM %sgm_levels WHERE course_id = %d AND points <= %d ORDER BY id asc $limit";
$course_levels = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id'], $points));
$course_level_ids = array();
foreach($course_levels as $course_level){
array_push($course_level_ids, $course_level['id']);
}
$this_levels = array();
foreach($course_levels as $course_level){
if(!in_array($course_level['id'], $this_levels)){
array_push($this_levels, $course_level);
}
}
foreach($levels as $level){
if(!in_array($level['id'], $course_level_ids)){
array_push($this_levels, $level);
}
}
krsort($this_levels);
//usort($this_levels, 'sortByOrder');
$content_dir = explode('/',AT_CONTENT_DIR);
array_pop($content_dir);
foreach($this_levels as $level){
if(in_array($level, $this_levels)){
$course_id = $_SESSION['course_id'];
} else{
$course_id = 0;
}
$sql = "SELECT id, icon, title, description FROM %sgm_levels WHERE id=%d AND course_id=%d";
$level_image = queryDB($sql, array(TABLE_PREFIX, $level['id'],$_SESSION['course_id']), TRUE);
if(!empty($level_image['icon'])){
if(is_file(AT_CONTENT_DIR.$_SESSION['course_id'].'/gameme/levels/'.$level_image['icon'])){
$level_file = $_base_href.'get.php/gameme/levels/'.$level_image['icon'];
}else if(is_file(AT_CONTENT_DIR.'0/gameme/levels/'.$level_image['icon'])){
$level_file = $_base_href.'mods/gameme/get_level_icon.php?level_id='.$level_image['id'];
}else {
$level_file = $_base_href.'mods/gameme/images/'.$level_image['icon'];
}
} else {
if(!in_array($level['id'] , $course_levels)){
$sql = "SELECT id, icon, title, description FROM %sgm_levels WHERE id=%d AND course_id=%d";
$level_image = queryDB($sql, array(TABLE_PREFIX, $level['id'],0), TRUE);
if(!is_file(AT_CONTENT_DIR.'0/gameme/levels/'.$level_image['icon'])){
$level_file = $_base_href.'mods/gameme/images/'.$level_image['icon'];
} else {
$content_dir = explode('/',AT_CONTENT_DIR);
array_pop($content_dir);
$level_file = $_base_href.'mods/gameme/get_level_icon.php?level_id='.$level_image['id'];
}
}
}
$stars .= '<img src="'.$level_file.'" alt="'.$level_image['title'].'" title="'.$level_image['title'].' '.$level_image['description'].'" style="margin:.2em;"/>'."\n";
}
return $stars;
}
/*
* Show the star associated with a particular point value
* @param $points
* @return html img tag for the star/level
*/
function showstar($points){
global $_base_href;
$sql = "SELECT id, icon, description FROM %sgm_levels WHERE points=%d AND course_id=%d";
$level_image = queryDB($sql, array(TABLE_PREFIX, $points,$_SESSION['course_id']), TRUE);
//$level_file = star_file($level_image['id']);
if(!empty($level_image['icon'])){
$level_file = star_file($level_image['id']);
} else {
$sql = "SELECT id, icon, description FROM %sgm_levels WHERE points=%d AND course_id=%d";
$level_image = queryDB($sql, array(TABLE_PREFIX, $points,0), TRUE);
$level_file = star_file($level_image['id']);
}
return '<img src="'.$level_file.'" alt="'.$level_image['description'].'" title="'.$level_image['description'].'" style="margin:.2em;"/>'."\n";
}
function star_file($id){
global $_base_href;
$sql = "SELECT icon, description FROM %sgm_levels WHERE id=%d AND course_id=%d";
$level_image = queryDB($sql, array(TABLE_PREFIX,$id,$_SESSION['course_id']), TRUE);
if(!empty($level_image['icon'])){
if(is_file(AT_CONTENT_DIR.$_SESSION['course_id'].'/gameme/levels/'.$level_image['icon'])){
// Course level
$level_file = $_base_href.'get.php/gameme/levels/'.$level_image['icon'];
}else {
$level_file = $_base_href.'mods/gameme/images/'.$level_image['icon'];
}
} else {
$sql = "SELECT icon, description FROM %sgm_levels WHERE id=%d AND course_id=%d";
$level_image = queryDB($sql, array(TABLE_PREFIX, $id,0), TRUE);
if(is_file(AT_CONTENT_DIR.'0/gameme/levels/'.$level_image['icon'])){
// Custom default level
$level_file = $_base_href.'mods/gameme/get_level_icon.php?level_id='.$id;
} else{
// Default Level
$level_file = $_base_href.'mods/gameme/images/'.$level_image['icon'];
}
}
return $level_file;
}
/*
*
*
*/
function showUserBadges($gamification)
{
echo "<h3>"._AT('gm_manage_badges')."</h3>";
$badges = $gamification->getUserBadges();
if (!empty($badges)){
echo "<table class='data'>
<tr>
<th> </th>
<th>"._AT('gm_badge_id')."</th>
<th>"._AT('gm_counter')."</th>
<th>"._AT('gm_alias')."</th>
<th>"._AT('gm_description')."</th>
</tr>";
foreach ($badges as $badge) {
echo "<tr>";
echo "<td>".getBadgeImage($badge->getIdBadge())."</td>";
echo "<td>".$badge->getIdBadge()."</td>";
echo "<td>".$badge->getBadgeCounter()."</td>";
echo "<td>".$badge->getBadge()->getAlias()."</td>";
echo "<td>".$badge->getBadge()->getDescription()."</td>";
echo "</tr>"."\n";
}
echo "</table>";
} else{
echo "No badges yet.";
}
}
function showUserBadgesStudents($gamification)
{
echo "<h3>"._AT('gm_your_badges')."</h3>";
$badges = $gamification->getUserBadges();
if ($badges){
echo "<table class='data'>
<tr>
<th> </th>
<!--<th>"._AT('gm_counter')."</th>-->
<th>"._AT('gm_title')."</th>
<th>"._AT('gm_description')."</th>
</tr>";
foreach ($badges as $badge) {
echo "<tr>";
echo "<td>".getBadgeImage($badge->getIdBadge())."</td>";;
//echo "<td>".$badge->getBadgeCounter()."</td>";
echo "<td>".$badge->getBadge()->getTitle()."</td>";
echo "<td>".$badge->getBadge()->getDescription()."</td>";
echo "</tr>"."\n";
}
echo "</table>";
}
}
/*
*
*
*/
function showUserBadge($gamification)
{
echo "<h3>"._AT('gm_your_badges')."</h3>";
$badges = $gamification->getUserBadges();
if (!empty($badges)){
foreach ($badges as $badge) {
echo getBadgeImage($badge->getIdBadge());
}
} else {
echo '<p>'._AT('gm_no_badges_earned').'</p>';
}
echo "<hr />";
}
/*
*
*
*/
function getBadgeImage($badge_id){
global $_base_href;
$sql = "SELECT image_url, description FROM %sgm_badges WHERE id=%d AND course_id=%d";
$badge_image = queryDB($sql, array(TABLE_PREFIX, $badge_id,$_SESSION['course_id']), TRUE);
if(strstr($badge_image['image_url'], "content")){
$badge_file_array = explode('/',$badge_image['image_url']);
if($badge_file_array[1] == 0){
$custom_default = TRUE;
}
array_shift($badge_file_array);
$badge_file_stem = implode('/',$badge_file_array);
if(is_file(AT_CONTENT_DIR.$badge_file_stem)){
if(!$custom_default){
$badge_file = $_base_href.'get.php/gameme/badges/'.end($badge_file_array);
} else{
$badge_file = $_base_href.'mods/gameme/get_badge_icon.php?badge_id='.$badge_id;
}
}
} else{
$badge_file = $_base_href.$badge_image['image_url'];
// Not a course badge, so check for custom system badge
$sql = "SELECT image_url, description FROM %sgm_badges WHERE id=%d AND course_id=%d";
$badge_image = queryDB($sql, array(TABLE_PREFIX, $badge_id, 0), TRUE);
$badge_file_array = explode('/',$badge_image['image_url']);
array_shift($badge_file_array);
// get the custom admin created icon
if(is_file(AT_CONTENT_DIR.'0/gameme/badges/'.end($badge_file_array))){
$badge_file = $_base_href.'mods/gameme/get_badge_icon.php?badge_id='.$badge_id;
} else{
// Default Badge
$badge_file = $_base_href.$badge_image['image_url'];
}
}
return '<img src="'.$badge_file.'" alt="'.$badge_image['description'].'" title="'.$badge_image['description'].'" style="margin:.2em;"/>'."\n";
}
/*
*
*
*/
function showUserEvents($gamification){
echo "<h2>"._AT('gm_events')."</h2>";
$events = $gamification->getUserEvents();
echo "<ul style='margin-left:1em;line-height:1.8em;'>"."\n";
foreach ($events as $event) {
echo '<li><strong>'.$event['event']->getDescription(). '</strong> - '._AT('gm_count').'('.$event[counter].')<br />';
echo $event['event']->getReachMessage();
}
echo "</ul>"."\n";
}
/*
*
*
*/
function showUserAlerts($gamification){
echo "<h3>"._AT('gm_your_alert')."</h3>"."\n";
$alerts = $gamification->getUserAlerts();
if ($alerts == null) {
echo _AT('gm_no_alerts')."<br/>";
} else {
foreach ($alerts as $alert) {
if ($alert->getIdBadge())
echo _AT('gm_badge').": " . $alert->getBadge()->getTitle();
if ($alert->getIdLevel())
echo _AT('gm_level').": " . $alert->getIdLevel();
echo "<br>"."\n";
}
}
}
/*
*
*
*/
function showUserLog($gamification){
echo "<h3>"._AT('gm_your_activity_log')."</h3>";
$logs = $gamification->getUserLog();
if ($logs)
echo "<table class='data'>
<tr>
<th>"._AT('gm_event_date')."</th>
<th>"._AT('gm_event')."</th>
<th>"._AT('gm_points')."</th>
<th>"._AT('gm_badge')."</th>
<th>"._AT('gm_level')."</th>
</tr>";
foreach ($logs as $log) {
$level = $gamification->getLevel($log->getIdLevel());
echo "<tr>";
echo "<td>".$log->getEventDate()."</td><td>".$log->getEvent()->getAlias()."</td>";
if ($log->getPoints()){
echo "<td>".$log->getPoints()."</td>";
}else{
echo "<td> </td>";
}
if ($log->getIdBadge()){
echo "<td>".$log->getBadge()->getTitle()."</td>";
}else{
echo "<td> </td>";
}
if ($log->getIdLevel() > 0){
echo "<td>".$level->getTitle()."</td>";
}else{
echo "<td> </td>";
}
echo "</tr>"."\n";
}
echo "</table>";
}
function get_leader_count(){
$sql = "SELECT * FROM %sgm_options WHERE `course_id`=%d and `gm_option` = 'showleader_count'";
$this_count = queryDB($sql, array(TABLE_PREFIX, $_SESSION['course_id']), TRUE);
return $this_count['value'];
}
function show_instructor(){
$sql = 'SELECT value FROM %sgm_options WHERE `gm_option` = "%s" AND `course_id` = %d';
$show_instructor = queryDB($sql, array(TABLE_PREFIX, 'showinstructor', $_SESSION['course_id']), TRUE);
return $show_instructor['value'];
}
/* Gets the reach message from the database, either
* 1. a message created by the instructor for a particular course
* 2. or the default message that come with the module
* -in that order, whichever come first-
* @$alias the alias for the event defined in the gm_events table,
* and passed from the events.php file
*/
function get_reach_message($alias){
if($_SESSION['course_id'] > 0){
$is_course = " AND course_id=".$_SESSION['course_id'];
} else{
$is_course = " AND course_id=0";
}
//$is_course = " AND course_id=".$_SESSION['course_id'];
$sql = "SELECT reach_message from %sgm_events WHERE alias = '%s' $is_course";
if($reach_message = queryDB($sql, array(TABLE_PREFIX, $alias), TRUE)){
// all good
}else{
// reach message does not exist so get the system default
$sql = "SELECT reach_message from %sgm_events WHERE alias = '%s' AND course_id=0";
$reach_message = queryDB($sql, array(TABLE_PREFIX, $alias), TRUE);
}
return $reach_message['reach_message'];
}
?>