dirroot.'/blocks/gradelevel/lib.php'); class block_gradelevel_edit_form extends block_edit_form { protected function specific_definition($mform) { global $CFG; // retrieve levelset $levelset = $this->block->levelset; // Section header title according to language file. $mform->addElement('header', 'config_header', get_string('blocksettings', 'block')); $skills = block_gradelevel_levelset::list_all(); // sort by name usort( $skills, function( $a, $b) { return ( $a->getName() < $b->getName()) ? -1 : 1; } ); $options = array( '-1' => get_string('edit_noneskill', 'block_gradelevel')); foreach($skills as $skill) { $options[$skill->getId()] = $skill->getName(); } // A sample string variable with a default value. $mform->addElement('select', 'attached_skill', get_string('edit_pickskill', 'block_gradelevel'), $options); } public function set_data($defaults) { global $COURSE; $defaults->attached_skill = -1; // retrieve levelset $skill = block_gradelevel_levelset::find_by_course($COURSE->id); if(isset($skill)){ $defaults->attached_skill = $skill->getId(); } parent::set_data($defaults); } public function get_data() { $data = parent::get_data(); if(isset($data)) { $this->save_data($data); } return $data; } protected function save_data($data) { global $COURSE; // retrieve levelset if(isset($data->attached_skill)) { $currentskill = block_gradelevel_levelset::find_by_course($COURSE->id); if(isset($currentskill)) { $this->debug("deetaching..."); $currentskill->detach_course($COURSE->id); } $skill = block_gradelevel_levelset::find_by_id($data->attached_skill); if(isset($skill)){ $this->debug("attaching..."); $skill->attach_course($COURSE->id); } } } private function debug($msg) { error_log($msg, 3, '/tmp/moodledevlog.log'); } }