3 /// Library of functions and constants for module lams
4 require_once($CFG->dirroot
.'/lib/datalib.php');
5 require_once($CFG->dirroot
.'/lib/moodlelib.php');
6 require_once($CFG->dirroot
.'/lib/soap/nusoap.php');
9 function lams_add_instance($lams) {
10 /// Given an object containing all the necessary data,
11 /// (defined by the form in mod.html) this function
12 /// will create a new instance and return the id number
13 /// of the new instance.
15 $lams->timemodified
= time();
16 $lams->learning_session_id
= lams_get_lesson($USER->username
,$lams->sequence
,$lams->course
,$lams->name
,$lams->introduction
,"normal");
17 return insert_record("lams", $lams);
21 function lams_update_instance($lams) {
22 /// Given an object containing all the necessary data,
23 /// (defined by the form in mod.html) this function
24 /// will update an existing instance with new data.
25 //echo "enter lams_update_instance<br/>";
26 $lams->timemodified
= time();
27 $lams->id
= $lams->instance
;
28 lams_delete_lesson($USER->username
,$lams->learning_session_id
);
29 $lams->learning_session_id
= lams_get_lesson($USER->username
,$lams->sequence
,$lams->course
,$lams->name
,$lams->introduction
,"normal");
30 if(!$lams->learning_session_id
){
33 # May have to add extra stuff in here #
34 //echo $lams->id."<br/>";
35 //echo $lams->sequence."<br/>";
36 //echo $lams->course."<br/>";
37 //echo $lams->name."<br/>";
38 //echo $lams->introduction."<br/>";
39 //echo $lams->learning_session_id."<br/>";
40 //echo "exit lams_update_instance<br/>";
41 return update_record("lams", $lams);
45 function lams_delete_instance($id) {
46 /// Given an ID of an instance of this module,
47 /// this function will permanently delete the instance
48 /// and any data that depends on it.
50 if (! $lams = get_record("lams", "id", "$id")) {
56 # Delete any dependent records here #
57 lams_delete_lesson($USER->username
,$lams->learning_session_id
);
58 if (! delete_records("lams", "id", "$lams->id")) {
65 function lams_user_outline($course, $user, $mod, $lams) {
66 /// Return a small object with summary information about what a
67 /// user has done with a given particular instance of this module
68 /// Used for user activity reports.
69 /// $return->time = the time they did it
70 /// $return->info = a short text description
75 function lams_user_complete($course, $user, $mod, $lams) {
76 /// Print a detailed representation of what a user has done with
77 /// a given particular instance of this module, for user activity reports.
82 function lams_print_recent_activity($course, $isteacher, $timestart) {
83 /// Given a course and a time, this module should find recent activity
84 /// that has occurred in lams activities and print it out.
85 /// Return true if there was output, or false is there was none.
89 return false; // True if anything was printed, otherwise false
92 function lams_cron () {
93 /// Function to be run periodically according to the moodle cron
94 /// This function searches for things that need to be done, such
95 /// as sending out mail, toggling flags etc ...
102 function lams_grades($lamsid) {
103 /// Must return an array of grades for a given instance of this module,
104 /// indexed by user. It also returns a maximum allowed grade.
106 /// $return->grades = array of grades;
107 /// $return->maxgrade = maximum allowed grade;
114 function lams_get_participants($lamsid) {
115 //Must return an array of user records (all data) who are participants
116 //for a given instance of lams. Must include every user involved
117 //in the instance, independient of his role (student, teacher, admin...)
118 //See other modules as example.
123 function lams_scale_used ($lamsid,$scaleid) {
124 //This function returns if a scale is being used by one lams
125 //it it has support for grading and scales. Commented code should be
126 //modified if necessary. See forum, glossary or journal modules
131 //$rec = get_record("lams","id","$lamsid","scale","-$scaleid");
133 //if (!empty($rec) && !empty($scaleid)) {
140 //////////////////////////////////////////////////////////////////////////////////////
141 /// Any other lams functions go here. Each of them must have a name that
142 /// starts with lams_
144 function lams_get_soap_client($relativeurl) {
146 if(!isset($CFG->lams_serverurl
))
150 $wsdl = $CFG->lams_serverurl
.$relativeurl;
151 $s = new soap_client($wsdl,true,false,false,false,false,2,3);
156 * Get sequences(learning designs) for the user in LAMS
158 * @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.
159 * @return Array sequence array
160 * @TODO complete the documentation of this function
162 function lams_get_sequences($username,$courseid) {
164 if(!isset($CFG->lams_serverid
)||
!isset($CFG->lams_serverkey
)||
!isset($CFG->lams_serverurl
))
166 return get_string("notsetup", "lams");
168 $relativeurl="/services/LearningDesignService?wsdl";
169 $s = lams_get_soap_client($relativeurl);
173 $datetime = date("F d,Y g:i a");
175 $login = lams_get_user($username,$courseid);
180 if(!isset($username)){
181 $username = $USER->username
;
183 $rawstring = trim($datetime).trim($username).trim($CFG->lams_serverid
).trim($CFG->lams_serverkey
);
184 $hashvalue = sha1(strtolower($rawstring));
185 $parameters = array($CFG->lams_serverid
,$datetime,$hashvalue,$username);
186 $result = $s->call('getAllLearningDesigns',$parameters);//Array of simpleLearningDesign objects
187 if($s->getError()){//if some exception happened
188 $result = $s->getError();//return the string describing the error
195 * Get learning session(lesson) id from LAMS
197 * @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
198 * @param int $ldid The id of the learning design that the lesson is based on
199 * @param int $courseid The id of the course that the lesson is associated with.
200 * @param string $title The title of the lesson
201 * @param string $desc The description of the lesson
202 * @param string $type The type of the lesson. Two types: normal, preview
203 * @return int lesson id
205 function lams_get_lesson($username,$ldid,$courseid,$title,$desc,$type) {
206 //echo "enter lams_get_lesson<br/>";
208 if(!isset($CFG->lams_serverid
)||
!isset($CFG->lams_serverkey
))
210 //echo "serverid or serverkey is not set<br/>";
213 $relativeurl="/services/LearningSessionService?wsdl";
214 $s = lams_get_soap_client($relativeurl);
216 //echo "soap client is null<br/>";
219 $datetime = date("F d,Y g:i a");
220 if(!isset($username)){
221 $username = $USER->username
;
223 $plaintext = $datetime.$username.$CFG->lams_serverid
.$CFG->lams_serverkey
;
225 $hashvalue = sha1(strtolower($plaintext));
227 $parameters = array($CFG->lams_serverid
,$datetime,$hashvalue,$username,$ldid,$courseid,$title,$desc,$type);
228 $result = $s->call('createLearningSession',$parameters);
229 //echo "result:".$result."<br/>";
230 //echo "exit lams_get_lesson<br/>";
232 $result = $s->getError();
239 * Delete learning session(lesson) from LAMS
241 * @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
242 * @param int $lsid The id of the learning session(lesson)
243 * @return true or false
245 function lams_delete_lesson($username,$lsid) {
246 //echo "enter lams_get_lesson<br/>";
248 if(!isset($CFG->lams_serverid
)||
!isset($CFG->lams_serverkey
))
250 return "The LAMS serverId and serverKey have not been set up";
252 $relativeurl="/services/LearningSessionService?wsdl";
253 $s = lams_get_soap_client($relativeurl);
255 return "Failed to get soap client based on:".$relativeurl;
257 $datetime = date("F d,Y g:i a");
258 if(!isset($username)){
259 $username = $USER->username
;
261 $plaintext = $datetime.$username.$CFG->lams_serverid
.$CFG->lams_serverkey
;
263 $hashvalue = sha1(strtolower($plaintext));
265 $parameters = array($CFG->lams_serverid
,$datetime,$hashvalue,$username,$lsid);
266 $result = $s->call('deleteLearningSession',$parameters);
268 $result = $s->getError();
277 * @param int courseid
278 * @return int class id
279 * @TODO complete the documentation of this function
282 function lams_get_class($courseid) {
284 //echo "enter lams_get_class"."<br/>";
285 $orgId = lams_get_organisation();
289 $lams_course = get_record("lams_course","course", $courseid);
290 if(empty($lams_course)){//LAMS class hasn't been created
292 $relativeurl="/services/UserManagementService?wsdl";
293 $s = lams_get_soap_client($relativeurl);
297 $datetime = date("F d,Y g:i a");
298 $rawstring = $datetime.$CFG->lams_serverid.$CFG->lams_serverkey;
299 $hashvalue = sha1(strtolower($rawstring));
300 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue);
301 $result = $s->call('createClass',$parameters);
302 //echo "<xmp/>".$s->request."</xmp>";
303 //echo "<xmp/>".$s->response."</xmp>";
304 //echo "result:".$result."<br/>";
305 $lams_course->course = $courseid;
306 $lams_course->classid = $result;
307 insert_record("lams_course",$lams_course);
308 //echo "exit lams_get_class"."<br/>";
311 //echo "exit lams_get_class"."<br/>";
312 return $lams_course->classid;
317 * Get organisation in LAMS
319 * @return int organisation id
320 * @TODO complete the documentation of this function
323 function lams_get_organisation() {
325 //echo "enter lams_get_organisaiton"."<br/>";
326 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
330 if(!isset($CFG->lams_orgid)){
331 $relativeurl="/services/UserManagementService?wsdl";
332 $s = lams_get_soap_client($relativeurl);
336 $datetime = date("F d,Y g:i a");
337 $rawstring = $datetime.$CFG->lams_serverid.$CFG->lams_serverkey;
338 $hashvalue = sha1(strtolower($rawstring));
339 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue);
340 $result = $s->call('createOrganisation',$parameters);
341 //echo "<xmp/>".$s->request."</xmp>";
342 //echo "<xmp/>".$s->response."</xmp>";
343 set_config("lams_orgid",$result);
344 //echo "result:".$result."<br/>";
345 //echo "exit lams_get_organisaiton"."<br/>";
348 //echo "exit lams_get_organisaiton"."<br/>";
349 return $CFG->lams_orgid;
357 * @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
358 * @param string $roles The user's roles in LAMS
359 * @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()
360 * @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()
361 * @return user login in LAMS if the user is successfully created
362 * @TODO complete the documentation of this function
365 function lams_get_user($username,$courseid) {
367 //echo "enter lams_get_user"."<br/>";
368 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
372 $lams_user = get_record("lams_user","username",$username);
373 if(empty($lams_user)){//LAMS user hasn't been created
374 $classid = lams_get_class($courseid);
375 if(empty($classid)){//Can't get class id from lams_course table. Something wrong!
378 $orgid = lams_get_organisation();//It won't be NULL. See lams_get_class function
379 $user = get_record("user","username",$username);
380 if(empty($user)){//Something wrong
383 $roles = lams_get_user_roles($user->id,$courseid);
384 $relativeurl="/services/UserManagementService?wsdl";
385 $s = lams_get_soap_client($relativeurl);
389 $datetime = date("F d,Y g:i a");
391 $rawstring = $datetime.$login.$CFG->lams_serverid.$CFG->lams_serverkey;
392 $hashvalue = sha1(strtolower($rawstring));
393 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$login,"password",$roles,$classid,$orgid);
394 $result = $s->call('createUser',$parameters);
395 //echo "<xmp/>".$s->request."</xmp>";
396 //echo "<xmp/>".$s->response."</xmp>";
397 $lams_user->username = $username;
398 $lams_user->login = $result;
399 insert_record("lams_user",$lams_user);
400 //echo "result:".$result."<br/>";
401 //echo "exit lams_get_user"."<br/>";
404 //echo "exit lams_get_user"."<br/>";
405 return $lams_user->login;
411 * Mapping moodle roles to LAMS roles
413 * @param int $courseid The id of the course that is being viewed
414 * @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.
415 * @return formatted string describing LAMS roles
416 * @TODO fill the gap of roles mapping between moodle and LAMS
419 function lams_get_user_roles($userid=0, $courseid){
421 if(isadmin($userid)){
422 $roles = "administrator"."|"."auhtor"."|"."staff";
423 }else if(isteacheredit($courseid,$userid)){
424 $roles = "auhtor"."|"."staff";
425 }else if(isteacher($courseid,$userid)){
428 if(isstudent($courseid,$userid)){
432 $roles .= "|"."learner";
435 //echo $roles."<br/>";