Monday, 15 May 2017

Moodle & PHPUnit: Capture events

Hello,

If you want to tests events in Moodle using PHPUnit, the best method is:

public function test_creationg_event() {
        $this->resetAfterTest();
        $event_sink = $this->redirectEvents();

        $course = $this->getDataGenerator()->create_course();

        $events = $event_sink->get_events();
        $event_sink->close();
        $event = $events[0];
        $this->assertInstanceOf('\core\event\course_created', $event);
}

Best regards!

Tuesday, 25 April 2017

Moodle: Block settings not shown for students

Hello,

If you want to disable (not shown) settings block for students in Moodle, you can do it changing the renderer in your theme. This code is valid only for themes not based on Boost.

Add this function in the core_renderer class of your theme:

public function block(block_contents $bc, $region) {
        global $DB;

        $idblock = isset($bc->attributes['id']) ? $bc->attributes['id'] : '';
        $arr_aux = explode('inst', $idblock);
        if (isset($arr_aux[1])) {
                $block_instance = $DB->get_record('block_instances', array('id' => $arr_aux[1]));
                $context_course = context_course::instance($this->page->course->id);
                if (isset($block_instance->blockname) && $block_instance->blockname == 'settings') {
                    if (!has_capability('moodle/grade:viewall', $context_course)) {
                        return '';
                    }
                }
        }

        return parent::block($bc, $region);
}

Best regards,
Iban Cardona.

Wednesday, 22 February 2017

Moodle: moodle_form inside module: Edit or new?

Hello,

Sometimes inside a custom Moodle module we need to know if we are editing or creating a new module (activity or resource) instance... Here the solution:

function definition() {
        global $CFG, $DB;
        $mform =& $this->_form;

        if (is_numeric($this->_instance) && $this->_instance && $mymodule = $DB->get_record("mymodule", array("id"=>$this->_instance))) {
            $mform->addElement('header', 'general', 'Editing');
        } else {
            $mform->addElement('header', 'general', 'Creating');
        }

...

Best regards,
Iban Cardona.

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!