LTI: Remove old services endpoints

pull/3065/head
Angel Fernando Quiroz Campos 6 years ago
parent 3846609c23
commit 3853fa7111
  1. 13
      plugin/ims_lti/configure.php
  2. 91
      plugin/ims_lti/gradebook/service/lineitem.php
  3. 134
      plugin/ims_lti/gradebook/service/lineitems.php
  4. 25
      plugin/ims_lti/gradebook/service/result.php
  5. 4
      plugin/ims_lti/gradebook/service/score.php
  6. 212
      plugin/ims_lti/src/Service/LtiAssignmentGradesService.php
  7. 2
      plugin/ims_lti/src/Service/Resource/LtiLineItemsResource.php

@ -105,13 +105,12 @@ switch ($action) {
$advServices = $tool->getAdvantageServices();
if (LtiAssignmentGradesService::AGS_NONE !== $advServices['ags']) {
$agService = new LtiAssignmentGradesService($tool);
$agService->createLineItem(
[
'label' => $tool->getName(),
'scoreMaximum' => 100,
],
$tool
$lineItemResource = new LtiLineItemsResource(
$tool->getId(),
$course->getId()
);
$lineItemResource->createLineItem(
['label' => $tool->getName(), 'scoreMaximum' => 100]
);
Display::addFlash(

@ -1,91 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
use Chamilo\PluginBundle\Entity\ImsLti\LineItem;
require_once __DIR__.'/../../../../main/inc/global.inc.php';
$httpAccept = $_SERVER['HTTP_ACCEPT'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestUri = $_SERVER['REQUEST_URI'];
$phpInput = file_get_contents('php://input');
$webPluginPath = api_get_path(WEB_PLUGIN_PATH);
$em = Database::getManager();
$ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
/** @var Course|null $course */
$course = isset($_REQUEST['c']) ? api_get_course_entity($_REQUEST['c']) : null;
/** @var ImsLtiTool|null $tool */
$tool = isset($_REQUEST['t']) ? $ltiToolRepo->find($_REQUEST['t']) : null;
/** @var LineItem|null $lineItem */
$lineItem = isset($_REQUEST['l']) ? $em->find('ChamiloPluginBundle:ImsLti\LineItem', $_REQUEST['l']) : null;
$baseTool = null;
if ($tool) {
$baseTool = $tool->getParent() ?: $tool;
}
$responseHeaders = [];
$responseData = [];
$jsonOptions = 0;
$service = new LtiAssignmentGradesService($tool);
try {
LtiAssignmentGradesService::validateLineItemRequest($course, $tool, $lineItem, $httpAccept);
switch ($requestMethod) {
case 'PUT':
$requestData = json_decode($phpInput, true);
$lineItem = $service->updateLineItem($lineItem, $requestData);
$requestData['id'] = $webPluginPath."ims_lti/gradebook/service/lineitem.php?"
.http_build_query(
['c' => $course->getId(), 'l' => $lineItem->getEvaluation()->getId(), 't' => $tool->getId()]
);
$requestData['scoreMaximum'] = $lineItem->getEvaluation()->getMax();
$responseData = $requestData;
$jsonOptions = JSON_UNESCAPED_SLASHES;
$responseHeaders['Content-Type'] = LtiAssignmentGradesService::TYPE_LINE_ITEM;
break;
case 'DELETE':
$service->deleteLineItem($lineItem);
header("HTTP/1.0 204 No content");
break;
case 'GET':
default:
$responseData = $service->getLineItemAsArray($lineItem);
$responseHeaders['Content-Type'] = LtiAssignmentGradesService::TYPE_LINE_ITEM;
break;
}
} catch (Exception $exception) {
header("HTTP/1.0 {$exception->getCode()} {$exception->getMessage()}");
$responseHeaders['Content-Type'] = 'application/json';
$responseData = [
'status' => $exception->getCode(),
'request' => [
'method' => $requestMethod,
'url' => $requestUri,
'accept' => $httpAccept,
],
];
}
foreach ($responseHeaders as $headerName => $headerValue) {
header("$headerName: $headerValue");
}
if (!empty($responseData)) {
echo json_encode($responseData, $jsonOptions);
}

@ -1,134 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
use Chamilo\PluginBundle\Entity\ImsLti\LineItem;
require_once __DIR__.'/../../../../main/inc/global.inc.php';
$httpAccept = $_SERVER['HTTP_ACCEPT'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestUri = $_SERVER['REQUEST_URI'];
$phpInput = file_get_contents('php://input');
$webPluginPath = api_get_path(WEB_PLUGIN_PATH);
$em = Database::getManager();
$ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
$course = isset($_REQUEST['c']) ? api_get_course_entity($_REQUEST['c']) : null;
/** @var ImsLtiTool $tool */
$tool = isset($_REQUEST['t']) ? $ltiToolRepo->find($_REQUEST['t']) : null;
$baseTool = null;
if ($tool) {
$baseTool = $tool->getParent() ?: $tool;
}
$responseHeaders = [];
$responseData = [];
$jsonOptions = 0;
$service = new LtiAssignmentGradesService($tool);
try {
LtiAssignmentGradesService::validateLineItemsRequest($course, $tool, $httpAccept);
switch ($requestMethod) {
case 'POST':
$requestData = json_decode($phpInput, true);
$lineItem = $service->createLineItem($requestData);
$requestData['id'] = $webPluginPath."ims_lti/gradebook/service/lineitem.php?"
.http_build_query(
['c' => $course->getId(), 'l' => $lineItem->getEvaluation()->getId(), 't' => $tool->getId()]
);
$responseData = $requestData;
$jsonOptions = JSON_UNESCAPED_SLASHES;
$responseHeaders['Content-Type'] = LtiAssignmentGradesService::TYPE_LINE_ITEM;
header("HTTP/1.0 201 Created");
break;
case 'GET':
default:
$resourceLinkId = isset($_GET['resource_link_id']) ? $_GET['resource_link_id'] : '';
$resourceId = isset($_GET['resource_id']) ? $_GET['resource_id'] : '';
$tag = isset($_GET['tag']) ? $_GET['tag'] : '';
$page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$queryData = [
'c' => $course->getId(),
't' => $tool->getId(),
'resource_link_id' => $resourceLinkId,
'resource_id' => $resourceId,
'tag' => $tag,
'limit' => $limit,
'page' => $page,
];
$lineItems = $tool->getLineItems($resourceLinkId, $resourceId, $tag, $limit, $page);
$headerLinks = [];
if ($limit > 0) {
$headerLinks['first'] = 1;
if ($page > 1) {
$headerLinks['prev'] = $page - 1;
}
if ($page < $lineItems->count() / $limit) {
$headerLinks['next'] = $page + 1;
}
$headerLinks['last'] = $lineItems->count() / $limit;
array_walk(
$headerLinks,
function (&$linkPage, $rel) use ($queryData, $webPluginPath) {
$queryData['page'] = $rel;
$url = "{$webPluginPath}ims_lti/gradebook/service/lineitems.php?"
.http_build_query($queryData);
$linkPage = '<'.$url.'>; rel="'.$rel.'"';
}
);
$responseHeaders['Link'] = implode(', ', $headerLinks);
}
$responseData = [];
/** @var LineItem $lineItem */
foreach ($lineItems as $lineItem) {
$responseData[] = $service->getLineItemAsArray($lineItem);
}
$responseHeaders['Content-Type'] = LtiAssignmentGradesService::TYPE_LINE_ITEM_CONTAINER;
break;
}
} catch (Exception $exception) {
header("HTTP/1.0 {$exception->getCode()} {$exception->getMessage()}");
$responseHeaders['Content-Type'] = 'application/json';
$responseData = [
'status' => $exception->getCode(),
'request' => [
'method' => $requestMethod,
'url' => $requestUri,
'accept' => $httpAccept,
],
];
}
foreach ($responseHeaders as $headerName => $headerValue) {
header("$headerName: $headerValue");
}
echo json_encode($responseData, $jsonOptions);

@ -1,25 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
use Chamilo\PluginBundle\Entity\ImsLti\LineItem;
require_once __DIR__.'/../../../../main/inc/global.inc.php';
var_dump($_SERVER);
$httpAccept = $_SERVER['HTTP_ACCEPT'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestUri = $_SERVER['REQUEST_URI'];
$phpInput = file_get_contents('php://input');
$webPluginPath = api_get_path(WEB_PLUGIN_PATH);
$em = Database::getManager();
$ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
$course = isset($_REQUEST['c']) ? api_get_course_entity($_REQUEST['c']) : null;
/** @var ImsLtiTool $tool */
$tool = isset($_REQUEST['t']) ? $ltiToolRepo->find($_REQUEST['t']) : null;
$baseTool = null;

@ -1,4 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/../../../../main/inc/global.inc.php';

@ -23,65 +23,6 @@ class LtiAssignmentGradesService extends LtiAdvantageService
const TYPE_LINE_ITEM_CONTAINER = 'application/vnd.ims.lis.v2.lineitemcontainer+json';
const TYPE_LINE_ITEM = 'application/vnd.ims.lis.v2.lineitem+json';
/**
* @param Course $course
* @param ImsLtiTool $tool
* @param string $httpAccept
*
* @throws Exception
*/
public static function validateLineItemsRequest(Course $course, ImsLtiTool $tool, $httpAccept)
{
if (!$course) {
throw new Exception('Course not found.', 404);
}
if (!$tool) {
throw new Exception('Tool not found.', 404);
}
if ($tool->getCourse()->getId() != $course->getId()) {
throw new Exception("Tool not allowed in this context.", 403);
}
$isMediaTypeAllowed = in_array($httpAccept, [self::TYPE_LINE_ITEM_CONTAINER, self::TYPE_LINE_ITEM]);
if (!$isMediaTypeAllowed) {
throw new Exception('Media type not allowed.', 403);
}
$parentTool = $tool->getParent();
if ($parentTool) {
$services = $parentTool->getAdvantageServices();
if ($services['ags'] === LtiAssignmentGradesService::AGS_NONE) {
throw new Exception('Service not allowed for tool.', 403);
}
}
}
/**
* @param Course $course
* @param ImsLtiTool $tool
* @param LineItem $lineItem
* @param string $httpAccept
*
* @throws Exception
*/
public static function validateLineItemRequest(Course $course, ImsLtiTool $tool, LineItem $lineItem, $httpAccept)
{
self::validateLineItemsRequest($course, $tool, $httpAccept);
if (!$lineItem) {
throw new Exception('Line item not found.', 404);
}
if ($lineItem->getTool()->getId() !== $tool->getId()) {
throw new Exception("Line item not allowed in this context.", 403);
}
}
/**
* @return array
*/
@ -100,159 +41,6 @@ class LtiAssignmentGradesService extends LtiAdvantageService
return $scopes;
}
/**
* @param array $lineItemData
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*
* @return LineItem
*/
public function createLineItem(array $lineItemData)
{
$categories = Category::load(null, null, $this->tool->getCourse()->getCode());
$gradebookCategory = $categories[0];
$weight = $gradebookCategory->getRemainingWeight();
$em = Database::getManager();
$userId = 1;
$evaluation = new Evaluation();
$evaluation->set_user_id($userId);
$evaluation->set_category_id($gradebookCategory->get_id());
$evaluation->set_course_code($this->tool->getCourse()->getCode());
$evaluation->set_name($lineItemData['label']);
$evaluation->set_description(null);
$evaluation->set_weight($weight);
$evaluation->set_max($lineItemData['scoreMaximum']);
$evaluation->set_visible(1);
$evaluation->add();
$gradebookEvaluation = $em->find('ChamiloCoreBundle:GradebookEvaluation', $evaluation->get_id());
$lineItem = new LineItem();
$lineItem
->setTool($this->tool)
->setEvaluation($gradebookEvaluation)
->setResourceId(!empty($lineItemData['resourceId']) ? $lineItemData['resourceId'] : null)
->setTag(!empty($lineItemData['tag']) ? $lineItemData['tag'] : null);
if (!empty($lineItemData['startDateTime'])) {
$lineItem->setStartDate(
new DateTime($lineItemData['startDateTime'])
);
}
if (!empty($lineItemData['endDateTime'])) {
$lineItem->setEndDate(
new DateTime($lineItemData['endDateTime'])
);
}
$em->persist($this->tool);
$em->persist($lineItem);
$em->flush();
return $lineItem;
}
/**
* @param LineItem $lineItem
* @param array $newLineItemData
*
* @return LineItem
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function updateLineItem(LineItem $lineItem, array $newLineItemData)
{
if (empty($newLineItemData) || empty($newLineItemData['label']) || empty($newLineItemData['scoreMaximum'])) {
throw new Exception('Missing data to update line item.', 400);
}
$lineItemEvaluation = $lineItem->getEvaluation();
$evaluations = Evaluation::load($lineItemEvaluation->getId());
/** @var Evaluation $evaluation */
$evaluation = $evaluations[0];
$lineItemEvaluation->setName($newLineItemData['label']);
if (isset($newLineItemData['resourceId'])) {
$lineItem->setResourceId($newLineItemData['resourceId']);
}
if (isset($newLineItemData['tag'])) {
$lineItem->setTag($newLineItemData['tag']);
}
if (!empty($newLineItemData['startDateTime'])) {
$lineItem->setStartDate(
new DateTime($newLineItemData['startDateTime'])
);
}
if (!empty($newLineItemData['endDateTime'])) {
$lineItem->setEndDate(
new DateTime($newLineItemData['endDateTime'])
);
}
if (!$evaluation->has_results()) {
$lineItemEvaluation->setMax(
$newLineItemData['scoreMaximum']
);
}
$em = Database::getManager();
$em->persist($lineItem);
$em->persist($lineItemEvaluation);
$em->flush();
return $lineItem;
}
/**
* @param LineItem $lineItem
*
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function deleteLineItem(LineItem $lineItem)
{
$lineItemEvaluation = $lineItem->getEvaluation();
$evaluations = Evaluation::load($lineItemEvaluation->getId());
/** @var Evaluation $evaluation */
$evaluation = $evaluations[0];
$em = Database::getManager();
$em->remove($lineItem);
$em->flush();
$evaluation->delete_with_results();
}
/**
* @param LineItem $lineItem
*
* @return array
*/
public function getLineItemAsArray(LineItem $lineItem)
{
$tool = $lineItem->getTool();
$data = $lineItem->toArray();
$data['id'] = api_get_path(WEB_PLUGIN_PATH)
."ims_lti/gradebook/service/lineitem.php?"
.http_build_query(
['c' => $tool->getCourse()->getId(), 'l' => $lineItem->getId(), 't' => $tool->getId()]
);
return $data;
}
/**
* @param Request $request
* @param JsonResponse $response

@ -109,7 +109,7 @@ class LtiLineItemsResource extends LtiAgsResource
*
* @return LineItem
*/
private function createLineItem(array $data)
public function createLineItem(array $data)
{
$caterories = Category::load(null, null, $this->course->getCode());
/** @var Category $gradebookCategory */

Loading…
Cancel
Save