Merge branch 'MDL-74911-master' of https://github.com/matthewhilton/moodle

This commit is contained in:
Andrew Nicols 2022-10-24 22:22:28 +08:00
commit f0be30af14

View file

@ -52,6 +52,9 @@ if (!$assignment->can_view_submission($userid)) {
} }
if ($action === 'pollconversions') { if ($action === 'pollconversions') {
// Poll conversions does not require session lock.
\core\session\manager::write_close();
$draft = true; $draft = true;
if (!has_capability('mod/assign:grade', $context)) { if (!has_capability('mod/assign:grade', $context)) {
// A student always sees the readonly version. // A student always sees the readonly version.
@ -65,7 +68,22 @@ if ($action === 'pollconversions') {
$draft = false; $draft = false;
} }
$response = (object) [ // Get a lock for the PDF/Image conversion of the assignment files.
$lockfactory = \core\lock\lock_config::get_lock_factory('assignfeedback_editpdf_pollconversions');
$resource = "user:${userid},assignmentid:${assignmentid},attemptnumber:${attemptnumber}";
$lock = $lockfactory->get_lock($resource, 0);
// Could not get lock, send back JSON to poll again.
if (!$lock) {
echo json_encode([
'status' => 0
]);
die();
}
// Obtained lock, now process the assignment conversion.
try {
$response = (object) [
'status' => -1, 'status' => -1,
'filecount' => 0, 'filecount' => 0,
'pagecount' => 0, 'pagecount' => 0,
@ -74,70 +92,76 @@ if ($action === 'pollconversions') {
'pages' => [], 'pages' => [],
]; ];
$combineddocument = document_services::get_combined_document_for_attempt($assignment, $userid, $attemptnumber); $combineddocument = document_services::get_combined_document_for_attempt($assignment, $userid, $attemptnumber);
$response->status = $combineddocument->get_status();
$response->filecount = $combineddocument->get_document_count();
$readystatuslist = [combined_document::STATUS_READY, combined_document::STATUS_READY_PARTIAL];
$completestatuslist = [combined_document::STATUS_COMPLETE, combined_document::STATUS_FAILED];
if (in_array($response->status, $readystatuslist)) {
// It seems that the files for this submission haven't been combined in cron yet.
// Try to combine them in the user session.
$combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
$response->status = $combineddocument->get_status(); $response->status = $combineddocument->get_status();
$response->filecount = $combineddocument->get_document_count(); $response->filecount = $combineddocument->get_document_count();
}
if (in_array($response->status, $completestatuslist)) { $readystatuslist = [combined_document::STATUS_READY, combined_document::STATUS_READY_PARTIAL];
$pages = document_services::get_page_images_for_attempt($assignment, $completestatuslist = [combined_document::STATUS_COMPLETE, combined_document::STATUS_FAILED];
$userid,
$attemptnumber,
$readonly);
$response->pagecount = $combineddocument->get_page_count(); if (in_array($response->status, $readystatuslist)) {
// It seems that the files for this submission haven't been combined in cron yet.
$grade = $assignment->get_user_grade($userid, true, $attemptnumber); // Try to combine them in the user session.
$combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
// The readonly files are stored in a different file area. $response->status = $combineddocument->get_status();
$filearea = document_services::PAGE_IMAGE_FILEAREA; $response->filecount = $combineddocument->get_document_count();
if ($readonly) {
$filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
} }
$response->partial = $combineddocument->is_partial_conversion();
foreach ($pages as $id => $pagefile) { if (in_array($response->status, $completestatuslist)) {
$index = count($response->pages); $pages = document_services::get_page_images_for_attempt($assignment,
$page = new stdClass(); $userid,
$comments = page_editor::get_comments($grade->id, $index, $draft); $attemptnumber,
$page->url = moodle_url::make_pluginfile_url($context->id, $readonly);
'assignfeedback_editpdf',
$filearea, $response->pagecount = $combineddocument->get_page_count();
$grade->id,
'/', $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
$pagefile->get_filename())->out();
$page->comments = $comments; // The readonly files are stored in a different file area.
if ($imageinfo = $pagefile->get_imageinfo()) { $filearea = document_services::PAGE_IMAGE_FILEAREA;
$page->width = $imageinfo['width']; if ($readonly) {
$page->height = $imageinfo['height']; $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
} else {
$page->width = 0;
$page->height = 0;
} }
$annotations = page_editor::get_annotations($grade->id, $index, $draft); $response->partial = $combineddocument->is_partial_conversion();
$page->annotations = $annotations;
$response->pages[] = $page;
}
$component = 'assignfeedback_editpdf'; foreach ($pages as $id => $pagefile) {
$filearea = document_services::PAGE_IMAGE_FILEAREA; $index = count($response->pages);
$filepath = '/'; $page = new stdClass();
$fs = get_file_storage(); $comments = page_editor::get_comments($grade->id, $index, $draft);
$files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath); $page->url = moodle_url::make_pluginfile_url($context->id,
$response->pageready = count($files); 'assignfeedback_editpdf',
$filearea,
$grade->id,
'/',
$pagefile->get_filename())->out();
$page->comments = $comments;
if ($imageinfo = $pagefile->get_imageinfo()) {
$page->width = $imageinfo['width'];
$page->height = $imageinfo['height'];
} else {
$page->width = 0;
$page->height = 0;
}
$annotations = page_editor::get_annotations($grade->id, $index, $draft);
$page->annotations = $annotations;
$response->pages[] = $page;
}
$component = 'assignfeedback_editpdf';
$filearea = document_services::PAGE_IMAGE_FILEAREA;
$filepath = '/';
$fs = get_file_storage();
$files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
$response->pageready = count($files);
}
} catch (\Throwable $e) {
// Release lock, and re-throw exception.
$lock->release();
throw $e;
} }
echo json_encode($response); echo json_encode($response);
$lock->release();
die(); die();
} else if ($action == 'savepage') { } else if ($action == 'savepage') {
require_capability('mod/assign:grade', $context); require_capability('mod/assign:grade', $context);