moodle_local_treestudyplan/cli/randomize_grades.php

185 lines
6.9 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
*/
namespace local_treestudyplan;
use \local_treestudyplan\local\gradegenerator;
define('CLI_SCRIPT', true);
require(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->dirroot. '/mod/assign/locallib.php');
$usage = "Fill all the gradables in a study plan with random values
Usage:
# php randomize_grades.php --studyplan=<shortname> [--file|-f=userfile]
# php randomize_grades.php --all|-a [--file|-f=userfile]
# php randomize_grades.php --help|-h
Options:
-h --help Print this help.
-f --file=<value> Specify file name to store generated user specs in
-d --dryrun Do not store grades
";
list($options, $unrecognised) = cli_get_params([
'help' => false,
'dryrun' => false,
'studyplan' => null,
'all' => false,
'file' => "/tmp/generategrades.json"
], [
'h' => 'help',
's' => 'studyplan',
'a' => 'all',
'f' => 'file',
'd' => 'dryrun'
]);
if ($unrecognised) {
$unrecognised = implode(PHP_EOL . ' ', $unrecognised);
cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
}
if ($options['help']) {
cli_writeln($usage);
exit(2);
}
2023-08-24 23:02:41 +02:00
/////////////////////////////////.
$user = get_admin();
if (!$user) {
cli_error("Unable to find admin user in DB.");
}
$auth = empty($user->auth) ? 'manual' : $user->auth;
if ($auth == 'nologin' or !is_enabled_auth($auth)) {
cli_error(sprintf("User authentication is either 'nologin' or disabled. Check Moodle authentication method for '%s'",
$user->username));
}
$authplugin = get_auth_plugin($auth);
$authplugin->sync_roles($user);
login_attempt_valid($user);
complete_user_login($user);
2023-08-24 23:02:41 +02:00
////////////////////////////////.
if (empty($options['studyplan']) && empty($options["all"])) {
cli_error('Missing mandatory argument studyplan.', 2);
}
2023-08-24 23:02:41 +02:00
if (!empty($options["all"])) {
2023-08-25 10:41:56 +02:00
$plans = studyplan::find_all();
} else {
$plans = studyplan::find_by_shortname($options["studyplan"]);
}
$generator = new gradegenerator();
$generator->fromFile($options["file"]);
$assignments = [];
cli_writeln(count($plans)." studyplans found:");
2023-08-24 23:02:41 +02:00
foreach ($plans as $plan) {
cli_heading($plan->name());
$users = $plan->find_linked_users();
2023-08-24 23:02:41 +02:00
foreach ($plan->pages() as $page) {
$lines = studyline::find_page_children($page);
2023-08-24 23:02:41 +02:00
foreach ($lines as $line) {
cli_writeln(" ** {$line->name()} **");
$items = studyitem::find_studyline_children($line);
2023-08-24 23:02:41 +02:00
foreach ($items as $item) {
2023-08-25 10:41:56 +02:00
if ($item->type() == studyitem::COURSE) {
// Only handle courses for now.
$courseinfo = $item->getcourseinfo();
cli_writeln(" # {$courseinfo->shortname()}");
2023-08-24 23:02:41 +02:00
if ($courseinfo->course()->startdate <= time()) {
2023-08-24 23:02:41 +02:00
foreach ($users as $u) {
cli_writeln(" -> {$u->firstname} {$u->lastname} <-");
$gradables = gradeinfo::list_studyitem_gradables($item);
2023-08-24 23:02:41 +02:00
$gen = $generator->generate($u->username, $line->shortname(), $gradables);
foreach ($gen as $gg) {
$g = $gg->gi;
$gi = $g->getGradeitem();
$name = $gi->itemname;
$grade = $gg->gradetext;
cli_write (" - {$name} = {$grade}");
2023-08-24 23:02:41 +02:00
// Check if the item is alreaady graded for this user.
$existing = $count = $DB->count_records_select('grade_grades', 'itemid = :gradeitemid AND finalgrade IS NOT NULL and userid = :userid',
['gradeitemid' => $gi->id, 'userid' => $u->id]);
2023-08-24 23:02:41 +02:00
if (!$existing) {
if ($gg->grade > 0) {
if ($gi->itemmodule == "assign") {
// If it is an assignment, submit though that interface .
list($c, $cminfo) = get_course_and_cm_from_instance($gi->iteminstance, $gi->itemmodule);
2023-08-25 09:33:42 +02:00
$cmctx = \context_module::instance($cminfo->id);
$a = new \assign($cmctx, $cminfo, $c);
2023-08-24 23:02:41 +02:00
$ug = $a->get_user_grade($u->id, true);
$ug->grade = grade_floatval($gg->grade);
$ug->grader = $USER->id;
$ug->feedbacktext = nl2br( htmlspecialchars($gg->fb));
$ug->feedbackformat = FORMAT_HTML;
2023-08-24 23:02:41 +02:00
if (!$options["dryrun"]) {
$a->update_grade($ug);
2023-08-24 23:02:41 +02:00
grade_regrade_final_grades($c->id, $u->id, $gi);
cli_writeln(" ... Stored");
} else {
cli_writeln(" ... (Dry Run)");
}
} else {
2023-08-24 23:02:41 +02:00
// Otherwise, set the grade through the manual grading override.
cli_writeln(" ... Cannot store");
}
} else {
cli_writeln(" ... No grade");
}
} else {
cli_writeln(" ... Already graded");
}
}
}
2023-08-25 09:33:42 +02:00
} else {
cli_writeln(" Skipping since it has not started yet");
}
}
}
}
}
}
$generator->toFile($options["file"]);