MDL-45064 mod_lti: use callback get_shortcuts()
authorMarina Glancy <marina@moodle.com>
Mon, 4 Apr 2016 00:58:12 +0000 (08:58 +0800)
committerMarina Glancy <marina@moodle.com>
Wed, 13 Apr 2016 00:30:05 +0000 (08:30 +0800)
deprecate callback for ltisource plugins get_types(), instead introduce
get_shortcuts()

mod/lti/lib.php
mod/lti/locallib.php
mod/lti/source/upgrade.txt [new file with mode: 0644]

index 3adf748..117b4bf 100644 (file)
@@ -198,60 +198,51 @@ function lti_delete_instance($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
+ * This is so you can add an external tool types directly to the activity chooser
  *
- * @return array An array of aliases for this activity
+ * @param stdClass $defaultitem default item that would be added to the activity chooser if this callback was not present.
+ *     It has properties: archetype, name, title, help, icon, link
+ * @return array An array of aliases for this activity. Each element is an object with same list of properties as $defaultitem.
+ *     Properties title and link are required
  **/
-function lti_get_aliases() {
+function lti_get_shortcuts($defaultitem) {
     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;
-
-    $subtypes = array();
-    foreach (get_plugin_list('ltisource') as $name => $dir) {
-        if ($moretypes = component_callback("ltisource_$name", 'get_types')) {
-            $subtypes = array_merge($subtypes, $moretypes);
+    $types = lti_get_configured_types($COURSE->id, $defaultitem->link->param('sr'));
+    $types[] = $defaultitem;
+
+    // Add items defined in ltisource plugins.
+    foreach (core_component::get_plugin_list('ltisource') as $pluginname => $dir) {
+        if ($moretypes = component_callback("ltisource_$pluginname", 'get_types')) {
+            // Callback 'get_types()' in 'ltisource' plugins is deprecated in 3.1 and will be removed in 3.5, TODO MDL-53697.
+            debugging('Deprecated callback get_types() is found in ltisource_' . $pluginname .
+                ', use get_shortcuts() instead', DEBUG_DEVELOPER);
+            $grouptitle = get_string('modulenameplural', 'mod_lti');
+            foreach ($moretypes as $subtype) {
+                // Instead of adding subitems combine the name of the group with the name of the subtype.
+                $subtype->title = get_string('activitytypetitle', '',
+                    (object)['activity' => $grouptitle, 'type' => $subtype->typestr]);
+                // Re-implement the logic of get_module_metadata() in Moodle 3.0 and below for converting
+                // subtypes into items in activity chooser.
+                $subtype->type = str_replace('&amp;', '&', $subtype->type);
+                $subtype->name = preg_replace('/.*type=/', '', $subtype->type);
+                $subtype->link = new moodle_url($defaultitem->link, array('type' => $subtype->name));
+                if (empty($subtype->help) && !empty($subtype->name) &&
+                        get_string_manager()->string_exists('help' . $subtype->name, $pluginname)) {
+                    $subtype->help = get_string('help' . $subtype->name, $pluginname);
+                }
+                unset($subtype->typestr);
+                $types[] = $subtype;
+            }
+        }
+        // LTISOURCE plugins can also implement callback get_shortcuts() to add items to the activity chooser.
+        // The return values are the same as of the 'mod' callbacks except that $defaultitem is only passed for reference and
+        // should not be added to the return value.
+        if ($moretypes = component_callback("ltisource_$pluginname", 'get_shortcuts', array($defaultitem))) {
+            $types = array_merge($types, $moretypes);
         }
     }
-    if (empty($subtypes)) {
-        return MOD_SUBTYPE_NO_CHILDREN;
-    }
-
-    $types = array();
-
-    $type           = new stdClass();
-    $type->modclass = MOD_CLASS_ACTIVITY;
-    $type->type     = 'lti_group_start';
-    $type->typestr  = '--'.get_string('modulenameplural', 'mod_lti');
-    $types[]        = $type;
-
-    $link     = get_string('modulename_link', 'mod_lti');
-    $linktext = get_string('morehelp');
-    $help     = get_string('modulename_help', 'mod_lti');
-    $help    .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext, true), array('class' => 'helpdoclink'));
-
-    $type           = new stdClass();
-    $type->modclass = MOD_CLASS_ACTIVITY;
-    $type->type     = '';
-    $type->typestr  = get_string('generaltool', 'mod_lti');
-    $type->help     = $help;
-    $types[]        = $type;
-
-    $types = array_merge($types, $subtypes);
-
-    $type           = new stdClass();
-    $type->modclass = MOD_CLASS_ACTIVITY;
-    $type->type     = 'lti_group_end';
-    $type->typestr  = '--';
-    $types[]        = $type;
-
     return $types;
 }
 
index 317403d..bc6a9aa 100644 (file)
@@ -1074,7 +1074,7 @@ function lti_filter_tool_types(array $tools, $state) {
  * 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
+ * @return stdClass[] All the lti types visible in the given course
  */
 function lti_get_lti_types_by_course($courseid) {
     global $DB, $SITE;
@@ -1112,9 +1112,10 @@ function lti_get_types_for_add_instance() {
  * 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
+ * @param int $sectionreturn section to return to for forming the URLs
+ * @return array Array of lti types. Each element is object with properties: name, title, icon, help, link
  */
-function lti_get_configured_types($courseid) {
+function lti_get_configured_types($courseid, $sectionreturn = 0) {
     global $OUTPUT;
     $types = array();
     $admintypes = lti_get_lti_types_by_course($courseid);
@@ -1129,11 +1130,8 @@ function lti_get_configured_types($courseid) {
         } 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));
+        $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid,
+            'sr' => $sectionreturn, 'typeid' => $ltitype->id));
         $types[] = $type;
     }
     return $types;
diff --git a/mod/lti/source/upgrade.txt b/mod/lti/source/upgrade.txt
new file mode 100644 (file)
index 0000000..5bfcc6a
--- /dev/null
@@ -0,0 +1,7 @@
+This files describes API changes in /mod/lti/source/* - LTI source plugins,
+information provided here is intended especially for developers.
+
+=== 3.1 ===
+
+* Callback get_types() is deprecated, instead ltisource plugins can define callback get_shortcuts().
+  See source code for lti_get_shortcuts() and get_module_metadata().