/**
  * Creates the specified element. More info about available elements in http://docs.moodle.org/dev/Acceptance_testing#Fixtures.
  *
  * @Given /^the following lp "(?P<element_string>(?:[^"]|\\")*)" exist:$/
  *
  * @throws Exception
  * @throws PendingException
  * @param string    $elementname The name of the entity to add
  * @param TableNode $data
  */
 public function the_following_lp_exist($elementname, TableNode $data)
 {
     // Now that we need them require the data generators.
     require_once __DIR__ . '/../../../../../lib/phpunit/classes/util.php';
     if (empty(self::$elements[$elementname])) {
         throw new PendingException($elementname . ' data generator is not implemented');
     }
     $datagenerator = testing_util::get_data_generator();
     $this->datageneratorlp = $datagenerator->get_plugin_generator('core_competency');
     $elementdatagenerator = self::$elements[$elementname]['datagenerator'];
     $requiredfields = self::$elements[$elementname]['required'];
     if (!empty(self::$elements[$elementname]['switchids'])) {
         $switchids = self::$elements[$elementname]['switchids'];
     }
     foreach ($data->getHash() as $elementdata) {
         // Check if all the required fields are there.
         foreach ($requiredfields as $requiredfield) {
             if (!isset($elementdata[$requiredfield])) {
                 throw new Exception($elementname . ' requires the field ' . $requiredfield . ' to be specified');
             }
         }
         // Switch from human-friendly references to ids.
         if (isset($switchids)) {
             foreach ($switchids as $element => $field) {
                 $methodname = 'get_' . $element . '_id';
                 // Not all the switch fields are required, default vars will be assigned by data generators.
                 if (isset($elementdata[$element])) {
                     // Temp $id var to avoid problems when $element == $field.
                     $id = $this->{$methodname}($elementdata[$element]);
                     unset($elementdata[$element]);
                     $elementdata[$field] = $id;
                 }
             }
         }
         // Preprocess the entities that requires a special treatment.
         if (method_exists($this, 'preprocess_' . $elementdatagenerator)) {
             $elementdata = $this->{'preprocess_' . $elementdatagenerator}($elementdata);
         }
         // Creates element.
         $methodname = 'create_' . $elementdatagenerator;
         if (method_exists($this->datageneratorlp, $methodname)) {
             // Using data generators directly.
             $this->datageneratorlp->{$methodname}($elementdata);
         } else {
             if (method_exists($this, 'process_' . $elementdatagenerator)) {
                 // Using an alternative to the direct data generator call.
                 $this->{'process_' . $elementdatagenerator}($elementdata);
             } else {
                 throw new PendingException($elementname . ' data generator is not implemented');
             }
         }
     }
 }
 /**
  * Add a label to a course that is a link to a file in the fixtures directory of this plugin.
  *
  * @Given /^course "(?P<COURSE_SHORTNAME>[^"]*)" has a label linking to Jmol fixture "(?P<FILE_NAME>[^"]*)"$/
  */
 public function course_contains_label_linking_fixture($shortname, $filename)
 {
     global $CFG, $DB;
     $courseid = $DB->get_field('course', 'id', array('shortname' => $shortname));
     if (!$courseid) {
         throw new Exception('The specified course with shortname "' . $shortname . '" does not exist.');
     }
     if (!is_readable($CFG->dirroot . '/filter/jmol/tests/fixtures/' . $filename)) {
         throw new Exception('The fixture file "' . $filename . '" does not exist.');
     }
     $link = html_writer::link(new moodle_url('/filter/jmol/tests/fixtures/' . $filename), "Molecule");
     $generator = testing_util::get_data_generator();
     $generator->create_module('label', array('course' => $courseid, 'intro' => $link));
 }