Jake
Member
  
Posts: 58
Joined: May 2009
Reputation: 1
|
RE: [MyPS] MyRaffle
Code:
<?php
/**
* [MyPS] MyRaffle
*
* http://mybbsource.com
*
* Copyright 2009 Nickman
*
* */
if(!defined("IN_MYBB")) {
die("This file cannot be accessed directly.");
}
$plugins->add_hook('misc_start', 'myraffle_do');
$plugins->add_hook("admin_config_menu", "myraffle_admin_nav");
$plugins->add_hook("admin_load", "myraffle_admin");
$plugins->add_hook("admin_config_action_handler", "myraffle_action_handler");
function myraffle_info() {
return array(
"name" => "Lottery!",
"description" => "Can you win the Jackpot?",
"website" => "http://mybbsource.com",
"author" => "Nickman",
"authorsite" => "http://mybbsource.com",
"version" => "1.0"
);
}
function myraffle_activate() {
global $db;
require MYBB_ROOT.'/inc/adminfunctions_templates.php';
$db->query("CREATE TABLE `".TABLE_PREFIX."raffles` (
`cid` INT NOT NULL AUTO_INCREMENT ,
`title` TEXT NOT NULL ,
`prize` INT NOT NULL ,
`max_entries` INT NOT NULL ,
`status` INT NOT NULL ,
`excluded_groups` TEXT NOT NULL ,
`cost` INT NOT NULL ,
`winner_uid` INT NOT NULL ,
PRIMARY KEY ( `cid` )
) ");
$db->query("CREATE TABLE `".TABLE_PREFIX."raffle_entries` (
`eid` INT NOT NULL AUTO_INCREMENT ,
`cid` INT NOT NULL ,
`uid` INT NOT NULL ,
PRIMARY KEY ( `eid` )
) ");
}
function myraffle_deactivate() {
global $db;
$db->query("DROP TABLE ".TABLE_PREFIX."raffles");
$db->query("DROP TABLE ".TABLE_PREFIX."raffle_entries");
}
function myraffle_do()
{
global $db,$mybb,$header,$theme,$footer,$headerinclude;
if ($mybb->input['action'] != 'raffle')
{
return;
}
//let's make sure MyPS is up and running
if ($mybb->settings['myps_status'] != 1)
{
error("Please make sure MyPS is enabled");
}
if ($mybb->user['uid'] == '')
{
error_no_permission();
}
$points=$mybb->settings['myps_name'];
//we are viewing a raffle
if ($mybb->input['cid'] != '' AND $mybb->input['do'] == '')
{
$cid=intval($mybb->input['cid']);
$raffle=$db->fetch_array($db->simple_select("raffles","*","cid='$cid'"));
if ($raffle['status'] == 1)
{
error("This Lottery Draw has closed");
}
//let's check their usergroup to see if they are excluded
if (raffle_check($mybb->user['usergroup'],$raffle['excluded_groups']) === false OR $mybb->user['uid'] == '')
{
error_no_permission();
}
//alright, they are good.
add_breadcrumb("Entering a raffle", "misc.php?action=raffle&cid=$cid");
$yourEntries=$db->num_rows($db->simple_select("raffle_entries","*","cid='$cid' AND uid='{$mybb->user['uid']}'"));
$total=$db->num_rows($db->simple_select("raffle_entries","*","cid='$cid'"));
$chance=($total > 0)?round($yourEntries/$total,2)*100:0;
$left=$raffle['max_entries']-$yourEntries;
$html.="<html xml:lang=\"en\" lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head>
<title>{$mybb->settings['bbname']} - {$raffle[title]}</title>
{$headerinclude}
</head>
<body>
{$header}
<table border=\"0\" cellspacing=\"{$theme['borderwidth']}\" cellpadding=\"{$theme['tablespace']}\" class=\"tborder\" width=\"100%\">
<tr>
<td colspan='2' class=\"thead\" colspan=\"$cells\"><strong>$raffle[title] (You have {$mybb->user['myps']} $points remaining!) </strong></td></tr>
<tr>
<td class='trow1' width='20%'>Prize</td><td class='trow1'>$raffle[prize] $points</td></tr><tr>
<td class='trow2'>Cost Per Ticket</td><td class='trow2'>$raffle[cost] $points</td></tr><tr>
<td class='trow1'>Maximum Entries</td><td class='trow1'>$raffle[max_entries]</td></tr><tr>
<td class='trow2'>Your Entries Remaining</td><td class='trow2'>$left</td></tr>
<td class='trow1'>Total Entries</td><td class='trow1'>$total</td></tr><tr>
<td class='trow2'>Your Chance of winning</td><td class='trow2'>$chance%</td></tr>";
if ($left != 0)
{
$html.="<tr><td class='tcat' colspan='2'><a href='misc.php?action=raffle&my_post_key={$mybb->post_code}&do=buy&cid=$cid'>Enter Now</a></td></tr>";
}
$html.="</table>{$footer}</body></html>";
output_page($html);
exit();
}
elseif ($mybb->input['cid'] != '' AND $mybb->input['do'] == 'buy')
{
$cid=intval($mybb->input['cid']);
$raffle=$db->fetch_array($db->simple_select("raffles","*","cid='$cid'"));
if (raffle_check($mybb->user['usergroup'],$raffle['excluded_groups']) ===false OR $mybb->user['uid'] == '')
{
error_no_permission();
}
if ($mybb->user['myps'] < $raffle['cost'])
{
error("You do not have enough $points to purchase a ticket");
}
$yourEntries=$db->num_rows($db->simple_select("raffle_entries","*","cid='$cid' AND uid='{$mybb->user['uid']}'"));
if ($yourEntries+1 > $raffle['max_entries'])
{
error("Uh-oh... You cant purchase any more Lottery tickets");
}
if ($raffle['status'] == 1)
{
error("Uh-oh!... This lottery draw is over");
}
verify_post_check($mybb->input['my_post_key']);
//they are ok, get them a ticket
$db->insert_query("raffle_entries",array("cid"=>$cid,"uid"=>$mybb->user['uid']));
$new=$mybb->user['myps']-$raffle[cost];
$db->update_query("users",array("myps"=>$new),"uid='{$mybb->user['uid']}'");
redirect("misc.php?action=raffle&cid=$cid","You have purchased a ticket!");
}
//we list of available contests
elseif ($mybb->input['cid'] == '')
{
add_breadcrumb("Viewing Lottery Draws", "misc.php?action=raffle");
$html.="<html xml:lang=\"en\" lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head>
<title>{$mybb->settings['bbname']} - Viewing Lottery Draws</title>
{$headerinclude}
</head>
<body>
{$header}
<table border=\"0\" cellspacing=\"{$theme['borderwidth']}\" cellpadding=\"{$theme['tablespace']}\" class=\"tborder\" width=\"100%\">
<tr>
<td colspan='3' class=\"thead\" colspan=\"$cells\"><strong>Currently open</strong></td></tr>
<tr>
<td class='tcat'>Title</td><td class='tcat'>Cost</td><td class='tcat'>Prize</td></tr>";
$getRaffles=$db->simple_select("raffles","*","status='0'");
$i=1;
$any=false;
while ($raffle = $db->fetch_array($getRaffles))
{
//lets check if they can access them
if (raffle_check($mybb->user['usergroup'],$raffle['excluded_groups']) !== false)
{
$any=true;
$html.="<tr class='trow$i'><td><a href='misc.php?action=raffle&cid=$raffle[cid]'>$raffle[title]</a></td><td>$raffle[cost]</td><td>$raffle[prize]</td></tr>";
}
$i=($i == 1)?2:1;
}
if (!$any)
{
$html.="<tr class='trow1'><td colspan='3'>Currently no Lottery draws planned</td></tr>";
}
$html.="</table>{$footer}</body></html>";
output_page($html);
}
}
function myraffle_action_handler(&$action)
{
$action['myraffle'] = array('active' => 'myraffle', 'file' => '');
}
function myraffle_admin_nav(&$sub_menu)
{
global $mybb, $lang;
end($sub_menu);
$key = (key($sub_menu))+10;
if(!$key)
{
$key = '50';
}
$sub_menu[$key] = array('id' => 'myraffle', 'title' => "Lottery", 'link' => "index.php?module=config/myraffle");
}
function myraffle_admin()
{
global $mybb, $db, $page, $lang;
if($page->active_action != "myraffle")
{
return;
}
$do=$mybb->input['do'];
$sub_tabs['raffles'] = array(
'title' => "Raffles",
'description' => "Show Lotto Draws",
'link' => "index.php?module=config/myraffle"
);
$sub_tabs['create'] = array(
'title' => "Create Raffle",
'description' => "Create a new Draw",
'link' => "index.php?module=config/myraffle&do=create"
);
$page->add_breadcrumb_item("MyRaffle", "index.php?module=config/MyRaffle");
$page->output_header("MyRaffle");
if ($do =='')
{
$page->output_nav_tabs($sub_tabs, "raffles");
$table = new Table;
$table->construct_header("Title");
$table->construct_header("Status");
$table->construct_header("Winner");
$table->construct_header("Options");
//get the raffles
$getraffles=$db->simple_select("raffles","*");
while ($raffle = $db->fetch_array($getraffles))
{
$table->construct_cell($raffle['title']);
$status=($raffle['status'] == 1)?'Closed':'Open';
$table->construct_cell($status);
if ($raffle['winner_uid'] != '0')
{
$winner=$db->fetch_array($db->simple_select("users","username","uid='{$raffle[winner_uid]}'"));
$table->construct_cell("<a href='index.php?module=user/users&action=edit&uid={$raffle[winner_uid]}'>$winner[username]</a>");
}
else
{
$table->construct_cell("N/A");
}
$popup=new PopupMenu("id_$raffle[cid]","Options");
if ($raffle['status'] != 1)
{
$popup->add_item("Close draw and pick us a winner!","index.php?module=config/myraffle&do=close&cid={$raffle['cid']}");
}
$popup->add_item("Edit","index.php?module=config/myraffle&do=edit&cid={$raffle['cid']}");
$popup->add_item("Delete Raffle","index.php?module=config/myraffle&do=delete&cid={$raffle['cid']}");
$table->construct_cell($popup->fetch(),array('class' => 'align_center'));
$table->construct_row();
}
if ($table->num_rows() < 1)
{
$table->construct_cell("No Raffles",array("colspan"=>3));
$table->construct_row();
}
$table->output("Raffles");
$page->output_footer();
}
if ($do == 'create')
{
if ($_POST['title'] == '')
{
$page->output_nav_tabs($sub_tabs, "create");
$form = new Form("index.php?module=config/myraffle&do=create", "post", "add");
$form_container = new FormContainer("New Raffle");
$form_container->output_row("Title", "", $form->generate_text_box('title','', array('id' => 'title')), 'title');
$form_container->output_row("Cost Per Ticket", "", $form->generate_text_box('cost','', array('id' => 'cost')), 'cost');
$form_container->output_row("Maximum Tickets", "", $form->generate_text_box('max','', array('id' => 'max')), 'max');
$form_container->output_row("Prize", "", $form->generate_text_box('prize','', array('id' => 'prize')), 'prize');
$form_container->output_row("Excluded Groups", "Set the GIDs, comma separated", $form->generate_text_box('excluded_groups','', array('id' => 'excluded_groups')), 'excluded_groups');
$form_container->end();
$buttons[] = $form->generate_submit_button("Create");
$form->output_submit_wrapper($buttons);
$form->end();
$page->output_footer();
}
else
{
$title=$db->escape_string($_POST['title']);
$cost=intval($_POST['cost']);
$max=intval($_POST['max']);
$prize=intval($_POST['prize']);
$excluded=$db->escape_string($_POST['excluded_groups']);
$db->insert_query("raffles",array("title"=>$title,"cost"=>$cost,"max_entries"=>$max,"prize"=>$prize,"excluded_groups"=>$excluded));
flash_message("Raffle added", 'success');
admin_redirect("index.php?module=config/myraffle");
}
}
if ($do == 'delete')
{
$cid=intval($_GET['cid']);
$db->query("DELETE FROM ".TABLE_PREFIX."raffles WHERE cid='$cid'");
$db->query("DELETE FROM ".TABLE_PREFIX."raffle_entries WHERE cid='$cid'");
flash_message("Raffle deleted", 'success');
admin_redirect("index.php?module=config/myraffle");
}
if ($do == 'close')
{
$cid=intval($_GET['cid']);
$contest=$db->fetch_array($db->simple_select("raffles","*","cid='$cid'"));
//first we choose a winner
$win=$db->fetch_array($db->query("SELECT * FROM ".TABLE_PREFIX."raffle_entries WHERE cid='$cid' ORDER BY rand() LIMIT 1"));
$winner=$db->fetch_array($db->simple_select("users","myps,username","uid='$win[uid]'"));
//close the contest
$db->update_query("raffles",array("status"=>1,"winner_uid"=>$win[uid]),"cid='$cid'");
//give the winner his/her points
$points=$mybb->settings['myps_name'];
$new=$winner['myps']+$contest['prize'];
$db->update_query("users",array("myps"=>$new),"uid='$win[uid]'");
//Now we send the winner a nice PM. Aren't we sweet? :P
require_once MYBB_ROOT."inc/datahandlers/pm.php";
$message="You have won the {$contest[title]} lottery! This entitles you to [b]{$contest[prize]} {$points}[/b]. Congratulations and dont spend it all at once!";
$pmhandler = new PMDataHandler();
$pm = array(
"subject" => "[System] = You have won a Lottery Draw!",
"message" => $message,
"icon" => -1,
"fromid" => -1,
"toid" => array(intval($mybb->user['uid'])),
"bccid" => '',
"do" => '',
"pmid" => ''
);
$pm['saveasdraft'] = 0;
$pmhandler->admin_override = 1;
$pmhandler->set_data($pm);
if($pmhandler->validate_pm())
{
$pmhandler->insert_pm();
}
flash_message("Raffle Closed. The winner was $winner[username]", 'success');
admin_redirect("index.php?module=config/myraffle");
}
if ($do == 'edit')
{
$cid=intval($_GET['cid']);
if ($_POST['title'] == '')
{
$raffle=$db->fetch_array($db->simple_select("raffles","*","cid='$cid'"));
$page->output_nav_tabs($sub_tabs, "raffles");
$form = new Form("index.php?module=config/myraffle&do=edit", "post", "add");
$form_container = new FormContainer("Edit Raffle");
$form_container->output_row("Title", "", $form->generate_text_box('title',$raffle['title'], array('id' => 'title')), 'title');
$form_container->output_row("Cost Per Ticket", "", $form->generate_text_box('cost',$raffle['cost'], array('id' => 'cost')), 'cost');
$form_container->output_row("Maximum Tickets", "", $form->generate_text_box('max',$raffle['max_entries'], array('id' => 'max')), 'max');
$form_container->output_row("Prize", "", $form->generate_text_box('prize',$raffle['prize'], array('id' => 'prize')), 'prize');
$form_container->output_row("Excluded Groups", "Set the GIDs, comma separated", $form->generate_text_box('excluded_groups',$raffle['excluded_groups'], array('id' => 'excluded_groups')), 'excluded_groups');
echo ($form->generate_hidden_field("cid",$cid));
$form_container->end();
$buttons[] = $form->generate_submit_button("Edit Raffle");
$form->output_submit_wrapper($buttons);
$form->end();
$page->output_footer();
}
else
{
$cid=intval($_POST['cid']);
$title=$db->escape_string($_POST['title']);
$cost=intval($_POST['cost']);
$max=intval($_POST['max']);
$prize=intval($_POST['prize']);
$excluded=$db->escape_string($_POST['excluded_groups']);
$db->update_query("raffles",array("title"=>$title,"cost"=>$cost,"max_entries"=>$max,"prize"=>$prize,"excluded_groups"=>$excluded),"cid='$cid'");
flash_message("Raffle edited", 'success');
admin_redirect("index.php?module=config/myraffle");
}
}
}
function raffle_check($tocheck,$in)
{
$in=explode(",",$in);
if (in_array($tocheck,$in))
{
return false;
}
else
{
return true;
}
}
?>
I wanted to have this sent by a specific username I have called System, but when I entered the UID and tested the PM's were not being sent after a user had won
Then, I logged into my Admin account to find all the PM's were sent there
Any help?
I hope you dont mind the Minor Edits my Adminsitrator made
(This post was last modified: 06-26-2009, 06:33 AM by Jake)
|
|