MyPlaza Development - A Simple Module

So we will start with a basic example of a MyPlaza module. I will basically pull apart the simpledemo.php file apart and explain how it works (hopefully, you'll get an understanding of how modules work after this).

Ignoring the comment lines, we'll skip to the code:

13
14
15
16
17
18
19
20
21
22
function simpledemo_info() {
	return array(
		"name"		=> "Simple Demo Module",
		"description"	=> "Demo module - shows the basic structure of how to make a module for MyPlaza",
		"website"	=> "http://yourwebsite.com/",
		"author"	=> "Your name here",
		"authorsite"	=> "http://authorwebsite.com/",
		"version"	=> "1.0",
	);
}
The above code sample is a fairly standard routine for all MyBB plugins and MyPlaza modules. It provides MyBB/MyPlaza with information about the plugin/module. What it tells MyBB/MyPlaza should be fairly obvious:

Note that many of the above are actually optional - you do not have to specify all of the above.
Being a little more technical, the above is a function. Functions are just a collection of code which does something. In the above case, this function simply returns some info. Since it needs to return a number of pieces of information, it's packaged toegether into an array. An array is basically a list of things, each thing separated by commas. Arrays can also have keys which can describe each item in the list. In the above example, the keys of the returned array are name, description, website and so on (note that they appear before the => operator).

If you didn't get the above, don't worry - just copy and paste it, using it as a template for your own modules.

Now, for the next piece of code:

24
25
26
27
28
29
30
31
// add your items in the _activate() function
function simpledemo_activate() {
	myplaza_add_item(array(
		"name"			=> 'Add 1000 to post count',
		"description"		=> 'Adds 1000 to your post count.',
		"cost"			=> 10.00,
	));
}

This is another function. This will be was is done when the user clicks the Activate button in the AdminCP. In this case, all we are doing when the Activate button is clicked, we are calling another function, myplaza_add_item. This function is a "pre-built" function - we don't have to worry about how it works, just that it does it's job. Again, since we are sending multiple pieces of information, we need to package it together into an array. It should be fairly obvious what the above does anyway. If you don't understand these technical details, do not worry - you can also copy and paste the above for your own modules.

Now, moving on:

33
34
35
36
// this function handles what is done when buying an item
function simpledemo_deactivate() {
	myplaza_remove_module_items();
}

As you may have guessed, this is what is done when a user clicks the Deactivate button in the AdminCP. In the above case, all we're doing is cleaning up - that is, removing any items we added when the module was activated.

Now for the next piece of code. This is probably the most complex piece of code in this module:

38
39
40
41
42
43
44
45
46
47
48
// remove all items we added earlier in the _deactivate() function
function simpledemo_run($item) {
	// add 1000 to the user's post count :)
	global $mybb;
	
	// this is a custom version of MyBB's $db->update_query
	db_update('users', array('postnum' => 'postnum + 1000'), 'uid='.intval($mybb->user['uid']));
	
	// everything successful, so return true
	return true;
}

Okay, firstly, let's look at the first line. This is a global statement. For those with some programming knowledge, it should make sense that we're trying to access a variable in the global scope within this function. For those who aren't so experienced, basically, we're trying to access a value which doesn't wholy lie within this function. In this case, we're global'ing $mybb, which is something which applies to the whole board, not just this _run() function, so to access it, we need to explicity say that it applies globally, not just to the function.

The next line executes an SQL query. Discussion about SQL and databases is beyond the scope of this document - I will assume you have knowledge of SQL. The db_update function just executes an SQL UPDATE table query. In the above example, the following MySQL query is actually executed: (assuming "mybb_" is the table prefix, and "1" is the current user's ID)

UPDATE mybb_users SET postnum = postnum + 1000 WHERE uid=1

The last line of code is simple. MyPlaza needs to know whether the purchase went successful, or failed. By returning true, we are saying that everything was successful and went well. If we were to return false, then MyPlaza would forward an error onto the user.


Take note in all the above functions, they have the simpledemo_ prefix. This comes from the filename, simpledemo. When making your own modules, be sure to prefix all your functions with the filename of your module! Also note, only numbers, letters and the underscore character (_) are allowed in filenames.