<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Damian Gostomski &#124; Freelance Web DevelopmentCodeIgniter &#187; Damian Gostomski | Freelance Web Development</title>
	<atom:link href="http://www.gostomski.co.uk/category/codeigniter/feed" rel="self" type="application/rss+xml" />
	<link>http://www.gostomski.co.uk</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 21:24:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Simple stats collection with CodeIgniter hooks</title>
		<link>http://www.gostomski.co.uk/codeigniter/simple-stats-collection-with-codeigniter-hooks</link>
		<comments>http://www.gostomski.co.uk/codeigniter/simple-stats-collection-with-codeigniter-hooks#comments</comments>
		<pubDate>Tue, 02 Mar 2010 20:30:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.gostomski.co.uk/?p=9</guid>
		<description><![CDATA[Yesterday, I launched the alpha version of Project Trackr, a web based project management system I&#8217;m working on. As well as the feedback I would receive from the users themselves, I wanted some additional data. I wanted to track the users journey throughout the site and to get some quantitative data on on the usage<a href="http://www.gostomski.co.uk/codeigniter/simple-stats-collection-with-codeigniter-hooks" class="more-info">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I <a href="http://projecttrackr.com/alpha/">launched the alpha version of Project Trackr</a>, a web based project management system I&#8217;m working on. As well as the feedback I would receive from the users themselves, I wanted some additional data. I wanted to track the users journey throughout the site and to get some quantitative data on on the usage of the application. With this information, I would know which features are popular are which features aren&#8217;t being used, and I could then try to find out if they&#8217;re not being used because they&#8217;re not useful, their implementation is poor or people just don&#8217;t know about it.</p>
<p><span id="more-9"></span></p>
<p>But surely I could find out this sort of stuff from something like Google analytics? Not really, the URLs are unique to each project, task, milestone, file etc. I also wanted the raw numerical data so that I could query it however I wanted.<br />
My immediate thought was to log the current activity at the start of each function in my controllers, but this would be time consuming, and as a developer, we always want the simplest solution for the job. So I thought about how this could be automated.</p>
<h3>Hooks</h3>
<p>Seeing as I want this code to execute for every request, then surely a hook is the best solution. You could also put the following code inside the constructor of a super controller, which I tend to prefer over using hooks, but in this case I felt hooks where the best solution, as the code executed in them isn&#8217;t directly related to the execution of the controller, so I&#8217;d rather keep the 2 separated. You can <a href="http://codeigniter.com/user_guide/general/hooks.html">read more about hooks</a> in the user guide.</p>
<h3>Enough talk, show me the code</h3>
<p>Ok, so first up, we need to enable hooks, which are disabled by default. This is simply a case of changing a value in the configuration file. Open up system/application/config/config.php and find the line</p>
<pre class="brush: php; title: ;">$config['enable_hooks'] = FALSE;</pre>
<p>and replace it with</p>
<pre class="brush: php; title: ;">$config['enable_hooks'] = TRUE;</pre>
<p>Next up, we need to define our hook point. As stated in the documentation for hooks, there are 8 different hook points available. The hook point you choose will affect the results you get. If you use a pre system hook, you won&#8217;t have the necessary libraries loaded, if you use something list a post system hook, then it will never get executed if you have a redirect in your code. This was very important to me, as I use redirects after all insert operations, so I wouldn&#8217;t actually log any new data being entered if I used the posst system hook. In the end, I went for the <strong>post controller constructor</strong> hook. The code for the hook looks like the following, and should go in system/application/config/hooks.php</p>
<pre class="brush: php; title: ;">
$hook['post_controller_constructor'] = array(
	'class'    =&gt; 'Statistics',
	'function' =&gt; 'log_activity',
	'filename' =&gt; 'Statistics.php',
	'filepath' =&gt; 'hooks'
);
</pre>
<p>You may change the items within the array if you wish, but remember to use your values for the rest of this.</p>
<p>Next, we need to decide what we actually want to track. In my case, I wanted the following:</p>
<ul>
<li>Account, project and user ID. These allow me to query events at a user, project and account level</li>
<li>Section &#8211; The part of the app the request occurs in, this is the controller</li>
<li>Action &#8211; What the user is doing, this is the method in the controller</li>
<li>When &#8211; A timestamp of when this occured, so I can query across date rannges</li>
<li>URI &#8211; The URI string, as it may contain other useful information, such as item IDs etc</li>
</ul>
<p>Based on this, I have the following SQL schema for my statistics table</p>
<pre class="brush: sql; title: ;">
CREATE TABLE IF NOT EXISTS `statistics` (
	`id` int(11) NOT NULL AUTO_INCREMENT,
	`account_id` int(11) NOT NULL,
	`project_id` int(11) NOT NULL,
	`user_id` int(11) NOT NULL,
	`section` varchar(32) NOT NULL,
	`action` varchar(32) NOT NULL,
	`when` int(11) NOT NULL,
	`uri` varchar(255) NOT NULL,
	PRIMARY KEY (`id`)
)
</pre>
<p>And lastly, the actual code for the hook which is pretty self explanatory</p>
<pre class="brush: php; title: ;">
&lt;?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Statistics {
	public function log_activity() {
		// We need an instance of CI as we will be using some CI classes
		$CI =&amp; get_instance();

		// Start off with the session stuff we know
		$data = array();
		$data['account_id'] = $CI-&gt;session-&gt;userdata('account_id');
		$data['project_id'] = $CI-&gt;session-&gt;userdata('project_id');
		$data['user_id'] = $CI-&gt;session-&gt;userdata('user_id');

		// Next up, we want to know what page we're on, use the router class
		$data['section'] = $CI-&gt;router-&gt;class;
		$data['action'] = $CI-&gt;router-&gt;method;

		// Lastly, we need to know when this is happening
		$data['when'] = time();

		// We don't need it, but we'll log the URI just in case
		$data['uri'] = uri_string();

		// And write it to the database
		$CI-&gt;db-&gt;insert('statistics', $data);
	}
}
</pre>
<p>And that&#8217;s it! I hope this was of use to you, let me know in the comments if you have any possible improvements to this (There must be plenty, but this is just an example of something basic). And even better, share how you&#8217;re using this in your applications, how are you querying the data to help you?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gostomski.co.uk/codeigniter/simple-stats-collection-with-codeigniter-hooks/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Getting full auto complete with CodeIgniter in Eclipse</title>
		<link>http://www.gostomski.co.uk/codeigniter/getting-full-auto-complete-with-codeigniter-in-eclipse</link>
		<comments>http://www.gostomski.co.uk/codeigniter/getting-full-auto-complete-with-codeigniter-in-eclipse#comments</comments>
		<pubDate>Wed, 20 Jan 2010 20:25:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.gostomski.co.uk/?p=4</guid>
		<description><![CDATA[CodeIgniter is a brilliant PHP framework and Eclipse is a brilliant IDE, so if I use them both, then it will be super brilliant? Not quite. In CodeIgniter, if you want to use one of the many fabulous libraries, such as the Database Active Record class, then you need to load it (using autoload or<a href="http://www.gostomski.co.uk/codeigniter/getting-full-auto-complete-with-codeigniter-in-eclipse" class="more-info">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a title="CodeIgniter" href="http://codeigniter.com/">CodeIgniter</a> is a brilliant PHP framework and <a title="Eclipse" href="http://www.eclipse.org/">Eclipse</a> is a brilliant IDE, so if I use them both, then it will be super brilliant? Not quite. In CodeIgniter, if you want to use one of the many fabulous libraries, such as the Database Active Record class, then you need to load it (using autoload or manually) but that happens at run time. What this means is Eclipse (and most IDEs for that matter) don&#8217;t know what $this-&gt;db is which means you don&#8217;t get auto complete.</p>
<p>This isn&#8217;t really a problem, but it&#8217;s a great place to have a couple of key strokes here and there, and we programmers are all about <span style="text-decoration: line-through;">laziness</span> efficiency. In order to enable auto completion, you need to follow these simple steps (I assume you already have Eclipse installed and a CodeIgniter project set up. I will also assume the default directory structure&#8230; if you know how to change it around I assume you&#8217;ll know where to find the relevant files)<span id="more-4"></span></p>
<ol>
<li>If you&#8217;re running CodeIgniter on PHP4 edit system/codeigniter/Base5.php and if you&#8217;re running PHP5, edit system/codeigniter/Base4.php</li>
<li>Find the constructor &#8211; It’s the function called CI_Base</li>
<li>Paste the following in at the end of it</li>
</ol>
<pre class="brush: php; title: ;">
$agent = new CI_User_agent();
$benchmark = new CI_Benchmark();
$calendar = new CI_Calendar();
$cart = new CI_Cart();
$config = new CI_Config();
$db = new CI_DB_active_record();
$email = new CI_Email();
$encrypt = new CI_Encrypt();
$form_validation = new CI_Form_validation();
$ftp = new CI_FTP();
$image_lib = new CI_Image_lib();
$input = new CI_Input();
$lang = new CI_Language();
$output = new CI_Output();
$pagination = new CI_Pagination();
$parser = new CI_Parser();
$session = new CI_Session();
$table = new CI_Table();
$trackback = new CI_Trackback();
$typography = new CI_Typography();
$unit = new CI_Unit_test();
$upload = new CI_Upload();
$uri = new CI_URI();
$xmlrpc = new CI_Xmlrpc();
$xmlrpcs = new CI_Xmlrpcs();
$zip = new CI_Zip();
</pre>
<p>You should now have full autocomplete support for all the CodeIgniter libraries. It&#8217;s also easy to do the same for your custom libraries and models.</p>
<p>If you have any comments on this, an easier way to get auto complete to work in Eclipse or one where you don&#8217;t need to define everything manually then do share it in the comments</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gostomski.co.uk/codeigniter/getting-full-auto-complete-with-codeigniter-in-eclipse/feed</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
	</channel>
</rss>

