';
echo '';
foreach ($userprofilefields as $field) {
echo '' . $field->fullname . ' | ';
}
if (!$this->onlyactive) {
echo ''.get_string("suspended")." | ";
}
foreach ($this->columns as $grade_item) {
echo ''.$this->format_column_name($grade_item).' | ';
/// add a column_feedback column
if ($this->export_feedback) {
echo ''.$this->format_column_name($grade_item, true).' | ';
}
}
echo '
';
/// Print all the lines of data.
$i = 0;
$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()) {
// number of preview rows
if ($this->previewrows and $this->previewrows <= $i) {
break;
}
$user = $userdata->user;
if ($require_user_idnumber and empty($user->idnumber)) {
// some exports require user idnumber so we can match up students when importing the data
continue;
}
$gradeupdated = false; // if no grade is update at all for this user, do not display this row
$rowstr = '';
foreach ($this->columns as $itemid=>$unused) {
$gradetxt = $this->format_grade($userdata->grades[$itemid]);
// get the status of this grade, and put it through track to get the status
$g = new grade_export_update_buffer();
$grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id));
$status = $g->track($grade_grade);
if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) {
$rowstr .= ''.get_string('unchangedgrade', 'grades').' | ';
} else {
$rowstr .= "$gradetxt | ";
$gradeupdated = true;
}
if ($this->export_feedback) {
$rowstr .= ''.$this->format_feedback($userdata->feedbacks[$itemid]).' | ';
}
}
// if we are requesting updated grades only, we are not interested in this user at all
if (!$gradeupdated && $this->updatedgradesonly) {
continue;
}
echo '';
foreach ($userprofilefields as $field) {
$fieldvalue = grade_helper::get_user_field_value($user, $field);
// @see profile_field_base::display_data().
echo '' . format_text($fieldvalue, FORMAT_MOODLE, $formatoptions) . ' | ';
}
if (!$this->onlyactive) {
$issuspended = ($user->suspendedenrolment) ? get_string('yes') : '';
echo "$issuspended | ";
}
echo $rowstr;
echo "
";
$i++; // increment the counter
}
echo '
';
$gui->close();
}
/**
* Returns array of parameters used by dump.php and export.php.
* @return array
*/
public function get_export_params() {
$itemids = array_keys($this->columns);
$itemidsparam = implode(',', $itemids);
if (empty($itemidsparam)) {
$itemidsparam = '-1';
}
// We have a single grade display type constant.
if (!is_array($this->displaytype)) {
$displaytypes = $this->displaytype;
} else {
// Implode the grade display types array as moodle_url function doesn't accept arrays.
$displaytypes = implode(',', $this->displaytype);
}
if (!empty($this->updatedgradesonly)) {
$updatedgradesonly = $this->updatedgradesonly;
} else {
$updatedgradesonly = 0;
}
$params = array('id' => $this->course->id,
'groupid' => $this->groupid,
'itemids' => $itemidsparam,
'export_letters' => $this->export_letters,
'export_feedback' => $this->export_feedback,
'updatedgradesonly' => $updatedgradesonly,
'decimalpoints' => $this->decimalpoints,
'export_onlyactive' => $this->onlyactive,
'usercustomfields' => $this->usercustomfields,
'displaytype' => $displaytypes,
'key' => $this->userkey);
return $params;
}
/**
* Either prints a "Export" box, which will redirect the user to the download page,
* or prints the URL for the published data.
*
* @deprecated since 2.8 MDL-46548. Call get_export_url and set the
* action of the grade_export_form instead.
* @return void
*/
public function print_continue() {
global $CFG, $OUTPUT;
debugging('function grade_export::print_continue is deprecated.', DEBUG_DEVELOPER);
$params = $this->get_export_params();
echo $OUTPUT->heading(get_string('export', 'grades'));
echo $OUTPUT->container_start('gradeexportlink');
if (!$this->userkey) {
// This button should trigger a download prompt.
$url = new moodle_url('/grade/export/'.$this->plugin.'/export.php', $params);
echo $OUTPUT->single_button($url, get_string('download', 'admin'));
} else {
$paramstr = '';
$sep = '?';
foreach($params as $name=>$value) {
$paramstr .= $sep.$name.'='.$value;
$sep = '&';
}
$link = $CFG->wwwroot.'/grade/export/'.$this->plugin.'/dump.php'.$paramstr.'&key='.$this->userkey;
echo get_string('download', 'admin').': ' . html_writer::link($link, $link);
}
echo $OUTPUT->container_end();
return;
}
/**
* Generate the export url.
*
* Get submitted form data and create the url to be used on the grade publish feature.
*
* @return moodle_url the url of grade publishing export.
*/
public function get_export_url() {
return new moodle_url('/grade/export/'.$this->plugin.'/dump.php', $this->get_export_params());
}
/**
* Convert the grade display types parameter into the required array to grade exporting class.
*
* In order to export, the array key must be the display type name and the value must be the grade display type
* constant.
*
* Note: Added support for combined display types constants like the (GRADE_DISPLAY_TYPE_PERCENTAGE_REAL) as
* the $CFG->grade_export_displaytype config is still used on 2.7 in case of missing displaytype url param.
* In these cases, the file will be exported with a column for each display type.
*
* @param string $displaytypes can be a single or multiple display type constants comma separated.
* @return array $types
*/
public static function convert_flat_displaytypes_to_array($displaytypes) {
$types = array();
// We have a single grade display type constant.
if (is_int($displaytypes)) {
$displaytype = clean_param($displaytypes, PARAM_INT);
// Let's set a default value, will be replaced below by the grade display type constant.
$display[$displaytype] = 1;
} else {
// Multiple grade display types constants.
$display = array_flip(explode(',', $displaytypes));
}
// Now, create the array in the required format by grade exporting class.
foreach ($display as $type => $value) {
$type = clean_param($type, PARAM_INT);
if ($type == GRADE_DISPLAY_TYPE_LETTER) {
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
} else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
} else if ($type == GRADE_DISPLAY_TYPE_REAL) {
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
} else if ($type == GRADE_DISPLAY_TYPE_REAL_PERCENTAGE) {
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
} else if ($type == GRADE_DISPLAY_TYPE_REAL_LETTER) {
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
} else if ($type == GRADE_DISPLAY_TYPE_LETTER_REAL) {
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
} else if ($type == GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE) {
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
} else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER) {
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
} else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE_REAL) {
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
}
}
return $types;
}
/**
* Convert the item ids parameter into the required array to grade exporting class.
*
* In order to export, the array key must be the grade item id and all values must be one.
*
* @param string $itemids can be a single item id or many item ids comma separated.
* @return array $items correctly formatted array.
*/
public static function convert_flat_itemids_to_array($itemids) {
$items = array();
// We just have one single item id.
if (is_int($itemids)) {
$itemid = clean_param($itemids, PARAM_INT);
$items[$itemid] = 1;
} else {
// Few grade items.
$items = array_flip(explode(',', $itemids));
foreach ($items as $itemid => $value) {
$itemid = clean_param($itemid, PARAM_INT);
$items[$itemid] = 1;
}
}
return $items;
}
/**
* Create the html code of the grade publishing feature.
*
* @return string $output html code of the grade publishing.
*/
public function get_grade_publishing_url() {
$url = $this->get_export_url();
$output = html_writer::start_div();
$output .= html_writer::tag('p', get_string('gradepublishinglink', 'grades', html_writer::link($url, $url)));
$output .= html_writer::end_div();
return $output;
}
/**
* Create a stdClass object from URL parameters to be used by grade_export class.
*
* @param int $id course id.
* @param string $itemids grade items comma separated.
* @param bool $exportfeedback export feedback option.
* @param bool $onlyactive only enrolled active students.
* @param string $displaytype grade display type constants comma separated.
* @param int $decimalpoints grade decimal points.
* @param null $updatedgradesonly recently updated grades only (Used by XML exporting only).
* @param null $separator separator character: tab, comma, colon and semicolon (Used by TXT exporting only).
*
* @return stdClass $formdata
*/
public static function export_bulk_export_data($id, $itemids, $exportfeedback, $onlyactive, $displaytype,
$decimalpoints, $updatedgradesonly = null, $separator = null) {
$formdata = new \stdClass();
$formdata->id = $id;
$formdata->itemids = self::convert_flat_itemids_to_array($itemids);
$formdata->exportfeedback = $exportfeedback;
$formdata->export_onlyactive = $onlyactive;
$formdata->display = self::convert_flat_displaytypes_to_array($displaytype);
$formdata->decimals = $decimalpoints;
if (!empty($updatedgradesonly)) {
$formdata->updatedgradesonly = $updatedgradesonly;
}
if (!empty($separator)) {
$formdata->separator = $separator;
}
return $formdata;
}
}
/**
* This class is used to update the exported field in grade_grades.
* It does internal buffering to speedup the db operations.
*/
class grade_export_update_buffer {
public $update_list;
public $export_time;
/**
* Constructor - creates the buffer and initialises the time stamp
*/
public function __construct() {
$this->update_list = array();
$this->export_time = time();
}
/**
* Old syntax of class constructor. Deprecated in PHP7.
*
* @deprecated since Moodle 3.1
*/
public function grade_export_update_buffer() {
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
self::__construct();
}
public function flush($buffersize) {
global $CFG, $DB;
if (count($this->update_list) > $buffersize) {
list($usql, $params) = $DB->get_in_or_equal($this->update_list);
$params = array_merge(array($this->export_time), $params);
$sql = "UPDATE {grade_grades} SET exported = ? WHERE id $usql";
$DB->execute($sql, $params);
$this->update_list = array();
}
}
/**
* Track grade export status
* @param object $grade_grade
* @return string $status (unknow, new, regrade, nochange)
*/
public function track($grade_grade) {
if (empty($grade_grade->exported) or empty($grade_grade->timemodified)) {
if (is_null($grade_grade->finalgrade)) {
// grade does not exist yet
$status = 'unknown';
} else {
$status = 'new';
$this->update_list[] = $grade_grade->id;
}
} else if ($grade_grade->exported < $grade_grade->timemodified) {
$status = 'regrade';
$this->update_list[] = $grade_grade->id;
} else if ($grade_grade->exported >= $grade_grade->timemodified) {
$status = 'nochange';
} else {
// something is wrong?
$status = 'unknown';
}
$this->flush(100);
return $status;
}
/**
* Flush and close the buffer.
*/
public function close() {
$this->flush(0);
}
}
/**
* Verify that there is a valid set of grades to export.
* @param $courseid int The course being exported
*/
function export_verify_grades($courseid) {
if (grade_needs_regrade_final_grades($courseid)) {
throw new moodle_exception('gradesneedregrading', 'grades');
}
}