Beispiel #1
0
    /**
     * Process an uploaded zip file
     *
     * @param assign $assignment - The assignment instance
     * @param assign_feedback_file $fileplugin - The file feedback plugin
     * @return string - The html response
     */
    public function import_zip_files($assignment, $fileplugin) {
        global $USER, $CFG, $PAGE, $DB;

        @set_time_limit(ASSIGNFEEDBACK_FILE_MAXFILEUNZIPTIME);
        $packer = get_file_packer('application/zip');

        $feedbackfilesupdated = 0;
        $feedbackfilesadded = 0;
        $userswithnewfeedback = array();
        $contextid = $assignment->get_context()->id;

        $fs = get_file_storage();
        $files = $fs->get_directory_files($contextid,
                                          'assignfeedback_file',
                                          ASSIGNFEEDBACK_FILE_IMPORT_FILEAREA,
                                          $USER->id,
                                          '/import/');

        $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
        $allusers = $assignment->list_participants($currentgroup, false);
        $participants = array();
        foreach ($allusers as $user) {
            $participants[$assignment->get_uniqueid_for_user($user->id)] = $user;
        }

        foreach ($files as $unzippedfile) {
            // Set the timeout for unzipping each file.
            $user = null;
            $plugin = null;
            $filename = '';

            if ($this->is_valid_filename_for_import($assignment, $unzippedfile, $participants, $user, $plugin, $filename)) {
                if ($this->is_file_modified($assignment, $user, $plugin, $filename, $unzippedfile)) {
                    $grade = $assignment->get_user_grade($user->id, true);

                    if ($oldfile = $fs->get_file($contextid,
                                                 'assignfeedback_file',
                                                 ASSIGNFEEDBACK_FILE_FILEAREA,
                                                 $grade->id,
                                                 '/',
                                                 $filename)) {
                        // Update existing feedback file.
                        $oldfile->replace_content_with($unzippedfile);
                        $feedbackfilesupdated++;
                    } else {
                        // Create a new feedback file.
                        $newfilerecord = new stdClass();
                        $newfilerecord->contextid = $contextid;
                        $newfilerecord->component = 'assignfeedback_file';
                        $newfilerecord->filearea = ASSIGNFEEDBACK_FILE_FILEAREA;
                        $newfilerecord->filename = $filename;
                        $newfilerecord->filepath = '/';
                        $newfilerecord->itemid = $grade->id;
                        $fs->create_file_from_storedfile($newfilerecord, $unzippedfile);
                        $feedbackfilesadded++;
                    }
                    $userswithnewfeedback[$user->id] = 1;

                    // Update the number of feedback files for this user.
                    $fileplugin->update_file_count($grade);

                    // Update the last modified time on the grade which will trigger student notifications.
                    $assignment->notify_grade_modified($grade);
                }
            }
        }

        require_once($CFG->dirroot . '/mod/assign/feedback/file/renderable.php');
        $importsummary = new assignfeedback_file_import_summary($assignment->get_course_module()->id,
                                                            count($userswithnewfeedback),
                                                            $feedbackfilesadded,
                                                            $feedbackfilesupdated);

        $assignrenderer = $assignment->get_renderer();
        $renderer = $PAGE->get_renderer('assignfeedback_file');

        $o = '';

        $o .= $assignrenderer->render(new assign_header($assignment->get_instance(),
                                                        $assignment->get_context(),
                                                        false,
                                                        $assignment->get_course_module()->id,
                                                        get_string('uploadzipsummary', 'assignfeedback_file')));

        $o .= $renderer->render($importsummary);

        $o .= $assignrenderer->render_footer();
        return $o;
    }
Beispiel #2
0
 /**
  * Process an uploaded zip file
  *
  * @param assign $assignment - The assignment instance
  * @param assign_feedback_file $fileplugin - The file feedback plugin
  * @return string - The html response
  */
 public function import_zip_files($assignment, $fileplugin)
 {
     global $CFG, $PAGE, $DB;
     core_php_time_limit::raise(ASSIGNFEEDBACK_FILE_MAXFILEUNZIPTIME);
     $packer = get_file_packer('application/zip');
     $feedbackfilesupdated = 0;
     $feedbackfilesadded = 0;
     $userswithnewfeedback = array();
     $contextid = $assignment->get_context()->id;
     $fs = get_file_storage();
     $files = $this->get_import_files($contextid);
     $currentgroup = groups_get_activity_group($assignment->get_course_module(), true);
     $allusers = $assignment->list_participants($currentgroup, false);
     $participants = array();
     foreach ($allusers as $user) {
         $participants[$assignment->get_uniqueid_for_user($user->id)] = $user;
     }
     foreach ($files as $unzippedfile) {
         // Set the timeout for unzipping each file.
         $user = null;
         $plugin = null;
         $filename = '';
         if ($this->is_valid_filename_for_import($assignment, $unzippedfile, $participants, $user, $plugin, $filename)) {
             if ($this->is_file_modified($assignment, $user, $plugin, $filename, $unzippedfile)) {
                 $grade = $assignment->get_user_grade($user->id, true);
                 // In 3.1 the default download structure of the submission files changed so that each student had their own
                 // separate folder, the files were not renamed and the folder structure was kept. It is possible that
                 // a user downloaded the submission files in 3.0 (or earlier) and edited the zip to add feedback or
                 // changed the behavior back to the previous format, the following code means that we will still support the
                 // old file structure. For more information please see - MDL-52489 / MDL-56022.
                 $path = pathinfo($filename);
                 if ($path['dirname'] == '.') {
                     // Student submissions are not in separate folders.
                     $basename = $filename;
                     $dirname = "/";
                     $dirnamewslash = "/";
                 } else {
                     $basename = $path['basename'];
                     $dirname = $path['dirname'];
                     $dirnamewslash = $dirname . "/";
                 }
                 if ($oldfile = $fs->get_file($contextid, 'assignfeedback_file', ASSIGNFEEDBACK_FILE_FILEAREA, $grade->id, $dirname, $basename)) {
                     // Update existing feedback file.
                     $oldfile->replace_file_with($unzippedfile);
                     $feedbackfilesupdated++;
                 } else {
                     // Create a new feedback file.
                     $newfilerecord = new stdClass();
                     $newfilerecord->contextid = $contextid;
                     $newfilerecord->component = 'assignfeedback_file';
                     $newfilerecord->filearea = ASSIGNFEEDBACK_FILE_FILEAREA;
                     $newfilerecord->filename = $basename;
                     $newfilerecord->filepath = $dirnamewslash;
                     $newfilerecord->itemid = $grade->id;
                     $fs->create_file_from_storedfile($newfilerecord, $unzippedfile);
                     $feedbackfilesadded++;
                 }
                 $userswithnewfeedback[$user->id] = 1;
                 // Update the number of feedback files for this user.
                 $fileplugin->update_file_count($grade);
                 // Update the last modified time on the grade which will trigger student notifications.
                 $assignment->notify_grade_modified($grade);
             }
         }
     }
     require_once $CFG->dirroot . '/mod/assign/feedback/file/renderable.php';
     $importsummary = new assignfeedback_file_import_summary($assignment->get_course_module()->id, count($userswithnewfeedback), $feedbackfilesadded, $feedbackfilesupdated);
     $assignrenderer = $assignment->get_renderer();
     $renderer = $PAGE->get_renderer('assignfeedback_file');
     $o = '';
     $o .= $assignrenderer->render(new assign_header($assignment->get_instance(), $assignment->get_context(), false, $assignment->get_course_module()->id, get_string('uploadzipsummary', 'assignfeedback_file')));
     $o .= $renderer->render($importsummary);
     $o .= $assignrenderer->render_footer();
     return $o;
 }