Skip to content

Instantly share code, notes, and snippets.

@markward
Created April 24, 2012 13:11
Show Gist options
  • Save markward/2479554 to your computer and use it in GitHub Desktop.
Save markward/2479554 to your computer and use it in GitHub Desktop.
This function can be used to grab a url and variables from $PAGE->button in Moodle 2.X. Working through a string and deconstructing the variables seemed to be a backwards approach to me, but in practice this is much quicker than going through the Navigati
/**
* Gets URL and actions from the old-style $PAGE->button
*
* It may not look it but this is lightening fast, 10x quicker than getting the button
* from the Settings Navigation through navigation API on Moodle 2.1!
*
* @param string $button The page's edit button from $PAGE->button
* @return array containing the 'url' and 'actions'
*/
function edit_to_query($button){
global $CFG, $PAGE;
if ($button == null){
return null;
}
$active = '';
$i = 0;
$type = '';
$url = '';
$query = array();
$finalquery = array();
if($button != ''){
$parts = explode("=", $button);
foreach ($parts as $part){
$part = explode('"', $part);
if($i > 0){
switch ($type){
case 'post':
$url = $part[1];
break;
case 'name':
$name = $part[1];
break;
case 'query':
$query[] = $part[1];
break;
}
$i--;
}
if (isset($part[1])){
switch($part[1]){
case 'post':
$i = 1;
$type = 'post';
break;
case 'submit':
$i = 1;
$type = 'name';
break;
case 'hidden':
$i = 2;
$type = 'query';
break;
}
}
}
}
if ($PAGE->user_is_editing()){
$active = " active";
}
foreach ($query as $key => $qelement){
if ($key % 2 != 0){
$finalquery[$query[$key- 1]] = $query[$key];
}
}
$result['url'] = $url;
$result['actions'] = $finalquery;
return $result;
@markward
Copy link
Author

This function can be used to grab a url and variables from $PAGE->button in Moodle 2.X. Working through a string and deconstructing the variables seemed to be a backwards approach to me, but in practice this is much quicker than going through the Navigation API. On average this executes for me in 0.000017 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment