function user_download_ods($fields) { global $CFG, $SESSION, $DB; require_once "{$CFG->libdir}/odslib.class.php"; require_once $CFG->dirroot . '/user/profile/lib.php'; $filename = clean_filename(get_string('users') . '.ods'); $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); $worksheet[0] =& $workbook->add_worksheet(''); $col = 0; foreach ($fields as $fieldname) { $worksheet[0]->write(0, $col, $fieldname); $col++; } $row = 1; foreach ($SESSION->bulk_users as $userid) { if (!($user = $DB->get_record('user', array('id' => $userid)))) { continue; } $col = 0; profile_load_data($user); foreach ($fields as $field => $unused) { $worksheet[0]->write($row, $col, $user->{$field}); $col++; } $row++; } $workbook->close(); die; }
function user_download_ods($fields, $extrafields = array()) { global $CFG, $SESSION, $DB; require_once "{$CFG->libdir}/odslib.class.php"; require_once $CFG->dirroot . '/user/profile/lib.php'; $filename = clean_filename(get_string('users') . '.ods'); $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); $worksheet[0] = $workbook->add_worksheet(''); $col = 0; foreach ($fields as $fieldname) { $worksheet[0]->write(0, $col, $fieldname); $col++; } $extrafield_sql = ''; foreach ($extrafields as $n => $v) { $extrafield_sql .= " MAX(IF(muif.shortname = '" . $v->shortname . "', muid.data, NULL)) profile_field_" . $v->shortname . ","; } $extrafield_sql = rtrim($extrafield_sql, ","); $idstoload = $SESSION->bulk_users; $users = array(); foreach (array_chunk($idstoload, 10000, true) as $user_id) { $userids = implode(",", $user_id); $users[] = $DB->get_records_sql("SELECT mu.*,\n {$extrafield_sql}\n FROM mdl_user AS mu\n LEFT JOIN mdl_user_info_data AS muid ON mu.id = muid.userid\n LEFT JOIN mdl_user_info_field AS muif ON muif.id = muid.fieldid\n WHERE mu.id IN ({$userids}) GROUP BY mu.id"); } $row = 1; foreach ($users as $userdetails) { foreach ($userdetails as $user) { #if (!$user = $DB->get_record('user', array('id'=>$userid))) { #continue; #} $col = 0; #profile_load_data($user,$userfields); foreach ($fields as $field => $unused) { $worksheet[0]->write($row, $col, $user->{$field}); $col++; } $row++; } } $workbook->close(); die; }
/** Configurable Reports * A Moodle block for creating customizable reports * @package blocks * @author: Juan leyva <http://www.twitter.com/jleyvadelgado> * @date: 2009 */ function export_report($report) { global $DB, $CFG; require_once($CFG->dirroot . '/lib/odslib.class.php'); $table = $report->table; $matrix = array(); $filename = 'report_' . (time()) . '.ods'; if (!empty($table->head)) { $countcols = count($table->head); $keys = array_keys($table->head); $lastkey = end($keys); foreach ($table->head as $key => $heading) { $matrix[0][$key] = str_replace("\n", ' ', htmlspecialchars_decode(strip_tags(nl2br($heading)))); } } if (!empty($table->data)) { foreach ($table->data as $rkey => $row) { foreach ($row as $key => $item) { $matrix[$rkey + 1][$key] = str_replace("\n", ' ', htmlspecialchars_decode(strip_tags(nl2br($item)))); } } } $downloadfilename = clean_filename($filename); /// Creating a workbook $workbook = new MoodleODSWorkbook("-"); /// Sending HTTP headers $workbook->send($downloadfilename); /// Adding the worksheet $myxls = & $workbook->add_worksheet($filename); foreach ($matrix as $ri => $col) { foreach ($col as $ci => $cv) { $myxls->write_string($ri, $ci, $cv); } } $workbook->close(); exit; }
if ($usergrps = groups_get_all_groups($course->id, $user->id)) { foreach ($usergrps as $ug) { $ug2 = $ug2 . $ug->name; } } $myxls->write_string($row, 3, $ug2); if (isset($option_text)) { $myxls->write_string($row, 4, format_string($option_text, true)); } $row++; $pos = 4; } } } /// Close the workbook $workbook->close(); exit; } //print spreadsheet if one is asked for: if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) { require_once "{$CFG->libdir}/excellib.class.php"; /// Calculate file name $filename = clean_filename("{$course->shortname} " . strip_tags(format_string($choice->name, true))) . '.xls'; /// Creating a workbook $workbook = new MoodleExcelWorkbook("-"); /// Send HTTP headers $workbook->send($filename); /// Creating the first worksheet $myxls = $workbook->add_worksheet($strresponses); /// Print names of all the fields $myxls->write_string(0, 0, get_string("lastname"));
function Export_ODS(&$questions, $filename) { global $CFG; require_once "{$CFG->libdir}/odslib.class.php"; /// Calculate file name $filename .= ".ods"; /// Creating a workbook $workbook = new MoodleODSWorkbook("-"); /// Sending HTTP headers $workbook->send($filename); /// Creating the first worksheet $sheettitle = get_string('reportanalysis', 'quiz_analysis'); $myxls =& $workbook->add_worksheet($sheettitle); /// format types $format =& $workbook->add_format(); $format->set_bold(0); $formatbc =& $workbook->add_format(); $formatbc->set_bold(1); $formatb =& $workbook->add_format(); $formatb->set_bold(1); $formaty =& $workbook->add_format(); $formaty->set_bg_color('yellow'); $formatyc =& $workbook->add_format(); $formatyc->set_bg_color('yellow'); //bold text on yellow bg $formatyc->set_bold(1); $formatyc->set_align('center'); $formatc =& $workbook->add_format(); $formatc->set_align('center'); $formatbc->set_align('center'); $formatbpct =& $workbook->add_format(); $formatbpct->set_bold(1); $formatbpct->set_num_format('0.0%'); $formatbrt =& $workbook->add_format(); $formatbrt->set_bold(1); $formatbrt->set_align('right'); $formatred =& $workbook->add_format(); $formatred->set_bold(1); $formatred->set_color('red'); $formatred->set_align('center'); $formatblue =& $workbook->add_format(); $formatblue->set_bold(1); $formatblue->set_color('blue'); $formatblue->set_align('center'); /// Here starts workshhet headers $myxls->write_string(0, 0, $sheettitle, $formatb); $headers = array(get_string('qidtitle', 'quiz_analysis'), get_string('qtypetitle', 'quiz_analysis'), get_string('qnametitle', 'quiz_analysis'), get_string('qtexttitle', 'quiz_analysis'), get_string('responsestitle', 'quiz_analysis'), get_string('rfractiontitle', 'quiz_analysis'), get_string('rcounttitle', 'quiz_analysis'), get_string('rpercenttitle', 'quiz_analysis'), get_string('qcounttitle', 'quiz_analysis'), get_string('facilitytitle', 'quiz_analysis'), get_string('stddevtitle', 'quiz_analysis'), get_string('dicsindextitle', 'quiz_analysis'), get_string('disccoefftitle', 'quiz_analysis')); foreach ($headers as $key => $header) { $headers[$key] = preg_replace('/<br[^>]*>/', ' ', $header); } $col = 0; foreach ($headers as $item) { $myxls->write(2, $col, $item, $formatbc); $col++; } $row = 3; foreach ($questions as $q) { $rows = $this->print_row_stats_data($q); foreach ($rows as $rowdata) { $col = 0; foreach ($rowdata as $item) { $myxls->write($row, $col, $item, $format); $col++; } $row++; } } /// Close the workbook $workbook->close(); exit; }
function exporttotableed($data, $filename, $format) { global $CFG; if ($format === 'excel') { require_once "{$CFG->libdir}/excellib.class.php"; $filename .= ".xls"; $workbook = new MoodleExcelWorkbook("-"); } else { require_once "{$CFG->libdir}/odslib.class.php"; $filename .= ".ods"; $workbook = new MoodleODSWorkbook("-"); } // Sending HTTP headers. $workbook->send($filename); // Creating the first worksheet. $myxls = $workbook->add_worksheet('Attendances'); // Format types. $formatbc = $workbook->add_format(); $formatbc->set_bold(1); $myxls->write(0, 0, get_string('course'), $formatbc); $myxls->write(0, 1, $data->course); $myxls->write(1, 0, get_string('group'), $formatbc); $myxls->write(1, 1, $data->group); $i = 3; $j = 0; foreach ($data->tabhead as $cell) { $myxls->write($i, $j++, $cell, $formatbc); } $i++; $j = 0; foreach ($data->table as $row) { foreach ($row as $cell) { $myxls->write($i, $j++, $cell); } $i++; $j = 0; } $workbook->close(); }
/** * @global object * @param array $export * @param string $dataname * @param int $count * @param string */ function data_export_ods($export, $dataname, $count) { global $CFG; require_once("$CFG->libdir/odslib.class.php"); $filename = clean_filename("{$dataname}-{$count}_record"); if ($count > 1) { $filename .= 's'; } $filename .= clean_filename('-' . gmdate("Ymd_Hi")); $filename .= '.ods'; $filearg = '-'; $workbook = new MoodleODSWorkbook($filearg); $workbook->send($filename); $worksheet = array(); $worksheet[0] =& $workbook->add_worksheet(''); $rowno = 0; foreach ($export as $row) { $colno = 0; foreach($row as $col) { $worksheet[0]->write($rowno, $colno, $col); $colno++; } $rowno++; } $workbook->close(); return $filename; }
/** Download data in ODS format * * @param array $fields Array of column headings * @param string $datarows Array of data to populate table with * @param string $file Name of file for exportig * @return Returns the ODS file */ function facetoface_download_ods($fields, $datarows, $file=null) { global $CFG, $DB; require_once("$CFG->libdir/odslib.class.php"); $filename = clean_filename($file . '.ods'); header("Content-Type: application/download\n"); header("Content-Disposition: attachment; filename=$filename"); header("Expires: 0"); header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); header("Pragma: public"); $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); $worksheet[0] = $workbook->add_worksheet(''); $row = 0; $col = 0; foreach ($fields as $field) { $worksheet[0]->write($row, $col, strip_tags($field)); $col++; } $row++; $numfields = count($fields); foreach ($datarows as $record) { for($col=0; $col<$numfields; $col++) { if (isset($record[$col])) { $worksheet[0]->write($row, $col, html_entity_decode($record[$col], ENT_COMPAT, 'UTF-8')); } } $row++; } $workbook->close(); die; }
function print_log_ods($course, $user, $date, $order = 'l.time DESC', $modname, $modid, $modaction, $groupid) { global $CFG, $DB; require_once "{$CFG->libdir}/odslib.class.php"; if (!($logs = build_logs_array($course, $user, $date, $order, '', '', $modname, $modid, $modaction, $groupid))) { return false; } $courses = array(); if ($course->id == SITEID) { $courses[0] = ''; if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) { foreach ($ccc as $cc) { $courses[$cc->id] = $cc->shortname; } } } else { $courses[$course->id] = $course->shortname; } $count = 0; $ldcache = array(); $tt = getdate(time()); $today = mktime(0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $strftimedatetime = get_string("strftimedatetime"); $nroPages = ceil(count($logs) / (EXCELROWS - FIRSTUSEDEXCELROW + 1)); $filename = 'logs_' . userdate(time(), get_string('backupnameformat', 'langconfig'), 99, false); $filename .= '.ods'; $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); $headers = array(get_string('course'), get_string('time'), get_string('ip_address'), get_string('fullnameuser'), get_string('action'), get_string('info')); // Creating worksheets for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) { $sheettitle = get_string('logs') . ' ' . $wsnumber . '-' . $nroPages; $worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle); $worksheet[$wsnumber]->set_column(1, 1, 30); $worksheet[$wsnumber]->write_string(0, 0, get_string('savedat') . userdate(time(), $strftimedatetime)); $col = 0; foreach ($headers as $item) { $worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW - 1, $col, $item, ''); $col++; } } if (empty($logs['logs'])) { $workbook->close(); return true; } $formatDate =& $workbook->add_format(); $formatDate->set_num_format(get_string('log_excel_date_format')); $row = FIRSTUSEDEXCELROW; $wsnumber = 1; $myxls =& $worksheet[$wsnumber]; foreach ($logs['logs'] as $log) { if (isset($ldcache[$log->module][$log->action])) { $ld = $ldcache[$log->module][$log->action]; } else { $ld = $DB->get_record('log_display', array('module' => $log->module, 'action' => $log->action)); $ldcache[$log->module][$log->action] = $ld; } if ($ld && !empty($log->info)) { // ugly hack to make sure fullname is shown correctly if ($ld->mtable == 'user' and $ld->field == $DB->sql_concat('firstname', "' '", 'lastname')) { $log->info = fullname($DB->get_record($ld->mtable, array('id' => $log->info)), true); } else { $log->info = $DB->get_field($ld->mtable, $ld->field, array('id' => $log->info)); } } // Filter log->info $log->info = format_string($log->info); $log->info = strip_tags(urldecode($log->info)); // Some XSS protection if ($nroPages > 1) { if ($row > EXCELROWS) { $wsnumber++; $myxls =& $worksheet[$wsnumber]; $row = FIRSTUSEDEXCELROW; } } $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $myxls->write_string($row, 0, format_string($courses[$log->course], true, array('context' => $coursecontext))); $myxls->write_date($row, 1, $log->time); $myxls->write_string($row, 2, $log->ip); $fullname = fullname($log, has_capability('moodle/site:viewfullnames', $coursecontext)); $myxls->write_string($row, 3, $fullname); $myxls->write_string($row, 4, $log->module . ' ' . $log->action); $myxls->write_string($row, 5, $log->info); $row++; } $workbook->close(); return true; }
/** Download data in ODS format * * @param array $fields Array of column headings * @param string $datarows Array of data to populate table with * @param string $file Name of file for exportig * @return Returns the ODS file */ function facetoface_download_ods($fields, $datarows, $file=null) { global $CFG, $DB; require_once("$CFG->libdir/odslib.class.php"); $filename = clean_filename($file . '.ods'); header("Content-Type: application/download\n"); header("Content-Disposition: attachment; filename=$filename"); header("Expires: 0"); header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); header("Pragma: public"); $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); if (!empty($fields)) { $countcols = count($fields); $keys = array_keys($fields); $lastkey = end($keys); foreach ($fields as $key => $heading) { $matrix[0][$key] = str_replace("\n", ' ', htmlspecialchars_decode(strip_tags(nl2br($heading)))); } } if (!empty($datarows)) { foreach ($datarows as $rkey => $row) { foreach ($row as $key => $item) { $matrix[$rkey + 1][$key] = str_replace("\n", ' ', htmlspecialchars_decode(strip_tags(nl2br($item)))); } } } $myxls = & $workbook->add_worksheet($filename); foreach ($matrix as $ri => $col) { foreach ($col as $ci => $cv) { $myxls->write_string($ri, $ci, $cv); } } //$worksheet[0] = $workbook->add_worksheet(''); //$row = 0; //$col = 0; // //foreach ($fields as $field) { // $myxls->write_string($ri, $ci, $cv); // $col++; //} //$row++; // //$numfields = count($fields); // //foreach ($datarows as $record) { // for($col=0; $col<$numfields; $col++) { // if (isset($record[$col])) { // $worksheet[0]->write($row, $col, html_entity_decode($record[$col], ENT_COMPAT, 'UTF-8')); // } // } // $row++; //} $workbook->close(); exit; }
/** * To be implemented by child classes */ function print_grades() { global $CFG; require_once $CFG->dirroot . '/lib/odslib.class.php'; $export_tracking = $this->track_exports(); $strgrades = get_string('grades'); $shortname = format_string($this->course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $this->course->id))); /// Calculate file name $downloadfilename = clean_filename("{$shortname} {$strgrades}.ods"); /// Creating a workbook $workbook = new MoodleODSWorkbook("-"); /// Sending HTTP headers $workbook->send($downloadfilename); /// Adding the worksheet $myxls = $workbook->add_worksheet($strgrades); /// Print names of all the fields $myxls->write_string(0, 0, get_string("firstname")); $myxls->write_string(0, 1, get_string("lastname")); $myxls->write_string(0, 2, get_string("idnumber")); $myxls->write_string(0, 3, get_string("institution")); $myxls->write_string(0, 4, get_string("department")); $myxls->write_string(0, 5, get_string("email")); $pos = 6; foreach ($this->columns as $grade_item) { $myxls->write_string(0, $pos++, $this->format_column_name($grade_item)); /// add a column_feedback column if ($this->export_feedback) { $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true)); } } /// Print all the lines of data. $i = 0; $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()) { $i++; $user = $userdata->user; $myxls->write_string($i, 0, $user->firstname); $myxls->write_string($i, 1, $user->lastname); $myxls->write_string($i, 2, $user->idnumber); $myxls->write_string($i, 3, $user->institution); $myxls->write_string($i, 4, $user->department); $myxls->write_string($i, 5, $user->email); $j = 6; foreach ($userdata->grades as $itemid => $grade) { if ($export_tracking) { $status = $geub->track($grade); } $gradestr = $this->format_grade($grade); if (is_numeric($gradestr)) { $myxls->write_number($i, $j++, $gradestr); } else { $myxls->write_string($i, $j++, $gradestr); } // writing feedback if requested if ($this->export_feedback) { $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid])); } } } $gui->close(); $geub->close(); /// Close the workbook $workbook->close(); exit; }
/** * To be implemented by child classes */ function print_grades() { global $CFG; require_once $CFG->dirroot . '/lib/odslib.class.php'; $export_tracking = $this->track_exports(); $strgrades = get_string('grades'); $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id))); // Calculate file name $downloadfilename = clean_filename("{$shortname} {$strgrades}.ods"); // Creating a workbook $workbook = new MoodleODSWorkbook("-"); // Sending HTTP headers $workbook->send($downloadfilename); // Adding the worksheet $myxls = $workbook->add_worksheet($strgrades); // Print names of all the fields. $profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields); foreach ($profilefields as $id => $field) { $myxls->write_string(0, $id, $field->fullname); } $pos = count($profilefields); if (!$this->onlyactive) { $myxls->write_string(0, $pos++, get_string("suspended")); } foreach ($this->columns as $grade_item) { $myxls->write_string(0, $pos++, $this->format_column_name($grade_item)); // Add a column_feedback column. if ($this->export_feedback) { $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true)); } } // Print all the lines of data. $i = 0; $geub = new grade_export_update_buffer(); $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid); $gui->require_active_enrolment($this->onlyactive); $gui->allow_user_custom_fields($this->usercustomfields); $gui->init(); while ($userdata = $gui->next_user()) { $i++; $user = $userdata->user; foreach ($profilefields as $id => $field) { $fieldvalue = grade_helper::get_user_field_value($user, $field); $myxls->write_string($i, $id, $fieldvalue); } $j = count($profilefields); if (!$this->onlyactive) { $issuspended = $user->suspendedenrolment ? get_string('yes') : ''; $myxls->write_string($i, $j++, $issuspended); } foreach ($userdata->grades as $itemid => $grade) { if ($export_tracking) { $status = $geub->track($grade); } $gradestr = $this->format_grade($grade); if (is_numeric($gradestr)) { $myxls->write_number($i, $j++, $gradestr); } else { $myxls->write_string($i, $j++, $gradestr); } // writing feedback if requested if ($this->export_feedback) { $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid])); } } } $gui->close(); $geub->close(); // Close the workbook. $workbook->close(); exit; }
public function view_issued_certificates(moodle_url $url) { global $OUTPUT, $DB, $CFG; // Declare some variables $strcertificates = get_string('modulenameplural', 'simplecertificate'); $strcertificate = get_string('modulename', 'simplecertificate'); $strto = html_writer::link($url->out(false, array('orderby' => 'username')), get_string('awardedto', 'simplecertificate')); $strdate = html_writer::link($url->out(false, array('orderby' => 'issuedate')), get_string('receiveddate', 'simplecertificate')); $strgrade = get_string('grade', 'simplecertificate'); $strcode = get_string('code', 'simplecertificate'); $strreport = get_string('report', 'simplecertificate'); $groupmode = groups_get_activity_groupmode($this->get_course_module()); $page = $url->get_param('page'); $perpage = $url->get_param('perpage'); $orderby = $url->get_param('orderby'); $usercount = 0; $users = $this->get_issued_certificate_users($orderby, $groupmode); if ($users) { $usercount = count($users); $users = array_slice($users, intval($page * $perpage), $perpage); } if (!$url->get_param('action')) { echo $OUTPUT->header(); $this->show_tabs($url); if ($groupmode) { groups_get_activity_group($this->coursemodule, true); } groups_print_activity_menu($this->coursemodule, $url); if (!$users) { notify(get_string('nocertificatesissued', 'simplecertificate')); echo $OUTPUT->footer(); exit; } // Create the table for the users $table = new html_table(); $table->width = "95%"; $table->tablealign = "center"; $table->head = array($strto, $strdate, $strgrade, $strcode); $table->align = array("left", "left", "center", "center"); foreach ($users as $user) { $name = $OUTPUT->user_picture($user) . fullname($user); $date = userdate($user->timecreated) . simplecertificate_print_issue_certificate_file($this->get_issue($user)); $code = $user->code; $table->data[] = array($name, $date, $this->get_grade($user->id), $code); } // Create table to store buttons $tablebutton = new html_table(); $tablebutton->attributes['class'] = 'downloadreport'; $btndownloadods = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download', 'type' => 'ods')), get_string("downloadods")); $btndownloadxls = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download', 'type' => 'xls')), get_string("downloadexcel")); $btndownloadtxt = $OUTPUT->single_button($url->out_as_local_url(false, array('action' => 'download', 'type' => 'txt')), get_string("downloadtext")); $tablebutton->data[] = array($btndownloadods, $btndownloadxls, $btndownloadtxt); echo $OUTPUT->paging_bar($usercount, $page, $perpage, $url); echo '<br />'; echo html_writer::table($table); echo html_writer::tag('div', html_writer::table($tablebutton), array('style' => 'margin:auto; width:50%')); } else { if ($url->get_param('action') == 'download') { $page = $perpage = 0; $type = $url->get_param('type'); // Calculate file name $filename = clean_filename($this->get_instance()->coursename . '-' . strip_tags(format_string($this->get_instance()->name, true)) . '.' . strip_tags(format_string($type, true))); switch ($type) { case 'ods': require_once "{$CFG->libdir}/odslib.class.php"; // Creating a workbook $workbook = new MoodleODSWorkbook("-"); // Send HTTP headers $workbook->send(format_text($filename, true)); // Creating the first worksheet $myxls = $workbook->add_worksheet($strreport); // Print names of all the fields $myxls->write_string(0, 0, get_string("fullname")); $myxls->write_string(0, 1, get_string("idnumber")); $myxls->write_string(0, 2, get_string("group")); $myxls->write_string(0, 3, $strdate); $myxls->write_string(0, 4, $strgrade); $myxls->write_string(0, 5, $strcode); // Generate the data for the body of the spreadsheet $i = 0; $row = 1; if ($users) { foreach ($users as $user) { $myxls->write_string($row, 0, fullname($user)); $studentid = !empty($user->idnumber) ? $user->idnumber : " "; $myxls->write_string($row, 1, $studentid); $ug2 = ''; if ($usergrps = groups_get_all_groups($this->get_course()->id, $user->id)) { foreach ($usergrps as $ug) { $ug2 = $ug2 . $ug->name; } } $myxls->write_string($row, 2, $ug2); $myxls->write_string($row, 3, userdate($user->timecreated)); $myxls->write_string($row, 4, $this->get_grade($user->id)); $myxls->write_string($row, 5, $user->code); $row++; } $pos = 5; } // Close the workbook $workbook->close(); break; case 'xls': require_once "{$CFG->libdir}/excellib.class.php"; // Creating a workbook $workbook = new MoodleExcelWorkbook("-"); // Send HTTP headers $workbook->send(format_text($filename, true)); // Creating the first worksheet $myxls = $workbook->add_worksheet($strreport); // Print names of all the fields $myxls->write_string(0, 0, get_string("fullname")); $myxls->write_string(0, 1, get_string("idnumber")); $myxls->write_string(0, 2, get_string("group")); $myxls->write_string(0, 3, $strdate); $myxls->write_string(0, 4, $strgrade); $myxls->write_string(0, 5, $strcode); // Generate the data for the body of the spreadsheet $i = 0; $row = 1; if ($users) { foreach ($users as $user) { $myxls->write_string($row, 0, fullname($user)); $studentid = !empty($user->idnumber) ? $user->idnumber : " "; $myxls->write_string($row, 1, $studentid); $ug2 = ''; if ($usergrps = groups_get_all_groups($this->get_course()->id, $user->id)) { foreach ($usergrps as $ug) { $ug2 = $ug2 . $ug->name; } } $myxls->write_string($row, 2, $ug2); $myxls->write_string($row, 3, userdate($user->timecreated)); $myxls->write_string($row, 4, $this->get_grade($user->id)); $myxls->write_string($row, 5, $user->code); $row++; } $pos = 5; } // Close the workbook $workbook->close(); break; case 'txt': header("Content-Type: application/download\n"); header("Content-Disposition: attachment; filename=\"" . format_text($filename, true) . "\""); header("Expires: 0"); header("Cache-Control: must-revalidate,post-check=0,pre-check=0"); header("Pragma: public"); // Print names of all the fields echo get_string("fullname") . "\t" . get_string("idnumber") . "\t"; echo get_string("group") . "\t"; echo $strdate . "\t"; echo $strgrade . "\t"; echo $strcode . "\n"; // Generate the data for the body of the spreadsheet $i = 0; $row = 1; if ($users) { foreach ($users as $user) { echo fullname($user); $studentid = " "; if (!empty($user->idnumber)) { $studentid = $user->idnumber; } echo "\t" . $studentid . "\t"; $ug2 = ''; if ($usergrps = groups_get_all_groups($this->get_course()->id, $user->id)) { foreach ($usergrps as $ug) { $ug2 = $ug2 . $ug->name; } } echo $ug2 . "\t"; echo userdate($user->timecreated) . "\t"; echo $this->get_grade($user->id) . "\t"; echo $user->code . "\n"; $row++; } } break; } exit; } } echo $OUTPUT->footer(); }
function user_download_ods($userids, $fields, $includecompanyfield) { global $CFG, $SESSION, $DB; require_once "{$CFG->libdir}/odslib.class.php"; require_once $CFG->dirroot . '/user/profile/lib.php'; $filename = clean_filename(get_string('users') . '.ods'); $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); $worksheet[0] = $workbook->add_worksheet(''); $col = 0; foreach ($fields as $fieldname) { if ($includecompanyfield || $fieldname != "profile_field_company") { $worksheet[0]->write(0, $col, $fieldname); $col++; } } $worksheet[0]->write(0, $col, 'temppassword'); $row = 1; foreach ($userids as $userid) { // Stop the script from timing out on large numbers of users. set_time_limit(30); if (!($user = $DB->get_record('user', array('id' => $userid)))) { continue; } $col = 0; profile_load_data($user); foreach ($fields as $field => $unused) { // Stop the script from timing out on large numbers of users. set_time_limit(30); if ($includecompanyfield || $field != "profile_field_company") { $worksheet[0]->write($row, $col, $user->{$field}); $col++; } } $worksheet[0]->write($row, $col, company_user::get_temporary_password($user)); $row++; } $workbook->close(); die; }
function grade_download($download, $id) { global $CFG; require_login(); if (!($course = get_record("course", "id", $id))) { error("Course ID was incorrect"); } require_capability('moodle/course:viewcoursegrades', get_context_instance(CONTEXT_COURSE, $id)); $strgrades = get_string("grades"); $strgrade = get_string("grade"); $strmax = get_string("maximumshort"); $stractivityreport = get_string("activityreport"); /// Check to see if groups are being used in this course $currentgroup = get_current_group($course->id); if (($currentgroup = get_current_group($course->id)) && groupmode($course) != 0) { $students = get_group_students($currentgroup, "u.lastname ASC"); } else { $students = grade_get_course_students($course->id); } if (!empty($students)) { foreach ($students as $student) { $grades[$student->id] = array(); // Collect all grades in this array $gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array $totals[$student->id] = array(); // Collect all totals in this array } } $columns = array(); // Accumulate column names in this array. $columnhtml = array(); // Accumulate column html in this array. /// Collect modules data get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused); /// Search through all the modules, pulling out grade data $sections = get_all_sections($course->id); // Sort everything the same as the course for ($i = 0; $i <= $course->numsections; $i++) { if (isset($sections[$i])) { // should always be true $section = $sections[$i]; if ($section->sequence) { $sectionmods = explode(",", $section->sequence); foreach ($sectionmods as $sectionmod) { $mod = $mods[$sectionmod]; $instance = get_record("{$mod->modname}", "id", "{$mod->instance}"); $libfile = "{$CFG->dirroot}/mod/{$mod->modname}/lib.php"; if (file_exists($libfile)) { require_once $libfile; $gradefunction = $mod->modname . "_grades"; if (function_exists($gradefunction)) { // Skip modules without grade function if ($modgrades = $gradefunction($mod->instance)) { if (!empty($modgrades->maxgrade)) { if ($mod->visible) { $maxgrade = "{$strmax}: {$modgrades->maxgrade}"; } else { $maxgrade = "{$strmax}: {$modgrades->maxgrade}"; } } else { $maxgrade = ""; } $columns[] = "{$mod->modfullname}: " . format_string($instance->name, true) . " - {$maxgrade}"; if (!empty($students)) { foreach ($students as $student) { if (!empty($modgrades->grades[$student->id])) { $grades[$student->id][] = $currentstudentgrade = $modgrades->grades[$student->id]; } else { $grades[$student->id][] = $currentstudentgrade = ""; $gradeshtml[$student->id][] = ""; } if (!empty($modgrades->maxgrade)) { $totals[$student->id] = (double) $totals[$student->id] + (double) $currentstudentgrade; } else { $totals[$student->id] = (double) $totals[$student->id] + 0; } } } } } } } } } } // a new Moodle nesting record? ;-) /// OK, we have all the data, now present it to the user /// OK, we have all the data, now present it to the user if ($download == "ods" and confirm_sesskey()) { require_once "../lib/odslib.class.php"; /// Calculate file name $downloadfilename = clean_filename("{$course->shortname} {$strgrades}.ods"); /// Creating a workbook $workbook = new MoodleODSWorkbook("-"); /// Sending HTTP headers $workbook->send($downloadfilename); /// Adding the worksheet $myxls =& $workbook->add_worksheet($strgrades); /// Print names of all the fields $myxls->write_string(0, 0, get_string("firstname")); $myxls->write_string(0, 1, get_string("lastname")); $myxls->write_string(0, 2, get_string("idnumber")); $myxls->write_string(0, 3, get_string("institution")); $myxls->write_string(0, 4, get_string("department")); $myxls->write_string(0, 5, get_string("email")); $pos = 6; foreach ($columns as $column) { $myxls->write_string(0, $pos++, strip_tags($column)); } $myxls->write_string(0, $pos, get_string("total")); /// Print all the lines of data. $i = 0; if (!empty($grades)) { foreach ($grades as $studentid => $studentgrades) { $i++; $student = $students[$studentid]; if (empty($totals[$student->id])) { $totals[$student->id] = ''; } $myxls->write_string($i, 0, $student->firstname); $myxls->write_string($i, 1, $student->lastname); $myxls->write_string($i, 2, $student->idnumber); $myxls->write_string($i, 3, $student->institution); $myxls->write_string($i, 4, $student->department); $myxls->write_string($i, 5, $student->email); $j = 6; foreach ($studentgrades as $grade) { if (is_numeric($grade)) { $myxls->write_number($i, $j++, strip_tags($grade)); } else { $myxls->write_string($i, $j++, strip_tags($grade)); } } $myxls->write_number($i, $j, $totals[$student->id]); } } /// Close the workbook $workbook->close(); exit; } else { if ($download == "xls" and confirm_sesskey()) { require_once "../lib/excellib.class.php"; /// Calculate file name $downloadfilename = clean_filename("{$course->shortname} {$strgrades}.xls"); /// Creating a workbook $workbook = new MoodleExcelWorkbook("-"); /// Sending HTTP headers $workbook->send($downloadfilename); /// Adding the worksheet $myxls =& $workbook->add_worksheet($strgrades); /// Print names of all the fields $myxls->write_string(0, 0, get_string("firstname")); $myxls->write_string(0, 1, get_string("lastname")); $myxls->write_string(0, 2, get_string("idnumber")); $myxls->write_string(0, 3, get_string("institution")); $myxls->write_string(0, 4, get_string("department")); $myxls->write_string(0, 5, get_string("email")); $pos = 6; foreach ($columns as $column) { $myxls->write_string(0, $pos++, strip_tags($column)); } $myxls->write_string(0, $pos, get_string("total")); /// Print all the lines of data. $i = 0; if (!empty($grades)) { foreach ($grades as $studentid => $studentgrades) { $i++; $student = $students[$studentid]; if (empty($totals[$student->id])) { $totals[$student->id] = ''; } $myxls->write_string($i, 0, $student->firstname); $myxls->write_string($i, 1, $student->lastname); $myxls->write_string($i, 2, $student->idnumber); $myxls->write_string($i, 3, $student->institution); $myxls->write_string($i, 4, $student->department); $myxls->write_string($i, 5, $student->email); $j = 6; foreach ($studentgrades as $grade) { if (is_numeric($grade)) { $myxls->write_number($i, $j++, strip_tags($grade)); } else { $myxls->write_string($i, $j++, strip_tags($grade)); } } $myxls->write_number($i, $j, $totals[$student->id]); } } /// Close the workbook $workbook->close(); exit; } else { if ($download == "txt" and confirm_sesskey()) { /// Print header to force download header("Content-Type: application/download\n"); $downloadfilename = clean_filename("{$course->shortname} {$strgrades}"); header("Content-Disposition: attachment; filename=\"{$downloadfilename}.txt\""); /// Print names of all the fields echo get_string("firstname") . "\t" . get_string("lastname") . "\t" . get_string("idnumber") . "\t" . get_string("institution") . "\t" . get_string("department") . "\t" . get_string("email"); foreach ($columns as $column) { $column = strip_tags($column); echo "\t{$column}"; } echo "\t" . get_string("total") . "\n"; /// Print all the lines of data. foreach ($grades as $studentid => $studentgrades) { $student = $students[$studentid]; if (empty($totals[$student->id])) { $totals[$student->id] = ''; } echo "{$student->firstname}\t{$student->lastname}\t{$student->idnumber}\t{$student->institution}\t{$student->department}\t{$student->email}"; foreach ($studentgrades as $grade) { $grade = strip_tags($grade); echo "\t{$grade}"; } echo "\t" . $totals[$student->id]; echo "\n"; } exit; } else { if ($download == '' and confirm_sesskey()) { error("No file type specified"); exit; } } } } }
function print_coursereport_ods($coursereport_ods) { global $CFG; require_once "{$CFG->libdir}/odslib.class.php"; //release coursereport ods data $courseID = $coursereport_ods->courseID; $courseName = $coursereport_ods->courseName; $categoryName = $coursereport_ods->categoryName; $userIDDataArray = $coursereport_ods->userIDDataArray; $actionIDNameArray = $coursereport_ods->actionIDNameArray; $courseUserArray = $coursereport_ods->courseUserArray; $userPassArray = $coursereport_ods->userPassArray; $tt = getdate(time()); $today = mktime(0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]); $filename = 'course_reports_' . userdate(time(), get_string('backupnameformat', 'langconfig'), 99, false); $filename .= '.ods'; $workbook = new MoodleODSWorkbook('-'); $workbook->send($filename); $worksheet = array(); // Creating worksheets $sheettitle = 'course_report'; //紀錄1-1 $worksheet =& $workbook->add_worksheet($sheettitle); $worksheet->set_column(1, 1, 60); $worksheet->write_string(0, 0, $categoryName); $worksheet->write_string(1, 0, $courseName); $col = 0; $row = 4; $headerArray = processCourseReportHeader($courseUserArray, $actionIDNameArray); foreach ($headerArray as $item) { $worksheet->write($row - 1, $col, $item, ''); $col++; } $myxls =& $worksheet; foreach ($courseUserArray as $userID => $actionInfo) { $col = 0; $myxls->write_string($row, $col, $userIDDataArray[$userID]['name']); $col++; $myxls->write_string($row, $col, $userIDDataArray[$userID]['description']); $col++; $myxls->write_string($row, $col, $userIDDataArray[$userID]['email']); $col++; foreach ($actionInfo as $actionID => $actionContent) { if ($actionContent['timeEnable']) { $myxls->write_string($row, $col, $actionContent['time']); $col++; } if ($actionContent['scoreEnable']) { $myxls->write_string($row, $col, $actionContent['score']); $col++; } if ($actionContent['timePass'] and $actionContent['scorePass']) { $myxls->write_string($row, $col, '通過'); $col++; } else { $myxls->write_string($row, $col, '不通過'); $col++; } } $myxls->write_string($row, $col, $userPassArray[$userID] ? '通過' : '不通過'); $col++; $row++; } $workbook->close(); return true; }