Beispiel #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;
 }