define('AJAX_SCRIPT', true);
// To be able to process concurrent ajax request with the generate pdf ajax request we can not use cookie.
define('NO_MOODLE_COOKIES', true);
use seplfeedback_editpdf\document_services;
require_once '../../../../config.php';
try {
    $seplmentid = required_param('seplmentid', PARAM_INT);
    $userid = required_param('userid', PARAM_INT);
    $attemptnumber = required_param('attemptnumber', PARAM_INT);
    // Retrieve the seplments.
    require_once $CFG->dirroot . '/mod/sepl/locallib.php';
    $cm = get_coursemodule_from_instance('sepl', $seplmentid, 0, false, MUST_EXIST);
    $context = context_module::instance($cm->id);
    $seplment = new sepl($context, null, null);
    // Get the generated images from file API call.
    $grade = $seplment->get_user_grade($userid, false, $attemptnumber);
    // Check we found a grade.
    if (empty($grade)) {
        throw new coding_exception('grade not found');
    }
    // No need to handle the readonly files here, the should be already generated.
    $component = 'seplfeedback_editpdf';
    $filearea = document_services::PAGE_IMAGE_FILEAREA;
    $filepath = '/';
    $fs = get_file_storage();
    $files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
    // The important security part: we ONLY RETURN the total NUMBER of generated images.
    echo $OUTPUT->header();
    echo json_encode(count($files));
    echo $OUTPUT->footer();
} catch (Exception $e) {
 /**
  * Does this file exist in any of the current files supported by this plugin for this user?
  *
  * @param sepl $seplment - The seplment instance
  * @param stdClass $user The user matching this uploaded file
  * @param sepl_plugin $plugin The matching plugin from the filename
  * @param string $filename The parsed filename from the zip
  * @param stored_file $fileinfo The info about the extracted file from the zip
  * @return bool - True if the file has been modified or is new
  */
 public function is_file_modified($seplment, $user, $plugin, $filename, $fileinfo)
 {
     $sg = null;
     if ($plugin->get_subtype() == 'seplsubmission') {
         $sg = $seplment->get_user_submission($user->id, false);
     } else {
         if ($plugin->get_subtype() == 'seplfeedback') {
             $sg = $seplment->get_user_grade($user->id, false);
         } else {
             return false;
         }
     }
     if (!$sg) {
         return true;
     }
     foreach ($plugin->get_files($sg, $user) as $pluginfilename => $file) {
         if ($pluginfilename == $filename) {
             // Extract the file and compare hashes.
             $contenthash = '';
             if (is_array($file)) {
                 $content = reset($file);
                 $contenthash = sha1($content);
             } else {
                 $contenthash = $file->get_contenthash();
             }
             if ($contenthash != $fileinfo->get_contenthash()) {
                 return true;
             } else {
                 return false;
             }
         }
     }
     return true;
 }
 /**
  * Copy annotations, comments, pages, and other required content from the source user to the current group member
  * being procssed when using applytoall.
  *
  * @param int|\sepl $seplment
  * @param stdClass $grade
  * @param int $sourceuserid
  * @return bool
  */
 public static function copy_drafts_from_to($seplment, $grade, $sourceuserid)
 {
     global $DB;
     // Delete any existing annotations and comments from current user.
     $DB->delete_records('seplfeedback_editpdf_annot', array('gradeid' => $grade->id));
     $DB->delete_records('seplfeedback_editpdf_cmnt', array('gradeid' => $grade->id));
     // Get gradeid, annotations and comments from sourceuserid.
     $sourceusergrade = $seplment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
     $annotations = $DB->get_records('seplfeedback_editpdf_annot', array('gradeid' => $sourceusergrade->id, 'draft' => 1));
     $comments = $DB->get_records('seplfeedback_editpdf_cmnt', array('gradeid' => $sourceusergrade->id, 'draft' => 1));
     $contextid = $seplment->get_context()->id;
     $sourceitemid = $sourceusergrade->id;
     // Add annotations and comments to current user to generate feedback file.
     foreach ($annotations as $annotation) {
         $annotation->gradeid = $grade->id;
         $DB->insert_record('seplfeedback_editpdf_annot', $annotation);
     }
     foreach ($comments as $comment) {
         $comment->gradeid = $grade->id;
         $DB->insert_record('seplfeedback_editpdf_cmnt', $comment);
     }
     $fs = get_file_storage();
     // Copy the stamp files.
     self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::STAMPS_FILEAREA, true);
     // Copy the PAGE_IMAGE_FILEAREA files.
     self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::PAGE_IMAGE_FILEAREA);
     return true;
 }