Moodle
  1. Moodle
  2. MDL-28592

Create a manual_unenrol_users external function

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.0.2
    • Fix Version/s: DEV backlog
    • Component/s: Enrolments
    • Labels:
    • Database:
      Any
    • Affected Branches:
      MOODLE_20_STABLE
    • Rank:
      18272

      Description

      The enrolment web service has a way through the manual plugin to enroll users, but it doesn't have a way to unenroll users. I've been working on writing an unenrol user function in moodleDir/enrol/manual/externallib.php and I can post what I have if you all feel like this is something that should be implemented.

        Activity

        Hide
        Michael de Raadt added a comment -

        Hi, Craig.

        Yes, that sounds quite logical.

        Please share what you create.

        Show
        Michael de Raadt added a comment - Hi, Craig. Yes, that sounds quite logical. Please share what you create.
        Hide
        Craig Baker added a comment -

        I started out trying to use course_enrolment_manager->get_user_enrolment_components like what's shown in enrol/users.php in the unenrol case, but those functions weren't working. After some digging I discovered $CFG->enrol_plugins_enabled did not exist, even though the entry exists in the config table. I ended up doing it using straight records calls. Its probably not the best way of doing things, so any advice is appreciated.

        	public static function manual_unenrol_users_parameters() {
        		return new external_function_parameters(
        			array(
        			    'enrolments' => new external_multiple_structure(
        				    new external_single_structure(
        					    array(
        						'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'),
        						'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'),
        					    )
        				    )
        			    )
        			)
        		);
        	}
        
            /**
             * Enrolment of users
             * Function throw an exception at the first error encountered.
             * @param array $enrolments  An array of user enrolment
             * @return null
             */
            public static function manual_unenrol_users($enrolments) {
                global $DB, $CFG;
        
                require_once($CFG->libdir . '/enrollib.php');
        	require('../../config.php');
        
                $params = self::validate_parameters(self::manual_unenrol_users_parameters(),
                        array('enrolments' => $enrolments));
        
                $transaction = $DB->start_delegated_transaction(); //rollback all enrolment if an error occurs
                                                                   //(except if the DB doesn't support it)
        
                foreach ($params['enrolments'] as $enrolment) {
        
        		/*
        		I haven't tested the context check yet, 
        		but this is how I think it should look.
        		
        		// Ensure the current user is allowed to run this function in the enrolment context
        		$context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']);
        		self::validate_context($context);
        
        		//check that the user has the permission to manual enrol
        		require_capability('enrol/manual:unenrol', $context);
        		*/
        		$courseInfo = $DB->get_record('enrol', array('courseid'=>$enrolment['courseid'], 'enrol'=>'manual'), '*', MUST_EXIST); #course and manual enroll
        		$manualCourseID = $courseInfo->id; # number for course and manual enroll
        
        		$ue = $DB->get_record('user_enrolments', array('enrolid'=>$manualCourseID, 'userid'=>$enrolment['userid']));
        
        		$DB->delete_records('user_enrolments', array('id'=>$ue->id));
        
                }
        
                $transaction->allow_commit();
            }
        
            /**
             * Returns description of method result value
             * @return external_description
             */
            public static function manual_unenrol_users_returns() {
                return null;
            }
        
        
        Show
        Craig Baker added a comment - I started out trying to use course_enrolment_manager->get_user_enrolment_components like what's shown in enrol/users.php in the unenrol case, but those functions weren't working. After some digging I discovered $CFG->enrol_plugins_enabled did not exist, even though the entry exists in the config table. I ended up doing it using straight records calls. Its probably not the best way of doing things, so any advice is appreciated. public static function manual_unenrol_users_parameters() { return new external_function_parameters( array( 'enrolments' => new external_multiple_structure( new external_single_structure( array( 'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'), 'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'), ) ) ) ) ); } /** * Enrolment of users * Function throw an exception at the first error encountered. * @param array $enrolments An array of user enrolment * @ return null */ public static function manual_unenrol_users($enrolments) { global $DB, $CFG; require_once($CFG->libdir . '/enrollib.php'); require('../../config.php'); $params = self::validate_parameters(self::manual_unenrol_users_parameters(), array('enrolments' => $enrolments)); $transaction = $DB->start_delegated_transaction(); //rollback all enrolment if an error occurs //(except if the DB doesn't support it) foreach ($params['enrolments'] as $enrolment) { /* I haven't tested the context check yet, but this is how I think it should look. // Ensure the current user is allowed to run this function in the enrolment context $context = get_context_instance(CONTEXT_COURSE, $enrolment['courseid']); self::validate_context($context); //check that the user has the permission to manual enrol require_capability('enrol/manual:unenrol', $context); */ $courseInfo = $DB->get_record('enrol', array('courseid'=>$enrolment['courseid'], 'enrol'=>'manual'), '*', MUST_EXIST); #course and manual enroll $manualCourseID = $courseInfo->id; # number for course and manual enroll $ue = $DB->get_record('user_enrolments', array('enrolid'=>$manualCourseID, 'userid'=>$enrolment['userid'])); $DB->delete_records('user_enrolments', array('id'=>$ue->id)); } $transaction->allow_commit(); } /** * Returns description of method result value * @ return external_description */ public static function manual_unenrol_users_returns() { return null ; }
        Hide
        Richard Gillette added a comment -

        I tested the context check this morning and it works

        Show
        Richard Gillette added a comment - I tested the context check this morning and it works
        Hide
        Francois J added a comment -

        I am interested in this webservice function, what about its implementation?

        Show
        Francois J added a comment - I am interested in this webservice function, what about its implementation?
        Hide
        Jérôme Mouneyrac added a comment -

        This issue was assigned to me automatically, however I will not be able to work on this issue in the immediate future. In order to create a truer sense of the state of this issue and to allow other developers to have chance to become involved, I am removing myself as the assignee of this issue.
        For more information, see http://docs.moodle.org/dev/Changes_to_issue_assignment

        Show
        Jérôme Mouneyrac added a comment - This issue was assigned to me automatically, however I will not be able to work on this issue in the immediate future. In order to create a truer sense of the state of this issue and to allow other developers to have chance to become involved, I am removing myself as the assignee of this issue. For more information, see http://docs.moodle.org/dev/Changes_to_issue_assignment

          People

          • Votes:
            4 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated: