Created
October 26, 2012 04:43
-
-
Save jsolid/3956854 to your computer and use it in GitHub Desktop.
PHP - Achievement event trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Badge { | |
public function __construct() { | |
// Define all the achievements | |
Events::register('badges', array($this, 'badgeCommentator')); | |
Events::register('badges', array($this, 'badgeContributor')); | |
} | |
public function badgeCommentator($comment = '') { | |
if (!empty($comment) && strlen($comment) > 5) { | |
return 'Achievement: Badass Commentator'; | |
} | |
} | |
public function badgeContributor($param = '') { | |
//... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Comments</title> | |
<style type="text/css"> | |
</style> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="container"> | |
<form id="jForm" name="jForm" method="POST"> | |
<h3>Comments</h3> | |
<input type="text" id="comment" name="comment" maxlength="150" /> | |
<input type="button" id="btnsubmit" name="btnsubmit" value="Send" /> | |
</form> | |
</div> | |
</body> | |
</html> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
$('#btnsubmit').click(function(){ | |
$('#jForm').submit(); | |
}); | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function __construct() { | |
parent::__construct(); | |
// You can put the Badge class in application/libraries but I want to try out third_party feature of CI | |
// If you put it into application/third_party folder, please create 'config', 'controllers', 'helpers', 'language', 'libraries', 'models', 'views' folders | |
// under 'application/third_party/achievements' directory | |
$this->load->add_package_path(APPPATH.'third_party/achievements/'); | |
$this->load->library('badge'); | |
} | |
public function comment() { | |
if($_POST) { | |
$c = $this->input->post('comment'); | |
echo Events::trigger('badges', $c, 'string'); //3rd: 'array', 'json', 'serialized', or 'string' | |
} | |
$this->load->view('exp'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment