diff --git a/course/format/singleactivity/lib.php b/course/format/singleactivity/lib.php index 50fa91a7f20..6556c127e50 100644 --- a/course/format/singleactivity/lib.php +++ b/course/format/singleactivity/lib.php @@ -36,6 +36,9 @@ class format_singleactivity extends format_base { /** @var cm_info the current activity. Use get_activity() to retrieve it. */ private $activity = false; + /** @var int The category ID guessed from the form data. */ + private $categoryid = false; + /** * The URL to use for the specified course * @@ -145,6 +148,30 @@ class format_singleactivity extends format_base { */ public function course_format_options($foreditform = false) { static $courseformatoptions = false; + + $fetchtypes = $courseformatoptions === false; + $fetchtypes = $fetchtypes || ($foreditform && !isset($courseformatoptions['activitytype']['label'])); + + if ($fetchtypes) { + $availabletypes = $this->get_supported_activities(); + if ($this->course) { + // The course exists. Test against the course. + $testcontext = context_course::instance($this->course->id); + } else if ($this->categoryid) { + // The course does not exist yet, but we have a category ID that we can test against. + $testcontext = context_coursecat::instance($this->categoryid); + } else { + // The course does not exist, and we somehow do not have a category. Test capabilities against the system context. + $testcontext = context_system::instance(); + } + foreach (array_keys($availabletypes) as $activity) { + $capability = "mod/{$activity}:addinstance"; + if (!has_capability($capability, $testcontext)) { + unset($availabletypes[$activity]); + } + } + } + if ($courseformatoptions === false) { $config = get_config('format_singleactivity'); $courseformatoptions = array( @@ -153,9 +180,13 @@ class format_singleactivity extends format_base { 'type' => PARAM_TEXT, ), ); + + if (!isset($availabletypes[$config->activitytype])) { + $courseformatoptions['activitytype']['default'] = array_keys($availabletypes)[0]; + } } + if ($foreditform && !isset($courseformatoptions['activitytype']['label'])) { - $availabletypes = $this->get_supported_activities(); $courseformatoptionsedit = array( 'activitytype' => array( 'label' => new lang_string('activitytype', 'format_singleactivity'), @@ -183,6 +214,11 @@ class format_singleactivity extends format_base { */ public function create_edit_form_elements(&$mform, $forsection = false) { global $PAGE; + + if (!$this->course && $submitvalues = $mform->getSubmitValues()) { + $this->categoryid = $submitvalues['category']; + } + $elements = parent::create_edit_form_elements($mform, $forsection); if (!$forsection && ($course = $PAGE->course) && !empty($course->format) && $course->format !== 'site' && $course->format !== 'singleactivity') { diff --git a/course/format/singleactivity/tests/behat/create_course.feature b/course/format/singleactivity/tests/behat/create_course.feature new file mode 100644 index 00000000000..6eee07ff1cf --- /dev/null +++ b/course/format/singleactivity/tests/behat/create_course.feature @@ -0,0 +1,38 @@ +@format @format_singleactivity +Feature: Courses can be created in Single Activity mode + In order to create a single activity course + As a manager + I need to create courses and set default values on them + + Scenario: Create a course as a custom course creator + Given the following "users" exist: + | username | firstname | lastname | email | + | kevin | Kevin | the | kevin@example.com | + And the following "roles" exist: + | shortname | name | archetype | + | creator | Creator | | + And the following "system role assigns" exist: + | user | role | contextlevel | + | kevin | creator | System | + And I log in as "admin" + And I set the following system permissions of "Creator" role: + | capability | permission | + | moodle/course:create | Allow | + | moodle/course:update | Allow | + | moodle/course:manageactivities | Allow | + | moodle/course:viewparticipants | Allow | + | moodle/role:assign | Allow | + | mod/quiz:addinstance | Allow | + And I log out + And I log in as "kevin" + And I am on site homepage + When I press "Add a new course" + And I set the following fields to these values: + | Course full name | My first course | + | Course short name | myfirstcourse | + | Format | Single activity format | + And I press "Update format" + Then I should see "Quiz" in the "Type of activity" "field" + And I should not see "Forum" in the "Type of activity" "field" + And I press "Save and display" + And I should see "Adding a new Quiz"