/**
  * Commit the new TAO resource to the database
  * 
  * @return bool, true = success
  */
 function add_instance()
 {
     // Given an object containing all the necessary data,
     // (defined by the form in mod.html) this function
     // will create a new instance and return the id number
     // of the new instance.
     global $CFG;
     // is this a local resource or a remote one?
     if (!empty($this->url) && empty($this->file)) {
         $this->identifier = sha1($this->url);
         $this->mimetype = mimeinfo("type", $this->url);
     }
     if (isset($this->url) && $this->url && !$this->is_local_resource()) {
         $this->file = '';
     } else {
         if (empty($this->url) && isset($this->file) && $this->file) {
             if (!taoresource_check_and_create_moddata_taoresource_dir()) {
                 // error - can't create resources temp dir
                 error("Error - can't create TAO resources dir");
             }
             $filename = $CFG->dataroot . TAORESOURCE_RESOURCEPATH . $this->file;
             if (!taoresource_copy_file($this->tempfilename, $filename, true)) {
                 error("Error - can't copy temporary resource file ({$this->tempfilename}) to resource path ({$filename})");
             }
             $this->url = $CFG->wwwroot . '/mod/taoresource/view.php?identifier=' . $this->identifier;
             // tidy up temp file
             if (!taoresource_delete_file($this->tempfilename)) {
                 error("Error - can't delete temporary resource file ({$this->tempfilename})");
             }
         }
     }
     // one way or another we must have a URL by now
     if (!$this->url) {
         error("Tried to create a TAO Resource without a URL");
     }
     // trigger the before save plugins
     $this->before_save();
     $this->timemodified = time();
     if (!($id = insert_record('taoresource_entry', $this))) {
         return false;
     }
     $this->id = $id;
     // now do the LOM metadata elements
     foreach ($this->metadata_elements as $element) {
         $element->entry_id = $id;
         if (!$element->add_instance()) {
             return false;
         }
     }
     // trigger the after save plugins
     $this->after_save();
     return true;
 }
Esempio n. 2
0
             error("Error - can't create resources temp dir");
         }
         $tempfile = $_FILES['taoresourcefile']['tmp_name'];
         $taoresource_entry->uploadname = clean_param($_FILES['taoresourcefile']['name'], PARAM_PATH);
         $taoresource_entry->mimetype = clean_param($_FILES['taoresourcefile']['type'], PARAM_URL);
         if (empty($tempfile) || !($hash = taoresource_sha1file($tempfile))) {
             error("Error - can't create hash of incoming resource file");
         }
         $taoresource_entry->identifier = $hash;
         $taoresource_entry->file = $hash . '-' . $taoresource_entry->uploadname;
         $taoresource_entry->tempfilename = $CFG->dataroot . TAORESOURCE_TEMPPATH . $taoresource_entry->file;
         $formdata->identifier = $taoresource_entry->identifier;
         $formdata->file = $taoresource_entry->file;
         $formdata->uploadname = $taoresource_entry->uploadname;
         $formdata->mimetype = $taoresource_entry->mimetype;
         if (!taoresource_copy_file($tempfile, $taoresource_entry->tempfilename, true)) {
             error("Error - can't copy upload file to temp");
         }
     }
 } else {
     // is this a local resource or a remote one?
     if (!empty($formdata->url)) {
         $taoresource_entry->url = $formdata->url;
         $taoresource_entry->identifier = sha1($taoresource_entry->url);
         $taoresource_entry->mimetype = mimeinfo("type", $taoresource_entry->url);
     } else {
         // if these values are missing then we have a big problem - blowup appropriately
         if (empty($formdata->uploadname) || empty($formdata->mimetype) || empty($formdata->identifier) || empty($formdata->file)) {
             // die a horrible death
             error("Error - transition hiddne fields from step 1 missing in step 2");
         }