2023-05-17 21:19:14 +02:00
|
|
|
<?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/>.
|
|
|
|
/**
|
2023-08-27 17:00:23 +02:00
|
|
|
* Generate random grades and feedback to initialize development environment
|
2023-08-24 23:02:41 +02:00
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
namespace local_treestudyplan\local;
|
|
|
|
|
|
|
|
use Exception;
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Generate random grades and feedback to initialize development environment
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
class gradegenerator {
|
2023-08-27 17:00:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table to store ficional skill data for students
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private $table = [];
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Lorem ipsum text to use if fortune is not available
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private static $loremipsum = [
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
|
|
|
"Etiam scelerisque ligula porttitor velit sollicitudin blandit.",
|
|
|
|
"Praesent laoreet nisi id lacus laoreet volutpat.",
|
|
|
|
"Donec rutrum tortor tempus lectus malesuada, ut pretium eros vehicula.",
|
|
|
|
"Phasellus vulputate tortor vehicula mauris porta ultricies.",
|
|
|
|
"Ut et lacus sit amet nisl facilisis elementum.",
|
|
|
|
"Vestibulum ut mauris ac justo tincidunt hendrerit.",
|
|
|
|
"Fusce congue nulla quis elit facilisis malesuada.",
|
|
|
|
"Aenean ornare eros placerat ipsum fringilla, sed imperdiet felis imperdiet.",
|
|
|
|
"Ut malesuada risus ultricies arcu dapibus, quis lobortis eros maximus.",
|
|
|
|
"Nam ullamcorper dolor ac est tristique, vel blandit tortor tristique.",
|
|
|
|
"Quisque quis lorem vitae leo lobortis posuere.",
|
|
|
|
"Nulla ac enim consectetur, rhoncus eros sed, malesuada enim.",
|
|
|
|
"Vestibulum lobortis lacus ac dolor pulvinar, gravida tincidunt dolor bibendum.",
|
|
|
|
"Maecenas fringilla urna eget sem bibendum, non lacinia lorem tempus.",
|
|
|
|
"Nullam quis metus sagittis, pharetra orci eget, ultrices nunc.",
|
|
|
|
"Morbi et ante at ipsum sodales porta.",
|
|
|
|
"Morbi vel neque in urna vestibulum vestibulum eu quis lectus.",
|
|
|
|
"Nam consequat dolor at enim vestibulum, ac gravida nisl consequat.",
|
|
|
|
"Phasellus ac libero vestibulum, vulputate tellus at, viverra dui.",
|
|
|
|
"Vivamus venenatis magna a nunc cursus, eget laoreet velit malesuada.",
|
|
|
|
"Cras fermentum velit vitae tellus sodales, vulputate semper purus porta.",
|
|
|
|
"Cras ultricies orci in est elementum, at laoreet erat tempus.",
|
|
|
|
"In non magna et lorem sagittis sollicitudin sit amet et est.",
|
|
|
|
"Etiam vitae augue ac turpis volutpat iaculis a vitae enim.",
|
|
|
|
"Integer pharetra quam ac tortor porta dignissim.",
|
|
|
|
"Pellentesque ullamcorper neque vitae ligula rhoncus accumsan.",
|
|
|
|
"Nullam in lectus sit amet est faucibus elementum vitae vel risus.",
|
|
|
|
"Aenean vehicula libero ut convallis blandit.",
|
|
|
|
"Aenean id mi facilisis, tristique enim vel, egestas lorem.",
|
|
|
|
"Mauris suscipit dui eget neque gravida, vel pellentesque leo gravida.",
|
|
|
|
"Quisque quis elit at velit maximus viverra ultricies in nisi.",
|
|
|
|
"Vivamus et orci nec magna hendrerit egestas sed quis arcu.",
|
|
|
|
"Suspendisse semper tortor sed justo iaculis volutpat.",
|
|
|
|
"Praesent interdum dolor nec ultricies imperdiet.",
|
|
|
|
"Vivamus tristique justo quis tellus commodo, at faucibus justo auctor.",
|
|
|
|
"Praesent pharetra tellus vel nunc mattis pharetra.",
|
|
|
|
"Cras a dui quis arcu rutrum ullamcorper sit amet et sem.",
|
|
|
|
"Aenean porttitor risus ac enim tempor posuere.",
|
|
|
|
"Mauris bibendum augue ac vehicula mattis.",
|
|
|
|
"Vestibulum nec justo vehicula, euismod enim sed, convallis magna.",
|
|
|
|
"Praesent ultrices elit vitae velit dignissim dignissim.",
|
|
|
|
"Curabitur vehicula velit vitae tortor consequat consectetur sit amet at leo.",
|
|
|
|
"Sed lobortis neque a magna facilisis aliquam.",
|
|
|
|
"Phasellus a libero in sem aliquam varius.",
|
|
|
|
"Mauris tincidunt ligula a risus efficitur euismod.",
|
|
|
|
"Sed pharetra diam ac neque tempus convallis.",
|
|
|
|
"Donec at ipsum elementum ex hendrerit laoreet mollis non elit.",
|
|
|
|
"Praesent eu arcu sollicitudin, fermentum tellus at, blandit dolor.",
|
|
|
|
"Curabitur in lectus consequat, bibendum ligula vitae, semper lacus.",
|
|
|
|
"Aenean eu risus non sem pretium dictum.",
|
|
|
|
"Praesent nec risus vestibulum quam venenatis tempor.",
|
|
|
|
"Nullam rhoncus ex a quam egestas, eu auctor enim lobortis.",
|
|
|
|
"Nam luctus ante id lacus scelerisque, quis blandit ante elementum.",
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Generate random feedback from fortune or internal loremipsum text
|
|
|
|
* @return string Randomized text to use as feedback
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
private function generatedfeedback() {
|
|
|
|
if (file_exists("/usr/games/fortune")) {
|
2023-08-25 09:44:34 +02:00
|
|
|
// Get a fortune if it is available.
|
2023-05-17 21:19:14 +02:00
|
|
|
return shell_exec("/usr/games/fortune -n 160 -e disclaimer literature science pratchett wisdom education");
|
|
|
|
} else {
|
2023-08-25 09:44:34 +02:00
|
|
|
// Get a random loremipsum string.
|
2023-08-25 17:33:20 +02:00
|
|
|
return self::$loremipsum[rand(0, count(self::$loremipsum) - 1)];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function __construct() {
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
2023-08-27 17:00:23 +02:00
|
|
|
* Register a new student in the skill table
|
|
|
|
* @param string $student Student identifier
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function addstudent(string $student) {
|
|
|
|
if (!array_key_exists($student, $this->table)) {
|
|
|
|
$this->table[$student] = [
|
|
|
|
"intelligence" => rand(70, 100),
|
|
|
|
"endurance" => rand(70, 100),
|
2023-05-17 21:19:14 +02:00
|
|
|
"skills" => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Add a specific skill for a student
|
2023-08-27 17:00:23 +02:00
|
|
|
* @param string $student Student identifier
|
|
|
|
* @param string $skill Skill identifier
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function addskill(string $student, string $skill) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->addstudent($student);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!array_key_exists($skill, $this->table[$student]["skills"])) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$int = $this->table[$student]["intelligence"];
|
|
|
|
$end = $this->table[$student]["endurance"];
|
2023-08-24 23:02:41 +02:00
|
|
|
$this->table[$student]["skills"][$skill] = [
|
|
|
|
"intelligence" => min(100, $int + rand(-30, 30)),
|
|
|
|
"endurance" => min(100, $end + rand(-10, 10)),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Register a firstname and lastname for a student identifier for reference in the json file.
|
|
|
|
* @param string $student Student identifier
|
|
|
|
* @param string $firstname First name for student
|
|
|
|
* @param string $lastname Last name for student
|
|
|
|
*/
|
|
|
|
public function add_username_info(string $student, string $firstname, string $lastname) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->addstudent($student);
|
|
|
|
$this->table[$student]["firstname"] = $firstname;
|
|
|
|
$this->table[$student]["lastname"] = $lastname;
|
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
2023-08-27 17:00:23 +02:00
|
|
|
* Generate a number of random results for a given student and skill
|
|
|
|
* @param string $student Student identifier
|
|
|
|
* @param string $skill Skill identifier
|
|
|
|
* @param int $count Number of results to generate
|
|
|
|
* @return array Raw outcomes
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function generateraw($student, $skill, $count ) {
|
|
|
|
$this->addskill($student, $skill);
|
2023-05-17 21:19:14 +02:00
|
|
|
$int = $this->table[$student]["skills"][$skill]["intelligence"];
|
|
|
|
$end = $this->table[$student]["skills"][$skill]["endurance"];
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
$gaveup = false;
|
2023-08-25 13:04:19 +02:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$r = new \stdClass;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($gaveup) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$r->done = !$gaveup;
|
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
$r->done = (rand(0, $end) > 20); // Determine if the assignment was done.
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r->done) {
|
2023-08-25 09:44:34 +02:00
|
|
|
$score = rand(0, $int);
|
2023-08-25 10:41:56 +02:00
|
|
|
$r->result = ($score > 20); // Determine if the assignment was successful.
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$r->result) {
|
|
|
|
$r->failed = !($score > 10);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-25 10:41:56 +02:00
|
|
|
$r->result = false; // Make sure a result property is always there.
|
2023-05-17 21:19:14 +02:00
|
|
|
$r->failed = true;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
// Aways generate a little feedback.
|
2023-05-17 21:19:14 +02:00
|
|
|
$r->fb = $this->generatedfeedback();
|
|
|
|
$results[] = $r;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$gaveup && $i >= 3) {
|
2023-08-25 11:52:05 +02:00
|
|
|
/* There is a slight chance the students with low endurance
|
|
|
|
for this course will stop with this course's work entirely.
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
$gaveup = (rand(0, $end) < 15);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Generate results for a number of gradeinfo
|
2023-08-27 17:00:23 +02:00
|
|
|
* Returns (object)[ "gi" => <gradeinfo object>,
|
|
|
|
* "grade" => <randomized grade>
|
|
|
|
* "fb" => <randomized feedback>
|
|
|
|
* ];
|
|
|
|
* @param string $student Student identifier
|
|
|
|
* @param string $skill Skill identifier
|
|
|
|
* @param gradeinfo[] $gradeinfos
|
|
|
|
* @return stdClass[] List of gradeinfo related results ready to add
|
|
|
|
*/
|
|
|
|
public function generate(string $student, string $skill, array $gradeinfos ) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-25 13:04:19 +02:00
|
|
|
$table = "local_treestudyplan_gradecfg";
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
$rlist = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
$gen = $this->generateraw($student, $skill, count($gradeinfos));
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-25 13:04:19 +02:00
|
|
|
for ($i = 0; $i < count($gradeinfos); $i++) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$g = $gradeinfos[$i];
|
2023-08-25 17:33:20 +02:00
|
|
|
$gi = $g->get_gradeitem();
|
2023-05-17 21:19:14 +02:00
|
|
|
$gr = $gen[$i];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// First get the configured interpretation for this scale or grade.
|
2023-05-17 21:19:14 +02:00
|
|
|
$scale = $gi->load_scale();
|
2023-08-24 23:02:41 +02:00
|
|
|
if ( isset($scale)) {
|
2023-08-25 10:41:56 +02:00
|
|
|
$gradecfg = $DB->get_record($table, ["scale_id" => $scale->id]);
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if ($gi->grademin == 0) {
|
2023-08-25 10:41:56 +02:00
|
|
|
$gradecfg = $DB->get_record($table, ["grade_points" => $gi->grademax]);
|
2023-08-25 09:33:42 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$gradecfg = null;
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Next generate the grade.
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($gradecfg) {
|
|
|
|
if (!$gr->done) {
|
|
|
|
// INCOMPLETE.
|
2023-08-25 09:44:34 +02:00
|
|
|
// Fair chance of teacher forgetting to set incomplete to "no evidence".
|
|
|
|
$grade = 0;
|
2023-08-25 10:41:56 +02:00
|
|
|
$r = (object)["gi" => $g, "grade" => $grade, "fb" => "" ];
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if (!$gr->result) {
|
2023-08-25 17:33:20 +02:00
|
|
|
$grade = rand($gradecfg->min_progress, $gradecfg->min_completed - 1 );
|
2023-08-25 10:41:56 +02:00
|
|
|
$r = (object)["gi" => $g, "grade" => $grade, "fb" => $gr->fb ];
|
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
// COMPLETED.
|
2023-08-25 10:41:56 +02:00
|
|
|
$r = (object)["gi" => $g, "grade" => rand( $gradecfg->min_completed, $gi->grademax ), "fb" => $gr->fb ];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$r->gradetext = $r->grade;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ( isset($scale)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$scaleitems = $scale->load_items();
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r->grade > 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$r->gradetext = trim($scale->get_nearest_item($r->grade));
|
|
|
|
} else {
|
|
|
|
$r->gradetext = "-";
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if ($gi->gradepass > 0) {
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$gr->done) {
|
|
|
|
// INCOMPLETe or FAILED.
|
2023-08-25 17:33:20 +02:00
|
|
|
$grade = rand(0, $gi->gradepass / 2);
|
|
|
|
$r = (object)["gi" => $g, "grade" => $grade, "fb" => ($grade > 0) ? $gr->fb : "" ];
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if (!$gr->result) {
|
2023-08-25 12:16:51 +02:00
|
|
|
// PROGRESS.
|
2023-08-25 17:33:20 +02:00
|
|
|
$r = (object)["gi" => $g, "grade" => rand( round($gi->gradepass / 2), $gi->gradepass - 1 ), "fb" => $gr->fb ];
|
2023-08-25 10:41:56 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
// COMPLETED.
|
2023-08-25 10:41:56 +02:00
|
|
|
$r = (object)["gi" => $g, "grade" => rand( $gi->gradepass, $gi->grademax ), "fb" => $gr->fb ];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$r->gradetext = $r->grade;
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Blind assumptions if nothing is provided.
|
2023-08-25 09:44:34 +02:00
|
|
|
// Over 55% of range is completed.
|
|
|
|
// Under 35% is not done.
|
2023-05-17 21:19:14 +02:00
|
|
|
$range = floatval($gi->grademax - $gi->grademin);
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$gr->done) {
|
|
|
|
// INCOMPLETe or FAILED.
|
2023-08-25 10:41:56 +02:00
|
|
|
$grade = rand(0, round($range * 0.35) - 1);
|
2023-08-25 11:52:05 +02:00
|
|
|
$r = (object)[
|
|
|
|
"gi" => $g,
|
2023-08-25 17:33:20 +02:00
|
|
|
"grade" => $gi->grademin + $grade,
|
|
|
|
"fb" => ($grade > 0) ? $gr->fb : ""
|
2023-08-25 11:52:05 +02:00
|
|
|
];
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if (!$gr->result) {
|
2023-08-25 12:16:51 +02:00
|
|
|
// PROGRESS.
|
2023-08-25 11:52:05 +02:00
|
|
|
$r = (object)[
|
|
|
|
"gi" => $g,
|
2023-08-25 17:33:20 +02:00
|
|
|
"grade" => $gi->grademin + rand(round($range * 0.35), round($range * 0.55) - 1 ),
|
2023-08-25 11:52:05 +02:00
|
|
|
"fb" => $gr->fb
|
|
|
|
];
|
2023-08-25 10:41:56 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
// COMPLETED.
|
2023-08-25 11:52:05 +02:00
|
|
|
$r = (object)[
|
|
|
|
"gi" => $g,
|
2023-08-25 17:33:20 +02:00
|
|
|
"grade" => $gi->grademin + rand(round($range * 0.55) , $range ),
|
2023-08-25 11:52:05 +02:00
|
|
|
"fb" => $gr->fb
|
|
|
|
];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$r->gradetext = $r->grade;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rlist[] = $r;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rlist;
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Get a fictional student's performance stats
|
|
|
|
* @param string $student Student identifier
|
|
|
|
* @return array Used stats for student
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function getstats($student) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->table[$student];
|
|
|
|
}
|
2023-08-27 17:00:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store skills table into a string
|
|
|
|
* @return string|null Json encoded string of the skills table
|
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
public function serialize(): ?string {
|
2023-05-17 21:19:14 +02:00
|
|
|
return json_encode([
|
2023-08-24 23:02:41 +02:00
|
|
|
"table" => $this->table], JSON_PRETTY_PRINT);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Load skills table from stored data
|
|
|
|
* @param string $data Json encoded string of the skills table
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public function unserialize(string $data): void {
|
2023-08-24 23:02:41 +02:00
|
|
|
$o = json_decode($data, true);
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->table = $o["table"];
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Store skills table to file
|
|
|
|
* @param string $filename The file to use
|
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
public function to_file(string $filename) {
|
2023-05-19 16:45:15 +02:00
|
|
|
$filename = self::expand_tilde($filename);
|
2023-08-24 23:02:41 +02:00
|
|
|
file_put_contents($filename, $this->serialize());
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Load skills table from file
|
|
|
|
* @param string $filename The file to use
|
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
public function from_file(string $filename) {
|
2023-05-19 16:45:15 +02:00
|
|
|
$filename = self::expand_tilde($filename);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (file_exists($filename)) {
|
2023-08-24 23:09:20 +02:00
|
|
|
try {
|
2023-05-17 21:19:14 +02:00
|
|
|
$json = file_get_contents($filename);
|
|
|
|
$this->unserialize($json);
|
2023-08-25 10:41:56 +02:00
|
|
|
} catch (Exception $x) {
|
2023-05-17 21:19:14 +02:00
|
|
|
cli_problem("ERROR loading from file");
|
2023-08-24 23:02:41 +02:00
|
|
|
throw $x; // Throw X up again to show the output.
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Internal function to properly handle the ~ symbol in unix context
|
|
|
|
* @param string $path A unix path
|
|
|
|
* @return string Unix path with ~ symbol properly expanded to user home dir
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
private static function expand_tilde($path) {
|
2023-05-19 16:45:15 +02:00
|
|
|
if (function_exists('posix_getuid') && strpos($path, '~') !== false) {
|
|
|
|
$info = posix_getpwuid(posix_getuid());
|
|
|
|
$path = str_replace('~', $info['dir'], $path);
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-19 16:45:15 +02:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|