Пример #1
0
 /**
  * Copy an entry and associated data
  *
  * @param   integer $course_id New course to copy to
  * @param   boolean $deep      Copy associated data?
  * @return  boolean True on success, false on error
  */
 public function copy($course_id = null, $deep = true)
 {
     // Get some old info we may need
     //  - Offering ID
     //  - Course ID
     $o_id = $this->get('id');
     $c_id = $this->get('course_id');
     $oldOfferingAssets = $this->assets();
     // Reset the ID. This will force store() to create a new record.
     $this->set('id', 0);
     // Are we copying to a new course?
     if ($course_id) {
         $this->set('course_id', $course_id);
     } else {
         // Copying to the same course so we want to distinguish
         // this offering from the one we copied from
         $this->set('title', $this->get('title') . ' (copy)');
         $this->set('alias', $this->get('alias') . '_copy');
     }
     if (!$this->store()) {
         return false;
     }
     if ($deep) {
         // Copy pages
         foreach ($this->pages(array('offering_id' => $o_id, 'active' => array(0, 1)), true) as $page) {
             if (!$page->copy($this->get('course_id'), $this->get('id'))) {
                 $this->setError($page->getError());
             }
         }
         // Copy units
         foreach ($this->units(array('offering_id' => $o_id, 'section_id' => -1), true) as $unit) {
             if (!$unit->copy($this->get('id'))) {
                 $this->setError($unit->getError());
             }
         }
         // Copy logo
         if ($file = $this->logo('file')) {
             $src = DS . trim($this->config('uploadpath', '/site/courses'), '/') . DS . $c_id . '/offerings/' . $o_id . DS . $file;
             if (file_exists(PATH_APP . $src)) {
                 $dest = DS . trim($this->config('uploadpath', '/site/courses'), '/') . DS . $this->get('course_id') . '/offerings/' . $this->get('id');
                 if (!is_dir(PATH_APP . $dest)) {
                     if (!Filesystem::makeDirectory(PATH_APP . $dest)) {
                         $this->setError(Lang::txt('UNABLE_TO_CREATE_UPLOAD_PATH'));
                     }
                 }
                 $dest .= DS . $file;
                 if (!Filesystem::copy(PATH_APP . $src, PATH_APP . $dest)) {
                     $this->setError(Lang::txt('Failed to copy offering logo.'));
                 }
             }
         }
         // Copy assets (grab the assets from the original offering)
         if ($oldOfferingAssets) {
             foreach ($oldOfferingAssets as $asset) {
                 $oldAssetId = $asset->get('id');
                 if (!$asset->copy()) {
                     $this->setError($asset->getError());
                 } else {
                     // Copy asset associations
                     $tbl = new Tables\AssetAssociation($this->_db);
                     foreach ($tbl->find(array('scope_id' => $o_id, 'scope' => 'offering', 'asset_id' => $oldAssetId)) as $aa) {
                         $tbl->bind($aa);
                         $tbl->id = 0;
                         $tbl->scope_id = $this->get('id');
                         $tbl->asset_id = $asset->get('id');
                         if (!$tbl->store()) {
                             $this->setError($tbl->getError());
                         }
                     }
                 }
             }
         }
     }
     return true;
 }
Пример #2
0
 /**
  * Copy an entry and associated data
  *
  * @param   integer $offering_id New offering to copy to
  * @param   boolean $deep        Copy associated data?
  * @return  boolean True on success, false on error
  */
 public function copy($offering_id = null, $deep = true)
 {
     // Get some old info we may need
     //  - Unit ID
     //  - Offering ID
     $u_id = $this->get('id');
     $o_id = $this->get('offering_id');
     // Reset the ID. This will force store() to create a new record.
     $this->set('id', 0);
     // Are we copying to a new offering?
     if ($offering_id) {
         $this->set('offering_id', $offering_id);
     } else {
         // Copying to the same offering so we want to distinguish
         // this unit from the one we copied from
         $this->set('title', $this->get('title') . ' (copy)');
         $this->set('alias', $this->get('alias') . '_copy');
     }
     if (!$this->store()) {
         return false;
     }
     if ($deep) {
         // Copy assets
         $tbl = new Tables\AssetAssociation($this->_db);
         //foreach ($this->assets(array('asset_scope_id' => $u_id)) as $asset)
         foreach ($tbl->find(array('scope_id' => $u_id, 'scope' => 'unit')) as $asset) {
             $tbl->bind($asset);
             $tbl->id = 0;
             $tbl->scope_id = $this->get('id');
             //if (!$asset->copy($this->get('id')))
             if (!$tbl->store()) {
                 $this->setError($tbl->getError());
             }
         }
         // Copy asset groups
         foreach ($this->assetgroups(null, array('unit_id' => $u_id, 'parent' => 0)) as $assetgroup) {
             if (!$assetgroup->copy($this->get('id'), $deep)) {
                 $this->setError($assetgroup->getError());
             }
         }
     }
     return true;
 }
Пример #3
0
 /**
  * Get a count or list of parents for this entry
  *
  * @param   array $filters Fitlers to apply to results query
  * @return  array
  */
 public function parents($filters = array())
 {
     if (!isset($filters['asset_id'])) {
         $filters['asset_id'] = (int) $this->get('id');
     }
     $tbl = new Tables\AssetAssociation($this->_db);
     if (isset($filters['count']) && $filters['count']) {
         return $tbl->count($filters);
     }
     if (!($results = $tbl->find($filters))) {
         $results = array();
     }
     return $results;
 }