Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixing issues with launching a tool which is not configured in the ad…

…min side, and improving utility methods for determing if a tool should be registered
  • Loading branch information...
commit a0eeacf9c73ba39b5574f2ddfd805498e3e07b28 1 parent f5134df
scriby authored September 01, 2011
13  mod/lti/db/install.xml
... ...
@@ -1,5 +1,5 @@
1 1
 <?xml version="1.0" encoding="UTF-8" ?>
2  
-<XMLDB PATH="mod/lti/db" VERSION="20110829" COMMENT="XMLDB file for Moodle mod/lti"
  2
+<XMLDB PATH="mod/lti/db" VERSION="20110901" COMMENT="XMLDB file for Moodle mod/lti"
3 3
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
     xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
5 5
 >
@@ -22,10 +22,13 @@
22 22
         <FIELD NAME="instructorcustomparameters" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Additional custom parameters provided by the instructor" PREVIOUS="instructorchoiceallowsetting" NEXT="instructorchoiceacceptgrades"/>
23 23
         <FIELD NAME="instructorchoiceacceptgrades" TYPE="int" LENGTH="1" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" COMMENT="Accept grades from tool" PREVIOUS="instructorcustomparameters" NEXT="grade"/>
24 24
         <FIELD NAME="grade" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="100" SEQUENCE="false" DECIMALS="5" COMMENT="Grade scale" PREVIOUS="instructorchoiceacceptgrades" NEXT="launchcontainer"/>
25  
-        <FIELD NAME="launchcontainer" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" COMMENT="Launch external tool in a pop-up" PREVIOUS="grade" NEXT="debuglaunch"/>
26  
-        <FIELD NAME="debuglaunch" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="Enable the debug-style launch which pauses before auto-submit" PREVIOUS="launchcontainer" NEXT="showtitle"/>
  25
+        <FIELD NAME="launchcontainer" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" COMMENT="Launch external tool in a pop-up" PREVIOUS="grade" NEXT="resourcekey"/>
  26
+        <FIELD NAME="resourcekey" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" PREVIOUS="launchcontainer" NEXT="password"/>
  27
+        <FIELD NAME="password" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" PREVIOUS="resourcekey" NEXT="debuglaunch"/>
  28
+        <FIELD NAME="debuglaunch" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="Enable the debug-style launch which pauses before auto-submit" PREVIOUS="password" NEXT="showtitle"/>
27 29
         <FIELD NAME="showtitle" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="debuglaunch" NEXT="showdescription"/>
28  
-        <FIELD NAME="showdescription" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showtitle"/>
  30
+        <FIELD NAME="showdescription" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showtitle" NEXT="servicesalt"/>
  31
+        <FIELD NAME="servicesalt" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false" PREVIOUS="showdescription"/>
29 32
       </FIELDS>
30 33
       <KEYS>
31 34
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
@@ -42,7 +45,7 @@
42 45
         <FIELD NAME="tooldomain" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="baseurl" NEXT="state"/>
43 46
         <FIELD NAME="state" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="2" SEQUENCE="false" COMMENT="Active = 1, Pending = 2, Rejected = 3" PREVIOUS="tooldomain" NEXT="course"/>
44 47
         <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="state" NEXT="coursevisible"/>
45  
-        <FIELD NAME="coursevisible" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" DEFAULT="0" PREVIOUS="course" NEXT="createdby"/>
  48
+        <FIELD NAME="coursevisible" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="course" NEXT="createdby"/>
46 49
         <FIELD NAME="createdby" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="coursevisible" NEXT="timecreated"/>
47 50
         <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="createdby" NEXT="timemodified"/>
48 51
         <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="timecreated"/>
101  mod/lti/locallib.php
@@ -60,26 +60,47 @@
60 60
 define('LTI_TOOL_STATE_PENDING', 2);
61 61
 define('LTI_TOOL_STATE_REJECTED', 3);
62 62
 
  63
+define('LTI_SETTING_NEVER', 0);
  64
+define('LTI_SETTING_ALWAYS', 1);
  65
+define('LTI_SETTING_DEFAULT', 2);
  66
+
63 67
 /**
64 68
  * Prints a Basic LTI activity
65 69
  *
66 70
  * $param int $basicltiid       Basic LTI activity id
67 71
  */
68 72
 function lti_view($instance, $makeobject=false) {
69  
-    global $PAGE;
  73
+    global $PAGE, $CFG;
70 74
 
71 75
     if(empty($instance->typeid)){
72 76
         $tool = lti_get_tool_by_url_match($instance->toolurl);
73 77
         if($tool){
74 78
             $typeid = $tool->id;
75 79
         } else {
76  
-            //Tool not found
  80
+            $typeid = null;
77 81
         }
78 82
     } else {
79 83
         $typeid = $instance->typeid;
80 84
     }
81 85
     
82  
-    $typeconfig = lti_get_type_config($typeid);
  86
+    if($typeid){
  87
+        $typeconfig = lti_get_type_config($typeid);
  88
+    } else {
  89
+        //There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
  90
+        $typeconfig = (array)$instance;
  91
+        
  92
+        $typeconfig['sendname'] = $instance->instructorchoicesendname;
  93
+        $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr;
  94
+        $typeconfig['customparameters'] = $instance->instructorcustomparameters;
  95
+    }
  96
+    
  97
+    //Default the organizationid if not specified
  98
+    if(empty($typeconfig['organizationid'])){
  99
+        $urlparts = parse_url($CFG->wwwroot);
  100
+        
  101
+        $typeconfig['organizationid'] = $urlparts['host'];
  102
+    }
  103
+    
83 104
     $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $typeconfig['toolurl'];
84 105
     $key = !empty($instance->resourcekey) ? $instance->resourcekey : $typeconfig['resourcekey'];
85 106
     $secret = !empty($instance->password) ? $instance->password : $typeconfig['password'];
@@ -104,8 +125,6 @@ function lti_view($instance, $makeobject=false) {
104 125
     
105 126
     $content = post_launch_html($parms, $endpoint, $debuglaunch);
106 127
     
107  
-//    $cm = get_coursemodule_from_instance("lti", $instance->id);
108  
-//    print '<object height='.$height.' width="80%" data="launch.php?id='.$cm->id.'">'.$content.'</object>';
109 128
     echo $content;
110 129
 }
111 130
 
@@ -303,10 +322,42 @@ function lti_get_type_config($typeid) {
303 322
     return $typeconfig;
304 323
 }
305 324
 
306  
-function lti_get_tools_by_domain($domain){
307  
-    global $DB;
  325
+function lti_get_tools_by_url($url, $state){
  326
+    $domain = lti_get_domain_from_url($url);
308 327
     
309  
-    return $DB->get_records('lti_types', array('tooldomain' => $domain, 'state' => LTI_TOOL_STATE_CONFIGURED));
  328
+    return lti_get_tools_by_domain($domain, $state);
  329
+}
  330
+
  331
+function lti_get_tools_by_domain($domain, $state = null, $courseid = null){
  332
+    global $DB, $SITE;
  333
+    
  334
+    $filters = array('tooldomain' => $domain);
  335
+    
  336
+    $statefilter = '';
  337
+    $coursefilter = '';
  338
+    
  339
+    if($state){
  340
+        $statefilter = 'AND state = :state';
  341
+    }
  342
+  
  343
+    if($courseid && $courseid != $SITE->id){
  344
+        $coursefilter = 'OR course = :courseid';
  345
+    }
  346
+    
  347
+    $query = <<<QUERY
  348
+        SELECT * FROM {lti_types}
  349
+        WHERE
  350
+            tooldomain = :tooldomain
  351
+        AND (course = :siteid $coursefilter)
  352
+        $statefilter
  353
+QUERY;
  354
+    
  355
+    return $DB->get_records_sql($query, array(
  356
+        'courseid' => $courseid, 
  357
+        'siteid' => $SITE->id, 
  358
+        'tooldomain' => $domain, 
  359
+        'state' => $state
  360
+    ));
310 361
 }
311 362
 
312 363
 /**
@@ -341,29 +392,42 @@ function lti_get_domain_from_url($url){
341 392
     }
342 393
 }
343 394
 
344  
-function lti_get_tool_by_url_match($url){
345  
-    $domain = lti_get_domain_from_url($url);
346  
-    
347  
-    $possibletools = lti_get_tools_by_domain($domain);
  395
+function lti_get_tool_by_url_match($url, $courseid = null){
  396
+    $possibletools = lti_get_tools_by_url($url, LTI_TOOL_STATE_CONFIGURED, $courseid);
348 397
     
349 398
     return lti_get_best_tool_by_url($url, $possibletools);
350 399
 }
351 400
 
  401
+function lti_get_url_thumbprint($url){
  402
+    $urlparts = parse_url(strtolower($url));
  403
+    if(!isset($urlparts['path'])){
  404
+        $urlparts['path'] = '';
  405
+    }
  406
+    
  407
+    if(substr($urlparts['host'], 0, 3) === 'www'){
  408
+        $urllparts['host'] = substr(3);
  409
+    }
  410
+    
  411
+    return $urllower = $urlparts['host'] . '/' . $urlparts['path'];
  412
+}
  413
+
352 414
 function lti_get_best_tool_by_url($url, $tools){
353 415
     if(count($tools) === 0){
354 416
         return null;
355 417
     }
356 418
     
357  
-    $urllower = strtolower($url);
  419
+    $urllower = lti_get_url_thumbprint($url);
358 420
     
359 421
     foreach($tools as $tool){
360 422
         $tool->_matchscore = 0;
361  
-        
362  
-        $toolbaseurllower = strtolower($tool->baseurl);
  423
+         
  424
+        $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl);
363 425
         
364 426
         if($urllower === $toolbaseurllower){
  427
+            //100 points for exact match
365 428
             $tool->_matchscore += 100;
366  
-        } else if(strstr($urllower, $toolbaseurllower) >= 0){
  429
+        } else if(substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower){
  430
+            //50 points if it starts with the base URL
367 431
             $tool->_matchscore += 50;
368 432
         }
369 433
     }
@@ -377,6 +441,11 @@ function lti_get_best_tool_by_url($url, $tools){
377 441
         
378 442
     }, (object)array('_matchscore' => -1));
379 443
     
  444
+    //None of the tools are suitable for this URL
  445
+    if($bestmatch->_matchscore <= 0){
  446
+        return null;
  447
+    }
  448
+    
380 449
     return $bestmatch;
381 450
 }
382 451
 
6  mod/lti/view.php
@@ -78,7 +78,11 @@
78 78
 }
79 79
 
80 80
 $tool = lti_get_tool_by_url_match($basiclti->toolurl);
81  
-$toolconfig = lti_get_type_config($tool->id);
  81
+if($tool){
  82
+    $toolconfig = lti_get_type_config($tool->id);
  83
+} else {
  84
+    $toolconfig = array('launchcontainer' => LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS);
  85
+}
82 86
 
83 87
 $PAGE->set_cm($cm, $course); // set's up global $COURSE
84 88
 $context = get_context_instance(CONTEXT_MODULE, $cm->id);

0 notes on commit a0eeacf

Please sign in to comment.
Something went wrong with that request. Please try again.