# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/lib/ajax/ajaxlib.php
--- moodle/lib/ajax/ajaxlib.php Base (1.78)
+++ moodle/lib/ajax/ajaxlib.php Locally Modified (Based On 1.78)
@@ -185,6 +185,8 @@
      *      'mod/mymod/styles.css';
      * @param boolean $fullurl This parameter is intended for internal use only.
      *      (If true, $stylesheet is treaded as a full URL, not relative $CFG->wwwroot.)
+     * @return required_css The required_css object. This allows you to control the priority
+     *      of the script and when it is printed.
      */
     public function css($stylesheet, $fullurl = false) {
         global $CFG;
@@ -203,6 +205,7 @@
         if (!isset($this->linkedrequirements[$url])) {
             $this->linkedrequirements[$url] = new required_css($this, $url);
         }
+        return $this->linkedrequirements[$url];
     }
 
     /**
@@ -379,13 +382,33 @@
 
     /**
      * Get the code for the linked resources that need to appear in a particular place.
+     *
+     * We first seperate out all of the linkedrequirements for the given area, then
+     * split those requirement based on thier priority. The reason to split is
+     * so that we can maintain the order things were added in if no special priority
+     * has been given.
+     *
      * @param $when one of the WHEN_... constants.
      * @return string the HTML that should be output in that place.
      */
     protected function get_linked_resources_code($when) {
-        $output = '';
+        $requirements = array('high'=>array(),'normal'=>array(),'low'=>array());
         foreach ($this->linkedrequirements as $requirement) {
             if (!$requirement->is_done() && $requirement->get_when() == $when) {
+                $level = 'normal';
+                if ($requirement->priority() > 0) {
+                    $level = 'high';
+                } else if ($requirement->priority() < 0) {
+                    $level = 'low';
+                }
+                $requirements[$level][] = $requirement;
+            }
+        }
+        $output = '';
+        usort($requirements['high'], 'linked_requirement::prioritise');
+        usort($requirements['low'], 'linked_requirement::prioritise');
+        foreach ($requirements as $requirementlevel) {
+            foreach ($requirementlevel as $requirement) {
                 $output .= $requirement->get_html();
                 $requirement->mark_done();
             }
@@ -549,6 +572,7 @@
  */
 abstract class linked_requirement extends requirement_base {
     protected $url;
+    protected $priority = 0;
 
     /**
      * Constructor. Normally the class and its subclasses should not be created
@@ -567,9 +591,43 @@
      * @return string the HTML needed to satisfy this requirement.
      */
     abstract public function get_html();
+
+    /**
+     * Sets the priority of this linked resource
+     *
+     * Priority is used to influence the order in which linked resources within
+     * the same area are included.
+     * Was introduced as part of MDL-19935 to allow us to prioritise theme style
+     * sheets
+     *
+     * @param int $newpriority
+     */
+    final public function priority($newpriority = null) {
+        if (is_int($newpriority)) {
+            $this->priority = $newpriority;
 }
+        return $this->priority;
+    }
 
+    /**
+     * Can be used to sort an array of linked_requirement inheriters by priority
+     * This function is designed to be used with usort in the following manner
+     *
+     * <code php>
+     * usort($linkedrequirements, 'linked_requirement::prioritise');
+     * </code>
+     *
+     */
+    final static function prioritise($requirementa, $requirementb) {
+        if ($requirementa->priority() > $requirementb->priority()) {
+            return -1;
+        } else {
+            return 1;
+        }
+    }
+}
 
+
 /**
  * A subclass of {@link linked_requirement} to represent a requried JavaScript file.
  *
@@ -694,7 +752,7 @@
  * are indented to be used as a fluid API, so you can say things like
  *     $PAGE->requires->yui_lib('autocomplete')->in_head();
  *
- * This class (with the help of {@link ajax_resolve_yui_lib()}) knows about the
+ * This class (with the help of {@link yui_translation::libraries}) knows about the
  * dependancies between the different YUI libraries, and will include all the
  * other libraries required by the one you ask for. It also knows which YUI
  * libraries require css files. If the library you ask for requires CSS files,
@@ -720,22 +778,25 @@
      *
      * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
      * @param string $libname The name of the YUI library you want. See the array
-     * defined in {@link ajax_resolve_yui_lib()} for a list of known libraries.
+     * {@link yui_translation::libraries} for a list of known libraries.
      */
     public function __construct(page_requirements_manager $manager, $libname) {
         parent::__construct($manager, '');
         $this->when = page_requirements_manager::WHEN_AT_END;
 
-        list($jsurls, $cssurls) = ajax_resolve_yui_lib($libname);
+        $jsurls = yui_translation::resolve_js_requirments($libname);
         foreach ($jsurls as $jsurl) {
             $this->jss[] = $manager->js($jsurl, true);
         }
+        if (!empty($CFG->useexternalyui)) {
+            $cssurls = yui_translation::resolve_css_requirments($libname);
         foreach ($cssurls as $cssurl) {
             // this might be a bit problematic because it requires yui to be
             // requested before print_header() - this was not required in 1.9.x
             $manager->css($cssurl, true);
         }
     }
+    }
 
     public function get_html() {
         // Since we create a required_js for each of our files, that will generate the HTML.
@@ -1148,29 +1209,26 @@
     }
 }
 
-
 /**
- * Given the name of a YUI library, return a list of the .js and .css files that
- * it requries.
+ * This class provides a set of static methods that can be used to resolve YUI
+ * lib JS and CSS dependancies.
  *
- * This method takes note of the $CFG->useexternalyui setting.
- *
- * If $CFG->debug is set to DEBUG_DEVELOPER then this method will return links to
- * the -debug version of the YUI files, otherwise it will return links to the -min versions.
- *
- * @param string $libname the name of a YUI library, for example 'autocomplete'.
- * @return array with two elementes. The first is an array of the JavaScript URLs
- *      that must be loaded to make this library work, in the order they should be
- *      loaded. The second element is a (possibly empty) list of CSS files that
- *      need to be loaded.
+ * @copyright 2009 Sam Hemelryk
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.0
  */
-function ajax_resolve_yui_lib($libname) {
-    global $CFG;
-
-    // Note, we always use yahoo-dom-event, even if we are only asked for part of it.
-    // because another part of the code may later ask for other bits. It is easier, and
-    // not very inefficient, just to always use (and get browsers to cache) the combined file.
-    static $translatelist = array(
+class yui_translation {
+    /**
+     * This CSS file contains all of the sam skin CSS for all components
+     * --- but not the reset css
+     * @var string
+     */
+    public static $completeskin = 'assets/skins/sam/skin.css';
+    /**
+     * An array of YUI library dependencies
+     * @var array
+     */
+    public static $libraries = array(
         'yahoo' => 'yahoo-dom-event',
         'animation' => array('yahoo-dom-event', 'animation'),
         'autocomplete' => array(
@@ -1249,22 +1307,59 @@
                 'js' => array('yahoo-dom-event', 'logger', 'yuitest'),
                 'css' => array('logger/assets/logger.css', 'yuitest/assets/testlogger.css')),
     );
-    if (!isset($translatelist[$libname])) {
+    /**
+     * This static array determines the order in which CSS files are loaded if
+     * they are requested individually
+     * @var array
+     */
+    public static $cssorder = Array(
+        'container',
+        'menu',
+        'autocomplete',
+        'button',
+        'calendar',
+        'carousel',
+        'colorpicker',
+        'datatable',
+        'editor',
+        'imagecropper',
+        'layout',
+        'paginator',
+        'resize',
+        'tabview',
+        'treeview',
+        'logger',
+        'simpleeditor',
+        'yuitest');
+    /**
+     * Given the name of a YUI library, return a list of .js files that
+     * it requries.
+     *
+     * This method takes note of the $CFG->useexternalyui setting.
+     *
+     * If $CFG->debug is set to DEBUG_DEVELOPER then this method will return links to
+     * the -debug version of the YUI files, otherwise it will return links to the -min versions.
+     *
+     * @param string $libname the name of a YUI library, for example 'autocomplete'.
+     * @return array (possibly empty) list of JS files that
+     *      need to be loaded.
+     */
+    public static function resolve_js_requirments($libname) {
+        global $CFG;
+        // Note, we always use yahoo-dom-event, even if we are only asked for part of it.
+        // because another part of the code may later ask for other bits. It is easier, and
+        // not very inefficient, just to always use (and get browsers to cache) the combined file.
+        if (!isset(self::$libraries[$libname])) {
         throw new coding_exception('Unknown YUI library ' . $libname);
     }
-
-    $data = $translatelist[$libname];
+        $data = self::$libraries[$libname];
     if (!is_array($data)) {
         $jsnames = array($data);
-        $cssfiles = array();
     } else if (isset($data['js']) && isset($data['css'])) {
         $jsnames = $data['js'];
-        $cssfiles = $data['css'];
     } else {
         $jsnames = $data;
-        $cssfiles = array();
     }
-
     $debugging = debugging('', DEBUG_DEVELOPER);
     if ($debugging) {
         $suffix = '-debug.js';
@@ -1272,13 +1367,20 @@
         $suffix = '-min.js';
     }
     $libpath = $CFG->httpswwwroot . '/lib/yui/';
-
     $externalyui = !empty($CFG->useexternalyui);
     if ($externalyui) {
-        include($CFG->libdir.'/yui/version.php'); // Sets $yuiversion.
+            // Fetch the YUI version number out of the thirdparty xml
+            // so that we can use it to get the correct YUI js
+            $xml = simplexml_load_file($CFG->libdir . '/thirdpartylibs.xml');
+            $yuiversion = '';
+            foreach ($xml->library as $libobject) {
+                if ($libobject->location=='yui') {
+                    $yuiversion = $libobject->version;
+                    break;
+                }
+            }
         $libpath = 'http://yui.yahooapis.com/' . $yuiversion . '/build/';
     }
-
     $jsurls = array();
     foreach ($jsnames as $js) {
         if ($js == 'yahoo-dom-event') {
@@ -1293,14 +1395,77 @@
             $jsurls[] = $libpath . $js . '/' . $js . $suffix;
         }
     }
-
-    $cssurls = array();
-    foreach ($cssfiles as $css) {
-        $cssurls[] = $libpath . $css;
+        return $jsurls;
     }
 
-    return array($jsurls, $cssurls);
+    /**
+     * Given the name of a YUI library, return a list of .css files that
+     * it requries.
+     *
+     * This method takes note of the $CFG->useexternalyui setting.
+     *
+     * If $CFG->debug is set to DEBUG_DEVELOPER then this method will return links to
+     * the -debug version of the YUI files, otherwise it will return links to the -min versions.
+     *
+     * @param string $libname the name of a YUI library, for example 'autocomplete'.
+     * @return array (possibly empty) list of CSS files that
+     *      need to be loaded.
+     */
+    public static function resolve_css_requirements($specificlibs=null) {
+        global $CFG;
+        if (is_string($specificlibs)) {
+            $specificlibs = Array($specificlibs);
 }
+        if (!is_array($specificlibs)) {
+            $specificlibs = null;
+        } else if (count($specificlibs)===0) {
+            return array();
+        }
+        $resetfiles = Array();
+        $cssfiles = Array();
+        $libpath = $CFG->httpswwwroot . '/lib/yui/';
+        if (!empty($CFG->useexternalyui)) {
+            // Fetch the YUI version number out of the thirdparty xml
+            // so that we can use it to get the correct YUI js
+            $xml = simplexml_load_file($CFG->libdir . '/thirdpartylibs.xml');
+            $yuiversion = '';
+            foreach ($xml->library as $libobject) {
+                if ($libobject->location=='yui') {
+                    $yuiversion = $libobject->version;
+                    break;
+                }
+            }
+            $libpath = 'http://yui.yahooapis.com/' . $yuiversion . '/build/';
+        }
+        foreach (self::$cssorder as $library) {
+            if ($specificlibs !== null && !in_array($library, $specificlibs)) {
+                continue;
+            }
+            if (is_array(self::$libraries[$library]) && array_key_exists('css', self::$libraries[$library])) {
+                if (is_string(self::$libraries[$library]['css'])) {
+                    $cssfile = self::$libraries[$library]['css'];
+                    if (strpos($cssfile, 'reset')!==false) {
+                            $resetfiles[] = $libpath.$cssfile;
+                        } else {
+                            $cssfiles[] = $libpath.$cssfile;
+                        }
+                } else if (is_array(self::$libraries[$library]['css'])) {
+                    foreach (self::$libraries[$library]['css'] as $cssfile) {
+                        if (strpos($cssfile, 'reset')!==false) {
+                            $resetfiles[] = $libpath.$cssfile;
+                        } else {
+                            $cssfiles[] = $libpath.$cssfile;
+                        }
+                    }
+                }
+            }
+        }
+        if (in_array($libpath.self::$completeskin, $cssfiles)) {
+            $cssfiles = array($libpath.self::$completeskin);
+        }
+        return array_merge($resetfiles, $cssfiles);
+    }
+}
 
 /**
  * Return the HTML required to link to a JavaScript file.
@@ -1443,5 +1608,3 @@
     }
 
 }
-
-?>
Index: moodle/lib/pagelib.php
--- moodle/lib/pagelib.php Base (1.146)
+++ moodle/lib/pagelib.php Locally Modified (Based On 1.146)
@@ -938,8 +938,8 @@
 
         // Require theme stylesheets.
         $stylesheets = $this->theme->get_stylesheet_urls();
-        foreach ($stylesheets as $stylesheet) {
-            $this->requires->css($stylesheet, true);
+        foreach ($stylesheets as $key=>$stylesheet) {
+            $this->requires->css($stylesheet, true)->priority(100-$key);
         }
 
         $this->initialise_standard_body_classes();
Index: moodle/theme/standard/config.php
--- moodle/theme/standard/config.php Base (1.32)
+++ moodle/theme/standard/config.php Locally Modified (Based On 1.32)
@@ -38,6 +38,12 @@
 $THEME->standardsheets = true;
 $THEME->pluginsheets = array('mod', 'block', 'format', 'gradereport');
 
+// yuisheets controls which YUI CSS sheets get included with this page. By default
+// (if not set for example) all CSS for all YUI lib's will be loaded to ensure
+// everything is available and overridable. Setting this to true is the same as not
+// setting it, it can also be an array of lib's to load CSS for if the list is known
+$THEME->yuisheets = true;
+
 $THEME->metainclude = false;
 $THEME->parentmetainclude = false;
 $THEME->standardmetainclude = true;
Index: moodle/theme/styles.php
--- moodle/theme/styles.php Base (1.4)
+++ moodle/theme/styles.php Locally Modified (Based On 1.4)
@@ -112,6 +112,27 @@
 // We will build up a list of CSS file path names, then concatenate them all.
 $files = array();
 
+// If we are using external yui stylesheets they will have *hopefully* been included
+// in the header, if we're not using external yui then lets include them first
+// so that we can override
+if (empty($CFG->useexternalyui) && $themename == 'standard') {
+    // Prepare to load the YUI css stylesheets, by default we will load ALL possible
+    // CSS for every YUI lib we know about, see MDL-19935 for reasons why
+    // We will look at $THEME->yuisheets which can be true|false|array or libs
+    $yuifiles = array();
+    if (empty($THEME->yuisheets) || $THEME->yuisheets===true || is_array($THEME->yuisheets)) {
+        $requiredyui = null;
+        if (!empty($THEME->yuisheets)) {
+            $requiredyui = $THEME->yuisheets;
+        }
+        $yuifiles = array_merge($yuifiles, yui_translation::resolve_css_requirements($requiredyui));
+    }
+    // Add each yuifile to the files array after stripping it back to its physical path
+    foreach ($yuifiles as $file) {
+        $files[] = $CFG->dirroot . '/' . str_replace(array($CFG->httpswwwroot . '/', $CFG->wwwroot . '/'), '', $file);
+    }
+}
+
 // If this theme wants plugin sheets, include them. Do this first, so styles
 // here can be overridden by theme CSS.
 if ($pluginsheets) {
@@ -172,12 +193,18 @@
 
 if (!empty($THEME->cssoutputfunction)) {
     call_user_func($THEME->cssoutputfunction, $files, $toreplace);
-
 } else {
     foreach ($files as $file) {
         $shortname = str_replace($toreplace, '', $file);
         echo '/******* ' . $shortname . " start *******/\n\n";
+        // Check files, if they are from the YUI library then we need to convert
+        // the path of any linked resources from relative to absolute
+        if (strpos($shortname, 'lib/yui/')!==false) {
+            $css = file_get_contents($file);
+            echo @preg_replace('#url\(([^/][^\)]+)\)#m', 'url('.$CFG->wwwroot.'/'.dirname($shortname).'/$1)', $css);
+        } else {
         @include_once($file);
+        }
         echo '/******* ' . $shortname . " end *******/\n\n";
     }
 }
