/** * Test getters and setters. */ public function testGetSet() { $term = new EducaTerm('type', 'uuid'); $term->setContext('LREv3.0'); $this->assertEquals('LREv3.0', $term->getContext(), "The getter/setter works for contexts."); }
/** * Parse the curriculum definition file. * * By passing the official curriculum definition file (JSON), this method * will parse it and return a curriculum definition it can understand and * treat. It mainly needs a "dictionary" of term types. See * \Educa\DSB\Client\Curriculum\EducaCurriculum::setCurriculumDictionary(). * * @param string $curriculumJson * The curriculum definition file, in JSON. * * @return array * An object with 2 properties: * - curriculum: A parsed and prepared curriculum tree. It uses * Educa\DSB\Client\Curriculum\Term\TermInterface elements to define * the curriculum tree. * - dictionary: A dictionary of term identifiers, with name and type * information for each one of them. * * @see \Educa\DSB\Client\Curriculum\EducaCurriculum::setCurriculumDictionary() */ public static function parseCurriculumJson($curriculumJson) { $data = json_decode($curriculumJson); // Prepare the dictionary. $dictionary = array(); // Prepare a list of items. This will make the creation of our // curriculum tree easier to manage. $list = array(); $root = new EducaTerm('root', 'root'); foreach ($data->vocabularies as $vocabulary) { $dictionary[$vocabulary->identifier] = (object) array('name' => $vocabulary->name, 'type' => $vocabulary->identifier); $list[$vocabulary->identifier]['root'] = new EducaTerm($vocabulary->identifier, $vocabulary->identifier, $vocabulary->name, 'LOM-CHv1.0'); $root->addChild($list[$vocabulary->identifier]['root']); foreach ($vocabulary->terms as $term) { if (!empty($term->deprecated)) { continue; } $type = static::parseCurriculumJsonGetType($vocabulary, $term); // Store the term definition in the dictionary. $dictionary[$term->identifier] = (object) array('name' => $term->name, 'type' => $type, 'context' => $term->context); // Did we already create this term, on a temporary basis? if (isset($list[$vocabulary->identifier][$term->identifier])) { // We need to "enhance" it now with its actual // information. $item = $list[$vocabulary->identifier][$term->identifier]; $item->setDescription($type, $term->identifier, $term->name); } else { // Prepare the term element. $item = new EducaTerm($type, $term->identifier, $term->name, $term->context); $list[$vocabulary->identifier][$term->identifier] = $item; } // Does it have a parent? if (!empty($term->parents)) { // Now, we may not have found the parent yet. Check if // we already have the parent item ready. Even though // the parents property is an array, in practice there // is always a single parent, so we can safely treat the // first key. if (isset($list[$vocabulary->identifier][$term->parents[0]])) { // Found the parent. $parent = $list[$vocabulary->identifier][$term->parents[0]]; } else { // There is no parent item ready yet. We need to // create a temporary one, which will be enhanced as // soon as we reach the actual parent term. $parent = new EducaTerm('temp', 'temp'); // Store it already; later, we will update its // description data. $list[$vocabulary->identifier][$term->parents[0]] = $parent; } } else { // If not, we add it to the root. $parent = $list[$vocabulary->identifier]['root']; } $parent->addChild($item); } } return (object) array('curriculum' => $root, 'dictionary' => $dictionary); }