moodle_local_treestudyplan/classes/studyitemconnection.php

189 lines
5.4 KiB
PHP
Raw Normal View History

<?php
2023-08-24 23:02:41 +02:00
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
2023-08-27 21:23:39 +02:00
* Class to manage connection lines between study items
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');
2023-08-27 21:23:39 +02:00
/**
* Class to manage connection lines between study items
*/
class studyitemconnection {
2023-08-27 21:23:39 +02:00
/**
2023-08-27 22:20:17 +02:00
* Database table for this model
2023-08-27 21:23:39 +02:00
* @var string */
const TABLE = "local_treestudyplan_connect";
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
*/
private $r;
2023-08-27 21:23:39 +02:00
/** @var int */
private $id;
2023-08-27 21:23:39 +02:00
/**
* Construct new object based on database object
* @param stdClass $r Database record
*/
2023-08-24 23:02:41 +02:00
protected function __construct($r) {
$this->r = $r;
$this->id = $r->id;
}
2023-08-27 21:23:39 +02:00
/**
* Webservice structure for basic info
* @param int $value Webservice requirement constant
2023-08-27 23:27:07 +02:00
* @return \external_description Webservice output structure
2023-08-27 21:23:39 +02:00
*/
2023-08-25 13:04:19 +02:00
public static function structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
'id' => new \external_value(PARAM_INT, 'id of connection'),
'from_id' => new \external_value(PARAM_INT, 'id of start item'),
'to_id' => new \external_value(PARAM_INT, 'id of end item'),
2023-08-24 23:02:41 +02:00
], '', $value);
}
2023-08-27 21:23:39 +02:00
/**
* Webservice model for basic info
* @return array Webservice data model
*/
2023-08-24 23:02:41 +02:00
public function model() {
return ['id' => $this->r->id, 'from_id' => $this->r->from_id, 'to_id' => $this->r->to_id];
}
2023-08-27 21:23:39 +02:00
/**
* Get originating studyitem for this connection
*/
public function from_item() : studyitem {
2023-08-25 17:33:20 +02:00
return studyitem::find_by_id($this->r->from_id);
}
2023-08-27 21:23:39 +02:00
/**
* Get target studyitem for this connection
*/
public function to_item() : studyitem {
2023-08-25 17:33:20 +02:00
return studyitem::find_by_id($this->r->to_id);
}
2023-08-27 21:23:39 +02:00
/**
* Get originating studyitem id for this connection
*/
public function from_id() : int {
return $this->r->from_id;
}
2023-08-27 21:23:39 +02:00
/**
* Get target studyitem id for this connection
*/
public function to_id() :int {
return $this->r->to_id;
}
2023-08-27 21:23:39 +02:00
/**
* Get all connections originating at a given studyitem's id
* @param int $itemid Id of the studyitem
* @return self[]
*/
public static function find_outgoing($itemid) : array {
global $DB;
$list = [];
2023-08-25 09:33:42 +02:00
$connout = $DB->get_records(self::TABLE, ['from_id' => $itemid]);
foreach ($connout as $c) {
$list[] = new self($c);
}
return $list;
}
2023-08-27 21:23:39 +02:00
/**
* Get all connections terminating at a given studyitem's id
* @param int $itemid Id of the studyitem
* @return self[]
*/
2023-08-25 09:33:42 +02:00
public static function find_incoming($itemid) {
global $DB;
$list = [];
2023-08-25 09:33:42 +02:00
$connin = $DB->get_records(self::TABLE, ['to_id' => $itemid]);
foreach ($connin as $c) {
$list[] = new self($c);
}
return $list;
}
2023-08-27 21:23:39 +02:00
/**
* Create a new connection between two study items
* @param int $fromid Id of the originating studyitem
* @param int $toid Id of the target studyitem
*/
2023-08-25 09:33:42 +02:00
public static function connect($fromid, $toid) {
global $DB;
2023-08-25 12:16:51 +02:00
// Check if link already exists.
2023-08-24 23:02:41 +02:00
2023-08-25 09:33:42 +02:00
if (!$DB->record_exists(self::TABLE, ['from_id' => $fromid, 'to_id' => $toid])) {
$id = $DB->insert_record(self::TABLE, [
2023-08-25 09:33:42 +02:00
'from_id' => $fromid,
'to_id' => $toid,
]);
2023-08-24 23:02:41 +02:00
return new self($DB->get_record(self::TABLE, ['id' => $id]));
} else {
2023-08-25 09:33:42 +02:00
return new self($DB->get_record(self::TABLE, ['from_id' => $fromid, 'to_id' => $toid]));
}
}
2023-08-27 21:23:39 +02:00
/**
* Delete a connection between two study items
* @param int $fromid Id of the originating studyitem
* @param int $toid Id of the target studyitem
*/
2023-08-25 09:33:42 +02:00
public static function disconnect($fromid, $toid) {
global $DB;
2023-08-25 09:33:42 +02:00
if ($DB->record_exists(self::TABLE, ['from_id' => $fromid, 'to_id' => $toid])) {
$DB->delete_records(self::TABLE, [
2023-08-25 09:33:42 +02:00
'from_id' => $fromid,
'to_id' => $toid,
]);
2023-08-24 23:02:41 +02:00
return success::success('Items Disconnected');
} else {
return success::success('Connection does not exist');
}
2023-08-24 23:02:41 +02:00
}
2023-08-27 21:23:39 +02:00
/**
* Clear all connection to and from a given studyitem
* @param int $id Id of the studyitem
*/
public static function clear($id) {
global $DB;
$DB->delete_records(self::TABLE, ['from_id' => $id]);
$DB->delete_records(self::TABLE, ['to_id' => $id]);
}
2023-08-25 11:52:05 +02:00
}