127 lines
		
	
	
		
			No EOL
		
	
	
		
			3.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			127 lines
		
	
	
		
			No EOL
		
	
	
		
			3.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
// This file is part of the Studyplan plugin for Moodle
 | 
						|
//
 | 
						|
// Moodle is free software: you can redistribute it and/or modify
 | 
						|
// it under the terms of the GNU General Public License as published by
 | 
						|
// the Free Software Foundation, either version 3 of the License, or
 | 
						|
// (at your option) any later version.
 | 
						|
//
 | 
						|
// Moodle is distributed in the hope that it will be useful,
 | 
						|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
// GNU General Public License for more details.
 | 
						|
//
 | 
						|
// You should have received a copy of the GNU General Public License
 | 
						|
// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
 | 
						|
/**
 | 
						|
 *
 | 
						|
 * @package    local_treestudyplan
 | 
						|
 * @copyright  2023 P.M. Kuipers
 | 
						|
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
						|
 */
 | 
						|
 | 
						|
namespace local_treestudyplan;
 | 
						|
require_once($CFG->libdir.'/externallib.php');
 | 
						|
 | 
						|
class studyitemconnection {
 | 
						|
 | 
						|
    const TABLE = "local_treestudyplan_connect";
 | 
						|
 | 
						|
    private $r;
 | 
						|
    private $id;
 | 
						|
 | 
						|
 | 
						|
    protected function __construct($r) {
 | 
						|
        $this->r = $r;
 | 
						|
        $this->id = $r->id;
 | 
						|
    }
 | 
						|
 | 
						|
    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'),
 | 
						|
        ], '', $value);
 | 
						|
    }
 | 
						|
 | 
						|
    public function model() {
 | 
						|
        return ['id' => $this->r->id, 'from_id' => $this->r->from_id, 'to_id' => $this->r->to_id];
 | 
						|
    }
 | 
						|
 | 
						|
    public function from_item() {
 | 
						|
        return studyitem::findById($this->r->from_id);
 | 
						|
    }
 | 
						|
 | 
						|
    public function to_item() {
 | 
						|
        return studyitem::findById($this->r->to_id);
 | 
						|
    }
 | 
						|
 | 
						|
    public function from_id() {
 | 
						|
        return $this->r->from_id;
 | 
						|
    }
 | 
						|
 | 
						|
    public function to_id() {
 | 
						|
        return $this->r->to_id;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public static function find_outgoing($item_id) {
 | 
						|
        global $DB;
 | 
						|
        $list = [];
 | 
						|
        $conn_out = $DB->get_records(self::TABLE, ['from_id' => $item_id]);
 | 
						|
        foreach ($conn_out as $c) {
 | 
						|
            $list[] = new self($c);
 | 
						|
        }
 | 
						|
        return $list;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function find_incoming($item_id) {
 | 
						|
        global $DB;
 | 
						|
        $list = [];
 | 
						|
        $conn_in = $DB->get_records(self::TABLE, ['to_id' => $item_id]);
 | 
						|
        foreach ($conn_in as $c) {
 | 
						|
            $list[] = new self($c);
 | 
						|
        }
 | 
						|
        return $list;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function connect($from_id, $to_id) {
 | 
						|
        global $DB;
 | 
						|
 | 
						|
        //check if link already exists.
 | 
						|
 | 
						|
        if (!$DB->record_exists(self::TABLE, ['from_id' => $from_id, 'to_id' => $to_id])) {
 | 
						|
            $id = $DB->insert_record(self::TABLE, [
 | 
						|
                'from_id' => $from_id,
 | 
						|
                'to_id' => $to_id,
 | 
						|
            ]);
 | 
						|
 | 
						|
            return new self($DB->get_record(self::TABLE, ['id' => $id]));
 | 
						|
 | 
						|
        } else {
 | 
						|
            return new self($DB->get_record(self::TABLE, ['from_id' => $from_id, 'to_id' => $to_id]));
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function disconnect($from_id, $to_id) {
 | 
						|
        global $DB;
 | 
						|
 | 
						|
        if ($DB->record_exists(self::TABLE, ['from_id' => $from_id, 'to_id' => $to_id])) {
 | 
						|
            $DB->delete_records(self::TABLE, [
 | 
						|
                'from_id' => $from_id,
 | 
						|
                'to_id' => $to_id,
 | 
						|
            ]);
 | 
						|
 | 
						|
            return success::success('Items Disconnected');
 | 
						|
        } else {
 | 
						|
            return success::success('Connection does not exist');
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function clear($id) {
 | 
						|
        global $DB;
 | 
						|
        $DB->delete_records(self::TABLE, ['from_id' => $id]);
 | 
						|
        $DB->delete_records(self::TABLE, ['to_id' => $id]);
 | 
						|
    }
 | 
						|
 | 
						|
} |