diff --git a/classes/task/autocohortsync.php b/classes/task/autocohortsync.php new file mode 100644 index 0000000..ee5d2be --- /dev/null +++ b/classes/task/autocohortsync.php @@ -0,0 +1,33 @@ +dirroot.'/course/externallib.php'); +use local_treestudyplan\studyplan; +use local_treestudyplan\enrolcohortsync; + +class autocohortsync extends \core\task\scheduled_task { + + /** + * Return the task's name as shown in admin screens. + * + * @return string + */ + public function get_name() { + return get_string('autocohortsync_name', 'local_treestudyplan'); + } + + /** + * Execute the task. + */ + public function execute() { + + if(get_config("local_treestudyplan","csync_enable")){ + $studyplans = studyplan::find_all(); + + foreach($studyplans as $studyplan) { + $enroller = new enrolcohortsync($studyplan); + $enroller->sync(); + } + } + + } +} \ No newline at end of file diff --git a/classes/task/deepcopy.php b/classes/task/deepcopy.php deleted file mode 100644 index 21de81b..0000000 --- a/classes/task/deepcopy.php +++ /dev/null @@ -1,137 +0,0 @@ -dirroot.'/course/externallib.php'); - -class deepcopy extends \core\task\adhoc_task { - - public const TABLE = "local_treestudyplan_item"; - private $id; - - public function __construct(){ - } - - public static function create($studyplan_id,$params){ - global $USER, $DB; - - $recordid = $DB->insert_record(self::TABLE, [ - 'studyplan_id' => $studyplan_id, - 'status' => 'pending', - 'completion' => 0, - 'parameters' => \json_encode($params), - ]); - - $task = new self(); - $task->set_custom_data((object)[ // cast to object to avoid confusion below (data encoded as JSON which always returns object on dictionary array) - 'recordid' => $recordid, - ]); - $task->set_userid($USER->id); - return $task; - } - - /** - * Return the task's name as shown in admin screens. - * - * @return string - */ - public function get_name() { - return get_string('task_process_request', 'report_downloadall'); - } - - protected function setStatus($status,$completion){ - global $DB; - if(!empty($this->id)){ - $DB->update_record(self::TABLE, ['id' => $this->id,"status"=>$status,"completion" => $completion]); - } - } - - public static function findStatus($studyplan_id){ - global $DB; - $records = $DB->get_records(self::TABLE,['studyplan_id' => $studyplan_id]); - $list = []; - - - return $list; - } - - - /** - * Execute the task. - */ - public function execute() { - global $DB; - $data = $this->get_custom_data(); // returns array data as object, because JSON - $recordid = $data->recordid; - $this->id = $recordid; - $record = $DB->get_record(self::TABLe,['id' => $recordid]); - $params = \json_decode(); - - $this->setStatus('copying',0); - - // gather the study items that need to be converted - $studyitems = []; - $studyplan = studyplan::findById($record->studyplan_id); - foreach(studyline::find_studyplan_children($studyplan) as $line){ - foreach(studyitem::find_studyline_children($line) as $item){ - if($item->type() == studyitem::COURSE && $item->isValid()){ - // queue this studyitem and related study line (for extra info) - $studyitems[] = [$item,$line]; - } - } - } - - // Make sure we can calculate progress - $count = count($studyitems); - - for($i=0;$i<$count;$i++){ - $progress = 100.0 * (floatval($i) / floatval($count)); - $this->setStatus('copying',$progress); - [$old, $line] = $studyitems[$i]; - $old_ci = $old->getcourseinfo(); - $old_course = $old_ci->course(); - - // perform a copy of the course - /** - * Duplicate a course - * - * @param int $courseid - * @param string $fullname Duplicated course fullname - * @param string $shortname Duplicated course shortname - * @param int $categoryid Duplicated course parent category id - * @param int $visible Duplicated course availability - * @param array $options List of backup options - * @return array New course info - * @since Moodle 2.3 - */ - // public static function duplicate_course($courseid, $fullname, $shortname, $categoryid, $visible = 1, $options = array()); - - if(!array_key_exists($old_course->id)) - { - $new_fullname = $old_course->fullname; - $new_shortname = $old_course->shortname; - $new_categoryid = $old_course->category; // We likely don't want this in the same cetegory, but in a new one for the next school year - // TODO: select new category, and optionally include - - // TODO: generate new shortname and verify it is unique - - [$new_course_id,] = \core_course_external::duplicate_course($old_course->id,$new_fullname,$new_shortname,$new_categoryid,true,[ - 'enrolments' => 0, - ]); - - $duplicationmap[$old_course->id] = $new_course_id; - } - else { - $new_course_id = $duplicationmap[$old_course->id]; - } - - // retrieve the course - $new_course = \get_course($new_course_id); - - // now, update the timing of the course, based on the provided parameters - // TODO: Make sure that the timing of courses included in multiple slots, is updated to match the start and end of alle those slots. - - - } - $this->setStatus('done',$progress); - - } -} \ No newline at end of file diff --git a/db/tasks.php b/db/tasks.php new file mode 100644 index 0000000..e921373 --- /dev/null +++ b/db/tasks.php @@ -0,0 +1,12 @@ + 'local_treestudyplan\task\autocohortsync', + 'blocking' => 0, + 'minute' => '*/10', + 'hour' => '*', + 'day' => '*', + 'month' => '*', + 'dayofweek' => '*', + ], +]; \ No newline at end of file