Skip to content

Commit

Permalink
Add option to enable sync only on newly created courses
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed Jan 12, 2021
1 parent d236551 commit c4bf458
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
16 changes: 11 additions & 5 deletions local/o365/classes/feature/usergroups/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,25 +605,31 @@ public static function delete_course_group($courseid) {
*
* @param int $courseid The ID of the course.
* @param bool $enabled Whether to enable or disable.
* @param bool $allfeatures Whether to enable all features, or just teams.
*/
public static function set_course_group_enabled($courseid, $enabled = true) {
public static function set_course_group_enabled($courseid, $enabled = true, $allfeatures = false) {
$usergroupconfig = get_config('local_o365', 'usergroupcustom');
$usergroupconfig = @json_decode($usergroupconfig, true);
if (empty($usergroupconfig) || !is_array($usergroupconfig)) {
$usergroupconfig = [];
}

if ($enabled === true) {
$usergroupconfig[$courseid] = $enabled;
static::set_course_group_feature_enabled($courseid, ['team'],
$enabled);
} else {
if (isset($usergroupconfig[$courseid])) {
unset($usergroupconfig[$courseid]);
static::delete_course_group($courseid);
}
static::set_course_group_feature_enabled($courseid, ['team'],
$enabled);
}

$features = ['team', 'onedrive', 'calendar', 'conversations', 'notebook'];
if ($allfeatures) {
static::set_course_group_feature_enabled($courseid, $features, $enabled);
} else {
static::set_course_group_feature_enabled($courseid, ['team'], $enabled);
}

set_config('usergroupcustom', json_encode($usergroupconfig), 'local_o365');
}

Expand Down
23 changes: 18 additions & 5 deletions local/o365/classes/observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,19 +530,32 @@ public static function construct_sharepoint_api_with_system_user() {
* Handle course_created event.
*
* Does the following:
* - create a sharepoint site and associated groups.
* - enable sync on new courses if course sync is "custom", and the option to enable sync on new courses by default is set.
* - create a sharepoint site and associated groups.
*
* @param \core\event\course_created $event The triggered event.
* @return bool Success/Failure.
*/
public static function handle_course_created(\core\event\course_created $event) {
if (\local_o365\utils::is_configured() !== true || \local_o365\rest\sharepoint::is_configured() !== true) {
if (\local_o365\utils::is_configured() !== true) {
return false;
}
$sharepoint = static::construct_sharepoint_api_with_system_user();
if (!empty($sharepoint)) {
$sharepoint->create_course_site($event->objectid);

// Enable team sync for newly created courses if the create teams setting is "custom", and the option to enable sync on
// new courses by default is on.
$syncnewcoursesetting = get_config('local_o365', 'sync_new_course');
if ((get_config('local_o365', 'createteams') === 'oncustom') && $syncnewcoursesetting) {
\local_o365\feature\usergroups\utils::set_course_group_enabled($event->objectid, true, true);
}

if (\local_o365\rest\sharepoint::is_configured() === true) {
$sharepoint = static::construct_sharepoint_api_with_system_user();
if (!empty($sharepoint)) {
$sharepoint->create_course_site($event->objectid);
}
}

return true;
}

/**
Expand Down
21 changes: 18 additions & 3 deletions local/o365/classes/page/acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,14 @@ public function mode_usermatch() {
* Endpoint to change Teams customization.
*/
public function mode_usergroupcustom_change() {
$coursedata = json_decode(required_param('coursedata', PARAM_RAW), true);
require_sesskey();

// Save enabled by default on new course settings.
$enabledfornewcourse = required_param('newcourse', PARAM_BOOL);
set_config('sync_new_course', $enabledfornewcourse, 'local_o365');

// Save course settings.
$coursedata = json_decode(required_param('coursedata', PARAM_RAW), true);
foreach ($coursedata as $courseid => $course) {
if (!is_scalar($courseid) || ((string)$courseid !== (string)(int)$courseid)) {
// Non-intlike courseid value. Invalid. Skip.
Expand Down Expand Up @@ -524,7 +529,7 @@ public function mode_usergroupcustom_bulkchange() {
* Teams customization.
*/
public function mode_usergroupcustom() {
global $OUTPUT, $PAGE;
global $CFG, $OUTPUT, $PAGE;

$PAGE->navbar->add(get_string('acp_usergroupcustom', 'local_o365'), new \moodle_url($this->url, ['mode' => 'usergroupcustom']));

Expand Down Expand Up @@ -688,7 +693,10 @@ public function mode_usergroupcustom() {
$js .= ' // Send data to server. '."\n";
$js .= '$.ajax({
url: \''.$endpoint->out(false).'\',
data: {coursedata: JSON.stringify(coursedata)},
data: {
coursedata: JSON.stringify(coursedata),
newcourse: $("input#id_s_local_o365_sync_new_course").prop("checked"),
},
type: "POST",
success: function(data) {
console.log(data);
Expand All @@ -700,6 +708,13 @@ public function mode_usergroupcustom() {
echo \html_writer::script($js);
echo \html_writer::tag('h2', get_string('acp_usergroupcustom', 'local_o365'));

// Option to enable sync by default for new courses.
require_once($CFG->libdir . '/adminlib.php');
$enablefornewcourse = new \admin_setting_configcheckbox('local_o365/sync_new_course',
get_string('acp_usergroupcustom_new_course', 'local_o365'),
get_string('acp_usergroupcustom_new_course_desc', 'local_o365'), '0');
echo $enablefornewcourse->output_html(get_config('local_o365', 'sync_new_course'));

// Bulk Operations
$strbulkenable = get_string('acp_usergroupcustom_bulk_enable', 'local_o365');
$strbulkdisable = get_string('acp_usergroupcustom_bulk_disable', 'local_o365');
Expand Down
2 changes: 2 additions & 0 deletions local/o365/lang/en/local_o365.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
$string['acp_usergroupcustom_bulk'] = 'Bulk Operations';
$string['acp_usergroupcustom_bulk_enable'] = 'Enable All';
$string['acp_usergroupcustom_bulk_disable'] = 'Disable All';
$string['acp_usergroupcustom_new_course'] = 'Enabled by default for new course';
$string['acp_usergroupcustom_new_course_desc'] = 'If enabled, all newly created courses will have sync enabled by default';
$string['acp_usergroupcustom_savemessage'] = 'Your changes have been saved.';
$string['acp_usergroupcustom_searchwarning'] = 'Note: Searches will lose any unsaved progress. Press save changes to ensure your changes are saved.';
$string['groups_team'] = 'Teams';
Expand Down

0 comments on commit c4bf458

Please sign in to comment.