3 /// Library of functions and constants for module lams
5 if (!defined('MOODLE_INTERNAL')) {
6 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
9 require_once($CFG->dirroot
.'/lib/soaplib.php');
12 function lams_add_instance($lams) {
13 /// Given an object containing all the necessary data,
14 /// (defined by the form in mod.html) this function
15 /// will create a new instance and return the id number
16 /// of the new instance.
18 $lams->timemodified
= time();
19 $lams->learning_session_id
= lams_get_lesson($USER->username
,$lams->sequence
,$lams->course
,$lams->name
,$lams->introduction
,"normal");
20 return insert_record("lams", $lams);
24 function lams_update_instance($lams) {
25 /// Given an object containing all the necessary data,
26 /// (defined by the form in mod.html) this function
27 /// will update an existing instance with new data.
28 //echo "enter lams_update_instance<br/>";
29 $lams->timemodified
= time();
30 $lams->id
= $lams->instance
;
31 lams_delete_lesson($USER->username
,$lams->learning_session_id
);
32 $lams->learning_session_id
= lams_get_lesson($USER->username
,$lams->sequence
,$lams->course
,$lams->name
,$lams->introduction
,"normal");
33 if(!$lams->learning_session_id
){
36 # May have to add extra stuff in here #
37 //echo $lams->id."<br/>";
38 //echo $lams->sequence."<br/>";
39 //echo $lams->course."<br/>";
40 //echo $lams->name."<br/>";
41 //echo $lams->introduction."<br/>";
42 //echo $lams->learning_session_id."<br/>";
43 //echo "exit lams_update_instance<br/>";
44 return update_record("lams", $lams);
48 function lams_delete_instance($id) {
49 /// Given an ID of an instance of this module,
50 /// this function will permanently delete the instance
51 /// and any data that depends on it.
53 if (! $lams = get_record("lams", "id", "$id")) {
59 # Delete any dependent records here #
60 lams_delete_lesson($USER->username
,$lams->learning_session_id
);
61 if (! delete_records("lams", "id", "$lams->id")) {
68 function lams_user_outline($course, $user, $mod, $lams) {
69 /// Return a small object with summary information about what a
70 /// user has done with a given particular instance of this module
71 /// Used for user activity reports.
72 /// $return->time = the time they did it
73 /// $return->info = a short text description
78 function lams_user_complete($course, $user, $mod, $lams) {
79 /// Print a detailed representation of what a user has done with
80 /// a given particular instance of this module, for user activity reports.
85 function lams_cron () {
86 /// Function to be run periodically according to the moodle cron
87 /// This function searches for things that need to be done, such
88 /// as sending out mail, toggling flags etc ...
95 function lams_grades($lamsid) {
96 /// Must return an array of grades for a given instance of this module,
97 /// indexed by user. It also returns a maximum allowed grade.
99 /// $return->grades = array of grades;
100 /// $return->maxgrade = maximum allowed grade;
107 function lams_get_participants($lamsid) {
108 //Must return an array of user records (all data) who are participants
109 //for a given instance of lams. Must include every user involved
110 //in the instance, independient of his role (student, teacher, admin...)
111 //See other modules as example.
116 function lams_scale_used ($lamsid,$scaleid) {
117 //This function returns if a scale is being used by one lams
118 //it it has support for grading and scales. Commented code should be
119 //modified if necessary. See forum, glossary or journal modules
124 //$rec = get_record("lams","id","$lamsid","scale","-$scaleid");
126 //if (!empty($rec) && !empty($scaleid)) {
134 * Checks if scale is being used by any instance of lams
136 * This is used to find out if scale used anywhere
137 * @param $scaleid int
138 * @return boolean True if the scale is used by any lams
140 function lams_scale_used_anywhere($scaleid) {
144 //////////////////////////////////////////////////////////////////////////////////////
145 /// Any other lams functions go here. Each of them must have a name that
146 /// starts with lams_
148 function lams_get_soap_client($relativeurl) {
150 if(!isset($CFG->lams_serverurl
))
154 $wsdl = $CFG->lams_serverurl
.$relativeurl;
155 $s = new soap_client($wsdl,true,false,false,false,false,2,3);
160 * Get sequences(learning designs) for the user in LAMS
162 * @param string $username The username of the user. Set this to "" if you would just like to get sequences for the currently logged in user.
163 * @return Array sequence array
164 * @TODO complete the documentation of this function
166 function lams_get_sequences($username,$courseid) {
168 if(!isset($CFG->lams_serverid
)||
!isset($CFG->lams_serverkey
)||
!isset($CFG->lams_serverurl
))
170 return get_string("notsetup", "lams");
172 $relativeurl="/services/LearningDesignService?wsdl";
173 $s = lams_get_soap_client($relativeurl);
177 $datetime = date("F d,Y g:i a");
179 $login = lams_get_user($username,$courseid);
184 if(!isset($username)){
185 $username = $USER->username
;
187 $rawstring = trim($datetime).trim($username).trim($CFG->lams_serverid
).trim($CFG->lams_serverkey
);
188 $hashvalue = sha1(strtolower($rawstring));
189 $parameters = array($CFG->lams_serverid
,$datetime,$hashvalue,$username);
190 $result = $s->call('getAllLearningDesigns',$parameters);//Array of simpleLearningDesign objects
191 if($s->getError()){//if some exception happened
192 $result = $s->getError();//return the string describing the error
199 * Get learning session(lesson) id from LAMS
201 * @param string $username The username of the user. Set this to "" if you would just like the currently logged in user to create the lesson
202 * @param int $ldid The id of the learning design that the lesson is based on
203 * @param int $courseid The id of the course that the lesson is associated with.
204 * @param string $title The title of the lesson
205 * @param string $desc The description of the lesson
206 * @param string $type The type of the lesson. Two types: normal, preview
207 * @return int lesson id
209 function lams_get_lesson($username,$ldid,$courseid,$title,$desc,$type) {
210 //echo "enter lams_get_lesson<br/>";
212 if(!isset($CFG->lams_serverid
)||
!isset($CFG->lams_serverkey
))
214 //echo "serverid or serverkey is not set<br/>";
217 $relativeurl="/services/LearningSessionService?wsdl";
218 $s = lams_get_soap_client($relativeurl);
220 //echo "soap client is null<br/>";
223 $datetime = date("F d,Y g:i a");
224 if(!isset($username)){
225 $username = $USER->username
;
227 $plaintext = $datetime.$username.$CFG->lams_serverid
.$CFG->lams_serverkey
;
229 $hashvalue = sha1(strtolower($plaintext));
231 $parameters = array($CFG->lams_serverid
,$datetime,$hashvalue,$username,$ldid,$courseid,$title,$desc,$type);
232 $result = $s->call('createLearningSession',$parameters);
233 //echo "result:".$result."<br/>";
234 //echo "exit lams_get_lesson<br/>";
236 $result = $s->getError();
243 * Delete learning session(lesson) from LAMS
245 * @param string $username The username of the user. Set this to "" if you would just like the currently logged in user to create the lesson
246 * @param int $lsid The id of the learning session(lesson)
247 * @return true or false
249 function lams_delete_lesson($username,$lsid) {
250 //echo "enter lams_get_lesson<br/>";
252 if(!isset($CFG->lams_serverid
)||
!isset($CFG->lams_serverkey
))
254 return "The LAMS serverId and serverKey have not been set up";
256 $relativeurl="/services/LearningSessionService?wsdl";
257 $s = lams_get_soap_client($relativeurl);
259 return "Failed to get soap client based on:".$relativeurl;
261 $datetime = date("F d,Y g:i a");
262 if(!isset($username)){
263 $username = $USER->username
;
265 $plaintext = $datetime.$username.$CFG->lams_serverid
.$CFG->lams_serverkey
;
267 $hashvalue = sha1(strtolower($plaintext));
269 $parameters = array($CFG->lams_serverid
,$datetime,$hashvalue,$username,$lsid);
270 $result = $s->call('deleteLearningSession',$parameters);
272 $result = $s->getError();
281 * @param int courseid
282 * @return int class id
283 * @TODO complete the documentation of this function
286 function lams_get_class($courseid) {
288 //echo "enter lams_get_class"."<br/>";
289 $orgId = lams_get_organisation();
293 $lams_course = get_record("lams_course","course", $courseid);
294 if(empty($lams_course)){//LAMS class hasn't been created
296 $relativeurl="/services/UserManagementService?wsdl";
297 $s = lams_get_soap_client($relativeurl);
301 $datetime = date("F d,Y g:i a");
302 $rawstring = $datetime.$CFG->lams_serverid.$CFG->lams_serverkey;
303 $hashvalue = sha1(strtolower($rawstring));
304 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue);
305 $result = $s->call('createClass',$parameters);
306 //echo "<xmp/>".$s->request."</xmp>";
307 //echo "<xmp/>".$s->response."</xmp>";
308 //echo "result:".$result."<br/>";
309 $lams_course->course = $courseid;
310 $lams_course->classid = $result;
311 insert_record("lams_course",$lams_course);
312 //echo "exit lams_get_class"."<br/>";
315 //echo "exit lams_get_class"."<br/>";
316 return $lams_course->classid;
321 * Get organisation in LAMS
323 * @return int organisation id
324 * @TODO complete the documentation of this function
327 function lams_get_organisation() {
329 //echo "enter lams_get_organisaiton"."<br/>";
330 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
334 if(!isset($CFG->lams_orgid)){
335 $relativeurl="/services/UserManagementService?wsdl";
336 $s = lams_get_soap_client($relativeurl);
340 $datetime = date("F d,Y g:i a");
341 $rawstring = $datetime.$CFG->lams_serverid.$CFG->lams_serverkey;
342 $hashvalue = sha1(strtolower($rawstring));
343 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue);
344 $result = $s->call('createOrganisation',$parameters);
345 //echo "<xmp/>".$s->request."</xmp>";
346 //echo "<xmp/>".$s->response."</xmp>";
347 set_config("lams_orgid",$result);
348 //echo "result:".$result."<br/>";
349 //echo "exit lams_get_organisaiton"."<br/>";
352 //echo "exit lams_get_organisaiton"."<br/>";
353 return $CFG->lams_orgid;
361 * @param string $username The username of the user. Set this to "" if you would just like to create LAMS user for the currently logged in user
362 * @param string $roles The user's roles in LAMS
363 * @param int $classid The id of the class that the user belongs to. The class should be already created in LAMS by calling lams_create_class()
364 * @param int $orgid The id of the organisation that the user belongs to. The organisation should be already created in LAMS by calling lams_create_organisation()
365 * @return user login in LAMS if the user is successfully created
366 * @TODO complete the documentation of this function
369 function lams_get_user($username,$courseid) {
371 //echo "enter lams_get_user"."<br/>";
372 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
376 $lams_user = get_record("lams_user","username",$username);
377 if(empty($lams_user)){//LAMS user hasn't been created
378 $classid = lams_get_class($courseid);
379 if(empty($classid)){//Can't get class id from lams_course table. Something wrong!
382 $orgid = lams_get_organisation();//It won't be NULL. See lams_get_class function
383 $user = get_record("user","username",$username);
384 if(empty($user)){//Something wrong
387 $roles = lams_get_user_roles($user->id,$courseid);
388 $relativeurl="/services/UserManagementService?wsdl";
389 $s = lams_get_soap_client($relativeurl);
393 $datetime = date("F d,Y g:i a");
395 $rawstring = $datetime.$login.$CFG->lams_serverid.$CFG->lams_serverkey;
396 $hashvalue = sha1(strtolower($rawstring));
397 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$login,"password",$roles,$classid,$orgid);
398 $result = $s->call('createUser',$parameters);
399 //echo "<xmp/>".$s->request."</xmp>";
400 //echo "<xmp/>".$s->response."</xmp>";
401 $lams_user->username = $username;
402 $lams_user->login = $result;
403 insert_record("lams_user",$lams_user);
404 //echo "result:".$result."<br/>";
405 //echo "exit lams_get_user"."<br/>";
408 //echo "exit lams_get_user"."<br/>";
409 return $lams_user->login;
415 * Mapping moodle roles to LAMS roles
417 * @param int $courseid The id of the course that is being viewed
418 * @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
419 * @return formatted string describing LAMS roles
420 * @TODO fill the gap of roles mapping between moodle and LAMS
423 function lams_get_user_roles($userid=0, $courseid){
425 if(isadmin($userid)){
426 $roles = "administrator"."|"."auhtor"."|"."staff";
427 }else if(isteacheredit($courseid,$userid)){
428 $roles = "auhtor"."|"."staff";
429 }else if(isteacher($courseid,$userid)){
432 if(isstudent($courseid,$userid)){
436 $roles .= "|"."learner";
439 //echo $roles."<br/>";