Commit 00f0ad51 authored by Abbassy's avatar Abbassy
Browse files

Update locallib.php: simplify code and improve automatic grading logic

parent daf6849f
...@@ -59,11 +59,8 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -59,11 +59,8 @@ class assign_feedback_dta extends assign_feedback_plugin {
return null; return null;
} }
$record = $DB->get_record(self::TABLE_DTA_SUMMARY, return $DB->get_record(self::TABLE_DTA_SUMMARY,
array("assignment_id" => $assignmentid, "submission_id" => $submissionid)); array("assignment_id" => $assignmentid, "submission_id" => $submissionid));
// Convert false to null for type safety (get_record returns false if not found)
return $record ? $record : null;
} }
/** /**
...@@ -78,32 +75,30 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -78,32 +75,30 @@ class assign_feedback_dta extends assign_feedback_plugin {
/** /**
* Check if this plugin is enabled. * Check if this plugin is enabled.
* Returns true if the plugin is enabled in the assignment settings. * Return false to hide the feedback column from the grading table.
* This controls whether the feedback column is shown in the grading table. * Note: This does NOT disable the automatic grading functionality,
* which still works via view_summary().
* *
* @return bool * @return bool
*/ */
public function is_enabled() { public function is_enabled() {
// Check if this plugin is enabled in the assignment settings. return false; // Hide the feedback column completely
// Moodle stores this in the assignment instance configuration.
// The parent class method checks if the plugin is enabled for this assignment.
return parent::is_enabled();
} }
/** /**
* Check if the feedback is empty. * Check if the feedback is empty.
* *
* This method determines if the feedback column should show content. * CRITICAL: This is the method Moodle uses to determine if the feedback block should be shown!
* We return false to always show the column (even if empty) so that * If this returns true, Moodle will NOT display the feedback block at all.
* the automatic grading functionality can work via view_summary(). *
* We always return true now since we don't want to show any UI in the feedback table.
* *
* @param stdClass $grade The grade object * @param stdClass $grade The grade object
* @return bool True if feedback is empty, false if feedback exists * @return bool True if feedback is empty, false if feedback exists
*/ */
public function is_empty(stdClass $grade) { public function is_empty(stdClass $grade) {
// Return false to always show the column, even if empty. // Always return true to hide the feedback column completely
// This allows view_summary() to be called and automatic grading to work. return true;
return false;
} }
/** /**
...@@ -135,17 +130,8 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -135,17 +130,8 @@ class assign_feedback_dta extends assign_feedback_plugin {
null, // No description text - help only via help button null, // No description text - help only via help button
null, array(0, 1)); null, array(0, 1));
$mform->addHelpButton('assignfeedback_dta_gradeautomatically', 'grade_automatically', self::COMPONENT_NAME); $mform->addHelpButton('assignfeedback_dta_gradeautomatically', 'grade_automatically', self::COMPONENT_NAME);
// Set default value from saved config, or 1 if not set yet. // Set default value from saved config, or 1 if not set yet.
// get_config() returns false if not set, so we need to handle that case. $defaultvalue = $this->get_config('gradeautomatically', 1);
$savedvalue = $this->get_config('gradeautomatically');
if ($savedvalue === false) {
// Value not set yet - use default of 1 (enabled)
$defaultvalue = 1;
} else {
// Value exists - use it (will be 0 or 1)
$defaultvalue = (int)$savedvalue;
}
$mform->setDefault('assignfeedback_dta_gradeautomatically', $defaultvalue); $mform->setDefault('assignfeedback_dta_gradeautomatically', $defaultvalue);
$mform->setType('assignfeedback_dta_gradeautomatically', PARAM_BOOL); $mform->setType('assignfeedback_dta_gradeautomatically', PARAM_BOOL);
...@@ -163,21 +149,13 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -163,21 +149,13 @@ class assign_feedback_dta extends assign_feedback_plugin {
*/ */
public function save_settings(stdClass $data) { public function save_settings(stdClass $data) {
// Check if the checkbox value exists in the form data. // Check if the checkbox value exists in the form data.
// The checkbox is only present in the form if the plugin is enabled. // advcheckbox returns the value directly (0 or 1), not as an array.
// advcheckbox sends the value directly (0 or 1) when checked/unchecked. if (isset($data->assignfeedback_dta_gradeautomatically)) {
if (property_exists($data, 'assignfeedback_dta_gradeautomatically')) {
// Get the old value before saving. // Get the old value before saving.
$oldvalue = $this->get_config('gradeautomatically'); $oldvalue = $this->get_config('gradeautomatically', 1);
if ($oldvalue === false) {
$oldvalue = 1; // Default to enabled if not set
} else {
$oldvalue = (int)$oldvalue;
}
// Get the new value from form data (will be 0 or 1).
$value = (int)$data->assignfeedback_dta_gradeautomatically;
// Save the value. // Value is set - save it (0 or 1)
$value = !empty($data->assignfeedback_dta_gradeautomatically) ? 1 : 0;
$this->set_config('gradeautomatically', $value); $this->set_config('gradeautomatically', $value);
// If the setting was changed from enabled (1) to disabled (0), // If the setting was changed from enabled (1) to disabled (0),
...@@ -185,10 +163,11 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -185,10 +163,11 @@ class assign_feedback_dta extends assign_feedback_plugin {
if ($oldvalue == 1 && $value == 0) { if ($oldvalue == 1 && $value == 0) {
$this->clear_automatic_grades(); $this->clear_automatic_grades();
} }
} else {
// Checkbox was hidden (because DTA was not enabled), so it won't be in $data.
// In this case, we don't change the existing value - it stays as is.
// This is correct behavior: if DTA is disabled, we can't change this setting anyway.
} }
// If the checkbox is not in the form data, it means the plugin is not enabled,
// so we don't change the existing value (it stays as is).
return true; return true;
} }
...@@ -224,40 +203,6 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -224,40 +203,6 @@ class assign_feedback_dta extends assign_feedback_plugin {
} }
} }
/**
* Clear only the "Final grade" column for a specific user.
* This keeps the grade displayed in DTA Feedback column but removes it from "Final grade".
*
* @param int $userid The user ID
* @return void
*/
private function clear_user_final_grade(int $userid): void {
global $DB;
$assignmentid = $this->assignment->get_instance()->id;
// Get the grade_item for grade_grades table operations.
$gradeitem = \grade_item::fetch(array(
'itemtype' => 'mod',
'itemmodule' => 'assign',
'iteminstance' => $assignmentid,
'courseid' => $this->assignment->get_course()->id
));
if ($gradeitem) {
// Set finalgrade to NULL to clear "Final grade" column, but keep the record.
$DB->set_field('grade_grades', 'finalgrade', null, array(
'itemid' => $gradeitem->id,
'userid' => $userid
));
// Also clear rawgrade to ensure nothing is displayed in "Final grade".
$DB->set_field('grade_grades', 'rawgrade', null, array(
'itemid' => $gradeitem->id,
'userid' => $userid
));
}
}
/** /**
* Clear automatically set grade for a specific user. * Clear automatically set grade for a specific user.
* Called when "Grade automatically" is disabled and view_summary() is called. * Called when "Grade automatically" is disabled and view_summary() is called.
...@@ -305,13 +250,7 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -305,13 +250,7 @@ class assign_feedback_dta extends assign_feedback_plugin {
* @return bool True if automatic grading is enabled * @return bool True if automatic grading is enabled
*/ */
public function is_grade_automatically_enabled() { public function is_grade_automatically_enabled() {
// get_config() returns false if not set, so we need to handle that case. return (bool)$this->get_config('gradeautomatically', 1); // Default to enabled
$value = $this->get_config('gradeautomatically');
if ($value === false) {
// Value not set yet - default to enabled (1)
return true;
}
return (bool)$value;
} }
/** /**
...@@ -691,6 +630,8 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -691,6 +630,8 @@ class assign_feedback_dta extends assign_feedback_plugin {
* @return string * @return string
*/ */
public function view_summary(stdClass $grade, &$showviewlink) { public function view_summary(stdClass $grade, &$showviewlink) {
debugging('MOJEC view_summary() wurde ausgeführt', DEBUG_DEVELOPER);
$showviewlink = false; $showviewlink = false;
// Get submission for grading calculations // Get submission for grading calculations
...@@ -703,40 +644,22 @@ class assign_feedback_dta extends assign_feedback_plugin { ...@@ -703,40 +644,22 @@ class assign_feedback_dta extends assign_feedback_plugin {
} }
} }
// Calculate grade if submission exists // Calculate and apply grade if submission exists
$output = '';
if ($this->are_dta_tables_available() && !empty($submission) && !empty($grade) && isset($grade->userid) && !empty($grade->userid)) { if ($this->are_dta_tables_available() && !empty($submission) && !empty($grade) && isset($grade->userid) && !empty($grade->userid)) {
$gradingservice = new grading_service($this->assignment); $gradingservice = new grading_service($this->assignment);
$gradesummary = $gradingservice->calculate_summary($submission, $dtasubmission); $gradesummary = $gradingservice->calculate_summary($submission, $dtasubmission);
// Only apply grade automatically if the setting is enabled.
// Display the grade in the DTA Feedback column
if ($gradesummary['gradable'] && $gradesummary['hasdata']) {
$maxgrade = $gradesummary['maxgrade'];
$finalgrade = $gradesummary['finalgrade'];
// Format: "42.36 / 100.00"
$output = html_writer::tag('div',
format_float($finalgrade, 2) . ' / ' . format_float($maxgrade, 2),
array('class' => 'dta-feedback-grade')
);
}
// Apply automatic grading if enabled
if ($this->is_grade_automatically_enabled()) { if ($this->is_grade_automatically_enabled()) {
// Apply the grade to both assign_grades and grade_grades (Final grade column) // Pass dtasubmission to allow checking if grade was manually modified
if ($gradesummary['gradable'] && $gradesummary['hasdata']) {
$gradingservice->apply_grade($grade->userid, $gradesummary, $dtasubmission); $gradingservice->apply_grade($grade->userid, $gradesummary, $dtasubmission);
// Note: apply_grade() sets both assign_grades.grade and grade_grades.finalgrade
// So the grade appears in both "Grade" column and "Final grade" column
}
} else { } else {
// If automatic grading is disabled, only show grade in DTA Feedback column // If automatic grading is disabled, clear any automatically set grades for this user.
// Clear any automatically set grades so "Final grade" stays empty
$this->clear_user_automatic_grade($grade->userid); $this->clear_user_automatic_grade($grade->userid);
} }
} }
return $output; // Return empty string - no feedback button needed
return '';
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment