Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactoring some code to use constants and deleting old services

  • Loading branch information...
commit 5e078d62c63cbd946423351b710d78747575f971 1 parent f17f495
scriby authored October 05, 2011
4  mod/lti/lib.php
@@ -91,7 +91,7 @@ function lti_add_instance($formdata) {
91 91
     
92 92
     $id = $DB->insert_record("lti", $formdata);
93 93
 
94  
-    if ($formdata->instructorchoiceacceptgrades == 1) {
  94
+    if ($formdata->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
95 95
         $basiclti = $DB->get_record('lti', array('id'=>$id));
96 96
         
97 97
         if(!isset($formdata->cmidnumber)){
@@ -128,7 +128,7 @@ function lti_update_instance($formdata) {
128 128
         $formdata->showdescription = 0;
129 129
     }
130 130
     
131  
-    if ($formdata->instructorchoiceacceptgrades == 1) {
  131
+    if ($formdata->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
132 132
         $basicltirec = $DB->get_record("lti", array("id" => $formdata->id));
133 133
         $basicltirec->cmidnumber = $formdata->cmidnumber;
134 134
         
20  mod/lti/locallib.php
@@ -66,7 +66,7 @@
66 66
 
67 67
 define('LTI_SETTING_NEVER', 0);
68 68
 define('LTI_SETTING_ALWAYS', 1);
69  
-define('LTI_SETTING_DEFAULT', 2);
  69
+define('LTI_SETTING_DELEGATE', 2);
70 70
 
71 71
 /**
72 72
  * Prints a Basic LTI activity
@@ -199,29 +199,29 @@ function lti_build_request($instance, $typeconfig, $course) {
199 199
     }
200 200
 
201 201
     if ( isset($placementsecret) &&
202  
-         ( $typeconfig['acceptgrades'] == 1 ||
203  
-         ( $typeconfig['acceptgrades'] == 2 && $instance->instructorchoiceacceptgrades == 1 ) ) ) {
  202
+         ( $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
  203
+         ( $typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS ) ) ) {
204 204
         $requestparams["lis_result_sourcedid"] = $sourcedid;
205 205
         $requestparams["ext_ims_lis_basic_outcome_url"] = $CFG->wwwroot.'/mod/lti/service.php';
206 206
     }
207 207
 
208 208
     if ( isset($placementsecret) &&
209  
-         ( $typeconfig['allowroster'] == 1 ||
210  
-         ( $typeconfig['allowroster'] == 2 && $instance->instructorchoiceallowroster == 1 ) ) ) {
  209
+         ( $typeconfig['allowroster'] == LTI_SETTING_ALWAYS ||
  210
+         ( $typeconfig['allowroster'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceallowroster == LTI_SETTING_ALWAYS ) ) ) {
211 211
         $requestparams["ext_ims_lis_memberships_id"] = $sourcedid;
212 212
         $requestparams["ext_ims_lis_memberships_url"] = $CFG->wwwroot.'/mod/lti/service.php';
213 213
     }
214 214
 
215 215
     // Send user's name and email data if appropriate
216  
-    if ( $typeconfig['sendname'] == 1 ||
217  
-         ( $typeconfig['sendname'] == 2 && $instance->instructorchoicesendname == 1 ) ) {
  216
+    if ( $typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
  217
+         ( $typeconfig['sendname'] == LTI_SETTING_DELEGATE && $instance->instructorchoicesendname == LTI_SETTING_ALWAYS ) ) {
218 218
         $requestparams["lis_person_name_given"] =  $USER->firstname;
219 219
         $requestparams["lis_person_name_family"] =  $USER->lastname;
220 220
         $requestparams["lis_person_name_full"] =  $USER->firstname." ".$USER->lastname;
221 221
     }
222 222
 
223  
-    if ( $typeconfig['sendemailaddr'] == 1 ||
224  
-         ( $typeconfig['sendemailaddr'] == 2 && $instance->instructorchoicesendemailaddr == 1 ) ) {
  223
+    if ( $typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
  224
+         ( $typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE && $instance->instructorchoicesendemailaddr == LTI_SETTING_ALWAYS ) ) {
225 225
         $requestparams["lis_person_contact_email_primary"] = $USER->email;
226 226
     }
227 227
 
@@ -235,7 +235,7 @@ function lti_build_request($instance, $typeconfig, $course) {
235 235
     if ($customstr) {
236 236
         $custom = lti_split_custom_parameters($customstr);
237 237
     }
238  
-    if (!isset($typeconfig['allowinstructorcustom']) || $typeconfig['allowinstructorcustom'] == 0) {
  238
+    if (!isset($typeconfig['allowinstructorcustom']) || $typeconfig['allowinstructorcustom'] == LTI_SETTING_NEVER) {
239 239
         $requestparams = array_merge($custom, $requestparams);
240 240
     } else {
241 241
         if ($instructorcustomstr) {
391  mod/lti/oldservice.php
... ...
@@ -1,391 +0,0 @@
1  
-<?php
2  
-// This file is part of BasicLTI4Moodle
3  
-//
4  
-// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
5  
-// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
6  
-// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
7  
-// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
8  
-// are already supporting or going to support BasicLTI. This project Implements the consumer
9  
-// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
10  
-// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
11  
-// at the GESSI research group at UPC.
12  
-// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
13  
-// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
14  
-// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
15  
-//
16  
-// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
17  
-// of the Universitat Politecnica de Catalunya http://www.upc.edu
18  
-// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu
19  
-//
20  
-// Moodle is free software: you can redistribute it and/or modify
21  
-// it under the terms of the GNU General Public License as published by
22  
-// the Free Software Foundation, either version 3 of the License, or
23  
-// (at your option) any later version.
24  
-//
25  
-// Moodle is distributed in the hope that it will be useful,
26  
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
27  
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  
-// GNU General Public License for more details.
29  
-//
30  
-// You should have received a copy of the GNU General Public License
31  
-// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
32  
-
33  
-/**
34  
- * This file contains all necessary code to support basiclti services
35  
- * like outcomes and roster access.
36  
- *
37  
- * @package lti
38  
- * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
39  
- *  marc.alier@upc.edu
40  
- * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
41  
- *
42  
- * @author Marc Alier
43  
- * @author Jordi Piguillem
44  
- * @author Nikolas Galanis
45  
- * @author Charles Severance
46  
- *
47  
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48  
- */
49  
-
50  
-require_once("../../config.php");
51  
-require_once($CFG->dirroot.'/mod/lti/lib.php');
52  
-require_once($CFG->dirroot.'/mod/lti/locallib.php');
53  
-require_once($CFG->dirroot.'/mod/lti/OAuth.php');
54  
-require_once($CFG->dirroot.'/mod/lti/TrivialStore.php');
55  
-
56  
-error_reporting(E_ALL & ~E_NOTICE);
57  
-ini_set("display_errors", 1);
58  
-
59  
-$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
60  
-$PAGE->set_url('/mod/lti/service.php');
61  
-$PAGE->set_pagetype('admin-setting-' . $section);
62  
-$PAGE->set_pagelayout('admin');
63  
-$PAGE->navigation->clear_cache();
64  
-
65  
-function message_response($major, $severity, $minor=false, $message=false, $xml=false) {
66  
-    $lti_message_type = $_REQUEST['lti_message_type'];
67  
-    $retval = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n" .
68  
-        "<message_response>\n" .
69  
-        "  <lti_message_type>$lti_message_type</lti_message_type>\n" .
70  
-        "  <statusinfo>\n" .
71  
-        "     <codemajor>$major</codemajor>\n" .
72  
-        "     <severity>$severity</severity>\n";
73  
-    if (! $codeminor === false) {
74  
-            $retval = $retval .  "     <codeminor>$minor</codeminor>\n";
75  
-    }
76  
-    $retval = $retval .
77  
-        "     <description>$message</description>\n" .
78  
-        "  </statusinfo>\n";
79  
-    if (! $xml === false) {
80  
-            $retval = $retval . $xml;
81  
-    }
82  
-    $retval = $retval . "</message_response>\n";
83  
-    return $retval;
84  
-}
85  
-
86  
-function do_error($message) {
87  
-    print message_response('Fail', 'Error', false, $message);
88  
-    exit();
89  
-}
90  
-
91  
-$lti_version = $_REQUEST['lti_version'];
92  
-if ($lti_version != "LTI-1p0") {
93  
-    do_error("Improperly formed message: wrong lti version: ".$lti_version);
94  
-}
95  
-
96  
-$lti_message_type = $_REQUEST['lti_message_type'];
97  
-if (! isset($lti_message_type)) {
98  
-     do_error("Improperly formed message: no lti_message_type parameter");
99  
-}
100  
-
101  
-$message_type = false;
102  
-if ($lti_message_type == "basic-lis-replaceresult" ||
103  
-    $lti_message_type == "basic-lis-createresult" ||
104  
-    $lti_message_type == "basic-lis-updateresult" ||
105  
-    $lti_message_type == "basic-lis-deleteresult" ||
106  
-    $lti_message_type == "basic-lis-readresult") {
107  
-      $sourcedid = $_REQUEST['sourcedid'];
108  
-      $message_type = "basicoutcome";
109  
-} else if ($lti_message_type == "basic-lti-loadsetting" ||
110  
-    $lti_message_type == "basic-lti-savesetting" ||
111  
-    $lti_message_type == "basic-lti-deletesetting") {
112  
-      $sourcedid = $_REQUEST['id'];
113  
-      $message_type = "toolsetting";
114  
-} else if ($lti_message_type == "basic-lis-readmembershipsforcontext") {
115  
-      $sourcedid = $_REQUEST['id'];
116  
-      $message_type = "roster";
117  
-}
118  
-
119  
-if ($message_type == false) {
120  
-    do_error("Illegal lti_message_type");
121  
-}
122  
-
123  
-if (!isset($sourcedid)) {
124  
-    do_error("sourcedid missing");
125  
-}
126  
-// Truncate to maximum length
127  
-$sourcedid = substr($sourcedid, 0, 2048);
128  
-
129  
-try {
130  
-    $info = explode(':::', $sourcedid);
131  
-    if (! is_array($info)) {
132  
-        do_error("Bad sourcedid (1)");
133  
-    }
134  
-    $signature = $info[0];
135  
-    $userid = intval($info[1]);
136  
-    $placement = $info[2];
137  
-} catch (Exception $e) {
138  
-    do_error("Bad sourcedid (2)");
139  
-}
140  
-
141  
-if (isset($signature) && isset($userid) && isset($placement)) {
142  
-    // OK
143  
-} else {
144  
-    do_error("Bad sourcedid (3)");
145  
-}
146  
-
147  
-// Retrieve the Basic LTI placement
148  
-if (! $basiclti = $DB->get_record('lti', array('id'=>$placement))) {
149  
-    do_error("Bad sourcedid (4)");
150  
-}
151  
-
152  
-$basiclti_types_config = (object)$basiclti_types_config;
153  
-
154  
-$typeconfig = lti_get_type_config($basiclti->typeid);
155  
-
156  
-if (isset($typeconfig) && isset($typeconfig['password'])) {
157  
-    // OK
158  
-} else {
159  
-    do_error("Unable to load type");
160  
-}
161  
-
162  
-if ($message_type == "basicoutcome") {
163  
-    if ($typeconfig["acceptgrades"] == 1 ||
164  
-         ($typeconfig["acceptgrades"] == 2 && $basiclti->instructorchoiceacceptgrades == 1)) {
165  
-        // The placement is configured to accept grades
166  
-    } else {
167  
-        do_error("Not permitted (1)");
168  
-    }
169  
-} else if ($message_type == "toolsetting") {
170  
-    if ($typeconfig["allowsetting"] == 1 ||
171  
-         ($typeconfig["allowsetting"] == 2 && $basiclti->instructorchoiceallowsetting == 1)) {
172  
-        // OK
173  
-    } else {
174  
-        do_error("Not permitted (2)");
175  
-    }
176  
-} else if ($message_type == "roster") {
177  
-    if ($typeconfig["allowroster"] == 1 ||
178  
-         ($typeconfig["allowroster"] == 2 && $basiclti->instructorchoiceallowroster == 1)) {
179  
-        // OK
180  
-    } else {
181  
-        do_error("Not permitted (3)");
182  
-    }
183  
-}
184  
-
185  
-// Retrieve the secret we use to sign lis_result_sourcedid
186  
-$placementsecret = $basiclti->placementsecret;
187  
-$oldplacementsecret = $basiclti->oldplacementsecret;
188  
-if (! isset($placementsecret)) {
189  
-    do_error("Not permitted (4)");
190  
-}
191  
-
192  
-$suffix = ':::' . $userid . ':::' . $placement;
193  
-$plaintext = $placementsecret . $suffix;
194  
-$hashsig = hash('sha256', $plaintext, false);
195  
-if (($hashsig != $signature) && isset($oldplacementsecret) && (strlen($oldplacementsecret) > 1)) {
196  
-    $plaintext = $oldplacementsecret . $suffix;
197  
-    $hashsig = hash('sha256', $plaintext, false);
198  
-}
199  
-
200  
-if ($hashsig != $signature) {
201  
-    do_error("Invalid sourcedid");
202  
-}
203  
-
204  
-// Check the OAuth Signature
205  
-$oauth_secret = $typeconfig["password"];
206  
-$oauth_consumer_key = $typeconfig["resourcekey"];
207  
-if (! isset($oauth_secret)) {
208  
-    do_error("Not permitted (5)");
209  
-}
210  
-if (! isset($oauth_consumer_key)) {
211  
-    do_error("Not permitted (6)");
212  
-}
213  
-
214  
-// Verify the message signature
215  
-$store = new TrivialOAuthDataStore();
216  
-$store->add_consumer($oauth_consumer_key, $oauth_secret);
217  
-
218  
-$server = new OAuthServer($store);
219  
-
220  
-$method = new OAuthSignatureMethod_HMAC_SHA1();
221  
-$server->add_signature_method($method);
222  
-$request = OAuthRequest::from_request();
223  
-
224  
-$basestring = $request->get_signature_base_string();
225  
-try {
226  
-    $server->verify_request($request);
227  
-} catch (Exception $e) {
228  
-    do_error($e->getMessage());
229  
-}
230  
-
231  
-if (! $course = $DB->get_record('course', array('id'=>$basiclti->course))) {
232  
-    do_error("Could not retrieve course");
233  
-}
234  
-
235  
-// TODO: Check that user is in course
236  
-
237  
-if (! $cm = get_coursemodule_from_instance("lti", $basiclti->id, $course->id)) {
238  
-    do_error("Course Module ID was incorrect");
239  
-}
240  
-
241  
-// Lets store the grade
242  
-require_once($CFG->libdir.'/gradelib.php');
243  
-
244  
-// Beginning of actual grade processing
245  
-if ($message_type == "basicoutcome") {
246  
-    $source = 'mod/lti';
247  
-    $courseid = $course->id;
248  
-    $itemtype = 'mod';
249  
-    $itemmodule = 'lti';
250  
-    $iteminstance =  $basiclti->id;
251  
-
252  
-    if ($lti_message_type == "basic-lis-readresult") {
253  
-        unset($grade);
254  
-        $thegrade = grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $userid);
255  
-        // print_r($thegrade->items[0]->grades);
256  
-        if (isset($thegrade) && is_array($thegrade->items[0]->grades)) {
257  
-            foreach ($thegrade->items[0]->grades as $agrade) {
258  
-                $grade = $agrade->grade;
259  
-                break;
260  
-            }
261  
-        }
262  
-        if (! isset($grade)) {
263  
-            do_error("Unable to read grade");
264  
-        }
265  
-
266  
-        $result = "  <result>\n" .
267  
-            "     <resultscore>\n" .
268  
-            "        <textstring>" .
269  
-            htmlspecialchars($grade/100.0) .
270  
-            "</textstring>\n" .
271  
-            "     </resultscore>\n" .
272  
-            "  </result>\n";
273  
-        print message_response('Success', 'Status', false, "Grade read", $result);
274  
-        exit();
275  
-    }
276  
-
277  
-    if ($lti_message_type == "basic-lis-deleteresult") {
278  
-        $params = array();
279  
-        $params['itemname'] = $basiclti->name;
280  
-
281  
-        $grade = new stdClass();
282  
-        $grade->userid   = $userid;
283  
-        $grade->rawgrade = null;
284  
-
285  
-        grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, 0, $grade, array('deleted'=>1));
286  
-    } else {
287  
-        if (isset($_REQUEST['result_resultscore_textstring'])) {
288  
-            $gradeval = floatval($_REQUEST['result_resultscore_textstring']);
289  
-            if ($gradeval <= 1.0 && $gradeval >= 0.0) {
290  
-                $gradeval = $gradeval * 100.0;
291  
-            }
292  
-        } else {
293  
-            do_error('Missing Grade');
294  
-        }
295  
-        $params = array();
296  
-        $params['itemname'] = $basiclti->name;
297  
-
298  
-        $grade = new stdClass();
299  
-        $grade->userid   = $userid;
300  
-        $grade->rawgrade = $gradeval;
301  
-
302  
-        grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, 0, $grade, $params);
303  
-    }
304  
-
305  
-    print message_response('Success', 'Status', 'fullsuccess', 'Grade updated');
306  
-
307  
-} else if ($lti_message_type == "basic-lti-loadsetting") {
308  
-    $xml = "  <setting>\n" .
309  
-           "     <value>".htmlspecialchars($basiclti->setting)."</value>\n" .
310  
-           "  </setting>\n";
311  
-    print message_response('Success', 'Status', 'fullsuccess', 'Setting retrieved', $xml);
312  
-} else if ($lti_message_type == "basic-lti-savesetting") {
313  
-    $setting = $_REQUEST['setting'];
314  
-    if (! isset($setting)) {
315  
-        do_error('Missing setting value');
316  
-    }
317  
-    $record = $DB->get_record('lti', array('id'=>$basiclti->id));
318  
-    $record->setting = $setting;
319  
-    $success = $DB->update_record('lti', $record);
320  
-    if ($success) {
321  
-        print message_response('Success', 'Status', 'fullsuccess', 'Setting updated');
322  
-    } else {
323  
-        do_error("Error updating error");
324  
-    }
325  
-} else if ($lti_message_type == "basic-lti-deletesetting") {
326  
-    $record = $DB->get_record('lti', array('id'=>$basiclti->id));
327  
-    $record->setting = '';
328  
-    $success = $DB->update_record('lti', $record);
329  
-    if ($success) {
330  
-        print message_response('Success', 'Status', 'fullsuccess', 'Setting deleted');
331  
-    } else {
332  
-        do_error("Error updating error");
333  
-    }
334  
-} else if ($message_type == "roster") {
335  
-    if (! $course = $DB->get_record('course', array('id'=>$basiclti->course))) {
336  
-        do_error("Could not retrieve course");
337  
-    }
338  
-    if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
339  
-        do_error("Could not retrieve context");
340  
-    }
341  
-    $sql = 'SELECT u.id, u.username, u.firstname, u.lastname, u.email, ro.shortname
342  
-        FROM  '.$CFG->prefix.'role_assignments ra
343  
-        JOIN  '.$CFG->prefix.'user AS u ON ra.userid = u.id
344  
-        JOIN  '.$CFG->prefix.'role ro ON ra.roleid = ro.id
345  
-        WHERE ra.contextid = '.$context->id;
346  
-    $userlist = $DB->get_recordset_sql($sql);
347  
-    $xml = "  <memberships>\n";
348  
-    foreach ($userlist as $user) {
349  
-        $role = "Learner";
350  
-        if ($user->shortname == 'editingteacher' || $user->shortname == 'admin') {
351  
-            $role = 'Instructor';
352  
-        }
353  
-        $userxml = "    <member>\n".
354  
-                   "      <user_id>".htmlspecialchars($user->id)."</user_id>\n".
355  
-                   "      <roles>$role</roles>\n";
356  
-        if ($typeconfig["sendname"] == 1 ||
357  
-             ($typeconfig["sendname"] == 2 && $basiclti->instructorchoicesendname == 1)) {
358  
-            if (isset($user->firstname)) {
359  
-                $userxml .=  "      <person_name_given>".htmlspecialchars($user->firstname)."</person_name_given>\n";
360  
-            }
361  
-            if (isset($user->lastname)) {
362  
-                $userxml .=  "      <person_name_family>".htmlspecialchars($user->lastname)."</person_name_family>\n";
363  
-            }
364  
-        }
365  
-        if ($typeconfig["sendemailaddr"] == 1 ||
366  
-             ($typeconfig["sendemailaddr"] == 2 && $basiclti->instructorchoicesendemailaddr == 1)) {
367  
-            if (isset($user->email)) {
368  
-                $userxml .=  "      <person_contact_email_primary>".htmlspecialchars($user->email)."</person_contact_email_primary>\n";
369  
-            }
370  
-        }
371  
-        $placementsecret = $basiclti->placementsecret;
372  
-        if (isset($placementsecret)) {
373  
-            $suffix = ':::' . $user->id . ':::' . $basiclti->id;
374  
-            $plaintext = $placementsecret . $suffix;
375  
-            $hashsig = hash('sha256', $plaintext, false);
376  
-            $sourcedid = $hashsig . $suffix;
377  
-        }
378  
-        if ($typeconfig["acceptgrades"] == 1 ||
379  
-             ($typeconfig["acceptgrades"] == 2 && $basiclti->instructorchoiceacceptgrades == 1)) {
380  
-            if (isset($sourcedid)) {
381  
-                $userxml .=  "      <lis_result_sourcedid>".htmlspecialchars($sourcedid)."</lis_result_sourcedid>\n";
382  
-            }
383  
-        }
384  
-        $userxml .= "    </member>\n";
385  
-        $xml .= $userxml;
386  
-    }
387  
-    $xml .= "  </memberships>\n";
388  
-    print message_response('Success', 'Status', 'fullsuccess', 'Roster retreived', $xml);
389  
-
390  
-}
391  
-

0 notes on commit 5e078d6

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