Load Dom & Xpath of xml item content & register xpath namespace

@param \core_kernel_classes_Resource $item
@return $this
@throws ExtractorException
777687
private function loadXml(\core_kernel_classes_Resource $item)
    {
        $itemService = Service::singleton();

        try {
            $xml = $itemService->getXmlByRdfItem($item);
            if (empty($xml)) {
                throw new ExtractorException('No content found for item ' . $item->getUri());
            }
        } catch (FileNotFoundException $e) {
            throw new ExtractorException('qti.xml file was not found for item '. $item->getUri() .'; The item might be empty.');
        }

        $this->dom   = new \DOMDocument();
        $this->dom->loadXml($xml);
        $this->xpath = new \DOMXpath($this->dom);
        $this->xpath->registerNamespace('qti', $this->dom->documentElement->namespaceURI);

        return $this;
    }
290061
public function addItem($item){
		$itemO=$this->getItemToAdd($item);
		$this->addContent($itemO);
		return $itemO;
	}
397426
public function removeItem(Item $item)
    {
        if ($this->items->contains($item)) {
            $this->items->removeElement($item);
            $item->removeLabel($this);
        }

        return $this;
    }
1259061
public function add(SelectionItem $item)
    {
        $this->items[$item->getTarget()->getId().'_'.$item->getLanguage()] = $item;

        return $this;
    }
964989
public function addItem($item)
    {
        if ($part = $this->getElements()->last()) {
            $part->addElement($item);
        } else {
            $this->addBody($item);
        }

        return $this;
    }
476918
private function loadXml($file)
    {
        $internal = libxml_use_internal_errors(true);

        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $doc->validateOnParse = true;

        if (!@$doc->load($file)) {
            $errors = libxml_get_errors();

            libxml_use_internal_errors($internal);

            throw XmlException::createUsingErrors($errors);
        }

        libxml_use_internal_errors($internal);

        return new DOMXPath($doc);
    }
232202
protected function getStorageDirectory(\core_kernel_classes_Resource $item, $lang)
    {
        $itemService = \taoItems_models_classes_ItemsService::singleton();
        $directory = $itemService->getItemDirectory($item, $lang);

        //we should use be language unaware for storage manipulation
        $path = str_replace($lang, '', $directory->getPrefix());
        return $itemService->getDefaultItemDirectory()->getDirectory($path);
    }
184979
public function writeItem(array $item)
    {
        $newNode = $this->xml->createElement($this->rowNodeName);

        //attributes
        if (isset($item['@attributes']) && is_array($item['@attributes'])) {
            foreach ($item['@attributes'] as $key => $value) {
                $attr = $this->xml->createAttribute($key);
                $attr->value = $value;
                $newNode->appendChild($attr);
            }
            unset($item['@attributes']);
        }

        //values
        foreach ($item as $key => $value) {
            $node = $this->xml->createElement($key);
            $node->nodeValue = $value;
            $newNode->appendChild($node);
        }

        $this->rootNode->appendChild($newNode);

        return $this;
    }
852494
protected function read($namespace, $group, $locale, $item)
	{
		$this->load($namespace, $group, $locale);

		$data = Arr::get($this->loaded[$namespace][$group][$locale], $item);

		return $data;
	}
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);

    }
520678
private function parseArrayToFieldEntity(array $item, $elementType)
    {
        $entity = new Field();
        $entity->set($item);
        $entity->element_type = $elementType;

        return $entity;
    }
1265533
public function set(CacheItem $item): self
    {
        $driver = $this->getDriver();
        $driver->set($item->key, $item->value, $item->ttl);
        if ($this->hasEventListeners('itemSet')) {
            $this->dispatchEvent('itemSet', new \BearFramework\App\Cache\ItemSetEventDetails(clone($item)));
        }
        if ($this->hasEventListeners('itemChange')) {
            $this->dispatchEvent('itemChange', new \BearFramework\App\Cache\ItemChangeEventDetails($item->key));
        }
        return $this;
    }
213019
protected function itemTypeToIconClass($item, $itemType)
    {
        if ($item->type == MediaLibraryItem::TYPE_FOLDER) {
            return 'icon-folder';
        }

        switch ($itemType) {
            case MediaLibraryItem::FILE_TYPE_IMAGE: return "icon-picture-o";
            case MediaLibraryItem::FILE_TYPE_VIDEO: return "icon-video-camera";
            case MediaLibraryItem::FILE_TYPE_AUDIO: return "icon-volume-up";
            default: return "icon-file";
        }
    }
735498
public function removeItem(Item $item)
    {
        if ($this->items->contains($item)) {
            $this->items->removeElement($item);
            $item->removeGenre($this);
        }

        return $this;
    }
1173332
protected function prepareItem($item)
    {
        if (!isset($item["type"])) {
            return $item;
        }

        if (isset($item["children"])) {
            $item["children"] = $this->prepareItems($item["children"]);
        }

        switch ($item['type']) {
            case 'select':
                if (isset($item['sql'])) {
                    $connectionName = isset($item['connection']) ? $item['connection'] : 'default';
                    $sql            = $item['sql'];
                    $options        = isset($item["options"]) ? $item["options"] : array();

                    unset($item['sql']);
                    unset($item['connection']);
                    /** @var Connection $connection */
                    $connection = $this->container->get("doctrine.dbal.{$connectionName}_connection");
                    $all        = $connection->fetchAll($sql);
                    foreach ($all as $option) {
                        $options[] = array(reset($option), end($option));
                    }
                    $item["options"] = $options;
                }

                if (isset($item['service'])) {
                    $serviceInfo = $item['service'];
                    $serviceName = isset($serviceInfo['serviceName']) ? $serviceInfo['serviceName'] : 'default';
                    $method      = isset($serviceInfo['method']) ? $serviceInfo['method'] : 'get';
                    $args        = isset($serviceInfo['args']) ? $item['args'] : '';
                    $service     = $this->container->get($serviceName);
                    $options     = $service->$method($args);

                    $item['options'] = $options;
                }

                if (isset($item['dataStore'])) {
                    $dataStoreInfo = $item['dataStore'];
                    $dataStore     = $this->container->get('data.source')->get($dataStoreInfo["id"]);
                    $options       = array();
                    foreach ($dataStore->search() as $dataItem) {
                        $options[ $dataItem->getId() ] = $dataItem->getAttribute($dataStoreInfo["text"]);
                    }
                    if (isset($item['dataStore']['popupItems'])) {
                        $item['dataStore']['popupItems'] = $this->prepareItems($item['dataStore']['popupItems']);
                    }
                    $item['options'] = $options;
                }
                break;
        }
        return $item;
    }
779073
public function render( core_kernel_classes_Resource $item, $langCode)
    {
    	$itemService = taoItems_models_classes_ItemsService::singleton();
    	$file = $itemService->getItemDirectory($item)->getFile('index.html');
        $xhtml = $file->read();

        // Check if all needed APIs are referenced.
        $xhtml = $this->replaceDeprecatedApis($xhtml); // throws ItemModelException.

        return $xhtml;
    }
942470
protected function processResponses(core_kernel_classes_Resource $item) {

        $jsonPayload = taoQtiCommon_helpers_Utils::readJsonPayload();

        try {
            $qtiXmlFileContent = QtiFile::getQtiFileContent($item);
            $qtiXmlDoc = new XmlDocument();
            $qtiXmlDoc->loadFromString($qtiXmlFileContent);
        }
        catch (StorageException $e) {
            $msg = "An error occured while loading QTI-XML file at expected location '${qtiXmlFilePath}'.";
            throw new \RuntimeException($msg, 0, $e);
        }

        $itemSession = new AssessmentItemSession($qtiXmlDoc->getDocumentComponent(), new SessionManager());
        $itemSession->beginItemSession();

        $variables = array();

        // Convert client-side data as QtiSm Runtime Variables.
        foreach($jsonPayload as $identifier => $response) {
            
            $filler = new taoQtiCommon_helpers_PciVariableFiller($qtiXmlDoc->getDocumentComponent());
            
            try {
                $var = $filler->fill($identifier, $response);
                // Do not take into account QTI File placeholders.
                if (taoQtiCommon_helpers_Utils::isQtiFilePlaceHolder($var) === false) {
                    $variables[] = $var;
                }
            }
            catch(\OutOfRangeException $e) {
                // A variable value could not be converted, ignore it.
                // Developer's note: QTI Pairs with a single identifier (missing second identifier of the pair) are transmitted as an array of length 1,
                // this might cause problem. Such "broken" pairs are simply ignored.
                common_Logger::d("Client-side value for variable '${identifier}' is ignored due to data malformation.");
            }
            catch(\OutOfBoundsException $e) {
                // The response identifier does not match any response declaration.
                common_Logger::d("Uknown item variable declaration '${identifier}.");
            }
        }

        try {
            $itemSession->beginAttempt();
            $itemSession->endAttempt(new State($variables));

            // Transmit results to the Result Server.
            $this->transmitResults($item, $itemSession);

            // Return the item session state to the client-side.
            echo json_encode(array(
                'success' => true,
                'displayFeedback' => true,
                'itemSession' => self::buildOutcomeResponse($itemSession),
                'feedbacks' => $this->getFeedbacks($itemSession)
            ));
        }
        catch(AssessmentItemSessionException $e) {
            $msg = "An error occured while processing the responses.";
            throw new \RuntimeException($msg, 0, $e);
        }
        catch(taoQtiCommon_helpers_ResultTransmissionException $e) {
            $msg = "An error occured while transmitting variable '${identifier}' to the target Result Server.";
            throw new \RuntimeException($msg, 0, $e);
        }
    }