-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathachievement.php
396 lines (358 loc) · 13.2 KB
/
achievement.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
<?php
// This file is part of the achievement plugin for Moodle
// This plugin 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.
//
// This plugin 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 this plugin. If not, see <http://www.gnu.org/licenses/>.
/**
*
* @package achievement
* @copyright 2012 Andil
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class achievement
{
protected $achievementlist = null;
protected $newachievementdonelist = array();
protected $hasupdate = false;
function load_achievementlist() {
global $DB;
$this->achievementlist = $DB->get_records('block_achievement', array('enable' => true), 'id ASC');
}
function get_achievementlist(){
return $this->achievementlist;
}
function compute_achievement_for_user(&$user) {
global $DB;
if (!isset($user->achievementdone)) {
//load achievement already done by $user
$this->load_achievement_for_user($user);
}
//search for new achievement
foreach ($this->achievementlist as $achievement) {
if ($this->achievementdone_search($user->achievementdone, $achievement) === false) {
if ($this->check_achievement_for_user($achievement, $user)) {
$newachievementdone = new stdClass();
$newachievementdone->achievementid = $achievement->id;
$newachievementdone->userid = $user->id;
$newachievementdone->timecreated = time();
$DB->insert_record('block_achievement_user', $newachievementdone);
$this->hasupdate = true;
$this->newachievementdonelist[] = $newachievementdone;
}
}
}
//reload achievement
if ($this->hasupdate) $this->load_achievement_for_user($user);
}
function has_update() {
return $this->hasupdate;
}
function load_achievement_for_user(&$user) {
global $DB;
$user->achievementdone = $DB->get_records_sql("
SELECT ach_user.*
FROM {block_achievement} AS ach, {block_achievement_user} AS ach_user
WHERE
(ach.id=ach_user.achievementid)
AND (ach.enable=1)
AND (ach_user.userid={$user->id})
ORDER BY ach_user.timecreated DESC");
}
function check_achievement_for_user(&$achievement, &$user) {
return $this->get_progress_achievement_for_user($achievement, $user) >= 1;
}
function get_progress_achievement_for_user(&$achievement, &$user) {
if (method_exists($this->get_called_class(), $achievement->compute_function)) {
$params = array();
$params[] = $user;
$nbrequire = 1;
if (isset($achievement->compute_function_parameters)
&& !empty($achievement->compute_function_parameters)
) {
$otherparamstmp = preg_split('/\|/', $achievement->compute_function_parameters);
$nbrequire = $otherparamstmp[0];
if(!empty($otherparamstmp)){
$otherparams = array();
for($i=1; $i < count($otherparamstmp); $i++){
$otherparams[] = $otherparamstmp[$i];
}
}
$params = array_merge($params, (!empty($otherparamstmp))?
$otherparams : array($achievement->compute_function_parameters));
}
return (call_user_func_array(array($this, $achievement->compute_function), $params)/$nbrequire);
}
return false;
}
function get_donetime_achievement_for_user(&$achievement, &$user) {
if (method_exists($this->get_called_class(), $achievement->compute_function)) {
$params = array();
$params[] = $user;
if (isset($achievement->compute_function_parameters)
&& !empty($achievement->compute_function_parameters)
) {
$otherparamstmp = preg_split('/\|/', $achievement->compute_function_parameters);
if(!empty($otherparamstmp)){
$otherparams = array();
for($i=1; $i < count($otherparamstmp); $i++){
$otherparams[] = $otherparamstmp[$i];
}
}
$params = array_merge($params, (!empty($otherparamstmp))?
$otherparams : array($achievement->compute_function_parameters));
}
return call_user_func_array(array($this, $achievement->compute_function), $params);
}
return false;
}
function achievement_search(&$parents, &$searched) {
if (empty($searched) || empty($parents)) {
return false;
}
foreach ($parents as $key => $value) {
$exists = true;
foreach ($searched as $skey => $svalue) {
$exists = ($exists && isset($value->$skey) && ($value->$skey == $svalue));
}
if ($exists) {
return $key;
}
}
return false;
}
/*
* search an achievement among an achievemnt_user list
* parent => achievement_user table
* $achievement => achievement
*/
function achievementdone_search(&$parents, &$achievement) {
if (empty($achievement) || empty($parents)) {
return false;
}
foreach ($parents as $key => $value) {
if ($value->achievementid == $achievement->id) return $key;
}
return false;
}
function get_achievementdone(&$achievement, &$user) {
global $DB;
return $DB->get_record_sql("
SELECT *
FROM {block_achievement_user} AS ach_user
WHERE
(ach_user.userid={$user->id})
AND (ach_user.achievementid={$achievement->id})");
}
function get_newachievementdonelist() {
return $this->newachievementdonelist;
}
function get_called_class() {
if (function_exists('get_called_class'))
return get_called_class();
$t = debug_backtrace();
$t = $t[0];
if (isset($t['object']) && $t['object'] instanceof $t['class'])
return get_class($t['object']);
return false;
}
function build_achievement_shortname($achievement) {
return $achievement->compute_function.'_'.preg_replace('/\|/', '_', $achievement->compute_function_parameters);
}
function build_achievement_generic_shortname($achievement) {
return $achievement->compute_function;
}
function get_achievement_string($achievement, $suffix='') {
//Search for specific string for this achievement
$default_specific = '[['.$this->build_achievement_shortname($achievement).$suffix.']]';
$a = preg_split('/\|/', $achievement->compute_function_parameters, 1);
$res = get_string($this->build_achievement_shortname($achievement).$suffix, 'block_achievement', $a[0]);
if($res != $default_specific)
return $res;
//Try to search for generic string for this achievement type
$default_generic = '[['.$this->build_achievement_generic_shortname($achievement).$suffix.']]';
$res = get_string($this->build_achievement_generic_shortname($achievement).$suffix, 'block_achievement', $a[0]);
return ($res != $default_generic)?$res:$default_specific;
}
function get_achievement_pix($achievement) {
global $CFG;
$imgextension = '.png';
$imgfilename = $this->build_achievement_shortname($achievement);
if(!file_exists($CFG->dirroot.'/blocks/achievement/pix/'.$imgfilename.$imgextension))
$imgfilename = $this->build_achievement_generic_shortname($achievement);
return $imgfilename.$imgextension;
}
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
/*
* compute functions
*/
function login($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='user'
AND `action`='login'
AND `userid`={$user->id}");
}
//Profile
function update_avatar($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} as log
WHERE
`module`='upload'
AND `action`='upload'
AND time IN (
SELECT time
FROM {log}
WHERE `userid`={$user->id}
AND `module`='user'
AND `action`='update')");
}
function update_profile($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} as log
WHERE `userid`={$user->id}
AND `module`='user'
AND `action`='update'");
}
//Forum
function forum_view($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='forum'
AND `action`='view forum'
AND `userid`={$user->id}");
}
function forum_post($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='forum'
AND `action`='add post'
AND `userid`={$user->id}");
}
//PM
function pm_sent($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='message'
AND `action`='write'
AND `userid`={$user->id}");
}
function pm_received($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='message'
AND `action`='write'
AND `info`={$user->id}");
}
//Blog
function blog_view($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='blog'
AND `action`='view'
AND `userid`={$user->id}");
}
function blog_add($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='blog'
AND `action`='add'
AND `userid`={$user->id}");
}
//Chat
function chat_talk($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='chat'
AND `action`='talk'
AND `userid`={$user->id}");
}
//BBB
function bbb_join($user) {
global $DB;
return $DB->count_records_sql(
"SELECT COUNT(log.id)
FROM {log} AS log
WHERE `module`='bigbluebutton'
AND `action`='join'
AND `userid`={$user->id}");
}
//Resource
function resource_view($user, $distinct=false) {
global $DB;
$distinctstring = ($distinct !=false)?"DISTINCT":"";
return $DB->count_records_sql(
"SELECT COUNT($distinctstring log.cmid)
FROM {log} AS log
WHERE `module` = 'resource'
AND `action` = 'view'
AND `userid` = {$user->id}");
}
function resource_distinct_view($user) {
return $this->resource_view($user, true);
}
//Assignment
function assignment_view($user, $distinct=false) {
global $DB;
$distinctstring = ($distinct != false)?"DISTINCT":"";
return $DB->count_records_sql(
"SELECT COUNT($distinctstring log.cmid)
FROM {log} AS log
WHERE `module` = 'assignment'
AND `action` = 'view'
AND `userid` = {$user->id}");
}
function assignment_distinct_view($user) {
return $this->assignment_view($user, true);
}
function assignment_done($user, $distinct=false) {
global $DB;
$distinctstring = ($distinct != false)?"DISTINCT":"";
return $DB->count_records_sql(
"SELECT COUNT($distinctstring log.cmid)
FROM {log} AS log
WHERE `module` = 'assignment'
AND `action` = 'upload'
AND `userid` = {$user->id}");
}
}