function emarking_create_anonymous_page_from_storedfile(stored_file $file, $student) { if (!$file) { throw new Exception('Stored file does not exist'); } // Get file storage and copy file to temp folder. $fs = get_file_storage(); $tmppath = $file->copy_content_to_temp('emarking', 'anonymous'); // Treat the file as an image, get its size and draw a white rectangle. $size = getimagesize($tmppath, $info); $imagemime = exif_imagetype($tmppath); if ($imagemime == IMAGETYPE_PNG) { $image = imagecreatefrompng($tmppath); } elseif ($imagemime == IMAGETYPE_JPEG) { $image = imagecreatefromjpeg($tmppath); } else { throw new Exception('Unsupported file type for pages'); } if (!$image) { throw new Exception('Could not read image from ' . $info[0] . ' ' . $tmppath); } $white = imagecolorallocate($image, 255, 255, 255); $y2 = round($size[1] / 10, 0); imagefilledrectangle($image, 0, 0, $size[0], $y2, $white); // Save the new image to replace the file. if ($imagemime == IMAGETYPE_PNG) { if (!imagepng($image, $tmppath)) { throw new Exception('Could not save image as PNG to ' . $tmppath); } } elseif ($imagemime == IMAGETYPE_JPEG) { if (!imagejpeg($image, $tmppath, 100)) { throw new Exception('Could not save image as JPEG to ' . $tmppath); } } else { throw new Exception('Unsupported file type for saving page'); } clearstatcache(); $filenameanonymous = emarking_get_anonymous_filename($file->get_filename()); // Copy file from temp folder to Moodle's filesystem. $filerecordanonymous = array('contextid' => $file->get_contextid(), 'component' => 'mod_emarking', 'filearea' => 'pages', 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => $filenameanonymous, 'timecreated' => $file->get_timecreated(), 'timemodified' => time(), 'userid' => $student->id, 'author' => $student->firstname . ' ' . $student->lastname, 'license' => 'allrightsreserved'); $previousfile = $fs->get_file($file->get_contextid(), 'mod_emarking', 'pages', $file->get_itemid(), '/', $filenameanonymous); if ($previousfile) { $previousfile->delete(); } $fileinfo = $fs->create_file_from_pathname($filerecordanonymous, $tmppath); if (!$fileinfo) { throw new Exception('Could not create anonymous version of a stored file'); } return $fileinfo; }
$PAGE->requires->jquery_plugin('ui-css'); // Save uploaded file in Moodle filesystem and check. $fs = get_file_storage(); if ($action === 'delete') { require_capability('mod/emarking:uploadexam', $context); $d = $_POST['d']; if (!is_array($d)) { print_error('Invalid parameters'); } foreach ($d as $fileidtodelete) { if (!is_number($fileidtodelete)) { continue; } $filetodelete = $fs->get_file_by_id($fileidtodelete); // Calculate anonymous file name from original file name. $anonymousfilename = emarking_get_anonymous_filename($filetodelete->get_filename()); $filetodelete->delete(); $anonymousfile = $fs->get_file($context->id, 'mod_emarking', 'orphanpages', $emarking->id, '/', $anonymousfilename); if ($anonymousfile) { $anonymousfile->delete(); } } redirect($url, get_string('transactionsuccessfull', 'mod_emarking'), 3); die; } if ($action === 'rotate') { require_capability('mod/emarking:uploadexam', $context); $fileidtorotate = required_param('file', PARAM_INT); $newpath = emarking_rotate_image_file($fileidtorotate); redirect($url, get_string('transactionsuccessfull', 'mod_emarking'), 3); die;
function emarking_create_anonymous_page_from_storedfile(stored_file $file) { if (!$file) { throw new Exception('Stored file does not exist'); } // Get file storage and copy file to temp folder. $fs = get_file_storage(); $tmppath = $file->copy_content_to_temp('emarking', 'anonymous'); // Treat the file as an image, get its size and draw a white rectangle. $size = getimagesize($tmppath); $image = imagecreatefrompng($tmppath); $white = imagecolorallocate($image, 255, 255, 255); $y2 = round($size[1] / 10, 0); imagefilledrectangle($image, 0, 0, $size[0], $y2, $white); // Save the new image to replace the file. if (!imagepng($image, $tmppath)) { return false; } clearstatcache(); $filenameanonymous = emarking_get_anonymous_filename($file->get_filename()); // Copy file from temp folder to Moodle's filesystem. $filerecordanonymous = array('contextid' => $file->get_contextid(), 'component' => 'mod_emarking', 'filearea' => 'pages', 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => $filenameanonymous, 'timecreated' => $file->get_timecreated(), 'timemodified' => time(), 'userid' => $file->get_userid(), 'author' => $file->get_author(), 'license' => 'allrightsreserved'); $previousfile = $fs->get_file($file->get_contextid(), 'mod_emarking', 'pages', $file->get_itemid(), '/', $filenameanonymous); if ($previousfile) { $previousfile->delete(); } $fileinfo = $fs->create_file_from_pathname($filerecordanonymous, $tmppath); return $fileinfo; }