Saturday, 7 January 2017

Moodle: Download exact commit from git

Hello,

If you want to manage a source code from Moodle of a specific commit, you need to do:

  • Install git: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
  • Find the commit to download in https://github.com/moodle/moodle . In my case I found this: https://github.com/moodle/moodle/commit/89117976b8afd75c588b8b3908f763aab2edfb6a#diff-5bbd3ccd25214f9956eea7e9f714bc08
  • Download the Moodle code: git clone https://github.com/moodle/moodle.git my_moodle
  • Copy the ID of the commit: 89117976b8afd75c588b8b3908f763aab2edfb6a
  • Checkout the commit:
    • cd my_moodle
    • git checkout 89117976b8afd75c588b8b3908f763aab2edfb6a

Best regards! 

Monday, 22 August 2016

Laravel : Get SQL of a DB object

Hello,

How to print SQL (Select statement) of DB Object in Laravel:

$sql = DB::table('users');
echo $sql->toSql();

Best regards,
Iban Cardona.

Thursday, 28 July 2016

Laravel 4.2 Add get params in URL::action helper function

Hello,

If we want to add get params we need to call function like this:

routes.php

Route::any('test/{id}',  'Controller@index');

blade.php

{{URL::action("Controller@index", array(111, "firstparam" => "aaa"))}}

And result is:
http://www.dummyurl.local/test/111?firstparam=aaa

Best regards,
Iban Cardona.

Tuesday, 10 May 2016

PHP: String HH:MM:SS to hours (float)

Hello,

How to parse string with format HH:MM:SS to hours:

$string = '01:58:03';
sscanf($string, "%d:%d:%d",  $hours, $minutes, $seconds);
$hours = $hours + $minutes/60 + $seconds/3600;

Best regards!
 

Thursday, 28 April 2016

Laravel 5.2: Create multilang site (Session+Middleware)

Hello,

How to create a multi-language site in Laravel 5.2:

config/app.php

'locale' => 'ca',
'locales' => ['ca' => 'Català', 'es' => 'Español', 'en' => 'English'],

app/Http/routes.php

Route::group(['middleware' => ['web']], function () {
    Route::get('/', [
        'as' => 'underconstruction', 'uses' => 'Underconstruction\UnderconstructionController@showIndex'
    ]);
    Route::get('/lang/{langcode?}', function ($langcode = 'ca') {
        return redirect()->route('underconstruction');
    });
});


app/Http/Kernel.php

/**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \App\Http\Middleware\LangMiddleware::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];


/app/Http/Middleware/LangMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;
use App;

class LangMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $lang = $request->segment(1);
        $locale = $request->segment(2);

        if ($lang == 'lang' && array_key_exists($locale, app()->config->get('app.locales'))) {
            $request->session()->put('locale', $locale);
        } else if ($request->session()->has('locale')) {
            App::setLocale($request->session()->get('locale'));
        }

        return $next($request);
    }
}


/app/Http/Controllers/Underconstruction/UnderconstructionController.php

<?php

namespace App\Http\Controllers\Underconstruction;

use App\Http\Controllers\Controller as Controller;

class UnderconstructionController extends Controller
{
    public function showIndex() {
        return app()->getLocale();
    }
}


If you access to http://yourweb/ --> lang = ca
if you access to http://yourweb/lang/es --> lang = es
If you access to http://yourweb/ --> lang =es

Lang is saved in session data.

Best regards,
Iban Cardona.

Thursday, 31 March 2016

Init class of a unnamed namespace inside other namespace

Hello,

How to init a class without namespace in another class with namespace:

<?php

class first_class {
   public function  __construct() {
   }
}

And:

<?php

namespace namespace1\namespace2;

class second_class {
         public function  __construct() {
         }

         public function test() {
                $object = first_class(); --> ERROR!!!!
                $object = \first_class(); --> OK
         }
}

Best regards!

Wednesday, 30 March 2016

Moodle: Get courses recursively

Hello,

How to get all courses of a category recursively?

<?php

global $CFG;
require_once($CFG->dirroot . '/lib/coursecatlib.php');
$categoryid = 1;
$allcourses = coursecat::get($categoryid)->get_courses(array('recursive' => true));

How to get all courses of a hidden category?

<?php

global $CFG;
require_once($CFG->dirroot . '/lib/coursecatlib.php');
$categoryid = 1;
$allcourses = coursecat::get($categoryid, MUST_EXIST, true)->get_courses(array('recursive' => true));

Best regards!