moodle php Programming: context course has_capbility moodle php teacher
by bseanvt
2 comments
How to Check if the Current Logged In User can Edit a Course in MOODLE Using the Has_capability Function
After an hour of fruitless searching through the docs and forums, I finally found an answer to my simple question, of course buried in the depths of the shark infested, murky water that is the Moodle code base .
How do I check if a user is a course creator for a given course? I’m still not 100% sure, the access control policy seems needlessly confusing on first glance and even after reading the heady and mostly pointless, expose on how roles/perms are calculated (Available Here: http://docs.moodle.org/en/How_permissions_are_calculated ) If you notice the section titled “Some Practical Examples” is 100% empty! (I’ve added my code to the wiki so not 100% empty any longer) The most useful part of the docs are completely empty! Instead boatloads of time went into a treatise on the internals and lovely graphs like this one 
But you could always hop into the forum dedicated just to this chunk of the code base (Available here: http://moodle.org/mod/forum/view.php?f=941&page=0) and search through the 500 topics w/ 5000+(i didn’t actually count) nested discussions (including version 1.7 of moodle as well).
No, instead, ACK came to the rescue and I found a snippet that performed the task.
Without further ado
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('moodle/course:update', $coursecontext)) {
/** do stuff here */
}
There it is. That could have saved me an hour if mentioned in the docs!
Moodle's Most Important Function Gets No Attention
Arguably the most important function in the Moodle API, is the create_course function. One would think… after all Moodle is an LMS. Courses are the bread and butter for course management platforms, right? Taking a look at the function in the course/lib.php file reveals almost nothing about the function! WTF? … btw, the function starts at line 3260
/**
* Create a course and either return a $course object or false
*
* @param object $data - all the data needed for an entry in the 'course' table
*/
function create_course($data) {
global $CFG, $USER;
/** omitted for your sanity */
}
Wow, I think I know less about this function now than before I went digging into the code. How do I assign a teacher to my new course, how do I populate the data object it takes as a parameter (is there a course setup function)? Since courses aren’t really objects in Moodle (just glorified arrays), am I just supposed to set them up by hand… $object->param = val? Are there default values and/or required attributes that need to be set, otherwise my program is going to come to a halt? What about student enrollments? No example to help for how one might actually use this function? Do I have to really read through the code, starting at around line 3260, then jump between the rest of this monstrous 4000 line piece of spaghetti coded file and try and make sense of it works (somehow).
How is it possible that the most important function (since the core problem Moodle solves is creating courses) in the entire application has no meaningful documentation? I think I have an answer. The entire code base is soooo convoluted and a giant waste of CPU cycles, that it would too embarrassing to actually document how it even works.
Programming: api book review moodle MySQL php software development
by bseanvt
leave a comment
My Review of Moodle 1.9 Extension Development
I wrote a review for Joseph Thibault’s Moodle News on extension development for Moodle. The book is quite good and I think an essential resource for anyone wanting to develop in Moodle. The book focuses on plugin development, but it will also give you an overview of the architecture, api and best practices.
I wish I had this book about 3 years ago when I first started fooling around in the code base! At any rate, you can read the review on Moodle News at http://www.moodlenews.com/2010/moodle-1-9-extension-development-review-by-bseanvt/
There is also an interesting discussion underway about the ‘bloat’ in the Moodle code at http://www.moodlenews.com/2010/opinion-1000000-lines-of-code/
Ruby on Rails: alias apache feedzirra installation mod rails passenger passenger app root path prefix php phussion ree relative url rails routing ruby enterprise edition subdirectory VirtualHost Wordpress
by bseanvt
2 comments
Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition
Here is how to install a Rails application out of a subdirectory (rather than as a subdomain) with the Apache web server(Apache2). In this example I’m going to use my own blog which is a WordPress installation and serve a Rails application from the subdirectory “reader”. Note, I’m not going to keep my Rails application in the document root of my WordPress Blog, which is a PHP application and therefore anyone could browse the ruby source code :(. I’ll keep it elsewhere on the filesystem and tell Apache about the location in the VirtualHost file.
You can visit the application by going to http://seanbehan.com/reader. The application just parses a bunch of RSS feeds and displays them.
It uses the Feedzirra library, which I’ve also written about http://seanbehan.com/ruby-on-rails/installing-feedzirra-rss-parser-on-ubuntu-8/.
I’m using Phussion Passenger and Ruby Enterprise Edition from the folks at Mod Rails. Installing both Phussion Passenger and Ruby Enterprise Edition is simple and a very well documented process. However, you’ll need to download and compile them from source. If you install Ruby Enterprise Edition (REE) it comes w/ Passenger so you won’t need to download it separately. I recommend going with REE. http://www.rubyenterpriseedition.com/download.html
Installing Ruby Enterprise Edition
Here are the commands to download and install (change the X.X.X to the package you’ve actually downloaded).
wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-1.8.7-2010.01.tar.gz tar xzvf ruby-enterprise-X.X.X.tar.gz ./ruby-enterprise-X.X.X/installer
When you run the installer you’ll be prompted for an installation location. Just hit enter to install in the default location. Follow the instructions from there and remember to copy/paste any code that they give you.
Official Instructions on installation for Passenger on its own are available here http://www.modrails.com/install.html I’ve written about setting up an entire box w/ Passenger here http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rails-git-and/ If you already have Passenger installed and want to use REE just download and install REE and it’ll recompile Passenger with REE support if you follow the instructions.
*** If you install REE you’ll need to either link or reinstall all your gems. I linked the REE gem with the one in /usr/bin so that I can run gem install
ln -s /opt/ruby-enterprise-X.X.X/bin/gem /usr/bin/gem
The VirtualHost
If you have Passenger and REE successfully installed you’ll need to modify your VirtualHost file and add Rails application information to it.
<VirtualHost *>
# Normal virtual host info
ServerName seanbehan.com
ServerAlias *.seanbehan.com
DocumentRoot /var/www/seanbehan.com/wordpress
# Rails info goes here
Alias /reader /var/www/seanbehan.com/reader/public
<Location /reader>
PassengerAppRoot /var/www/seanbehan.com/reader
RailsEnv production
</Location>
</VirtualHost>
The “Location” directive tells apache to forward requests starting with /reader to the directory
/var/www/seanbehan.com/reader/public which is the location of our Rails app. However, we need to add the PassengerAppRoot assignment so that it knows where the actual application lives.
Routing in Rails with Relative Path
And finally, your Rails application will need to be aware of the relative url prefix assigned to each route. Normally, you could do this w/ the :path_prefix at the individual route level like so
map.resources :feeds, :path_prefix => "reader"
This will work but you can add a line to your RAILS_ROOT/config/environments/production.rb file which will handle all your routes for you. This way you don’t need to setup a relative path on your development environment work.
# in RAILS_ROOT/config/environments/production.rb config.action_controller.relative_url_root = '/reader'
Final Thoughts
Remember Ruby Enterprise Edition is the new Ruby Interpreter that your Rails web applications are using. If you
have any gem or rake issues make sure that you’re using the same interpreter that REE is using. Look in the location
installation of REE “/opt/ruby-ent…” bin/gem or bin/rake and see if that helps. I just linked those to the standard /usr/bin/gem and /usr/bin/rake and everything worked fine.
Also I’ve read some people have trouble using the alias with passenger. This may be an older issue but works for me without a problem on Ubuntu (latest).
Here are some useful resources I found along the way…
http://robots.thoughtbot.com/post/159806388/phusion-passenger-with-a-prefix
http://www.modrails.com/documentation/Users%20guide.html#deploying_rails_to_sub_uri
http://www.modrails.com/documentation/Users%20guide.html#RailsBaseURI
http://stackoverflow.com/questions/848258/server-prefix-and-rails-routes
Generate MySQL Datetime Type Using PHP Date() Function
If you want to insert a datetime that matches the default mysql datetime type format use this
date('Y-m-d H:i:s');
PHP Paypal IPN Script
Simple script for posting a valid response back to Paypal service after a sale is completed using the Paypal Instant Checkout payment method.
<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
} else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
I grabbed this script from Paypal Docs… I never can find the info I need w/in their site so I’m posting here for reference. https://www.paypal.com/us/cgi-bin/webscr?cmd=p/pdn/ipn-codesamples-pop-outside#php
Listing Files and Directories with PHP
Listing all files and directories using PHP 5.
<?php
$files = array(); $dir = dir(".");
while(false!==($file=$dir->read())):
if(($file{0}!=".") && ($file{0}!="~") && (substr($file, -3)!="LCK")
&& ($file!=basename($_SERVER["PHP_SELF"]))):
$files[$file] = stat($file); //
endif;
endwhile;
$dir->close(); ksort($files); ?>
<table border=1 cellpadding=5>
<tr><th>Name</th><th>Size</th><th>Date</th></tr>
<?php foreach($files as $name => $stats): ?>
<tr>
<td><?php print $name;?></td>
<td><?php print $stats['size']; ?></td>
<td><?php print date("m-d-Y h:ia", $stats['mtime']); ?></td>
</tr>
<?php endforeach; ?></table>
Highlight String in PHP
Function for highlighting text/strings in PHP.
$content = file_get_contents("http://php.net/");
print highlight("PHP", $content);
function highlight($match, $string){
return str_ireplace($match, "<span style='background:yellow'>$match</span>", $string);
}
Will output something like this

Highlighted PHP.Net
***Notice all the styles are gone :( … would need to parse the document body to maintain stylesheet and other ‘php’ strings that might be in resources paths.
php: absolute links file_get_contents php preg_replace_callback regex relative links
by bseanvt
1 comment
Absolutize Relative Links Using PHP and Preg_Replace_Callback
I was in the market for a simple php script to replace hrefs with their absolute paths from scraped web pages. I wrote one myself. I used the preg_replace_callback function so that I could pass the parsed results as a single variable.
<?php
$domain = "http://seanbehan.com";
$pattern = "/\bhref=[\"|'](.*?)[\"|']/";
$string = file_get_contents($domain);
// prepends relative links w/ $domain skips returns the match if already absolute
function replace_href($match){
global $domain;
if(substr($match[1], 0, 7)!=="http://" && substr($match[1],0,8)!=="https://"){
return "href='".$domain.$match[1]."'";
} else {
return "href='".$match[1]."'s";
}
}
print preg_replace_callback($pattern, "replace_href", $string);
Wordpress: javascript php plugins text editor tinymce Wordpress
by bseanvt
leave a comment
TinyMCE Rich Text Editor: HELLO EDITOR Plugin Tutorial and Example
I wanted to create a button for the TinyMCE Rich Text Editor for WordPress. It was tough to find good docs on the subject. There are a couple of useful posts out there but in general I found them lacking.
http://codex.wordpress.org/TinyMCE_Custom_Buttons#Creating_an_MCE_Editor_Plugin
The above resource has a good section on the PHP code needed to write your own callbacks but there is no mention of the JavaScript required to create a button and interact with it. Links are available for more reading but why not also post a code sample to get up and running?
Anyway, I made a simple plugin after hobbling a bunch of resources together to get a button in the text editor that when clicked triggers an alert to the window. The code isn’t fancy but it might provide a starting off point for a larger project or be useful as a resource for some of the basic ideas involved. I read through a couple of the sample plugins included with WordPress along with the Viper Video Quicktags Plugin, which makes extensive use of the feature. However, they were too complex for a quick, basic understanding.
This plugin doesn’t do a lot. I’ll probably flesh it out a bit more when I have a clearer understanding of how it all works. For now here are some code samples and at the bottom of this post there is a link to the .zip file for the plugin which you can install on your own site.
In wp-content/plugins/hello_editor/index.php
<?php
/*
Plugin Name: Hello Editor
Plugin URI: http://seanbehan.com/wordpress/tinymce-rich-text-editor-hello-editor-plugin-tutorial-and-example/
Description: A simple plugin showing how to add a button to the rich text editor in WordPress.
The plugin doesn't do anything except place a button and respond to onclick event, with "Hello Editor".
Most of the editor plugins available are too 'functional' to quickly read and see how to do the
basics. This plugin doesn't 'do' anything, rather it makes it simple to read what is going on.
Version: 0.1
Author: Sean Behan
Author URI: http://seanbehan.com
*/
define( "HELLO_EDITOR_PLUGIN_DIR", "hello_editor" );
define( "HELLO_EDITOR_PLUGIN_URL", "/wp-content/plugins/" . HELLO_EDITOR_PLUGIN_DIR );
// Register the external pllugin from the .js file
function hello_editor_register_external_plugin($plugin_array) {
$plugin_array['HELLO_EDITOR'] = HELLO_EDITOR_PLUGIN_URL . '/editor_plugin.js';
return $plugin_array;
}
// Add the button to the array ***NOTE*** The name here "HELLO_EDITOR" must
//match the name in the editor plugin file for the addButton(name_of_button) function!
function hello_editor_register_button($buttons) {
array_push( $buttons, "|", "HELLO_EDITOR" );
return $buttons;
}
// Filters which will call our functions
function hello_editor_init(){
add_filter("mce_external_plugins", "hello_editor_register_external_plugin");
add_filter('mce_buttons', 'hello_editor_register_button');
}
// When the editor is initialized
add_action('init', 'hello_editor_init');
In wp-content/plugins/hello_editor/editor_plugin.js
(function() {
//Init the plugin
tinymce.create('tinymce.plugins.HELLO_EDITOR', {
init : function( ed, url ) {
//addButton name needs to match $plugin_array['HELLO_EDITOR'] in add_filter function
ed.addButton('HELLO_EDITOR', {
title : 'Hello Editor',
image : url + "/buttons/hello.png",
onclick : function(){
alert("HELLO EDITOR!");
}
});
},
//Info about the plugin
getInfo : function() {
return {
longname : "Sean Behan's Sample Hello Editor Plugin",
author : 'Sean Behan',
authorurl : 'http://www.seanbehan.com/',
infourl : 'http://seanbehan.com/wordpress/tinymce-rich-text-editor-hello-editor-plugin-tutorial-and-example/',
version : "0.1"
};
}
});
tinymce.PluginManager.add('HELLO_EDITOR', tinymce.plugins.HELLO_EDITOR);
})();
You’ll also need a directory called buttons inside the hello_editor plugin directory with a hello.png file which will be displayed. Without it you’ll just get a blank button. You can rename this but remember to change the name in the editor_plugin.js file as well.
You can download the sample plugin here http://seanbehan.com/wp-content/uploads/2009/11/hello_editor.zip


