Skip to content

Commit

Permalink
set user positions
Browse files Browse the repository at this point in the history
  • Loading branch information
univietw committed Sep 4, 2024
1 parent 27cb613 commit e82091d
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 15 deletions.
15 changes: 5 additions & 10 deletions lib/editor/tiny/plugins/collaborative/classes/change_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@
*/
class change_manager {

protected $autosaveid;

/** @var int The contextid */
protected $contextid;

/** @var string The page hash reference */
protected $pagehash;

/** @var string The page instance reference */
protected $pageinstance;

/** @var string The elementid for this editor */
protected $elementid;

Expand Down Expand Up @@ -78,11 +73,11 @@ public function add_collaborative_record($newcontenthash, $changes) {
$record->oldcontenthash = $this->oldcontenthash;
$record->userid = $USER->id;

// try {
try {
$record->id = $DB->insert_record('tiny_collaborative_changes', $record);
// } catch(\Exception $e) {
// return "-1";
// }
} catch(\Exception $e) {
return "-1";
}
return $record->id;
}

Expand All @@ -91,7 +86,7 @@ public function get_changes() {
$changesarray = [];
$currenthash = $this->oldcontenthash;
while ($change = $DB->get_record('tiny_collaborative_changes', ['oldcontenthash' => $currenthash,
'pagehash' => $this->$pagehash,
'pagehash' => $this->pagehash,
'elementid' => $this->elementid,
'contextid' => $this->contextid
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_multiple_structure;
use tiny_collaborative;

/**
* Web Service to resume an autosave session.
Expand Down Expand Up @@ -82,19 +83,21 @@ public static function execute(
if (isloggedin() && !isguestuser()) {
$manager = new \tiny_collaborative\change_manager($contextid, $pagehash, $pageinstance, $elementid, $currenthash);
$changes = $manager->get_changes();
$positionmanager = new tiny_collaborative\position_manager($contextid,$pagehash,$elementid);
$positions = $positionmanager->get_user_positions();
}
return $changes;
return ['changes' => $changes, 'positions' => $positions];
}

/**
* Describe the return structure of the external service.
*
* @return external_single_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(new external_value(PARAM_RAW, 'Description of the change')

// Add other fields related to the change here
public static function execute_returns(): external_single_structure {
return new external_single_structure(
['changes' => new external_multiple_structure(PARAM_RAW, 'Description of the change'),
'positions' => new external_multiple_structure(PARAM_RAW, 'Positions of the users')]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.

namespace tiny_collaborative\external;

use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_multiple_structure;
use tiny_collaborative;

/**
* Web Service to resume an autosave session.
*
* @package tiny_autosave
* @category external
* @copyright 2022 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_changes extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'contextid' => new external_value(PARAM_INT, 'The context id that owns the editor', VALUE_REQUIRED),
'elementid' => new external_value(PARAM_RAW, 'The ID of the element', VALUE_REQUIRED),
'pageinstance' => new external_value(PARAM_RAW, 'The ID of the pageinstance', VALUE_REQUIRED),
'position' => new external_value(PARAM_RAW, 'The position of the user', VALUE_REQUIRED),
]);
}

/**
* Reset the autosave entry for this autosave instance.
*
* If not matching autosave area could be found, the function will
* silently return and this is not treated as an error condition.
*
* @param int $contextid The context id of the owner
* @param string $pagehash The hash of the page
* @param string $pageinstance The instance id of the page
* @param string $elementid The id of the element
* @param int $draftid The id of the draftid to resume to
* @return null
*/
public static function execute(
int $contextid,
string $elementid,
string $pageinstance,
string $position
): array {

[
'contextid' => $contextid,
'elementid' => $elementid,
'pageinstance' => $pageinstance,
'position' => $position,
] = self::validate_parameters(self::execute_parameters(), [
'contextid' => $contextid,
'elementid' => $elementid,
'pageinstance' => $pageinstance,
'position' => $position,
]);


// May have been called by a non-logged in user.
if (isloggedin() && !isguestuser()) {
$manager = new \tiny_collaborative\position_manager($contextid,$elementid);
$value = $manager->set_position($pageinstance, $position);
}
return $value;
}

/**
* Describe the return structure of the external service.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure( ['changes' =>
new external_multiple_structure(PARAM_RAW, 'Description of the change'),
'positions' => new external_multiple_structure(PARAM_RAW, '')

// Add other fields related to the change here
);
}
}
94 changes: 94 additions & 0 deletions lib/editor/tiny/plugins/collaborative/classes/position_manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.

namespace tiny_collaborative;

use stdClass;

/**
* Autosave Manager.
*
* @package tiny_autosave
* @copyright 2022 Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class position_manager {

/** @var int The contextid */
protected $contextid;

/** @var string The page hash reference */
protected $pagehash;

/** @var string The elementid for this editor */
protected $elementid;

/**
* Constructor for the autosave manager.
*
* @param int $contextid The contextid of the session
* @param string $pageinstance The page instance
* @param string $elementid The element id
* @param null|stdClass $user The user object for the owner of the autosave
*/
public function __construct(
int $contextid,
string $elementid
) {
global $DB;
$this->contextid = $contextid;
$this->elementid = $elementid;
}

public function set_position($pageinstance,$position) {
global $DB,$USER;

$record = $DB->get_record('tiny_collaborative_positions',[
'contextid' => $this->contextid,
'elementid' => $this->elementid,
'pageinstance' => $pageinstance
]);
if($record) {
$record->position = $position;
$record->timemodified = time();
$DB->update_record('tiny_collaborative_positions', $record);
} else {
$record = new \stdClass();
$record->timemodified = time();
$record->elementid = $this->elementid;
$record->userid = $USER->id;
$record->contextid = $this->contextid;
$record->pageinstance = $pageinstance;
$record->position = $position;
$record = $DB->insert_record('tiny_collaborative_positions', $record);
}
return $record->id;
}

public function get_user_positions() {
global $DB;
$positions = [];
$records = $DB->get_records('tiny_collaborative_positions', [
'elementid' => $this->elementid,
'contextid' => $this->contextid]);
$positions = [];
foreach ($records as $record) {
$positions[] = [$record->userid => $record->position];
}
return $positions;
}

}
8 changes: 8 additions & 0 deletions lib/editor/tiny/plugins/collaborative/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@
<INDEX NAME="colabsearch" UNIQUE="false" FIELDS="pagehash, elementid, contextid"/>
</INDEXES>
</TABLE>
<TABLE NAME="changeme" COMMENT="Default comment for the table, please edit me">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
8 changes: 8 additions & 0 deletions lib/editor/tiny/plugins/collaborative/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@
'loginrequired' => false,
'ajax' => true,
),
'tiny_collaborate_set_position' => array(
'classname' => 'tiny_collaborative\external\save_changes',
'methodname' => 'execute',
'description' => 'Save changes the user made in a collaborative session',
'type' => 'write',
'loginrequired' => false,
'ajax' => true,
),
];

0 comments on commit e82091d

Please sign in to comment.