forked from gocodebox/lifterlms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.llms.achievements.php
130 lines (104 loc) · 2.84 KB
/
class.llms.achievements.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
<?php
/**
* Achievements Base Class
*
* @package LifterLMS/Classes/Achievements
* @since 1.0.0
* @version 3.24.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Main Achievements singleton
* Accessible via LLMS()->achievements()
*/
class LLMS_Achievements {
public $achievements;
public $content;
private $_from_address;
private $_from_name;
private $_content_type;
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self(); }
return self::$_instance;
}
/**
* Constructor
* @since 1.0.0
* @version 3.24.0
*/
private function __construct() {
$this->init();
}
/**
* Includes achievement class
* @return void
* @since 1.0.0
* @version ??
*/
public function init() {
include_once( 'class.llms.achievement.php' );
$this->achievements['LLMS_Achievement_User'] = include_once( 'achievements/class.llms.achievement.user.php' );
}
/**
* Get a list of achievement Achievement Template IDs for a given post
*
* @param array|int $post_ids Post IDs or single post ID to look for achievements by.
* @param bool $include_children If true, will include course children (sections, lessons, and quizzes).
* @return array
* @since 3.24.0
* @version 3.24.0
*/
public function get_achievements_by_post( $post_ids, $include_children = true ) {
if ( ! is_array( $post_ids ) ) {
$post_ids = array( $post_ids );
}
if ( $include_children ) {
foreach ( $post_ids as $post_id ) {
if ( 'course' === get_post_type( $post_id ) ) {
$course = llms_get_post( $post_id );
$post_ids = array_merge(
$post_ids,
$course->get_sections( 'ids' ),
$course->get_lessons( 'ids' ),
$course->get_quizzes()
);
}
}
}
$query = new WP_Query( array(
'post_type' => 'llms_engagement',
'meta_query' => array(
array(
'key' => '_llms_engagement_type',
'value' => 'achievement',
),
array(
'compare' => 'IN',
'key' => '_llms_engagement_trigger_post',
'value' => $post_ids,
),
),
) );
$achievements = array();
foreach ( $query->posts as $engagement ) {
$achievements[] = get_post_meta( $engagement->ID, '_llms_engagement', true );
}
return $achievements;
}
/**
* Award an achievement to a user
* Calls trigger method passing arguments
* @param int $person_id [ID of the current user]
* @param int $achievement [Achievement template post ID]
* @param int $related_post_id Post ID of the related engagement (eg lesson id)
* @return void
* @since ??
* @version ??
*/
public function trigger_engagement( $person_id, $achievement_id, $related_post_id ) {
$achievement = $this->achievements['LLMS_Achievement_User'];
$achievement->trigger( $person_id, $achievement_id, $related_post_id );
}
}