Ignore:
Timestamp:
May 2, 2016, 12:09:23 PM (3 years ago)
Author:
jrpelegrina
Message:

Updated to moodle 3.0.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • moodle/trunk/fuentes/enrol/meta/locallib.php

    r136 r1331  
    8181    protected static function sync_with_parent_course(stdClass $instance, $userid) {
    8282        global $DB, $CFG;
     83        require_once($CFG->dirroot . '/group/lib.php');
    8384
    8485        $plugin = enrol_get_plugin('meta');
     
    9596        $params['userid'] = $userid;
    9697        $params['parentcourse'] = $instance->customint1;
    97         $sql = "SELECT ue.*
     98        $sql = "SELECT ue.*, e.status AS enrolstatus
    9899                  FROM {user_enrolments} ue
    99100                  JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol <> 'meta' AND e.courseid = :parentcourse AND e.enrol $enabled)
     
    142143        }
    143144
    144         // is parent enrol active? (we ignore enrol starts and ends, sorry it would be too complex)
     145        // Is parent enrol active? Find minimum timestart and maximum timeend of all active enrolments.
    145146        $parentstatus = ENROL_USER_SUSPENDED;
     147        $parenttimeend = null;
     148        $parenttimestart = null;
    146149        foreach ($parentues as $pue) {
    147             if ($pue->status == ENROL_USER_ACTIVE) {
     150            if ($pue->status == ENROL_USER_ACTIVE && $pue->enrolstatus == ENROL_INSTANCE_ENABLED) {
    148151                $parentstatus = ENROL_USER_ACTIVE;
    149                 break;
    150             }
    151         }
    152 
    153         // enrol user if not enrolled yet or fix status
     152                if ($parenttimeend === null || $pue->timeend == 0 || ($parenttimeend && $parenttimeend < $pue->timeend)) {
     153                    $parenttimeend = $pue->timeend;
     154                }
     155                if ($parenttimestart === null || $parenttimestart > $pue->timestart) {
     156                    $parenttimestart = $pue->timestart;
     157                }
     158            }
     159        }
     160
     161        // Enrol user if not enrolled yet or fix status/timestart/timeend. Use the minimum timestart and maximum timeend found above.
    154162        if ($ue) {
    155             if ($parentstatus != $ue->status) {
    156                 $plugin->update_user_enrol($instance, $userid, $parentstatus);
     163            if ($parentstatus != $ue->status ||
     164                    ($parentstatus == ENROL_USER_ACTIVE && ($parenttimestart != $ue->timestart || $parenttimeend != $ue->timeend))) {
     165                $plugin->update_user_enrol($instance, $userid, $parentstatus, $parenttimestart, $parenttimeend);
    157166                $ue->status = $parentstatus;
     167                $ue->timestart = $parenttimestart;
     168                $ue->timeend = $parenttimeend;
    158169            }
    159170        } else {
    160             $plugin->enrol_user($instance, $userid, NULL, 0, 0, $parentstatus);
     171            $plugin->enrol_user($instance, $userid, NULL, (int)$parenttimestart, (int)$parenttimeend, $parentstatus);
    161172            $ue = new stdClass();
    162173            $ue->userid = $userid;
    163174            $ue->enrolid = $instance->id;
    164175            $ue->status = $parentstatus;
     176            if ($instance->customint2) {
     177                groups_add_member($instance->customint2, $userid, 'enrol_meta', $instance->id);
     178            }
    165179        }
    166180
    167181        $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
    168182
    169         // only active users in enabled instances are supposed to have roles (we can reassign the roles any time later)
    170         if ($ue->status != ENROL_USER_ACTIVE or $instance->status != ENROL_INSTANCE_ENABLED) {
     183        // Only active users in enabled instances are supposed to have roles (we can reassign the roles any time later).
     184        if ($ue->status != ENROL_USER_ACTIVE or $instance->status != ENROL_INSTANCE_ENABLED or
     185                ($parenttimeend and $parenttimeend < time()) or ($parenttimestart > time())) {
    171186            if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND) {
    172187                // Always keep the roles.
    173188            } else if ($roles) {
     189                // This will only unassign roles that were assigned in this enrolment method, leaving all manual role assignments intact.
    174190                role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$instance->id));
    175191            }
     
    245261function enrol_meta_sync($courseid = NULL, $verbose = false) {
    246262    global $CFG, $DB;
     263    require_once("{$CFG->dirroot}/group/lib.php");
    247264
    248265    // purge all roles if meta sync disabled, those can be recreated later here in cron
     
    275292
    276293
    277     // iterate through all not enrolled yet users
     294    // Iterate through all not enrolled yet users. For each active enrolment of each user find the minimum
     295    // enrolment startdate and maximum enrolment enddate.
     296    // This SQL relies on the fact that ENROL_USER_ACTIVE < ENROL_USER_SUSPENDED
     297    // and ENROL_INSTANCE_ENABLED < ENROL_INSTANCE_DISABLED. Condition "pue.status + pe.status = 0" means
     298    // that enrolment is active. When MIN(pue.status + pe.status)=0 it means there exists an active
     299    // enrolment.
    278300    $onecourse = $courseid ? "AND e.courseid = :courseid" : "";
    279301    list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled), SQL_PARAMS_NAMED, 'e');
    280302    $params['courseid'] = $courseid;
    281     $sql = "SELECT pue.userid, e.id AS enrolid, pue.status
     303    $sql = "SELECT pue.userid, e.id AS enrolid, MIN(pue.status + pe.status) AS status,
     304                      MIN(CASE WHEN (pue.status + pe.status = 0) THEN pue.timestart ELSE 9999999999 END) AS timestart,
     305                      MAX(CASE WHEN (pue.status + pe.status = 0) THEN
     306                                (CASE WHEN pue.timeend = 0 THEN 9999999999 ELSE pue.timeend END)
     307                                ELSE 0 END) AS timeend
    282308              FROM {user_enrolments} pue
    283309              JOIN {enrol} pe ON (pe.id = pue.enrolid AND pe.enrol <> 'meta' AND pe.enrol $enabled)
     
    285311              JOIN {user} u ON (u.id = pue.userid AND u.deleted = 0)
    286312         LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = pue.userid)
    287              WHERE ue.id IS NULL";
     313             WHERE ue.id IS NULL
     314             GROUP BY pue.userid, e.id";
    288315
    289316    $rs = $DB->get_recordset_sql($sql, $params);
     
    310337        }
    311338
    312         $meta->enrol_user($instance, $ue->userid, $ue->status);
     339        // So now we have aggregated values that we will use for the meta enrolment status, timeend and timestart.
     340        // Again, we use the fact that active=0 and disabled/suspended=1. Only when MIN(pue.status + pe.status)=0 the enrolment is active:
     341        $ue->status = ($ue->status == ENROL_USER_ACTIVE + ENROL_INSTANCE_ENABLED) ? ENROL_USER_ACTIVE : ENROL_USER_SUSPENDED;
     342        // Timeend 9999999999 was used instead of 0 in the "MAX()" function:
     343        $ue->timeend = ($ue->timeend == 9999999999) ? 0 : (int)$ue->timeend;
     344        // Timestart 9999999999 is only possible when there are no active enrolments:
     345        $ue->timestart = ($ue->timestart == 9999999999) ? 0 : (int)$ue->timestart;
     346
     347        $meta->enrol_user($instance, $ue->userid, null, $ue->timestart, $ue->timeend, $ue->status);
     348        if ($instance->customint2) {
     349            groups_add_member($instance->customint2, $ue->userid, 'enrol_meta', $instance->id);
     350        }
    313351        if ($verbose) {
    314352            mtrace("  enrolling: $ue->userid ==> $instance->courseid");
     
    364402
    365403
    366     // update status - meta enrols + start and end dates are ignored, sorry
    367     // note the trick here is that the active enrolment and instance constants have value 0
     404    // Update status - meta enrols are ignored to avoid recursion.
     405    // Note the trick here is that the active enrolment and instance constants have value 0.
    368406    $onecourse = $courseid ? "AND e.courseid = :courseid" : "";
    369407    list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled), SQL_PARAMS_NAMED, 'e');
    370408    $params['courseid'] = $courseid;
    371     $sql = "SELECT ue.userid, ue.enrolid, pue.pstatus
     409    $sql = "SELECT ue.userid, ue.enrolid, pue.pstatus, pue.ptimestart, pue.ptimeend
    372410              FROM {user_enrolments} ue
    373411              JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'meta' $onecourse)
    374               JOIN (SELECT xpue.userid, xpe.courseid, MIN(xpue.status + xpe.status) AS pstatus
     412              JOIN (SELECT xpue.userid, xpe.courseid, MIN(xpue.status + xpe.status) AS pstatus,
     413                      MIN(CASE WHEN (xpue.status + xpe.status = 0) THEN xpue.timestart ELSE 9999999999 END) AS ptimestart,
     414                      MAX(CASE WHEN (xpue.status + xpe.status = 0) THEN
     415                                (CASE WHEN xpue.timeend = 0 THEN 9999999999 ELSE xpue.timeend END)
     416                                ELSE 0 END) AS ptimeend
    375417                      FROM {user_enrolments} xpue
    376418                      JOIN {enrol} xpe ON (xpe.id = xpue.enrolid AND xpe.enrol <> 'meta' AND xpe.enrol $enabled)
    377419                  GROUP BY xpue.userid, xpe.courseid
    378420                   ) pue ON (pue.courseid = e.customint1 AND pue.userid = ue.userid)
    379              WHERE (pue.pstatus = 0 AND ue.status > 0) OR (pue.pstatus > 0 and ue.status = 0)";
     421             WHERE (pue.pstatus = 0 AND ue.status > 0) OR (pue.pstatus > 0 and ue.status = 0)
     422             OR ((CASE WHEN pue.ptimestart = 9999999999 THEN 0 ELSE pue.ptimestart END) <> ue.timestart)
     423             OR ((CASE WHEN pue.ptimeend = 9999999999 THEN 0 ELSE pue.ptimeend END) <> ue.timeend)";
    380424    $rs = $DB->get_recordset_sql($sql, $params);
    381425    foreach($rs as $ue) {
     
    384428        }
    385429        $instance = $instances[$ue->enrolid];
    386         $ue->pstatus = ($ue->pstatus == ENROL_USER_ACTIVE) ? ENROL_USER_ACTIVE : ENROL_USER_SUSPENDED;
    387 
    388         if ($ue->pstatus == ENROL_USER_ACTIVE and !$syncall and $unenrolaction != ENROL_EXT_REMOVED_UNENROL) {
     430        $ue->pstatus = ($ue->pstatus == ENROL_USER_ACTIVE + ENROL_INSTANCE_ENABLED) ? ENROL_USER_ACTIVE : ENROL_USER_SUSPENDED;
     431        $ue->ptimeend = ($ue->ptimeend == 9999999999) ? 0 : (int)$ue->ptimeend;
     432        $ue->ptimestart = ($ue->ptimestart == 9999999999) ? 0 : (int)$ue->ptimestart;
     433
     434        if ($ue->pstatus == ENROL_USER_ACTIVE and (!$ue->ptimeend || $ue->ptimeend > time())
     435                and !$syncall and $unenrolaction != ENROL_EXT_REMOVED_UNENROL) {
    389436            // this may be slow if very many users are ignored in sync
    390437            $parentcontext = context_course::instance($instance->customint1);
     
    402449        }
    403450
    404         $meta->update_user_enrol($instance, $ue->userid, $ue->pstatus);
     451        $meta->update_user_enrol($instance, $ue->userid, $ue->pstatus, $ue->ptimestart, $ue->ptimeend);
    405452        if ($verbose) {
    406453            if ($ue->pstatus == ENROL_USER_ACTIVE) {
     
    545592    }
    546593
     594    // Finally sync groups.
     595    $affectedusers = groups_sync_with_enrolment('meta', $courseid);
     596    if ($verbose) {
     597        foreach ($affectedusers['removed'] as $gm) {
     598            mtrace("removing user from group: $gm->userid ==> $gm->courseid - $gm->groupname", 1);
     599        }
     600        foreach ($affectedusers['added'] as $ue) {
     601            mtrace("adding user to group: $ue->userid ==> $ue->courseid - $ue->groupname", 1);
     602        }
     603    }
     604
    547605    if ($verbose) {
    548606        mtrace('...user enrolment synchronisation finished.');
     
    551609    return 0;
    552610}
     611
     612/**
     613 * Create a new group with the course's name.
     614 *
     615 * @param int $courseid
     616 * @param int $linkedcourseid
     617 * @return int $groupid Group ID for this cohort.
     618 */
     619function enrol_meta_create_new_group($courseid, $linkedcourseid) {
     620    global $DB, $CFG;
     621
     622    require_once($CFG->dirroot.'/group/lib.php');
     623
     624    $coursename = $DB->get_field('course', 'fullname', array('id' => $linkedcourseid), MUST_EXIST);
     625    $a = new stdClass();
     626    $a->name = $coursename;
     627    $a->increment = '';
     628    $inc = 1;
     629    $groupname = trim(get_string('defaultgroupnametext', 'enrol_meta', $a));
     630    // Check to see if the group name already exists in this course. Add an incremented number if it does.
     631    while ($DB->record_exists('groups', array('name' => $groupname, 'courseid' => $courseid))) {
     632        $a->increment = '(' . (++$inc) . ')';
     633        $groupname = trim(get_string('defaultgroupnametext', 'enrol_meta', $a));
     634    }
     635    // Create a new group for the course meta sync.
     636    $groupdata = new stdClass();
     637    $groupdata->courseid = $courseid;
     638    $groupdata->name = $groupname;
     639    $groupid = groups_create_group($groupdata);
     640
     641    return $groupid;
     642}
Note: See TracChangeset for help on using the changeset viewer.