Ejemplo n.º 1
0
 /**
  * Download a zip file of all assignment submissions.
  *
  * @return string - If an error occurs, this will contain the error page.
  */
 protected function download_submissions()
 {
     global $CFG, $DB;
     // More efficient to load this here.
     require_once $CFG->libdir . '/filelib.php';
     require_capability('mod/assign:grade', $this->context);
     // Load all users with submit.
     $students = get_enrolled_users($this->context, "mod/assign:submit", null, 'u.*', null, null, null, $this->show_only_active_users());
     // Build a list of files to zip.
     $filesforzipping = array();
     $fs = get_file_storage();
     $groupmode = groups_get_activity_groupmode($this->get_course_module());
     // All users.
     $groupid = 0;
     $groupname = '';
     if ($groupmode) {
         $groupid = groups_get_activity_group($this->get_course_module(), true);
         $groupname = groups_get_group_name($groupid) . '-';
     }
     // Construct the zip file name.
     $filename = clean_filename($this->get_course()->shortname . '-' . $this->get_instance()->name . '-' . $groupname . $this->get_course_module()->id . '.zip');
     // Get all the files for each student.
     foreach ($students as $student) {
         $userid = $student->id;
         if (groups_is_member($groupid, $userid) or !$groupmode or !$groupid) {
             // Get the plugins to add their own files to the zip.
             $submissiongroup = false;
             $groupname = '';
             if ($this->get_instance()->teamsubmission) {
                 $submission = $this->get_group_submission($userid, 0, false);
                 $submissiongroup = $this->get_submission_group($userid);
                 if ($submissiongroup) {
                     $groupname = $submissiongroup->name . '-';
                 } else {
                     $groupname = get_string('defaultteam', 'assign') . '-';
                 }
             } else {
                 $submission = $this->get_user_submission($userid, false);
             }
             if ($this->is_blind_marking()) {
                 $prefix = str_replace('_', ' ', $groupname . get_string('participant', 'assign'));
                 $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($userid) . '_');
             } else {
                 $prefix = str_replace('_', ' ', $groupname . fullname($student));
                 $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($userid) . '_');
             }
             if ($submission) {
                 foreach ($this->submissionplugins as $plugin) {
                     if ($plugin->is_enabled() && $plugin->is_visible()) {
                         $pluginfiles = $plugin->get_files($submission, $student);
                         foreach ($pluginfiles as $zipfilename => $file) {
                             $subtype = $plugin->get_subtype();
                             $type = $plugin->get_type();
                             $prefixedfilename = clean_filename($prefix . $subtype . '_' . $type . '_' . $zipfilename);
                             $filesforzipping[$prefixedfilename] = $file;
                         }
                     }
                 }
             }
         }
     }
     $result = '';
     if (count($filesforzipping) == 0) {
         $header = new assign_header($this->get_instance(), $this->get_context(), '', $this->get_course_module()->id, get_string('downloadall', 'assign'));
         $result .= $this->get_renderer()->render($header);
         $result .= $this->get_renderer()->notification(get_string('nosubmission', 'assign'));
         $url = new moodle_url('/mod/assign/view.php', array('id' => $this->get_course_module()->id, 'action' => 'grading'));
         $result .= $this->get_renderer()->continue_button($url);
         $result .= $this->view_footer();
     } else {
         if ($zipfile = $this->pack_files($filesforzipping)) {
             $addtolog = $this->add_to_log('download all submissions', get_string('downloadall', 'assign'), '', true);
             $params = array('context' => $this->context, 'objectid' => $this->get_instance()->id);
             $event = \mod_assign\event\all_submissions_downloaded::create($params);
             $event->set_legacy_logdata($addtolog);
             $event->trigger();
             // Send file and delete after sending.
             send_temp_file($zipfile, $filename);
             // We will not get here - send_temp_file calls exit.
         }
     }
     return $result;
 }
Ejemplo n.º 2
0
    /**
     * Download a zip file of all assignment submissions.
     *
     * @param array $userids Array of user ids to download assignment submissions in a zip file
     * @return string - If an error occurs, this will contain the error page.
     */
    protected function download_submissions($userids = false) {
        global $CFG, $DB;

        // More efficient to load this here.
        require_once($CFG->libdir.'/filelib.php');

        // Increase the server timeout to handle the creation and sending of large zip files.
        core_php_time_limit::raise();

        $this->require_view_grades();

        // Load all users with submit.
        $students = get_enrolled_users($this->context, "mod/assign:submit", null, 'u.*', null, null, null,
                        $this->show_only_active_users());

        // Build a list of files to zip.
        $filesforzipping = array();
        $fs = get_file_storage();

        $groupmode = groups_get_activity_groupmode($this->get_course_module());
        // All users.
        $groupid = 0;
        $groupname = '';
        if ($groupmode) {
            $groupid = groups_get_activity_group($this->get_course_module(), true);
            $groupname = groups_get_group_name($groupid).'-';
        }

        // Construct the zip file name.
        $filename = clean_filename($this->get_course()->shortname . '-' .
                                   $this->get_instance()->name . '-' .
                                   $groupname.$this->get_course_module()->id . '.zip');

        // Get all the files for each student.
        foreach ($students as $student) {
            $userid = $student->id;
            // Download all assigments submission or only selected users.
            if ($userids and !in_array($userid, $userids)) {
                continue;
            }

            if ((groups_is_member($groupid, $userid) or !$groupmode or !$groupid)) {
                // Get the plugins to add their own files to the zip.

                $submissiongroup = false;
                $groupname = '';
                if ($this->get_instance()->teamsubmission) {
                    $submission = $this->get_group_submission($userid, 0, false);
                    $submissiongroup = $this->get_submission_group($userid);
                    if ($submissiongroup) {
                        $groupname = $submissiongroup->name . '-';
                    } else {
                        $groupname = get_string('defaultteam', 'assign') . '-';
                    }
                } else {
                    $submission = $this->get_user_submission($userid, false);
                }

                if ($this->is_blind_marking()) {
                    $prefix = str_replace('_', ' ', $groupname . get_string('participant', 'assign'));
                    $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($userid));
                } else {
                    $prefix = str_replace('_', ' ', $groupname . fullname($student));
                    $prefix = clean_filename($prefix . '_' . $this->get_uniqueid_for_user($userid));
                }

                if ($submission) {
                    $downloadasfolders = get_user_preferences('assign_downloadasfolders', 1);
                    foreach ($this->submissionplugins as $plugin) {
                        if ($plugin->is_enabled() && $plugin->is_visible()) {
                            if ($downloadasfolders) {
                                // Create a folder for each user for each assignment plugin.
                                // This is the default behavior for version of Moodle >= 3.1.
                                $submission->exportfullpath = true;
                                $pluginfiles = $plugin->get_files($submission, $student);
                                foreach ($pluginfiles as $zipfilepath => $file) {
                                    $subtype = $plugin->get_subtype();
                                    $type = $plugin->get_type();
                                    $zipfilename = basename($zipfilepath);
                                    $prefixedfilename = clean_filename($prefix .
                                                                       '_' .
                                                                       $subtype .
                                                                       '_' .
                                                                       $type .
                                                                       '_');
                                    if ($type == 'file') {
                                        $pathfilename = $prefixedfilename . $file->get_filepath() . $zipfilename;
                                    } else if ($type == 'onlinetext') {
                                        $pathfilename = $prefixedfilename . '/' . $zipfilename;
                                    } else {
                                        $pathfilename = $prefixedfilename . '/' . $zipfilename;
                                    }
                                    $pathfilename = clean_param($pathfilename, PARAM_PATH);
                                    $filesforzipping[$pathfilename] = $file;
                                }
                            } else {
                                // Create a single folder for all users of all assignment plugins.
                                // This was the default behavior for version of Moodle < 3.1.
                                $submission->exportfullpath = false;
                                $pluginfiles = $plugin->get_files($submission, $student);
                                foreach ($pluginfiles as $zipfilename => $file) {
                                    $subtype = $plugin->get_subtype();
                                    $type = $plugin->get_type();
                                    $prefixedfilename = clean_filename($prefix .
                                                                       '_' .
                                                                       $subtype .
                                                                       '_' .
                                                                       $type .
                                                                       '_' .
                                                                       $zipfilename);
                                    $filesforzipping[$prefixedfilename] = $file;
                                }
                            }
                        }
                    }
                }
            }
        }
        $result = '';
        if (count($filesforzipping) == 0) {
            $header = new assign_header($this->get_instance(),
                                        $this->get_context(),
                                        '',
                                        $this->get_course_module()->id,
                                        get_string('downloadall', 'assign'));
            $result .= $this->get_renderer()->render($header);
            $result .= $this->get_renderer()->notification(get_string('nosubmission', 'assign'));
            $url = new moodle_url('/mod/assign/view.php', array('id'=>$this->get_course_module()->id,
                                                                    'action'=>'grading'));
            $result .= $this->get_renderer()->continue_button($url);
            $result .= $this->view_footer();
        } else if ($zipfile = $this->pack_files($filesforzipping)) {
            \mod_assign\event\all_submissions_downloaded::create_from_assign($this)->trigger();
            // Send file and delete after sending.
            send_temp_file($zipfile, $filename);
            // We will not get here - send_temp_file calls exit.
        }
        return $result;
    }