Last active
March 7, 2016 18:57
-
-
Save drparham/ea18a72369d5169aca0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Http\Controllers\Admin; | |
Use Core\Http\Controllers\Controller as AdminController; | |
use Wc\Lms\Repositories\Course\CourseRepositoryInterface; | |
class CoursesController extends AdminController { | |
/** | |
* {@inheritDoc} | |
*/ | |
protected $csrfWhitelist = [ | |
'executeAction', | |
]; | |
/** | |
* The Learningmanagementsystem repository. | |
* | |
* @var \Modules\Lms\Src\Repositories\Course\CourseRepositoryInterface | |
*/ | |
protected $courses; | |
/** | |
* Holds all the mass actions we can execute. | |
* | |
* @var array | |
*/ | |
protected $actions = [ | |
'delete', | |
'enable', | |
'disable', | |
]; | |
/** | |
* Constructor. | |
* | |
* @param Modules\Lms\Src\Repositories\Course\CourseRepositoryInterface $course | |
* @return mixed | |
*/ | |
public function __construct(CourseRepositoryInterface $course) | |
{ | |
$this->middleware('auth'); | |
$this->middleware('needsPermission:lms.course.index', ['only' => ['index']]); | |
$this->middleware('needsPermission:lms.course.create', ['only' => ['create','store']]); | |
$this->middleware('needsPermission:lms.course.update', ['only' => ['edit','update']]); | |
$this->middleware('needsPermission:lms.course.delete', ['only' => ['delete']]); | |
$this->courses = $course; | |
} | |
/** | |
* Display a listing of courses. | |
* | |
* @return \Illuminate\View\View | |
*/ | |
public function index() | |
{ | |
$courses = $this->courses->findAll(); | |
return view('wc/lms-core::admin.courses.index')->with('content',$courses); | |
} | |
/** | |
* Show the form for creating new courses. | |
* | |
* @return \Illuminate\View\View | |
*/ | |
public function create() | |
{ | |
return $this->showForm('create'); | |
} | |
/** | |
* Handle posting of the form for creating new courses. | |
* | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function store() | |
{ | |
return $this->processForm('create'); | |
} | |
/** | |
* Show the form for updating courses. | |
* | |
* @param int $id | |
* @return mixed | |
*/ | |
public function edit($id) | |
{ | |
return $this->showForm('update', $id); | |
} | |
/** | |
* Handle posting of the form for updating courses. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function update($id) | |
{ | |
return $this->processForm('update', $id); | |
} | |
/** | |
* Remove the specified courses. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function delete($id) | |
{ | |
$type = $this->courses->delete($id) ? 'success' : 'error'; | |
$this->alerts->{$type}( | |
trans("wc/lms::courses/message.{$type}.delete") | |
); | |
return redirect()->route('admin.wc.lms.courses.all'); | |
} | |
/** | |
* Executes the mass action. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function executeAction() | |
{ | |
$action = request()->input('action'); | |
if (in_array($action, $this->actions)) | |
{ | |
foreach (request()->input('rows', []) as $row) | |
{ | |
$this->courses->{$action}($row); | |
} | |
return response('Success'); | |
} | |
return response('Failed', 500); | |
} | |
/** | |
* Shows the form. | |
* | |
* @param string $mode | |
* @param int $id | |
* @return mixed | |
*/ | |
protected function showForm($mode, $id = null) | |
{ | |
// Show the page | |
return view('wc/lms-core::admin.courses.form')->with('mode',$mode)->with('id',$id); | |
} | |
/** | |
* Processes the form. | |
* | |
* @param string $mode | |
* @param int $id | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
protected function processForm($mode, $id = null) | |
{ | |
// Store the courses | |
list($messages) = $this->courses->store($id, request()->all()); | |
// Do we have any errors? | |
if ($messages->isEmpty()) | |
{ | |
$this->alerts->success(trans("wc/lms::courses/message.success.{$mode}")); | |
return redirect()->route('admin.wc.lms.courses.all'); | |
} | |
$this->alerts->error($messages, 'form'); | |
return redirect()->back()->withInput(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Http\Handlers\Course; | |
use Illuminate\Events\Dispatcher; | |
use Wc\Lms\Entities\Course; | |
use Wc\Lms\Http\Handlers\EventHandler as BaseEventHandler; | |
class CourseEventHandler extends BaseEventHandler implements CourseEventHandlerInterface { | |
/** | |
* {@inheritDoc} | |
*/ | |
public function subscribe(Dispatcher $dispatcher) | |
{ | |
$dispatcher->listen('wc.lms.course.creating', __CLASS__.'@creating'); | |
$dispatcher->listen('wc.lms.course.created', __CLASS__.'@created'); | |
$dispatcher->listen('wc.lms.course.updating', __CLASS__.'@updating'); | |
$dispatcher->listen('wc.lms.course.updated', __CLASS__.'@updated'); | |
$dispatcher->listen('wc.lms.course.deleted', __CLASS__.'@deleted'); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function creating(array $data) | |
{ | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function created(Course $course) | |
{ | |
$this->flushCache($course); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function updating(Course $courses, array $data) | |
{ | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function updated(Course $course) | |
{ | |
$this->flushCache($course); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function deleted(Course $course) | |
{ | |
$this->flushCache($course); | |
} | |
/** | |
* Flush the cache. | |
* | |
* @param \Modules\Lms\Entities\Course $courses | |
* @return void | |
*/ | |
protected function flushCache(Course $course) | |
{ | |
$this->app['cache']->forget('wc.lms.course.all'); | |
$this->app['cache']->forget('wc.lms.course.highlights'); | |
$this->app['cache']->forget('wc.lms.course.'.$course->id); | |
$this->app['cache']->forget('wc.lms.course.description.'.$course->id); | |
$this->app['cache']->forget('wc.lms.course.department.'.$course->department_id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Facades\Course; | |
use Illuminate\Support\Facades\Facade; | |
class CourseFacade Extends Facade | |
{ | |
/** | |
* Get the registered name of the component. | |
* | |
* @return string | |
*/ | |
protected static function getFacadeAccessor() { return 'Wc\Lms\Facades\Course\CourseLibrary'; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Facades\Course; | |
use Wc\Lms\Facades\Department\DepartmentLibrary; | |
use Wc\Lms\Repositories\Course\CourseRepositoryInterface; | |
use Wc\Lms\Repositories\User\UserRepositoryInterface; | |
/** | |
* Class courseLibrary | |
* @package Modules\Lms\Facades\course | |
*/ | |
class CourseLibrary | |
{ | |
private $course; | |
private $user; | |
private $department; | |
/** | |
* @param CourseRepositoryInterface $course | |
* @param UserRepositoryInterface $user | |
* @param DepartmentLibrary $department | |
*/ | |
public function __construct(CourseRepositoryInterface $course, UserRepositoryInterface $user, DepartmentLibrary $department) | |
{ | |
$this->course = $course; | |
$this->user = $user; | |
$this->department = $department; | |
} | |
/** | |
* @param null $department | |
* @return \Westcottcourses\Learningmanagementsystem\Models\course | |
*/ | |
public function getCourses($department = null) | |
{ | |
if (is_null($department)) { | |
return $this->course->findAll(true); | |
} else { | |
$department = $this->department->findByAlias($department); | |
return $this->course->findByDepartment($department->id); | |
} | |
} | |
public function getHighlights() | |
{ | |
return $this->course->findByHighlight(); | |
} | |
/** | |
* @param $id | |
* @return \Westcottcourses\Learningmanagementsystem\Models\course | |
*/ | |
public function getCourseDescription($id) | |
{ | |
return $this->course->findCourseDescription($id); | |
} | |
public function getByAlias($slug) | |
{ | |
return $this->course->findByAlias($slug); | |
} | |
public function getUserCourses($user_id) | |
{ | |
return $this->user->findUserCourses($user_id); | |
} | |
public function findCourses($options) | |
{ | |
$options = $this->cleanSearchFields($options); | |
if (is_int($options['department'])) { | |
if (is_int($options['is_lab'])) { | |
if (!is_null($options['search'])) { | |
return $this->course->searchCourse(array('department_id' => $options['department'], 'is_lab' => $options['is_lab'], 'search' => $options['search'])); | |
} else { | |
return $this->course->searchCourse(array('department_id' => $options['department'], 'is_lab' => $options['is_lab'])); | |
} | |
} else { | |
if (!is_null($options['search'])) { | |
return $this->course->searchCourse(array('department_id' => $options['department'], 'search' => $options['search'])); | |
} else { | |
return $this->course->searchCourse(array('department_id' => $options['department'])); | |
} | |
} | |
} else { | |
if (is_int($options['is_lab'])) { | |
if (!is_null($options['search'])) { | |
return $this->course->searchCourse(array('is_lab' => $options['is_lab'], 'search' => $options['search'])); | |
} else { | |
return $this->course->searchCourse(array('is_lab' => $options['is_lab'])); | |
} | |
} else { | |
if (!is_null($options['search'])) { | |
return $this->course->searchCourse(array('search' => $options['search'])); | |
} else { | |
return $this->course->findAll(); | |
} | |
} | |
} | |
} | |
private function cleanSearchFields($options) | |
{ | |
if (strtolower($options['department']) == "all" || $options['department'] == "0") { | |
//leave as string | |
} else { | |
//convert to int | |
$options['department'] = (int)$options['department']; | |
} | |
if (strtolower($options['is_lab']) == "all") { | |
//leave as string | |
} else { | |
$options['is_lab'] = (int)$options['is_lab']; | |
} | |
return $options; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Entities; | |
use Illuminate\Database\Eloquent\Model; | |
use Pta\Formbuilder\Traits\ModelSchemaBuilderTrait; | |
use Pta\Formbuilder\Lib\Fields\SelectField; | |
use Wc\Lms\Src\Entities\Department; | |
class Course extends Model | |
{ | |
use ModelSchemaBuilderTrait; | |
protected $table = 'courses'; | |
protected $guarded = [ | |
'id', | |
]; | |
protected $skipFields = [ | |
'start_date', | |
'end_date', | |
'created_at', | |
'deleted_at', | |
'new', | |
'active', | |
'updated_at', | |
'rolling_enrollment', | |
'enrollment_type' | |
]; | |
/** | |
* Create Relationship with CreditTypes | |
* @return CreditType many-to-many relationship | |
*/ | |
public function credits() | |
{ | |
return $this->belongsToMany('Modules\Lms\Entities\CreditType', | |
'course_credit_type')->withPivot('key', 'value', 'course_code', 'course_price', 'transfer', | |
'transfer_level', 'proctored', 'prerequisite', 'active', 'deleted_at')->orderBy('weight', 'asc'); | |
} | |
/** | |
* Links Relationship | |
* @return link Many-to-Many relationship | |
*/ | |
public function links() | |
{ | |
return $this->belongsToMany('Modules\Lms\Src\Entities\Link')->orderBy('weight', 'asc'); | |
} | |
/** | |
* Syllabus Relationship | |
* @return Syllabus one-to-one relationship | |
*/ | |
public function syllabus() | |
{ | |
return $this->hasOne('Modules\Lms\Src\Entities\Syllabus'); | |
} | |
/** | |
* CourseMenu Relationship | |
* @return CourseMenu one-to-one relationship | |
*/ | |
public function menu() | |
{ | |
return $this->hasOne('Modules\Lms\Src\Entities\CourseMenu'); | |
} | |
/** | |
* @return Department Relationship | |
*/ | |
public function department() | |
{ | |
return $this->belongsTo('Modules\Lms\Src\Entities\Department'); | |
} | |
/** | |
* @return School Relationship | |
*/ | |
public function schools() | |
{ | |
return $this->belongsToMany('Modules\Lms\Src\Entities\School', | |
'school_course_credit_type')->withPivot('credit_type_id'); | |
} | |
/** | |
* @return Section Relationship | |
*/ | |
public function courseSections() | |
{ | |
return $this->hasMany('Modules\Lms\Src\Entities\Section'); | |
} | |
public function department_id() | |
{ | |
$department = new Department; | |
return new SelectField($department, 'name', function() use ($department) { | |
if(is_subclass_of($department, 'Illuminate\Database\Eloquent\Model')){ | |
return $department->where('active',1)->get(array('id','name')); | |
} | |
return false; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Repositories\Course; | |
use Illuminate\Container\Container; | |
use Cartalyst\Support\Traits; | |
use Symfony\Component\Finder\Finder; | |
class CourseRepository implements CourseRepositoryInterface | |
{ | |
use Traits\ContainerTrait, Traits\EventTrait, Traits\RepositoryTrait, Traits\ValidatorTrait; | |
/** | |
* The Data handler. | |
* | |
* @var \Modules\Lms\Handlers\Course\CourseDataHandlerInterface | |
*/ | |
protected $data; | |
/** | |
* The Eloquent learningmanagementsystem model. | |
* | |
* @var string | |
*/ | |
protected $model; | |
/** | |
* Constructor. | |
* | |
* @param \Illuminate\Container\Container $app | |
* @return void | |
*/ | |
public function __construct(Container $app) | |
{ | |
$this->setContainer($app); | |
$this->setDispatcher($app['events']); | |
$this->data = $app['wc.lms.course.handler.data']; | |
$this->setValidator($app['wc.lms.course.validator']); | |
$this->setModel(get_class($app['Wc\Lms\Entities\Course'])); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function grid() | |
{ | |
return $this | |
->createModel(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function findAll($active = null) | |
{ | |
return $this->container['cache']->rememberForever('wc.lms.course.all', | |
function () use ($active) { | |
if ($active) { | |
return $this->createModel()->where('active', '=', 1)->get(); | |
} | |
return $this->createModel()->get(); | |
}); | |
} | |
public function findByDepartment($department) | |
{ | |
return $this->container['cache']->rememberForever('wc.lms.course.department.' . $department, | |
function () use ($department) { | |
return $this->createModel()->where('department_id', '=', $department)->where('active', '=', 1)->get(); | |
}); | |
} | |
public function findByHighlight() | |
{ | |
return $this->container['cache']->rememberForever('wc.lms.course.highlights', | |
function () { | |
return $this->createModel()->where('highlight', '=', '1')->where('active', '=', 1)->get(); | |
}); | |
} | |
public function findCourseDescription($slug) | |
{ | |
return $this->container['cache']->rememberForever('wc.lms.course.description.' . $slug, | |
function () use ($slug) { | |
return $this->createModel()->with('courseSections')->find($this->findByAlias($slug)->id); | |
}); | |
} | |
public function findByAlias($slug) | |
{ | |
return $this->createModel()->where('alias', '=', $slug)->first(); | |
} | |
public function searchCourse($options) | |
{ | |
if (isset($options['department_id'])) { | |
if (isset($options['is_lab'])) { | |
if (isset($options['search'])) { | |
return $this->createModel()->where('department_id', '=', $options['department_id'])->where('is_lab', | |
'=', $options['is_lab'])->where('description', 'LIKE', | |
'%' . $options['search'] . '%')->where('active', '=', 1)->get(); | |
} else { | |
return $this->createModel()->where('department_id', '=', $options['department_id'])->where('is_lab', | |
'=', $options['is_lab'])->where('active', '=', 1)->get(); | |
} | |
} else { | |
if (isset($options['search'])) { | |
return $this->createModel()->where('department_id', '=', | |
$options['department_id'])->where('description', 'LIKE', | |
'%' . $options['search'] . '%')->where('active', '=', 1)->get(); | |
} else { | |
return $this->createModel()->where('department_id', '=', | |
$options['department_id'])->where('active', '=', 1)->get(); | |
} | |
} | |
} else { | |
if (isset($options['is_lab'])) { | |
if (isset($options['search'])) { | |
return $this->createModel()->where('is_lab', '=', $options['is_lab'])->where('description', 'LIKE', | |
'%' . $options['search'] . '%')->where('active', '=', 1)->get(); | |
} else { | |
return $this->createModel()->where('is_lab', '=', $options['is_lab'])->where('active', '=', | |
1)->get(); | |
} | |
} else { | |
if (isset($options['search'])) { | |
return $this->createModel()->where('description', 'LIKE', | |
'%' . $options['search'] . '%')->where('active', '=', 1)->get(); | |
} else { | |
return $this->createModel()->where('active', '=', 1)->get(); | |
} | |
} | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function find($id) | |
{ | |
return $this->container['cache']->rememberForever('wc.lms.course.' . $id, | |
function () use ($id) { | |
return $this->createModel()->find($id); | |
}); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function validForCreation(array $input) | |
{ | |
return $this->validator->on('create')->validate($input); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function validForUpdate($id, array $input) | |
{ | |
return $this->validator->on('update')->validate($input); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function store($id, array $input) | |
{ | |
return !$id ? $this->create($input) : $this->update($id, $input); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function create(array $input) | |
{ | |
// Create a new courses | |
$course = $this->createModel(); | |
// Fire the 'wc.lms.courses.creating' event | |
if ($this->fireEvent('wc.lms.course.creating', [$input]) === false) { | |
return false; | |
} | |
// Prepare the submitted data | |
$data = $this->data->prepare($input); | |
// Validate the submitted data | |
$messages = $this->validForCreation($data); | |
// Check if the validation returned any errors | |
if ($messages->isEmpty()) { | |
// Save the courses | |
$course->fill($data)->save(); | |
// Fire the 'wc.lms.courses.created' event | |
$this->fireEvent('wc.lms.course.created', [$course]); | |
} | |
return [$messages, $course]; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function update($id, array $input) | |
{ | |
// Get the courses object | |
$courses = $this->find($id); | |
// Fire the 'wc.lms.courses.updating' event | |
if ($this->fireEvent('wc.lms.course.updating', [$courses, $input]) === false | |
) { | |
return false; | |
} | |
// Prepare the submitted data | |
$data = $this->data->prepare($input); | |
// Validate the submitted data | |
$messages = $this->validForUpdate($courses, $data); | |
// Check if the validation returned any errors | |
if ($messages->isEmpty()) { | |
// Update the courses | |
$courses->fill($data)->save(); | |
// Fire the 'wc.lms.courses.updated' event | |
$this->fireEvent('wc.lms.course.updated', [$courses]); | |
} | |
return [$messages, $courses]; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function delete($id) | |
{ | |
// Check if the courses exists | |
if ($courses = $this->find($id)) { | |
// Fire the 'wc.lms.courses.deleted' event | |
$this->fireEvent('wc.lms.course.deleted', [$courses]); | |
// Delete the courses entry | |
$courses->delete(); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function enable($id) | |
{ | |
$this->validator->bypass(); | |
return $this->update($id, ['enabled' => true]); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function disable($id) | |
{ | |
$this->validator->bypass(); | |
return $this->update($id, ['enabled' => false]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Wc\Lms\Providers; | |
use Cartalyst\Support\ServiceProvider; | |
use Illuminate\Foundation\AliasLoader; | |
use Wc\Lms\Facades\Course\CourseLibrary; | |
class CourseServiceProvider extends ServiceProvider | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function boot() | |
{ | |
// Subscribe the registered event handler | |
$this->app['events']->subscribe('wc.lms.course.handler.event'); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function register() | |
{ | |
// Register the repository | |
$this->bindIf('wc.lms.course', 'Wc\Lms\Repositories\Course\CourseRepository'); | |
$this->app->bind('Wc\Lms\Repositories\Course\CourseRepositoryInterface', 'Wc\Lms\Repositories\Course\CourseRepository'); | |
// Register the event handler | |
$this->bindIf('wc.lms.course.handler.event', 'Wc\Lms\Http\Handlers\Course\CourseEventHandler'); | |
$this->registerTranslations(); | |
$this->registerFacade(); | |
} | |
/** | |
* Register translations. | |
* | |
* @return void | |
*/ | |
public function registerTranslations() | |
{ | |
$this->loadTranslationsFrom(realpath(__DIR__ . '/../../resources/lang/course'), 'wc/lms/course'); | |
} | |
public function registerFacade() | |
{ | |
$this->app['Course'] = $this->app->share(function ($app) { | |
return new CourseLibrary(); | |
}); | |
$loader = AliasLoader::getInstance(); | |
$loader->alias('Course', 'Wc\Lms\Facades\Course\CourseFacade'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment