function local_mail_downloadall($message) { $attachments = local_mail_attachments($message); if (count($attachments) > 1) { foreach ($attachments as $attach) { $filename = clean_filename($attach->get_filename()); $filesforzipping[$filename] = $attach; } $filename = clean_filename(fullname($message->sender()) . '-' . $message->subject() . '.zip'); if ($zipfile = pack_files($filesforzipping)) { send_temp_file($zipfile, $filename); } } }
/** * creates a zip of all assignment submissions and sends a zip to the browser */ public function download_submissions() { global $CFG,$DB; require_once($CFG->libdir.'/filelib.php'); $submissions = $this->get_submissions('',''); if (empty($submissions)) { print_error('errornosubmissions', 'assignment', new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id))); } $filesforzipping = array(); $fs = get_file_storage(); $groupmode = groups_get_activity_groupmode($this->cm); $groupid = 0; // All users $groupname = ''; if ($groupmode) { $groupid = groups_get_activity_group($this->cm, true); $groupname = groups_get_group_name($groupid).'-'; } $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file. foreach ($submissions as $submission) { // If assignment is open and submission is not finalized then don't add it to zip. $submissionstatus = $this->is_finalized($submission); if ($this->isopen() && empty($submissionstatus)) { continue; } $a_userid = $submission->userid; //get userid if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) { $a_assignid = $submission->assignment; //get name of this assignment for use in the file names. $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false); foreach ($files as $file) { //get files new name. $fileext = strstr($file->get_filename(), '.'); $fileoriginal = str_replace($fileext, '', $file->get_filename()); $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext); //save file name to array for zipping. $filesforzipping[$fileforzipname] = $file; } } } // end of foreach loop // Throw error if no files are added. if (empty($filesforzipping)) { print_error('errornosubmissions', 'assignment', new moodle_url('/mod/assignment/submissions.php', array('id'=>$this->cm->id))); } if ($zipfile = assignment_pack_files($filesforzipping)) { send_temp_file($zipfile, $filename); //send file and delete after sending. } }
/** * To be implemented by child classes * @param boolean $feedback * @param boolean $publish Whether to output directly, or send as a file * @return string */ public function print_grades($feedback = false) { global $CFG; require_once $CFG->libdir . '/filelib.php'; $export_tracking = $this->track_exports(); $strgrades = get_string('grades'); /// Calculate file name $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id))); $downloadfilename = clean_filename("{$shortname} {$strgrades}.xml"); make_temp_directory('gradeexport'); $tempfilename = $CFG->tempdir . '/gradeexport/' . md5(sesskey() . microtime() . $downloadfilename); if (!($handle = fopen($tempfilename, 'w+b'))) { print_error('cannotcreatetempdir'); } /// time stamp to ensure uniqueness of batch export fwrite($handle, '<results batch="xml_export_' . time() . '">' . "\n"); $export_buffer = array(); $geub = new grade_export_update_buffer(); $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid); $gui->require_active_enrolment($this->onlyactive); $gui->init(); while ($userdata = $gui->next_user()) { $user = $userdata->user; if (empty($user->idnumber)) { //id number must exist otherwise we cant match up students when importing continue; } // studentgrades[] index should match with corresponding $index foreach ($userdata->grades as $itemid => $grade) { $grade_item = $this->grade_items[$itemid]; $grade->grade_item =& $grade_item; $gradestr = $this->format_grade($grade, $this->displaytype); // no formating for now // MDL-11669, skip exported grades or bad grades (if setting says so) if ($export_tracking) { $status = $geub->track($grade); if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) { continue; } } fwrite($handle, "\t<result>\n"); if ($export_tracking) { fwrite($handle, "\t\t<state>{$status}</state>\n"); } // only need id number fwrite($handle, "\t\t<assignment>{$grade_item->idnumber}</assignment>\n"); // this column should be customizable to use either student id, idnumber, uesrname or email. fwrite($handle, "\t\t<student>{$user->idnumber}</student>\n"); fwrite($handle, "\t\t<score>{$gradestr}</score>\n"); if ($this->export_feedback) { $feedbackstr = $this->format_feedback($userdata->feedbacks[$itemid]); fwrite($handle, "\t\t<feedback>{$feedbackstr}</feedback>\n"); } fwrite($handle, "\t</result>\n"); } } fwrite($handle, "</results>"); fclose($handle); $gui->close(); $geub->close(); if (defined('BEHAT_SITE_RUNNING')) { // If behat is running, we cannot test the output if we force a file download. include $tempfilename; } else { @header("Content-type: text/xml; charset=UTF-8"); send_temp_file($tempfilename, $downloadfilename, false); } }
/** * @param $list array List of item id's to download. Empty array means all files. * @return void */ public function download_items(array $list = array()) { global $CFG, $DB; // More efficient to load this here. require_once $CFG->libdir . '/filelib.php'; $filesforzipping = array(); $fs = get_file_storage(); $filename = clean_filename('mediagallery-export-' . $this->record->name . '.zip'); $files = $fs->get_area_files($this->get_collection()->context->id, 'mod_mediagallery', 'item', false, 'id', false); $items = $this->get_items(); $keyed = array_flip($list); foreach ($files as $file) { $selected = isset($keyed[$file->get_itemid()]) || empty($list); if ($selected && isset($items[$file->get_itemid()])) { $filesforzipping[$file->get_filename()] = $file; } } if (empty($filesforzipping)) { return; } $tempzip = tempnam($CFG->tempdir . '/', 'mediagallery_'); $zipper = new \zip_packer(); $zipper->archive_to_pathname($filesforzipping, $tempzip); // Send file and delete after sending. send_temp_file($tempzip, $filename); }
public function view_bulk_certificates(moodle_url $url, array $selectedusers = null) { global $OUTPUT, $CFG, $DB; $course_context = context_course::instance($this->get_course()->id); $page = $url->get_param('page'); $perpage = $url->get_param('perpage'); $issuelist = $url->get_param('issuelist'); $action = $url->get_param('action'); $groupid = 0; $groupmode = groups_get_activity_groupmode($this->coursemodule); if ($groupmode) { $groupid = groups_get_activity_group($this->coursemodule, true); } $page_start = intval($page * $perpage); $usercount = 0; if (!$selectedusers) { $users = get_enrolled_users($course_context, '', $groupid); $usercount = count($users); $users = array_slice($users, $page_start, $perpage); } else { list($sqluserids, $params) = $DB->get_in_or_equal($selectedusers); $sql = "SELECT * FROM {user} WHERE id {$sqluserids}"; //Adding sort $sort = ''; $override = new stdClass(); $override->firstname = 'firstname'; $override->lastname = 'lastname'; $fullnamelanguage = get_string('fullnamedisplay', '', $override); if ($CFG->fullnamedisplay == 'firstname lastname' or $CFG->fullnamedisplay == 'firstname' or $CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname') { $sort = " ORDER BY firstname, lastname"; } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname') $sort = " ORDER BY lastname, firstname"; } $users = $DB->get_records_sql($sql . $sort, $params); } if (!$action) { echo $OUTPUT->header(); $this->show_tabs($url); groups_print_activity_menu($this->coursemodule, $url); $selectoptions = array('completed' => get_string('completedusers', 'simplecertificate'), 'allusers' => get_string('allusers', 'simplecertificate')); $select = new single_select($url, 'issuelist', $selectoptions, $issuelist); $select->label = get_string('showusers', 'simplecertificate'); echo $OUTPUT->render($select); echo '<br>'; echo '<form id="bulkissue" name="bulkissue" method="post" action="view.php">'; echo html_writer::label(get_string('bulkaction', 'simplecertificate'), 'menutype', true); echo ' '; $selectoptions = array('pdf' => get_string('onepdf', 'simplecertificate'), 'zip' => get_string('multipdf', 'simplecertificate'), 'email' => get_string('sendtoemail', 'simplecertificate')); echo html_writer::select($selectoptions, 'type', 'pdf'); $table = new html_table(); $table->width = "95%"; $table->tablealign = "center"; //strgrade $table->head = array(' ', get_string('fullname'), get_string('grade')); $table->align = array("left", "left", "center"); $table->size = array('1%', '89%', '10%'); foreach ($users as $user) { $canissue = $this->can_issue($user, $issuelist != 'allusers'); if (empty($canissue)) { $chkbox = html_writer::checkbox('selectedusers[]', $user->id, false); $name = $OUTPUT->user_picture($user) . fullname($user); $table->data[] = array($chkbox, $name, $this->get_grade($user->id)); } } $downloadbutton = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download')), get_string('bulkbuttonlabel', 'simplecertificate')); echo $OUTPUT->paging_bar($usercount, $page, $perpage, $url); echo '<br />'; echo html_writer::table($table); echo html_writer::tag('div', $downloadbutton, array('style' => 'text-align: center')); echo '</form>'; } else { if ($action == 'download') { $type = $url->get_param('type'); // Calculate file name $filename = str_replace(' ', '_', clean_filename($this->get_instance()->coursename . ' ' . get_string('modulenameplural', 'simplecertificate') . ' ' . strip_tags(format_string($this->get_instance()->name, true)) . '.' . strip_tags(format_string($type, true)))); switch ($type) { //One pdf with all certificates case 'pdf': $pdf = $this->create_pdf_object(); foreach ($users as $user) { $canissue = $this->can_issue($user, $issuelist != 'allusers'); if (empty($canissue)) { //To one pdf file $issuecert = $this->get_issue($user); $this->create_pdf($issuecert, $pdf, true); //Save certificate PDF if (!$this->issue_file_exists($issuecert)) { //To force file creation $issuecert->haschage = true; $this->get_issue_file($issuecert); } } } $pdf->Output($filename, 'D'); break; //One zip with all certificates in separated files //One zip with all certificates in separated files case 'zip': $filesforzipping = array(); foreach ($users as $user) { $canissue = $this->can_issue($user, $issuelist != 'allusers'); if (empty($canissue)) { $issuecert = $this->get_issue($user); if ($file = $this->get_issue_file($issuecert)) { $fileforzipname = $file->get_filename(); $filesforzipping[$fileforzipname] = $file; } else { error_log(get_string('filenotfound', 'simplecertificate')); print_error(get_string('filenotfound', 'simplecertificate')); } } } $tempzip = $this->create_temp_file('issuedcertificate_'); //zipping files $zipper = new zip_packer(); if ($zipper->archive_to_pathname($filesforzipping, $tempzip)) { //send file and delete after sending. send_temp_file($tempzip, $filename); } break; case 'email': foreach ($users as $user) { $canissue = $this->can_issue($user, $issuelist != 'allusers'); if (empty($canissue)) { $issuecert = $this->get_issue($user); if ($this->get_issue_file($issuecert)) { $this->send_certificade_email($issuecert); } else { error_log(get_string('filenotfound', 'simplecertificate')); print_error('filenotfound', 'simplecertificate'); } } } $url->remove_params('action', 'type'); redirect($url, get_string('emailsent', 'simplecertificate'), 5); break; } exit; } } echo $OUTPUT->footer(); }
/** * Download all user badges in zip archive. * * @param int $userid ID of badge owner. */ function badges_download($userid) { global $CFG, $DB; $context = context_user::instance($userid); $records = $DB->get_records('badge_issued', array('userid' => $userid)); // Get list of files to download. $fs = get_file_storage(); $filelist = array(); foreach ($records as $issued) { $badge = new badge($issued->badgeid); // Need to make image name user-readable and unique using filename safe characters. $name = $badge->name . ' ' . userdate($issued->dateissued, '%d %b %Y') . ' ' . hash('crc32', $badge->id); $name = str_replace(' ', '_', $name); if ($file = $fs->get_file($context->id, 'badges', 'userbadge', $issued->badgeid, '/', $issued->uniquehash . '.png')) { $filelist[$name . '.png'] = $file; } } // Zip files and sent them to a user. $tempzip = tempnam($CFG->tempdir . '/', 'mybadges'); $zipper = new zip_packer(); if ($zipper->archive_to_pathname($filelist, $tempzip)) { send_temp_file($tempzip, 'badges.zip'); } else { debugging("Problems with archiving the files.", DEBUG_DEVELOPER); die; } }
<?php // $Id$ require_once dirname(__FILE__) . '/../config.php'; require_once $CFG->libdir . '/filelib.php'; // Note: file.php always calls require_login() with $setwantsurltome=false // in order to avoid messing redirects. MDL-14495 require_login(0, true, null, false); $relativepath = get_file_argument(); if (!$relativepath) { print_error('invalidarg', 'question'); } $pathname = $CFG->dataroot . '/temp/questionexport/' . $USER->id . '/' . $relativepath; send_temp_file($pathname, $relativepath);
/** * Download a zip file of all assignment submissions * * @return void */ private function download_submissions() { global $CFG,$DB; // More efficient to load this here. require_once($CFG->libdir.'/filelib.php'); // Load all users with submit. $students = get_enrolled_users($this->context, "mod/assign:submit"); // 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); $groupname = '-' . $submissiongroup->name; } else { $submission = $this->get_user_submission($userid, false); } if ($this->is_blind_marking()) { $prefix = clean_filename(str_replace('_', ' ', get_string('participant', 'assign') . $groupname) . "_" . $this->get_uniqueid_for_user($userid) . "_"); } else { $prefix = clean_filename(str_replace('_', ' ', fullname($student) . $groupname) . "_" . $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); foreach ($pluginfiles as $zipfilename => $file) { $subtype = $plugin->get_subtype(); $type = $plugin->get_type(); $prefixedfilename = $prefix . $subtype . '_' . $type . '_' . $zipfilename; $filesforzipping[$prefixedfilename] = $file; } } } } } } if ($zipfile = $this->pack_files($filesforzipping)) { $this->add_to_log('download all submissions', get_string('downloadall', 'assign')); // Send file and delete after sending. send_temp_file($zipfile, $filename); } }
function block_quickmail_pluginfile($course, $record, $context, $filearea, $args, $forcedownload) { $fs = get_file_storage(); global $DB; list($itemid, $filename) = $args; if ($filearea == 'attachment_log') { $time = $DB->get_field('block_quickmail_log', 'time', array('id' => $itemid)); if ("{$time}_attachments.zip" == $filename) { $path = quickmail::zip_attachments($context, 'log', $itemid); send_temp_file($path, 'attachments.zip'); } } $params = array('component' => 'block_quickmail', 'filearea' => $filearea, 'itemid' => $itemid, 'filename' => $filename); $instanceid = $DB->get_field('files', 'id', $params); if (empty($instanceid)) { send_file_not_found(); } else { $file = $fs->get_file_by_id($instanceid); send_stored_file($file); } }
/// Get all the chapters $chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum'); /// Generate the manifest and all the contents chapters2imsmanifest($chapters, $book, $cm); /// Now zip everything make_upload_directory('temp'); $zipfile = $CFG->dataroot . "/temp/" . time() . '.zip'; $files = get_directory_list($CFG->dataroot . "/{$cm->course}/moddata/book/{$book->id}", basename($zipfile), false, true, true); foreach ($files as $key => $value) { $files[$key] = $CFG->dataroot . "/{$cm->course}/moddata/book/{$book->id}/" . $value; } zip_files($files, $zipfile); /// Now delete all the temp dirs delete_dir_contents($CFG->dataroot . "/{$cm->course}/moddata/book/{$book->id}"); /// Now serve the file send_temp_file($zipfile, clean_filename($book->name) . '.zip'); /** * This function will create the default imsmanifest plus contents for the book chapters passed as array * Everything will be created under the book moddata file area * */ function chapters2imsmanifest($chapters, $book, $cm) { global $CFG; /// Init imsmanifest and others $imsmanifest = ''; $imsitems = ''; $imsresources = ''; /// Moodle and Book version $moodle_release = $CFG->release; $moodle_version = $CFG->version; $book_version = get_field('modules', 'version', 'name', 'book');
* @package mod_folder * @copyright 2015 Andrew Hancox <*****@*****.**> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once __DIR__ . "/../../config.php"; $id = required_param('id', PARAM_INT); // Course module ID. $cm = get_coursemodule_from_id('folder', $id, 0, true, MUST_EXIST); $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_course_login($course, true, $cm); $context = context_module::instance($cm->id); require_capability('mod/folder:view', $context); $folder = $DB->get_record('folder', array('id' => $cm->instance), '*', MUST_EXIST); $downloadable = folder_archive_available($folder, $cm); if (!$downloadable) { print_error('cannotdownloaddir', 'repository'); } folder_downloaded($folder, $course, $cm, $context); $fs = get_file_storage(); $file = $fs->get_file($context->id, 'mod_folder', 'content', 0, '/', '.'); if (!$file) { print_error('cannotdownloaddir', 'repository'); } $zipper = get_file_packer('application/zip'); $filename = clean_filename($folder->name . "-" . date("Ymd")) . ".zip"; $temppath = make_request_directory() . $filename; if ($zipper->archive_to_pathname(array('/' => $file), $temppath)) { send_temp_file($temppath, $filename); } else { print_error('cannotdownloaddir', 'repository'); }
/** * Creates a zip of all uploaded files and sends a zip to the browser * @param unknown $users false => empty zip, true all users, array files from users in array */ public function download_zip($users = array()) { global $CFG, $DB; require_once $CFG->libdir . '/filelib.php'; $context = $this->get_context(); $cm = $this->get_coursemodule(); $conditions = array(); $conditions['publication'] = $this->get_instance()->id; $filesforzipping = array(); $fs = get_file_storage(); $filearea = 'attachment'; // Find out current groups mode. $groupmode = groups_get_activity_groupmode($this->get_coursemodule()); $currentgroup = groups_get_activity_group($this->get_coursemodule(), true); // Get group name for filename. $groupname = ''; // Get all ppl that are allowed to submit assignments. list($esql, $params) = get_enrolled_sql($context, 'mod/publication:view', $currentgroup); $showall = false; if (has_capability('mod/publication:approve', $context) || has_capability('mod/publication:grantextension', $context)) { $showall = true; } if (is_array($users) && count($users) > 0) { $customusers = " and u.id IN (" . implode($users, ', ') . ") "; } else { if ($users == false) { $customusers = " AND 1=2 "; } } if ($showall) { $sql = 'SELECT u.id FROM {user} u ' . 'LEFT JOIN (' . $esql . ') eu ON eu.id=u.id ' . 'WHERE u.deleted = 0 AND eu.id=u.id ' . $customusers; } else { $sql = 'SELECT u.id FROM {user} u ' . 'LEFT JOIN (' . $esql . ') eu ON eu.id=u.id ' . 'LEFT JOIN {publication_file} files ON (u.id = files.userid) ' . 'WHERE u.deleted = 0 AND eu.id=u.id ' . $customusers . 'AND files.publication = ' . $this->get_instance()->id . ' '; if ($this->get_instance()->mode == PUBLICATION_MODE_UPLOAD) { // Mode upload. if ($this->get_instance()->obtainteacherapproval) { // Need teacher approval. $where = 'files.teacherapproval = 1'; } else { // No need for teacher approval. // Teacher only hasnt rejected. $where = '(files.teacherapproval = 1 OR files.teacherapproval IS NULL)'; } } else { // Mode import. if (!$this->get_instance()->obtainstudentapproval) { // No need to ask student and teacher has approved. $where = 'files.teacherapproval = 1'; } else { // Student and teacher have approved. $where = 'files.teacherapproval = 1 AND files.studentapproval = 1'; } } $sql .= 'AND ' . $where . ' '; $sql .= 'GROUP BY u.id'; } $users = $DB->get_records_sql($sql, $params); if (!empty($users)) { $users = array_keys($users); } // If groupmembersonly used, remove users who are not in any group. if ($users and !empty($CFG->enablegroupmembersonly) and $cm->groupmembersonly) { if ($groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id')) { $users = array_intersect($users, array_keys($groupingusers)); } } $filename = str_replace(' ', '_', clean_filename($this->course->shortname . '-' . $this->get_instance()->name . '-' . $groupname . $this->get_instance()->id . '.zip')); // Name of new zip file. $userfields = get_all_user_name_fields(); $userfields['id'] = 'id'; $userfields['username'] = '******'; $userfields = implode(', ', $userfields); $viewfullnames = has_capability('moodle/site:viewfullnames', $this->context); // Get all files from each user. foreach ($users as $uploader) { $auserid = $uploader; $conditions['userid'] = $uploader; $records = $DB->get_records('publication_file', $conditions); $aassignid = $auserid; // Get name of this assignment for use in the file names. // Get user firstname/lastname. $auser = $DB->get_record('user', array('id' => $auserid), $userfields); foreach ($records as $record) { if (has_capability('mod/publication:approve', $this->get_context()) || $this->has_filepermission($record->fileid)) { // Is teacher or file is public. $file = $fs->get_file_by_id($record->fileid); // Get files new name. $fileext = strstr($file->get_filename(), '.'); $fileoriginal = str_replace($fileext, '', $file->get_filename()); $fileforzipname = clean_filename(($viewfullnames ? fullname($auser) : '') . '_' . $fileoriginal . '_' . $auserid . $fileext); // Save file name to array for zipping. $filesforzipping[$fileforzipname] = $file; } } } // End of foreach. if ($zipfile = $this->pack_files($filesforzipping)) { send_temp_file($zipfile, $filename); // Send file and delete after sending. } }
function watermark_and_sent(stdClass $issuedcert) { global $CFG, $USER, $COURSE, $DB, $PAGE; if ($issuedcert->haschange) { //This issue have a haschange flag, try to reissue if (empty($issuedcert->timedeleted)) { require_once $CFG->dirroot . '/mod/simplecertificate/locallib.php'; try { // Try to get cm $cm = get_coursemodule_from_instance('simplecertificate', $issuedcert->certificateid, 0, false, MUST_EXIST); $context = context_module::instance($cm->id); //Must set a page context to issue .... $PAGE->set_context($context); $simplecertificate = new simplecertificate($context, null, null); $file = $simplecertificate->get_issue_file($issuedcert); } catch (moodle_exception $e) { // Only debug, no errors debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace()); } } else { //Have haschange and timedeleted, somehting wrong, it will be impossible to reissue //add wraning debugging("issued certificate [{$issuedcert->id}], have haschange and timedeleted"); } $issuedcert->haschange = 0; $DB->update_record('simplecertificate_issues', $issuedcert); } if (empty($file)) { $fs = get_file_storage(); if (!$fs->file_exists_by_hash($issuedcert->pathnamehash)) { print_error(get_string('filenotfound', 'simplecertificate', '')); } $file = $fs->get_file_by_hash($issuedcert->pathnamehash); } $canmanage = false; if (!empty($COURSE)) { $canmanage = has_capability('mod/simplecertificate:manage', context_course::instance($COURSE->id)); } if ($canmanage || !empty($USER) && $USER->id == $issuedcert->userid) { send_stored_file($file, 0, 0, true); } else { require_once $CFG->libdir . '/pdflib.php'; require_once $CFG->dirroot . '/mod/simplecertificate/lib/fpdi/fpdi.php'; // copy to a tmp file $tmpfile = $file->copy_content_to_temp(); // TCPF doesn't import files yet, so i must use FPDI $pdf = new FPDI(); $pageCount = $pdf->setSourceFile($tmpfile); for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { // import a page $templateId = $pdf->importPage($pageNo); // get the size of the imported page $size = $pdf->getTemplateSize($templateId); // create a page (landscape or portrait depending on the imported page size) if ($size['w'] > $size['h']) { $pdf->AddPage('L', array($size['w'], $size['h'])); // Font size 1/3 Height if it landscape $fontsize = $size['h'] / 3; } else { $pdf->AddPage('P', array($size['w'], $size['h'])); // Font size 1/3 Width if it portrait $fontsize = $size['w'] / 3; } // use the imported page $pdf->useTemplate($templateId); // Calculating the rotation angle $rotAngle = atan($size['h'] / $size['w']) * 180 / pi(); // Find the middle of the page to use as a pivot at rotation. $mX = $size['w'] / 2; $mY = $size['h'] / 2; // Set the transparency of the text to really light $pdf->SetAlpha(0.25); $pdf->StartTransform(); $pdf->Rotate($rotAngle, $mX, $mY); $pdf->SetFont("freesans", "B", $fontsize); $pdf->SetXY(0, $mY); $boder_style = array('LTRB' => array('width' => 2, 'dash' => $fontsize / 5, 'cap' => 'round', 'join' => 'round', 'phase' => $fontsize / $mX)); $pdf->Cell($size['w'], $fontsize, get_string('certificatecopy', 'simplecertificate'), $boder_style, 0, 'C', false, '', 4, true, 'C', 'C'); $pdf->StopTransform(); // Reset the transparency to default $pdf->SetAlpha(1); } // Set protection seems not work, so comment // $pdf->SetProtection(array('print', 'modify', 'print-high'),null, random_string(), '1',null); // For DEGUG // $pdf->Output($file->get_filename(), 'I'); // Save and send tmpfiles $pdf->Output($tmpfile, 'F'); send_temp_file($tmpfile, $file->get_filename()); } }
/** * 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; }
if (0 === strpos($filename, 'form-')) { $path = get_string('questionforms', 'offlinequiz'); } else { if (0 === strpos($filename, 'answer-')) { $path = get_string('answerforms', 'offlinequiz'); } else { $path = get_string('correctionforms', 'offlinequiz'); } } $path = clean_filename($path); $filelist[$path . '/' . $filename] = $file; } } $zipper = new zip_packer(); if ($zipper->archive_to_pathname($filelist, $tempzip)) { send_temp_file($tempzip, $zipfilename); } } // Print the page header. echo $OUTPUT->header(); // Print the offlinequiz name heading and tabs for teacher. $currenttab = 'createofflinequiz'; require 'tabs.php'; $hasscannedpages = offlinequiz_has_scanned_pages($offlinequiz->id); if ($offlinequiz->grade == 0) { echo '<div class="linkbox"><strong>'; echo $OUTPUT->notification(get_string('gradeiszero', 'offlinequiz'), 'notifyproblem'); echo '</strong></div>'; } // Preview. if ($mode == 'preview') {
/** * creates a zip of all assignment submissions and sends a zip to the browser */ public function download_submissions() { global $CFG, $DB; raise_memory_limit(MEMORY_EXTRA); $submissions = $this->get_submissions('', ''); if (empty($submissions)) { print_error('errornosubmissions', 'assignment'); } $filesforzipping = array(); //NOTE: do not create any stuff in temp directories, we now support unicode file names and that would not work, sorry //online assignment can use html $filextn = ".html"; $groupmode = groups_get_activity_groupmode($this->cm); $groupid = 0; // All users $groupname = ''; if ($groupmode) { $groupid = groups_get_activity_group($this->cm, true); $groupname = groups_get_group_name($groupid) . '-'; } $filename = str_replace(' ', '_', clean_filename($this->course->shortname . '-' . $this->assignment->name . '-' . $groupname . $this->assignment->id . ".zip")); //name of new zip file. foreach ($submissions as $submission) { $a_userid = $submission->userid; //get userid if (groups_is_member($groupid, $a_userid) or !$groupmode or !$groupid) { $a_assignid = $submission->assignment; //get name of this assignment for use in the file names. $a_user = $DB->get_record("user", array("id" => $a_userid), 'id,username,firstname,lastname'); //get user firstname/lastname $submissioncontent = "<html><body>" . format_text($submission->data1, $submission->data2) . "</body></html>"; //fetched from database //get file name.html $fileforzipname = clean_filename(fullname($a_user) . "_" . $a_userid . $filextn); $filesforzipping[$fileforzipname] = array($submissioncontent); } } //end of foreach if ($zipfile = assignment_pack_files($filesforzipping)) { send_temp_file($zipfile, $filename); //send file and delete after sending. } }
/** * Download a zip file of all assignment submissions * * @return void */ private function download_submissions() { global $CFG, $DB; // more efficient to load this here require_once $CFG->libdir . '/filelib.php'; // load all submissions $submissions = $this->get_all_submissions('', ''); if (empty($submissions)) { print_error('errornosubmissions', 'assign'); return; } // build a list of files to zip $filesforzipping = array(); $fs = get_file_storage(); $groupmode = groups_get_activity_groupmode($this->get_course_module()); $groupid = 0; // All users $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 = str_replace(' ', '_', clean_filename($this->get_course()->shortname . '-' . $this->get_instance()->name . '-' . $groupname . $this->get_course_module()->id . ".zip")); //name of new zip file. // get all the files for each submission foreach ($submissions as $submission) { $userid = $submission->userid; //get userid if (groups_is_member($groupid, $userid) or !$groupmode or !$groupid) { // get the plugins to add their own files to the zip $prefix = clean_filename(fullname($submission) . "_" . $userid . "_"); foreach ($this->submissionplugins as $plugin) { if ($plugin->is_enabled() && $plugin->is_visible()) { $pluginfiles = $plugin->get_files($submission); foreach ($pluginfiles as $zipfilename => $file) { $filesforzipping[$prefix . $zipfilename] = $file; } } } } } // end of foreach loop if ($zipfile = $this->pack_files($filesforzipping)) { $this->add_to_log('download all submissions', get_string('downloadall', 'assign')); send_temp_file($zipfile, $filename); //send file and delete after sending. } }
/** * 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; }
/** * Creates an entry for an report * * @copyright © 2011 University of London Computer Centre * @author http://www.ulcc.ac.uk, http://moodle.ulcc.ac.uk, * @author Greg Pasciak * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package ILP * @version 2.0 */ require '../../../config.php'; global $CFG, $USER; require_once $CFG->dirroot . '/lib/filelib.php'; require_once $CFG->dirroot . '/blocks/ilp/actions_includes.php'; //require_login(); if ($access_viewilp || $access_viewotherilp) { $queryStr = $_SERVER["QUERY_STRING"]; $args = explode('/', ltrim($queryStr, '/')); $dir = $args[0]; $fileName = $args[1]; //copy file to the sitedata/temp folder, send and delete temp file if ($dir) { $ilp_filesDir = str_replace('/docroot', "", $CFG->dirroot) . "/ilp_files"; $ilp_filesDir = str_replace('\\docroot', "", $ilp_filesDir); // on Win server copy($ilp_filesDir . '/' . $dir . '/' . $fileName, $CFG->dataroot . '/temp/' . $fileName); $temppath = $CFG->dataroot . '/temp/' . $fileName; send_temp_file($temppath, $fileName, false); } }