Extracting add/edit type functions for reuse
authorChris Scribner <scriby@gmail.com>
Thu, 1 Sep 2011 17:52:09 +0000 (13:52 -0400)
committerChris Scribner <scriby@gmail.com>
Mon, 7 Nov 2011 01:41:48 +0000 (20:41 -0500)
mod/lti/locallib.php
mod/lti/typessettings.php

index e0fc4ae..da2d545 100644 (file)
@@ -56,6 +56,7 @@ define('LTI_LAUNCH_CONTAINER_EMBED', 2);
 define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);\r
 define('LTI_LAUNCH_CONTAINER_WINDOW', 4);\r
 \r
+define('LTI_TOOL_STATE_ANY', 0);\r
 define('LTI_TOOL_STATE_CONFIGURED', 1);\r
 define('LTI_TOOL_STATE_PENDING', 2);\r
 define('LTI_TOOL_STATE_REJECTED', 3);\r
@@ -392,8 +393,8 @@ function lti_get_domain_from_url($url){
     }\r
 }\r
 \r
-function lti_get_tool_by_url_match($url, $courseid = null){\r
-    $possibletools = lti_get_tools_by_url($url, LTI_TOOL_STATE_CONFIGURED, $courseid);\r
+function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED){\r
+    $possibletools = lti_get_tools_by_url($url, $state, $courseid);\r
     \r
     return lti_get_best_tool_by_url($url, $possibletools);\r
 }\r
@@ -642,6 +643,79 @@ function lti_get_type_type_config($id) {
     return $type;\r
 }\r
 \r
+function lti_prepare_type_for_save($type, $config){\r
+    $type->baseurl = $config->lti_toolurl;\r
+    $type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);\r
+    $type->name = $config->lti_typename;\r
+    \r
+    $type->coursevisible = !empty($config->lti_coursevisible) ? $config->lti_coursevisible : 0;\r
+    $config->lti_coursevisible = $type->coursevisible;\r
+    \r
+    $type->timemodified = time();\r
+    \r
+    unset ($config->lti_typename);\r
+    unset ($config->lti_toolurl);\r
+}\r
+\r
+function lti_update_type($type, $config){\r
+    global $DB;\r
+    \r
+    lti_prepare_type_for_save($type, $config);\r
+    \r
+    if ($DB->update_record('lti_types', $type)) {\r
+        foreach ($config as $key => $value) {\r
+            if (substr($key, 0, 4)=='lti_' && !is_null($value)) {\r
+                $record = new StdClass();\r
+                $record->typeid = $type->id;\r
+                $record->name = substr($key, 4);\r
+                $record->value = $value;\r
+                \r
+                lti_update_config($record);\r
+            }\r
+        }\r
+    }\r
+}\r
+\r
+function lti_add_type($type, $config){\r
+    global $USER, $SITE, $DB;\r
+    \r
+    lti_prepare_type_for_save($type, $config);\r
+    \r
+    if(!isset($type->state)){\r
+        $type->state = LTI_TOOL_STATE_PENDING;\r
+    }\r
+    \r
+    if(!isset($type->timecreated)){\r
+        $type->timecreated = time();\r
+    }\r
+    \r
+    if(!isset($type->createdby)){\r
+        $type->createdby = $USER->id;\r
+    }\r
+    \r
+    if(!isset($type->course)){\r
+        $type->course = $SITE->id;\r
+    }\r
+    \r
+    //Create a salt value to be used for signing passed data to extension services\r
+    $config->lti_servicesalt = uniqid('', true);\r
+\r
+    $id = $DB->insert_record('lti_types', $type);\r
+\r
+    if ($id) {\r
+        foreach ($config as $key => $value) {\r
+            if (substr($key, 0, 4)=='lti_' && !is_null($value)) {\r
+                $record = new StdClass();\r
+                $record->typeid = $id;\r
+                $record->name = substr($key, 4);\r
+                $record->value = $value;\r
+\r
+                lti_add_config($record);\r
+            }\r
+        }\r
+    }\r
+}\r
+\r
 /**\r
  * Add a tool configuration in the database\r
  *\r
index 12386e0..20f3fda 100644 (file)
@@ -80,65 +80,19 @@ $data = data_submitted();
 \r
 if (confirm_sesskey() && isset($data->submitbutton)) {\r
     $type = new StdClass();\r
-    $type->name = $data->lti_typename;\r
-    $type->baseurl = $data->lti_toolurl;\r
-    $type->tooldomain = lti_get_domain_from_url($data->lti_toolurl);\r
-    $type->course = $SITE->id;\r
-    $type->coursevisible = !empty($data->lti_coursevisible) ? $data->lti_coursevisible : 0;\r
-    $type->timemodified = time();\r
-    \r
-    $data->lti_coursevisible = $type->coursevisible;//When not checked, it does not appear in data array. Set it manually.\r
     \r
     if (isset($id)) {\r
         $type->id = $id;\r
+\r
+        lti_update_type($type, $data);\r
         \r
-        if ($DB->update_record('lti_types', $type)) {\r
-            unset ($data->lti_typename);\r
-            \r
-            foreach ($data as $key => $value) {\r
-                if (substr($key, 0, 4)=='lti_' && !is_null($value)) {\r
-                    $record = new StdClass();\r
-                    $record->typeid = $id;\r
-                    $record->name = substr($key, 4);\r
-                    $record->value = $value;\r
-                    if (lti_update_config($record)) {\r
-                        $statusmsg = get_string('changessaved');\r
-                    } else {\r
-                        $errormsg = get_string('errorwithsettings', 'admin');\r
-                    }\r
-                }\r
-            }\r
-        }\r
         redirect($redirect);\r
         die;\r
     } else {\r
-        $type->createdby = $USER->id;\r
-        $type->timecreated = time();\r
         $type->state = LTI_TOOL_STATE_CONFIGURED;\r
+       \r
+        lti_add_type($type, $data);\r
         \r
-        //Create a salt value to be used for signing passed data to extension services\r
-        $data->lti_servicesalt = uniqid('', true);\r
-        \r
-        $id = $DB->insert_record('lti_types', $type);\r
-        \r
-        if ($id) {\r
-            unset ($data->lti_typename);\r
-            foreach ($data as $key => $value) {\r
-                if (substr($key, 0, 4)=='lti_' && !is_null($value)) {\r
-                    $record = new StdClass();\r
-                    $record->typeid = $id;\r
-                    $record->name = substr($key, 4);\r
-                    $record->value = $value;\r
-                    if (lti_add_config($record)) {\r
-                        $statusmsg = get_string('changessaved');\r
-                    } else {\r
-                        $errormsg = get_string('errorwithsettings', 'admin');\r
-                    }\r
-                }\r
-            }\r
-        } else {\r
-            $errormsg = get_string('errorwithsettings', 'admin');\r
-        }\r
         redirect($redirect);\r
         die;\r
     }\r