From c89c845a0e6675ca2c62f198a0087dcf4ad54b54 Mon Sep 17 00:00:00 2001 From: Johannes Burk Date: Thu, 26 May 2016 16:45:42 +0200 Subject: [PATCH] Do not allow manual enrolment and unenrolment, remove corresponding code. --- enroluser.php | 187 ------------------------------------------------ lib.php | 19 ++--- selectlib.php | 155 --------------------------------------- unenroluser.php | 97 ------------------------- 4 files changed, 7 insertions(+), 451 deletions(-) delete mode 100644 enroluser.php delete mode 100644 selectlib.php delete mode 100644 unenroluser.php diff --git a/enroluser.php b/enroluser.php deleted file mode 100644 index 901f135..0000000 --- a/enroluser.php +++ /dev/null @@ -1,187 +0,0 @@ -dirroot.'/enrol/apply/selectlib.php'); - -$enrolid = required_param('enrolid', PARAM_INT); -$roleid = optional_param('roleid', -1, PARAM_INT); -$extendperiod = optional_param('extendperiod', 0, PARAM_INT); -$extendbase = optional_param('extendbase', 3, PARAM_INT); - -$instance = $DB->get_record('enrol', array('id'=>$enrolid, 'enrol'=>'apply'), '*', MUST_EXIST); -$course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST); -$context = context_course::instance($course->id, MUST_EXIST); - -require_login($course); -//require_capability('enrol/manual:enrol', $context); -//require_capability('enrol/manual:manage', $context); -//require_capability('enrol/manual:unenrol', $context); - -if ($roleid < 0) { - $roleid = $instance->roleid; -} -$roles = get_assignable_roles($context); -$roles = array('0'=>get_string('none')) + $roles; - -if (!isset($roles[$roleid])) { - // weird - security always first! - $roleid = 0; -} - -if (!$enrol_manual = enrol_get_plugin('apply')) { - throw new coding_exception('Can not instantiate enrol_manual'); -} - -$instancename = $enrol_manual->get_instance_name($instance); - -$PAGE->set_url('/enrol/apply/enroluser.php', array('enrolid'=>$instance->id)); -$PAGE->set_pagelayout('admin'); -$PAGE->set_title($enrol_manual->get_instance_name($instance)); -$PAGE->set_heading($course->fullname); -navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id'=>$course->id))); - -// Create the user selector objects. -$options = array('enrolid' => $enrolid); - -$potentialuserselector = new enrol_apply_potential_participant('addselect', $options); -$currentuserselector = new enrol_apply_current_participant('removeselect', $options); - -// Build the list of options for the enrolment period dropdown. -$unlimitedperiod = get_string('unlimited'); -$periodmenu = array(); -for ($i=1; $i<=365; $i++) { - $seconds = $i * 86400; - $periodmenu[$seconds] = get_string('numdays', '', $i); -} -// Work out the apropriate default setting. -if ($extendperiod) { - $defaultperiod = $extendperiod; -} else { - $defaultperiod = $instance->enrolperiod; -} - -// Build the list of options for the starting from dropdown. -$timeformat = get_string('strftimedatefullshort'); -$today = time(); -$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); - -// enrolment start -$basemenu = array(); -if ($course->startdate > 0) { - $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $timeformat) . ')'; -} -$basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ; - -// process add and removes -if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { - $userstoassign = $potentialuserselector->get_selected_users(); - if (!empty($userstoassign)) { - foreach($userstoassign as $adduser) { - switch($extendbase) { - case 2: - $timestart = $course->startdate; - break; - case 3: - default: - $timestart = $today; - break; - } - - if ($extendperiod <= 0) { - $timeend = 0; - } else { - $timeend = $timestart + $extendperiod; - } - //echo "
";print_r($instance);exit();
-            $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
-
-            // Deprecated fixed by Shiro 
-            //add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
-            $context = context_course::instance($course->id);
-            \core\event\user_enrolment_created::create(array('context' => $context))->trigger();
-        }
-
-        $potentialuserselector->invalidate_selected_users();
-        $currentuserselector->invalidate_selected_users();
-
-        //TODO: log
-    }
-}
-
-// Process incoming role unassignments
-if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
-    $userstounassign = $currentuserselector->get_selected_users();
-    if (!empty($userstounassign)) {
-        foreach($userstounassign as $removeuser) {
-            $enrol_manual->unenrol_user($instance, $removeuser->id);
-
-            // Deprecated fixed by Shiro 
-            //add_to_log($course->id, 'course', 'unenrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
-            $context = context_course::instance($course->id);
-            \core\event\user_enrolment_deleted::delete(array('context' => $context))->trigger();
-        }
-
-        $potentialuserselector->invalidate_selected_users();
-        $currentuserselector->invalidate_selected_users();
-
-        //TODO: log
-    }
-}
-
-
-echo $OUTPUT->header();
-echo $OUTPUT->heading($instancename);
-
-?>
-
- - - - - - - - -
-

- display() ?> -
-
-
- -
- -


-

- -


-

- -


-

- -
-
- -
- -
-
-

- display() ?> -
-
-footer(); diff --git a/lib.php b/lib.php index 3bc16d2..7a9196f 100644 --- a/lib.php +++ b/lib.php @@ -118,11 +118,6 @@ class enrol_apply_plugin extends enrol_plugin { $icons[] = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('confirmenrol', 'enrol_apply'), 'core', array('class'=>'iconsmall'))); } - if (has_capability("enrol/apply:enrol", $context)) { - $enrollink = new moodle_url("/enrol/apply/enroluser.php", array('enrolid'=>$instance->id)); - $icons[] = $OUTPUT->action_icon($enrollink, new pix_icon('t/enrolusers', get_string('enrolusers', 'enrol_apply'), 'core', array('class'=>'iconsmall'))); - } - return $icons; } @@ -175,13 +170,13 @@ class enrol_apply_plugin extends enrol_plugin { $instance = $ue->enrolmentinstance; $params = $manager->get_moodlepage()->url->params(); $params['ue'] = $ue->id; - if ($this->allow_unenrol($instance) && has_capability("enrol/apply:unenrol", $context)) { - $url = new moodle_url('/enrol/apply/unenroluser.php', $params); - $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id)); - } - if ($this->allow_manage($instance) && has_capability("enrol/apply:manage", $context)) { - $url = new moodle_url('/enrol/apply/editenrolment.php', $params); - $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id)); + if ($this->allow_unenrol_user($instance, $ue) && has_capability("enrol/apply:unenrol", $context)) { + $url = new moodle_url('/enrol/unenroluser.php', $params); + $actions[] = new user_enrolment_action( + new pix_icon('t/delete', ''), + get_string('unenrol', 'enrol'), + $url, + array('class' => 'unenrollink', 'rel' => $ue->id)); } return $actions; } diff --git a/selectlib.php b/selectlib.php deleted file mode 100644 index f162cab..0000000 --- a/selectlib.php +++ /dev/null @@ -1,155 +0,0 @@ -. - -/** - * Auxiliary manual user enrolment lib, the main purpose is to lower memory requirements... - * - * @package enrol - * @subpackage manual - * @copyright 2010 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->dirroot . '/user/selector/lib.php'); - -/** - * Enrol candidates - */ -class enrol_apply_potential_participant extends user_selector_base { - protected $enrolid; - - public function __construct($name, $options) { - $this->enrolid = $options['enrolid']; - parent::__construct($name, $options); - } - - /** - * Candidate users - * @param $search - * @return array - */ - public function find_users($search) { - global $DB; - //by default wherecondition retrieves all users except the deleted, not confirmed and guest - list($wherecondition, $params) = $this->search_sql($search, 'u'); - $params['enrolid'] = $this->enrolid; - - $fields = 'SELECT ' . $this->required_fields_sql('u'); - $countfields = 'SELECT COUNT(1)'; - - $sql = " FROM {user} u - WHERE $wherecondition - AND u.id NOT IN (SELECT ue.userid - FROM {user_enrolments} ue - JOIN {enrol} e ON (e.id = ue.enrolid AND e.id = :enrolid))"; - - $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; - - if (!$this->is_validating()) { - $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); - if ($potentialmemberscount > 100) { - return $this->too_many_results($search, $potentialmemberscount); - } - } - - $availableusers = $DB->get_records_sql($fields . $sql . $order, $params); - - if (empty($availableusers)) { - return array(); - } - - - if ($search) { - $groupname = get_string('enrolcandidatesmatching', 'enrol', $search); - } else { - $groupname = get_string('enrolcandidates', 'enrol'); - } - - return array($groupname => $availableusers); - } - - protected function get_options() { - $options = parent::get_options(); - $options['enrolid'] = $this->enrolid; - $options['file'] = 'enrol/apply/selectlib.php'; - return $options; - } -} - -/** - * Enroled users - */ -class enrol_apply_current_participant extends user_selector_base { - protected $courseid; - protected $enrolid; - - public function __construct($name, $options) { - $this->enrolid = $options['enrolid']; - parent::__construct($name, $options); - } - - /** - * Candidate users - * @param $search - * @return array - */ - public function find_users($search) { - global $DB; - //by default wherecondition retrieves all users except the deleted, not confirmed and guest - list($wherecondition, $params) = $this->search_sql($search, 'u'); - $params['enrolid'] = $this->enrolid; - - $fields = 'SELECT ' . $this->required_fields_sql('u'); - $countfields = 'SELECT COUNT(1)'; - - $sql = " FROM {user} u - JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid) - WHERE $wherecondition"; - - $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; - - if (!$this->is_validating()) { - $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); - if ($potentialmemberscount > 100) { - return $this->too_many_results($search, $potentialmemberscount); - } - } - - $availableusers = $DB->get_records_sql($fields . $sql . $order, $params); - - if (empty($availableusers)) { - return array(); - } - - - if ($search) { - $groupname = get_string('enrolledusersmatching', 'enrol', $search); - } else { - $groupname = get_string('enrolledusers', 'enrol'); - } - - return array($groupname => $availableusers); - } - - protected function get_options() { - $options = parent::get_options(); - $options['enrolid'] = $this->enrolid; - $options['file'] = 'enrol/apply/apply_form.php'; - return $options; - } -} diff --git a/unenroluser.php b/unenroluser.php deleted file mode 100644 index f2b1912..0000000 --- a/unenroluser.php +++ /dev/null @@ -1,97 +0,0 @@ -dirroot/enrol/renderer.php"); - -$ueid = required_param('ue', PARAM_INT); // user enrolment id -$filter = optional_param('ifilter', 0, PARAM_INT); -$confirm = optional_param('confirm', false, PARAM_BOOL); - -// Get the user enrolment object -$ue = $DB->get_record('user_enrolments', array('id' => $ueid), '*', MUST_EXIST); -// Get the user for whom the enrolment is -$user = $DB->get_record('user', array('id'=>$ue->userid), '*', MUST_EXIST); -// Get the course the enrolment is to -//list($ctxsql, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); -$ctxsql = context_helper::get_preload_record_columns_sql('ctx'); -$sql = "SELECT c.*, $ctxsql - FROM {course} c - LEFT JOIN {enrol} e ON e.courseid = c.id - LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextcourse) - WHERE e.id = :enrolid"; -$params = array('enrolid' => $ue->enrolid, 'contextcourse' => CONTEXT_COURSE); -$course = $DB->get_record_sql($sql, $params, MUST_EXIST); -context_helper::preload_from_record($course); - - -// Make sure it's not the front page -if ($course->id == SITEID) { - redirect(new moodle_url('/')); -} - -// Obviously -require_login($course); -// Make sure the user can unenrol users. -require_capability("enrol/apply:unenrol", context_course::instance($course->id)); - -// Get the enrolment manager for this course -$manager = new course_enrolment_manager($PAGE, $course, $filter); -// Get an enrolment users table object. Doign this will automatically retrieve the the URL params -// relating to table the user was viewing before coming here, and allows us to return the user to the -// exact page of the users screen they can from. -$table = new course_enrolment_users_table($manager, $PAGE); - -// The URL of the enrolled users page for the course. -$usersurl = new moodle_url('/enrol/users.php', array('id' => $course->id)); -// The URl to return the user too after this screen. -$returnurl = new moodle_url($usersurl, $manager->get_url_params()+$table->get_url_params()); -// The URL of this page -$url = new moodle_url('/enrol/apply/unenroluser.php', $returnurl->params()); -$url->param('ue', $ueid); - -$PAGE->set_url($url); -$PAGE->set_pagelayout('admin'); -navigation_node::override_active_url($usersurl); - -list($instance, $plugin) = $manager->get_user_enrolment_components($ue); -if (!$plugin->allow_unenrol($instance) || $instance->enrol != 'apply' || !($plugin instanceof enrol_apply_plugin)) { - print_error('erroreditenrolment', 'enrol'); -} - -// If the unenrolment has been confirmed and the sesskey is valid unenrol the user. -if ($confirm && confirm_sesskey() && $manager->unenrol_user($ue)) { - redirect($returnurl); -} - -$yesurl = new moodle_url($PAGE->url, array('confirm'=>1, 'sesskey'=>sesskey())); -$message = get_string('unenroluser', 'enrol_self', array('user' => fullname($user, true), 'course' => format_string($course->fullname))); -$fullname = fullname($user); -$title = get_string('unenrol', 'enrol_self'); - -$PAGE->set_title($title); -$PAGE->set_heading($title); -$PAGE->navbar->add($title); -$PAGE->navbar->add($fullname); - -echo $OUTPUT->header(); -echo $OUTPUT->heading($fullname); -echo $OUTPUT->confirm($message, $yesurl, $returnurl); -echo $OUTPUT->footer();