How to auto complete on global variables in Moodle in Netbeans? - moodle

If you are using Netbeans with Moodle, then the autocomplete doesn't seem to work with the global variables eg: $DB, $PAGE and $OUTPUT.

Discovered there is a very easy solution - just make sure the correct types are declared for each of the global variables in the phpdoc for the function.
eg:
/**
* Returns something fancy
*
* #global moodle_database $DB
* #global moodle_page $PAGE
* #global core_renderer $OUTPUT
*/
function local_myfancyfunction() {
global $DB, $PAGE, $OUTPUT;
...
Now autocomplete will work by simply typing $DB-> for example.

Related

how to generate Frontend URI in Scheduler Command (TYPO3 9)

What is the best way to generate frontend URIs in a scheduler command in TYPO3 v9.
I have seen attempts by initializing the TSFE manually, but for me this seems fishy.
Are there any other ways?
The proper way to create links in any context (FE/BE/CLI) is by using the PageRouter. This router is always attached to a site, so you will need to retrieve the correct site first, e.g. by using the SiteFinder. After that you can use PageRouter::generateUri().
Complete example:
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageUid);
$arguments = [
'foo' => 1,
];
// E.g.: "https://example.org/slug-of-page/?foo=1"
$uri = (string)$site->getRouter()->generateUri((string)$pageUid, $arguments);
Notice that this API knows nothing about Extbase and passes through $arguments to the URI so if you need to mimic the behavior of the Extbase UriBuilder you'll need to do that yourself:
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ExtensionService;
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$extensionService = $objectManager->get(ExtensionService::class);
// E.g. "tx_acme_test"
$argumentsPrefix = $extensionService->getPluginNamespace($extensionName, $pluginName);
$arguments = [
$argumentsPrefix => [
'action' => $actionName, // E.g. "bar"
'controller' => $controllerName, // E.g. "Foo"
'foo' => 42,
],
];
// E.g.: "https://example.org/slug-of-page/?tx_acme_test[action]=bar&tx_acme_test[controller]=Foo&tx_acme_test[foo]=42"
// Or with a route enhancer: "https://example.org/slug-of-page/detail/slug-of-foo"
$uri = (string)$site->getRouter()->generateUri((string)$pageUid, $arguments);
Depending on your needs, the native way described by #mathias-brodala might not be sufficient. In those cases you will need a proper TSFE otherwise you do not have all the TypoScript settings that might influence link generation.
For these cases you basically need to create an instance of the TypoScriptFrontendController yourself, call setup methods and inject real or mocked dependencies like FrontendUserAuthentication depending on your usecase.
A working solution for TYPO3v9, for instance, can be found in the
rx_scheduled_social extension.

generate comments for function by emacs

The function :
function show($string = "hi") {
echo $string;
}
I need the comments like:
/**
* #brief
*
* #param $string
*
* #returns
*/
function show($string = "hi") {
echo $string;
}
Can I generate comments like this by yasnippet?
In vim, doxygentoolkit can do it.
Whitch tool i can use in emacs?
Use Doxymacs. It hasn't been updated in a while, but still works for me.

Slim Framework - Using a GET route

I'm absolutely new to Slim Framework. I'm working on an Webservice that should provide an interface between an Android App and a Web-Application. I used the Slim Documentation to make my first steps and now I want to create a simple GET route, to receive information from the App. Here is what I have so far:
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/:name', function ($name) use($app) {
$name_outside = $name;
echo $name;
});
$app->run();
echo $name_outside;
I need to access the variable :name outside the function, but what I get is nothing. What I am doing wrong here?
Btw: I know that GET-routes usually are used to list existing resources, but for my simple case, I decided to use it that way.
Fix your code to hold name as args parameters ,then you can get in in Your function
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/{name}', function ($args) use($app) {
$name_outside = $args['name'];
echo $args['name'];
});
$app->run();
To acess the $name_outside inside the function context you pass it.
$app->get('/session/program_name/:name', function ($name) use($app, &$name_outside) {
$name_outside = $name;
echo $name;
});
But perhaps you're using Slim in a wrong way. Why you have to access the variable outside of your route?
No code is execyted after the run() call. That's the way slim works, try to put a die where you're echo the variable, it isn't reachable.
You shouldn't have the need to access the context of the route outside of it this way. To transform a request or a response you use middlewares with the hooks.

CakeEmail::viewVars - CakePHP

I am trying to set the variable $purchase to use in my email template per the following configuration:
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->emailFormat('html');
$email->template('new_order_email', 'default');
$email->from(array('info#mydomain.com' => 'A Great Site'));
$email->to($this->request->data['email']);
$email->subject('Order details');
$email->viewVars($purchase);
$email->send();
But this doesn't work when I attempt to use the $purchase variable in the template. Instead, the email which is sent contains the following error:
Notice (8): Undefined variable: purchase
[APP/View/Emails/html/new_order_email.ctp, line 2]
This indicates that $purchase is not available, even though I did set that variable using the CakeEmail:viewVars(); function. Any ideas why I am having this problem?
Use this:
$email->viewVars(array('purchase' => $purchase));
the quickest and shortest way:
$email->viewVars(compact('purchase'));
it will pass the var $purchase as the same key 'purchase' into the email viewVars and is exactly what you need here.
you can also quickly add more variables this way:
$html = 'foo';
$url = '/my/url';
$email->viewVars(compact('purchase', 'html', 'url'));
Look at my answer Here
$emial->viewVars($valiables);
in your email template print this. then you will be able to see all your variables :)
var_dump($this->viewVars);

how to build query string in zend framework?

I'm trying to build a query string as following:
Next Page
I want to add an array to query string. For example, array('find_loc'=>'New+York', 'find_name'=>'starbucks')
I expect to get url that looks like http://example.com/1/?find_loc=New+York&find_name=starbucks
What's the best way to do this? I found a similar question that suggested appending the string to the url. Is there a helper for query string?
Simple answer to your question is no.
Here is the class description:
/**
* Helper for making easy links and getting urls that depend on the routes and router
*
* #package Zend_View
* #subpackage Helper
* #copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
Helper for making easy links and getting urls that depend on the routes and router
I think the description is clear in it's purpose. Use it for making URLs that depend on the routes and router. So, just append your query strings as recommend in the link you posted in your question.
The following should work for you:
Next Page
The ZF-Router will map the values to the Request object.
In your controller you can access these params with the Response-Object:
$loc = $this->getRequest()->getParam('find_loc');
$name = $this->getRequest()->getParam('find_name);
You can make custom helper:
class My_View_Helper_UrlHttpQuery extends Zend_View_Helper_Abstract
{
public function urlHttpQuery($query)
{
$urlHelper = $this->view->getHelper('url');
$params = func_get_args();
array_shift($params);//removing first argument
$url = call_user_func_array(($urlHelper, 'url'), $params);
if(!is_string($query)) { //allow raw query string
$query = array($query);
$query = http_build_query($query);
}
if(!empty($query) {
$url .= '?' . ltrim('?', $query);
}
return $url;
}
}
After you register this helper with view, you can use it like this Next Page
Working code
/**
* Class Wp_View_Helper_UrlHttpQuery
*/
class Wp_View_Helper_UrlHttpQuery extends Zend_View_Helper_Abstract
{
public function urlHttpQuery($query = array())
{
$urlHelper = $this->view->getHelper('url');
$params = func_get_args();
//removing first argument
array_shift($params);
$url = call_user_func_array(array($urlHelper, 'url'), $params);
if (is_array($query) || is_object($query)) {
$query = http_build_query($query);
}
if (!empty($query)) {
$url .= '?' . ltrim($query, '?');
}
return $url;
}
}
since the upstream code doesn't work