diff --git a/classes/form/studyplan_fromtemplateform.php b/classes/form/studyplan_fromtemplateform.php index 8a92011..f4c5e2c 100644 --- a/classes/form/studyplan_fromtemplateform.php +++ b/classes/form/studyplan_fromtemplateform.php @@ -100,51 +100,62 @@ class studyplan_fromtemplateform extends formbase { $templatelist[$s->id()] = implode(" / ",$c['path']) . " / " . $s->name(); } - $field = 'template_id'; - $mform->addElement('autocomplete', $field, - get_string('studyplan_fromtemplate','local_treestudyplan'), - $templatelist); - $mform->addRule($field, null, 'required', null, 'client'); + if (count($templatelist) > 0) { + $mform->addElement('hidden','hastemplates','yes'); - $field = 'name'; - $mform->addElement('text',$field, - get_string('studyplan_name','local_treestudyplan'), - []); - $mform->addRule($field, null, 'required', null, 'client'); + $field = 'template_id'; + $mform->addElement('autocomplete', $field, + get_string('studyplan_fromtemplate','local_treestudyplan'), + $templatelist); + $mform->addRule($field, null, 'required', null, 'client'); - $field = 'shortname'; - $mform->addElement('text',$field, - get_string('studyplan_shortname','local_treestudyplan'), - []); - $mform->addRule($field, null, 'required', null, 'client'); - - $field = 'idnumber'; - $mform->addElement('text',$field, - get_string('studyplan_idnumber','local_treestudyplan'), - []); - - $contextlist = []; - foreach(courseservice::list_available_categories('edit') as $c){ - $contextlist[$c['context_id']] = implode(" / ",$c['category']['path']); - } - - $mform->addElement('autocomplete', 'context_id', - get_string('studyplan_context','local_treestudyplan'), - $contextlist); - - $mform->addRule('context_id', null, 'required', null, 'client'); - - $timeless = \get_config("local_treestudyplan","timelessperiods"); - if ( !$timeless) { - // Only add these fields if the studyplans are timed - $field = 'startdate'; - $mform->addElement('date_selector',$field, - get_string('studyplan_startdate','local_treestudyplan'), + $field = 'name'; + $mform->addElement('text',$field, + get_string('studyplan_name','local_treestudyplan'), []); $mform->addRule($field, null, 'required', null, 'client'); - } + $field = 'shortname'; + $mform->addElement('text',$field, + get_string('studyplan_shortname','local_treestudyplan'), + []); + $mform->addRule($field, null, 'required', null, 'client'); + $field = 'idnumber'; + $mform->addElement('text',$field, + get_string('studyplan_idnumber','local_treestudyplan'), + []); + + $contextlist = []; + foreach(courseservice::list_available_categories('edit') as $c){ + $contextlist[$c['context_id']] = implode(" / ",$c['category']['path']); + } + + $field = 'context_id'; + $mform->addElement('autocomplete', $field, + get_string('studyplan_context','local_treestudyplan'), + $contextlist); + $mform->addRule($field, null, 'required', null, 'client'); + + $timeless = \get_config("local_treestudyplan","timelessperiods"); + if ( !$timeless) { + // Only add these fields if the studyplans are timed + $field = 'startdate'; + $mform->addElement('date_selector',$field, + get_string('studyplan_startdate','local_treestudyplan'), + []); + $mform->addRule($field, null, 'required', null, 'client'); + + } + } else { + $mform->addElement('hidden','hastemplates','no'); + + $field = 'warning'; + $mform->addElement('static', $field, + get_string('warning','core'), + get_string('no_templates','local_treestudyplan') + ); + } } /** @@ -156,16 +167,20 @@ class studyplan_fromtemplateform extends formbase { protected function process_submitted_data($entry) { $customdata = (object)$this->_customdata; - // Find template study plan. - $template = studyplan::find_by_id($entry->template_id); - // Copy template plan. - $plan = $template->duplicate($entry->name,$entry->shortname,$entry->context_id,$entry->idnumber,$entry->startdate); + if($entry->hastemplates == "yes") { + // Find template study plan. + $template = studyplan::find_by_id($entry->template_id); + // Copy template plan. + $plan = $template->duplicate($entry->name,$entry->shortname,$entry->context_id,$entry->idnumber,$entry->startdate); - /* Return the simple model of the plan to make sure we can update stuff. - Parse it through the clean_returnvalue function of exernal api (of which studyplanservice is a subclass) - so we return it in a consistent way - */ - return studyplanservice::clean_returnvalue(studyplan::simple_structure(),$plan->simple_model()); + /* Return the simple model of the plan to make sure we can update stuff. + Parse it through the clean_returnvalue function of exernal api (of which studyplanservice is a subclass) + so we return it in a consistent way + */ + return studyplanservice::clean_returnvalue(studyplan::simple_structure(),$plan->simple_model()); + } else { + return null; + } } diff --git a/lang/en/local_treestudyplan.php b/lang/en/local_treestudyplan.php index 9f8432b..66d8903 100644 --- a/lang/en/local_treestudyplan.php +++ b/lang/en/local_treestudyplan.php @@ -511,4 +511,5 @@ $string["line_can_enrol"] = 'You can register for this line'; $string["line_is_enrolled"] = 'You are registered for this line'; $string["line_enrolled_in"] = 'Registered in {$a}'; $string["switch_coach_editmode"] = "Edit studyplan"; -$string["suspended"] = "Suspended"; \ No newline at end of file +$string["suspended"] = "Suspended"; +$string["no_templates"] = "There are no templates available. Mark a study plan as template to enable template based creation."; \ No newline at end of file diff --git a/lang/nl/local_treestudyplan.php b/lang/nl/local_treestudyplan.php index de011e9..19bad82 100644 --- a/lang/nl/local_treestudyplan.php +++ b/lang/nl/local_treestudyplan.php @@ -511,4 +511,5 @@ $string["line_can_enrol"] = 'Je kunt jezelf inschrijven voor deze leerlijn'; $string["line_is_enrolled"] = 'Je bent ingeschreven voor deze leerlijn'; $string["line_enrolled_in"] = 'Ingeschreven in {$a}'; $string["switch_coach_editmode"] = "Studieplan bewerken"; -$string["suspended"] = "Tijdelijk uitgeschakeld"; \ No newline at end of file +$string["suspended"] = "Tijdelijk uitgeschakeld"; +$string["no_templates"] = "Er zijn geen templates beschikbaar. Zet het vinkje 'Bruikbaar als template' aan bij een studieplan om deze als template te kunnen gebruiken."; \ No newline at end of file