-Intro-
This mod creates a simple raffle where users can buy tickets and win currency. When a user buys a ticket, that amount of money goes into the pool (or jackpot if you will) and the winner gets the total amount of money in the pool. There is currently no adminCP support. The code it a bit messy and could probably be cleaned up somehow, but it works well. The code is full of comments too, so you know which part does what. It contains some unused code for stopping the raffle on a certain date that you can fix up and use if you know how, but I couldn't figure it out.
-Part 0-
If you've checked out my other mods, you know that part 0 is where I usually input the "under-the-hood" information like the database tables; so let's do that! In phpMyAdmin, we're going to create 3 new tables. First make a table called "raffle" with this information:
Now create another table called raffle_tickets with this information:
And now make a third table called raffle_winner with this information:
-Part 1-
Now we're going to go to your root folder (presumably called public_html) and make a new file called raffle.php. Inside, paste this:
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedList;
class RaffleController extends AppController{
public function index(){
$mysidia = Registry::get("mysidia");
}
}
?>
-Part 2-
Next, go to your view folder and make a new file called raffleview.php. Add this inside:
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedList;
class RaffleView extends View{
public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$document->setTitle("Raffles");
$pool = $mysidia->db->select("raffle", array("pool"))->fetchColumn();
$ticketcheck = $mysidia->db->select("raffle_tickets")->rowCount();
$rafflecheck = $mysidia->db->select("raffle")->rowCount();
$moneyvalidate = $mysidia->user->getcash();
//This part picks a winner from the database
if($mysidia->input->post("win")){
//This checks if any tickets have been bought.
if($ticketcheck < 1){
$document->add(new Comment("<font color='red'>This raffle had no entries! A winner was not chosen.</font>"));
}
else{
$winner = $mysidia->db->select("raffle_tickets", array("owner"), "1 ORDER BY RAND() DESC LIMIT 1")->fetchColumn();
$winnermoneycheck = $mysidia->db->select("users", array("money"), "username = '$winner'")->fetchColumn();
$mysidia->db->update("users", array("money" => ($winnermoneycheck + $pool)), "username = '$winner'");
$document->add(new Comment( "<font color='green'>{$winner} won {$pool} CURRENCY from the raffle!</font>"));
$mysidia->db->query("TRUNCATE TABLE adopts_raffle_tickets");
$mysidia->db->query("TRUNCATE TABLE adopts_raffle");
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
}
//This starts a new raffle. For good measure, it deletes the previous raffle before making a new one.
if($mysidia->input->post("new")){
$mysidia->db->query("TRUNCATE TABLE adopts_raffle_tickets");
$mysidia->db->query("TRUNCATE TABLE adopts_raffle");
$mysidia->db->insert("raffle", array("pool" => 0));
$document->add(new Comment( "A new raffle has been started."));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
//This part adds the number of tickets the user buys to the database
if($mysidia->input->post("buy")){
$chosenOption = $mysidia->input->post("myList");
if($chosenOption == "5"){
if($moneyvalidate < 25){
$document->add(new Comment("You cannot afford this!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
else{
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->user->changecash(-25);
$mysidia->db->update("raffle", array("pool" => ($pool + 25)));
$document->add(new Comment( "You bought 5 tickets!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
}
elseif($chosenOption == "10"){
if($moneyvalidate < 50){
$document->add(new Comment("You cannot afford this!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
else{
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->user->changecash(-50);
$mysidia->db->update("raffle", array("pool" => ($pool + 50)));
$document->add(new Comment( "You bought 10 tickets!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
}
elseif($chosenOption == "15"){
if($moneyvalidate < 75){
$document->add(new Comment("You cannot afford this!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
else{
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->user->changecash(-75);
$mysidia->db->update("raffle", array("pool" => ($pool + 75)));
$document->add(new Comment( "You bought 15 tickets!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
}
elseif($chosenOption == "20"){
if($moneyvalidate < 100){
$document->add(new Comment("You cannot afford this!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
else{
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->db->insert("raffle_tickets", array("owner" => $mysidia->user->username));
$mysidia->user->changecash(-100);
$mysidia->db->update("raffle", array("pool" => ($pool + 100)));
$document->add(new Comment( "You bought 20 tickets!"));
$document->add(new Comment("<meta http-equiv='refresh' content='1;url=raffle' />"));
return TRUE;
}
}
}
//This section can only be seen by admins!
if($mysidia->user instanceof Admin){
$document->add(new Comment( "<h2>-Admin Section-</h2>"));
$document->add(new Comment( "Choosing a winner will end the current raffle. Starting a new raffle will end the current one, but will not pick a winner."));
$adminForm = new FormBuilder("adminForm", "", "post");
$adminForm->buildButton("Choose a winner!", "win", "submit");
$adminForm->buildButton("Start new raffle", "new", "submit");
$document->add($adminForm);
}
//This checks if a raffle is going on.
if($rafflecheck < 1){
$document->add(new Comment("<font color='red'><h2>There is no raffle going on right now. Come back later!</h2></font>"));
return TRUE;
}
if($pool < 1){
$document->add(new Comment( "There is currently 0 CURRENCY in the pool!"));
$document->add(new Comment( "It costs 5 CURRENCY per ticket."));
$document->add(new Paragraph());
}
else{
$document->add(new Comment( "There is currently {$pool} CURRENCY in the pool!"));
$document->add(new Comment( "It costs 5 CURRENCY per ticket."));
$document->add(new Paragraph());
}
//This section checks the date! It's commented out since it isn't being used.
//$currenttime = time();
// $timerange = array(strtotime($this->stopdate));
// if(!empty($timerange[1]) and $currenttime > $timerange[1]){
// The raffle has ended!
// $winner = $mysidia->db->select("raffle_tickets", array("owner"), "1 ORDER BY RAND() DESC LIMIT 1")->fetchColumn();
// $document->add(new Comment( "{$winner} won {$pool} CURRENCY from the raffle! Congratulations!"));
// $endraffle = TRUE;
// }
//else{
//This is the ticket form!
$ticketForm = new FormBuilder("ticketForm", "", "post");
$exampleDropdown = new DropdownList("myList");
$exampleDropdown->add(new Option("5 tickets", "5"));
$exampleDropdown->add(new Option("10 tickets", "10"));
$exampleDropdown->add(new Option("15 tickets", "15"));
$exampleDropdown->add(new Option("20 tickets", "20"));
$ticketForm->buildComment("Amount: ", FALSE);
$ticketForm->add($exampleDropdown);
$ticketForm->buildComment("<br></br>");
$ticketForm->buildButton("Buy tickets", "buy", "submit");
$document->add($ticketForm);
//}
}
}
?>
If you've never seen "TRUNCATE" before, it empties the whole table and resets the auto-increment number back to 1. You are free to change the amount it costs to buy tickets to whatever you like, I just chose 5.
Since I couldn't figure out adminCP support, there is a section right there on the raffle page that can only be seen by admins; so users won't be able to end the raffles on their own.
-Unused code-
This is the adminCP code that I couldn't figure out how to finish. If anyone can figure it out and fix it up, be my guest. Note that both files below are in the admincp and admindcp view folder, not the root folder.
raffle.php (Please disregard the shop information. I usually copy-paste from existing files to avoid typing from scratch and running into syntax errors, and it had nothing to do with the raffle script)
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedHashMap;
class ACPRaffleController extends AppController{
public function __construct(){
parent::__construct();
$mysidia = Registry::get("mysidia");
if($mysidia->usergroup->getpermission("canmanagesettings") != "yes"){
throw new NoPermissionException("You do not have permission to manage raffles.");
}
}
public function index(){
parent::index();
$mysidia = Registry::get("mysidia");
}
public function start(){
$mysidia = Registry::get("mysidia");
$cost = $mysidia->input->post("cost");
$stopdate = $mysidia->input->post("stopdate");
if($mysidia->input->post("submit")){
$mysidia->db->insert("raffle", array("pool" => 0, "cost" => $cost, "stopdate" => $stopdate));
}
}
public function end(){
$mysidia = Registry::get("mysidia");
if(!$mysidia->input->get("sid")){
$this->index();
return;
}
elseif($mysidia->input->post("submit")){
$this->dataValidate();
$imageurl = (!$mysidia->input->post("imageurl"))?$mysidia->input->post("existingimageurl"):$mysidia->input->post("imageurl");
$salestax = (!$mysidia->input->post("salestax"))?0:$mysidia->input->post("salestax");
$mysidia->db->update("shops", array("category" => $mysidia->input->post("category"), "shopname" => $mysidia->input->post("shopname"), "description" => $mysidia->input->post("description"), "imageurl" => $imageurl,
"status" => $mysidia->input->post("status"), "restriction" => $mysidia->input->post("restriction"), "salestax" => $salestax), "sid='{$mysidia->input->get("sid")}'");
return;
}
else{
$shop = $mysidia->db->select("shops", array(), "sid='{$mysidia->input->get("sid")}'")->fetchObject();
if(!is_object($shop)) throw new InvalidIDException("nonexist");
$this->setField("shop", new DataObject($shop));
}
}
}
?>
raffleview.php
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedHashMap;
class ACPRaffleView extends View{
public function index(){
parent::index();
$stmt = $this->getField("stmt")->get();
$document = $this->document;
}
public function start(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
if($mysidia->input->post("submit")){
$document->setTitle("Successfully created raffle!");
$document->add(new Comment("You will now be redirected to the index page."));
header("Refresh:3; URL='../index'");
return;
}
$document->setTitle("Create new raffle");
$document->add(new Comment("Here you can start a new raffle. <em>You can only start a new one if there isn't one already!</em>", TRUE, "b"));
$document->add(new Paragraph);
$raffleForm = new Form("addform", "add", "post");
$raffleForm->add(new Comment("<b>Ticket Cost:</b>", FALSE));
$raffleForm->add(new TextField("cost"));
$raffleForm->add(new Paragraph);
$raffleForm->add(new Comment("<b>End Date:</b>", FALSE));
$raffleForm->add(new TextField("stopdate"));
$raffleForm->add(new Comment("<b>Note: <em>Date must follow the format (mm/dd/yyyy)!</em></b>"));
$raffleForm->add(new Button("Create raffle", "submit", "submit"));
$document->add($raffleForm);
}
public function end(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
if($mysidia->input->post("submit")){
$document->setTitle("Successfully ended raffle!");
$document->addLangvar("A winner has been chosen. You will now be redirected to the index page. Be sure to check out your site's raffle page to see who won!");
header("Refresh:3; URL='../index'");
return;
}
else{
$document->setTitle("End current raffle");
$document->add(new Comment("By clicking the button below, a winner will be chosen and the raffle will end!", TRUE, "b"));
$raffleForm = new Form("endform", "end", "post");
$raffleForm->add(new Button("End raffle", "submit", "submit"));
$document->add($raffleForm);
}
}
}
?>
-Screenshots-