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!
Tuesday, 10 May 2016
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.
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!
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!
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!
Thursday, 3 March 2016
PHP : How to access to an object variable whose name is another variable of the object
Hello,
If we suppose that:
<?php
class test_class
{
private $variable_name = 'id';
private $id = 0;
}
How to acces to variable 'id' inside an instance? :
<?php
class test_class
{
private $variable_name = 'id';
private $id = 0;
function test() {
echo $this->$this->variable_name; // ERROR!!!!
echo $this->{$this->variable_name};
}
}
$object = new test_class();
$object->test();
Best regards!
If we suppose that:
<?php
class test_class
{
private $variable_name = 'id';
private $id = 0;
}
How to acces to variable 'id' inside an instance? :
<?php
class test_class
{
private $variable_name = 'id';
private $id = 0;
function test() {
echo $this->{$this->variable_name};
}
}
$object = new test_class();
$object->test();
Best regards!
Wednesday, 10 February 2016
Moodle: Create zip file with all course resources
Hello,
How to create a zip file with all resource modules of a course:
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once('../../config.php');
$id = required_param('id', PARAM_INT);
$course = get_course($id);
if (!$course) {
die();
}
$context = context_course::instance($course->id);
require_login($course);
$filename = 'contents.zip';
$new_names = array();
// Get files resources
$query_resources = "SELECT {resource}.*
FROM {resource}, {course_modules}, {modules}
WHERE {course_modules}.instance = {resource}.id
AND {modules}.id = {course_modules}.module
AND {modules}.name = 'resource'
AND {resource}.course = ?
AND {course_modules}.course = ?";
$resources = $DB->get_records_sql($query_resources, array($course->id, $course->id));
foreach ($resources as $resource) {
$cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
$context_resource = context_module::instance($cm->id);
$fs = get_file_storage();
$resource_files = $fs->get_area_files($context_resource->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
if (count($resource_files) < 1) continue;
$resource_file = reset($resource_files);
unset($resource_files);
$contenthash = $resource_file->get_contenthash();
$l1 = $contenthash[0].$contenthash[1];
$l2 = $contenthash[2].$contenthash[3];
$filedir = ((isset($CFG->filedir))) ? $CFG->filedir : $CFG->dataroot.'/filedir';
$path = "$filedir/$l1/$l2/$contenthash";
$new_names['/'.$resource->name.'/'.$resource_file->get_filename()] = $path;
}
// End get files resources
// Create zip file
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$result = $packer->archive_to_storage($new_names, $context->id, 'local_zip', 'zip_files', 0, '/', $filename);
// End Create zip file
print_r($result);
Best regards,
Iban Cardona.
How to create a zip file with all resource modules of a course:
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once('../../config.php');
$id = required_param('id', PARAM_INT);
$course = get_course($id);
if (!$course) {
die();
}
$context = context_course::instance($course->id);
require_login($course);
$filename = 'contents.zip';
$new_names = array();
// Get files resources
$query_resources = "SELECT {resource}.*
FROM {resource}, {course_modules}, {modules}
WHERE {course_modules}.instance = {resource}.id
AND {modules}.id = {course_modules}.module
AND {modules}.name = 'resource'
AND {resource}.course = ?
AND {course_modules}.course = ?";
$resources = $DB->get_records_sql($query_resources, array($course->id, $course->id));
foreach ($resources as $resource) {
$cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
$context_resource = context_module::instance($cm->id);
$fs = get_file_storage();
$resource_files = $fs->get_area_files($context_resource->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
if (count($resource_files) < 1) continue;
$resource_file = reset($resource_files);
unset($resource_files);
$contenthash = $resource_file->get_contenthash();
$l1 = $contenthash[0].$contenthash[1];
$l2 = $contenthash[2].$contenthash[3];
$filedir = ((isset($CFG->filedir))) ? $CFG->filedir : $CFG->dataroot.'/filedir';
$path = "$filedir/$l1/$l2/$contenthash";
$new_names['/'.$resource->name.'/'.$resource_file->get_filename()] = $path;
}
// End get files resources
// Create zip file
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$result = $packer->archive_to_storage($new_names, $context->id, 'local_zip', 'zip_files', 0, '/', $filename);
// End Create zip file
print_r($result);
Best regards,
Iban Cardona.
Tuesday, 2 February 2016
Moodle: Use config.php vars (CFG) loading minimum data
Hello,
Sometimes, in a custom moodle script or in your local plugin you need $CFG vars but you don't need all moodle features.
The best option is write:
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
define('NO_DEBUG_DISPLAY', true);
define('ABORT_AFTER_CONFIG', true);
require_once('../../config.php');
...
With this code, you could, for example, execute echo $CFG->dbhost
Best regards,
Iban Cardona.
Sometimes, in a custom moodle script or in your local plugin you need $CFG vars but you don't need all moodle features.
The best option is write:
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
define('NO_DEBUG_DISPLAY', true);
define('ABORT_AFTER_CONFIG', true);
require_once('../../config.php');
...
With this code, you could, for example, execute echo $CFG->dbhost
Best regards,
Iban Cardona.
Subscribe to:
Posts (Atom)