2023-07-27 15:49:59 +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/>.
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2023-07-27 15:49:59 +02:00
|
|
|
namespace local_treestudyplan;
|
|
|
|
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
class period {
|
|
|
|
const TABLE = "local_treestudyplan_period";
|
|
|
|
private static $CACHE = [];
|
2023-07-27 16:58:23 +02:00
|
|
|
private static $PAGECACHE = [];
|
|
|
|
|
2023-07-27 15:49:59 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
private $r; // Holds database record.
|
2023-07-27 15:49:59 +02:00
|
|
|
private $id;
|
|
|
|
private $page;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function aggregator() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this->studyplan->aggregator();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache constructors to avoid multiple creation events in one session.
|
|
|
|
public static function findById($id): self {
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!array_key_exists($id, self::$CACHE)) {
|
2023-07-27 15:49:59 +02:00
|
|
|
self::$CACHE[$id] = new self($id);
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-07-27 15:49:59 +02:00
|
|
|
return self::$CACHE[$id];
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:58:23 +02:00
|
|
|
/**
|
|
|
|
* Find a period by page and period number [1..$page->periods()]
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function find(studyplanpage $page, $periodnr): self{
|
2023-07-27 16:58:23 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($periodnr < 1) {
|
|
|
|
// Clamp period index .
|
2023-07-27 16:58:23 +02:00
|
|
|
$periodnr = 1;
|
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
try {
|
2023-08-24 23:02:41 +02:00
|
|
|
$id = $DB->get_field(self::TABLE, "id", ["page_id"=>$page->id(), "period" => $periodnr], MUST_EXIST);
|
2023-07-27 16:58:23 +02:00
|
|
|
$period = self::findById($id);
|
2023-08-24 23:02:41 +02:00
|
|
|
} catch(\dml_missing_record_exception $x) {
|
2023-07-27 16:58:23 +02:00
|
|
|
// Period does not exist - create one ...
|
2023-08-24 23:02:41 +02:00
|
|
|
// Make a best guess estimate of the start and end date, based on surrounding periods,.
|
2023-08-25 09:44:34 +02:00
|
|
|
// Or specified duration of the page and the sequence of the periods .
|
2023-07-27 16:58:23 +02:00
|
|
|
$pcount = $page->periods();
|
|
|
|
$ystart = $page->startdate()->getTimestamp();
|
|
|
|
$yend = $page->enddate()->getTimestamp();
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
// Estimate the period's timing to make a reasonable first guess.
|
|
|
|
$ydelta = $yend - $ystart;
|
2023-07-27 16:58:23 +02:00
|
|
|
$ptime = $ydelta / $pcount;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-08-24 23:09:20 +02:00
|
|
|
try {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Check if we have a previous period to glance the end date of as a reference.
|
|
|
|
$startdate = $DB->get_field(self::TABLE, "enddate", ["page_id"=>$page->id(), "period" => $periodnr-1], MUST_EXIST);
|
|
|
|
$pstart = strtotime($startdate)+(24*60*60); // Add one day.
|
|
|
|
} catch(\dml_missing_record_exception $x2) {
|
|
|
|
// If not, do a fair guess.
|
2023-07-27 16:58:23 +02:00
|
|
|
$pstart = $ystart + (($periodnr-1)*$ptime);
|
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
try {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Check if we have a next period to glance the start date of as a reference.
|
|
|
|
$enddate = $DB->get_field(self::TABLE, "startdate", ["page_id"=>$page->id(), "period" => $periodnr+1], MUST_EXIST);
|
|
|
|
$pstart = strtotime($enddate)-(24*60*60); // subtract one day.
|
|
|
|
} catch(\dml_missing_record_exception $x2) {
|
|
|
|
// If not, do a fair guess.
|
2023-07-27 16:58:23 +02:00
|
|
|
$pend = $pstart + $ptime;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// And create the period.
|
2023-07-27 16:58:23 +02:00
|
|
|
$period = self::add([
|
|
|
|
'page_id' => $page->id(),
|
|
|
|
'period' => $periodnr,
|
2023-08-24 23:02:41 +02:00
|
|
|
'fullname' => \get_string("period_default_fullname", "local_treestudyplan", $periodnr),
|
|
|
|
'shortname' => \get_string("period_default_shortname", "local_treestudyplan", $periodnr),
|
|
|
|
'startdate' => date("Y-m-d", $pstart),
|
|
|
|
'enddate' => date("Y-m-d", $pend),
|
2023-07-27 16:58:23 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
return $period;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache constructors to avoid multiple creation events in one session.
|
|
|
|
public static function findForPage(studyplanpage $page): array {
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!array_key_exists($page->id(), self::$PAGECACHE)) {
|
2023-07-27 16:58:23 +02:00
|
|
|
$periods = [];
|
2023-08-25 09:44:34 +02:00
|
|
|
// Find and add the periods to an array with the period sequence as a key.
|
2023-08-24 23:02:41 +02:00
|
|
|
for($i=1; $i <= $page->periods(); $i++) {
|
|
|
|
$period = self::find($page, $i);
|
2023-07-27 16:58:23 +02:00
|
|
|
$periods[$i] = $period;
|
|
|
|
}
|
|
|
|
self::$PAGECACHE[$page->id()] = $periods;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-07-27 16:58:23 +02:00
|
|
|
return self::$PAGECACHE[$page->id()];
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:49:59 +02:00
|
|
|
private function __construct($id) {
|
|
|
|
global $DB;
|
|
|
|
$this->id = $id;
|
2023-08-24 23:02:41 +02:00
|
|
|
$this->r = $DB->get_record(self::TABLE, ['id' => $id]);
|
2023-07-27 15:49:59 +02:00
|
|
|
$this->page = studyplanpage::findById($this->r->page_id);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function id() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function studyplan() : studyplan {
|
|
|
|
return $this->page->studyplan();
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function page() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this->page;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function shortname() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this->r->shortname;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function fullname() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this->r->fullname;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function period() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this->r->period;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function startdate() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return new \DateTime($this->r->startdate);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function enddate() {
|
|
|
|
if ($this->r->enddate && strlen($this->r->enddate) > 0) {
|
2023-07-27 15:49:59 +02:00
|
|
|
return new \DateTime($this->r->enddate);
|
2023-08-24 23:09:20 +02:00
|
|
|
} else{
|
2023-08-25 09:44:34 +02:00
|
|
|
// Return a date 100 years into the future.
|
2023-07-27 15:49:59 +02:00
|
|
|
return (new \DateTime($this->r->startdate))->add(new \DateInterval("P100Y"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function structure($value=VALUE_REQUIRED) {
|
2023-07-27 15:49:59 +02:00
|
|
|
return new \external_single_structure([
|
2023-07-27 16:58:23 +02:00
|
|
|
"id" => new \external_value(PARAM_INT, 'id of period'),
|
|
|
|
"fullname" => new \external_value(PARAM_TEXT, 'Full name of period'),
|
|
|
|
"shortname"=> new \external_value(PARAM_TEXT, 'Short name of period'),
|
2023-07-27 15:49:59 +02:00
|
|
|
"period" => new \external_value(PARAM_INT, 'period sequence'),
|
2023-07-27 16:58:23 +02:00
|
|
|
"startdate" => new \external_value(PARAM_TEXT, 'start date of period'),
|
|
|
|
"enddate" => new \external_value(PARAM_TEXT, 'end date of period'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'Period info', $value);
|
2023-07-27 15:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function model() {
|
2023-07-27 15:49:59 +02:00
|
|
|
return [
|
|
|
|
'id' => $this->r->id,
|
|
|
|
'fullname' => $this->r->fullname,
|
|
|
|
'shortname' => $this->r->shortname,
|
|
|
|
'period' => $this->r->period,
|
|
|
|
'startdate' => $this->r->startdate,
|
|
|
|
'enddate' => $this->r->enddate,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:58:23 +02:00
|
|
|
/**
|
|
|
|
* Do not use directly to add periods, unless performing import
|
|
|
|
* The static find() and findForPage() functions create the period if needed
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function add($fields) {
|
2023-07-27 16:58:23 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
if (!isset($fields['page_id'])) {
|
2023-07-27 16:58:23 +02:00
|
|
|
throw new \InvalidArgumentException("parameter 'page_id' missing");
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!isset($fields['period'])) {
|
2023-07-27 16:58:23 +02:00
|
|
|
throw new \InvalidArgumentException("parameter 'period' missing");
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($DB->record_exists(self::TABLE, ["page_id"=>$fields["page_id"], "period"=>$fields["period"]])) {
|
2023-07-27 16:58:23 +02:00
|
|
|
throw new \InvalidArgumentException("record already exists for specified page and period");
|
2023-07-27 15:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$addable = ['page_id', 'fullname', 'shortname', 'period', 'startdate', 'enddate'];
|
2023-07-27 16:58:23 +02:00
|
|
|
$info = [ ];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($addable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-07-27 15:49:59 +02:00
|
|
|
$info[$f] = $fields[$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$id = $DB->insert_record(self::TABLE, $info);
|
2023-08-24 23:02:41 +02:00
|
|
|
unset(self::$PAGECACHE[$fields['page_id']]); // invalidate the cache for this page.
|
|
|
|
return self::findById($id); // make sure the new page is immediately cached.
|
2023-07-27 15:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function edit($fields) {
|
2023-07-27 15:49:59 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
$editable = ['fullname', 'shortname', 'startdate', 'enddate'];
|
|
|
|
$info = ['id' => $this->id, ];
|
|
|
|
foreach ($editable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-07-27 15:49:59 +02:00
|
|
|
$info[$f] = $fields[$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$DB->update_record(self::TABLE, $info);
|
2023-08-24 23:02:41 +02:00
|
|
|
//reload record after edit.
|
|
|
|
$this->r = $DB->get_record(self::TABLE, ['id' => $this->id], "*", MUST_EXIST);
|
|
|
|
unset(self::$PAGECACHE[$this->r->page_id]); // invalidate the cache for this page.
|
2023-07-27 15:49:59 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function delete() {
|
2023-07-27 15:49:59 +02:00
|
|
|
global $DB;
|
2023-07-27 16:58:23 +02:00
|
|
|
$DB->delete_records(self::TABLE, ['id' => $this->id]);
|
2023-08-24 23:02:41 +02:00
|
|
|
unset(self::$PAGECACHE[$this->r->page_id]); // invalidate the cache for this page.
|
2023-07-27 16:58:23 +02:00
|
|
|
return success::success();
|
2023-07-27 15:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function page_structure($value=VALUE_REQUIRED) {
|
|
|
|
return new \external_multiple_structure(self::structure(), "The periods in the page", $value);
|
2023-07-27 15:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function page_model(studyplanpage $page) {
|
2023-07-27 16:58:23 +02:00
|
|
|
$model = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach (self::findForPage($page) as $p) {
|
2023-07-27 16:58:23 +02:00
|
|
|
$model[] = $p->model();
|
2023-07-27 15:49:59 +02:00
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|