With this, we just need to award the achievement, associated with the lesson, to the user at this point?
Hi Michael-
Yes I need the achievement warded with the associated points.
Does this all make sense ok?
Matt
Hi Matt, sorry for the delay in response, it’s been a busy day after the holiday weekend.
Well, from the looks of it, you have the user ID already, which is good. The question is where can we get the achievement ID necessary to award. It looks like you’ll have the lesson ID that we can work with.
Try out the code below. Best way I could think of offhand to get the achievement ID based on lesson ID.
global $wpdb;
$sql = "SELECT post_id as lesson FROM " . $wpdb->postmeta . " WHERE meta_key = %s AND meta_value = %d";
$rs1 = $wpdb->get_results( $wpdb->prepare( $sql, '_badgeos_learndash_object_id', $lesson_id ) );
if ( !empty( $rs1 ) ) {
//Only need one of the "steps" to get the appropriate achievement ID later.
$step_id = $rs1[0]->lesson;
}
$sql = "SELECT DISTINCT p2p_to as achievement_id FROM " . $wpdb->prefix . "p2p WHERE p2p_from = %d";
$rs2 = $wpdb->get_results( $wpdb->prepare( $sql, $step_id ) );
if ( !empty( $rs2 ) ) {
//We'll have our achievement ID!
$achievement_id = $rs2[0]->achievement_id;
}
if ( !empty( $achievement_id ) ) {
badgeos_maybe_award_achievement_to_user( $achievement_id, $user_id );
}
We need to do a couple queries. One using the lesson ID you have available to get the BadgeOS step it’s associated with. This is from the post meta table and a key I noticed was specific for Learndash. Next, we use that step ID and query the p2p tables that BadgeOS utilizes for associations. We select the “p2p_to” column which is the achievement, that the p2p_from step column is tied to. For both sql statements, we only need the first result from the returned values.
At this point, we check if we have an achievement ID to work with, and if we do, pass that plus the user ID into our badgeos_maybe_award_achievement_to_user()
function. It’ll check if all requirements are met etc, and if yes, award. If not, carry on.
Let me know how this works out and if it needs any tweaks.
Hi Michael-
I tried the code but it did not award any achievements. Two things:
1. Could you say if I have the plugin formatted correctly. I am no PHP expert.
2. Also, I’d be happy to hard code in the Lesson ID’s if that would make it guaranteed to work.
Here is my code:
[ Moderator note: Code fixed, please wrap code in backticks or use the code button. ]
<?php
/*
Plugin Name: Fix Grassblade Mark Complete for BadgeOS
Plugin URI:
Description: Processes BadgeOS Badges on Grassblade Mark Complete functions
Version: 0.1
Author: Matt McLean
Author URI:
License: GPLv2
*/
function ap_badge_award_grassblade($data) {
//This is the bit I got from Pankaj:
add_action("learndash_lesson_completed", function ($data) {
$lesson_id = $data["lesson"]->ID;
$user_id = $data["user"]->ID;
//Required BadgeOS Code Here
global $wpdb;
$sql = "SELECT post_id as lesson FROM " . $wpdb->postmeta . " WHERE meta_key = %s AND meta_value = %d";
$rs1 = $wpdb->get_results( $wpdb->prepare( $sql, '_badgeos_learndash_object_id', $lesson_id ) );
if ( !empty( $rs1 ) ) {
//Only need one of the "steps" to get the appropriate achievement ID later.
$step_id = $rs1[0]->lesson;
}
$sql = "SELECT DISTINCT p2p_to as achievement_id FROM " . $wpdb->prefix . "p2p WHERE p2p_from = %d";
$rs2 = $wpdb->get_results( $wpdb->prepare( $sql, $step_id ) );
if ( !empty( $rs2 ) ) {
//We'll have our achievement ID!
$achievement_id = $rs2[0]->achievement_id;
}
if ( !empty( $achievement_id ) ) {
badgeos_maybe_award_achievement_to_user( $achievement_id, $user_id );
}
Here is the response from the BadgeOS Developer:
"Well, from the looks of it, you have the user ID already, which is good. The question is where can we get the achievement ID necessary to award. It looks like you'll have the lesson ID that we can work with.
Try out the code below. Best way I could think of offhand to get the achievement ID based on lesson ID.
global $wpdb;
$sql = "SELECT post_id as lesson FROM " . $wpdb->postmeta . " WHERE meta_key = %s AND meta_value = %d";
$rs1 = $wpdb->get_results( $wpdb->prepare( $sql, '_badgeos_learndash_object_id', $lesson_id ) );
if ( !empty( $rs1 ) ) {
//Only need one of the "steps" to get the appropriate achievement ID later.
$step_id = $rs1[0]->lesson;
}
$sql = "SELECT DISTINCT p2p_to as achievement_id FROM " . $wpdb->prefix . "p2p WHERE p2p_from = %d";
$rs2 = $wpdb->get_results( $wpdb->prepare( $sql, $step_id ) );
if ( !empty( $rs2 ) ) {
//We'll have our achievement ID!
$achievement_id = $rs2[0]->achievement_id;
}
if ( !empty( $achievement_id ) ) {
badgeos_maybe_award_achievement_to_user( $achievement_id, $user_id );
}
https://gist.github.com/tw2113/08b9302d8a28de9dbfb5
This should work, assuming the user has met the requirements for the badge. That’s the only blocker I can think of over than perhaps not fetching the right IDs for the steps/achievements or not finding any at all.
Michael-
Thank you so much for your efforts. Unfortunately it is not working for me. I installed the plugin and tried 3 different lesson with Grassblade content. The Lesson is marked complete but the associated badge is not awarded. I have the badge setting set to “Complete Steps” and the step is a Learndash Lesson being completed.
Did I need to change anything in the plugin as far as entering any lesson ID numbers?
Thanks again and let me know what else I can try.
Matt
Let me do some tire kicking on this soon, I’ll see if I can get the same results. Stay tuned.
Trying to find an example of this in your site at the moment so I can get a better understanding what’s going on on that side, but am unable to find an example lesson. Could you help me out with that? You can email it if you don’t want the url public.
Sure no problem.
Go to:
http://www.yciw.net/bs/lessons/sequences-in-jazz-2/ (this one has the easiest answers)
I’ve created a user name for you. Login with: mbeckwith PW: welcome
Other lessons that use Grassblade are:
http://www.yciw.net/bs/lessons/sequences-and-scales-2/
http://www.yciw.net/bs/lessons/consonance-and-dissonance-lesson/
http://www.yciw.net/bs/lessons/3-note-harmonies/
Let me know if you need any other info.
Thank you!
Matt
Also, Michael- Grassblade has quoted me a very reasonable rate to try and debug the plugin. Should I go ahead with that or do you think you’re close?
Thank you so much!
Matt
Honestly, if they have an idea of what may be going on, I’d say go with them. Let them know that they can contact me if they need some help finding some parts in BadgeOS as well, or if they need something explained. You should have my address to CC in.
I’m not as close as I’d like to think, even if logically the code above should be working from what I can tell. This is, of course, assuming we have the right lesson ID that is tied to the BadgeOS achievement in question. I’m also completely confused by the visual editor setup your site has, but that’s just me π I’m not familiar with it.
Before we go any further I need to ask this: Is the plugin designed to take into account one particular Achievement ID and one particular Lesson ID? I can’t find in the code where those id’s would be. When you say “assuming we have the right lesson ID that is tied to the BadgeOS achievement in question”…..what do you think they are? I tried to ask this earlier so sorry if I wasn’t clear.
So for example, the badge ID would be: 29846
The lesson ID would be: 29801
Do these numbers need to go into the code somewhere??
Also, believe it or not I can’t find your email anywhere. I have only “no-reply” responses.
I know our achievements are all post types so they’re in the wp_post table. Last I checked, LearnDash uses custom post types as well for their stuff, so they have entries in the post table. When associating the two as a requirement to earn the achievement, things are being connected via IDs, especially in the p2p tables.
They’re all post type and ID powered.
Got some emails from the GrassBlade people this weekend and got those replied to this morning. Hopefully we can all find a resolution to this soon.