Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many improvements #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 187 additions & 4 deletions classes/observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public static function enrol_instance_created(\core\event\enrol_instance_created
if (strcasecmp($instance->enrol, 'meta') == 0) {
$course = get_course($instance->courseid);

$syncall = get_config('local_metagroups', 'syncall');

// Return early if course doesn't use groups.
if (groups_get_course_groupmode($course) == NOGROUPS) {
if (!$syncall && groups_get_course_groupmode($course) == NOGROUPS) {
return;
}

// Immediate synchronization could be expensive, defer to adhoc task.
$task = new \local_metagroups\task\synchronize();
$task = new \local_metagroups\task\adhoc();
$task->set_custom_data(['courseid' => $course->id]);

\core\task\manager::queue_adhoc_task($task);
Expand Down Expand Up @@ -86,14 +88,16 @@ public static function enrol_instance_deleted(\core\event\enrol_instance_deleted
public static function group_created(\core\event\group_created $event) {
global $DB;

$syncall = get_config('local_metagroups', 'syncall');

$group = $event->get_record_snapshot('groups', $event->objectid);

$courseids = local_metagroups_parent_courses($group->courseid);
foreach ($courseids as $courseid) {
$course = get_course($courseid);

// If parent course doesn't use groups, we can skip synchronization.
if (groups_get_course_groupmode($course) == NOGROUPS) {
// If parent course doesn't use groups and syncall disabled, we can skip synchronization.
if (!$syncall && groups_get_course_groupmode($course) == NOGROUPS) {
continue;
}

Expand All @@ -102,6 +106,12 @@ public static function group_created(\core\event\group_created $event) {
$metagroup->courseid = $course->id;
$metagroup->idnumber = $group->id;
$metagroup->name = $group->name;
$metagroup->description = $group->description;
$metagroup->descriptionformat = $group->descriptionformat;
$metagroup->picture = $group->picture;
$metagroup->hidepicture = $group->hidepicture;
// No need to sync enrolmentkey, user should be able to enrol only on source course.
$metagroup->enrolmentkey = null;

groups_create_group($metagroup, false, false);
}
Expand All @@ -125,6 +135,12 @@ public static function group_updated(\core\event\group_updated $event) {

if ($metagroup = $DB->get_record('groups', array('courseid' => $course->id, 'idnumber' => $group->id))) {
$metagroup->name = $group->name;
$metagroup->description = $group->description;
$metagroup->descriptionformat = $group->descriptionformat;
$metagroup->picture = $group->picture;
$metagroup->hidepicture = $group->hidepicture;
// No need to sync enrolmentkey, user should be able to enrol only on source course.
$metagroup->enrolmentkey = null;

groups_update_group($metagroup, false, false);
}
Expand Down Expand Up @@ -195,4 +211,171 @@ public static function group_member_removed(\core\event\group_member_removed $ev
}
}
}

/**
* Grouping created
*
* @param \core\event\grouping_created $event
* @return void
*/
public static function grouping_created(\core\event\grouping_created $event) {
global $DB;

$syncgroupings = get_config('local_metagroups', 'syncgroupings');
if (!$syncgroupings) {
return;
}

$grouping = $event->get_record_snapshot('groupings', $event->objectid);

$courseids = local_metagroups_parent_courses($grouping->courseid);
foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

// If parent course doesn't use groups and syncall disabled, we can skip synchronization.
if (!$syncall && groups_get_course_groupmode($course) == NOGROUPS) {
continue;
}

if (!$DB->record_exists('groupings', array('courseid' => $course->id, 'idnumber' => $grouping->id))) {
$metagrouping = new \stdClass();
$metagrouping->courseid = $course->id;
$metagrouping->idnumber = $grouping->id;
$metagrouping->name = $grouping->name;
groups_create_grouping($metagrouping);
}
}
}

/**
* Grouping deleted
*
* @param \core\event\grouping_deleted $event
* @return void
*/
public static function grouping_deleted(\core\event\grouping_deleted $event) {
global $DB;

$syncgroupings = get_config('local_metagroups', 'syncgroupings');
if (!$syncgroupings) {
return;
}

$grouping = $event->get_record_snapshot('groupings', $event->objectid);

$courseids = local_metagroups_parent_courses($grouping->courseid);

foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

if ($metagrouping = $DB->get_record('groupings', array('courseid' => $course->id, 'idnumber' => $grouping->id))) {
groups_delete_grouping($metagrouping);
}
}
}

/**
* Grouping updated
*
* @param \core\event\grouping_updated $event
* @return void
*/
public static function grouping_updated(\core\event\grouping_updated $event) {
global $DB;

$syncgroupings = get_config('local_metagroups', 'syncgroupings');
if (!$syncgroupings) {
return;
}

$grouping = $event->get_record_snapshot('groupings', $event->objectid);

$courseids = local_metagroups_parent_courses($grouping->courseid);
foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

if ($metagrouping = $DB->get_record('groupings', array('courseid' => $course->id, 'idnumber' => $grouping->id))) {
$metagrouping->name = $grouping->name;
groups_update_grouping($metagrouping);
}
}
}

/**
* Course updated
* We need to listen for course_updated event to check situation, when group mode changed from NOGROUPS
*
* @param \core\event\course_updated $event
* @return void
*/
public static function course_updated(\core\event\course_updated $event) {
global $DB;

$syncall = get_config('local_metagroups', 'syncall');
if ($syncall) {// If syncall is on, then course is alredy synced.
return;
}

// Immediate synchronization could be expensive, defer to adhoc task.
$task = new \local_metagroups\task\adhoc();
$task->set_custom_data(['courseid' => $event->objectid]);

\core\task\manager::queue_adhoc_task($task);
}

/**
* Group assigned to grouping
*
* @param \core\event\grouping_group_assigned $event
* @return void
*/
public static function grouping_group_assigned(\core\event\grouping_group_assigned $event) {
global $DB;

$syncgroupings = get_config('local_metagroups', 'syncgroupings');
if (!$syncgroupings) {
return;
}

$grouping = $event->get_record_snapshot('groupings', $event->objectid);
$groupid = $event->other['groupid'];

$parents = local_metagroups_parent_courses($grouping->courseid);
foreach($parents as $parentid) {
if ($metagrouping = $DB->get_record('groupings', array('courseid' => $parentid, 'idnumber' => $grouping->id))) {
$targetgroups = local_metagroups_group_match($groupid, $parentid);
foreach ($targetgroups as $targetgroup) {
groups_assign_grouping($metagrouping->id, $targetgroup->id);
}
}
}
}

/**
* Group unassigned from grouping
*
* @param \core\event\grouping_group_assigned $event
* @return void
*/
public static function grouping_group_unassigned(\core\event\grouping_group_unassigned $event) {
global $DB;

$syncgroupings = get_config('local_metagroups', 'syncgroupings');
if (!$syncgroupings) {
return;
}

$grouping = $event->get_record_snapshot('groupings', $event->objectid);
$groupid = $event->other['groupid'];

$parents = local_metagroups_parent_courses($grouping->courseid);
foreach($parents as $parentid) {
if ($metagrouping = $DB->get_record('groupings', array('courseid' => $parentid, 'idnumber' => $grouping->id))) {
$targetgroups = local_metagroups_group_match($groupid, $parentid);
foreach ($targetgroups as $targetgroup) {
groups_unassign_grouping($metagrouping->id, $targetgroup->id);
}
}
}
}
}
2 changes: 1 addition & 1 deletion classes/task/synchronize.php → classes/task/adhoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

require_once($CFG->dirroot . '/local/metagroups/locallib.php');

class synchronize extends \core\task\adhoc_task {
class adhoc extends \core\task\adhoc_task {

/**
* Execute the synchronize task
Expand Down
58 changes: 58 additions & 0 deletions classes/task/scheduled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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/>.

/**
* Sync scheduled task
*
* @package local_metagroups
* @copyright 2016 Vadim Dvorovenko ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_metagroups\task;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/local/metagroups/locallib.php');

/**
* Class for sync scheduled task
* @package local_metagroups
* @copyright 2016 Vadim Dvorovenko ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduled extends \core\task\scheduled_task {

/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('scheduledtask', 'local_metagroups');
}

/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
$trace = new \text_progress_trace();
local_metagroups_sync($trace, null);
$trace->finished();
}

}
31 changes: 31 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,35 @@
'eventname' => '\core\event\group_member_removed',
'callback' => '\local_metagroups\observers::group_member_removed',
),

array(
'eventname' => '\core\event\grouping_created',
'callback' => '\local_metagroups\observers::grouping_created',
),

array(
'eventname' => '\core\event\grouping_updated',
'callback' => '\local_metagroups\observers::grouping_updated',
),

array(
'eventname' => '\core\event\grouping_deleted',
'callback' => '\local_metagroups\observers::grouping_deleted',
),

array(
'eventname' => '\core\event\course_updated',
'callback' => '\local_metagroups\observers::course_updated',
),

array(
'eventname' => '\core\event\grouping_group_assigned',
'callback' => '\local_metagroups\observers::grouping_group_assigned',
),

array(
'eventname' => '\core\event\grouping_group_unassigned',
'callback' => '\local_metagroups\observers::grouping_group_unassigned',
),

);
38 changes: 38 additions & 0 deletions db/tasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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/>.

/**
* Scheduled tasks definition
*
* @package local_metagroups
* @copyright 2016 Vadim Dvorovenko ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$tasks = array(
array(
'classname' => 'local_metagroups\task\scheduled',
'blocking' => 0,
'minute' => '0',
'hour' => '0',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 1
)
);
7 changes: 6 additions & 1 deletion lang/en/local_metagroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@

defined('MOODLE_INTERNAL') || die();

$string['pluginname'] = 'Meta-course group synchronization';
$string['pluginname'] = 'Meta-course group and grouping synchronization';
$string['privacy:metadata:core_group'] = 'The Meta-course group synchronization plugin can create groups or use existing groups to add participants of linked courses';
$string['scheduledtask'] = 'Meta-course group and grouping synchronization';
$string['syncall'] = 'Syncronize groups in all courses';
$string['syncall_desc'] = 'If enabled, this plugin will syncronize groups and groupings in courses, where group mode is set to "No groups".';
$string['syncgroupings'] = 'Syncronize groupings';
$string['syncgroupings_desc'] = 'If enabled, this plugin will syncronize groupings in addition to groups.';
Loading