Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
7 tasks done
jshrek opened this issue Nov 24, 2023 · 0 comments
Open
7 tasks done

Comments

@jshrek
Copy link

jshrek commented Nov 24, 2023

Prerequisites

  • I am requesting the new feature to the correct repository.
  • I checked the latest version on GitHub and the README.md documentation to ensure that this functionality does not already exist.
  • I searched to make sure that this feature has not already been requested.
  • I have, or I know someone who has, the ability and willingness to test the new feature on a Moodle LMS-based site.
  • I agree to submit one feature request per issue ticket (do not submit multiple requests in one issue ticket).
  • I understand that development and support is provided by volunteer(s) and that all final implementation decisions are up TNG Consulting Inc.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant