Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / lams / lib.php
blobf96f32877a977897c219b2af8736c1162bdf710e
1 <?PHP // $Id$
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.
17 global $USER;
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){
34 return false;
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")) {
54 return false;
57 $result = true;
59 # Delete any dependent records here #
60 lams_delete_lesson($USER->username,$lams->learning_session_id);
61 if (! delete_records("lams", "id", "$lams->id")) {
62 $result = false;
65 return $result;
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
75 return $return;
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.
82 return true;
85 function lams_print_recent_activity($course, $isteacher, $timestart) {
86 /// Given a course and a time, this module should find recent activity
87 /// that has occurred in lams activities and print it out.
88 /// Return true if there was output, or false is there was none.
90 global $CFG;
92 return false; // True if anything was printed, otherwise false
95 function lams_cron () {
96 /// Function to be run periodically according to the moodle cron
97 /// This function searches for things that need to be done, such
98 /// as sending out mail, toggling flags etc ...
100 global $CFG;
102 return true;
105 function lams_grades($lamsid) {
106 /// Must return an array of grades for a given instance of this module,
107 /// indexed by user. It also returns a maximum allowed grade.
109 /// $return->grades = array of grades;
110 /// $return->maxgrade = maximum allowed grade;
112 /// return $return;
114 return NULL;
117 function lams_get_participants($lamsid) {
118 //Must return an array of user records (all data) who are participants
119 //for a given instance of lams. Must include every user involved
120 //in the instance, independient of his role (student, teacher, admin...)
121 //See other modules as example.
123 return false;
126 function lams_scale_used ($lamsid,$scaleid) {
127 //This function returns if a scale is being used by one lams
128 //it it has support for grading and scales. Commented code should be
129 //modified if necessary. See forum, glossary or journal modules
130 //as reference.
132 $return = false;
134 //$rec = get_record("lams","id","$lamsid","scale","-$scaleid");
136 //if (!empty($rec) && !empty($scaleid)) {
137 // $return = true;
140 return $return;
143 //////////////////////////////////////////////////////////////////////////////////////
144 /// Any other lams functions go here. Each of them must have a name that
145 /// starts with lams_
147 function lams_get_soap_client($relativeurl) {
148 global $CFG;
149 if(!isset($CFG->lams_serverurl))
151 return NULL;
153 $wsdl = $CFG->lams_serverurl.$relativeurl;
154 $s = new soap_client($wsdl,true,false,false,false,false,2,3);
155 return $s;
159 * Get sequences(learning designs) for the user in LAMS
161 * @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.
162 * @return Array sequence array
163 * @TODO complete the documentation of this function
165 function lams_get_sequences($username,$courseid) {
166 global $CFG,$USER;
167 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey)||!isset($CFG->lams_serverurl))
169 return get_string("notsetup", "lams");
171 $relativeurl="/services/LearningDesignService?wsdl";
172 $s = lams_get_soap_client($relativeurl);
173 if(is_null($s)){
174 return NULL;
176 $datetime = date("F d,Y g:i a");
178 $login = lams_get_user($username,$courseid);
179 if(empty($login)){
180 return NULL;
183 if(!isset($username)){
184 $username = $USER->username;
186 $rawstring = trim($datetime).trim($username).trim($CFG->lams_serverid).trim($CFG->lams_serverkey);
187 $hashvalue = sha1(strtolower($rawstring));
188 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$username);
189 $result = $s->call('getAllLearningDesigns',$parameters);//Array of simpleLearningDesign objects
190 if($s->getError()){//if some exception happened
191 $result = $s->getError();//return the string describing the error
193 unset($s);
194 return $result;
198 * Get learning session(lesson) id from LAMS
200 * @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
201 * @param int $ldid The id of the learning design that the lesson is based on
202 * @param int $courseid The id of the course that the lesson is associated with.
203 * @param string $title The title of the lesson
204 * @param string $desc The description of the lesson
205 * @param string $type The type of the lesson. Two types: normal, preview
206 * @return int lesson id
208 function lams_get_lesson($username,$ldid,$courseid,$title,$desc,$type) {
209 //echo "enter lams_get_lesson<br/>";
210 global $CFG,$USER;
211 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
213 //echo "serverid or serverkey is not set<br/>";
214 return NULL;
216 $relativeurl="/services/LearningSessionService?wsdl";
217 $s = lams_get_soap_client($relativeurl);
218 if(is_null($s)){
219 //echo "soap client is null<br/>";
220 return NULL;
222 $datetime = date("F d,Y g:i a");
223 if(!isset($username)){
224 $username = $USER->username;
226 $plaintext = $datetime.$username.$CFG->lams_serverid.$CFG->lams_serverkey;
227 //echo $plaintext;
228 $hashvalue = sha1(strtolower($plaintext));
229 //echo $hashvalue;
230 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$username,$ldid,$courseid,$title,$desc,$type);
231 $result = $s->call('createLearningSession',$parameters);
232 //echo "result:".$result."<br/>";
233 //echo "exit lams_get_lesson<br/>";
234 if($s->getError()){
235 $result = $s->getError();
237 unset($s);
238 return $result;
242 * Delete learning session(lesson) from LAMS
244 * @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
245 * @param int $lsid The id of the learning session(lesson)
246 * @return true or false
248 function lams_delete_lesson($username,$lsid) {
249 //echo "enter lams_get_lesson<br/>";
250 global $CFG,$USER;
251 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
253 return "The LAMS serverId and serverKey have not been set up";
255 $relativeurl="/services/LearningSessionService?wsdl";
256 $s = lams_get_soap_client($relativeurl);
257 if(is_null($s)){
258 return "Failed to get soap client based on:".$relativeurl;
260 $datetime = date("F d,Y g:i a");
261 if(!isset($username)){
262 $username = $USER->username;
264 $plaintext = $datetime.$username.$CFG->lams_serverid.$CFG->lams_serverkey;
265 //echo $plaintext;
266 $hashvalue = sha1(strtolower($plaintext));
267 //echo $hashvalue;
268 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$username,$lsid);
269 $result = $s->call('deleteLearningSession',$parameters);
270 if($s->getError()){
271 $result = $s->getError();
273 unset($s);
274 return $result;
279 * Get class in LAMS
280 * @param int courseid
281 * @return int class id
282 * @TODO complete the documentation of this function
285 function lams_get_class($courseid) {
286 global $CFG,$USER;
287 //echo "enter lams_get_class"."<br/>";
288 $orgId = lams_get_organisation();
289 if(empty($orgId)){
290 return NULL;
292 $lams_course = get_record("lams_course","course", $courseid);
293 if(empty($lams_course)){//LAMS class hasn't been created
294 //create LAMS class
295 $relativeurl="/services/UserManagementService?wsdl";
296 $s = lams_get_soap_client($relativeurl);
297 if(is_null($s)){
298 return NULL;
300 $datetime = date("F d,Y g:i a");
301 $rawstring = $datetime.$CFG->lams_serverid.$CFG->lams_serverkey;
302 $hashvalue = sha1(strtolower($rawstring));
303 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue);
304 $result = $s->call('createClass',$parameters);
305 //echo "<xmp/>".$s->request."</xmp>";
306 //echo "<xmp/>".$s->response."</xmp>";
307 //echo "result:".$result."<br/>";
308 $lams_course->course = $courseid;
309 $lams_course->classid = $result;
310 insert_record("lams_course",$lams_course);
311 //echo "exit lams_get_class"."<br/>";
312 return $result;
313 }else{
314 //echo "exit lams_get_class"."<br/>";
315 return $lams_course->classid;
320 * Get organisation in LAMS
322 * @return int organisation id
323 * @TODO complete the documentation of this function
326 function lams_get_organisation() {
327 global $CFG,$USER;
328 //echo "enter lams_get_organisaiton"."<br/>";
329 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
331 return NULL;
333 if(!isset($CFG->lams_orgid)){
334 $relativeurl="/services/UserManagementService?wsdl";
335 $s = lams_get_soap_client($relativeurl);
336 if(empty($s)){
337 return NULL;
339 $datetime = date("F d,Y g:i a");
340 $rawstring = $datetime.$CFG->lams_serverid.$CFG->lams_serverkey;
341 $hashvalue = sha1(strtolower($rawstring));
342 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue);
343 $result = $s->call('createOrganisation',$parameters);
344 //echo "<xmp/>".$s->request."</xmp>";
345 //echo "<xmp/>".$s->response."</xmp>";
346 set_config("lams_orgid",$result);
347 //echo "result:".$result."<br/>";
348 //echo "exit lams_get_organisaiton"."<br/>";
349 return $result;
350 }else{
351 //echo "exit lams_get_organisaiton"."<br/>";
352 return $CFG->lams_orgid;
358 * Get user in LAMS
360 * @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
361 * @param string $roles The user's roles in LAMS
362 * @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()
363 * @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()
364 * @return user login in LAMS if the user is successfully created
365 * @TODO complete the documentation of this function
368 function lams_get_user($username,$courseid) {
369 global $CFG,$USER;
370 //echo "enter lams_get_user"."<br/>";
371 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey))
373 return NULL;
375 $lams_user = get_record("lams_user","username",$username);
376 if(empty($lams_user)){//LAMS user hasn't been created
377 $classid = lams_get_class($courseid);
378 if(empty($classid)){//Can't get class id from lams_course table. Something wrong!
379 return NULL;
381 $orgid = lams_get_organisation();//It won't be NULL. See lams_get_class function
382 $user = get_record("user","username",$username);
383 if(empty($user)){//Something wrong
384 return NULL;
386 $roles = lams_get_user_roles($user->id,$courseid);
387 $relativeurl="/services/UserManagementService?wsdl";
388 $s = lams_get_soap_client($relativeurl);
389 if(empty($s)){
390 return NULL;
392 $datetime = date("F d,Y g:i a");
393 $login = $username;
394 $rawstring = $datetime.$login.$CFG->lams_serverid.$CFG->lams_serverkey;
395 $hashvalue = sha1(strtolower($rawstring));
396 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$login,"password",$roles,$classid,$orgid);
397 $result = $s->call('createUser',$parameters);
398 //echo "<xmp/>".$s->request."</xmp>";
399 //echo "<xmp/>".$s->response."</xmp>";
400 $lams_user->username = $username;
401 $lams_user->login = $result;
402 insert_record("lams_user",$lams_user);
403 //echo "result:".$result."<br/>";
404 //echo "exit lams_get_user"."<br/>";
405 return $result;
406 }else{
407 //echo "exit lams_get_user"."<br/>";
408 return $lams_user->login;
414 * Mapping moodle roles to LAMS roles
416 * @param int $courseid The id of the course that is being viewed
417 * @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.
418 * @return formatted string describing LAMS roles
419 * @TODO fill the gap of roles mapping between moodle and LAMS
422 function lams_get_user_roles($userid=0, $courseid){
423 $roles = "";
424 if(isadmin($userid)){
425 $roles = "administrator"."|"."auhtor"."|"."staff";
426 }else if(isteacheredit($courseid,$userid)){
427 $roles = "auhtor"."|"."staff";
428 }else if(isteacher($courseid,$userid)){
429 $roles = "staff";
431 if(isstudent($courseid,$userid)){
432 if(empty($roles)){
433 $roles = "learner";
434 }else{
435 $roles .= "|"."learner";
438 //echo $roles."<br/>";
439 return $roles;