/** * 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)); }
/** * Reorders assets * * @apiMethod POST * @apiUri /courses/asset/reorder * @apiParameter { * "name": "asset", * "description": "Array of IDs of assets to reorder", * "type": "array", * "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 reorderTask() { // Get our asset group object $database = App::get('db'); $assetAssocationObj = new AssetAssociation($database); $assets = Request::getVar('asset', array()); $scope_id = Request::getInt('scope_id', 0); $scope = Request::getWord('scope', 'asset_group'); $order = 1; foreach ($assets as $asset_id) { if (!$assetAssocationObj->loadByAssetScope($asset_id, $scope_id, $scope)) { App::abort(500, "Loading asset association {$asset_id} failed"); } // Save the asset group if (!$assetAssocationObj->save(array('ordering' => $order))) { App::abort(500, 'Asset asssociation save failed'); } $order++; } // Return message $this->send('New asset order saved'); }
/** * Save method for this handler * // @FIXME: reduce code duplication here * * @return array of assets created **/ public function save() { // Include needed files require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'models' . DS . 'asset.php'; // Create our asset object $id = Request::getInt('id', null); $asset = new \Components\Courses\Models\Asset($id); // Grab the incoming content $content = Request::getVar('content', '', 'default', 'none', 2); // Get everything ready to store // Check if vars are already set (i.e. by a sub class), before setting them here $asset->set('title', !empty($this->asset['title']) ? $this->asset['title'] : strip_tags(substr($content, 0, 25))); $asset->set('type', !empty($this->asset['type']) ? $this->asset['type'] : 'text'); $asset->set('subtype', !empty($this->asset['subtype']) ? $this->asset['subtype'] : 'content'); $asset->set('content', !empty($this->asset['content']) ? $this->asset['content'] : $content); // If we have a state coming in as an int if ($graded = Request::getInt('graded', false)) { $asset->set('graded', $graded); // By default, weight asset as a 'homework' type $grade_weight = $asset->get('grade_weight'); if (empty($grade_weight)) { $asset->set('grade_weight', 'homework'); } else { $asset->set('grade_weight', $grade_weight); } } elseif ($graded = Request::getInt('edit_graded', false)) { $asset->set('graded', 0); } // If we're saving progress calculation var if ($progress = Request::getInt('progress_factors', false)) { $asset->set('progress_factors', array('asset_id' => $asset->get('id'), 'section_id' => Request::getInt('section_id', 0))); } elseif (Request::getInt('edit_progress_factors', false)) { $asset->set('section_id', Request::getInt('section_id', 0)); $asset->set('progress_factors', 'delete'); } // Save the asset if (!$asset->store()) { return array('error' => 'Asset save failed'); } $scope_id = Request::getInt('scope_id', null); $original_scope_id = Request::getInt('original_scope_id', null); $scope = Request::getCmd('scope', 'asset_group'); // Only worry about this if scope id is changing if (!is_null($scope_id) && !is_null($original_scope_id) && $scope_id != $original_scope_id) { // Create asset assoc object require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'tables' . DS . 'asset.association.php'; $assoc = new Tables\AssetAssociation($this->db); if (!$assoc->loadByAssetScope($asset->get('id'), $original_scope_id, $scope)) { return array('error' => 'Failed to load asset association'); } // Save the asset association if (!$assoc->save(array('scope_id' => $scope_id))) { return array('error' => 'Asset association save failed'); } } // Get the url to return to the page $course_id = Request::getInt('course_id', 0); $offering_alias = Request::getCmd('offering', ''); $course = new \Components\Courses\Models\Course($course_id); $course->offering($offering_alias); $url = Route::url($course->offering()->link() . '&asset=' . $asset->get('id')); $url = rtrim(str_replace('/api', '', Request::root()), '/') . '/' . ltrim($url, '/'); $files = array('asset_id' => $asset->get('id'), 'asset_title' => $asset->get('title'), 'asset_type' => $asset->get('type'), 'asset_subtype' => $asset->get('subtype'), 'asset_url' => $url, 'asset_state' => $asset->get('state'), 'scope_id' => $scope_id); $return_info = array('asset_id' => $asset->get('id'), 'asset_title' => $asset->get('title'), 'asset_type' => $asset->get('type'), 'asset_subtype' => $asset->get('subtype'), 'asset_url' => $url, 'course_id' => $asset->get('course_id'), 'offering_alias' => $offering_alias, 'scope_id' => $scope_id, 'files' => array($files)); // Return info return array('assets' => $return_info); }