/**
  * Retrieves the XML manifest of a widget for a given user. Be carefull, the returned
  * manifest will not be exactly the same as the manifest deployed on the application server.
  * Actually, the return manifest will be provided with the preferences relevant with a given user,
  * and not the default values of the preferences as expected.
  *
  * @param integer $userId The user's identifier.
  * @param string $widgetInstanceId The widget's instance identifier.
  * @return string The requested manifest as a character stream.
  */
 public static function retrieveWidgetManifest($userId, $widgetInstanceId)
 {
     $instanceIsRequested = is_numeric($widgetInstanceId);
     # Get the widget associated with the instance
     if ($instanceIsRequested) {
         $widgetId = WidgetInstances::getWidgetIdByInstanceId($widgetInstanceId);
         if (!$widgetId) {
             throw new MwwException(MwwException::MODEL, 'Unable to find the any widget instance [' . $widgetInstanceId . ']');
         }
     } else {
         $widgetId = $widgetInstanceId;
     }
     // We load the document wich must be in the widgets direcory.
     try {
         $doc = new DOMDocument();
         $doc->preserveWhiteSpace = false;
         $doc->load('widgets/' . $widgetId . '/config.xml');
         // Try to determine if l10n module is activate
         $l10nModule = $doc->getElementsByTagNameNS('http://palette.ercim.org/ns/', 'active_l10n_module');
         if ($l10nModule->length == 1 && $l10nModule->item(0)->nodeValue == 'true') {
             # Now inject avalaible language into manifest
             $doc->createElementNS('http://palette.ercim.org/ns/', 'l10n_avalaible_language');
             $l = Widgets::retrieveAvalaibleLanguage($widgetId);
             if (!empty($l)) {
                 $l10nAvalaibleLanguageNode = $doc->createElementNS('http://palette.ercim.org/ns/', 'l10n_avalaible_language');
                 # Process All Avalaible language
                 foreach ($l as $l10n_code => $l10n_label) {
                     $langageNode = $doc->createElementNS('http://palette.ercim.org/ns/', 'l10n_langage');
                     $langageCodeNode = $doc->createAttribute('code');
                     $l10n_code = trim($l10n_code);
                     $l10n_label = trim($l10n_label);
                     $langageAttCodeValueNode = $doc->createTextNode($l10n_code);
                     $langageLabelValueNode = $doc->createTextNode($l10n_label);
                     $langageNode->appendChild($langageCodeNode);
                     $langageCodeNode->appendChild($langageAttCodeValueNode);
                     $langageNode->appendChild($langageLabelValueNode);
                     $l10nAvalaibleLanguageNode->appendChild($langageNode);
                 }
                 $widget = $doc->getElementsByTagName('widget')->item(0);
                 $widget->appendChild($l10nAvalaibleLanguageNode);
             }
         }
         if (VALIDATE_XML_FILE && !$doc->schemaValidate(XML_SCHEMA_FILE)) {
             throw new XMLValidationException(MwwException::MODEL, XML_SCHEMA_FILE);
         } else {
             if ($instanceIsRequested) {
                 // Manifest loaded and validated.
                 // Now we add it dynamic values relevant to the user preferences.
                 $db = DbUtil::accessFactory();
                 $userId = $db->escape($userId);
                 $widgetInstanceId = $db->escape($widgetInstanceId);
                 $prefs = $doc->getElementsByTagNameNS('http://palette.ercim.org/ns/', 'preference');
                 foreach ($prefs as $pref) {
                     $name = $pref->attributes->getNamedItem('name')->value;
                     $rs = $db->select("SELECT `value` FROM `interface_preferences` WHERE `widgetInstanceId` = '{$widgetInstanceId}' AND `userid` = {$userId} AND `preference` = '{$name}'");
                     /* Read the the result set into an associative array */
                     if ($rs->count() == 1) {
                         $pref->setAttribute('value', $rs->value);
                     } else {
                         if ($pref->attributes->getNamedItem('default_value')) {
                             $pref->setAttribute('value', $pref->attributes->getNamedItem('default_value')->value);
                         } else {
                             $pref->setAttribute('value', '');
                         }
                     }
                 }
             }
             return $doc->saveXML();
         }
     } catch (DOMException $e) {
         throw new FileException(MwwException::MODEL, 'Unable to load the manifest for widget ' . $widgetId);
     }
 }