Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/cohort/locallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 require_once($CFG->dirroot . '/enrol/locallib.php');
00030 
00031 
00038 class enrol_cohort_handler {
00039     public function member_added($ca) {
00040         global $DB;
00041 
00042         if (!enrol_is_enabled('cohort')) {
00043             return true;
00044         }
00045 
00046         // does anything want to sync with this parent?
00047         //TODO: add join to role table to make sure that roleid actually exists
00048         if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) {
00049             return true;
00050         }
00051 
00052         $plugin = enrol_get_plugin('cohort');
00053         foreach ($enrols as $enrol) {
00054             // no problem if already enrolled
00055             $plugin->enrol_user($enrol, $ca->userid, $enrol->roleid);
00056         }
00057 
00058         return true;
00059     }
00060 
00061     public function member_removed($ca) {
00062         global $DB;
00063 
00064         // does anything want to sync with this parent?
00065         if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) {
00066             return true;
00067         }
00068 
00069         $plugin = enrol_get_plugin('cohort');
00070         foreach ($enrols as $enrol) {
00071             // no problem if already enrolled
00072             $plugin->unenrol_user($enrol, $ca->userid);
00073         }
00074 
00075         return true;
00076     }
00077 
00078     public function deleted($cohort) {
00079         global $DB;
00080 
00081         // does anything want to sync with this parent?
00082         if (!$enrols = $DB->get_records('enrol', array('customint1'=>$cohort->id, 'enrol'=>'cohort'), 'id ASC')) {
00083             return true;
00084         }
00085 
00086         $plugin = enrol_get_plugin('cohort');
00087         foreach ($enrols as $enrol) {
00088             $plugin->delete_instance($enrol);
00089         }
00090 
00091         return true;
00092     }
00093 }
00094 
00100 function enrol_cohort_sync($courseid = NULL) {
00101     global $CFG, $DB;
00102 
00103     // unfortunately this may take a long time
00104     @set_time_limit(0); //if this fails during upgrade we can continue from cron, no big deal
00105 
00106     $cohort = enrol_get_plugin('cohort');
00107 
00108     $onecourse = $courseid ? "AND e.courseid = :courseid" : "";
00109 
00110     // iterate through all not enrolled yet users
00111     if (enrol_is_enabled('cohort')) {
00112         $params = array();
00113         $onecourse = "";
00114         if ($courseid) {
00115             $params['courseid'] = $courseid;
00116             $onecourse = "AND e.courseid = :courseid";
00117         }
00118         $sql = "SELECT cm.userid, e.id AS enrolid
00119                   FROM {cohort_members} cm
00120                   JOIN {enrol} e ON (e.customint1 = cm.cohortid AND e.status = :statusenabled AND e.enrol = 'cohort' $onecourse)
00121              LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = cm.userid)
00122                  WHERE ue.id IS NULL";
00123         $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
00124         $params['courseid'] = $courseid;
00125         $rs = $DB->get_recordset_sql($sql, $params);
00126         $instances = array(); //cache
00127         foreach($rs as $ue) {
00128             if (!isset($instances[$ue->enrolid])) {
00129                 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
00130             }
00131             $cohort->enrol_user($instances[$ue->enrolid], $ue->userid);
00132         }
00133         $rs->close();
00134         unset($instances);
00135     }
00136 
00137     // unenrol as necessary - ignore enabled flag, we want to get rid of all
00138     $sql = "SELECT ue.userid, e.id AS enrolid
00139               FROM {user_enrolments} ue
00140               JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' $onecourse)
00141          LEFT JOIN {cohort_members} cm ON (cm.cohortid  = e.customint1 AND cm.userid = ue.userid)
00142              WHERE cm.id IS NULL";
00143     //TODO: this may use a bit of SQL optimisation
00144     $rs = $DB->get_recordset_sql($sql, array('courseid'=>$courseid));
00145     $instances = array(); //cache
00146     foreach($rs as $ue) {
00147         if (!isset($instances[$ue->enrolid])) {
00148             $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
00149         }
00150         $cohort->unenrol_user($instances[$ue->enrolid], $ue->userid);
00151     }
00152     $rs->close();
00153     unset($instances);
00154 
00155     // now assign all necessary roles
00156     if (enrol_is_enabled('cohort')) {
00157         $sql = "SELECT e.roleid, ue.userid, c.id AS contextid, e.id AS itemid
00158                   FROM {user_enrolments} ue
00159                   JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' AND e.status = :statusenabled $onecourse)
00160                   JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :coursecontext)
00161              LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = ue.userid AND ra.itemid = e.id AND ra.component = 'enrol_cohort' AND e.roleid = ra.roleid)
00162                  WHERE ra.id IS NULL";
00163         $params = array();
00164         $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
00165         $params['coursecontext'] = CONTEXT_COURSE;
00166         $params['courseid'] = $courseid;
00167 
00168         $rs = $DB->get_recordset_sql($sql, $params);
00169         foreach($rs as $ra) {
00170             role_assign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid);
00171         }
00172         $rs->close();
00173     }
00174 
00175     // remove unwanted roles - include ignored roles and disabled plugins too
00176     $onecourse = $courseid ? "AND e.courseid = :courseid" : "";
00177     $sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid
00178               FROM {role_assignments} ra
00179               JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :coursecontext)
00180               JOIN {enrol} e ON (e.id = ra.itemid AND e.enrol = 'cohort' $onecourse)
00181          LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
00182              WHERE ra.component = 'enrol_cohort' AND ue.id IS NULL";
00183     $params = array('coursecontext' => CONTEXT_COURSE, 'courseid' => $courseid);
00184 
00185     $rs = $DB->get_recordset_sql($sql, $params);
00186     foreach($rs as $ra) {
00187         role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid);
00188     }
00189     $rs->close();
00190 
00191 }
00192 
00205 function enrol_cohort_enrol_all_users(course_enrolment_manager $manager, $cohortid, $roleid) {
00206     global $DB;
00207     $context = $manager->get_context();
00208     require_capability('moodle/course:enrolconfig', $context);
00209 
00210     $instance = false;
00211     $instances = $manager->get_enrolment_instances();
00212     foreach ($instances as $i) {
00213         if ($i->enrol == 'manual') {
00214             $instance = $i;
00215             break;
00216         }
00217     }
00218     $plugin = enrol_get_plugin('manual');
00219     if (!$instance || !$plugin || !$plugin->allow_enrol($instance) || !has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
00220         return false;
00221     }
00222     $sql = "SELECT com.userid
00223               FROM {cohort_members} com
00224          LEFT JOIN (
00225                 SELECT *
00226                 FROM {user_enrolments} ue
00227                 WHERE ue.enrolid = :enrolid
00228                  ) ue ON ue.userid=com.userid
00229              WHERE com.cohortid = :cohortid AND ue.id IS NULL";
00230     $params = array('cohortid' => $cohortid, 'enrolid' => $instance->id);
00231     $rs = $DB->get_recordset_sql($sql, $params);
00232     $count = 0;
00233     foreach ($rs as $user) {
00234         $count++;
00235         $plugin->enrol_user($instance, $user->userid, $roleid);
00236     }
00237     $rs->close();
00238     return $count;
00239 }
00240 
00248 function enrol_cohort_get_cohorts(course_enrolment_manager $manager) {
00249     global $DB;
00250     $context = $manager->get_context();
00251     $cohorts = array();
00252     $instances = $manager->get_enrolment_instances();
00253     $enrolled = array();
00254     foreach ($instances as $instance) {
00255         if ($instance->enrol == 'cohort') {
00256             $enrolled[] = $instance->customint1;
00257         }
00258     }
00259     list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
00260     $sql = "SELECT id, name, contextid
00261               FROM {cohort}
00262              WHERE contextid $sqlparents
00263           ORDER BY name ASC";
00264     $rs = $DB->get_recordset_sql($sql, $params);
00265     foreach ($rs as $c) {
00266         $context = get_context_instance_by_id($c->contextid);
00267         if (!has_capability('moodle/cohort:view', $context)) {
00268             continue;
00269         }
00270         $cohorts[$c->id] = array(
00271             'cohortid'=>$c->id,
00272             'name'=>format_string($c->name),
00273             'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
00274             'enrolled'=>in_array($c->id, $enrolled)
00275         );
00276     }
00277     $rs->close();
00278     return $cohorts;
00279 }
00280 
00288 function enrol_cohort_can_view_cohort($cohortid) {
00289     global $DB;
00290     $cohort = $DB->get_record('cohort', array('id' => $cohortid), 'id, contextid');
00291     if ($cohort) {
00292         $context = get_context_instance_by_id($cohort->contextid);
00293         if (has_capability('moodle/cohort:view', $context)) {
00294             return true;
00295         }
00296     }
00297     return false;
00298 }
00299 
00310 function enrol_cohort_search_cohorts(course_enrolment_manager $manager, $offset = 0, $limit = 25, $search = '') {
00311     global $DB;
00312     $context = $manager->get_context();
00313     $cohorts = array();
00314     $instances = $manager->get_enrolment_instances();
00315     $enrolled = array();
00316     foreach ($instances as $instance) {
00317         if ($instance->enrol == 'cohort') {
00318             $enrolled[] = $instance->customint1;
00319         }
00320     }
00321 
00322     list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
00323 
00324     // Add some additional sensible conditions
00325     $tests = array('contextid ' . $sqlparents);
00326 
00327     // Modify the quesry to perform the search if requred
00328     if (!empty($search)) {
00329         $conditions = array(
00330             'name',
00331             'idnumber',
00332             'description'
00333         );
00334         $searchparam = '%' . $search . '%';
00335         foreach ($conditions as $key=>$condition) {
00336             $conditions[$key] = $DB->sql_like($condition,"?", false);
00337             $params[] = $searchparam;
00338         }
00339         $tests[] = '(' . implode(' OR ', $conditions) . ')';
00340     }
00341     $wherecondition = implode(' AND ', $tests);
00342 
00343     $fields = 'SELECT id, name, contextid, description';
00344     $countfields = 'SELECT COUNT(1)';
00345     $sql = " FROM {cohort}
00346              WHERE $wherecondition";
00347     $order = ' ORDER BY name ASC';
00348     $rs = $DB->get_recordset_sql($fields . $sql . $order, $params, $offset);
00349 
00350     // Produce the output respecting parameters
00351     foreach ($rs as $c) {
00352         // Track offset
00353         $offset++;
00354         // Check capabilities
00355         $context = get_context_instance_by_id($c->contextid);
00356         if (!has_capability('moodle/cohort:view', $context)) {
00357             continue;
00358         }
00359         if ($limit === 0) {
00360             // we have reached the required number of items and know that there are more, exit now
00361             $offset--;
00362             break;
00363         }
00364         $cohorts[$c->id] = array(
00365             'cohortid'=>$c->id,
00366             'name'=>  shorten_text(format_string($c->name), 35),
00367             'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
00368             'enrolled'=>in_array($c->id, $enrolled)
00369         );
00370         // Count items
00371         $limit--;
00372     }
00373     $rs->close();
00374     return array('more' => !(bool)$limit, 'offset' => $offset, 'cohorts' => $cohorts);
00375 }
 All Data Structures Namespaces Files Functions Variables Enumerations