| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?PHP // $Id: lib.php,v 1.8.2.1 2008/01/24 20:29:31 skodak Exp $ 2 3 /// Library of functions and constants for module lams 4 5 if (!defined('MOODLE_INTERNAL')) { 6 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page 7 } 8 9 require_once($CFG->dirroot.'/lib/soaplib.php'); 10 11 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); 21 } 22 23 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; 35 } 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); 45 } 46 47 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. 52 53 if (! $lams = get_record("lams", "id", "$id")) { 54 return false; 55 } 56 57 $result = true; 58 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; 63 } 64 65 return $result; 66 } 67 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 74 75 return $return; 76 } 77 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. 81 82 return true; 83 } 84 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 ... 89 90 global $CFG; 91 92 return true; 93 } 94 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. 98 /// 99 /// $return->grades = array of grades; 100 /// $return->maxgrade = maximum allowed grade; 101 /// 102 /// return $return; 103 104 return NULL; 105 } 106 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. 112 113 return false; 114 } 115 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 120 //as reference. 121 122 $return = false; 123 124 //$rec = get_record("lams","id","$lamsid","scale","-$scaleid"); 125 // 126 //if (!empty($rec) && !empty($scaleid)) { 127 // $return = true; 128 //} 129 130 return $return; 131 } 132 133 /** 134 * Checks if scale is being used by any instance of lams 135 * 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 139 */ 140 function lams_scale_used_anywhere($scaleid) { 141 return false; 142 } 143 144 ////////////////////////////////////////////////////////////////////////////////////// 145 /// Any other lams functions go here. Each of them must have a name that 146 /// starts with lams_ 147 148 function lams_get_soap_client($relativeurl) { 149 global $CFG; 150 if(!isset($CFG->lams_serverurl)) 151 { 152 return NULL; 153 } 154 $wsdl = $CFG->lams_serverurl.$relativeurl; 155 $s = new soap_client($wsdl,true,false,false,false,false,2,3); 156 return $s; 157 } 158 159 /** 160 * Get sequences(learning designs) for the user in LAMS 161 * 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 165 */ 166 function lams_get_sequences($username,$courseid) { 167 global $CFG,$USER; 168 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey)||!isset($CFG->lams_serverurl)) 169 { 170 return get_string("notsetup", "lams"); 171 } 172 $relativeurl="/services/LearningDesignService?wsdl"; 173 $s = lams_get_soap_client($relativeurl); 174 if(is_null($s)){ 175 return NULL; 176 } 177 $datetime = date("F d,Y g:i a"); 178 /* 179 $login = lams_get_user($username,$courseid); 180 if(empty($login)){ 181 return NULL; 182 } 183 */ 184 if(!isset($username)){ 185 $username = $USER->username; 186 } 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 193 } 194 unset($s); 195 return $result; 196 } 197 198 /** 199 * Get learning session(lesson) id from LAMS 200 * 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 208 */ 209 function lams_get_lesson($username,$ldid,$courseid,$title,$desc,$type) { 210 //echo "enter lams_get_lesson<br/>"; 211 global $CFG,$USER; 212 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey)) 213 { 214 //echo "serverid or serverkey is not set<br/>"; 215 return NULL; 216 } 217 $relativeurl="/services/LearningSessionService?wsdl"; 218 $s = lams_get_soap_client($relativeurl); 219 if(is_null($s)){ 220 //echo "soap client is null<br/>"; 221 return NULL; 222 } 223 $datetime = date("F d,Y g:i a"); 224 if(!isset($username)){ 225 $username = $USER->username; 226 } 227 $plaintext = $datetime.$username.$CFG->lams_serverid.$CFG->lams_serverkey; 228 //echo $plaintext; 229 $hashvalue = sha1(strtolower($plaintext)); 230 //echo $hashvalue; 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/>"; 235 if($s->getError()){ 236 $result = $s->getError(); 237 } 238 unset($s); 239 return $result; 240 } 241 242 /** 243 * Delete learning session(lesson) from LAMS 244 * 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 248 */ 249 function lams_delete_lesson($username,$lsid) { 250 //echo "enter lams_get_lesson<br/>"; 251 global $CFG,$USER; 252 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey)) 253 { 254 return "The LAMS serverId and serverKey have not been set up"; 255 } 256 $relativeurl="/services/LearningSessionService?wsdl"; 257 $s = lams_get_soap_client($relativeurl); 258 if(is_null($s)){ 259 return "Failed to get soap client based on:".$relativeurl; 260 } 261 $datetime = date("F d,Y g:i a"); 262 if(!isset($username)){ 263 $username = $USER->username; 264 } 265 $plaintext = $datetime.$username.$CFG->lams_serverid.$CFG->lams_serverkey; 266 //echo $plaintext; 267 $hashvalue = sha1(strtolower($plaintext)); 268 //echo $hashvalue; 269 $parameters = array($CFG->lams_serverid,$datetime,$hashvalue,$username,$lsid); 270 $result = $s->call('deleteLearningSession',$parameters); 271 if($s->getError()){ 272 $result = $s->getError(); 273 } 274 unset($s); 275 return $result; 276 } 277 278 279 /** 280 * Get class in LAMS 281 * @param int courseid 282 * @return int class id 283 * @TODO complete the documentation of this function 284 */ 285 /* 286 function lams_get_class($courseid) { 287 global $CFG,$USER; 288 //echo "enter lams_get_class"."<br/>"; 289 $orgId = lams_get_organisation(); 290 if(empty($orgId)){ 291 return NULL; 292 } 293 $lams_course = get_record("lams_course","course", $courseid); 294 if(empty($lams_course)){//LAMS class hasn't been created 295 //create LAMS class 296 $relativeurl="/services/UserManagementService?wsdl"; 297 $s = lams_get_soap_client($relativeurl); 298 if(is_null($s)){ 299 return NULL; 300 } 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/>"; 313 return $result; 314 }else{ 315 //echo "exit lams_get_class"."<br/>"; 316 return $lams_course->classid; 317 } 318 } 319 */ 320 /** 321 * Get organisation in LAMS 322 * 323 * @return int organisation id 324 * @TODO complete the documentation of this function 325 */ 326 /* 327 function lams_get_organisation() { 328 global $CFG,$USER; 329 //echo "enter lams_get_organisaiton"."<br/>"; 330 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey)) 331 { 332 return NULL; 333 } 334 if(!isset($CFG->lams_orgid)){ 335 $relativeurl="/services/UserManagementService?wsdl"; 336 $s = lams_get_soap_client($relativeurl); 337 if(empty($s)){ 338 return NULL; 339 } 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/>"; 350 return $result; 351 }else{ 352 //echo "exit lams_get_organisaiton"."<br/>"; 353 return $CFG->lams_orgid; 354 } 355 } 356 */ 357 358 /** 359 * Get user in LAMS 360 * 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 367 */ 368 /* 369 function lams_get_user($username,$courseid) { 370 global $CFG,$USER; 371 //echo "enter lams_get_user"."<br/>"; 372 if(!isset($CFG->lams_serverid)||!isset($CFG->lams_serverkey)) 373 { 374 return NULL; 375 } 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! 380 return NULL; 381 } 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 385 return NULL; 386 } 387 $roles = lams_get_user_roles($user->id,$courseid); 388 $relativeurl="/services/UserManagementService?wsdl"; 389 $s = lams_get_soap_client($relativeurl); 390 if(empty($s)){ 391 return NULL; 392 } 393 $datetime = date("F d,Y g:i a"); 394 $login = $username; 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/>"; 406 return $result; 407 }else{ 408 //echo "exit lams_get_user"."<br/>"; 409 return $lams_user->login; 410 } 411 } 412 */ 413 414 /** 415 * Mapping moodle roles to LAMS roles 416 * 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 421 */ 422 /* 423 function lams_get_user_roles($userid=0, $courseid){ 424 $roles = ""; 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)){ 430 $roles = "staff"; 431 } 432 if(isstudent($courseid,$userid)){ 433 if(empty($roles)){ 434 $roles = "learner"; 435 }else{ 436 $roles .= "|"."learner"; 437 } 438 } 439 //echo $roles."<br/>"; 440 return $roles; 441 } 442 */ 443 444 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |