]> git.mjollnir.org Git - moodle.git/commitdiff
Mnet: Update user's enrolment information on jump/land
authordonal72 <donal72>
Tue, 23 Jan 2007 02:38:54 +0000 (02:38 +0000)
committerdonal72 <donal72>
Tue, 23 Jan 2007 02:38:54 +0000 (02:38 +0000)
auth/mnet/auth.php
lang/en_utf8/auth_mnet.php
mnet/xmlrpc/server.php

index 6fc89c67a30b18f2c5808f739b4f3ebab59de4ce..5b23e27a84a0c0d7cacf819d0661fb52dfa13d3f 100644 (file)
@@ -306,7 +306,7 @@ class auth_plugin_mnet
 
         // check sso access control list for permission first
         if (!$this->can_login_remotely($localuser->username, $remotehost->id)) {
-            error("Username '$localuser->username' is not permitted to login from '$remotehost->name'.");
+            print_error('sso_mnet_login_refused', 'mnet', '', array($localuser->username, $remotehost->name));
         }
 
         $session_gc_maxlifetime = 1440;
@@ -423,6 +423,16 @@ class auth_plugin_mnet
                 $keys = array_keys($courses);
                 $defaultrolename = get_field('role', 'shortname', 'id', $CFG->defaultcourseroleid);
                 foreach ($keys AS $id) {
+                    if ($courses[$id]->visible == 0) {
+                        unset($courses[$id]);
+                        continue;
+                    }
+                    $courses[$id]->cat_id          = $extra[$id]->category;
+                    $courses[$id]->defaultroleid   = $extra[$id]->defaultrole;
+                    unset($courses[$id]->category);
+                    unset($courses[$id]->defaultrole);
+                    unset($courses[$id]->visible);
+
                     $courses[$id]->cat_name        = $extra[$id]->cat_name;
                     $courses[$id]->cat_description = $extra[$id]->cat_description;
                     if (!empty($extra[$id]->defaultrolename)) {
@@ -461,7 +471,7 @@ class auth_plugin_mnet
      *   @returns bool
      */
     function update_enrolments($username, $courses) {
-        global $MNET_REMOTE_CLIENT;
+        global $MNET_REMOTE_CLIENT, $CFG;
 
         if (empty($username) || !is_array($courses)) {
             return false;
@@ -482,10 +492,99 @@ class auth_plugin_mnet
             return true;
         }
 
-        // add/update courses && enrolment entries
-        // remove stale enrolments (but not the courses)
-        // TODO
+        // IMPORTANT: Ask for remoteid as the first element in the query, so
+        // that the array that comes back is indexed on the same field as the
+        // array that we have received from the remote client
+        $sql = '
+                SELECT
+                    c.remoteid,
+                    c.id,
+                    c.cat_id,
+                    c.cat_name,
+                    c.cat_description,
+                    c.sortorder,
+                    c.fullname,
+                    c.shortname,
+                    c.idnumber,
+                    c.summary,
+                    c.startdate,
+                    c.cost,
+                    c.currency,
+                    c.defaultroleid,
+                    c.defaultrolename,
+                    a.id as assignmentid
+                FROM
+                    '.$CFG->prefix.'mnet_enrol_course c
+                LEFT JOIN
+                    '.$CFG->prefix.'mnet_enrol_assignments a
+                ON
+                   (a.courseid = c.id AND
+                    a.hostid   = c.hostid AND
+                    a.userid = \''.$userid.'\')
+                WHERE
+                    c.hostid = \''.(int)$MNET_REMOTE_CLIENT->id.'\'';
+
+        $currentcourses = get_records_sql($sql);
+
+        $local_courseid_array = array();
+        foreach($courses as $course) {
+
+            $course['remoteid'] = $course['id'];
+            $course['hostid']   =  (int)$MNET_REMOTE_CLIENT->id;
+            $userisregd         = false;
+
+            // First up - do we have a record for this course?
+            if (!array_key_exists($course['remoteid'], $currentcourses)) {
+                // No record - we must create it
+                $course['id']  =  insert_record('mnet_enrol_course', (object)$course);
+                $currentcourse = (object)$course;
+            } else {
+                // Pointer to current course:
+                $currentcourse =& $currentcourses[$course['remoteid']];
+                // We have a record - is it up-to-date?
+                $course['id'] = $currentcourse->id;
+
+                $saveflag = false;
+
+                foreach($course as $key => $value) {
+                    if ($currentcourse->$key != $value) {
+                        $saveflag = true;
+                        $currentcourse->$key = $value;
+                    }
+                }
+
+                if ($saveflag) {
+                    update_record('mnet_enrol_course', $currentcourse);
+                }
+                
+                if (isset($currentcourse->assignmentid) && is_numeric($currentcourse->assignmentid)) {
+                    $userisregd = true;
+                }
+            }
+
+            // By this point, we should always have a $dataObj->id
+            $local_courseid_array[] = $course['id'];
+
+            // Do we have a record for this assignment?
+            if ($userisregd) {
+                // Yes - we know about this one already
+                // We don't want to do updates because the new data is probably
+                // 'less complete' than the data we have.
+            } else {
+                // No - create a record
+                $assignObj = new stdClass();
+                $assignObj->userid    = $userid;
+                $assignObj->hostid    = (int)$MNET_REMOTE_CLIENT->id;
+                $assignObj->courseid  = $course['id'];
+                $assignObj->rolename  = $course['defaultrolename'];
+                $assignObj->id = insert_record('mnet_enrol_assignments', $assignObj);
+            }
+        }
 
+        // Clean up courses that the user is no longer enrolled in.
+        $local_courseid_string = implode(', ', $local_courseid_array);
+        $whereclause = " userid = '$userid' AND hostid = '{$MNET_REMOTE_CLIENT->id}' AND courseid NOT IN ($local_courseid_string)";
+        delete_records_select('mnet_enrol_assignments', $whereclause);
     }
 
     /**
index 8d0386fee0352c72e0bf3a2ba431df2d00ce0d3e..045fa56643b7829876bae05d0567695ebc851478 100644 (file)
@@ -10,4 +10,5 @@ $string['sso_sp_description']          = 'Publish  this service to allow authent
                                          '<ul><li><em>Dependency</em>: You must also <strong>subscribe</strong> to the SSO (Identity Provider) service on $a.</li></ul><br />'.
                                          'Subscribe to this service to allow your users to roam to the $a Moodle site without having to re-login there. '.
                                          '<ul><li><em>Dependency</em>: You must also <strong>publish</strong> the SSO (Identity Provider) service to $a.</li></ul><br />';
+$string['sso_mnet_login_refused']      = 'Username $a[0] is not permitted to login from $a[1].';
 ?>
\ No newline at end of file
index 3eb30b3b26f487e26a69a5c2b43a48c7312a7c2b..9acc80fda6e8d3f1547b66b8f0b00a5f1bf15b0b 100644 (file)
@@ -253,7 +253,7 @@ function mnet_server_fault($code, $text, $param = null) {
  * @return string $text   The XML text of the error message
  */
 function mnet_server_fault_xml($code, $text) {
-    global $MNET_REMOTE_CLIENT;
+    global $MNET_REMOTE_CLIENT, $CFG;
     // Replace illegal XML chars - is this already in a lib somewhere?
     $text = str_replace(array('<','>','&','"',"'"), array('&lt;','&gt;','&amp;','&quot;','&apos;'), $text);
 
@@ -274,6 +274,12 @@ function mnet_server_fault_xml($code, $text) {
          </value>
       </fault>
    </methodResponse>');
+
+    if (!empty($CFG->mnet_rpcdebug)) {
+        trigger_error("XMLRPC Error Response");
+        trigger_error(print_r($return,1));
+    }
+
     return $return;
 }