/** * 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; }
/** * Cancel a course page task * * @return void */ public function reorderTask() { // Check for request forgeries Request::checkToken(); $move = $this->_task == 'orderup' ? -1 : 1; // Incoming $id = Request::getVar('id', array()); $id = $id[0]; $tmpl = Request::getVar('tmpl', ''); $scope = Request::getVar('scope', 'asset_group'); $scope_id = Request::getInt('scope_id', 0); $course_id = Request::getInt('course_id', 0); // Get the element moving down - item 1 $tbl = new Tables\AssetAssociation($this->database); $tbl->loadByAssetScope($id, $scope_id, $scope); if (!$tbl->move($move, "scope=" . $this->database->Quote($scope) . " AND scope_id=" . $this->database->Quote(intval($scope_id)))) { echo $tbl->getError(); return; } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&tmpl=' . $tmpl . '&scope=' . $scope . '&scope_id=' . $scope_id . '&course_id=' . $course_id, false)); }
/** * Deletes an asset * * @apiMethod POST * @apiUri /courses/asset/delete * @apiParameter { * "name": "asset_id", * "description": "ID of asset to delete", * "type": "integer", * "required": true, * "default": null * } * @apiParameter { * "name": "scope", * "description": "Asset scope", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "scope_id", * "description": "Asset scope ID", * "type": "integer", * "required": true, * "default": null * } * @return void */ public function deleteTask() { // Require authentication and authorization $this->authorizeOrFail(); // First, delete the asset association $database = App::get('db'); $assocObj = new AssetAssociation($database); // Get vars $asset_id = Request::getInt('asset_id', 0); $scope = Request::getCmd('scope', 'asset_group'); $scope_id = Request::getInt('scope_id', 0); // Make sure we're not missing anything if (!$asset_id || !$scope || !$scope_id) { // Missing needed variables to identify asset association App::abort(404, 'Missing one of asset id, scope, or scope id'); } else { // Try to load the association if (!$assocObj->loadByAssetScope($asset_id, $scope_id, $scope)) { App::abort(500, 'Loading asset association failed'); } else { // Delete the association if (!$assocObj->delete()) { App::abort(500, $assocObj->getError()); } } } // Then, lookup whether or not there are other assocations connected to this asset $assetObj = new AssetTbl($database); if (!$assetObj->load($asset_id)) { App::abort(500, "Loading asset {$id} failed"); } // See if the asset is orphaned if (!$assetObj->isOrphaned()) { // Asset isn't an orphan (i.e. it's still being used elsewhere), so we're done $this->send(['asset_id' => $assetObj->id]); return; } // If no other associations exist, we'll delete the asset file and folder on the file system $deleted = []; $params = Component::params('com_courses'); $path = DS . trim($params->get('uploadpath', '/site/courses'), DS) . DS . $this->course_id . DS . $assetObj->id; // If the path exists, delete it! if (Filesystem::exists($path)) { $deleted = Filesystem::listFolderTree($path); Filesystem::deleteDirectory($path); } // Then we'll delete the asset entry itself if (!$assetObj->delete()) { App::abort(500, $assetObj->getError()); } // Return message $this->send(['asset_id' => $assetObj->id, 'deleted' => $deleted]); }
/** * 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; }