public function test_import_framework() { $this->resetAfterTest(true); $this->setAdminUser(); $importer = new framework_importer(file_get_contents(__DIR__ . '/fixtures/example.csv')); $this->assertEquals('', $importer->get_error()); $framework = $importer->import(); $this->assertEmpty('', $importer->get_error()); $this->assertGreaterThan(0, $framework->get_id()); $filters = ['competencyframeworkid' => $framework->get_id()]; $count = api::count_competencies($filters); $this->assertEquals(64, $count); // We can't test the exporter because it sends force-download headers. }
/** * Export all the competencies from this framework to a csv file. */ public function export() { global $CFG; require_once $CFG->libdir . '/csvlib.class.php'; $writer = new csv_export_writer(); $filename = clean_param($this->framework->get_shortname() . '-' . $this->framework->get_idnumber(), PARAM_FILE); $writer->set_filename($filename); $headers = framework_importer::list_required_headers(); $writer->add_data($headers); // Order and number of columns must match framework_importer::list_required_headers(). $row = array('', $this->framework->get_idnumber(), $this->framework->get_shortname(), $this->framework->get_description(), $this->framework->get_descriptionformat(), $this->framework->get_scale()->compact_items(), $this->framework->get_scaleconfiguration(), '', '', '', '', '', true, implode(',', $this->framework->get_taxonomies())); $writer->add_data($row); $filters = array('competencyframeworkid' => $this->framework->get_id()); $competencies = api::list_competencies($filters); // Index by id so we can lookup parents. $indexed = array(); foreach ($competencies as $competency) { $indexed[$competency->get_id()] = $competency; } foreach ($competencies as $competency) { $parentidnumber = ''; if ($competency->get_parentid() > 0) { $parent = $indexed[$competency->get_parentid()]; $parentidnumber = $parent->get_idnumber(); } $scalevalues = ''; $scaleconfig = ''; if ($competency->get_scaleid() !== null) { $scalevalues = $competency->get_scale()->compact_items(); $scaleconfig = $competency->get_scaleconfiguration(); } $ruleconfig = $competency->get_ruleconfig(); if ($ruleconfig === null) { $ruleconfig = "null"; } $allrelated = $competency->get_related_competencies(); $relatedidnumbers = array(); foreach ($allrelated as $onerelated) { $relatedidnumbers[] = str_replace(',', '%2C', $onerelated->get_idnumber()); } $relatedidnumbers = implode(',', $relatedidnumbers); // Order and number of columns must match framework_importer::list_required_headers(). $row = array($parentidnumber, $competency->get_idnumber(), $competency->get_shortname(), $competency->get_description(), $competency->get_descriptionformat(), $scalevalues, $scaleconfig, $competency->get_ruletype(), $competency->get_ruleoutcome(), $ruleconfig, $relatedidnumbers, $competency->get_id(), false, ''); $writer->add_data($row); } $writer->download_file(); }