michael-milette / moodle-filter_filtercodes

FilterCodes filter for Moodle enables content creators to easily customize and personalize course and site content using plain text tags (no HTML). For premium support, contact us at https://www.tngconsulting.ca/contact
https://moodle.org/plugins/filter_filtercodes
GNU General Public License v3.0
30 stars 42 forks source link

Feature Request: Add {ifcourseenrolmentactive ID} and {ifcourseenrolmentnotactive ID} #279

Open jshrek opened 7 months ago

jshrek commented 7 months ago

Prerequisites

Feature Request - The User Story

I would like the ability to check if the user is enrolled in another course (not the one they are currently viewing), by specify the Course ID. Note that the {ifenrolled} tag only checks the current course that the user is in, but does not allow for checking other course enrolments.

USE CASE: I sell Course 1 and Course 2 on my site. Customer can purchase Course 1, and once they pass the final quiz in Course 1, there is one more activity/page (still in Course 1) which provides them with a link to purchase Course 2. But if they are already enrolled in Course 2, then I want to hide the purchase link and instead show "You are already enrolled in Course 2".

Tag: {ifcourseenrolmentactive ID}..{/ifcourseenrolmentactive}. Description: Only allows one course ID to be specified. Will display content if user has an ACTIVE enrolment in the course specified by the course ID. An Active enrolment means the current date is within the users Enrolment Start/End Dates of the course, AND the users enrolment is not Suspended. Parameters: None. Requires content between tags.

Tag: {ifcourseenrolmentnotactive ID}..{/ifcourseenrolmentnotactive}. Description: Only allows one course ID to be specified. Will display content if user does NOT have an active enrolment in the course specified by the course ID. A non-active enrolment means that (i) user is not enrolled in the course, or (ii) user is enrolled in course, but the current date is outside the Enrolment Start/End Dates of the course, or (iii) user is enrolled in the course, but their enrolment is currently Suspended. Parameters: None. Requires content between tags.

Screenshots / Mock-ups

I have added the following code to filters.php and tested it and it works. I placed this code just before the {ifenrolled}..{/ifenrolled} tag code (but I do not think it matters where it goes):

        // Tag: {ifcourseenrolmentactive ID}..{/ifcourseenrolmentactive}.
        // Description: Only allows one course ID to be specified. Will display content if user has an ACTIVE enrolment in the course specified by the course ID. An Active enrolment means the current date is within the users Enrolment Start/End Dates of the course, AND the users enrolment is not Suspended.
        // Parameters: None.
        // Requires content between tags.
        // MOD by Jeff Sherk

        // Tag: {ifcourseenrolmentnotactive ID}..{/ifcourseenrolmentnotactive}.
        // Description: Only allows one course ID to be specified. Will display content if user does NOT have an active enrolment in the course specified by the course ID. A non-active enrolment means that (i) user is not enrolled in the course, or (ii) user is enrolled in course, but the current date is outside the Enrolment Start/End Dates of the course, or (iii) user is enrolled in the course, but their enrolment is currently Suspended.
        // Parameters: None.
        // Requires content between tags.
        // MOD by Jeff Sherk

        if ( stripos($text, '{ifcourseenrolmentactive ') !== false || stripos($text, '{ifcourseenrolmentnotactive ') !== false ) {

            // Course enrolment ACTIVE
            if (stripos($text, '{ifcourseenrolmentactive ') !== false) {
                // Course ID was specified
                preg_match_all('/\{ifcourseenrolmentactive ([0-9]+)\}/', $text, $matches);
                $courseid_array = array_unique($matches[1]);
                $courseid = $courseid_array[0]; // Which course ID was specified?
                $user_id = $USER->id;
                $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
                $is_active = is_enrolled( $coursecontext, $user_id, '', true); // true = return only Active users. 
                if ($is_active === true) {
                    // TRUE - This removes the tags but keeps the string content in between the tags.
                    $replace['/\{ifcourseenrolmentactive ' . $courseid . '\}/i'] = '';
                    $replace['/\{\/ifcourseenrolmentactive\}/i'] = '';
                } else {
                    // FALSE - This removes the tags and all the string content in between the tags as well.
                    $replace['/\{ifcourseenrolmentactive '.$courseid.'\}(.*)\{\/ifcourseenrolmentactive\}/isuU'] = '';
                }
            }

            // Course enrolment NOT active
            if (stripos($text, '{ifcourseenrolmentnotactive ') !== false) {
                // Course ID was specified
                preg_match_all('/\{ifcourseenrolmentnotactive ([0-9]+)\}/', $text, $matches);
                $courseid_array = array_unique($matches[1]);
                $courseid = $courseid_array[0]; // Which course ID was specified?
                $user_id = $USER->id;
                $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
                $is_active = is_enrolled( $coursecontext, $user_id, '', true); // true = return only Active users. 
                if ($is_active === true) {
                    // TRUE - This removes the tags and all the string content in between the tags as well.
                    $replace['/\{ifcourseenrolmentnotactive '.$courseid.'\}(.*)\{\/ifcourseenrolmentnotactive\}/isuU'] = '';
                } else {
                    // FALSE - This removes the tags but keeps the string content in between the tags.
                    $replace['/\{ifcourseenrolmentnotactive ' . $courseid . '\}/i'] = '';
                    $replace['/\{\/ifcourseenrolmentnotactive\}/i'] = '';
                }
            }

            // Unset some vars
            unset($matches, $course, $courseid_array, $courseid, $user_id, $coursecontext, $is_active);
        }

Alterative you have considered

No response

Additional information

No response

Planning on submitting a solution in a pull request (PR)?

Maybe

Code of Conduct