/**
  * Starts the import based on the form
  *
  * @param \core_kernel_classes_Class $class
  * @param \tao_helpers_form_Form $form
  * @return \common_report_Report $report
  */
 public function import($class, $form)
 {
     //as upload may be called multiple times, we remove the session lock as soon as possible
     session_write_close();
     try {
         $file = $form->getValue('source');
         $service = MediaService::singleton();
         $classUri = $class->getUri();
         if (is_null($this->instanceUri) || $this->instanceUri === $classUri) {
             //if the file is a zip do a zip import
             if ($file['type'] !== 'application/zip') {
                 try {
                     self::isValidSharedStimulus($file['uploaded_file']);
                     $filepath = $file['uploaded_file'];
                     $name = $file['name'];
                     if (!$service->createMediaInstance($filepath, $classUri, \tao_helpers_Uri::decode($form->getValue('lang')), $name, 'application/qti+xml')) {
                         $report = \common_report_Report::createFailure(__('Fail to import Shared Stimulus'));
                     } else {
                         $report = \common_report_Report::createSuccess(__('Shared Stimulus imported successfully'));
                     }
                 } catch (XmlStorageException $e) {
                     // The shared stimulus is not qti compliant, display error
                     $report = \common_report_Report::createFailure($e->getMessage());
                 }
             } else {
                 $report = $this->zipImporter->import($class, $form);
             }
         } else {
             if ($file['type'] !== 'application/zip') {
                 self::isValidSharedStimulus($file['uploaded_file']);
                 $filepath = $file['uploaded_file'];
                 if (in_array($file['type'], array('application/xml', 'text/xml'))) {
                     $name = basename($file['name'], 'xml');
                     $name .= 'xhtml';
                     $filepath = dirname($file['name']) . '/' . $name;
                     \tao_helpers_File::copy($file['uploaded_file'], $filepath);
                 }
                 if (!$service->editMediaInstance($filepath, $this->instanceUri, \tao_helpers_Uri::decode($form->getValue('lang')))) {
                     $report = \common_report_Report::createFailure(__('Fail to edit shared stimulus'));
                 } else {
                     $report = \common_report_Report::createSuccess(__('Shared Stimulus edited successfully'));
                 }
             } else {
                 $report = $this->zipImporter->edit(new \core_kernel_classes_Resource($this->instanceUri), $form);
             }
         }
         return $report;
     } catch (\Exception $e) {
         $report = \common_report_Report::createFailure($e->getMessage());
         return $report;
     }
 }