forked from tunapanda/wp-swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-swag-admin.php
executable file
·305 lines (242 loc) · 7.98 KB
/
wp-swag-admin.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
<?php
/*
wp-swag plugin admin functionalities and hooks
*/
require_once __DIR__."/utils.php";
require_once __DIR__."/src/model/SwagUser.php";
require_once __DIR__."/src/utils/Xapi.php";
require_once __DIR__."/src/model/SwagPost.php";
require_once __DIR__."/src/utils/Template.php";
require_once __DIR__."/src/utils/ShortcodeUtil.php";
require_once __DIR__."/src/controller/SettingsPageController.php";
class WP_Swag_admin{
static $plugins_uri;
public function init_hooks(){
self::$plugins_uri = plugins_url()."/wp-swag";
// initialise the the admin settings
add_action('admin_init',array(get_called_class(),'ti_admin_init'));
add_action('admin_menu',array(get_called_class(),'ti_admin_menu'));
add_shortcode("course", array(get_called_class(), "ti_course"));
add_shortcode("track-listing",array(get_called_class(), "ti_track_listing"));
add_shortcode("course-listing",array(get_called_class(),"ti_course_listing"));
add_action('wp_enqueue_scripts',array(get_called_class(), "ti_enqueue_scripts"));
add_action('admin_enqueue_scripts',array(get_called_class(), "ti_enqueue_scripts"));
add_shortcode("swagmap", array(get_called_class(), "ti_swagmap"));
add_action("h5p-xapi-post-save",array(get_called_class(),"ti_xapi_post_save"));
add_action("h5p-xapi-pre-save",array(get_called_class(),"ti_xapi_pre_save"));
add_action("deliverable-xapi-post-save",array(get_called_class(), "ti_xapi_post_save"));
add_shortcode("my-swag",array(get_called_class(), "ti_my_swag"));
add_filter("h5p-xapi-auth-settings",array(get_called_class(),"ti_xapi_h5p_auth_settings"));
}
/**
* Create the admin menu.
*/
public function ti_admin_menu() {
add_options_page(
'Tunapanda Swag',
'Tunapanda Swag',
'manage_options',
'ti_settings',
array(get_called_class(), 'ti_create_settings_page')
);
}
/**
* Admin init.
*/
public function ti_admin_init() {
register_setting("ti","ti_xapi_endpoint_url");
register_setting("ti","ti_xapi_username");
register_setting("ti","ti_xapi_password");
}
/**
* Create settings page.
*/
public function ti_create_settings_page() {
$settingsPageController=new SettingsPageController();
$settingsPageController->process();
}
/**
* Handle the track-listing short_code.
*/
public function ti_track_listing() {
$parentId=get_the_ID();
$pages=get_pages(array(
"parent"=>$parentId
));
$out = '<div class="masonry-loop">';
foreach ($pages as $page) {
if ($page->ID!=$parentId) {
$page->swagpaths = count(get_pages(array('child_of'=>$page->ID)));
$out.=render_tpl(__DIR__."/tpl/tracklisting.php",array(
"page"=>$page
));
}
}
$out .= '</div>';
return $out;
}
/**
* Handle the course-listing short code.
*/
public function ti_course_listing() {
$swagUser=new SwagUser(wp_get_current_user());
$parentId=get_the_ID();
$q=new WP_Query(array(
"post_type"=>"any",
"post_parent"=>$parentId,
"posts_per_page"=>-1
));
$pages=$q->get_posts();
$out = '<div class="masonry-loop">';
$unpreparedCount=0;
foreach ($pages as $page) {
if ($page->ID!=$parentId) {
$swagPost=new SwagPost($page);
$prepared=$swagUser->isSwagCompleted($swagPost->getRequiredSwag());
$completed=$swagUser->isSwagCompleted($swagPost->getProvidedSwag());
if (!$swagPost->getProvidedSwag())
$completed=FALSE;
if (!$prepared)
$unpreparedCount++;
$out.=render_tpl(__DIR__."/tpl/courselisting.php",array(
"page"=>$page,
"prepared"=>$prepared,
"completed"=>$completed
));
}
}
if ($unpreparedCount) {
$out.=render_tpl(__DIR__."/tpl/afterlisting.php",array(
"unprepared"=>$unpreparedCount
));
}
$out .= '</div>';
return $out;
}
/**
* Scripts and styles in the plugin
*/
public function ti_enqueue_scripts() {
wp_register_style("wp_swag",plugins_url( "/style.css", __FILE__)); //?v=x added to refresh browser cache when stylesheet is updated.
wp_enqueue_style("wp_swag");
wp_register_script("d3",plugins_url("/d3.v3.min.js", __FILE__));
wp_register_script("ti-main",plugins_url("/main.js", __FILE__));
wp_enqueue_script("ti-main");
wp_enqueue_script("d3");
}
/**
* Handle the course shortcode.
*/
function ti_course($args, $content) {
if (!$args)
$args=array();
$swagPost=SwagPost::getCurrent();
$swagUser=SwagUser::getCurrent();
$selectedItem=$swagPost->getSelectedItem();
$selectedItem->preShow();
//print_r($selectedItem);
$template=new Template(__DIR__."/tpl/course.php");
$template->set("swagUser",$swagUser);
$template->set("swagPost",$swagPost);
/** Leson Plan Functionality */
$template->set("showLessonPlan",FALSE);
if (array_key_exists("lessonplan",$args) AND is_user_logged_in()) {
$template->set("lessonPlan",get_home_url().'/wp-content/uploads'.$args["lessonplan"]);
$template->set("showLessonPlan",TRUE);}
if ($swagUser->isSwagCompleted($swagPost->getProvidedSwag())) {
$template->set("lessonplanAvailable",TRUE);
}
else if (current_user_can('edit_others_pages') || get_the_author_id() == get_current_user_id()) {
$template->set("lessonplanAvailable",TRUE);
}
else {
$template->set("lessonplanAvailable",FALSE);
}
$template->set("showHintInfo",FALSE);
if (!$swagUser->isSwagCompleted($swagPost->getRequiredSwag())) {
$template->set("showHintInfo",TRUE);
$uncollected=$swagUser->getUncollectedSwag($swagPost->getRequiredSwag());
$uncollectedFormatted=array();
foreach ($uncollected as $swag)
$uncollectedFormatted[]="<b>$swag</b>";
$swagpaths=SwagPost::getPostsProvidingSwag($uncollected);
$swagpathsFormatted=array();
foreach ($swagpaths as $swagpath)
$swagpathsFormatted[]=
"<a href='".get_post_permalink($swagpath->ID)."'>".
$swagpath->post_title.
"</a>";
$template->set("uncollectedSwag",join(", ",$uncollectedFormatted));
$template->set("uncollectedSwagpaths",join(", ",$swagpathsFormatted));
}
return $template->render();
}
/**
* Render swagmap.
*/
public function ti_swagmap() {
$plugins_uri = self::$plugins_uri;
return "<div id='swagmapcontainer'>
<div id='swag_description_container'>A swagmap is gamified display of performance. The green hollow nodes indicate the swagpath is not completed or attempted while non-hollow green nodes indicate the swagpaths is completed and questions answered.
</div>
<script>
var PLUGIN_URI = '$plugins_uri';
</script>
</div>";
}
/**
* Here we have the chance to modify the statement before it is saved.
*/
public function ti_xapi_pre_save($statement) {
return $statement;
}
/**
* Act on completed xapi statements.
* Save xapi statement for swag if applicable.
*/
public function ti_xapi_post_save($statement) {
if ($statement["verb"]["id"]!="http://adlnet.gov/expapi/verbs/completed")
return;
foreach ($statement["context"]["contextActivities"]["grouping"] as $groupingActivity) {
$id=url_to_postid($groupingActivity["id"]);
if ($id)
$postId=$id;
}
foreach ($statement["context"]["contextActivities"]["category"] as $categoryActivity) {
$id=url_to_postid($categoryActivity["id"]);
if ($id)
$postId=$id;
}
if (!$postId)
return;
$post=get_post($postId);
if (!$post)
return;
$swagUser=SwagUser::getByEmail($statement["actor"]["mbox"]);
$swagPost=new SwagPost($post);
$swagPost->saveProvidedSwagIfCompleted($swagUser);
}
public function ti_my_swag() {
$plugins_uri = self::$plugins_uri;
$swagUser=new SwagUser(wp_get_current_user());
$completedSwag=$swagUser->getCompletedSwag();
$out="";
foreach ($completedSwag as $swag) {
$out.="<div class='swag-badge-container'>\n";
$out.="<img class='swag-badge-image' src='$plugins_uri/img/badge.png'>\n";
$out.="<div class='swag-badge-label'>$swag</div>\n";
$out.="</div>\n";
}
return $out;
}
/**
* Provide settings for wp-h5p-xapi
*/
public function ti_xapi_h5p_auth_settings($arg) {
return array(
"endpoint_url"=>get_option("ti_xapi_endpoint_url"),
"username"=>get_option("ti_xapi_username"),
"password"=>get_option("ti_xapi_password")
);
}
}