moodle_local_treestudyplan/classes/studyplanpage.php

896 lines
32 KiB
PHP
Raw Permalink 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/>.
2024-07-19 12:31:26 +02:00
2023-08-24 23:02:41 +02:00
/**
2023-08-27 23:27:07 +02:00
* Studyplan page management class
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
*/
namespace local_treestudyplan;
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/externallib.php');
require_once($CFG->libdir.'/filelib.php');
2023-08-27 23:27:07 +02:00
/**
* Studyplan page management class
*/
class studyplanpage {
2023-08-27 23:29:46 +02:00
/**
2023-08-27 23:27:07 +02:00
* Database table this class models for
* @var string */
const TABLE = "local_treestudyplan_page";
2023-08-27 23:27:07 +02:00
/**
* Cache for finding previously loaded pages
* @var array */
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-27 23:29:46 +02:00
private $r;
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-27 23:27:07 +02:00
/**
* Get aggregator for the studyplan (page)
* @return aggregator
*/
2024-06-02 19:23:40 +02:00
public function aggregator(): aggregator {
return $this->studyplan->aggregator();
}
2023-08-27 23:27:07 +02:00
/**
* Find record in database and return management object
* @param int $id Id of database record
*/
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];
}
2023-08-27 23:27:07 +02:00
/**
* Construct new instance from DB record
* @param int $id Id of database record
*/
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;
}
2023-08-27 23:27:07 +02:00
/**
* Find studyplan for this page
*/
2024-06-02 19:23:40 +02:00
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-27 23:27:07 +02:00
/**
* Numer of periods for this page
*/
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-27 23:27:07 +02:00
/**
* Start date
* @return \DateTime
*/
2023-08-24 23:02:41 +02:00
public function startdate() {
return new \DateTime($this->r->startdate);
}
2023-08-27 23:27:07 +02:00
/**
* End date
2024-06-03 04:00:46 +02:00
* @param bool $farahead Return a date 100 years in the future if end date is not set. Returns null otherwise
* @return \DateTime|null
2023-08-27 23:27:07 +02:00
*/
public function enddate($farahead = true) {
2023-08-24 23:02:41 +02:00
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.
if ($farahead) {
return (new \DateTime($this->r->startdate))->add(new \DateInterval("P100Y"));
} else {
return null;
}
}
}
2024-06-02 19:23:40 +02:00
/**
* Return description with all file references resolved
2024-06-02 19:23:40 +02:00
* @return string
*/
public function description() {
$text = \file_rewrite_pluginfile_urls(
// The content of the text stored in the database.
$this->r->description,
// The pluginfile URL which will serve the request.
'pluginfile.php',
2024-06-02 19:23:40 +02:00
2024-06-02 23:23:32 +02:00
// The combination of contextid / component / filearea / itemid.
// Form the virtual bucket that file are stored in.
\context_system::instance()->id, // System instance is always used for this.
'local_treestudyplan',
'studyplanpage',
$this->id
);
return $text;
}
2024-06-02 23:23:32 +02:00
/**
* Determine studyplan timing
* @return string
*/
public function timing() {
$now = new \DateTime();
2024-05-31 20:41:18 +02:00
if ($now > $this->startdate()) {
if ($now > $this->enddate()) {
return "past";
} else {
return "present";
}
} else {
return "future";
}
}
2023-08-27 22:20:17 +02:00
/**
* Webservice structure for basic info
* @param int $value Webservice requirement constant
*/
2024-06-02 19:23:40 +02:00
public static function simple_structure($value = VALUE_REQUIRED): \external_description {
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-10-21 23:24:25 +02:00
"description" => new \external_value(PARAM_RAW, 'description of studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
"timing" => new \external_value(PARAM_TEXT, '(past|present|future)'),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_structure(),
"timeless" => new \external_value(PARAM_BOOL, 'Page does not in'),
2023-08-24 23:02:41 +02:00
], 'Studyplan page basic info', $value);
}
2023-08-27 23:29:46 +02:00
/**
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->description(),
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
'timing' => $this->timing(),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_model($this),
2024-06-02 19:23:40 +02:00
'timeless' => \get_config("local_treestudyplan", "timelessperiods"),
];
}
2023-08-27 21:57:21 +02:00
/**
* Webservice structure for editor info
* @param int $value Webservice requirement constant
*/
2024-06-02 19:23:40 +02:00
public static function editor_structure($value = VALUE_REQUIRED): \external_description {
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'),
2023-10-21 23:24:25 +02:00
"description" => new \external_value(PARAM_RAW, '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'),
"timing" => new \external_value(PARAM_TEXT, '(past|present|future)'),
"studylines" => new \external_multiple_structure(studyline::editor_structure()),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_structure(),
"timeless" => new \external_value(PARAM_BOOL, 'Page does not include time information'),
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->description(),
'periods' => $this->r->periods,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
'timing' => $this->timing(),
'studylines' => [],
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_model($this),
2024-06-02 19:23:40 +02:00
'timeless' => \get_config("local_treestudyplan", "timelessperiods"),
];
$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-27 23:27:07 +02:00
/**
* Add new study plan page
* @param mixed $fields Parameter for new study plan page
2024-05-10 15:22:52 +02:00
* @param bool $bare If true, do not create a first page with copy of studyplan names
2023-08-27 23:27:07 +02:00
*/
2024-06-02 19:23:40 +02:00
public static function add($fields, $bare = false): self {
global $DB;
2023-08-24 23:02:41 +02:00
if (!isset($fields['studyplan_id'])) {
throw new \InvalidArgumentException("parameter 'studyplan_id' missing");
} else {
$plan = studyplan::find_by_id($fields['studyplan_id']);
}
$addable = ['studyplan_id', 'fullname', 'shortname', 'description', 'descriptionformat', '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-09-09 20:53:39 +02:00
if (!isset($info['periods'])) {
$info['periods'] = 4;
} else if ($info['periods'] < 1) {
$info['periods'] = 1;
2023-08-16 23:15:48 +02:00
}
$id = $DB->insert_record(self::TABLE, $info);
2024-06-02 23:23:32 +02:00
// Refresh the page cache for the studyplan.
$plan->pages(true);
2024-05-10 15:22:52 +02:00
$page = self::find_by_id($id); // Make sure the new page is immediately cached.
if (!$bare) {
2024-06-02 19:23:40 +02:00
if (get_config("local_treestudyplan", "copystudylinesnewpage")) {
2024-06-01 14:00:32 +02:00
$templatepage = null;
$templatelines = [];
2024-06-02 23:23:32 +02:00
// Find the latest a page with lines in the plan.
2024-06-01 14:00:32 +02:00
$pages = $plan->pages(true);
foreach ($pages as $p) {
if ($p->id() != $id) {
$lines = studyline::find_page_children($p);
if (count($lines) > 0) {
$templatepage = $p;
$templatelines = $lines;
}
}
}
if (isset($templatepage)) {
foreach ($templatelines as $l) {
$map = []; // Bare copy still requires passing an empty array.
2024-06-02 19:23:40 +02:00
$l->duplicate($page, $map, true); // Do bare copy, which does not include line content.
2024-06-01 14:00:32 +02:00
}
}
}
2024-06-02 19:23:40 +02:00
if (count(studyline::find_page_children($page)) == 0) {
2024-06-02 23:23:32 +02:00
// Add an empty study line if there are currently no study lines.
2024-06-01 14:00:32 +02:00
$lineinfo = ['page_id' => $id,
2024-06-02 19:23:40 +02:00
'name' => get_string("default_line_name", "local_treestudyplan"),
'shortname' => get_string("default_line_shortname", "local_treestudyplan"),
2024-06-01 14:00:32 +02:00
"color" => "#DDDDDD",
];
studyline::add($lineinfo);
}
2024-05-10 15:22:52 +02:00
}
return $page;
}
2023-08-27 23:27:07 +02:00
/**
* Edit studyplan page
* @param mixed $fields Parameters to change
*/
2024-06-02 19:23:40 +02:00
public function edit($fields): self {
global $DB;
$editable = ['fullname', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
2024-06-02 19:23:40 +02:00
$info = ['id' => $this->id ];
2023-08-24 23:02:41 +02:00
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-27 23:27:07 +02:00
/**
* Delete study plan page
* @param bool $force Force deletion even if not empty
* @return success
*/
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
*/
2024-06-02 19:23:40 +02:00
public static function user_structure($value = VALUE_REQUIRED): \external_description {
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'),
2023-10-21 23:24:25 +02:00
"description" => new \external_value(PARAM_RAW, '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'),
"timing" => new \external_value(PARAM_TEXT, '(past|present|future)'),
"studylines" => new \external_multiple_structure(studyline::user_structure()),
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_structure(),
2024-03-10 23:11:54 +01:00
"timeless" => new \external_value(PARAM_BOOL, 'Page does not include time information'),
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->description(),
'periods' => $this->r->periods,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
'timing' => $this->timing(),
'studylines' => [],
2023-07-27 16:58:23 +02:00
"perioddesc" => period::page_model($this),
2024-06-02 19:23:40 +02:00
'timeless' => \get_config("local_treestudyplan", "timelessperiods"),
];
$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;
}
/**
* Scan user progress (completed modules) over this page for a specific user
* @param int $userid ID of user to check for
* @return float Fraction of completion
*/
public function scanuserprogress($userid) {
$courses = 0;
$completed = 0;
foreach (studyline::find_page_children($this) as $line) {
$items = studyitem::find_studyline_children($line);
foreach ($items as $c) {
if (in_array($c->type(), studyline::COURSE_TYPES)) {
$courses += 1;
2024-06-02 19:23:40 +02:00
if ($c->completion($userid) >= completion::COMPLETED) {
$completed += 1;
}
}
}
}
2024-06-02 23:23:32 +02:00
if ($courses > 0) {
return ($completed / $courses);
} else {
return 0;
}
}
2023-08-27 23:27:07 +02:00
/**
* Find list of pages belonging to a specified study plan
* @param studyplan $plan Studyplan to search pages for
2023-08-28 08:51:52 +02:00
* @return studyplanpage[]
2023-08-27 23:27:07 +02:00
*/
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-27 23:27:07 +02:00
/**
* Duplicate a studyplan page
* @param int $pageid Id of the page to copy
* @param studyplan $newstudyplan Studyplan to copy the page into
*/
2024-06-02 19:23:40 +02:00
public static function duplicate_page(int $pageid, studyplan $newstudyplan): self {
2023-08-25 17:33:20 +02:00
$ori = self::find_by_id($pageid);
2023-08-27 23:27:07 +02:00
$new = $ori->duplicate($newstudyplan);
return $new;
}
2023-08-27 23:27:07 +02:00
/**
* Duplicate this studyplan page
* @param studyplan $newstudyplan Studyplan to copy the page into
2024-06-03 04:00:46 +02:00
* @param \DateInterval|null $timeoffset Amount if time to shift the dates for the new page
2023-08-27 23:27:07 +02:00
*/
2024-06-02 19:23:40 +02:00
public function duplicate(studyplan $newstudyplan, $timeoffset = null): self {
2024-04-19 16:46:30 +02:00
if ($timeoffset == null) {
$timeoffset = new \DateInterval("P0D");
}
2024-06-02 19:23:40 +02:00
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,
2024-04-19 16:46:30 +02:00
'startdate' => $this->startdate()->add($timeoffset)->format("Y-m-d"),
2024-06-02 23:23:32 +02:00
'enddate' => empty($this->r->enddate) ? null : ($this->enddate()->add($timeoffset)->format("Y-m-d")),
]);
2023-08-24 23:02:41 +02:00
2024-04-19 16:46:30 +02:00
// Copy any files related to this page.
$areas = ["studyplanpage"];
$fs = \get_file_storage();
foreach ($areas as $area) {
$files = $fs->get_area_files(
\context_system::instance()->id,
"local_treestudyplan",
$area,
$this->id()
);
foreach ($files as $file) {
$path = $file->get_filepath();
$filename = $file->get_filename();
if ($filename != ".") {
// Prepare new file info for the target file.
$fileinfo = [
'contextid' => \context_system::instance()->id, // System context.
'component' => 'local_treestudyplan', // Your component name.
'filearea' => $area, // Area name.
'itemid' => $new->id(), // Study plan id.
'filepath' => $path, // Original file path.
'filename' => $filename, // Original file name.
];
// Copy existing file into new file.
$fs->create_file_from_storedfile($fileinfo, $file);
}
}
}
// Adjust the time offsets of the periods.
foreach ($this->periods() as $p) {
$p->edit([
'startdate' => $p->startdate()->add($timeoffset)->format("Y-m-d"),
'enddate' => $p->enddate()->add($timeoffset)->format("Y-m-d"),
]);
}
2024-04-19 16:46:30 +02:00
// Now , copy the studylines.
$children = studyline::find_page_children($this);
2024-04-19 16:46:30 +02:00
$itemtranslation = []; // Stores ids of original and copied items.
$linetranslation = []; // Stores ids of original and copied lines.
2023-08-24 23:02:41 +02:00
foreach ($children as $c) {
2024-04-19 16:46:30 +02:00
$translation = [];
$newchild = $c->duplicate($new, $translation);
$itemtranslation += $translation; // Fixes behaviour where translation array is reset on each call.
2024-06-02 19:23:40 +02:00
$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) {
2024-04-19 16:46:30 +02:00
studyitemconnection::connect($itemtranslation[$conn->from_id()], $itemtranslation[$conn->to_id()]);
}
}
return $new;
2023-08-24 23:02:41 +02:00
}
2023-08-27 23:27:07 +02:00
/**
* Description of export structure for webservices
*/
2024-06-02 19:23:40 +02:00
public static function export_structure(): \external_description {
return new \external_single_structure([
"format" => new \external_value(PARAM_TEXT, 'format of studyplan export'),
2023-11-11 20:17:45 +01:00
"content" => new \external_value(PARAM_RAW, 'exported studyplan content'),
2023-08-24 23:02:41 +02:00
], 'Exported studyplan');
}
2023-08-27 23:27:07 +02:00
/**
* Export this page into a json model
* @return array
*/
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,
2024-06-02 23:23:32 +02:00
"page" => $model,
2023-08-24 23:02:41 +02:00
], \JSON_PRETTY_PRINT);
return [ "format" => "application/json", "content" => $json];
}
2023-08-27 23:27:07 +02:00
/**
* Export this page into a csv model
* @return array
*/
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-28 11:26:14 +02:00
foreach ($line["slots"][$i][studyline::SLOTSET_COURSES] as $itm) {
2023-08-24 23:02:41 +02:00
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-28 11:26:14 +02:00
foreach ($line["slots"][$i][studyline::SLOTSET_COURSES] as $itm) {
2023-08-24 23:02:41 +02:00
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-27 23:27:07 +02:00
/**
* Export this page's studylines into a json model
* @return array
*/
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-27 23:27:07 +02:00
/**
* Export this pages periods into a json model
* @return array
*/
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() {
$exportfiles = $this->export_files('studyplanpage');
$model = [
2023-08-07 23:07:59 +02:00
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'descriptionformat' => $this->r->descriptionformat,
'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),
'files' => $exportfiles,
];
return $model;
}
/**
* Export files from file storage
* @param string $area Name of the file area to export
* @return array information model
*/
public function export_files($area) {
$exportfiles = [];
$fs = get_file_storage();
$files = $fs->get_area_files(
\context_system::instance()->id,
'local_treestudyplan',
$area,
$this->id);
foreach ($files as $file) {
if ($file->get_filename() != ".") {
$contents = $file->get_content();
$exportfiles[] = [
"name" => $file->get_filename(),
"path" => $file->get_filepath(),
"content" => convert_uuencode($contents),
];
}
}
return $exportfiles;
}
/**
* Import previously exported files into the file storage
* @param mixed $importfiles List of files to import from string in the format exported in export_model()
2024-06-03 04:00:46 +02:00
* @param string $area Name of the file area to export
*/
2024-06-02 19:23:40 +02:00
public function import_files($importfiles, $area) {
$fs = get_file_storage();
2024-06-02 23:23:32 +02:00
foreach ($importfiles as $file) {
if ($file['name'] != ".") {
$fileinfo = [
2024-06-02 23:23:32 +02:00
'contextid' => \context_system::instance()->id, // ID of the system context.
'component' => 'local_treestudyplan', // Your component name.
'filearea' => $area, // Usually = table name.
'itemid' => $this->id, // ID of the studyplanpage.
'filepath' => $file["path"], // Path of the file.
'filename' => $file["name"], // Name of the file..
];
$fs->create_file_from_string($fileinfo, convert_uudecode($file["content"]));
}
}
}
2023-08-27 23:27:07 +02:00
/**
* Export this pages periods into an array before serialization
* @return array
*/
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-27 23:27:07 +02:00
/**
* Import periods from file contents
* @param string $content String
* @param string $format Format description
*/
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-27 23:27:07 +02:00
/**
* Import studylines from file contents
* @param string $content String
* @param string $format Format description
*/
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-27 23:27:07 +02:00
/**
* Find a studyline in this page by its shortname
2023-08-27 23:29:46 +02:00
* @param string $shortname
2023-08-27 23:27:07 +02:00
* @return studyline|null
*/
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-27 23:27:07 +02:00
/**
* Import periods from decoded array model
* @param array $model Decoded array
*/
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-27 23:27:07 +02:00
/**
* Import studylines from decoded array model
* @param array $model Decoded array
*/
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 = [];
2024-04-19 16:46:30 +02:00
$itemconnections = [];
2023-08-24 23:02:41 +02:00
foreach ($model as $ix => $linemodel) {
2024-06-02 19:23:40 +02:00
$translation = [];
2024-04-19 16:46:30 +02:00
$connections = [];
$linemap[$ix]->import_studyitems($linemodel["slots"], $translation, $connections);
$itemtranslation += $translation; // Fixes behaviour where translation array is reset on each call.
$itemconnections += $connections;
}
2023-08-24 23:02:41 +02:00
// Finally, create the links between the study items.
2024-04-19 16:46:30 +02:00
foreach ($itemconnections as $from => $dests) {
2023-08-24 23:02:41 +02:00
foreach ($dests as $to) {
studyitemconnection::connect($from, $itemtranslation[$to]);
}
}
return true;
}
2023-08-25 11:52:05 +02:00
}