Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-26105 block setting, improvement, replace switch-case with get_co…

…re_subsystems + get_plugin_types
  • Loading branch information...
commit 32a510de4ef3f63b15bc2def1054ae431a7fcd5b 1 parent b1627a9
Sam Hemelryk samhemelryk authored andyjdavis committed

Showing 1 changed file with 101 additions and 66 deletions. Show diff stats Hide diff stats

  1. +101 66 lib/blocklib.php
167 lib/blocklib.php
@@ -1558,78 +1558,53 @@ function matching_page_type_patterns($pagetype) {
1558 1558 */
1559 1559 function generate_page_type_patterns($pagetype, $parentcontext = null, $currentcontext = null) {
1560 1560 global $CFG;
1561   - // always includes current page type: $PAGE->pagetype
1562   - // if plugin callbacks defined, it will be overwriten by a property description
1563   - $patterns = array($pagetype=>$pagetype);
  1561 +
1564 1562 $bits = explode('-', $pagetype);
1565   - switch ($bits[0]) {
1566   - case 'mod':
1567   - // for modules
1568   - $modname = $bits[1];
1569   - require_once("$CFG->dirroot/mod/$modname/lib.php");
1570   - $listfunction = $modname . '_pagetypelist';
1571   - if (function_exists($listfunction)) {
1572   - $module_pagetype = $listfunction($pagetype, $parentcontext, $currentcontext);
1573   - if ($parentcontext->contextlevel == CONTEXT_COURSE) {
1574   - // including course page type
1575   - require_once("$CFG->dirroot/course/lib.php");
1576   - return array_merge(course_pagetypelist($pagetype, $parentcontext, $currentcontext), $module_pagetype);
1577   - } else {
1578   - return $module_pagetype;
1579   - }
1580   - } else {
1581   - // if modules don't have callbacks
1582   - // generate two default page type patterns for modules only
1583   - if ($bits[2] == 'view') {
1584   - $patterns['mod-*-view'] = get_string('page-mod-x-view', 'pagetype');
1585   - } else if ($bits[2] == 'index') {
1586   - $patterns['mod-*-index'] = get_string('page-mod-x-index', 'pagetype');
1587   - }
1588   - }
1589   - break;
1590   -
1591   - case 'my':
1592   - case 'user':
1593   - require_once("$CFG->dirroot/user/lib.php");
1594   - return user_pagetypelist($pagetype, $parentcontext, $currentcontext);
1595   - break;
1596   -
1597   - case 'course':
1598   - require_once("$CFG->dirroot/course/lib.php");
1599   - return course_pagetypelist($pagetype, $parentcontext, $currentcontext);
1600   - break;
1601   -
1602   - case 'tag':
1603   - require_once("$CFG->dirroot/tag/lib.php");
1604   - return tag_pagetypelist($pagetype, $parentcontext, $currentcontext);
1605   - break;
1606   -
1607   - case 'blog':
1608   - require_once("$CFG->dirroot/blog/lib.php");
1609   - return blog_pagetypelist($pagetype, $parentcontext, $currentcontext);
1610   - break;
1611   -
1612   - case 'note':
1613   - require_once("$CFG->dirroot/notes/lib.php");
1614   - return note_pagetypelist($pagetype, $parentcontext, $currentcontext);
1615   - break;
1616   -
1617   - case 'question':
1618   - require_once("$CFG->libdir/questionlib.php");
1619   - return question_pagetypelist($pagetype, $parentcontext, $currentcontext);
1620   - break;
1621   -
1622   - case 'local':
1623   - require_once("$CFG->dirroot/local/{$bits[1]}/lib.php");
1624   - $functionname = 'local_' . $bits[1] . '_pagetypelist';
1625   - if (function_exists($functionname)) {
1626   - return $functionname($pagetype, $parentcontext, $currentcontext);
  1563 +
  1564 + $component = clean_param(reset($bits), PARAM_ALPHANUMEXT);
  1565 + $function = 'default_pagetypelist';
  1566 +
  1567 + $core = get_core_subsystems();
  1568 + $plugins = get_plugin_types();
  1569 +
  1570 + // First check to see if the initial component is a core component
  1571 + // if its not check to see if it is a plugin component.
  1572 + if (array_key_exists($component, $core) && !empty($core[$component])) {
  1573 + $libfile = $CFG->dirroot.'/'.$core[$component].'/lib.php';
  1574 + if (file_exists($libfile)) {
  1575 + require_once($libfile);
  1576 + if (function_exists($component.'_pagetypelist')) {
  1577 + $function = $component.'_pagetypelist';
1627 1578 }
1628   - break;
  1579 + }
  1580 + } else if (array_key_exists($component, $plugins) && !empty($plugins[$component])) {
  1581 + $function = 'plugin_pagetypelist';
  1582 + if (function_exists($component.'_pagetypelist')) {
  1583 + $function = $component.'_pagetypelist';
  1584 + }
1629 1585 }
  1586 + // Call the most appropriate function we could determine
  1587 + $patterns = $function($pagetype, $parentcontext, $currentcontext);
  1588 + if (empty($patterns)) {
  1589 + // If there are no patterns default to just the current pattern.
  1590 + $patterns = array($pagetype => $pagetype);
  1591 + }
  1592 + return $patterns;
  1593 +}
1630 1594
  1595 +/**
  1596 + * Generates a default page type list when a more appropriate callback cannot be decided upon.
  1597 + *
  1598 + * @param string $pagetype
  1599 + * @param stdClass $parentcontext
  1600 + * @param stdClass $currentcontext
  1601 + * @return array
  1602 + */
  1603 +function default_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
1631 1604 // Generate page type patterns based on current page type if
1632 1605 // callbacks haven't been defined
  1606 + $patterns = array($pagetype => $pagetype);
  1607 + $bits = explode('-', $pagetype);
1633 1608 while (count($bits) > 0) {
1634 1609 $pattern = implode('-', $bits) . '-*';
1635 1610 $pagetypestringname = 'page-'.str_replace('*', 'x', $pattern);
@@ -1645,6 +1620,66 @@ function generate_page_type_patterns($pagetype, $parentcontext = null, $currentc
1645 1620 return $patterns;
1646 1621 }
1647 1622
  1623 +/**
  1624 + * Generates a page type list for plugins
  1625 + *
  1626 + * @param string $pagetype
  1627 + * @param stdClass $parentcontext
  1628 + * @param stdClass $currentcontext
  1629 + * @return array
  1630 + */
  1631 +function plugin_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
  1632 + // for modules
  1633 + $bits = explode('-', $pagetype);
  1634 + $plugintype = $bits[0];
  1635 + $pluginname = $bits[1];
  1636 + $directory = get_plugin_directory($plugintype, $pluginname);
  1637 + if (empty($directory)) {
  1638 + return array();
  1639 + }
  1640 + $libfile = $directory.'/lib.php';
  1641 + require_once($libfile);
  1642 + $function = $pluginname.'_pagetypelist';
  1643 + if (!function_exists($function)) {
  1644 + return array();
  1645 + }
  1646 + $patterns = $function($pagetype, $parentcontext, $currentcontext);
  1647 + if ($parentcontext->contextlevel == CONTEXT_COURSE) {
  1648 + // including course page type
  1649 + require_once("$CFG->dirroot/course/lib.php");
  1650 + $patterns = array_merge(course_pagetypelist($pagetype, $parentcontext, $currentcontext), $module_pagetype);
  1651 + }
  1652 + return $patterns;
  1653 +}
  1654 +
  1655 +/**
  1656 + * Generates the page type list for a module by either locating and using the modules callback
  1657 + * or by generating a default list.
  1658 + *
  1659 + * @param string $pagetype
  1660 + * @param stdClass $parentcontext
  1661 + * @param stdClass $currentcontext
  1662 + * @return array
  1663 + */
  1664 +function mod_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
  1665 + $patterns = plugin_pagetypelist($pagetype, $parentcontext, $currentcontext);
  1666 + if (empty($patterns)) {
  1667 + // if modules don't have callbacks
  1668 + // generate two default page type patterns for modules only
  1669 + $bits = explode('-', $pagetype);
  1670 + $patterns = array($pagetype => $pagetype);
  1671 + if ($bits[2] == 'view') {
  1672 + $patterns['mod-*-view'] = get_string('page-mod-x-view', 'pagetype');
  1673 + } else if ($bits[2] == 'index') {
  1674 + $patterns['mod-*-index'] = get_string('page-mod-x-index', 'pagetype');
  1675 + }
  1676 + }
  1677 + return $patterns;
  1678 +}
  1679 +
  1680 +function my_pagetypelist($pagetype, $parentcontext = null, $currentcontext = null) {
  1681 +
  1682 +}
1648 1683 /// Functions update the blocks if required by the request parameters ==========
1649 1684
1650 1685 /**

0 comments on commit 32a510d

Please sign in to comment.
Something went wrong with that request. Please try again.