Sunday, 9 November 2014

Moodle: get top category from course data

Hello!

If we have course data and we want to get top category from that, we can do this:

$category = $DB->get_record('course_categories', array('id' => $courseObj->category));
$arrCats = explode("/", ($category->path));
$topCategory = $DB->get_record('course_categories', array('id' => intval($arrCats[1])));


Best regards!

Sunday, 26 October 2014

Moodle: purge all caches by console

Hello,

If we have a moodle installed, a very interesting method to delete all caches is to use system console. To do that, we have to go to our moodle route and execute:

php admin/cli/purge_caches.php

Best regards!

Saturday, 18 October 2014

Backup with rsync

Hello,

Today I set up a bash script in order to create backups with rsync. I created it in a OS 10.7.x (Lion) environment. Steps I did are:
Best regards!

Wednesday, 10 September 2014

PHP: Clean uploaded filenames

Hello,

When a file is uploaded to a server, you could have filenames with characters that can cause problems. To clear the filename you can use:

$filename = time().'_'.$_FILES["file"]["name"]; // File name.
$filename = preg_replace('/[[:^print:]]/', '', $filename); // Replace illegal chars


Regards!

Wednesday, 9 July 2014

Laravel 4: Create multi language page with session

Laravel is a strong PHP framework that is widely used. Recently, I was developing with that framework and I needed to create a website with content in 2 languages: catalan and spanish.

To solve this problem you can use some options... like url with language or save language in session. I decided to use the last one.

I created this code:

filters.php

App::before(function($request) {
// We use the locale of the session
if ( Session::has('locale') ) { App::setLocale(Session::get('locale')); }
});

BaseController.php

class BaseController extends Controller {

public function switchLocale($lang) {
// We change the locale lang in the session and redirect back (see filter language in filters.php).
if ( in_array($lang, Config::get('app.available_language')) ) {
Session::put('locale', $lang);
}
return Redirect::back();
}
}

app.php

'locale' => 'es',
'fallback_locale' => 'ca',
'available_language' => array('es', 'ca'),

Finally,  in our view we use this code in the links used to change the language:

Catalan -> URL::action('BaseController@switchLocale', array('ca'))
Spanish -> URL::action('BaseController@switchLocale', array('es'))

Wednesday, 4 June 2014

felogin: actions to do after login

Sometimes inside a TYPO3 website after install and config felogin, we would like to do some actions everytime that user would acces to website. To do that, we can use an extension hook. In this example we use TYPO3 4.5.32 and 1.3.1 felogin.

Inside ext_localconf.php of our extension:

$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['felogin']['login_confirmed'][] = 'EXT:test_extension/hooks/class.feloginhook.php:test_extension_hooksHandler->onLogin';

Inside class.feloginhook.php:

class test_extension_hooksHandler { public function onLogin() { // Actions to do here... Insert in a database, in a log... redirections... } }

Regards!

Thursday, 15 May 2014

jQuery: Send form by ajax

Hello,

Sometimes we want to send form data but we don't want to reload page. In this case best option is send data by ajax and jQuery. In this example we used version 1.10.2 of jQuery.

HTML code:

<form id="FORMID" accept-charset="UTF-8" action="action.php" method="POST"> <input type="text" name="username" id="username" placeholder="Write username"> </form>;

Javascript code:

$('#FORMID').on('submit', function(e)  {
$.post(
$(this).prop('action'),
{
"username": $(this).find('input[name=username]').val()
},
function(data) {
alert(data.result);
},
'json'
); return false;
});


php (action.php) code:

<?php
$username = $_POST['username'];
$ret = array();
$ret['result'] = 'OK';
return json_encode($ret);

Best regards!