By default Namaste! LMS lets you set the following requirements to complete a lesson:
- Manual admin approval
- Completing homework / assignments
- Completing quiz / test
- Manually clicking on “Mark completed” button
Since version 1.8.1 Namaste! LMS introduces new actions and filters that let you create custom plugins that will set lesson completion requirements. You can use this for example to integrate with plugin like Gravity forms, to have a game completion requirement, points earned from other plugin, or just literally everything.
Warning: programming stuff ahead.
Here are the steps you need to perform to do this with a bit of sample code:
1. Display and save your custom requirements in the Edit Lesson page. To do this you need to use the action called in lesson-meta-box.html.php:
do_action(‘namaste-lesson-requirements’, $post);
Here is how to use it:
a) In your plugin’s init() function add the output action:
add_action('namaste-lesson-requirements', 'my_plugin_lesson_requirements');
b) Create the function “my_plugin_lesson_requirements” and output checkboxes, radio buttons, drop-downs or whatever you need to do to allow the user to select custom requirements:
function my_plugin_lesson_requirements($lesson) { $current_state = get_post_meta($lesson->ID, 'my_plugin_test', true); echo 'Just a test: <input checked="checked" name="my_plugin_test" type="checkbox" value="1" />'; }
The checkbox above is just hardcoded but you should be getting the idea. If for example you are asking for some form completions, you need to list all your forms with checkboxes, radio buttons, or in a drop-down and let the editor choose.
c) In your plugin’s init() function add the save action:
add_action('save_post', 'my_plugin_save_lesson');
d) Then your function should save the selection:
function my_plugin_save_lesson($post_id) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( !current_user_can( 'edit_post', $post_id ) ) return; if ('namaste_lesson' != $_POST['post_type']) return; // sanitize and validate your data yourself!!! update_post_meta($post_id, "my_plugin_test", $_POST['my_plugin_test']); }
So far so good. You are displaying a requirement and saving it. Now you need to handle two more things:
2. Check the requirement Namaste! LMS user tries to complete a lesson. To do this you must use the namaste-lesson-other-requirements filter which Namaste! LMS calls like this:
apply_filters(‘namaste-lesson-other-requirements’, $current_status, $lesson_id, $student_id);
Here is how to do it:
a) In your plugin’s init() function declare the filter:
add_filter('namaste-lesson-other-requirements', 'my_plugin_is_lesson_ready', 10, 3);
b) Then create the function:
function my_plugin_is_lesson_ready($other_requirements, $lesson_id, $student_id) { if($other_requirements) return true; // very important!!! if other plugin has set this to true, just return true // IF there are unsatisfied requirements in your custom code, return true return false; // if no, return false }
Finally, you should take care to output proper information in the lesson-todo function and the lesson-todo shortcode:
3. Handle lesson to-do information:
To do this you need to use the “namaste-lesson-other-todo” which Namaste! LMS defines like this:
apply_filters(‘namaste-lesson-other-todo’, $other_todo, $lesson_id, $student_id);
So, step by step:
a) In your plugin’s init() method declare the filter:
add_filter('namaste-lesson-other-todo', 'my_plugin_lesson_todo', 10, 3);
b) Then create the function:
function my_plugin_lesson_todo($other_todo, $lesson_id, $student_id) { // select what this student need to do from your plugin - queries not shown here // for anything that they have to do, APPEND it to $other_todo in a "li" tag: foreach($my_plugin_items as $item) { $other_todo .= '<li>'.$item->title.'</li>'."\n"; } return $other_todo; }
It’s very important to append your output to $other_todo because other plugins may have modified the variable too. It’s also important to put each item into a “li” tag.