Skip to content

Commit

Permalink
Merge branch 'MDL-78922-master' of https://github.com/ilyatregubov/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta authored and sarjona committed Aug 17, 2023
2 parents 0d86da6 + 8490867 commit 3cfaa97
Showing 1 changed file with 118 additions and 14 deletions.
132 changes: 118 additions & 14 deletions grade/classes/form/add_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use grade_helper;
use grade_item;
use grade_plugin_return;
use grade_scale;
use moodle_url;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -131,7 +132,8 @@ private function get_gradecategory(): array {

return [
'gradecategory' => $gradecategory,
'categoryitem' => $category
'categoryitem' => $category,
'gradeitem' => $gradeitem
];
}

Expand All @@ -144,7 +146,7 @@ private function get_gradecategory(): array {
* @throws \moodle_exception
*/
protected function definition(): void {
global $CFG;
global $CFG, $OUTPUT, $COURSE;
$courseid = $this->optional_param('courseid', null, PARAM_INT);
$id = $this->optional_param('category', 0, PARAM_INT);
$gprplugin = $this->optional_param('gpr_plugin', '', PARAM_TEXT);
Expand Down Expand Up @@ -183,18 +185,6 @@ protected function definition(): void {
$mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
$mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');

$mform->addElement('float', 'grade_item_grademax', get_string('grademax', 'grades'));
$mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
$mform->hideIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
$mform->hideIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);

if ((bool) get_config('moodle', 'grade_report_showmin')) {
$mform->addElement('float', 'grade_item_grademin', get_string('grademin', 'grades'));
$mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
$mform->hideIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
$mform->hideIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
}

if (empty($CFG->enableoutcomes)) {
$mform->addElement('hidden', 'aggregateoutcomes');
$mform->setType('aggregateoutcomes', PARAM_INT);
Expand All @@ -215,6 +205,77 @@ protected function definition(): void {
$mform->hideIf('keephigh', 'droplow', 'noteq', 0);
$mform->hideIf('droplow', 'keephigh', 'noteq', 0);

if (!empty($category->id)) {
$gradeitem = $local['gradeitem'];
// If grades exist set a message so the user knows why they can not alter the grade type or scale.
// We could never change the grade type for external items, so only need to show this for manual grade items.
if ($gradeitem->has_overridden_grades()) {
// Set a message so the user knows why the can not alter the grade type or scale.
if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
$gradesexistmsg = get_string('modgradecategorycantchangegradetyporscalemsg', 'grades');
} else {
$gradesexistmsg = get_string('modgradecategorycantchangegradetypemsg', 'grades');
}
$notification = new \core\output\notification($gradesexistmsg, \core\output\notification::NOTIFY_INFO);
$notification->set_show_closebutton(false);
$mform->addElement('static', 'gradesexistmsg', '', $OUTPUT->render($notification));
}
}

$options = [
GRADE_TYPE_NONE => get_string('typenone', 'grades'),
GRADE_TYPE_VALUE => get_string('typevalue', 'grades'),
GRADE_TYPE_SCALE => get_string('typescale', 'grades'),
GRADE_TYPE_TEXT => get_string('typetext', 'grades')
];

$mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
$mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
$mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
$mform->hideIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);

$options = [0 => get_string('usenoscale', 'grades')];
if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
foreach ($scales as $scale) {
$options[$scale->id] = $scale->get_name();
}
}
if ($scales = grade_scale::fetch_all_global()) {
foreach ($scales as $scale) {
$options[$scale->id] = $scale->get_name();
}
}
// Ugly BC hack - it was possible to use custom scale from other courses.
if (!empty($category->grade_item_scaleid) && !isset($options[$category->grade_item_scaleid])) {
if ($scale = grade_scale::fetch(['id' => $category->grade_item_scaleid])) {
$options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
}
}
$mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
$mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
$mform->hideIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
$mform->hideIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);

$choices = [];
$choices[''] = get_string('choose');
$choices['no'] = get_string('no');
$choices['yes'] = get_string('yes');
$mform->addElement('select', 'grade_item_rescalegrades', get_string('modgradecategoryrescalegrades', 'grades'), $choices);
$mform->addHelpButton('grade_item_rescalegrades', 'modgradecategoryrescalegrades', 'grades');
$mform->hideIf('grade_item_rescalegrades', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);

$mform->addElement('float', 'grade_item_grademax', get_string('grademax', 'grades'));
$mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
$mform->hideIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
$mform->hideIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);

if ((bool) get_config('moodle', 'grade_report_showmin')) {
$mform->addElement('float', 'grade_item_grademin', get_string('grademin', 'grades'));
$mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
$mform->hideIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
$mform->hideIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
}

// Hiding.
// advcheckbox is not compatible with disabledIf!
$mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
Expand Down Expand Up @@ -377,6 +438,7 @@ public function definition_after_data(): void {
}

} else {
// Adding new category
// Remove unwanted aggregation options.
if ($mform->elementExists('aggregation')) {
$allaggoptions = array_keys($this->aggregation_options);
Expand All @@ -389,6 +451,7 @@ public function definition_after_data(): void {
}
}

$mform->removeElement('grade_item_rescalegrades');
}

// Grade item.
Expand All @@ -401,11 +464,39 @@ public function definition_after_data(): void {
$mform->setDefault('grade_item_hidden', $gradeitem->get_hidden());
}

if ($gradeitem->has_overridden_grades()) {
// Can't change the grade type or the scale if there are grades.
$mform->hardFreeze('grade_item_gradetype, grade_item_scaleid');

// If we are using scales then remove the unnecessary rescale and grade fields.
if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
$mform->removeElement('grade_item_rescalegrades');
$mform->removeElement('grade_item_grademax');
if ($mform->elementExists('grade_item_grademin')) {
$mform->removeElement('grade_item_grademin');
}
} else {
// Not using scale, so remove it.
$mform->removeElement('grade_item_scaleid');
$mform->hideIf('grade_item_grademax', 'grade_item_rescalegrades', 'eq', '');
$mform->hideIf('grade_item_grademin', 'grade_item_rescalegrades', 'eq', '');
}
} else { // Remove the rescale element if there are no grades.
$mform->removeElement('grade_item_rescalegrades');
}

// Remove the aggregation coef element if not needed.
if ($gradeitem->is_course_item()) {
if ($mform->elementExists('grade_item_aggregationcoef')) {
$mform->removeElement('grade_item_aggregationcoef');
}

if ($mform->elementExists('grade_item_weightoverride')) {
$mform->removeElement('grade_item_weightoverride');
}
if ($mform->elementExists('grade_item_aggregationcoef2')) {
$mform->removeElement('grade_item_aggregationcoef2');
}
} else {
if ($gradeitem->is_category_item()) {
$category = $gradeitem->get_item_category();
Expand Down Expand Up @@ -436,6 +527,19 @@ public function definition_after_data(): void {
$mform->insertElementBefore($element, 'parentcategory');
$mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
}

// Remove fields used by natural weighting if the parent category is not using natural weighting.
// Or if the item is a scale and scales are not used in aggregation.
if ($parentcategory->aggregation != GRADE_AGGREGATE_SUM
|| (empty($CFG->grade_includescalesinaggregation) && $gradeitem->gradetype == GRADE_TYPE_SCALE)) {
if ($mform->elementExists('grade_item_weightoverride')) {
$mform->removeElement('grade_item_weightoverride');
}
if ($mform->elementExists('grade_item_aggregationcoef2')) {
$mform->removeElement('grade_item_aggregationcoef2');
}
}

}
}
}
Expand Down

0 comments on commit 3cfaa97

Please sign in to comment.