Skip to content

Instantly share code, notes, and snippets.

@mycred
Created July 23, 2018 05:49
Show Gist options
  • Save mycred/bf29af6fdbbddf2ed2908b7a2de71c5a to your computer and use it in GitHub Desktop.
Save mycred/bf29af6fdbbddf2ed2908b7a2de71c5a to your computer and use it in GitHub Desktop.
Custom myCRED Hook for WP Courseware. Points can be awarded or deducted for users completing units, modules and courses. Requires myCRED 1.6 or higher.
/**
* WP Courseware Hook
* @version 1.0.2
*/
add_filter( 'mycred_setup_hooks', 'mycred_register_courseware_hook' );
function mycred_register_courseware_hook( $installed ) {
$installed['courseware'] = array(
'title' => 'WP Courseware',
'description' => 'Award or deduct %plural% for users completing WP Courseware courses, modules or units.',
'callback' => array( 'myCRED_Hook_CourseWare' )
);
return $installed;
}
if ( class_exists( 'myCRED_Hook' ) ) :
class myCRED_Hook_CourseWare extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
parent::__construct( array(
'id' => 'courseware',
'defaults' => array(
'unit' => array(
'creds' => 1,
'log' => '%plural% for completing unit'
),
'module' => array(
'creds' => 1,
'log' => '%plural% for completing module'
),
'course' => array(
'creds' => 1,
'log' => '%plural% for completing course'
)
)
), $hook_prefs, $type );
}
/**
* Run
* @since 1.0
* @version 1.0
*/
public function run() {
if ( $this->prefs['unit']['creds'] != 0 )
add_action( 'wpcw_user_completed_unit', array( $this, 'completed_unit' ), 10, 3 );
if ( $this->prefs['module']['creds'] != 0 )
add_action( 'wpcw_user_completed_module', array( $this, 'completed_module' ), 10, 3 );
if ( $this->prefs['course']['creds'] != 0 )
add_action( 'wpcw_user_completed_course', array( $this, 'completed_course' ), 10, 3 );
}
/**
* Unit Hook
* @since 1.0
* @version 1.0.1
*/
public function completed_unit( $user_id, $unit_id, $parent = '' ) {
// Check for exclusion
if ( $this->core->exclude_user( $user_id ) ) return;
$data = array( 'module' => $parent->parent_module_id, 'course' => $parent->parent_course_id );
// Prevent duplicates
if ( $this->has_entry( 'completing_unit', $unit_id, $user_id, $data, $this->mycred_type ) ) return;
// Execute
$this->core->add_creds(
'completing_unit',
$user_id,
$this->prefs['unit']['creds'],
$this->prefs['unit']['log'],
$unit_id,
$data,
$this->mycred_type
);
}
/**
* Module Hook
* @since 1.0
* @version 1.0.2
*/
public function completed_module( $user_id, $module_id, $unitParentData = '' ) {
// Check for exclusion
if ( $this->core->exclude_user( $user_id ) ) return;
// Prevent duplicates
if ( $this->has_entry( 'completing_module', $module_id, $user_id, '', $this->mycred_type ) ) return;
// Execute
$this->core->add_creds(
'completing_module',
$user_id,
$this->prefs['module']['creds'],
$this->prefs['module']['log'],
$module_id,
'',
$this->mycred_type
);
}
/**
* Course Hook
* @since 1.0
* @version 1.0
*/
public function completed_course( $user_id, $course_id, $unitParentData = '' ) {
// Check for exclusion
if ( $this->core->exclude_user( $user_id ) === true ) return;
// Prevent duplicates
if ( $this->has_entry( 'completing_course', $course_id, $user_id ) ) return;
// Execute
$this->core->add_creds(
'completing_course',
$user_id,
$this->prefs['course']['creds'],
$this->prefs['course']['log'],
$course_id
);
}
/**
* Preference for Coursewave Hook
* @since 1.0
* @version 1.0
*/
public function preferences() {
$prefs = $this->prefs;
?>
<label class="subheader"><?php _e( 'Units', 'textdomain' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'unit' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'unit' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['unit']['creds'] ); ?>" size="8" /></div>
</li>
<li class="empty"></li>
<li>
<label for="<?php echo $this->field_id( array( 'unit' => 'log' ) ); ?>"><?php _e( 'Log template', 'textdomain' ); ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'unit' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'unit' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['unit']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
</li>
</ol>
<label class="subheader"><?php _e( 'Modules', 'textdomain' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'module' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'module' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['module']['creds'] ); ?>" size="8" /></div>
</li>
<li class="empty"></li>
<li>
<label for="<?php echo $this->field_id( array( 'module' => 'log' ) ); ?>"><?php _e( 'Log template', 'textdomain' ); ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'module' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'module' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['module']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
</li>
</ol>
<label class="subheader"><?php _e( 'Courses', 'textdomain' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'course' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'course' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['course']['creds'] ); ?>" size="8" /></div>
</li>
<li class="empty"></li>
<li>
<label for="<?php echo $this->field_id( array( 'course' => 'log' ) ); ?>"><?php _e( 'Log template', 'textdomain' ); ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'course' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'course' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['course']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
</li>
</ol>
<?php
}
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment