Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/manual/locallib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 defined('MOODLE_INTERNAL') || die();
00027 
00028 require_once($CFG->dirroot . '/user/selector/lib.php');
00029 require_once($CFG->dirroot . '/enrol/locallib.php');
00030 
00031 
00035 class enrol_manual_potential_participant extends user_selector_base {
00036     protected $enrolid;
00037 
00038     public function __construct($name, $options) {
00039         $this->enrolid  = $options['enrolid'];
00040         parent::__construct($name, $options);
00041     }
00042 
00048     public function find_users($search) {
00049         global $DB;
00050         //by default wherecondition retrieves all users except the deleted, not confirmed and guest
00051         list($wherecondition, $params) = $this->search_sql($search, 'u');
00052         $params['enrolid'] = $this->enrolid;
00053 
00054         $fields      = 'SELECT ' . $this->required_fields_sql('u');
00055         $countfields = 'SELECT COUNT(1)';
00056 
00057         $sql = " FROM {user} u
00058                 WHERE $wherecondition AND
00059                       u.id NOT IN (
00060                           SELECT ue.userid
00061                             FROM {user_enrolments} ue
00062                             JOIN {enrol} e ON (e.id = ue.enrolid AND e.id = :enrolid))";
00063         $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
00064 
00065         if (!$this->is_validating()) {
00066             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
00067             if ($potentialmemberscount > 100) {
00068                 return $this->too_many_results($search, $potentialmemberscount);
00069             }
00070         }
00071 
00072         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
00073 
00074         if (empty($availableusers)) {
00075             return array();
00076         }
00077 
00078 
00079         if ($search) {
00080             $groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
00081         } else {
00082             $groupname = get_string('enrolcandidates', 'enrol');
00083         }
00084 
00085         return array($groupname => $availableusers);
00086     }
00087 
00088     protected function get_options() {
00089         $options = parent::get_options();
00090         $options['enrolid'] = $this->enrolid;
00091         $options['file']    = 'enrol/manual/locallib.php';
00092         return $options;
00093     }
00094 }
00095 
00099 class enrol_manual_current_participant extends user_selector_base {
00100     protected $courseid;
00101     protected $enrolid;
00102 
00103     public function __construct($name, $options) {
00104         $this->enrolid  = $options['enrolid'];
00105         parent::__construct($name, $options);
00106     }
00107 
00113     public function find_users($search) {
00114         global $DB;
00115         //by default wherecondition retrieves all users except the deleted, not confirmed and guest
00116         list($wherecondition, $params) = $this->search_sql($search, 'u');
00117         $params['enrolid'] = $this->enrolid;
00118 
00119         $fields      = 'SELECT ' . $this->required_fields_sql('u');
00120         $countfields = 'SELECT COUNT(1)';
00121 
00122         $sql = " FROM {user} u
00123                  JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
00124                 WHERE $wherecondition";
00125 
00126         $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
00127 
00128         if (!$this->is_validating()) {
00129             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
00130             if ($potentialmemberscount > 100) {
00131                 return $this->too_many_results($search, $potentialmemberscount);
00132             }
00133         }
00134 
00135         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
00136 
00137         if (empty($availableusers)) {
00138             return array();
00139         }
00140 
00141 
00142         if ($search) {
00143             $groupname = get_string('enrolledusersmatching', 'enrol', $search);
00144         } else {
00145             $groupname = get_string('enrolledusers', 'enrol');
00146         }
00147 
00148         return array($groupname => $availableusers);
00149     }
00150 
00151     protected function get_options() {
00152         $options = parent::get_options();
00153         $options['enrolid'] = $this->enrolid;
00154         $options['file']    = 'enrol/manual/locallib.php';
00155         return $options;
00156     }
00157 }
00158 
00165 class enrol_manual_editselectedusers_operation extends enrol_bulk_enrolment_operation {
00166 
00172     public function get_title() {
00173         return get_string('editselectedusers', 'enrol_manual');
00174     }
00175 
00180     public function get_identifier() {
00181         return 'editselectedusers';
00182     }
00183 
00192     public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
00193         global $DB, $USER;
00194 
00195         if (!has_capability("enrol/manual:manage", $manager->get_context())) {
00196             return false;
00197         }
00198 
00199         // Get all of the user enrolment id's
00200         $ueids = array();
00201         $instances = array();
00202         foreach ($users as $user) {
00203             foreach ($user->enrolments as $enrolment) {
00204                 $ueids[] = $enrolment->id;
00205                 if (!array_key_exists($enrolment->id, $instances)) {
00206                     $instances[$enrolment->id] = $enrolment;
00207                 }
00208             }
00209         }
00210 
00211         // Check that each instance is manageable by the current user.
00212         foreach ($instances as $instance) {
00213             if (!$this->plugin->allow_manage($instance)) {
00214                 return false;
00215             }
00216         }
00217 
00218         // Collect the known properties.
00219         $status = $properties->status;
00220         $timestart = $properties->timestart;
00221         $timeend = $properties->timeend;
00222 
00223         list($ueidsql, $params) = $DB->get_in_or_equal($ueids, SQL_PARAMS_NAMED);
00224 
00225         $updatesql = array();
00226         if ($status == ENROL_USER_ACTIVE || $status == ENROL_USER_SUSPENDED) {
00227             $updatesql[] = 'status = :status';
00228             $params['status'] = (int)$status;
00229         }
00230         if (!empty($timestart)) {
00231             $updatesql[] = 'timestart = :timestart';
00232             $params['timestart'] = (int)$timestart;
00233         }
00234         if (!empty($timeend)) {
00235             $updatesql[] = 'timeend = :timeend';
00236             $params['timeend'] = (int)$timeend;
00237         }
00238         if (empty($updatesql)) {
00239             return true;
00240         }
00241 
00242         // Update the modifierid
00243         $updatesql[] = 'modifierid = :modifierid';
00244         $params['modifierid'] = (int)$USER->id;
00245 
00246         // Update the time modified
00247         $updatesql[] = 'timemodified = :timemodified';
00248         $params['timemodified'] = time();
00249 
00250         // Build the SQL statement
00251         $updatesql = join(', ', $updatesql);
00252         $sql = "UPDATE {user_enrolments}
00253                    SET $updatesql
00254                  WHERE id $ueidsql";
00255 
00256         if ($DB->execute($sql, $params)) {
00257             foreach ($users as $user) {
00258                 foreach ($user->enrolments as $enrolment) {
00259                     $enrolment->courseid  = $enrolment->enrolmentinstance->courseid;
00260                     $enrolment->enrol     = 'manual';
00261                     events_trigger('user_enrol_modified', $enrolment);
00262                 }
00263             }
00264             return true;
00265         }
00266 
00267         return false;
00268     }
00269 
00278     public function get_form($defaultaction = null, $defaultcustomdata = null) {
00279         global $CFG;
00280         require_once($CFG->dirroot.'/enrol/manual/bulkchangeforms.php');
00281         return new enrol_manual_editselectedusers_form($defaultaction, $defaultcustomdata);
00282     }
00283 }
00284 
00285 
00292 class enrol_manual_deleteselectedusers_operation extends enrol_bulk_enrolment_operation {
00293 
00299     public function get_identifier() {
00300         return 'deleteselectedusers';
00301     }
00302 
00309     public function get_title() {
00310         return get_string('deleteselectedusers', 'enrol_manual');
00311     }
00312 
00321     public function get_form($defaultaction = null, array $defaultcustomdata = null) {
00322         global $CFG;
00323         require_once($CFG->dirroot.'/enrol/manual/bulkchangeforms.php');
00324         if (!array($defaultcustomdata)) {
00325             $defaultcustomdata = array();
00326         }
00327         $defaultcustomdata['title'] = $this->get_title();
00328         $defaultcustomdata['message'] = get_string('confirmbulkdeleteenrolment', 'enrol_manual');
00329         $defaultcustomdata['button'] = get_string('unenrolusers', 'enrol_manual');
00330         return new enrol_manual_deleteselectedusers_form($defaultaction, $defaultcustomdata);
00331     }
00332 
00341     public function process(course_enrolment_manager $manager, array $users, stdClass $properties) {
00342         global $DB;
00343 
00344         if (!has_capability("enrol/manual:unenrol", $manager->get_context())) {
00345             return false;
00346         }
00347         foreach ($users as $user) {
00348             foreach ($user->enrolments as $enrolment) {
00349                 $plugin = $enrolment->enrolmentplugin;
00350                 $instance = $enrolment->enrolmentinstance;
00351                 if ($plugin->allow_unenrol_user($instance, $enrolment)) {
00352                     $plugin->unenrol_user($instance, $user->id);
00353                 }
00354             }
00355         }
00356         return true;
00357     }
00358 }
 All Data Structures Namespaces Files Functions Variables Enumerations