Skip to content

Commit 93d30c5

Browse files
authored
Merge pull request ucfopen#1106 from Ishfaq-code/quiz-question-scan-fix
Fixing Quiz Questions Scanning
2 parents 4677c7d + 6358120 commit 93d30c5

1 file changed

Lines changed: 22 additions & 78 deletions

File tree

src/Lms/Canvas/CanvasLms.php

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,14 @@ public function updateCourseContent(Course $course, User $user, $force = false):
354354
{
355355
$output = new ConsoleOutput();
356356
$this->contentItemList = [];
357-
$urls = $this->getCourseContentUrls($course->getLmsCourseId());
357+
$courseId = $course->getLmsCourseId();
358+
$urls = $this->getCourseContentUrls($courseId);
358359
$apiDomain = $this->getApiDomain($user);
359360
$apiToken = $this->getApiToken($user);
360361

361362
$canvasApi = new CanvasApi($apiDomain, $apiToken);
362-
$tempPages = [];
363+
364+
// Batch page pulling maintaince
363365
$pageUrls = [];
364366
$asyncFetch = true;
365367

@@ -381,7 +383,6 @@ public function updateCourseContent(Course $course, User $user, $force = false):
381383

382384
foreach ($contentList as $content) {
383385
if ('file' === $contentType) {
384-
$output->writeln('Found file: ' . $content['display_name'] . ' Type: ' . $content['mime_class']);
385386
if (in_array($content['mime_class'], $this->util->getUnscannableFileMimeClasses())) {
386387
$this->updateFileItem($course, $content);
387388
continue;
@@ -396,86 +397,34 @@ public function updateCourseContent(Course $course, User $user, $force = false):
396397
if (('assignment' === $contentType) && isset($content['discussion_topic'])) {
397398
continue;
398399
}
399-
400-
$lmsContent = $this->normalizeLmsContent($course, $contentType, $content);
401-
if (!$lmsContent) {
402-
continue;
403-
}
404-
405-
/* Check to see if the existing content item is already in the database and hasn't been updated since.
406-
The $force variable is used to force the full rescan, and skips the 'already exists' check */
407-
408-
$contentItem = $this->contentItemRepo->findOneBy([
409-
'contentType' => $contentType,
410-
'lmsContentId' => $lmsContent['id'],
411-
'course' => $course,
412-
]);
413-
414-
if (!$force && $contentItem) {
415-
$contentItemUpdated = $contentItem->getUpdated();
416-
$lmsUpdated = new \DateTime($lmsContent['updated'], UtilityService::$timezone);
417-
if ($contentItemUpdated == $lmsUpdated) {
418-
$contentItem->setActive(true);
419-
continue;
420-
}
421-
$output->writeln('Content item already exists but is out of date. Updating ' . $contentType . ': ' . $lmsContent['title']);
422-
}
423-
else {
424-
$output->writeln('New content item - ' . $contentType . ': ' . $lmsContent['title']);
425-
}
426-
427-
if (!$contentItem) {
400+
if(('page' === $contentType) && ($asyncFetch)){
401+
// If we are using async fetch we need the
402+
$lmsContent = $this->normalizeLmsContent($course, $contentType, $content);
403+
$contentItem = $this->contentItemRepo->findOneBy([
404+
'contentType' => $contentType,
405+
'lmsContentId' => $lmsContent['id'],
406+
'course' => $course,
407+
]);
408+
409+
if (!$contentItem) {
428410
$contentItem = new ContentItem();
429411
$contentItem->setCourse($course)
430412
->setLmsContentId($lmsContent['id'])
431413
->setActive(true)
432414
->setContentType($contentType);
433415
$this->entityManager->persist($contentItem);
434416
}
435-
436-
if ('page' === $contentType) {
437-
$url = "courses/{$course->getLmsCourseId()}/pages/{$lmsContent['id']}";
438-
if($asyncFetch) {
439-
/* NEW PAGE FETCH: New asynchronous batch fetch. The real magic is in the $pageUrls handler beneath this foreach loop (line ~305). */
440-
$tempContentItems[] = $contentItem;
441-
$pageUrls[] = $url;
442-
continue;
443-
}
444-
else {
445-
/* OLD PAGE FETCH: 1-at-a-time synchronous fetch */
446-
$pageResponse = $canvasApi->apiGet($url);
447-
$pageObj = $pageResponse->getContent();
448-
449-
if (!empty($pageObj['body'])) {
450-
$lmsContent['body'] = $pageObj['body'];
451-
}
452-
}
453-
}
454-
455-
/* get HTML file content */
456-
if (('file' === $contentType) && ('html' === $content['mime_class'])) {
457-
$lmsContent['body'] = file_get_contents($content['url']);
458-
}
459-
460-
// some content types don't have an updated date, so we'll compare content
461-
// to find out if content has changed.
462-
if (in_array($contentType, ['syllabus', 'discussion_topic', 'announcement', 'quiz'])) {
463-
if ($contentItem->getBody() === $lmsContent['body']) {
464-
if ($contentItem->getUpdated()) {
465-
$lmsContent['updated'] = $contentItem->getUpdated()->format('c');
466-
}
467-
}
417+
$url = "courses/{$course->getLmsCourseId()}/pages/{$lmsContent['id']}";
418+
$tempContentItems[] = $contentItem;
419+
$pageUrls[] = $url;
420+
continue;
468421
}
469422

470-
$contentItem->update($lmsContent);
471-
if($contentItem->getBody() !== null) {
472-
$contentItems[] = $contentItem;
473-
}
423+
$this->saveOrUpdateContentItem($canvasApi, $course, $contentType, $content, $force);
474424
}
475425
}
476426
}
477427

478-
// If there are any pages to fetch, handle that now...
479428
if(count($pageUrls) > 0) {
480429

481430
$output->writeln('Fetching contents for ' . count($pageUrls) . ' pages asynchronously...');
@@ -498,20 +447,15 @@ public function updateCourseContent(Course $course, User $user, $force = false):
498447
if(isset($tempContentItemsIndexById[$lmsContentId])) {
499448
$index = $tempContentItemsIndexById[$lmsContentId];
500449
$tempContentItems[$index]->update($lmsContent);
501-
$contentItems[] = $tempContentItems[$index];
450+
$this->contentItemList[] = $tempContentItems[$index];
502451
}
503452
}
504453
}
505454
}
506-
455+
507456
// push any updates made to content items to DB
508457
$this->entityManager->flush();
509-
510-
// Log how long things took (compare synchronous vs asynchronous page fetch)
511-
$end_time = microtime(true);
512-
$output->writeln('updateCourseContent - time taken: ' . ($end_time - $start_time) . ' seconds');
513-
514-
return $contentItems;
458+
return $this->contentItemList;
515459
}
516460

517461
public function getCourseSections(Course $course, User $user)

0 commit comments

Comments
 (0)