2023-05-17 21:19:14 +02:00
|
|
|
<?php
|
2023-08-24 23:02:41 +02:00
|
|
|
// This file is part of the Studyplan plugin for Moodle
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
/**
|
2023-08-28 11:26:14 +02:00
|
|
|
* Model class for study lines
|
2023-08-24 23:02:41 +02:00
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
namespace local_treestudyplan;
|
2023-08-25 12:04:27 +02:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Model class for study lines
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
class studyline {
|
2024-02-25 23:45:39 +01:00
|
|
|
/**
|
|
|
|
* Studyline is not enrollable
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public const ENROLLABLE_NONE = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Studyline can be enrolled into by the student
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public const ENROLLABLE_SELF = 1;
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-25 23:45:39 +01:00
|
|
|
/**
|
|
|
|
* Studyline can be enrolled into by specific role(s)
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public const ENROLLABLE_ROLE = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Studyline can be enrolled by user and/or role
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public const ENROLLABLE_SELF_ROLE = 3;
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Handle for course slots in the period for webservice export
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const SLOTSET_COURSES = 'courses';
|
|
|
|
/**
|
|
|
|
* Handle for filter slots around the periods for webservice export
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public const SLOTSET_FILTER = 'filters';
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* List of item types allowed in the course slot
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public const COURSE_TYPES = [
|
2023-05-17 21:19:14 +02:00
|
|
|
studyitem::COURSE,
|
|
|
|
];
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* List of item types allowed in the filter slot
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public const FILTER_TYPES = [
|
|
|
|
studyitem::JUNCTION,
|
|
|
|
studyitem::BADGE,
|
|
|
|
studyitem::FINISH,
|
|
|
|
studyitem::START,
|
|
|
|
];
|
2023-08-28 11:26:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of item types allowed in the first filter slot
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public const FILTER0_TYPES = [
|
|
|
|
studyitem::START,
|
|
|
|
];
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
public const TABLE = "local_treestudyplan_line";
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Cache retrieved studylines for optimization
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $cache = [];
|
2023-05-17 21:19:14 +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:27:07 +02:00
|
|
|
private $r;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $id;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var studyplanpage */
|
2023-07-23 16:25:08 +02:00
|
|
|
private $page;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var studyplan*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private $studyplan;
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return the context the studyplan is associated to
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public function context(): \context {
|
|
|
|
return $this->studyplan->context();
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Return the studyplan for this line
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function studyplan(): studyplan {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->studyplan;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Return the studyplan page for this line
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function page(): studyplanpage {
|
2023-08-16 23:36:11 +02:00
|
|
|
return $this->page;
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-08-28 11:26:14 +02:00
|
|
|
if (!array_key_exists($id, self::$cache)) {
|
|
|
|
self::$cache[$id] = new self($id);
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-08-28 11:26:14 +02:00
|
|
|
return self::$cache[$id];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Construct new instance from DB record
|
|
|
|
* @param int $id Id of database record
|
|
|
|
*/
|
2023-05-17 21:19:14 +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->page = studyplanpage::find_by_id($this->r->page_id);
|
2023-07-23 16:25:08 +02:00
|
|
|
$this->studyplan = $this->page->studyplan();
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return database identifier
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function id(): int {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return full name
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function name(): string {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->r->name;
|
|
|
|
}
|
2023-08-28 11:26:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return short name
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function shortname(): string {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->r->shortname;
|
|
|
|
}
|
|
|
|
|
2024-02-25 23:45:39 +01:00
|
|
|
/**
|
|
|
|
* Whether this line is enrollable by the student
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function self_enrollable(): bool {
|
2024-06-02 23:23:32 +02:00
|
|
|
return ($this->r->enrollable == self::ENROLLABLE_SELF || $this->r->enrollable == self::ENROLLABLE_SELF_ROLE);
|
2024-02-25 23:45:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this line is enrollable by a role
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function role_enrollable(): bool {
|
2024-06-02 23:23:32 +02:00
|
|
|
return ($this->r->enrollable == self::ENROLLABLE_ROLE || $this->r->enrollable == self::ENROLLABLE_SELF_ROLE);
|
2024-02-25 23:45:39 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
/**
|
|
|
|
* Whether the current user can enrol a specific user in this line
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param int|null $userid ID of user to check for. Checks current user if left empty or null
|
2024-03-04 22:39:29 +01:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function can_enrol($userid=null): bool {
|
2024-03-04 22:39:29 +01:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
$plan = $this->studyplan();
|
2024-06-02 19:23:40 +02:00
|
|
|
if (!empty($userid)) {
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($plan->has_linked_user(intval($userid))) {
|
|
|
|
if ($this->self_enrollable() && $userid == $USER->id) {
|
2024-03-04 22:39:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($this->role_enrollable()) {
|
2024-03-04 22:39:29 +01:00
|
|
|
$context = $plan->context();
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($this->enrol_roleids() as $rid) {
|
2024-06-02 19:23:40 +02:00
|
|
|
if (\user_has_role_assignment($USER->id, $rid, $context->id)) {
|
2024-03-04 22:39:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-02 23:23:32 +02:00
|
|
|
if ($this->role_enrollable()) {
|
2024-03-04 22:39:29 +01:00
|
|
|
$context = $plan->context();
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($this->enrol_roleids() as $rid) {
|
2024-06-02 19:23:40 +02:00
|
|
|
if (\user_has_role_assignment($USER->id, $rid, $context->id)) {
|
2024-03-04 22:39:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-25 23:45:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this line is enrollable at all
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function enrollable(): bool {
|
2024-02-25 23:45:39 +01:00
|
|
|
return $this->r->enrollable != self::ENROLLABLE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List the available roles that can enrol the student
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function enrol_roles(): array {
|
2024-02-25 23:45:39 +01:00
|
|
|
global $DB;
|
|
|
|
$list = [];
|
2024-06-03 23:24:16 +02:00
|
|
|
if ($this->r->enrolrole) {
|
|
|
|
$roles = explode(",", $this->r->enrolrole);
|
|
|
|
foreach ($roles as $r) {
|
|
|
|
$roleid = intval($r);
|
|
|
|
if ($roleid > 0) {
|
|
|
|
try {
|
|
|
|
$role = $DB->get_record('role', ["id" => $roleid], "*", MUST_EXIST);
|
|
|
|
$list[] = $role;
|
|
|
|
} catch (\Exception $x) {
|
|
|
|
$role = null;
|
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
}
|
2024-02-25 23:45:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List the available roles that can enrol the student
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function enrol_roleids(): array {
|
2024-02-25 23:45:39 +01:00
|
|
|
global $DB;
|
|
|
|
$list = [];
|
2024-06-03 23:24:16 +02:00
|
|
|
if ($this->r->enrolrole) {
|
|
|
|
$roles = explode(",", $this->r->enrolrole);
|
|
|
|
foreach ($roles as $r) {
|
|
|
|
$roleid = intval($r);
|
|
|
|
if ($roleid > 0) {
|
|
|
|
$list[] = $roleid;
|
|
|
|
}
|
2024-02-25 23:45:39 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-25 23:45:39 +01:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List the available roles that can enrol the student in a proper model
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function enrol_roles_model(): array {
|
2024-02-25 23:45:39 +01:00
|
|
|
$list = [];
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($this->enrol_roles() as $role) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$name = role_get_name($role, $this->context()); // Get localized role name.
|
2024-02-25 23:45:39 +01:00
|
|
|
$list[] = [
|
|
|
|
"id" => $role->id,
|
|
|
|
"name" => $name,
|
|
|
|
];
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-25 23:45:39 +01:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of studyline'),
|
|
|
|
"name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'idnumber of studyline'),
|
|
|
|
"color" => new \external_value(PARAM_TEXT, 'description of studyline'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"sequence" => new \external_value(PARAM_INT, 'order of studyline'),
|
2024-03-04 22:39:29 +01:00
|
|
|
"enrol" => self::enrol_info_structure(),
|
2023-08-24 23:02:41 +02:00
|
|
|
"slots" => new \external_multiple_structure(
|
2023-05-17 21:19:14 +02:00
|
|
|
new \external_single_structure([
|
2023-08-28 11:26:14 +02:00
|
|
|
self::SLOTSET_COURSES => new \external_multiple_structure(
|
2023-08-25 11:52:05 +02:00
|
|
|
studyitem::editor_structure(), 'competency items', VALUE_OPTIONAL),
|
|
|
|
self::SLOTSET_FILTER => new \external_multiple_structure(
|
|
|
|
studyitem::editor_structure(), 'filter items'),
|
2023-05-17 21:19:14 +02:00
|
|
|
])
|
2024-06-02 23:23:32 +02:00
|
|
|
),
|
2024-06-02 19:23:40 +02:00
|
|
|
], "Study line editor structure", $value);
|
2024-02-18 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
/**
|
|
|
|
* Webservice structure for enrolment info
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
|
|
|
public static function enrol_info_structure($value = VALUE_REQUIRED) {
|
|
|
|
return new \external_single_structure([
|
|
|
|
"enrollable" => new \external_value(PARAM_INT, 'enrol mode (raw)'),
|
|
|
|
"enrolroles" => new \external_multiple_structure(new \external_value(PARAM_INT, 'id of role')),
|
|
|
|
"allowedroles" => new \external_multiple_structure(new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of role'),
|
|
|
|
"name" => new \external_value(PARAM_TEXT, 'name of role'),
|
|
|
|
])),
|
2024-06-02 19:23:40 +02:00
|
|
|
"can_enrol" => new \external_value(PARAM_BOOL, 'enrollable by current user', VALUE_OPTIONAL),
|
|
|
|
"enrolled" => new \external_value(PARAM_BOOL, 'student is enrolled', VALUE_OPTIONAL),
|
|
|
|
"enrolled_time" => new \external_value(PARAM_INT, 'moment of enrollment', VALUE_OPTIONAL),
|
|
|
|
"enrolled_by" => new \external_value(PARAM_TEXT, 'Name of enrolling user', VALUE_OPTIONAL),
|
|
|
|
"selfview" => new \external_value(PARAM_BOOL, 'viewing user is the student', VALUE_OPTIONAL),
|
|
|
|
], "Enrollment info", $value);
|
2024-03-04 22:39:29 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 13:47:08 +01:00
|
|
|
/**
|
|
|
|
* Webservice structure for simple 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 {
|
2024-02-18 13:47:08 +01:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of studyline'),
|
|
|
|
"name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
|
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'idnumber of studyline'),
|
|
|
|
"color" => new \external_value(PARAM_TEXT, 'description of studyline'),
|
|
|
|
"sequence" => new \external_value(PARAM_INT, 'order of studyline'),
|
2024-03-04 22:39:29 +01:00
|
|
|
"enrol" => self::enrol_info_structure(),
|
2024-06-02 19:23:40 +02:00
|
|
|
], "Study line simple structure", $value);
|
2024-02-18 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webservice model for simple info
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function simple_model(): array {
|
2024-02-18 13:47:08 +01:00
|
|
|
return [
|
|
|
|
'id' => $this->r->id,
|
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
|
|
|
'color' => $this->r->color,
|
|
|
|
'sequence' => $this->r->sequence,
|
2024-03-04 22:39:29 +01:00
|
|
|
'enrol' => $this->enrol_info_model(),
|
2024-02-18 13:47:08 +01:00
|
|
|
];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice model for editor info
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function editor_model(): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->generate_model("editor");
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Create a model for the given type of operation
|
|
|
|
* @param string $mode One of [ 'editor', 'export']
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
protected function generate_model($mode): array {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Mode parameter is used to geep this function for both editor model and export model.
|
|
|
|
// (Export model results in fewer parameters on children, but is otherwise basically the same as this function).
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$model = [
|
|
|
|
'id' => $this->r->id,
|
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
|
|
|
'color' => $this->r->color,
|
|
|
|
'sequence' => $this->r->sequence,
|
2024-03-04 22:39:29 +01:00
|
|
|
'enrol' => $this->enrol_info_model(),
|
2023-05-17 21:19:14 +02:00
|
|
|
'slots' => [],
|
|
|
|
];
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($mode == "export") {
|
|
|
|
// Id and sequence are not used in export model.
|
2023-05-17 21:19:14 +02:00
|
|
|
unset($model["id"]);
|
|
|
|
unset($model["sequence"]);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Get the number of slots.
|
|
|
|
// As a safety data integrity measure, if there are any items in a higher slot than currently allowed, .
|
2023-08-25 09:44:34 +02:00
|
|
|
// Make sure there are enought slots to account for them.
|
2023-05-17 21:19:14 +02:00
|
|
|
// Alternatively, we could ensure that on reduction of slots, the items that no longer have a slot will be removed.
|
2023-08-25 09:33:42 +02:00
|
|
|
$maxslot = $DB->get_field_select(studyitem::TABLE, "MAX(slot)", "line_id = :lineid", ['lineid' => $this->id]);
|
2023-08-25 17:33:20 +02:00
|
|
|
$numslots = max($this->page->periods(), $maxslot + 1);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Create the required amount of slots.
|
2023-08-25 17:33:20 +02:00
|
|
|
for ($i = 0; $i < $numslots + 1; $i++) {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($mode == "export") {
|
|
|
|
// Export mode does not separate between filter or competency type, since that is determined automatically.
|
2023-05-17 21:19:14 +02:00
|
|
|
$slots = [];
|
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($i > 0) {
|
2023-08-28 11:26:14 +02:00
|
|
|
$slots = [self::SLOTSET_COURSES => [], self::SLOTSET_FILTER => []];
|
2023-05-17 21:19:14 +02:00
|
|
|
} else {
|
|
|
|
$slots = [self::SLOTSET_FILTER => []];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$model['slots'][$i] = $slots;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$children = studyitem::find_studyline_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
|
|
|
if ($mode == "export") {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model['slots'][$c->slot()][] = $c->export_model();
|
|
|
|
} else {
|
|
|
|
$slotset = null;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($c->slot() > 0) {
|
2023-08-28 11:26:14 +02:00
|
|
|
if (in_array($c->type(), self::COURSE_TYPES)) {
|
|
|
|
$slotset = self::SLOTSET_COURSES;
|
2023-08-24 23:02:41 +02:00
|
|
|
} else if (in_array($c->type(), self::FILTER_TYPES)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$slotset = self::SLOTSET_FILTER;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if (in_array($c->type(), self::FILTER0_TYPES)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$slotset = self::SLOTSET_FILTER;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if (isset($slotset)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model['slots'][$c->slot()][$slotset][] = $c->editor_model();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Add a new study line
|
|
|
|
* @param array $fields Properties for study line ['page_id', 'name', 'shortname', 'color']
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function add($fields): self {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!isset($fields['page_id'])) {
|
2023-07-23 16:25:08 +02:00
|
|
|
throw new \InvalidArgumentException("parameter 'page_id' missing");
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 09:33:42 +02:00
|
|
|
$pageid = $fields['page_id'];
|
|
|
|
$sqmax = $DB->get_field_select(self::TABLE, "MAX(sequence)", "page_id = :page_id", ['page_id' => $pageid]);
|
2024-06-02 19:23:40 +02:00
|
|
|
$addable = ['page_id', 'name', 'shortname', 'color', 'enrollable', 'enrolrole'];
|
2023-08-25 17:33:20 +02:00
|
|
|
$info = ['sequence' => $sqmax + 1];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($addable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$info[$f] = $fields[$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$id = $DB->insert_record(self::TABLE, $info);
|
2023-08-25 17:33:20 +02:00
|
|
|
return self::find_by_id($id);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Edit study line properties
|
|
|
|
* @param array $fields Changed roperties for study line ['name', 'shortname', 'color']
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function edit($fields): self {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2024-06-02 19:23:40 +02:00
|
|
|
$editable = ['name', 'shortname', 'color', 'enrollable', 'enrolrole'];
|
|
|
|
$info = ['id' => $this->id ];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($editable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$info[$f] = $fields[$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$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-05-17 21:19:14 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Delete studyline
|
|
|
|
* @param bool $force Force deletion even if study line contains items
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function delete($force = false): success {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($force) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$children = studyitem::find_studyline_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$c->delete($force);
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 09:44:34 +02:00
|
|
|
// Check if this item has study items in it.
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($DB->count_records(studyitem::TABLE, ['line_id' => $this->id]) > 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return success::fail('cannot delete studyline with items');
|
2023-08-25 09:33:42 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$DB->delete_records(self::TABLE, ['id' => $this->id]);
|
|
|
|
return success::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Reorder study lines
|
|
|
|
* @param int[] $resequence New order of study lines by id
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function reorder($resequence): success {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($resequence as $sq) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$DB->update_record(self::TABLE, [
|
|
|
|
'id' => $sq['id'],
|
|
|
|
'sequence' => $sq['sequence'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success::success();
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Find all studylines associated with a studyplan page
|
|
|
|
* @param studyplanpage $page The studyplanpage to search for
|
|
|
|
* @return studyline[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function find_page_children(studyplanpage $page): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$list = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", "page_id = :page_id ORDER BY sequence",
|
2023-07-23 16:25:08 +02:00
|
|
|
['page_id' => $page->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-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of studyline'),
|
|
|
|
"name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'idnumber of studyline'),
|
|
|
|
"color" => new \external_value(PARAM_TEXT, 'description of studyline'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"sequence" => new \external_value(PARAM_INT, 'order of studyline'),
|
2024-03-04 22:39:29 +01:00
|
|
|
"enrol" => self::enrol_info_structure(),
|
2023-08-24 23:02:41 +02:00
|
|
|
"slots" => new \external_multiple_structure(
|
2023-05-17 21:19:14 +02:00
|
|
|
new \external_single_structure([
|
2023-08-28 11:26:14 +02:00
|
|
|
self::SLOTSET_COURSES => new \external_multiple_structure(
|
2023-08-25 11:52:05 +02:00
|
|
|
studyitem::user_structure(), 'competency items', VALUE_OPTIONAL),
|
|
|
|
self::SLOTSET_FILTER => new \external_multiple_structure(
|
|
|
|
studyitem::user_structure(), 'filter items'),
|
2023-05-17 21:19:14 +02:00
|
|
|
])
|
2024-06-02 23:23:32 +02:00
|
|
|
),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'Studyline with user info', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
/**
|
|
|
|
* Webservice model for user enrolment info
|
|
|
|
* @param int $userid ID of user to check specific info for
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
|
|
|
public function enrol_info_model($userid=null) {
|
2024-06-02 19:23:40 +02:00
|
|
|
global $DB, $USER;
|
2024-03-04 22:39:29 +01:00
|
|
|
|
|
|
|
$model = [
|
|
|
|
'enrolroles' => $this->enrol_roleids(),
|
|
|
|
'allowedroles' => $this->enrol_roles_model(),
|
|
|
|
'enrollable' => $this->r->enrollable,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($userid)) {
|
2024-06-02 23:23:32 +02:00
|
|
|
$r = $DB->get_record('local_treestudyplan_lineuser', [
|
2024-03-04 22:39:29 +01:00
|
|
|
'line_id' => $this->id(),
|
|
|
|
'user_id' => $userid,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (empty($r)) {
|
|
|
|
$enrolled = false;
|
2024-06-02 19:23:40 +02:00
|
|
|
$enrolledtime = 0;
|
|
|
|
$enrolledby = "";
|
2024-03-04 22:39:29 +01:00
|
|
|
} else {
|
|
|
|
$enrolled = boolval($r->enrolled);
|
2024-06-02 19:23:40 +02:00
|
|
|
$enrolledtime = $r->timeenrolled;
|
|
|
|
$by = $DB->get_record('user', ["id" => $r->enrolledby]);
|
2024-03-04 22:39:29 +01:00
|
|
|
if (empty($by)) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$enrolledby = \get_string("unknownuser", "core");
|
2024-03-04 22:39:29 +01:00
|
|
|
} else {
|
2024-06-02 19:23:40 +02:00
|
|
|
$enrolledby = "{$by->firstname} {$by->lastname}";
|
2024-03-04 22:39:29 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
$usermodel = [
|
|
|
|
'can_enrol' => $this->can_enrol($userid),
|
|
|
|
"enrolled" => $enrolled,
|
2024-06-02 19:23:40 +02:00
|
|
|
"enrolled_time" => $enrolledtime,
|
|
|
|
"enrolled_by" => $enrolledby,
|
2024-03-08 17:05:07 +01:00
|
|
|
"selfview" => boolval($userid == $USER->id),
|
2024-03-04 22:39:29 +01:00
|
|
|
];
|
2024-06-02 19:23:40 +02:00
|
|
|
$model = array_merge($model, $usermodel);
|
2024-03-04 22:39:29 +01:00
|
|
|
} else {
|
|
|
|
$model["can_enrol"] = $this->can_enrol();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Get the users enrolled in this studyline if relevant
|
|
|
|
* @return array of Userids linked
|
2024-03-08 11:54:39 +01:00
|
|
|
*/
|
|
|
|
public function get_enrolled_userids() {
|
|
|
|
$userids = $this->studyplan()->find_linked_userids();
|
2024-06-02 19:23:40 +02:00
|
|
|
if ($this->enrollable()) {
|
2024-03-08 11:54:39 +01:00
|
|
|
$list = [];
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($userids as $uid) {
|
|
|
|
if ($this->isenrolled($uid)) {
|
2024-03-08 11:54:39 +01:00
|
|
|
$list[] = $uid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
} else {
|
|
|
|
return $userids;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:44:29 +01:00
|
|
|
/**
|
|
|
|
* Check if student is enrolled in the line.
|
|
|
|
* @param int $userid ID of user to check specific info for
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
|
|
|
public function isenrolled($userid) {
|
|
|
|
global $DB;
|
|
|
|
if ($this->r->enrollable == self::ENROLLABLE_NONE) {
|
2024-06-02 23:23:32 +02:00
|
|
|
return true; // If student cannot enrol, the student always is enrolled.
|
2024-03-07 22:44:29 +01:00
|
|
|
} else {
|
2024-06-02 19:23:40 +02:00
|
|
|
$r = $DB->get_record('local_treestudyplan_lineuser', [
|
2024-03-07 22:44:29 +01:00
|
|
|
'line_id' => $this->id(),
|
|
|
|
'user_id' => $userid,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (empty($r)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return boolval($r->enrolled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
/**
|
|
|
|
* List the course id is linked in this studyplan
|
|
|
|
* Used for cohort enrolment cascading
|
|
|
|
* @return int[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function get_linked_course_ids(): array {
|
2024-03-08 11:54:39 +01:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$sql = "SELECT i.course_id
|
|
|
|
FROM {local_treestudyplan_line} l
|
|
|
|
INNER JOIN {local_treestudyplan_item} i ON l.id = i.line_id
|
|
|
|
WHERE l.id = :studyline_id AND i.type = :itemtype";
|
|
|
|
$fields = $DB->get_fieldset_sql($sql, ["studyline_id" => $this->id, "itemtype" => studyitem::COURSE]);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
/**
|
|
|
|
* Enrol student from this line (if line enrollable)
|
|
|
|
* NOTE: This function does not check if the current user should be allowed to do this,
|
|
|
|
* the checks need to be done in the calling webservice / function
|
|
|
|
* @param int $userid of user to enrol
|
|
|
|
*/
|
|
|
|
public function enrol($userid) {
|
2024-06-02 19:23:40 +02:00
|
|
|
global $DB, $USER;
|
2024-03-04 22:39:29 +01:00
|
|
|
|
|
|
|
if ($this->r->enrollable > self::ENROLLABLE_NONE) {
|
|
|
|
$r = $DB->get_record("local_treestudyplan_lineuser",
|
|
|
|
["line_id" => $this->id(),
|
|
|
|
"user_id" => $userid]);
|
|
|
|
if ($r) {
|
|
|
|
// Registration already exists, check if enrol is false and update accordingly.
|
|
|
|
if (! boolval($r->enrolled)) {
|
|
|
|
$r->enrolled = 1;
|
|
|
|
$r->timeenrolled = time();
|
|
|
|
$r->enrolledby = $USER->id;
|
2024-06-02 19:23:40 +02:00
|
|
|
$DB->update_record("local_treestudyplan_lineuser", $r);
|
2024-03-04 22:39:29 +01:00
|
|
|
}
|
|
|
|
// Otherwise, ignore the request.
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Insert new record.
|
|
|
|
$r = new \stdClass;
|
|
|
|
$r->enrolled = 1;
|
|
|
|
$r->line_id = $this->id();
|
|
|
|
$r->user_id = $userid;
|
|
|
|
$r->timeenrolled = time();
|
|
|
|
$r->enrolledby = $USER->id;
|
2024-06-02 19:23:40 +02:00
|
|
|
$DB->insert_record("local_treestudyplan_lineuser", $r);
|
2024-03-04 22:39:29 +01:00
|
|
|
}
|
2024-03-08 11:54:39 +01:00
|
|
|
|
|
|
|
$this->studyplan()->mark_csync_changed();
|
2024-03-04 22:39:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unenrol student from this line (if line enrollable)
|
|
|
|
* NOTE: This function does not check if the current user should be allowed to do this,
|
|
|
|
* the checks need to be done in the calling webservice / function
|
|
|
|
* @param int $userid of user to unenrol
|
|
|
|
*/
|
|
|
|
public function unenrol($userid) {
|
2024-06-02 19:23:40 +02:00
|
|
|
global $DB, $USER;
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
if ($this->r->enrollable > self::ENROLLABLE_NONE) {
|
|
|
|
// Check if an enrollment line exist.
|
|
|
|
$r = $DB->get_record("local_treestudyplan_lineuser",
|
|
|
|
["line_id" => $this->id(),
|
|
|
|
"user_id" => $userid]);
|
|
|
|
if ($r) {
|
|
|
|
// Registration already exists, check if enrolled is true and update accordingly.
|
|
|
|
if (boolval($r->enrolled)) {
|
|
|
|
$r->enrolled = 0;
|
2024-06-02 23:23:32 +02:00
|
|
|
$r->timeenrolled = time(); // Regi.
|
2024-03-04 22:39:29 +01:00
|
|
|
$r->enrolledby = $USER->id;
|
2024-06-02 19:23:40 +02:00
|
|
|
$DB->update_record("local_treestudyplan_lineuser", $r);
|
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
$this->studyplan()->mark_csync_changed();
|
2024-03-04 22:39:29 +01:00
|
|
|
}
|
|
|
|
// Otherwise, ignore the request.
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
}
|
2024-03-04 22:39:29 +01:00
|
|
|
// Otherwise, no action is needed.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$model = [
|
|
|
|
'id' => $this->r->id,
|
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
|
|
|
'color' => $this->r->color,
|
|
|
|
'sequence' => $this->r->sequence,
|
2024-03-04 22:39:29 +01:00
|
|
|
'enrol' => $this->enrol_info_model($userid),
|
2023-05-17 21:19:14 +02:00
|
|
|
'slots' => [],
|
|
|
|
];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Get the number of slots.
|
|
|
|
// As a safety data integrity measure, if there are any items in a higher slot than currently allowed, .
|
2023-08-25 09:44:34 +02:00
|
|
|
// Make sure there are enought slots to account for them.
|
2023-05-17 21:19:14 +02:00
|
|
|
// Alternatively, we could ensure that on reduction of slots, the items that no longer have a slot will be removed.
|
2023-08-25 09:33:42 +02:00
|
|
|
$maxslot = $DB->get_field_select(studyitem::TABLE, "MAX(slot)", "line_id = :lineid", ['lineid' => $this->id]);
|
2023-08-25 17:33:20 +02:00
|
|
|
$numslots = max($this->page->periods(), $maxslot + 1);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Create the required amount of slots.
|
2023-08-25 17:33:20 +02:00
|
|
|
for ($i = 0; $i < $numslots + 1; $i++) {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($i > 0) {
|
2023-08-28 11:26:14 +02:00
|
|
|
$slots = [self::SLOTSET_COURSES => [], self::SLOTSET_FILTER => []];
|
2023-05-17 21:19:14 +02:00
|
|
|
} else {
|
|
|
|
$slots = [self::SLOTSET_FILTER => []];
|
|
|
|
}
|
|
|
|
$model['slots'][$i] = $slots;
|
|
|
|
}
|
|
|
|
|
|
|
|
$children = studyitem::find_studyline_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-08-25 17:33:20 +02:00
|
|
|
if ($c->valid()) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$slotset = null;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($c->slot() > 0) {
|
2023-08-28 11:26:14 +02:00
|
|
|
if (in_array($c->type(), self::COURSE_TYPES)) {
|
|
|
|
$slotset = self::SLOTSET_COURSES;
|
2023-08-24 23:02:41 +02:00
|
|
|
} else if (in_array($c->type(), self::FILTER_TYPES)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$slotset = self::SLOTSET_FILTER;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if (in_array($c->type(), self::FILTER0_TYPES)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$slotset = self::SLOTSET_FILTER;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if (isset($slotset)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model['slots'][$c->slot()][$slotset][] = $c->user_model($userid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
2024-06-03 04:00:46 +02:00
|
|
|
* Duplicate this studyline into another page
|
|
|
|
* @param studyplanpage $newpage Studyplan page to copy the line into
|
2023-08-28 11:26:14 +02:00
|
|
|
* @param array $translation Mapping array of old item ids to new item ids for connection matching
|
2024-06-01 14:00:32 +02:00
|
|
|
* @param bool $bare Set to true to skip copying contents
|
2023-08-28 11:26:14 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function duplicate($newpage, &$translation, $bare = false): self {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Clone the database fields.
|
2023-05-17 21:19:14 +02:00
|
|
|
$fields = clone $this->r;
|
2023-08-25 09:44:34 +02:00
|
|
|
// Set new studyplan id.
|
2023-05-17 21:19:14 +02:00
|
|
|
unset($fields->id);
|
2024-04-19 16:46:30 +02:00
|
|
|
$fields->page_id = $newpage->id();
|
2023-08-25 09:44:34 +02:00
|
|
|
// Create new record with the new data.
|
2023-05-17 21:19:14 +02:00
|
|
|
$id = $DB->insert_record(self::TABLE, (array)$fields);
|
2023-08-25 17:33:20 +02:00
|
|
|
$new = self::find_by_id($id);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2024-06-01 14:00:32 +02:00
|
|
|
if (!$bare) {
|
|
|
|
// Next copy all the study items for this studyline.
|
|
|
|
// And record the original and copy id's in the $translation array.
|
|
|
|
// So the calling function can connect the new studyitems as required.
|
|
|
|
$children = studyitem::find_studyline_children($this);
|
|
|
|
$translation = [];
|
|
|
|
foreach ($children as $c) {
|
|
|
|
$newchild = $c->duplicate($new);
|
|
|
|
$translation[$c->id()] = $newchild->id();
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $new;
|
|
|
|
}
|
|
|
|
|
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-05-17 21:19:14 +02:00
|
|
|
return $this->generate_model("export");
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Import study items into line from decoded array model
|
|
|
|
* @param array $model Decoded array
|
|
|
|
* @param array $itemtranslation Link to array to map old item ids to new item ids for connection matching
|
|
|
|
* @param array $connections Link to array of connections between item
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function import_studyitems(array $model, array &$itemtranslation, array &$connections) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-25 13:04:19 +02:00
|
|
|
foreach ($model as $slot => $slotmodel) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$courselayer = 0;
|
|
|
|
$filterlayer = 0;
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($slotmodel as $itemmodel) {
|
|
|
|
if ($itemmodel["type"] == "course") {
|
2023-05-17 21:19:14 +02:00
|
|
|
$itemmodel["layer"] = $courselayer;
|
|
|
|
$courselayer++;
|
2023-08-25 17:33:20 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$itemmodel["layer"] = $filterlayer;
|
|
|
|
$filterlayer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemmodel["slot"] = $slot;
|
|
|
|
$itemmodel["line_id"] = $this->id();
|
|
|
|
$item = studyitem::import_item($itemmodel);
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!empty($item)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$itemtranslation[$itemmodel["id"]] = $item->id();
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
if (count($itemmodel["connections"]) > 0) {
|
|
|
|
if (! isset($connections[$item->id()]) || ! is_array($connections[$item->id()])) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$connections[$item->id()] = [];
|
|
|
|
}
|
2023-08-25 09:33:42 +02:00
|
|
|
foreach ($itemmodel["connections"] as $toid) {
|
|
|
|
$connections[$item->id()][] = $toid;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|