Get content of qti.xml following an item + language

@param core_kernel_classes_Resource $item
@param string $language
@return false|string
@throws \common_Exception
505794
public static function getQtiFileContent(core_kernel_classes_Resource $item, $language = '')
    {
        $itemDirectory = taoItems_models_classes_ItemsService::singleton()->getItemDirectory($item, $language);
        return $itemDirectory->getFile(self::FILE)->read();
    }
236195
public function Match( $item, $context )
	{
		if ( $this->TypeCode == $item->getSchemaType()->TypeCode ) return true;

		switch ( $this->TypeCode )
		{
			case XmlTypeCode::None:
				return false;

			case XmlTypeCode::Item:
				return true;

			case XmlTypeCode::Node:
				return $item->getIsNode();

			case XmlTypeCode::AnyAtomicType:
				return ! $item->getIsNode();

			case XmlTypeCode::UntypedAtomic:
				return ! $item->getIsNode() && $item->GetSchemaType() == XmlSchema::$UntypedAtomic;

			case XmlTypeCode::Document:

				if ( $item instanceof XPathNavigator )
				{
					/**
					 * @var XPathNavigator $nav
					 */
					$nav = $item;
					if ( $nav->getNodeType() == XPathNodeType::Root)
					{
						/**
						 * @var XPathNavigator $cur
						 */
						$cur = $nav->CloneInstance();
						if ( is_null( $this->SchemaElement ) )
						{
							if ( $cur->MoveToChild( XPathNodeType::Element ) && $this->MatchName( $cur, $context ) )
							{
								if ( is_null( $this->SchemaType ) || $this->SchemaType == XmlSchema::$UntypedAtomic )
								{
									return true;
								}
								/**
								 * @var IXmlSchemaInfo $schemaInfo
								 */
								$schemaInfo = $cur->getSchemaInfo();
								if ( ! is_null( $this->schemaInfo ) )
								{
									if ( DOMSchemaType::IsDerivedFrom( $schemaInfo->getSchemaType(), $this->SchemaType, XmlSchemaDerivationMethod::Empty_ ) )
										return ! $schemaInfo->getIsNil() || $this->Nillable;
								}
								else
								{
									return DOMSchemaType::IsDerivedFrom( XmlSchema::$UntypedAtomic, $this->SchemaType, XmlSchemaDerivationMethod::Empty_ );
								}
							}
						}
						else
						{
							if ( ! $cur->MoveToChild( XPathNodeType::Element ) )
							{
								return false;
							}

							/**
							 * @var IXmlSchemaInfo $schemaInfo
							 */
							$schemaInfo = $cur->getSchemaInfo();
							if ( ! is_null( $schemaInfo ) )
							{
								return $schemaInfo->getSchemaElement()->QualifiedName->equals( $this->SchemaElement->QualifiedName );
							}
						}
					}
				}
				break;

			case XmlTypeCode::Element:

				if ( $item instanceof XPathNavigator )
				{
					/**
				   	 * @var XPathNavigator $nav
				   	 */
					$nav = $item;
					if ( $nav->getNodeType() == XPathNodeType::Element )
					{
						if ( is_null( $this->SchemaElement ) )
						{
							if ( $this->MatchName( $nav, $context ) )
							{
								if ( is_null( $this->SchemaType ) || $this->SchemaType == XmlSchema::$UntypedAtomic )
								{
									return true;
								}

								/**
								 * @var IXmlSchemaInfo $schemaInfo
								 */
								$schemaInfo = $nav->getSchemaInfo();
								if ( is_null( $schemaInfo ) )
								{
									return DOMSchemaType::IsDerivedFrom( XmlSchema::$UntypedAtomic, $this->SchemaType, XmlSchemaDerivationMethod::Empty_ );
								}
								else
								{
									if ( DOMSchemaType::IsDerivedFrom( $schemaInfo->getSchemaType(), $this->SchemaType, XmlSchemaDerivationMethod::Empty_ ) )
									{
										return ! $schemaInfo->getIsNil() || $this->Nillable;
									}
								}
							}
						}
						else
						{
							/**
							 * @var IXmlSchemaInfo $schemaInfo
							 */
							$schemaInfo = $nav->getSchemaInfo();
							if ( ! is_null( $schemaInfo ) )
								return $schemaInfo->getSchemaElement()->QualifiedName->equals( $this->SchemaElement->QualifiedName );
						}
					}
				}
				break;

			case XmlTypeCode::Attribute:

				if ( $item instanceof XPathNavigator )
				{
					/**
					 * @var XPathNavigator $nav
					 */
					$nav = $item;
					if ( $nav->getNodeType() == XPathNodeType::Attribute )
					{
						if ( is_null( $this->SchemaAttribute ) )
						{
							if ( $this->MatchName( $nav, $context ) )
							{
								if ( is_null( $this->SchemaType ) || $this->SchemaType == XmlSchema::$UntypedAtomic )
								{
									return true;
								}
								/**
								 * @var IXmlSchemaInfo $schemaInfo
								 */
								$schemaInfo = $nav->getSchemaInfo();
								if ( is_null( $schemaInfo ) )
									return DOMSchemaType::IsDerivedFrom( XmlSchema::$UntypedAtomic, $this->SchemaType, XmlSchemaDerivationMethod::Empty_ );
								else
									return DOMSchemaType::IsDerivedFrom( $schemaInfo->getSchemaType(), $this->SchemaType, XmlSchemaDerivationMethod::Empty_ );
							}
						}
						else
						{
							/**
							 * @var IXmlSchemaInfo $schemaInfo
							 */
							$schemaInfo = $nav->getSchemaInfo();
							if ( ! is_null( $schemaInfo ) )
								return $schemaInfo->getSchemaAttribute()->QualifiedName->equals( $this->SchemaAttribute->QualifiedName );
						}
					}
				}
				break;

			case XmlTypeCode::ProcessingInstruction:

				if ( $item instanceof XPathNavigator )
				{
					/**
					 * @var XPathNavigator $nav
					 */
					$nav = $item;
					return ( $nav->getNodeType() == XPathNodeType::ProcessingInstruction && ( $this->NameTest->IsNameWildcard() || $this->NameTest->localName == $nav->getLocalName() ) );
				}
				break;

			case XmlTypeCode::Comment:

				if ( $item instanceof XPathNavigator )
				{
					/**
					 * @var XPathNavigator $nav
					 */
					$nav = $item;
					return $nav->getNodeType() == XPathNodeType::Comment;
				}
				break;

			case XmlTypeCode::Text:

				if ( $item instanceof XPathNavigator )
				{
					/**
					 * @var XPathNavigator $nav
					 */
					$nav = $item;
					return $nav->getNodeType() == XPathNodeType::Text || $nav->getNodeType() == XPathNodeType::SignificantWhitespace;
				}
				break;

			case XmlTypeCode::PositiveInteger:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Byte:
					case XmlTypeCode::Short:
					case XmlTypeCode::Int:
					case XmlTypeCode::Integer:
						return $item->getValueAsInt() >= 0;

					case XmlTypeCode::Long:
						return false;
				}
				break;

			case XmlTypeCode::NegativeInteger:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Byte:
					case XmlTypeCode::Short:
					case XmlTypeCode::Int:
					case XmlTypeCode::Integer:
						return $item->getValueAsInt() <= 0;

					case XmlTypeCode::Long:
						return false;
				}
				break;

			case XmlTypeCode::NonPositiveInteger:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Byte:
					case XmlTypeCode::Short:
					case XmlTypeCode::Int:
					case XmlTypeCode::Long:
					case XmlTypeCode::Integer:
						return $item->getValueAsInt() <= 0;
				}
				break;

			case XmlTypeCode::NonNegativeInteger:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Byte:
					case XmlTypeCode::Short:
					case XmlTypeCode::Int:
					case XmlTypeCode::Integer:
						return $item->getValueAsInt() >= 0;

					case XmlTypeCode::Long:
						return $item->getValueAsLong() == 0;

					case XmlTypeCode::UnsignedByte:
					case XmlTypeCode::UnsignedShort:
					case XmlTypeCode::UnsignedInt:
					case XmlTypeCode::UnsignedLong:
						return true;
				}
				break;

			case XmlTypeCode::Int:
			case XmlTypeCode::Byte:
			case XmlTypeCode::Short:
			case XmlTypeCode::Long:
			case XmlTypeCode::Integer:
			case XmlTypeCode::UnsignedLong:
			case XmlTypeCode::UnsignedInt:
			case XmlTypeCode::UnsignedByte:
			case XmlTypeCode::UnsignedShort:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Byte:
					case XmlTypeCode::Short:
					case XmlTypeCode::Int:
					case XmlTypeCode::Long:
					case XmlTypeCode::Integer:
					case XmlTypeCode::UnsignedByte:
					case XmlTypeCode::UnsignedShort:
					case XmlTypeCode::UnsignedInt:
					case XmlTypeCode::UnsignedLong:
						return true;

					case XmlTypeCode::Decimal:
						// BMS 2017-07-20 Changed result
						/**
						 * @var DecimalValue $dec
						 */
						$dec = $item->getTypedValue();
						return $dec->getIntegerPart() == $dec->getValue();
						// BMS 2017-07-10 Changed result
						return false;
						return ! $item->ValueAs( Types::$DecimalType, $context->NamespaceManager )->getIsDecimal();

					case XmlTypeCode::Double:
						// BMS 2017-07-10 Changed result
						return false;
						$value = $item->getTypedValue(); // ->ValueAs( Types::$DoubleType );
						return $value == floor( $value );
				}
				break;

			case XmlTypeCode::Entity:
				return	( $item->GetSchemaType()->TypeCode == XmlTypeCode::String ) ||
						( $item->GetSchemaType()->TypeCode == XmlTypeCode::Entity );

			// In PHP these are synonymous
			// BMS 2017-07-10 Changed result
			case XmlTypeCode::Float:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					// Note the order of these cases is significant
					// If the order is different then float will be
					// reported as an instance of double and will fail
					// test instanceof49 in the exprSeqTypes test set.
					case XmlTypeCode::Float:
						return true;

					case XmlTypeCode::Double:
						if ( is_nan( $item->getTypedValue() ) )
						{
							return true;
						}
				}
				break;

			// BMS 2017-07-10 Changed result
			case XmlTypeCode::Double:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Double:
						return true;
				}
				break;

			// BMS 2017-07-10 Changed result
			case XmlTypeCode::Decimal:
				switch ( $item->GetSchemaType()->TypeCode )
				{
					case XmlTypeCode::Byte:
					case XmlTypeCode::Short:
					case XmlTypeCode::Int:
					case XmlTypeCode::Long:
					case XmlTypeCode::Integer:
					case XmlTypeCode::UnsignedByte:
					case XmlTypeCode::UnsignedShort:
					case XmlTypeCode::UnsignedInt:
					case XmlTypeCode::UnsignedLong:
					case XmlTypeCode::Decimal:
						return true;
				}
				break;

			default:
				{
					$schemaType = $item->getSchemaType();
					if ( ! is_null( $schemaType ) )
					{
						// BMS 2017-09-03	Added this condition. The XPath documentation indicates that an
						//					untyped type can be matched with any other type.
						// https://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive
						if ( $schemaType->TypeCode == XmlTypeCode::UntypedAtomic )
						{
							return true;
						}
						return DOMSchemaType::IsDerivedFrom( $schemaType, $this->SchemaType, XmlSchemaDerivationMethod::Empty_ );
					}
				}
				break;
		}
		return false;
	}
796407
public function getUsedLanguages( core_kernel_classes_Resource $resource,  core_kernel_classes_Property $property)
    {
        $returnValue = array();

        
        
    	$sqlQuery = 'SELECT l_language FROM statements WHERE subject = ? AND predicate = ? ';
        $sqlResult = $this->getPersistence()->query($sqlQuery, array (
        	$resource->getUri(),
        	$property->getUri()
        ));
        while ($row = $sqlResult->fetch()) {
            if (!empty($row['l_language'])) {
                $returnValue[] = $row['l_language'];
            }
        }
        
        

        return (array) $returnValue;
    }
337866
protected function getRenderedItem($item) {

        //@todo make getRenderedItem language dependent
        $lang = \common_session_SessionManager::getSession()->getDataLanguage();
        $qtiItem = Service::singleton()->getDataItemByRdfItem($item, $lang, true);
        if ($qtiItem) {
            $contentVariableElements = array_merge($this->getModalFeedbacks($qtiItem), $this->getRubricBlocks($qtiItem));

            /** @var AssetService $assetService */
            $assetService = ServiceManager::getServiceManager()->get(AssetService::SERVICE_ID);

            $taoBaseUrl = $assetService->getJsBaseWww( 'tao' );
            $qtiBaseUrl = $assetService->getJsBaseWww( 'taoQtiItem' );

            $taoLibUrl = $taoBaseUrl.'js/';
            $taoQtiItemLibUrl = $qtiBaseUrl.'js/';

            $xhtml = $qtiItem->toXHTML(array(
                'contentVariableElements' => $contentVariableElements,
                //  'js' => array($qtiBaseUrl.'js/preview/qtiViewSelector.js'),
                'js_var' => array('view' => $this->getRequestView()),
                // 'css' => array($qtiBaseUrl.'css/preview/qtiViewSelector.css'),
                'path' => array(
                    'tao' => $taoLibUrl,
                    'taoQtiItem' => $taoQtiItemLibUrl
                ),
                'client_config_url' => $this->getClientConfigUrl()

            ));
        } else {
            \common_Logger::i('Try to preview an empty item', [$item->getUri()]);
            $xhtml = '';
        }

        return $xhtml;
    }
290061
public function addItem($item){
		$itemO=$this->getItemToAdd($item);
		$this->addContent($itemO);
		return $itemO;
	}
995136
public function packItem(core_kernel_classes_Resource $item, $lang, Directory $directory)
    {
        //use the QtiParser to transform the QTI XML into an assoc array representation
        $content = $this->getXmlByItem($item, $lang);
        //load content
        $qtiParser = new QtiParser($content);
        //validate it
        $qtiParser->validate();
        if (!$qtiParser->isValid()) {
            throw new common_Exception('Invalid QTI content : ' . $qtiParser->displayErrors(false));
        }

        //parse
        $qtiItem = $qtiParser->load();

        return $this->packQtiItem($item, $lang, $qtiItem, $directory);

    }
685643
protected function getCreatorConfig(core_kernel_classes_Resource $item){

        $config = new CreatorConfig();

        $ext = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiItem');
        $creatorConfig = $ext->getConfig('qtiCreator');

        if (is_array($creatorConfig)) {
            foreach ($creatorConfig as $prop => $value) {
                $config->setProperty($prop, $value);
            }
        }

        $config->setProperty('uri', $item->getUri());
        $config->setProperty('label', $item->getLabel());

        //set the current data lang in the item content to keep the integrity
        //@todo : allow preview in a language other than the one in the session
        $lang = \common_session_SessionManager::getSession()->getDataLanguage();
        $config->setProperty('lang', $lang);

        //base url:
        $url = tao_helpers_Uri::url('getFile', 'QtiCreator', 'taoQtiItem', array(
            'uri' => $item->getUri(),
            'lang' => $lang,
            'relPath' => ''
        ));
        $config->setProperty('baseUrl', $url);

        //map the multi column config to the plugin
        //TODO migrate the config
        if($config->getProperty('multi-column') == true){
            $config->addPlugin('blockAdder', 'taoQtiItem/qtiCreator/plugins/content/blockAdder', 'content');
        }

        $mediaSourcesUrl = tao_helpers_Uri::url(
            'getMediaSources',
            'QtiCreator',
            'taoQtiItem'
        );

        $config->setProperty('mediaSourcesUrl', $mediaSourcesUrl);

        //initialize all registered hooks:
        $hookClasses = HookRegistry::getRegistry()->getMap();
        foreach ($hookClasses as $hookClass) {
            $hook = new $hookClass();
            $hook->init($config);
        }

        $config->init();

        return $config;
    }
46500
public function getChildren(Content $content, Language $language)
    {
        return $content->children()
            ->with(self::$loadRelations)
            ->where('published_at', '<=', Carbon::now())
            ->whereHas('routes', function ($query) use ($language) {
                $query->where('routes.is_active', true)
                    ->where('language_code', $language->code);
            })
            ->orderBy('is_promoted', 'DESC')
            ->orderBy('is_sticky', 'DESC')
            ->orderBy('weight', 'ASC')
            ->orderBy('published_at', 'DESC')
            ->paginate(option('general', 'default_page_size', 20));
    }
966024
public function itemCreate(Item $item)
    {
        $response = $this->getGuzzleClient()
            ->post(
                '/items',
                array_merge($this->setQuery(), $this->setBody($item))
            );

        return $this->serializer->deserialize(
            $response->getBody()->getContents(),
            Item::class,
            'json'
        );
    }
1295886
public static function getByLanguage($language, Site $site = null)
    {
        if (!$site) {
            $site = \Core::make('site')->getSite();
        }

        $em = Database::get()->getEntityManager();
        /**
         * @var $section Locale
         */
        $section = $em->getRepository('Concrete\Core\Entity\Site\Locale')
            ->findOneBy(['site' => $site, 'msLanguage' => $language]);

        if (is_object($section)) {
            $obj = parent::getByID($section->getSiteTree()->getSiteHomePageID(), 'RECENT');
            $obj->setLocale($section);

            return $obj;
        }

        return false;
    }
1129519
public function get($item)
    {
        $key = $this->getItemKey($item);

        if (null !== $key) {
            if (isset($this->items[$key])) {
                $item = $this->items[$key];
            } else {
                $this->items[$key] = $item;
            }
        }

        return $item;
    }
405745
public function getTranslation($item, $langcode, $store = null)
    {
        $config = $this->getConfig(null, $store);

        if (!empty($config['translation'][$langcode][$item])) {
            return $config['translation'][$langcode][$item];
        }

        if (isset($config[$item])) {
            return $config[$item];
        }

        return '';
    }
549545
public static function getMetadata($lang)
	{
		$path = self::getLanguagePath(JPATH_ROOT, $lang);
		$file = $lang . '.xml';

		$result = null;

		if (is_file("$path/$file"))
		{
			$result = self::parseXMLLanguageFile("$path/$file");
		}

		if (empty($result))
		{
			return null;
		}

		return $result;
	}
620282
public function dolog( common_log_Item $item)
    {
        if(php_sapi_name() != 'cli'){
            switch($item->getSeverity()){
            case common_Logger::ERROR_LEVEL :
                ChromePhp::error($item->getDescription() . ' at ' . $item->getCallerFile() . ':' . $item->getCallerLine());
                break;
            case common_Logger::WARNING_LEVEL :
                ChromePhp::warn($item->getDescription());
                break;
            case common_Logger::INFO_LEVEL :
                ChromePhp::info($item->getDescription());
                break;
            default :
                ChromePhp::log($item->getDescription());
                break;
            }
        }
    }
856319
public function handleItem($item, $lang)
    {
        if (is_array($item)) {
            $item = new SitemapArrayItem($item);
            $loc = $item->getSitemapLoc();
            if (empty($loc)) {
                $this->printMessage('Warning: item "' . var_export($item->item, true) .
                    '" does not have a "loc" value');

                return false;
            }
        } elseif (!$item instanceof Basic) {
            $this->printMessage('Warning: item "' . var_export($item, true) .
                '" does not compatible with "demi\sitemap\interfaces\Basic" or Array');

            return false;
        }

        $builder = $this->builder;
        $url = $builder->newUrl();

        // Basic attributes
        $url->loc($item->getSitemapLoc($lang))
            ->lastmod($item->getSitemapLastmod($lang))
            ->changefreq($item->getSitemapChangefreq($lang))
            ->priority($item->getSitemapPriority($lang));

        // Google image attribute
        if ($item instanceof GoogleImage) {
            $images = $item->getSitemapMaterialImages($item, $lang);

            foreach ($images as $image) {
                $url->addImage(
                    $item->getSitemapImageLoc($image, $lang),
                    $item->getSitemapImageGeoLocation($image, $lang),
                    $item->getSitemapImageCaption($image, $lang),
                    $item->getSitemapImageTitle($image, $lang),
                    $item->getSitemapImageLicense($image, $lang)
                );
            }
        }

        // Google alternate links
        if ($item instanceof GoogleAlternateLang || $item instanceof SitemapArrayItem) {
            $links = $item->getSitemapAlternateLinks();

            foreach ($links as $hreflang => $href) {
                $url->addAlternateLink($hreflang, $href);
            }
        }

        return $builder->writeUrl($url);
    }
936891
protected function updateItem(\oat\taoQtiItem\model\qti\Item $item, $itemFile)
    {
        $changed = false;
        $responses = $item->getResponses();
        $interactions = $item->getInteractions();
        $usedResponses = [];
        foreach($interactions as $interaction){
            $usedResponses[] = $interaction->attr('responseIdentifier');
        }
        foreach ($responses as $response) {
            $responseIdentifier = $response->attr('identifier');
            if(!in_array($responseIdentifier, $usedResponses)){
                $changed = true;
                $item->removeResponse($response);
            }
        }

        $xml        = simplexml_load_file($itemFile);
        $rpTemplate = (string) $xml->responseProcessing['template'];
        
        //detect wrong usage for standard standard response declaration
        $rp = $item->getResponseProcessing();
        if ($rp instanceof \oat\taoQtiItem\model\qti\response\TemplatesDriven && $rpTemplate) {
            if (count($interactions) > 1) {
                $changed = true;
            } else {
                $interaction = reset($interactions);
                if ($interaction && $interaction->attr('responseIdentifier') != 'RESPONSE') {
                    $changed = true;
                }
            }
        }

        return $changed;
    }
1361155
public function getBodyStyles(core_kernel_classes_Resource $itemResource, $langCode = ''){
        $itemContent = $this->getItemContent($itemResource, $langCode);
        if(is_null($itemContent)){
            throw new \common_Exception('cannot find valid qti item content');
        }else{
            $classAttr = (string) $itemContent->itemBody['class'];
            preg_match_all('/x-tao-style-'.self::STYLE_NAME_PATTERN.'/', $classAttr, $matches);
            if(isset($matches[1])){
                return $matches[1];
            }
        }
    }