2023-07-23 16:25:08 +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 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
|
|
|
|
*/
|
|
|
|
|
2023-07-23 16:25:08 +02:00
|
|
|
namespace local_treestudyplan;
|
2023-08-25 12:04:27 +02:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2023-07-23 16:25:08 +02:00
|
|
|
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Studyplan page management class
|
|
|
|
*/
|
2023-07-23 16:25:08 +02:00
|
|
|
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 */
|
2023-07-23 16:25:08 +02:00
|
|
|
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-07-23 16:25:08 +02:00
|
|
|
|
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 */
|
2023-07-23 16:25:08 +02:00
|
|
|
private $id;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var studyplan*/
|
2023-07-23 16:25:08 +02:00
|
|
|
private $studyplan;
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Get aggregator for the studyplan (page)
|
|
|
|
* @return aggregator
|
|
|
|
*/
|
|
|
|
public function aggregator() : aggregator {
|
2023-07-23 16:25:08 +02:00
|
|
|
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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Construct new instance from DB record
|
|
|
|
* @param int $id Id of database record
|
|
|
|
*/
|
2023-07-23 16:25:08 +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-08-25 17:33:20 +02:00
|
|
|
$this->studyplan = studyplan::find_by_id($this->r->studyplan_id);
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return database identifier
|
|
|
|
* @return int
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function id() {
|
2023-07-23 16:25:08 +02:00
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Find studyplan for this page
|
|
|
|
*/
|
2023-07-23 16:25:08 +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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
return new \DateTime($this->r->startdate);
|
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* End date
|
|
|
|
* @return \DateTime
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function enddate() {
|
|
|
|
if ($this->r->enddate && strlen($this->r->enddate) > 0) {
|
2023-07-23 16:25:08 +02:00
|
|
|
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.
|
2023-07-23 16:25:08 +02:00
|
|
|
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
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public static function simple_structure($value = VALUE_REQUIRED) : \external_description {
|
2023-07-23 16:25:08 +02:00
|
|
|
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-07-23 16:25:08 +02:00
|
|
|
"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'),
|
2023-07-23 16:25:08 +02:00
|
|
|
"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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
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-07-23 16:25:08 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for editor info
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public static function editor_structure($value = VALUE_REQUIRED) : \external_description {
|
2023-07-23 16:25:08 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of studyplan'),
|
2023-07-27 15:07:14 +02:00
|
|
|
"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'),
|
2023-07-23 16:25:08 +02:00
|
|
|
"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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$model = [
|
|
|
|
'id' => $this->r->id,
|
2023-07-27 15:07:14 +02:00
|
|
|
'fullname' => $this->r->fullname,
|
2023-07-23 16:25:08 +02:00
|
|
|
'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),
|
2023-07-23 16:25:08 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$children = studyline::find_page_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public static function add($fields) : self {
|
2023-07-23 16:25:08 +02:00
|
|
|
global $CFG, $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
if (!isset($fields['studyplan_id'])) {
|
2023-07-23 16:25:08 +02:00
|
|
|
throw new \InvalidArgumentException("parameter 'studyplan_id' missing");
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$addable = ['studyplan_id', 'fullname', 'shortname', 'description', 'periods', 'startdate', 'enddate'];
|
2023-07-23 16:25:08 +02:00
|
|
|
$info = ['enddate' => null ];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($addable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2023-07-23 16:25:08 +02:00
|
|
|
$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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Edit studyplan page
|
|
|
|
* @param mixed $fields Parameters to change
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public function edit($fields) : self {
|
2023-07-23 16:25:08 +02:00
|
|
|
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)) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-07-23 16:25:08 +02:00
|
|
|
$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);
|
|
|
|
|
2023-07-23 16:25:08 +02:00
|
|
|
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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($force) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$children = studyline::find_page_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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]);
|
2023-07-23 16:25:08 +02:00
|
|
|
return success::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for userinfo
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public static function user_structure($value = VALUE_REQUIRED) : \external_description {
|
2023-07-23 16:25:08 +02:00
|
|
|
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'),
|
2023-07-23 16:25:08 +02:00
|
|
|
"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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
|
|
|
|
$model = [
|
|
|
|
'id' => $this->r->id,
|
2023-07-27 15:07:14 +02:00
|
|
|
'fullname' => $this->r->fullname,
|
2023-07-23 16:25:08 +02:00
|
|
|
'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),
|
2023-07-23 16:25:08 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$children = studyline::find_page_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$model['studylines'][] = $c->user_model($userid);
|
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-10-23 23:19:14 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
if($c->completion($userid) >= completion::COMPLETED){
|
|
|
|
$completed += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ($completed/$courses);
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
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",
|
2023-07-23 16:25:08 +02:00
|
|
|
['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);
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2023-08-28 08:51:52 +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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Duplicate this studyplan page
|
|
|
|
* @param studyplan $newstudyplan Studyplan to copy the page into
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public function duplicate(studyplan $newstudyplan) : self {
|
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,
|
2023-07-23 16:25:08 +02:00
|
|
|
'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-07-23 16:25:08 +02:00
|
|
|
]);
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Next, copy the studylines.
|
2023-07-23 16:25:08 +02:00
|
|
|
|
|
|
|
$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);
|
2023-07-23 16:25:08 +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) {
|
|
|
|
studyitemconnection::connect($itemtranslation[$conn->from_id], $itemtranslation[$conn->to_id]);
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $new;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-07-23 16:25:08 +02:00
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Description of export structure for webservices
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public static function export_structure() : \external_description {
|
2023-07-23 16:25:08 +02:00
|
|
|
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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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);
|
2023-07-23 16:25:08 +02:00
|
|
|
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);
|
2023-07-23 16:25:08 +02:00
|
|
|
|
|
|
|
$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}\"";
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
$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.
|
2023-07-23 16:25:08 +02:00
|
|
|
$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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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") {
|
2023-07-23 16:25:08 +02:00
|
|
|
$ct += 1;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($ct > $maxlines) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$maxlines = $ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 10:41:56 +02:00
|
|
|
for ($lct = 0; $lct < $maxlines; $lct++) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$csv .= "\"{$line["name"]}\"";
|
2023-08-25 10:41:56 +02:00
|
|
|
for ($i = 1; $i <= $periods; $i++) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$filled = false;
|
2023-08-24 23:02:41 +02:00
|
|
|
if (count($line["slots"]) > $i) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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 .= ", \"";
|
2023-07-23 16:25:08 +02:00
|
|
|
$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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$first = false;
|
2023-08-25 10:41:56 +02:00
|
|
|
} else {
|
2023-07-23 16:25:08 +02:00
|
|
|
$csv .= "\r\n";
|
|
|
|
}
|
|
|
|
$csv .= "- ".str_replace('"', '\'', $g["name"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$csv .= "\"";
|
|
|
|
$filled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$ct++;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
|
|
|
if (!$filled) {
|
|
|
|
$csv .= ", \"\"";
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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);
|
2023-07-23 16:25:08 +02:00
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
$model = [
|
2023-08-07 23:07:59 +02:00
|
|
|
'fullname' => $this->r->fullname,
|
2023-07-23 16:25:08 +02:00
|
|
|
'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),
|
2023-07-23 16:25:08 +02:00
|
|
|
];
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
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() {
|
2023-07-23 16:25:08 +02:00
|
|
|
$children = studyline::find_page_children($this);
|
|
|
|
$lines = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
return $this->import_studylines_model($content["studylines"]);
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if ($content["type"] == "studyplanpage" && $content["version"] >= 2.0) {
|
2023-07-23 16:25:08 +02:00
|
|
|
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 {
|
2023-07-23 16:25:08 +02:00
|
|
|
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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$children = studyline::find_page_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $l) {
|
|
|
|
if ($shortname == $l->shortname()) {
|
2023-07-23 16:25:08 +02:00
|
|
|
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) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$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;
|
2023-07-23 16:25:08 +02:00
|
|
|
$line = studyline::add($linemodel);
|
2023-08-25 10:41:56 +02:00
|
|
|
}
|
2023-08-25 09:33:42 +02:00
|
|
|
$linemap[$ix] = $line;
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
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 = [];
|
2023-07-23 16:25:08 +02:00
|
|
|
$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-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
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]);
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|