Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace File References #949

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions assets/js/Components/FilesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ class FilesModal extends React.Component {
return
}

const userConfirmed = window.confirm(this.props.t('msg.confirmation'));

if (!userConfirmed) {
return;
}

let api = new Api(this.props.settings)
api.postFile(activeFile, this.state.replaceFileObj)
.then((responsStr) => responsStr.json())
Expand Down
257 changes: 248 additions & 9 deletions src/Lms/Canvas/CanvasApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function apiPost($url, $options, $sendAuthorized = true)
}

// Posts a file to Canvas
public function apiFilePost(string $url, array $options, string $filepath, string $newFileName): LmsResponse
public function apiFilePost(string $url, array $options, string $filepath, string $newFileName, $changeReferences = null) : LmsResponse
{
$fileResponse = $this->apiGet($url);
$file = $fileResponse->getContent();
Expand All @@ -122,8 +122,6 @@ public function apiFilePost(string $url, array $options, string $filepath, strin
$endpointResponse = $this->apiPost($options['postUrl'], ['query' => $endpointOptions], true);
$endpointContent = $endpointResponse->getContent();

$this->apiDelete($url);

// TODO: handle failed call

$formFields = $endpointContent['upload_params'];
Expand All @@ -135,6 +133,17 @@ public function apiFilePost(string $url, array $options, string $filepath, strin
'body' => $formData->bodyToIterable(),
], false);

$fileResponseContent = $fileResponse->getContent();

if($changeReferences) {
$this->changeLinksInSyllabus($changeReferences['course_id'], $file['id'], $fileResponseContent['url'], $newFileName);
$this->changeModuleItemInstances($changeReferences['course_id'], $changeReferences['modules'], $newFileName, $fileResponseContent['id']);
$this->changeLinksInPages($changeReferences['course_id'], $file['id'], $changeReferences['pages'], $fileResponseContent['url'], $newFileName);
$this->changeLinksInAssignments($changeReferences['course_id'], $file['id'], $changeReferences['assignments'], $fileResponseContent['url'], $newFileName);
$this->changeLinksInQuizzes($changeReferences['course_id'], $file['id'], $changeReferences['quizzes'], $fileResponseContent['url'], $newFileName);
$this->changeLinksInQuizQuestions($changeReferences['course_id'], $file['id'], $changeReferences['quizQuestions'], $fileResponseContent['url'], $newFileName);
}

return $fileResponse;
}

Expand Down Expand Up @@ -164,12 +173,8 @@ public function apiPut($url, $options)
public function apiDelete($url) {
$lmsResponse = new LmsResponse();

if (strpos($url, 'https://') === false) {
$pattern = '/\/files\/\d+/';

preg_match($pattern, $url, $matches);

$url = "https://" . $this->baseUrl . "/api/v1/" . $matches[0];
if (strpos($url, 'https://') === false) {
$url = "https://{$this->baseUrl}/api/v1/{$url}";
}

$response = $this->httpClient->request('DELETE', $url);
Expand All @@ -189,4 +194,238 @@ public function apiDelete($url) {

}

public function getCourse($courseId, $includeSyllabus = false)
{
if ($includeSyllabus) {
$url = "courses/{$courseId}?include[]=syllabus_body";
}
else {
$url = "courses/{$courseId}";
}
$response = $this->apiGet($url);
return $response->getContent();
}

public function listModules($courseId)
{
$url = "courses/{$courseId}/modules";
$response = $this->apiGet($url);
return $response->getContent();
}

public function listModuleItems($courseId, $moduleId)
{
$url = "courses/{$courseId}/modules/{$moduleId}/items";
$response = $this->apiGet($url);
return $response->getContent();
}

public function deleteModuleItem($courseId, $moduleId, $itemId)
{
$url = "courses/{$courseId}/modules/{$moduleId}/items/{$itemId}";
$response = $this->apiDelete($url);
return $response->getContent();
}

public function listPages($courseId)
{
$url = "courses/{$courseId}/pages";
$response = $this->apiGet($url . '?include[]=body');

$responseWithPageBody = [];

foreach ($response->getContent() as $content) {
$page = $this->showPage($courseId, $content['page_id']);
$content['body'] = $page['body'];
$responseWithPageBody[] = $content;
}

return $responseWithPageBody;
}

public function showPage($courseId, $pageId)
{
$url = "courses/{$courseId}/pages/{$pageId}";
$response = $this->apiGet($url . '?include[]=body');
return $response->getContent();
}

public function listAssignments($courseId)
{
$url = "courses/{$courseId}/assignments";
$response = $this->apiGet($url);
return $response->getContent();
}

public function listQuizzes($courseId)
{
$url = "courses/{$courseId}/quizzes";
$response = $this->apiGet($url);
return $response->getContent();
}

public function listQuizQuestions($courseId, $quizId)
{
$url = "courses/{$courseId}/quizzes/{$quizId}/questions";
$response = $this->apiGet($url);
return $response->getContent();
}

public function updateCourse($courseId, $options)
{
$url = "courses/{$courseId}";
$response = $this->apiPut($url, $options);
return $response->getContent();
}

public function uploadModuleItem($courseId, $moduleId, $options)
{
$url = "courses/{$courseId}/modules/{$moduleId}/items";
$response = $this->apiPost($url, $options);
return $response->getContent();
}

public function updatePage($courseId, $pageId, $options)
{
$url = "courses/{$courseId}/pages/{$pageId}";
$response = $this->apiPut($url, $options);
return $response->getContent();
}

public function updateAssignment($courseId, $assignmentId, $options)
{
$url = "courses/{$courseId}/assignments/{$assignmentId}";
$response = $this->apiPut($url, $options);
return $response->getContent();
}

public function updateQuiz($courseId, $quizId, $options)
{
$url = "courses/{$courseId}/quizzes/{$quizId}";
$response = $this->apiPut($url, $options);
return $response->getContent();
}

public function updateQuizQuestion($courseId, $quizId, $questionId, $options)
{
$url = "courses/{$courseId}/quizzes/{$quizId}/questions/{$questionId}";
$response = $this->apiPut($url, $options);
return $response->getContent();
}

/**********************
* PROTECTED FUNCTIONS
**********************/


protected function changeLinksInSyllabus($courseId, $oldFileId, $newUrl, $newFileName)
{
$course = $this->getCourse($courseId, true);
$dom = new \DOMDocument();

$dom->loadHTML($course['syllabus_body']);
$anchors = $dom->getElementsByTagName('a');
$newSyllabus = $this->changeAnchorTags($dom, $oldFileId, $newUrl, $newFileName);
if ($newSyllabus != $course['syllabus_body']) {
$this->updateCourse($courseId, ['json' => ['course' => ['syllabus_body' => $newSyllabus]]]);
}
}

protected function changeModuleItemInstances($courseId, $moduleInstances, $newFileName, $newFileId) {
foreach ($moduleInstances as $moduleInstance) {
$this->uploadModuleItem($courseId, $moduleInstance, [
'json' => [
'module_item' => [
'title' => $newFileName,
'type' => 'File',
'content_id' => $newFileId,
],
],
]);
}
}

protected function changeLinksInPages($courseId, $oldFileId, $pages, $newUrl, $newFileName) {
foreach ($pages as $page) {
$dom = new \DOMDocument();
$dom->loadHTML($page['body']);
$newPage = $this->changeAnchorTags($dom, $oldFileId, $newUrl, $newFileName);
if ($newPage != $page['body']) {
$this->updatePage($courseId, $page['page_id'], [
'json' => [
'wiki_page' => [
'body' => $newPage,
],
],
]);
}
}
}

protected function changeLinksInAssignments($courseId, $oldFileId, $assignments, $newUrl, $newFileName) {
foreach ($assignments as $assignment) {
$dom = new \DOMDocument();
$dom->loadHTML($assignment['description']);
$newAssignment = $this->changeAnchorTags($dom, $oldFileId, $newUrl, $newFileName);
if ($newAssignment != $assignment['description']) {
$this->updateAssignment($courseId, $assignment['id'], [
'json' => [
'assignment' => [
'description' => $newAssignment,
],
],
]);
}
}
}

protected function changeLinksInQuizzes($courseId, $oldFileId, $quizzes, $newUrl, $newFileName) {
foreach ($quizzes as $quiz) {
$dom = new \DOMDocument();
$dom->loadHTML($quiz['description']);
$newQuiz = $this->changeAnchorTags($dom, $oldFileId, $newUrl, $newFileName);
if ($newQuiz != $quiz['description']) {
$this->updateQuiz($courseId, $quiz['id'], [
'json' => [
'quiz' => [
'description' => $newQuiz,
'notify_of_update' => $quiz['notify_of_update'],
],
],
]);
}
}
}

public function changeLinksInQuizQuestions($courseId, $oldFileId, $replaceQuizQuestions, $newUrl, $newFileName) {
foreach ($replaceQuizQuestions as $quizQuestion) {
$dom = new \DOMDocument();
$dom->loadHTML($quizQuestion['question_text']);
$newQuestion = $this->changeAnchorTags($dom, $oldFileId, $newUrl, $newFileName);
if ($newQuestion != $quizQuestion['question_text']) {
$this->updateQuizQuestion($courseId, $quizQuestion['quiz_id'], $quizQuestion['id'], [
'json' => [
'question' => [
'question_text' => $newQuestion,
],
],
]);
}
}
}

protected function changeAnchorTags($dom, $fileId, $newUrl, $newFileName) {
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
preg_match('/files\/(\d+)/', $anchor->getAttribute('href'), $matches);
if (isset($matches[1]) && $matches[1] == $fileId) {
$anchor->setAttribute('href', $newUrl);
$anchor->setAttribute('title', $newFileName);
$anchor->nodeValue = $newFileName;
}
}

return $dom->saveHTML();
}

}
Loading
Loading