Skip to content

Commit

Permalink
Adding UFIXIT functionality to server. Most UFIXIT work will be done …
Browse files Browse the repository at this point in the history
…on front-end, but this will replace the content and send it to the LMS API.
  • Loading branch information
webchuckweb committed Oct 2, 2020
1 parent 486e395 commit d4a5359
Show file tree
Hide file tree
Showing 23 changed files with 617 additions and 196 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ yarn-error.log
/.vscode/
/.idea/
.idea/*

###> Temp folder ###
/tmp/
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ext-ctype": "*",
"ext-iconv": "*",
"cidilabs/phpally": "dev-master",
"deruli/html-minifier": "^0.5.1",
"firebase/php-jwt": "^5.2",
"imsglobal/lti-1p3-tool": "dev-master",
"knplabs/knp-snappy-bundle": "^1.7",
Expand Down
63 changes: 62 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ security:
- App\Security\TokenAuthenticator
entry_point: App\Security\TokenAuthenticator

test:
pattern: ^/test
provider: users
guard:
authenticators:
- App\Security\SessionAuthenticator
- App\Security\TokenAuthenticator
entry_point: App\Security\TokenAuthenticator

# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
Expand Down
18 changes: 2 additions & 16 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
<?php


namespace App\Controller;


use App\Entity\Course;
use App\Entity\Institution;
use App\Entity\Report;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class ApiController extends AbstractController
{
public function getInstitutionId() : int {
$user = $this->getUser();
$institution = $user->getInstitution();
return $institution->getId();
}

public function userHasCourseAccess(int $courseId) : bool {
// Get Course
$repository = $this->getDoctrine()->getRepository(Course::class);
$course = $repository->find($courseId);

public function userHasCourseAccess(Course $course) : bool {
// Check if course belongs to user's institution
$userInstitutionId = $this->getInstitutionId();
$userInstitutionId = $this->getUser()->getInstitution()->getId();
$resourceInstitutionId = $course->getInstitution()->getId();
return $resourceInstitutionId === $userInstitutionId;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/CoursesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getAllCourses(
$institution = $user->getInstitution();

// Get Courses
$courses = $institution->getCourses()->toArray();
$courses = $institution->getCourses();

// Construct API Response
$apiResponse = new ApiResponse();
Expand Down Expand Up @@ -80,7 +80,7 @@ public function getCourse(
$apiResponse->setData($course);

} catch(\Exception $e) {
$apiResponse->setData($e->getMessage());
$apiResponse->addError($e->getMessage());
}

// Construct Response
Expand Down
17 changes: 0 additions & 17 deletions src/Controller/DefaultController.php

This file was deleted.

64 changes: 38 additions & 26 deletions src/Controller/IssuesController.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
<?php


namespace App\Controller;


use App\Entity\Course;
use App\Entity\Issue;
use App\Entity\Report;
use App\Request\IssueRequest;
use App\Response\ApiResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Services\PhpAllyService;
use App\Services\UfixitService;
use App\Services\UtilityService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;


class IssuesController extends ApiController
{
/**
* UFIXIT endpoint. Takes an array of updates for future changes in the course.
* @Route("/api/courses/{courseId}/issues/{issueId}", methods={"PUT"}, name="put_issue")
* @param $courseId
* @param $issueId
*
* @Route("/api/issues/{issue}/fix", methods={"POST","GET"}, name="fix_issue")
* @param Issue $issue
* @return \Symfony\Component\HttpFoundation\Response
*/
public function fixIssue(Request $request, $courseId, $issueId) {
public function fixIssue(Request $request, UfixitService $ufixit, PhpAllyService $phpAlly, UtilityService $util, Issue $issue) {
$apiResponse = new ApiResponse();
$user = $this->getUser();

try {
// Check if user has access to course
if(!$this->userHasCourseAccess($courseId)) {
throw new \Exception("You do not have permission to access the specified course.");
$course = $issue->getContentItem()->getCourse();
if(!$this->userHasCourseAccess($course)) {
throw new \Exception("You do not have permission to access this issue.");
}

// Get Request Info
$requestBody = json_decode($request->getContent( ), true);
$issueRequest = new IssueRequest($issueId, $requestBody["scanRuleId"], $requestBody["data"]);
// Get fixed content
//$fixedHtml = $request->request->get('html');
// for testing
$fixedHtml = $request->query->get('html');

// Get Issue
$repository = $this->getDoctrine()->getRepository(Issue::class);
$issue = $repository->find($issueId);
// Run fixed content through PhpAlly to validate it
$report = $phpAlly->scanHtml($fixedHtml);
if ($issues = $report->getIssues()) {
$apiResponse->addData('issues', $issues);
}
if ($errors = $report->getErrors()) {
$apiResponse->addData('errors', $errors);
}

// Check if Issue exists
if(is_null($issue)) {
throw new \Exception(sprintf("Issue with ID %s could not be found", $issueId));
if (!empty($issues) || !empty($errors)) {
throw new \Exception('Updated content does not pass all UDOIT tests.');
}

// TODO: Make fix here
// Save content to LMS
$ufixit->saveContentToLms($issue, $fixedHtml);

// Update issue
$issue->setStatus(true);
$issue->setFixedBy($user);
$issue->setFixedOn($util->getCurrentTime());
$this->getDoctrine()->getManager()->flush();

$apiResponse->setData($issueRequest);
// Create response
$apiResponse->addMessage('Your fix has been saved.');
$apiResponse->setData(['issue' => $issue]);
}
catch(\Exception $e) {
$apiResponse->setData($e->getMessage());
$apiResponse->addError($e->getMessage());
}

// Format Response JSON
Expand Down
Loading

0 comments on commit d4a5359

Please sign in to comment.