moodle_local_treestudyplan/cfg_grades.php

243 lines
9.2 KiB
PHP
Raw Normal View History

<?php
2023-08-24 23:02:41 +02:00
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
*
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
2023-08-24 23:02:41 +02:00
require_once("../../config.php");
require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup("local_treestudyplan_gradeconfig");
$systemcontext = context_system::instance();
2023-08-24 23:02:41 +02:00
// Check if user has capability to manage this.
require_capability('local/treestudyplan:configure', $systemcontext);
$PAGE->requires->js_call_amd('local_treestudyplan/cfg-grades', 'init');
const GRADECFG_TABLE = "local_treestudyplan_gradecfg";
$scales = \grade_scale::fetch_all_global();
$mappings = $DB->get_records(GRADECFG_TABLE);
2023-08-25 09:33:42 +02:00
$scalecfgs = [];
$gradecfgs = [];
2023-08-24 23:02:41 +02:00
foreach ($mappings as $cfg) {
if (!empty($cfg->scale_id)) {
2023-08-25 09:33:42 +02:00
$scalecfgs[$cfg->scale_id] = $cfg;
2023-08-24 23:09:20 +02:00
} else if (!empty($cfg->grade_points)) {
2023-08-25 09:33:42 +02:00
$gradecfgs[$cfg->grade_points] = $cfg;
}
}
print $OUTPUT->header();
2023-08-24 23:02:41 +02:00
if ($_POST["action"] == "update") {
// First loop through the scales to see which need to be updated.
foreach ($scales as $scale) {
2023-08-25 09:33:42 +02:00
if (array_key_exists($scale->id, $scalecfgs)) {
$scalecfg = $scalecfgs[$scale->id];
$needupdate = false;
2023-08-24 23:02:41 +02:00
foreach (["min_progress", "min_completed"] as $handle) {
$key = "s_{$scale->id}_{$handle}";
2023-08-24 23:02:41 +02:00
if (array_key_exists($key, $_POST) && is_numeric($_POST[$key])) {
$value = intval($_POST[$key]);
2023-08-24 23:02:41 +02:00
if ($value != $scalecfg->$handle) {
$scalecfg->$handle = $value;
$needupdate = true;
}
}
}
2023-08-24 23:02:41 +02:00
if ($needupdate) {
$DB->update_record(GRADECFG_TABLE, $scalecfg);
}
2023-08-24 23:09:20 +02:00
} else {
2023-08-24 23:02:41 +02:00
$scalecfg = (object)[ "scale_id" => $scale->id, ];
$requireinsert = false;
2023-08-24 23:02:41 +02:00
foreach (["min_progress", "min_completed"] as $handle) {
$key = "s_{$scale->id}_{$handle}";
2023-08-24 23:02:41 +02:00
if (array_key_exists($key, $_POST) && is_numeric($_POST[$key])) {
$scalecfg->$handle = intval($_POST[$key]);
$requireinsert = true;
}
}
2023-08-24 23:02:41 +02:00
if ($requireinsert) {
// Insert into database and add to the list of scale configs.
$id = $DB->insert_record(GRADECFG_TABLE, $scalecfg);
$scalecfg = $DB->get_record(GRADECFG_TABLE, ['id' => $id]);
2023-08-25 09:33:42 +02:00
$scalecfgs[$id] = $scalecfg;
}
}
}
2023-08-24 23:02:41 +02:00
// Now, loop through the gradepoints to parse .
$deletelist = [];
2023-08-25 09:33:42 +02:00
foreach ($gradecfgs as $gradecfg) {
$deletekey = "g_{$gradecfg->grade_points}_delete";
2023-08-24 23:02:41 +02:00
if (array_key_exists($deletekey, $_POST) && boolval($_POST[$deletekey]) === true) {
$DB->delete_records(GRADECFG_TABLE, ["id" => $gradecfg->id]);
$deletelist[] = $gradecfg;
2023-08-25 09:33:42 +02:00
} else {
2023-08-24 23:02:41 +02:00
foreach (["min_progress", "min_completed"] as $handle) {
$key = "g_{$gradecfg->grade_points}_{$handle}";
2023-08-24 23:02:41 +02:00
if (array_key_exists($key, $_POST) && is_numeric($_POST[$key])) {
$gradecfg->$handle = floatval($_POST[$key]);
}
}
2023-08-24 23:02:41 +02:00
$DB->update_record(GRADECFG_TABLE, $gradecfg);
2023-08-25 09:44:34 +02:00
// Reload to ensure proper rounding is done.
2023-08-25 09:33:42 +02:00
$gradecfgs[$gradecfg->grade_points] = $DB->get_record(GRADECFG_TABLE, ['id' => $gradecfg->id]);
}
}
2023-08-24 23:02:41 +02:00
foreach ($deletelist as $gradeconfig) {
2023-08-25 09:33:42 +02:00
unset($gradecfgs[$gradecfg->grade_points]);
}
unset($deletelist);
2023-08-24 23:02:41 +02:00
// And add an optionally existing new gradepoint setting.
2023-08-25 09:44:34 +02:00
if (array_key_exists("g_new_gradepoints", $_POST)
&& !empty($_POST["g_new_gradepoints"])
2023-08-25 09:33:42 +02:00
&& is_numeric($_POST["g_new_gradepoints"]) ) {
$gp = intval($_POST["g_new_gradepoints"]);
2023-08-25 09:33:42 +02:00
if (!array_key_exists($gp, $gradecfgs)) {
$gradecfg = (object)[ "grade_points" => $gp];
$requireinsert = false;
2023-08-24 23:02:41 +02:00
foreach (["min_progress", "min_completed"] as $handle) {
$key = "g_new_{$handle}";
2023-08-24 23:02:41 +02:00
if (array_key_exists($key, $_POST) && is_numeric($_POST[$key])) {
$gradecfg->$handle = floatval($_POST[$key]);
$requireinsert = true;
}
}
2023-08-24 23:02:41 +02:00
if ($requireinsert) {
// Insert into database and add to the list of grade configs.
$id = $DB->insert_record(GRADECFG_TABLE, $gradecfg);
2023-08-25 09:44:34 +02:00
// Reload to ensure proper rounding is done.
2023-08-24 23:02:41 +02:00
$gradecfg = $DB->get_record(GRADECFG_TABLE, ['id' => $id]);
2023-08-25 09:33:42 +02:00
$gradecfgs[$id] = $gradecfg;
}
}
}
2023-08-24 23:02:41 +02:00
}
2023-08-25 09:44:34 +02:00
// Process all available scales and load the current configuration for it.
$data = [];
2023-08-24 23:02:41 +02:00
foreach ($scales as $scale) {
$scale->load_items();
$scalecfg = null;
2023-08-25 09:33:42 +02:00
if (array_key_exists($scale->id, $scalecfgs)) {
$scalecfg = $scalecfgs[$scale->id];
}
2023-08-25 09:33:42 +02:00
$attrsc = ['value' => '', 'disabled' => 'disabled', ];
$attrsp = ['value' => '', 'disabled' => 'disabled', ];
2023-08-24 23:02:41 +02:00
if (!isset($scalecfg) || $scalecfg->min_completed == "") {
2023-08-25 09:33:42 +02:00
$attrsc["selected"] = "selected";
}
2023-08-24 23:02:41 +02:00
if (!isset($scalecfg) || $scalecfg->min_progress == "") {
2023-08-25 09:33:42 +02:00
$attrsp["selected"] = "selected";
}
2023-08-25 09:33:42 +02:00
$optionscompleted = html_writer::tag("option", get_string('select_scaleitem', 'local_treestudyplan'), $attrsc);
$optionsprogress = html_writer::tag("option", get_string('select_scaleitem', 'local_treestudyplan'), $attrsp);
2023-08-24 23:02:41 +02:00
$key = 1; // Start counting by one, as used in sum aggregations.
2023-08-24 23:02:41 +02:00
foreach ($scale->scale_items as $value) {
2023-08-25 09:33:42 +02:00
$attrsc = ["value" => $key];
$attrsp = ["value" => $key];
2023-08-24 23:02:41 +02:00
if (isset($scalecfg)) {
if (intval($scalecfg->min_completed) == $key) {
2023-08-25 09:33:42 +02:00
$attrsc["selected"] = "selected";
}
2023-08-24 23:02:41 +02:00
if (intval($scalecfg->min_progress) == $key) {
2023-08-25 09:33:42 +02:00
$attrsp["selected"] = "selected";
}
}
2023-08-25 09:33:42 +02:00
$optionsprogress .= html_writer::tag("option", $value, $attrsp);
$optionscompleted .= html_writer::tag("option", $value, $attrsc);
$key++;
}
2023-08-25 09:44:34 +02:00
$row = [];
$row[] = $scale->name;
$row[] = html_writer::tag("select", $optionscompleted, ['name' => "s_{$scale->id}_min_completed", 'autocomplete' => 'off']);
$data[] = $row;
}
2023-08-24 23:02:41 +02:00
print html_writer::start_tag("form", ["method" => "post", ]);
print html_writer::tag("input", null, ['name' => "action", 'value' => 'update', 'type' => 'hidden']);
$table = new html_table();
$table->id = "";
$table->attributes['class'] = 'generaltable m-roomtable';
$table->tablealign = 'center';
$table->head = [];
$table->data = $data;
$table->head[] = get_string('scale');
$table->head[] = get_string('min_completed', 'local_treestudyplan');
print $OUTPUT->heading(get_string('cfg_grades_desc_head', 'local_treestudyplan'));
print html_writer::tag('p', get_string('cfg_grades_desc', 'local_treestudyplan'));
print $OUTPUT->heading(get_string('cfg_grades_scales', 'local_treestudyplan'));
print html_writer::tag('div', html_writer::table($table), ['class'=>'flexible-wrap']);
2023-08-24 23:02:41 +02:00
$data = [];
2023-08-25 09:33:42 +02:00
foreach ($gradecfgs as $g) {
$row = [];
2023-08-25 09:44:34 +02:00
$row[] = $g->grade_points;
$row[] = html_writer::tag("input", null, ['name' => "g_{$g->grade_points}_min_completed", 'value' => "{$g->min_completed}", 'type' => 'text', "class" => "float", 'autocomplete' => 'off']);
$row[] = html_writer::tag("input", null, ['name' => "g_{$g->grade_points}_delete", 'type' => 'checkbox', ]);
$data[] = $row;
}
$row = [];
$row[] = html_writer::tag("input", null, ['name' => "g_new_gradepoints", 'value' => '', 'type' => 'number', 'min' => '0', 'pattern' => '/d+', 'step' => '1', 'autocomplete' => 'off']);
2023-08-25 09:44:34 +02:00
$row[] = html_writer::tag("input", null, ['name' => "g_new_min_completed", 'value' => '', 'type' => 'text', "class" => "float", 'autocomplete' => 'off']);
$data[] = $row;
$table = new html_table();
$table->id = "";
$table->attributes['class'] = 'generaltable m-roomtable';
$table->tablealign = 'center';
$table->head = [];
$table->data = $data;
$table->head[] = get_string('grade_points', 'local_treestudyplan');
$table->head[] = get_string('min_completed', 'local_treestudyplan');
2023-08-24 23:02:41 +02:00
$table->head[] = get_string('delete', );
print $OUTPUT->heading(get_string('cfg_grades_grades', 'local_treestudyplan'));
print html_writer::tag('div', html_writer::table($table), ['class'=>'flexible-wrap']);
print html_writer::tag("input", null, ['value' => get_string("save"), 'type' => 'submit', "class" => "btn btn-primary"]);
print html_writer::end_tag("form");
print $OUTPUT->footer();