Skip to content

Commit

Permalink
MDL-27829 blocks: altered the algorithm used to find plugin page types
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjdavis committed Jun 27, 2011
1 parent b822fc8 commit b38e2e2
Show file tree
Hide file tree
Showing 39 changed files with 180 additions and 117 deletions.
2 changes: 1 addition & 1 deletion blog/lib.php
Expand Up @@ -1035,7 +1035,7 @@ function blog_comment_validate($comment_param) {
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function blog_pagetypelist($pagetype, $parentcontext, $currentcontext) {
function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
return array(
'*'=>get_string('page-x', 'pagetype'),
'blog-*'=>get_string('page-blog-x', 'blog'),
Expand Down
2 changes: 1 addition & 1 deletion course/lib.php
Expand Up @@ -4218,7 +4218,7 @@ protected function notify($touser, $fromuser, $name='courserequested', $subject,
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function course_pagetypelist($pagetype, $parentcontext, $currentcontext) {
function course_page_type_list($pagetype, $parentcontext, $currentcontext) {
// if above course context ,display all course fomats
list($currentcontext, $course, $cm) = get_context_info_array($currentcontext->id);
if ($course->id == SITEID) {
Expand Down
Expand Up @@ -2,5 +2,5 @@

$string['completion:view'] = 'View course completion report';
$string['completiondate']='Completion date';
$string['pluginpagetype'] = 'Any completion course report';
$string['pluginpagetype'] = 'Completion course report';
$string['pluginname']='Course completion';
15 changes: 15 additions & 0 deletions course/report/completion/lib.php
Expand Up @@ -44,3 +44,18 @@ function completion_report_extend_navigation($navigation, $course, $context) {
}
}
}

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function completion_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype'),
'course-report-completion-*' => get_string('pluginpagetype', 'coursereport_completion')
);
return $array;
}
20 changes: 6 additions & 14 deletions course/report/lib.php
Expand Up @@ -16,31 +16,23 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This file contains functions used by the outline report
* This file contains functions used by course reports
*
* @since 2.1
* @package course-report
* @copyright 2011 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function coursereport_pagetypelist($pagetype, $parentcontext, $currentcontext) {
$array = array('*'=>get_string('page-x', 'pagetype'),
'course-report-*'=>get_string('page-course-report-x', 'pagetype')
);

//extract course-report-outline from course-report-outline-index
$bits = explode('-', $pagetype);
if (count($bits >= 3)) {
$report = array_slice($bits, 2, 1);
$array['course-report-'.$report[0].'-*'] = get_string('pluginpagetype', 'coursereport_'.$report);
}

function coursereport_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype')
);
return $array;
}
2 changes: 1 addition & 1 deletion course/report/log/lang/en/coursereport_log.php
Expand Up @@ -27,5 +27,5 @@
$string['log:view'] = 'View course logs';
$string['log:viewlive'] = 'View live logs';
$string['log:viewtoday'] = 'View today\'s logs';
$string['pluginpagetype'] = 'Any log course report';
$string['pluginpagetype'] = 'Log course report';
$string['pluginname'] = 'Logs';
15 changes: 15 additions & 0 deletions course/report/log/lib.php
Expand Up @@ -537,3 +537,18 @@ function log_report_extend_navigation($navigation, $course, $context) {
$navigation->add(get_string('pluginname', 'coursereport_log'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
}
}

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function log_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype'),
'course-report-log-*' => get_string('pluginpagetype', 'coursereport_log')
);
return $array;
}
2 changes: 1 addition & 1 deletion course/report/outline/lang/en/coursereport_outline.php
Expand Up @@ -24,5 +24,5 @@
*/

$string['outline:view'] = 'View course activity report';
$string['pluginpagetype'] = 'Any outline course report';
$string['pluginpagetype'] = 'Course activity report';
$string['pluginname'] = 'Course activity';
15 changes: 15 additions & 0 deletions course/report/outline/lib.php
Expand Up @@ -37,4 +37,19 @@ function outline_report_extend_navigation($navigation, $course, $context) {
$url = new moodle_url('/course/report/outline/index.php', array('id'=>$course->id));
$navigation->add(get_string( 'activityreport' ), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
}
}

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function outline_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype'),
'course-report-outline-*' => get_string('pluginpagetype', 'coursereport_outline')
);
return $array;
}
Expand Up @@ -24,5 +24,5 @@
*/

$string['participation:view'] = 'View course participation report';
$string['pluginpagetype'] = 'Any participation course report';
$string['pluginpagetype'] = 'Participation course report';
$string['pluginname'] = 'Course participation';
15 changes: 15 additions & 0 deletions course/report/participation/lib.php
Expand Up @@ -37,4 +37,19 @@ function participation_report_extend_navigation($navigation, $course, $context)
$url = new moodle_url('/course/report/participation/index.php', array('id'=>$course->id));
$navigation->add(get_string('participationreport'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
}
}

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function participation_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype'),
'course-report-participation-*' => get_string('pluginpagetype', 'coursereport_participation')
);
return $array;
}
2 changes: 1 addition & 1 deletion course/report/progress/lang/en/coursereport_progress.php
Expand Up @@ -24,5 +24,5 @@
*/

$string['pluginname'] = 'Activity completion';
$string['pluginpagetype'] = 'Any progress course report';
$string['pluginpagetype'] = 'Progress course report';
$string['progress:view'] = 'View activity completion reports';
15 changes: 15 additions & 0 deletions course/report/progress/lib.php
Expand Up @@ -49,3 +49,18 @@ function progress_report_extend_navigation($navigation, $course, $context) {
$navigation->add(get_string('pluginname','coursereport_progress'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
}
}

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function progress_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype'),
'course-report-progress-*' => get_string('pluginpagetype', 'coursereport_progress')
);
return $array;
}
2 changes: 1 addition & 1 deletion course/report/stats/lang/en/coursereport_stats.php
Expand Up @@ -24,5 +24,5 @@
*/

$string['pluginname'] = 'Course statistics';
$string['pluginpagetype'] = 'Any stats course report';
$string['pluginpagetype'] = 'Statistics course report';
$string['stats:view'] = 'View course statistics report';
15 changes: 15 additions & 0 deletions course/report/stats/lib.php
Expand Up @@ -91,3 +91,18 @@ function stats_report_extend_navigation($navigation, $course, $context) {
}
}
}

/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function stats_page_type_list($pagetype, $parentcontext, $currentcontext) {
$array = array(
'*' => get_string('page-x', 'pagetype'),
'course-report-*' => get_string('page-course-report-x', 'pagetype'),
'course-report-stats-*' => get_string('pluginpagetype', 'coursereport_stats')
);
return $array;
}
123 changes: 52 additions & 71 deletions lib/blocklib.php
Expand Up @@ -1563,57 +1563,72 @@ function generate_page_type_patterns($pagetype, $parentcontext = null, $currentc

$bits = explode('-', $pagetype);

$function = 'default_pagetypelist';

$core = get_core_subsystems();
$plugins = get_plugin_types();

//progressively strip pieces off the page type looking for a match
//eg: for pagetype "course-report-outline-index" try coursereportoutlineindex
// then coursereportoutline then coursereport then course
$componentarray = null;
for($i = count($bits); $i > 0; $i--) {
$componentarray = array_slice($bits, 0, $i);
$component = implode($componentarray);

// Look for special case components like course reports
$libfile = $CFG->dirroot.'/'.implode('/', $componentarray).'/lib.php';
if (file_exists($libfile)) {
require_once($libfile);
if (function_exists($component.'_pagetypelist')) {
$function = $component.'_pagetypelist';
break;
}
}
for ($i = count($bits); $i > 0; $i--) {
$possiblecomponentarray = array_slice($bits, 0, $i);
$possiblecomponent = implode('', $possiblecomponentarray);

// Then check to see if the component is a core component
if (array_key_exists($component, $core) && !empty($core[$component])) {
$libfile = $CFG->dirroot.'/'.$core[$component].'/lib.php';
// Check to see if the component is a core component
if (array_key_exists($possiblecomponent, $core) && !empty($core[$possiblecomponent])) {
$libfile = $CFG->dirroot.'/'.$core[$possiblecomponent].'/lib.php';
if (file_exists($libfile)) {
require_once($libfile);
if (function_exists($component.'_pagetypelist')) {
$function = $component.'_pagetypelist';
break;
$function = $possiblecomponent.'_page_type_list';
if (function_exists($function)) {
if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) {
break;
}
}
}
}

// If its not a special or core component check to see if it is a plugin component
if (array_key_exists($component, $plugins) && !empty($plugins[$component])) {
$function = 'plugin_pagetypelist';
if (function_exists($component.'_pagetypelist')) {
$function = $component.'_pagetypelist';
break;
//check the plugin directory and look for a callback
if (array_key_exists($possiblecomponent, $plugins) && !empty($plugins[$possiblecomponent])) {

//We've found a plugin type. Look for a plugin name by getting the next section of page type
if (count($bits) > $i) {
$pluginname = $bits[$i];
$directory = get_plugin_directory($possiblecomponent, $pluginname);
if (!empty($directory)){
$libfile = $directory.'/lib.php';
if (file_exists($libfile)) {
require_once($libfile);
$function = $pluginname.'_page_type_list';
if (function_exists($function)) {
if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) {
break;
}
}
}
}
}

//we'll only get to here if we still don't have any patterns
//the plugin type may have a callback
$directory = get_plugin_directory($possiblecomponent, null);
if (!empty($directory)){
$libfile = $directory.'/lib.php';
if (file_exists($libfile)) {
require_once($libfile);
$function = $possiblecomponent.'_page_type_list';
if (function_exists($function)) {
if ($patterns = $function($pagetype, $parentcontext, $currentcontext)) {
break;
}
}
}
}
}
}

// Call the most appropriate function we could determine
$patterns = $function($pagetype, $parentcontext, $currentcontext);
if (empty($patterns)) {
// If there are no patterns default to just the current pattern.
$patterns = array($pagetype => $pagetype);
$patterns = default_page_type_list($pagetype, $parentcontext, $currentcontext);
}

return $patterns;
}

Expand All @@ -1625,7 +1640,7 @@ function generate_page_type_patterns($pagetype, $parentcontext = null, $currentc
* @param stdClass $currentcontext
* @return array
*/
function default_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
function default_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) {
// Generate page type patterns based on current page type if
// callbacks haven't been defined
$patterns = array($pagetype => $pagetype);
Expand All @@ -1645,40 +1660,6 @@ function default_pagetypelist($pagetype, $parentcontext = null, $currentcontext
return $patterns;
}

/**
* Generates a page type list for plugins
*
* @param string $pagetype
* @param stdClass $parentcontext
* @param stdClass $currentcontext
* @return array
*/
function plugin_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
global $CFG;

// for modules
$bits = explode('-', $pagetype);
$plugintype = $bits[0];
$pluginname = $bits[1];
$directory = get_plugin_directory($plugintype, $pluginname);
if (empty($directory)) {
return array();
}
$libfile = $directory.'/lib.php';
require_once($libfile);
$function = $pluginname.'_pagetypelist';
if (!function_exists($function)) {
return array();
}
$patterns = $function($pagetype, $parentcontext, $currentcontext);
if ($parentcontext->contextlevel == CONTEXT_COURSE) {
// including course page type
require_once("$CFG->dirroot/course/lib.php");
$patterns = array_merge(course_pagetypelist($pagetype, $parentcontext, $currentcontext), $patterns);
}
return $patterns;
}

/**
* Generates the page type list for the my moodle page
*
Expand All @@ -1687,7 +1668,7 @@ function plugin_pagetypelist($pagetype, $parentcontext = null, $currentcontext =
* @param stdClass $currentcontext
* @return array
*/
function my_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
function my_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) {
return array('my-index' => 'my-index');
}

Expand All @@ -1700,8 +1681,8 @@ function my_pagetypelist($pagetype, $parentcontext = null, $currentcontext = nul
* @param stdClass $currentcontext
* @return array
*/
function mod_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
$patterns = plugin_pagetypelist($pagetype, $parentcontext, $currentcontext);
function mod_page_type_list($pagetype, $parentcontext = null, $currentcontext = null) {
$patterns = plugin_page_type_list($pagetype, $parentcontext, $currentcontext);
if (empty($patterns)) {
// if modules don't have callbacks
// generate two default page type patterns for modules only
Expand Down

0 comments on commit b38e2e2

Please sign in to comment.