/** * Displays an edit form * * @return void */ public function editTask($model = null) { Request::setVar('hidemainmenu', 1); if (!is_object($model)) { // Incoming $id = Request::getVar('id', array(0)); // Get the single ID we're working with if (is_array($id)) { $id = !empty($id) ? $id[0] : ''; } $model = new \Components\Courses\Models\Assetgroup($id); } $this->view->row = $model; if (!$this->view->row->get('unit_id')) { $this->view->row->set('unit_id', Request::getInt('unit', 0)); } $this->view->unit = \Components\Courses\Models\Unit::getInstance($this->view->row->get('unit_id')); $this->view->offering = \Components\Courses\Models\Offering::getInstance($this->view->unit->get('offering_id')); $rows = $this->view->unit->assetgroups(); // establish the hierarchy of the menu $children = array(0 => array()); $levellimit = 500; // first pass - collect children foreach ($rows as $v) { $children[0][] = $v; $children[$v->get('id')] = $v->children(); } // second pass - get an indent list of the items $this->view->assetgroups = $this->treeRecurse(0, '', array(), $children, max(0, $levellimit - 1)); // Set any errors foreach ($this->getErrors() as $error) { \Notify::error($error); } // Output the HTML $this->view->setLayout('edit')->display(); }
defined('_HZEXEC_') or die; $results = null; $notes = $this->model->notes($this->filters); if ($notes) { foreach ($notes as $note) { $ky = $note->get('scope_id'); if (!isset($results[$ky])) { $results[$ky] = array(); } $results[$ky][] = $note; } } $base = $this->offering->link(); if ($results) { foreach ($results as $id => $notes) { $lecture = new \Components\Courses\Models\Assetgroup($id); $unit = \Components\Courses\Models\Unit::getInstance($lecture->get('unit_id')); echo $this->escape(stripslashes($lecture->get('title'))) . "\n"; echo '--------------------------------------------------' . "\n"; foreach ($notes as $note) { echo '#' . $note->get('id'); if ($note->get('timestamp') != '00:00:00') { echo ' video time: ' . $this->escape($note->get('timestamp')); } echo "\n"; echo $this->escape(stripslashes($note->get('content'))); echo "\n\n"; } echo "\n"; } }
/** * Removes a course and all associated information * * @return void */ public function deleteTask() { // Check for request forgeries Request::checkToken(); // Incoming $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; $num = 0; // Do we have any IDs? if (!empty($ids)) { foreach ($ids as $id) { // Load the course page $model = \Components\Courses\Models\Unit::getInstance($id); // Ensure we found the course info if (!$model->exists()) { continue; } // Delete course if (!$model->delete()) { throw new Exception(Lang::txt('COM_COURSES_ERROR_UNABLE_TO_REMOVE_ENTRY'), 500); } // Log the course approval $log = new Tables\Log($this->database); $log->scope_id = $id; $log->scope = 'course_unit'; $log->user_id = User::get('id'); $log->timestamp = Date::toSql(); $log->action = 'unit_deleted'; $log->actor_id = User::get('id'); if (!$log->store()) { $this->setError($log->getError()); } $num++; } } // Redirect back to the courses page App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&offering=' . Request::getInt('offering', 0), false), Lang::txt('COM_COURSES_ITEMS_REMOVED', $num)); }
/** * Method to get/set the current unit * * @param mixed $id ID or alias of specific unit * @return object \Components\Courses\Models\Unit */ public function unit($id = null) { if (!isset($this->_unit) || $id !== null && (int) $this->_unit->get('id') != $id && (string) $this->_unit->get('alias') != $id) { $this->_unit = null; if (isset($this->_units)) { foreach ($this->units() as $key => $unit) { if ((int) $unit->get('id') == $id || (string) $unit->get('alias') == $id) { $this->_unit = $unit; $units = $this->units(); $this->_unit->siblings($units); break; } } } if (is_null($this->_unit)) { $this->_unit = Unit::getInstance($id, $this->get('id')); } } return $this->_unit; }
/** * Actions to perform after deleting an assetgroup * * @param object $model \Components\Courses\Models\Assetgroup * @return void */ public function onAssetgroupDelete($assetgroup) { if (!$assetgroup->exists()) { return; } require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'models' . DS . 'unit.php'; $db = App::get('db'); $unit = \Components\Courses\Models\Unit::getInstance($assetgroup->get('unit_id')); // Attempt to load an associated category $category = new \Components\Forum\Tables\Category($db); $category->loadByObject($assetgroup->get('id'), null, $unit->get('offering_id'), 'course'); // Was a category found? if ($category->id && $category->state != 2) { // Mark as deleted $category->state = 2; if ($category->check()) { $category->store(); } // Mark all threads in category as deleted $thread = new \Components\Forum\Tables\Post($db); $thread->setStateByCategory($category->get('id'), 2); } // Bit of recursion here for nested asset groups if ($assetgroup->children(null, true)->total() > 0) { foreach ($assetgroup->children() as $child) { $this->onAssetgroupDelete($child); } } }
/** * Actions to perform after deleting an assetgroup * * @param object $model \Components\Courses\Models\Assetgroup * @return void */ public function onAssetgroupDelete($assetgroup) { if (!$assetgroup->exists()) { return; } require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'models' . DS . 'unit.php'; $unit = \Components\Courses\Models\Unit::getInstance($assetgroup->get('unit_id')); // Attempt to load an associated category $category = Category::all()->whereEquals('object_id', $assetgroup->get('id'))->whereEquals('scope', 'course')->whereEquals('scope_id', $unit->get('offering_id'))->whereEquals('state', Category::STATE_PUBLISHED)->row(); // Was a category found? if ($category->get('id') && $category->get('state') != Category::STATE_DELETED) { // Mark as deleted // Note: State will carry through to threads under this category. $category->set('state', Category::STATE_DELETED); $category->save(); } // Bit of recursion here for nested asset groups if ($assetgroup->children(null, true)->total() > 0) { foreach ($assetgroup->children() as $child) { $this->onAssetgroupDelete($child); } } }
/** * Saves a course unit * * @apiMethod POST * @apiUri /courses/unit/save * @apiParameter { * "name": "id", * "description": "Unit ID to edit", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "section_id", * "description": "Section ID of unit", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "offering_id", * "description": "Offering ID of unit", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "title", * "description": "Unit title", * "type": "string", * "required": false, * "default": "New Unit" * } * @apiParameter { * "name": "publish_up", * "description": "Start publishing date", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "publish_down", * "description": "Stop publishing date", * "type": "string", * "required": false, * "default": null * } * @return void */ public function saveTask() { // Require authentication and authorization $this->authorizeOrFail(); // Make sure we have an incoming 'id' $id = Request::getInt('id', null); // Create our unit model $unit = Unit::getInstance($id); // Check to make sure we have a unit object if (!is_object($unit)) { App::abort(500, 'Failed to instantiate a unit object'); } if ($section_id = Request::getInt('section_id', false)) { $unit->set('section_id', $section_id); } // We'll always save the title again, even if it's just to the same thing $title = $unit->get('title'); $title = !empty($title) ? $title : 'New Unit'; // Set our values $unit->set('title', Request::getString('title', $title)); $unit->set('alias', strtolower(str_replace(' ', '', $unit->get('title')))); $offset = Config::get('offset'); // If we have dates coming in, save those if ($publish_up = Request::getVar('publish_up', false)) { $unit->set('publish_up', Date::of($publish_up, $offset)->toSql()); } if ($publish_down = Request::getVar('publish_down', false)) { $unit->set('publish_down', Date::of($publish_down, $offset)->toSql()); } // When creating a new unit if (!$id) { $unit->set('offering_id', Request::getInt('offering_id', 0)); $unit->set('created', Date::toSql()); $unit->set('created_by', App::get('authn')['user_id']); } // Save the unit if (!$unit->store()) { App::abort(500, "Saving unit {$id} failed ({$unit->getError()})"); } // Create a placeholder for our return object $assetGroups = []; // If this is a new unit, give it some default asset groups // Create a top level asset group for each of lectures, homework, and exam if (!$id) { // Get the courses config $config = Component::params('com_courses'); $asset_groups = explode(',', $config->get('default_asset_groups', 'Lectures, Homework, Exam')); array_map('trim', $asset_groups); foreach ($asset_groups as $key) { // Get our asset group object $assetGroup = new Assetgroup(null); $assetGroup->set('title', $key); $assetGroup->set('alias', strtolower(str_replace(' ', '', $assetGroup->get('title')))); $assetGroup->set('unit_id', $unit->get('id')); $assetGroup->set('parent', 0); $assetGroup->set('created', Date::toSql()); $assetGroup->set('created_by', App::get('authn')['user_id']); // Save the asset group if (!$assetGroup->store()) { App::abort(500, 'Asset group save failed'); } $return = new stdclass(); $return->assetgroup_id = $assetGroup->get('id'); $return->assetgroup_title = $assetGroup->get('title'); $return->course_id = $this->course_id; $return->assetgroup_style = ''; $assetGroups[] = $return; } } // Need to return the content of the prerequisites view (not sure of a better way to do this at the moment) // @FIXME: need to handle this another way...shouldn't be loading up views from API! /*$view = new \Hubzero\Plugin\View(array( 'folder' => 'courses', 'element' => 'outline', 'name' => 'outline', 'layout' => '_prerequisites' )); $view->set('scope', 'unit') ->set('scope_id', $unit->get('id')) ->set('section_id', $this->course->offering()->section()->get('id')) ->set('items', clone($this->course->offering()->units()));*/ // Return message $this->send(['unit_id' => $unit->get('id'), 'unit_title' => $unit->get('title'), 'course_id' => $this->course_id, 'assetgroups' => $assetGroups, 'course_alias' => $this->course->get('alias'), 'offering_alias' => $this->offering_alias, 'section_id' => isset($section_id) ? $section_id : $this->course->offering()->section()->get('id'), 'prerequisites' => ''], $id ? 200 : 201); }