Studyplan marking for csync cascade

This commit is contained in:
PMKuipers 2023-06-27 07:33:27 +02:00
parent 4994f8a63e
commit 13e862de9a
11 changed files with 68 additions and 10 deletions

View File

@ -201,6 +201,7 @@ class associationservice extends \external_api
'cohort_id' => $cohort_id,
]);
$studyplan->mark_csync_changed();
return ['success' => true, 'msg'=>'Cohort connected'];
} else {
@ -240,6 +241,8 @@ class associationservice extends \external_api
'cohort_id' => $cohort_id,
]);
$studyplan->mark_csync_changed();
return ['success' => true, 'msg'=>'Cohort Disconnected'];
} else {
return ['success' => true, 'msg'=>'Connection does not exist'];
@ -314,8 +317,7 @@ class associationservice extends \external_api
'studyplan_id' => $studyplan_id,
'user_id' => $user_id,
]);
return ['success' => true, 'msg'=>'USer Disconnected'];
return ['success' => true, 'msg'=>'User Disconnected'];
} else {
return ['success' => true, 'msg'=>'Connection does not exist'];
}

View File

@ -198,7 +198,12 @@ class studyitem {
}
}
$id = $DB->insert_record(self::TABLE, $info);
return self::findById($id);
$item = self::findById($id);
if($item->type() == self::COURSE){
// Signal the studyplan that a course has been added so it can be marked for csync cascading
$item->getStudyline()->getStudyplan()->mark_csync_changed();
}
return $item;
}
public function edit($fields)

View File

@ -729,6 +729,28 @@ class studyplan {
return true;
}
/**
* Mark the studyplan as changed regarding courses and associated cohorts
*/
public function mark_csync_changed(){
global $DB;
$DB->update_record(self::TABLE, ['id' => $this->id,"csync_flag" => 1]);
$this->r->csync_flag = 1; //manually set it in the cache, if something unexpected happened, an exception has already been thrown anyway
}
/**
* Clear the csync mark
*/
public function clear_csync_changed(){
global $DB;
$DB->update_record(self::TABLE, ['id' => $this->id,"csync_flag" => 0]);
$this->r->csync_flag = 0; //manually set it in the cache, if something unexpected happened, an exception has already been thrown anyway
}
public function has_csync_changed(){
return ($this->r->csync_flag > 0)?true:false;
}
/**
* See if the specified course id is linked in this studyplan
*/

View File

@ -20,12 +20,22 @@ class autocohortsync extends \core\task\scheduled_task {
*/
public function execute() {
if(get_config("local_treestudyplan","csync_enable")){
\mtrace("Automatic csync cascading enabled");
$studyplans = studyplan::find_all();
foreach($studyplans as $studyplan) {
$enroller = new cascadecohortsync($studyplan);
$enroller->sync();
// Only process studyplans that have been marked for change because
// a cohort change has occurred or a course has been added....
if($studyplan->has_csync_changed()){
\mtrace("Studyplan {$studyplan->shortname()} needs processing");
$enroller = new cascadecohortsync($studyplan);
$enroller->sync();
$studyplan->clear_csync_changed();
}
}
\mtrace("Task done");
} else {
\mtrace("Automatic csync cascading disabled");
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/treestudyplan/db" VERSION="20210827" COMMENT="XMLDB file for Moodle local/treestudyplan"
<XMLDB PATH="local/treestudyplan/db" VERSION="20230626" COMMENT="XMLDB file for Moodle local/treestudyplan"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
@ -33,6 +33,7 @@
<FIELD NAME="aggregation" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="bistate" SEQUENCE="false"/>
<FIELD NAME="aggregation_config" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="context_id" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="csync_flag" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="Flags the studyplan as needing a csync update"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View File

@ -3,7 +3,7 @@ $tasks = [
[
'classname' => 'local_treestudyplan\task\autocohortsync',
'blocking' => 0,
'minute' => '*/10',
'minute' => '*',
'hour' => '*',
'day' => '*',
'month' => '*',

View File

@ -183,7 +183,20 @@ function xmldb_local_treestudyplan_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2023051700, 'local', 'treestudyplan');
}
if ($oldversion < 2023062603) {
// Define field csync_flag to be added to local_treestudyplan.
$table = new xmldb_table('local_treestudyplan');
$field = new xmldb_field('csync_flag', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'context_id');
// Conditionally launch add field csync_flag.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Treestudyplan savepoint reached.
upgrade_plugin_savepoint(true, 2023062603, 'local', 'treestudyplan');
}
return true;
}

View File

@ -69,6 +69,8 @@ $string['setting_csync_role_field'] = 'Role';
$string['settingdesc_csync_role_field'] = 'The role to use for automatic cohort sync enrolment created in courses';
$string['setting_csync_remember_manual_csync_field'] = 'Remember existing cohort-syncs';
$string['settingdesc_csync_remember_manual_csync_field'] = 'Mark cohort syncs that were manually created earlier, so they won\'t be removed during autosync if cohorts are removed from the studyplan';
$string['autocohortsync_name'] = 'Studyplan automatic cohort sync cascading';
$string['studyplan_add'] = 'Add study plan';
$string['studyplan_edit'] = 'Edit study plan';

View File

@ -71,6 +71,7 @@ $string['setting_csync_role_field'] = 'Synchronisatierol';
$string['settingdesc_csync_role_field'] = 'Welke rol te gebruiken voor automatische site-groep sync koppelingen';
$string['setting_csync_remember_manual_csync_field'] = 'Bewaar bestaande site-group syncs';
$string['settingdesc_csync_remember_manual_csync_field'] = 'Markeer site-group synchronisaties die eerder handmatig zijn aangemaakt, zodat deze niet worden verwijderd als ze uit het studieplan worden gehaald.';
$string['autocohortsync_name'] = 'Studyplan automatisch site-group synchronisatie doorzetten';
$string['studyplan_add'] = 'Nieuw studieplan';
$string['studyplan_edit'] = 'Studieplan bewerken';

View File

@ -198,7 +198,9 @@ if ($hassiteconfig) {
$student = get_archetype_roles('student');
$student = reset($student);
$page_csync->add(new admin_setting_configselect('local_treestudyplan/csync_roleid',
get_string('defaultrole', 'role'), '', $student->id ?? null, $options));
get_string('setting_csync_role_field', 'local_treestudyplan'),
get_string('settingdesc_csync_role_field', 'local_treestudyplan'),
$student->id ?? null, $options));
}
// Add settings page2 to the admin settings category.

View File

@ -1,6 +1,6 @@
<?php
$plugin->component = 'local_treestudyplan'; // Recommended since 2.0.2 (MDL-26035). Required since 3.0 (MDL-48494)
$plugin->version = 2023062602; // YYYYMMDDHH (year, month, day, iteration)
$plugin->version = 2023062604; // YYYYMMDDHH (year, month, day, iteration)
$plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11)
$plugin->dependencies = [