. require_once($CFG->dirroot.'/lib/gradelib.php'); require_once($CFG->dirroot.'/grade/lib.php'); require_once($CFG->dirroot.'/grade/export/grade_export_form.php'); /** * Base export class */ abstract class grade_export { public $plugin; // plgin name - must be filled in subclasses! public $grade_items; // list of all course grade items public $groupid; // groupid, 0 means all groups public $course; // course object public $columns; // array of grade_items selected for export public $export_letters; // export letters public $export_feedback; // export feedback public $userkey; // export using private user key public $updatedgradesonly; // only export updated grades /** * Grade display type (real, percentages or letter). * * This attribute is an integer for XML file export. Otherwise is an array for all other formats (ODS, XLS and TXT). * * @var $displaytype Grade display type constant (1, 2 or 3) or an array of display types where the key is the name * and the value is the grade display type constant or 0 for unchecked display types. * @access public. */ public $displaytype; public $decimalpoints; // number of decimal points for exports public $onlyactive; // only include users with an active enrolment public $usercustomfields; // include users custom fields /** * @deprecated since Moodle 2.8 * @var $previewrows Number of rows in preview. */ public $previewrows; /** * Constructor should set up all the private variables ready to be pulled. * * This constructor used to accept the individual parameters as separate arguments, in * 2.8 this was simplified to just accept the data from the moodle form. * * @access public * @param object $course * @param int $groupid * @param stdClass|null $formdata * @note Exporting as letters will lead to data loss if that exported set it re-imported. */ public function __construct($course, $groupid, $formdata) { if (func_num_args() != 3 || ($formdata != null && get_class($formdata) != "stdClass")) { $args = func_get_args(); return call_user_func_array(array($this, "deprecated_constructor"), $args); } $this->course = $course; $this->groupid = $groupid; $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id)); $this->process_form($formdata); } /** * Old deprecated constructor. * * This deprecated constructor accepts the individual parameters as separate arguments, in * 2.8 this was simplified to just accept the data from the moodle form. * * @deprecated since 2.8 MDL-46548. Instead call the shortened constructor which accepts the data * directly from the grade_export_form. */ protected function deprecated_constructor($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2, $onlyactive = false, $usercustomfields = false) { debugging('Many argument constructor for class "grade_export" is deprecated. Call the 3 argument version instead.', DEBUG_DEVELOPER); $this->course = $course; $this->groupid = $groupid; $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id)); //Populating the columns here is required by /grade/export/(whatever)/export.php //however index.php, when the form is submitted, will construct the collection here //with an empty $itemlist then reconstruct it in process_form() using $formdata $this->columns = array(); if (!empty($itemlist)) { if ($itemlist=='-1') { //user deselected all items } else { $itemids = explode(',', $itemlist); // remove items that are not requested foreach ($itemids as $itemid) { if (array_key_exists($itemid, $this->grade_items)) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } } } else { foreach ($this->grade_items as $itemid=>$unused) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } $this->export_feedback = $export_feedback; $this->userkey = ''; $this->previewrows = false; $this->updatedgradesonly = $updatedgradesonly; $this->displaytype = $displaytype; $this->decimalpoints = $decimalpoints; $this->onlyactive = $onlyactive; $this->usercustomfields = $usercustomfields; } /** * Init object based using data from form * @param object $formdata */ function process_form($formdata) { global $USER; $this->columns = array(); if (!empty($formdata->itemids)) { if ($formdata->itemids=='-1') { //user deselected all items } else { foreach ($formdata->itemids as $itemid=>$selected) { if ($selected and array_key_exists($itemid, $this->grade_items)) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } } } else { foreach ($this->grade_items as $itemid=>$unused) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } if (isset($formdata->key)) { if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) { // Create a new key $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil); } $this->userkey = $formdata->key; } if (isset($formdata->decimals)) { $this->decimalpoints = $formdata->decimals; } if (isset($formdata->export_letters)) { $this->export_letters = $formdata->export_letters; } if (isset($formdata->export_feedback)) { $this->export_feedback = $formdata->export_feedback; } if (isset($formdata->export_onlyactive)) { $this->onlyactive = $formdata->export_onlyactive; } if (isset($formdata->previewrows)) { $this->previewrows = $formdata->previewrows; } if (isset($formdata->display)) { $this->displaytype = $formdata->display; // Used by grade exports which accept multiple display types. // If the checkbox value is 0 (unchecked) then remove it. if (is_array($formdata->display)) { $this->displaytype = array_filter($formdata->display); } } if (isset($formdata->updatedgradesonly)) { $this->updatedgradesonly = $formdata->updatedgradesonly; } } /** * Update exported field in grade_grades table * @return boolean */ public function track_exports() { global $CFG; /// Whether this plugin is entitled to update export time if ($expplugins = explode(",", $CFG->gradeexport)) { if (in_array($this->plugin, $expplugins)) { return true; } else { return false; } } else { return false; } } /** * Returns string representation of final grade * @param object $grade instance of grade_grade class * @param integer $gradedisplayconst grade display type constant. * @return string */ public function format_grade($grade, $gradedisplayconst = null) { $displaytype = $this->displaytype; if (is_array($this->displaytype) && !is_null($gradedisplayconst)) { $displaytype = $gradedisplayconst; } return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $displaytype, $this->decimalpoints); } /** * Returns the name of column in export * @param object $grade_item * @param boolean $feedback feedback colum * @param string $gradedisplayname grade display name. * @return string */ public function format_column_name($grade_item, $feedback=false, $gradedisplayname = null) { $column = new stdClass(); if ($grade_item->itemtype == 'mod') { $column->name = get_string('modulename', $grade_item->itemmodule).get_string('labelsep', 'langconfig').$grade_item->get_name(); } else { $column->name = $grade_item->get_name(); } // We can't have feedback and display type at the same time. $column->extra = ($feedback) ? get_string('feedback') : get_string($gradedisplayname, 'grades'); return html_to_text(get_string('gradeexportcolumntype', 'grades', $column), 0, false); } /** * Returns formatted grade feedback * @param object $feedback object with properties feedback and feedbackformat * @return string */ public function format_feedback($feedback) { return strip_tags(format_text($feedback->feedback, $feedback->feedbackformat)); } /** * Implemented by child class */ public abstract function print_grades(); /** * Prints preview of exported grades on screen as a feedback mechanism * @param bool $require_user_idnumber true means skip users without idnumber * @deprecated since 2.8 MDL-46548. Previews are not useful on export. */ public function display_preview($require_user_idnumber=false) { global $OUTPUT; debugging('function grade_export::display_preview is deprecated.', DEBUG_DEVELOPER); $userprofilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields); $formatoptions = new stdClass(); $formatoptions->para = false; echo $OUTPUT->heading(get_string('previewrows', 'grades')); 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 ''.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 '
---|---|---|---|
' . format_text($fieldvalue, FORMAT_MOODLE, $formatoptions) . ' | '; } if (!$this->onlyactive) { $issuspended = ($user->suspendedenrolment) ? get_string('yes') : ''; echo "$issuspended | "; } echo $rowstr; echo "