Skip to content

Commit

Permalink
add replacement functionality for assignment descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
taheralfayad committed May 13, 2024
1 parent 7f4657d commit 35c319e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
33 changes: 32 additions & 1 deletion 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, $moduleInstances = null, $courseId = null, $replacePages = null) : LmsResponse
public function apiFilePost(string $url, array $options, string $filepath, string $newFileName, $moduleInstances = null, $courseId = null, $replacePages = null, $replaceAssignments = null) : LmsResponse
{
$fileResponse = $this->apiGet($url);
$file = $fileResponse->getContent();
Expand Down Expand Up @@ -188,6 +188,30 @@ public function apiFilePost(string $url, array $options, string $filepath, strin
}
}

if ($replaceAssignments) {
foreach ($replaceAssignments as $assignment) {
$dom = new \DOMDocument();
$dom->loadHTML($assignment['description']);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
preg_match('/files\/(\d+)/', $anchor->getAttribute('href'), $matches);
if (isset($matches[1]) && $matches[1] == $file['id']) {
$anchor->setAttribute('href', $fileResponseContent['url']);
$anchor->setAttribute('title', $newFileName);
$anchor->nodeValue = $newFileName;
}
$assignment['description'] = $dom->saveHTML();
$response = $this->apiPut("courses/{$courseId}/assignments/{$assignment['id']}", [
'json' => [
'assignment' => [
'description' => $assignment['description'],
],
],
]);
}
}
}

return $fileResponse;
}

Expand Down Expand Up @@ -286,4 +310,11 @@ public function showPage($courseId, $pageId)
return $response->getContent();
}

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

}
29 changes: 27 additions & 2 deletions src/Lms/Canvas/CanvasLms.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ public function postFileItem(FileItem $file, string $newFileName)
$canvasApi = new CanvasApi($apiDomain, $apiToken);
$modules = $canvasApi->listModules($file->getCourse()->getLmsCourseId());
$pages = $canvasApi->listPages($file->getCourse()->getLmsCourseId());
$assignments = $canvasApi->listAssignments($file->getCourse()->getLmsCourseId());
$refillModules = [];
$changePages = [];
$changeAssignments = [];

// Delete any existing module items with the same file name
foreach ($modules as $module) {
Expand All @@ -393,7 +395,7 @@ public function postFileItem(FileItem $file, string $newFileName)
}
}

// Find all pages that contain this file in their HTML
// Find all wiki pages that contain this file in their HTML
foreach ($pages as $page) {
$dom = new \DOMDocument();
$dom->loadHTML($page['body']);
Expand All @@ -407,13 +409,20 @@ public function postFileItem(FileItem $file, string $newFileName)
}
}

// Find all assignments that contain this file in their description HTML
foreach ($assignments as $assignment) {
if ($this->fileIsInLink($file->getLmsFileId(), $assignment['description'])) {
$changeAssignments[] = $assignment;
}
}

$url = "courses/{$file->getCourse()->getLmsCourseId()}/files/{$file->getLmsFileId()}";
$filepath = $this->util->getTempPath() . '/file.' . $file->getId();
$options = [
'postUrl' => "courses/{$file->getCourse()->getLmsCourseId()}/files"
];

$fileResponse = $canvasApi->apiFilePost($url, $options, $filepath, $newFileName, $refillModules, $file->getCourse()->getLmsCourseId(), $changePages);
$fileResponse = $canvasApi->apiFilePost($url, $options, $filepath, $newFileName, $refillModules, $file->getCourse()->getLmsCourseId(), $changePages, $changeAssignments);
$fileObj = $fileResponse->getContent();

if (isset($fileObj['id'])) {
Expand Down Expand Up @@ -535,6 +544,22 @@ public function getContentTypes()
* PROTECTED FUNCTIONS
**********************/

protected function fileIsInLink($fileId, $html)
{
$dom = new \DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');

foreach ($anchors as $anchor) {
preg_match('/files\/(\d+)/', $anchor->getAttribute('href'), $matches);
if(isset($matches[1]) && $matches[1] == $fileId) {
return true;
}
}

return false;
}

protected function getAccountInfo(User $user, $accountId)
{
$url = "accounts/${accountId}";
Expand Down

0 comments on commit 35c319e

Please sign in to comment.