From 01e8bfd745cea2fbcde09da2b6f399f4b1edd097 Mon Sep 17 00:00:00 2001 From: John Okely Date: Fri, 22 Jan 2016 08:35:15 +0000 Subject: [PATCH] MDL-45064 lti: LTI types in activity chooser --- course/lib.php | 9 ++++ lib/tests/behat/behat_forms.php | 14 ++++++ mod/lti/lib.php | 15 +++++++ mod/lti/locallib.php | 55 +++++++++++++++++++++-- mod/lti/mod_form.php | 8 ++++ mod/lti/tests/behat/addtool.feature | 37 +++++++++++++++ mod/lti/tests/fixtures/tool_provider.html | 8 ++++ 7 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 mod/lti/tests/behat/addtool.feature create mode 100644 mod/lti/tests/fixtures/tool_provider.html diff --git a/course/lib.php b/course/lib.php index e005dc03431..bbc0eb1b288 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1257,6 +1257,15 @@ function get_module_metadata($course, $modnames, $sectionreturn = null) { } include_once($libfile); + $aliases = component_callback($modname, 'get_aliases', array(), array()); + if (count($aliases) > 0) { + foreach ($aliases as $alias) { + $module = $alias; + $module->archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER); + $return[$alias->name] = $module; + } + } + // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!! $gettypesfunc = $modname.'_get_types'; $types = MOD_SUBTYPE_NO_CHILDREN; diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index 90507b8de06..e93e98c6540 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -143,6 +143,20 @@ class behat_forms extends behat_base { } + /** + * Sets the field to wwwroot plus the given path. Include the first slash. + * + * @Given /^I set the field "(?P(?:[^"]|\\")*)" to local url "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $field + * @param string $path + * @return void + */ + public function i_set_the_field_to_local_url($field, $path) { + global $CFG; + $this->set_field_value($field, $CFG->wwwroot . $path); + } + /** * Sets the specified value to the field. * diff --git a/mod/lti/lib.php b/mod/lti/lib.php index 090ae808237..3adf748cd29 100644 --- a/mod/lti/lib.php +++ b/mod/lti/lib.php @@ -196,6 +196,21 @@ function lti_delete_instance($id) { return $DB->delete_records("lti", array("id" => $basiclti->id)); } +/** + * Return aliases of this activity. LTI should have an alias for each configured tool type + * This is so you can add an external tool types directly from the activity chooser + * + * @return array An array of aliases for this activity + **/ +function lti_get_aliases() { + global $CFG, $COURSE; + require_once($CFG->dirroot.'/mod/lti/locallib.php'); + + $types = lti_get_configured_types($COURSE->id); + + return $types; +} + function lti_get_types() { global $OUTPUT; diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index 19578c90095..317403dc398 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -1070,8 +1070,14 @@ function lti_filter_tool_types(array $tools, $state) { return $return; } -function lti_get_types_for_add_instance() { - global $DB, $SITE, $COURSE; +/** + * Returns all lti types visible in this course + * + * @param int $courseid The id of the course to retieve types for + * @return stdClass All the lti types visible in the given course + */ +function lti_get_lti_types_by_course($courseid) { + global $DB, $SITE; $query = "SELECT * FROM {lti_types} @@ -1079,8 +1085,18 @@ function lti_get_types_for_add_instance() { AND (course = :siteid OR course = :courseid) AND state = :active"; - $admintypes = $DB->get_records_sql($query, - array('siteid' => $SITE->id, 'courseid' => $COURSE->id, 'active' => LTI_TOOL_STATE_CONFIGURED)); + return $DB->get_records_sql($query, + array('siteid' => $SITE->id, 'courseid' => $courseid, 'active' => LTI_TOOL_STATE_CONFIGURED)); +} + +/** + * Returns tool types for lti add instance and edit page + * + * @return array Array of lti types + */ +function lti_get_types_for_add_instance() { + global $COURSE; + $admintypes = lti_get_lti_types_by_course($COURSE->id); $types = array(); $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => 0, 'toolproxyid' => null); @@ -1092,6 +1108,37 @@ function lti_get_types_for_add_instance() { return $types; } +/** + * Returns a list of configured types in the given course + * + * @param int $courseid The id of the course to retieve types for + * @return array Array of lti types + */ +function lti_get_configured_types($courseid) { + global $OUTPUT; + $types = array(); + $admintypes = lti_get_lti_types_by_course($courseid); + + foreach ($admintypes as $ltitype) { + $type = new stdClass(); + $type->modclass = MOD_CLASS_ACTIVITY; + $type->name = 'lti_type_' . $ltitype->id; + $type->title = $ltitype->name; + if (empty($ltitype->icon)) { + $type->icon = $OUTPUT->pix_icon('icon', '', 'lti', array('class' => 'icon')); + } else { + $type->icon = html_writer::empty_tag('img', array('src' => $ltitype->icon, 'alt' => $ltitype->name, 'class' => 'icon')); + } + if (!empty($ltitype->description)) { + $type->help = $ltitype->description; + } + $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid, 'sr' => 0, + 'typeid' => $ltitype->id)); + $types[] = $type; + } + return $types; +} + function lti_get_domain_from_url($url) { $matches = array(); diff --git a/mod/lti/mod_form.php b/mod/lti/mod_form.php index efa2a884c7e..af3105da4d8 100644 --- a/mod/lti/mod_form.php +++ b/mod/lti/mod_form.php @@ -96,6 +96,8 @@ class mod_lti_mod_form extends moodleform_mod { // Tool settings. $tooltypes = $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'), array()); + $typeid = optional_param('typeid', false, PARAM_INT); + $mform->getElement('typeid')->setValue($typeid); $mform->addHelpButton('typeid', 'external_tool_type', 'lti'); $toolproxy = array(); @@ -149,6 +151,7 @@ class mod_lti_mod_form extends moodleform_mod { $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions); $mform->setDefault('launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT); $mform->addHelpButton('launchcontainer', 'launchinpopup', 'lti'); + $mform->setAdvanced('launchcontainer'); $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti')); $mform->setType('resourcekey', PARAM_TEXT); @@ -243,6 +246,11 @@ class mod_lti_mod_form extends moodleform_mod { ), ); + if (!empty($typeid)) { + $mform->setAdvanced('typeid'); + $mform->setAdvanced('toolurl'); + } + $PAGE->requires->js_init_call('M.mod_lti.editor.init', array(json_encode($jsinfo)), true, $module); } diff --git a/mod/lti/tests/behat/addtool.feature b/mod/lti/tests/behat/addtool.feature new file mode 100644 index 00000000000..873f83802d7 --- /dev/null +++ b/mod/lti/tests/behat/addtool.feature @@ -0,0 +1,37 @@ +@mod @mod_lti +Feature: Add tool types + In order to provide activities for learners + As a teacher + I need to be able to add external tools to a course + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Terry1 | Teacher1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + And I log in as "admin" + And I navigate to "Manage external tool types" node in "Site administration > Plugins > Activity modules > LTI" + And I follow "Add external tool configuration" + And I set the following fields to these values: + | Tool name | Teaching Tool 1 | + | Show tool type when creating tool instances | 1 | + And I set the field "Tool base URL" to local url "/mod/lti/tests/fixtures/tool_provider.html" + And I press "Save changes" + And I log out + + @javascript + Scenario: Add a tool via the acitivity picker + When I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + And I add a "Teaching Tool 1" to section "1" and I fill the form with: + | Activity name | Test tool activity 1 | + | Launch container | Embed | + And I open "Test tool activity 1" actions menu + And I follow "Edit settings" in the open menu + Then the field "External tool type" matches value "Teaching Tool 1" diff --git a/mod/lti/tests/fixtures/tool_provider.html b/mod/lti/tests/fixtures/tool_provider.html new file mode 100644 index 00000000000..543a796bf40 --- /dev/null +++ b/mod/lti/tests/fixtures/tool_provider.html @@ -0,0 +1,8 @@ + + + Tool provider + + +

This represents a tool provider

+ +