moodle_local_treestudyplan/classes/studyplanpage.php

565 lines
20 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;
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/externallib.php');
class studyplanpage {
2023-08-27 21:23:39 +02:00
/** @var string */
const TABLE = "local_treestudyplan_page";
2023-08-25 17:33:20 +02:00
private static $cache = [];
2023-08-27 21:23:39 +02:00
/**
2023-08-27 22:20:17 +02:00
* Holds database record
2023-08-27 21:23:39 +02:00
* @var stdClass
*/
2023-08-24 23:02:41 +02:00
private $r; // Holds database record.
2023-08-27 21:23:39 +02:00
/** @var int */
private $id;
2023-08-27 21:23:39 +02:00
/** @var studyplan*/
private $studyplan;
2023-08-24 23:02:41 +02:00
public function aggregator() {
return $this->studyplan->aggregator();
}
// Cache constructors to avoid multiple creation events in one session.
2023-08-25 17:33:20 +02:00
public static function find_by_id($id): self {
if (!array_key_exists($id, self::$cache)) {
self::$cache[$id] = new self($id);
2023-08-24 23:02:41 +02:00
}
2023-08-25 17:33:20 +02:00
return self::$cache[$id];
}
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-08-25 17:33:20 +02:00
$this->studyplan = studyplan::find_by_id($this->r->studyplan_id);
}
2023-08-27 22:20:17 +02:00
/**
* Return database identifier
* @return int
*/
2023-08-24 23:02:41 +02:00
public function id() {
return $this->id;
}
public function studyplan() : studyplan {
return $this->studyplan;
}
2023-08-27 22:20:17 +02:00
/**
* Return short name
* @return string
*/
2023-08-24 23:02:41 +02:00
public function shortname() {
return $this->r->shortname;
}
2023-08-24 23:02:41 +02:00
public function periods() {
return $this->r->periods;
}
2023-08-27 22:20:17 +02:00
/**
* Return full name
* @return string
*/
2023-08-24 23:02:41 +02:00
public function fullname() {
return $this->r->fullname;
}
2023-08-24 23:02:41 +02:00
public function startdate() {
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) {
return new \DateTime($this->r->enddate);
2023-08-25 10:41:56 +02:00
} else {
2023-08-25 09:44:34 +02:00
// Return a date 100 years into the future.
return (new \DateTime($this->r->startdate))->add(new \DateInterval("P100Y"));
}
}
2023-08-27 22:20:17 +02:00
/**
* Webservice structure for basic info
* @param int $value Webservice requirement constant
* @return external_single_structure Webservice output structure
*/
2023-08-25 13:04:19 +02:00
public static function simple_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
"id" => new \external_value(PARAM_INT, 'id of studyplan page'),
"fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'),
2023-08-25 10:41:56 +02:00
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'),
"periods" => new \external_value(PARAM_INT, 'number of periods in studyplan page'),
2023-08-25 10:41:56 +02:00
"description" => new \external_value(PARAM_TEXT, 'description of studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_structure(),
2023-08-24 23:02:41 +02:00
], 'Studyplan page basic info', $value);
}
2023-08-27 22:20:17 +02:00
/**
* Webservice model for basic info
* @return array Webservice data model
*/
2023-08-24 23:02:41 +02:00
public function simple_model() {
return [
'id' => $this->r->id,
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'periods' => $this->r->periods,
'description' => $this->r->description,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_model($this),
];
}
2023-08-27 21:57:21 +02:00
/**
* Webservice structure for editor info
* @param int $value Webservice requirement constant
* @return \external_single_structure Webservice output structure
*/
2023-08-25 13:04:19 +02:00
public static function editor_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
"id" => new \external_value(PARAM_INT, 'id of studyplan'),
"fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'),
2023-08-25 10:41:56 +02:00
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan page'),
"periods" => new \external_value(PARAM_INT, 'number of periods in studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan page'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan page'),
"studylines" => new \external_multiple_structure(studyline::editor_structure()),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_structure(),
2023-08-24 23:02:41 +02:00
], 'Studyplan page full structure', $value);
}
2023-08-27 21:57:21 +02:00
/**
* Webservice model for editor info
* @return array Webservice data model
*/
2023-08-24 23:02:41 +02:00
public function editor_model() {
global $DB;
$model = [
'id' => $this->r->id,
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'periods' => $this->r->periods,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
'studylines' => [],
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_model($this),
];
$children = studyline::find_page_children($this);
2023-08-24 23:02:41 +02:00
foreach ($children as $c) {
$model['studylines'][] = $c->editor_model();
}
return $model;
}
2023-08-24 23:02:41 +02:00
public static function add($fields) {
global $CFG, $DB;
2023-08-24 23:02:41 +02:00
if (!isset($fields['studyplan_id'])) {
throw new \InvalidArgumentException("parameter 'studyplan_id' missing");
}
2023-08-24 23:02:41 +02:00
$addable = ['studyplan_id', 'fullname', 'shortname', 'description', 'periods', 'startdate', 'enddate'];
$info = ['enddate' => null ];
2023-08-24 23:02:41 +02:00
foreach ($addable as $f) {
if (array_key_exists($f, $fields)) {
$info[$f] = $fields[$f];
}
}
2023-08-24 23:02:41 +02:00
if (!isset($addable['periods'])) {
2023-08-16 23:15:48 +02:00
$addable['periods'] = 4;
2023-08-24 23:02:41 +02:00
} else if ($addable['periods'] < 1) {
2023-08-16 23:15:48 +02:00
$addable['periods'] = 1;
}
$id = $DB->insert_record(self::TABLE, $info);
2023-08-25 17:33:20 +02:00
return self::find_by_id($id); // Make sure the new page is immediately cached.
}
2023-08-24 23:02:41 +02:00
public function edit($fields) {
global $DB;
2023-08-24 23:02:41 +02:00
$editable = ['fullname', 'shortname', 'description', 'periods', 'startdate', 'enddate'];
$info = ['id' => $this->id, ];
foreach ($editable as $f) {
if (array_key_exists($f, $fields)) {
$info[$f] = $fields[$f];
}
}
2023-08-24 23:02:41 +02:00
if (isset($info['periods']) && $info['periods'] < 1) {
2023-08-16 23:15:48 +02:00
$info['periods'] = 1;
}
$DB->update_record(self::TABLE, $info);
2023-08-25 12:16:51 +02:00
// Reload record after edit.
2023-08-24 23:02:41 +02:00
$this->r = $DB->get_record(self::TABLE, ['id' => $this->id], "*", MUST_EXIST);
return $this;
}
2023-08-25 13:04:19 +02:00
public function delete($force = false) {
global $DB;
2023-08-24 23:02:41 +02:00
if ($force) {
$children = studyline::find_page_children($this);
2023-08-24 23:02:41 +02:00
foreach ($children as $c) {
$c->delete($force);
}
}
2023-08-24 23:02:41 +02:00
if ($DB->count_records('local_treestudyplan_line', ['page_id' => $this->id]) > 0) {
2023-08-07 23:07:59 +02:00
return success::fail('cannot delete studyplan page that still has studylines');
2023-08-25 09:33:42 +02:00
} else {
2023-07-27 15:49:59 +02:00
$DB->delete_records(self::TABLE, ['id' => $this->id]);
return success::success();
}
}
2023-08-27 21:57:21 +02:00
/**
* Webservice structure for userinfo
* @param int $value Webservice requirement constant
* @return \external_single_structure Webservice output structure
*/
2023-08-25 13:04:19 +02:00
public static function user_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
"id" => new \external_value(PARAM_INT, 'id of studyplan page'),
"fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'),
2023-08-25 10:41:56 +02:00
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan page'),
"periods" => new \external_value(PARAM_INT, 'number of slots in studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan page'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan page'),
"studylines" => new \external_multiple_structure(studyline::user_structure()),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_structure(),
2023-08-24 23:02:41 +02:00
], 'Studyplan page with user info', $value);
}
2023-08-27 21:57:21 +02:00
/**
* Webservice model for user info
* @param int $userid ID of user to check specific info for
* @return array Webservice data model
*/
2023-08-24 23:02:41 +02:00
public function user_model($userid) {
$model = [
'id' => $this->r->id,
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'periods' => $this->r->periods,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
'studylines' => [],
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_model($this),
];
$children = studyline::find_page_children($this);
2023-08-24 23:02:41 +02:00
foreach ($children as $c) {
$model['studylines'][] = $c->user_model($userid);
}
return $model;
}
2023-08-24 23:02:41 +02:00
public static function find_studyplan_children(studyplan $plan) {
global $DB;
$list = [];
2023-08-24 23:02:41 +02:00
$ids = $DB->get_fieldset_select(self::TABLE, "id", "studyplan_id = :plan_id ORDER BY startdate",
['plan_id' => $plan->id()]);
2023-08-24 23:02:41 +02:00
foreach ($ids as $id) {
2023-08-25 17:33:20 +02:00
$list[] = self::find_by_id($id);
}
return $list;
}
2023-08-25 09:33:42 +02:00
public static function duplicate_page($pageid, $name, $shortname) {
2023-08-25 17:33:20 +02:00
$ori = self::find_by_id($pageid);
2023-08-24 23:02:41 +02:00
$new = $ori->duplicate($name, $shortname);
return $new->simple_model();
}
2023-08-25 09:33:42 +02:00
public function duplicate($newstudyplan) {
2023-08-24 23:02:41 +02:00
// First duplicate the studyplan structure.
2023-08-25 17:33:20 +02:00
$new = self::add([
2023-08-25 09:33:42 +02:00
'studyplan_id' => $newstudyplan->id(),
2023-08-07 23:07:59 +02:00
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'pages' => $this->r->pages,
'startdate' => $this->r->startdate,
2023-08-25 10:41:56 +02:00
'enddate' => empty($this->r->enddate) ? null : $this->r->enddate,
]);
2023-08-24 23:02:41 +02:00
2023-08-25 09:44:34 +02:00
// Next, copy the studylines.
$children = studyline::find_page_children($this);
$itemtranslation = [];
$linetranslation = [];
2023-08-24 23:02:41 +02:00
foreach ($children as $c) {
$newchild = $c->duplicate($this, $itemtranslation);
$linetranslation[$c->id()] = $newchild->id();
}
2023-08-25 09:44:34 +02:00
// Now the itemtranslation array contains all of the old child id's as keys and all of the related new ids as values.
2023-08-24 23:02:41 +02:00
// (feature of the studyline::duplicate function).
2023-08-25 09:44:34 +02:00
// Use this to recreate the lines in the new plan.
2023-08-25 09:33:42 +02:00
foreach (array_keys($itemtranslation) as $itemid) {
2023-08-25 09:44:34 +02:00
// Copy based on the outgoing connections of each item, to avoid duplicates.
2023-08-25 09:33:42 +02:00
$connections = studyitemconnection::find_outgoing($itemid);
2023-08-24 23:02:41 +02:00
foreach ($connections as $conn) {
studyitemconnection::connect($itemtranslation[$conn->from_id], $itemtranslation[$conn->to_id]);
}
}
return $new;
2023-08-24 23:02:41 +02:00
}
2023-08-24 23:02:41 +02:00
public static function export_structure() {
return new \external_single_structure([
"format" => new \external_value(PARAM_TEXT, 'format of studyplan export'),
2023-08-25 10:41:56 +02:00
"content" => new \external_value(PARAM_TEXT, 'exported studyplan content'),
2023-08-24 23:02:41 +02:00
], 'Exported studyplan');
}
2023-08-24 23:02:41 +02:00
public function export_page() {
$model = $this->export_model();
$json = json_encode([
2023-08-25 10:41:56 +02:00
"type" => "studyplanpage",
"version" => 2.0,
"page" => $model
2023-08-24 23:02:41 +02:00
], \JSON_PRETTY_PRINT);
return [ "format" => "application/json", "content" => $json];
}
2023-08-24 23:02:41 +02:00
public function export_page_csv() {
2023-08-25 17:33:20 +02:00
$plist = period::find_for_page($this);
$model = $this->editor_model();
2023-08-24 23:02:41 +02:00
2023-08-07 23:07:59 +02:00
$periods = intval($model["periods"]);
2023-08-24 23:02:41 +02:00
// First line.
2023-08-07 23:07:59 +02:00
$csv = "\"\"";
2023-08-25 10:41:56 +02:00
for ($i = 1; $i <= $periods; $i++) {
2023-07-27 16:58:23 +02:00
$name = $plist[$i]->shortname();
2023-08-24 23:02:41 +02:00
$csv .= ", \"{$name}\"";
}
$csv .= "\r\n";
2023-08-25 09:44:34 +02:00
// Next, make one line per studyline.
2023-08-24 23:02:41 +02:00
foreach ($model["studylines"] as $line) {
2023-08-25 09:44:34 +02:00
// Determine how many fields are simultaneous in the line at maximum.
$maxlines = 1;
2023-08-25 10:41:56 +02:00
for ($i = 1; $i <= $periods; $i++) {
2023-08-24 23:02:41 +02:00
if (count($line["slots"]) > $i) {
$ct = 0;
2023-08-24 23:02:41 +02:00
foreach ($line["slots"][$i][studyline::SLOTSET_COMPETENCY] as $itm) {
if ($itm["type"] == "course") {
$ct += 1;
}
}
2023-08-24 23:02:41 +02:00
if ($ct > $maxlines) {
$maxlines = $ct;
}
}
}
2023-08-25 10:41:56 +02:00
for ($lct = 0; $lct < $maxlines; $lct++) {
$csv .= "\"{$line["name"]}\"";
2023-08-25 10:41:56 +02:00
for ($i = 1; $i <= $periods; $i++) {
$filled = false;
2023-08-24 23:02:41 +02:00
if (count($line["slots"]) > $i) {
$ct = 0;
2023-08-24 23:02:41 +02:00
foreach ($line["slots"][$i][studyline::SLOTSET_COMPETENCY] as $itm) {
if ($itm["type"] == "course") {
if ($ct == $lct) {
$csv .= ", \"";
$csv .= $itm["course"]["fullname"];
$csv .= "\r\n";
$first = true;
2023-08-24 23:02:41 +02:00
foreach ($itm["course"]["grades"] as $g) {
if ($g["selected"]) {
if ($first) {
$first = false;
2023-08-25 10:41:56 +02:00
} else {
$csv .= "\r\n";
}
$csv .= "- ".str_replace('"', '\'', $g["name"]);
}
}
$csv .= "\"";
$filled = true;
break;
}
$ct++;
}
}
2023-08-24 23:02:41 +02:00
}
if (!$filled) {
$csv .= ", \"\"";
}
}
$csv .= "\r\n";
}
}
return [ "format" => "text/csv", "content" => $csv];
}
2023-08-24 23:02:41 +02:00
public function export_studylines() {
$model = $this->export_studylines_model();
$json = json_encode([
2023-08-25 10:41:56 +02:00
"type" => "studylines",
"version" => 2.0,
"studylines" => $model,
2023-08-24 23:02:41 +02:00
], \JSON_PRETTY_PRINT);
return [ "format" => "application/json", "content" => $json];
}
2023-08-24 23:02:41 +02:00
public function export_periods() {
2023-07-27 16:58:23 +02:00
$model = period::page_model($this);
$json = json_encode([
2023-08-25 10:41:56 +02:00
"type" => "periods",
"version" => 2.0,
"perioddesc" => $model,
2023-08-24 23:02:41 +02:00
], \JSON_PRETTY_PRINT);
2023-07-27 16:58:23 +02:00
return [ "format" => "application/json", "content" => $json];
}
2023-08-27 21:57:21 +02:00
/**
* Export essential information for export
* @return array information model
*/
2023-08-24 23:02:41 +02:00
public function export_model() {
$model = [
2023-08-07 23:07:59 +02:00
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'periods' => $this->r->periods,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
'studylines' => $this->export_studylines_model(),
2023-07-27 16:58:23 +02:00
'perioddesc' => period::page_model($this),
];
return $model;
}
2023-08-24 23:02:41 +02:00
public function export_studylines_model() {
$children = studyline::find_page_children($this);
$lines = [];
2023-08-24 23:02:41 +02:00
foreach ($children as $c) {
$lines[] = $c->export_model();
}
return $lines;
}
2023-08-25 13:04:19 +02:00
public function import_periods($content, $format = "application/json") {
2023-08-25 10:41:56 +02:00
if ($format != "application/json") {
return false;
}
2023-08-24 23:02:41 +02:00
$content = json_decode($content, true);
if ($content["type"] == "periods" && $content["version"] >= 2.0) {
2023-07-27 16:58:23 +02:00
return $this->import_periods_model($content["perioddesc"]);
2023-08-24 23:09:20 +02:00
} else if ($content["type"] == "studyplanpage" && $content["version"] >= 2.0) {
2023-07-27 16:58:23 +02:00
return $this->import_periods_model($content["page"]["perioddesc"]);
2023-08-24 23:09:20 +02:00
} else {
2023-07-27 16:58:23 +02:00
return false;
}
}
2023-08-25 13:04:19 +02:00
public function import_studylines($content, $format = "application/json") {
2023-08-25 10:41:56 +02:00
if ($format != "application/json") {
return false;
}
2023-08-24 23:02:41 +02:00
$content = json_decode($content, true);
if ($content["type"] == "studylines" && $content["version"] >= 2.0) {
return $this->import_studylines_model($content["studylines"]);
2023-08-24 23:09:20 +02:00
} else if ($content["type"] == "studyplanpage" && $content["version"] >= 2.0) {
return $this->import_studylines_model($content["page"]["studylines"]);
2023-08-24 23:09:20 +02:00
} else if ($content["type"] == "studyplan" && $content["version"] >= 2.0) {
2023-08-07 23:07:59 +02:00
return $this->import_studylines_model($content["studyplan"]["pages"][0]["studylines"]);
2023-08-24 23:09:20 +02:00
} else {
return false;
}
}
2023-08-24 23:02:41 +02:00
protected function find_studyline_by_shortname($shortname) {
$children = studyline::find_page_children($this);
2023-08-24 23:02:41 +02:00
foreach ($children as $l) {
if ($shortname == $l->shortname()) {
return $l;
}
}
return null;
}
2023-08-24 23:02:41 +02:00
public function import_periods_model($model) {
2023-08-25 17:33:20 +02:00
$periods = period::find_for_page($this);
2023-08-24 23:02:41 +02:00
foreach ($model as $pmodel) {
2023-07-27 16:58:23 +02:00
$pi = $pmodel["period"];
2023-08-24 23:02:41 +02:00
if (array_key_exists($pi, $periods)) {
2023-07-27 16:58:23 +02:00
$periods[$pi]->edit($pmodel);
}
}
}
2023-08-24 23:02:41 +02:00
public function import_studylines_model($model) {
// First attempt to map each studyline model to an existing or new line.
2023-08-25 09:33:42 +02:00
$linemap = [];
2023-08-24 23:02:41 +02:00
foreach ($model as $ix => $linemodel) {
$line = $this->find_studyline_by_shortname($linemodel["shortname"]);
2023-08-24 23:02:41 +02:00
if (empty($line)) {
2023-08-07 23:07:59 +02:00
$linemodel["page_id"] = $this->id;
$line = studyline::add($linemodel);
2023-08-25 10:41:56 +02:00
}
2023-08-25 09:33:42 +02:00
$linemap[$ix] = $line;
}
2023-08-25 09:44:34 +02:00
// Next, let each study line import the study items.
2023-08-24 23:02:41 +02:00
$itemtranslation = [];
$connections = [];
2023-08-24 23:02:41 +02:00
foreach ($model as $ix => $linemodel) {
2023-08-25 09:33:42 +02:00
$linemap[$ix]->import_studyitems($linemodel["slots"], $itemtranslation, $connections);
}
2023-08-24 23:02:41 +02:00
// Finally, create the links between the study items.
foreach ($connections as $from => $dests) {
foreach ($dests as $to) {
studyitemconnection::connect($from, $itemtranslation[$to]);
}
}
return true;
}
2023-08-25 11:52:05 +02:00
}