myCRED_Lottery

Codex / mycred/game / Classes / myCRED_Lottery

Description

The myCRED_Lottery class handles all lottery related information. This class does not save any information to your database, it only retrieves and processes lottery details in order for you to be able to present or retrieve.

This class requires a lottery post ID to work and only handles one lottery at a time.

 

 

Usage

$lottery = new myCRED_Lottery( $lottery_id );

 

Properties

  • $id (int)
    Lottery post ID.
  • $post (object)
    The lottery post object.
  • $prefs (array)
    This lotteries preferences.
  • $state (string)
    The lotteries current state.
  • $mycred (array)
    The myCRED_Settings Object.

 

Methods

  1. myCRED_Lottery()Constructs the lottery class and populates the lottery’s ID, preferences and loads the myCRED_Settings object.

  2. lottery_exists()Checks if a lottery exists and if the lottery is found, the lottery post object is populated. If this method is not called, the post object is not populated!

  3. default_prefs()This method holds the default settings for lotteries. It is mainly used internally though this class to populate preferences. It’s main job is to make sure there are no missing settings.

  4. get_prefs()Returns the current lotteries preferences.

  5. get_type()Returns the lottery type which is stored as a post meta under ‘mycred_lottery_type’.

  6. get_schedule_prefs()Returns the lottery schedule settings.

  7. get_game_prefs()Returns the lottery game settings.

  8. get_participation_prefs()Returns the lottery participation settings.

  9. get_winnings_prefs()Returns the lottery winnings settings.

  10. get_jackpot_prefs()Returns the lottery jackpot settings.

  11. get_exclude_prefs()Returns the lottery exclude settings.

  12. get_requirements_prefs()Returns the lottery requirements settings.

  13. get_templates_prefs()Returns the lottery templates.

  14. get_state()Returns the lotteries state which is stored as a post meta under ‘mycred_lottery_status’.

  15. get_title()Returns the lottery post title.

  16. get_content()Returns the lottery post content.

  17. can_play( $user_id, $entries, $repeat )Checks if a user can play this lottery. Returns a string value.

    • $user_id (int) required
      The user id to check.
    • $entries (int) required
      The number of entries this user would like to enter. Defaults to 1.
    • $repeat (int) required
      The number of repeats that the user would like play (if allowed). Defaults to 1.

    Returned string values:

    • play – User can play
    • visitor – User is not logged in
    • closed – Lottery is closed
    • excluded – User is excluded
    • req – User does not meet minimum requirements
    • max – User has reached heir maximum number of entries in this lottery
    • insolvent – User can not afford to play
  18. is_excluded( $user_id )Checks if a user is excluded from playing according to the “Exclude” preferences. Returns true or false.

    • $user_id (int)
      The user ID to check.
  19. fail_requirements( $user_id )Checks if a user fails to reach the minimum requirements for this lottery. Returns to true or false.

    • $user_id (int)
      The user ID to check.
  20. max_entry( $user_id )Checks if a user has reached the maximum entries allowed for this lottery according to the “Participation” settings.

    • $user_id (int)
      The user ID to check.
  21. is_insolvent( $user_id, $entries, $repeat )Checks if a user has enough points to play the number of entries and repeats that is requested. Returns true or false.

    • $user_id (int) required
      The user id to check.
    • $entries (int) required
      The number of entries this user would like to enter. Defaults to 1.
    • $repeat (int) required
      The number of repeats that the user would like play (if allowed). Defaults to 1.
  22. get_entries( $users )Returns either all lottery entries or all users with entries in this lottery. Returns an associative array.

    • $users (bool)
      If set to true, an array of user ids are returned else if it is set to false, an associative array of user ids and entries are returned.
  23. get_total_users_playing()Returns the number of users that are playing this lottery.

  24. get_total_entries()Returns the total number of entries in this lottery.

  25. get_users_entries( $user_id )Returns a given users entries.

    • $user_id (int)
      The user ID.
  26. count_users_entries( $user_id )Returns the number of entries a user has in this lottery.

    • $user_id (int)
      The user ID to check.
  27. get_ticket( $entry )Converts an entry string into an assosiative array containing the entries values.

    • $entry (string)
      The entry string.
  28. get_current_ticket_id()Returns the latest lottery ticket ID.

  29. get_ticket_play( $ticket_id, $play )Returns the users play in this lottery. If the lottery is a draw winner type of lottery, the lottery ID is shown while in pick number lotteries, the users played number are returned.

    • $ticket_id (int)
      The ticket ID.
    • $play (string)
      The users play.
  30. get_ticket_run( $repeat )Returns the number of days / weeks a play will run based on the lottery schedule.

    • $repeat (int)
      The number a lottery entry is repeated.
  31. get_current_jackpot()Returns the current jackpot (if used).

  32. increment_current_jackpot()Returns the jackpot amount based on the current jackpot incremented according to the “Jackpot” settings.

 

Lottery Parameters

  • id (int)
    Required Lottery Post ID.

 

Usage Examples

Example 1: Get the lottery post object and echo the publishing date.

$lottery_id = 1;
$lottery = new myCRED_Lottery( $lottery_id );
if ( $lottery->lottery_exists() ) {
	echo 'This lottery was published on: ' . $lottery->post->post_date;
}
else {
	echo 'Lottery not found.';
}

Example 2: Check if the current user can play the lottery and if they can not, show the appropriate template for why not.

$user_id = get_current_user_id();
$lottery_id = 1;
$lottery = new myCRED_Lottery( $lottery_id );
$can_play = $lottery->can_play( $user_id );
// can_play() will return the string 'play' if user can play
if ( $can_play == 'play' ) {
	// user can play
}
else {
	// user can not play so we get all templates
	$templates = $lottery->get_templates_prefs();
	echo $templates[ $can_play ];
}

Last edited June 21, 2016