Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 94... Línea 94...
94
define('ENROL_ACTION_UNENROL', 'unenrol');
94
define('ENROL_ACTION_UNENROL', 'unenrol');
Línea 95... Línea 95...
95
 
95
 
96
/**
96
/**
97
 * Returns instances of enrol plugins
97
 * Returns instances of enrol plugins
98
 * @param bool $enabled return enabled only
98
 * @param bool $enabled return enabled only
99
 * @return array of enrol plugins name=>instance
99
 * @return enrol_plugin[] array of enrol plugins name=>instance
100
 */
100
 */
101
function enrol_get_plugins($enabled) {
101
function enrol_get_plugins($enabled) {
Línea 102... Línea 102...
102
    global $CFG;
102
    global $CFG;
Línea 807... Línea 807...
807
                $params = array_merge($params, $allowedparams);
807
                $params = array_merge($params, $allowedparams);
808
            }
808
            }
809
        }
809
        }
810
    }
810
    }
Línea 811... Línea 811...
811
 
811
 
812
    // Note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why
812
    // Note: we can not use DISTINCT + text fields due to MS limitations, that is why
813
    // we have the subselect there.
813
    // we have the subselect there.
814
    $sql = "SELECT $coursefields $ccselect $timeaccessselect
814
    $sql = "SELECT $coursefields $ccselect $timeaccessselect
815
              FROM {course} c
815
              FROM {course} c
816
              JOIN ($courseidsql) en ON (en.courseid = c.id)
816
              JOIN ($courseidsql) en ON (en.courseid = c.id)
Línea 847... Línea 847...
847
 *
847
 *
848
 * @param object $course
848
 * @param object $course
849
 * @param array $instances enrol instances of this course, improves performance
849
 * @param array $instances enrol instances of this course, improves performance
850
 * @return array of pix_icon
850
 * @return array of pix_icon
851
 */
851
 */
852
function enrol_get_course_info_icons($course, array $instances = NULL) {
852
function enrol_get_course_info_icons($course, ?array $instances = NULL) {
853
    $icons = array();
853
    $icons = array();
854
    if (is_null($instances)) {
854
    if (is_null($instances)) {
855
        $instances = enrol_get_instances($course->id, true);
855
        $instances = enrol_get_instances($course->id, true);
856
    }
856
    }
857
    $plugins = enrol_get_plugins(true);
857
    $plugins = enrol_get_plugins(true);
Línea 1109... Línea 1109...
1109
 
1109
 
Línea 1110... Línea 1110...
1110
    $params = [];
1110
    $params = [];
1111
 
1111
 
1112
    if ($onlyactive) {
1112
    if ($onlyactive) {
1113
        $subwhere = "WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
-
 
1114
        $params['now1']    = round(time(), -2); // improves db caching
1113
        $subwhere = "WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
1115
        $params['now2']    = $params['now1'];
1114
        $params['now1']    = $params['now2'] = \core\di::get(\core\clock::class)->time();
1116
        $params['active']  = ENROL_USER_ACTIVE;
1115
        $params['active']  = ENROL_USER_ACTIVE;
1117
        $params['enabled'] = ENROL_INSTANCE_ENABLED;
1116
        $params['enabled'] = ENROL_INSTANCE_ENABLED;
1118
    } else {
1117
    } else {
Línea 1122... Línea 1121...
1122
    $coursefields = 'c.' .join(',c.', $fields);
1121
    $coursefields = 'c.' .join(',c.', $fields);
1123
    $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
1122
    $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
1124
    $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
1123
    $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
1125
    $params['contextlevel'] = CONTEXT_COURSE;
1124
    $params['contextlevel'] = CONTEXT_COURSE;
Línea 1126... Línea 1125...
1126
 
1125
 
1127
    //note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
1126
    //note: we can not use DISTINCT + text fields due to MS limitations, that is why we have the subselect there
1128
    $sql = "SELECT $coursefields $ccselect
1127
    $sql = "SELECT $coursefields $ccselect
1129
              FROM {course} c
1128
              FROM {course} c
1130
              JOIN (SELECT DISTINCT e.courseid
1129
              JOIN (SELECT DISTINCT e.courseid
1131
                      FROM {enrol} e
1130
                      FROM {enrol} e
Línea 1893... Línea 1892...
1893
    }
1892
    }
Línea 1894... Línea 1893...
1894
 
1893
 
1895
    /**
1894
    /**
1896
     * Returns localised name of enrol instance
1895
     * Returns localised name of enrol instance
1897
     *
1896
     *
1898
     * @param object $instance (null is accepted too)
1897
     * @param stdClass|null $instance (null is accepted too)
1899
     * @return string
1898
     * @return string
1900
     */
1899
     */
1901
    public function get_instance_name($instance) {
1900
    public function get_instance_name($instance) {
1902
        if (empty($instance->name)) {
1901
        if (empty($instance->name)) {
Línea 1907... Línea 1906...
1907
            return format_string($instance->name, true, array('context'=>$context));
1906
            return format_string($instance->name, true, array('context'=>$context));
1908
        }
1907
        }
1909
    }
1908
    }
Línea 1910... Línea 1909...
1910
 
1909
 
-
 
1910
    /**
-
 
1911
     * How this enrolment method should be displayed on the "Enrolment methods" page
-
 
1912
     *
-
 
1913
     * Some plugins may choose to add more information, for example, user role, dates, etc.
-
 
1914
     *
-
 
1915
     * @param stdClass $instance
-
 
1916
     * @return string
-
 
1917
     */
-
 
1918
    public function get_instance_name_for_management_page(stdClass $instance): string {
-
 
1919
        return (string)$this->get_instance_name($instance);
-
 
1920
    }
-
 
1921
 
1911
    /**
1922
    /**
1912
     * Returns optional enrolment information icons.
1923
     * Returns optional enrolment information icons.
1913
     *
1924
     *
1914
     * This is used in course list for quick overview of enrolment options.
1925
     * This is used in course list for quick overview of enrolment options.
1915
     *
1926
     *
Línea 2157... Línea 2168...
2157
 
2168
 
2158
        // Dispatch the hook for post enrol user actions.
2169
        // Dispatch the hook for post enrol user actions.
2159
        $hook = new \core_enrol\hook\after_user_enrolled(
2170
        $hook = new \core_enrol\hook\after_user_enrolled(
2160
            enrolinstance: $instance,
2171
            enrolinstance: $instance,
-
 
2172
            userenrolmentinstance: $ue,
2161
            userenrolmentinstance: $ue,
2173
            roleid: $roleid,
2162
        );
2174
        );
Línea 2163... Línea 2175...
2163
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
2175
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
2164
 
2176
 
Línea 2433... Línea 2445...
2433
        // override for most plugins, check if instance already exists in cases only one instance is supported
2445
        // override for most plugins, check if instance already exists in cases only one instance is supported
2434
        return NULL;
2446
        return NULL;
2435
    }
2447
    }
Línea 2436... Línea 2448...
2436
 
2448
 
2437
    /**
-
 
2438
     * @deprecated since Moodle 2.8 MDL-35864 - please use can_delete_instance() instead.
-
 
2439
     */
-
 
2440
    public function instance_deleteable($instance) {
-
 
2441
        throw new coding_exception('Function enrol_plugin::instance_deleteable() is deprecated, use
-
 
2442
                enrol_plugin::can_delete_instance() instead');
-
 
2443
    }
-
 
2444
 
-
 
2445
    /**
2449
    /**
2446
     * Is it possible to delete enrol instance via standard UI?
2450
     * Is it possible to delete enrol instance via standard UI?
2447
     *
2451
     *
2448
     * @param stdClass  $instance
2452
     * @param stdClass  $instance
2449
     * @return bool
2453
     * @return bool
Línea 2592... Línea 2596...
2592
     * Add new instance of enrol plugin.
2596
     * Add new instance of enrol plugin.
2593
     * @param object $course
2597
     * @param object $course
2594
     * @param array instance fields
2598
     * @param array instance fields
2595
     * @return int id of new instance, null if can not be created
2599
     * @return int id of new instance, null if can not be created
2596
     */
2600
     */
2597
    public function add_instance($course, array $fields = NULL) {
2601
    public function add_instance($course, ?array $fields = NULL) {
2598
        global $DB;
2602
        global $DB;
Línea 2599... Línea 2603...
2599
 
2603
 
2600
        if ($course->id == SITEID) {
2604
        if ($course->id == SITEID) {
2601
            throw new coding_exception('Invalid request to add enrol instance to frontpage.');
2605
            throw new coding_exception('Invalid request to add enrol instance to frontpage.');
Línea 2725... Línea 2729...
2725
        // Invalidate all enrol caches.
2729
        // Invalidate all enrol caches.
2726
        $context->mark_dirty();
2730
        $context->mark_dirty();
2727
    }
2731
    }
Línea 2728... Línea 2732...
2728
 
2732
 
2729
    /**
-
 
2730
     * Update instance members.
-
 
2731
     *
-
 
2732
     * Update communication room membership for an instance action being performed.
-
 
2733
     *
-
 
2734
     * @param int $enrolmentinstanceid ID of the enrolment instance
-
 
2735
     * @param string $action The update action being performed
-
 
2736
     * @param stdClass $course The course object
-
 
2737
     * @return void
2733
    /**
2738
     * @deprecated Since Moodle 4.4.0.
-
 
2739
     * @see \core_communication\hook_listener::update_communication_memberships_for_enrol_status_change()
-
 
2740
     * @todo MDL-80491 Final deprecation in Moodle 4.8.
-
 
2741
     *
2734
     * @deprecated Since Moodle 4.4.0.
2742
     */
2735
     */
2743
    public function update_communication(int $enrolmentinstanceid, string $action, stdClass $course): void {
2736
    #[\core\attribute\deprecated(null, reason: 'Replaced with hooks', since: '4.4', mdl: 'MDL-78551', final: true)]
2744
        debugging('Use of method update_communication is deprecated. This feature has been moved to
-
 
2745
        core_communication as a part of hooks api implementation so that plugins or core does not need to call this method anymore.
2737
    public function update_communication(): void {
2746
        Method update_communication_memberships_for_enrol_status_change method in communication/classes/hook_listener.php
-
 
2747
        now handles all the operations related to this method using hooks callback recorded in lib/db/hooks.php.', DEBUG_DEVELOPER);
-
 
2748
        return;
2738
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
Línea 2749... Línea 2739...
2749
    }
2739
    }
2750
 
2740
 
2751
    /**
2741
    /**
Línea 2792... Línea 2782...
2792
        // Invalidate all enrol caches.
2782
        // Invalidate all enrol caches.
2793
        $context->mark_dirty();
2783
        $context->mark_dirty();
2794
    }
2784
    }
Línea 2795... Línea 2785...
2795
 
2785
 
2796
    /**
2786
    /**
-
 
2787
     * Creates a widget to display on the course enrolment page. It can also redirect.
-
 
2788
     *
-
 
2789
     * It is recommended that all plugins use the same template for the consistent output. Example:
2797
     * Creates course enrol form, checks if form submitted
2790
     *
-
 
2791
     *     $obj = new \core_enrol\output\enrol_page($instance, ...);
2798
     * and enrols user if necessary. It can also redirect.
2792
     *     return $OUTPUT->render($obj);
2799
     *
2793
     *
2800
     * @param stdClass $instance
2794
     * @param stdClass $instance
2801
     * @return string html text, usually a form in a text box
2795
     * @return string|null html to display on the enrolment page
2802
     */
2796
     */
2803
    public function enrol_page_hook(stdClass $instance) {
2797
    public function enrol_page_hook(stdClass $instance) {
2804
        return null;
2798
        return null;
Línea 3605... Línea 3599...
3605
        // Send as the first user assigned as the course contact.
3599
        // Send as the first user assigned as the course contact.
3606
        if ($sendoption === ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) {
3600
        if ($sendoption === ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) {
3607
            $rusers = [];
3601
            $rusers = [];
3608
            if (!empty($CFG->coursecontact)) {
3602
            if (!empty($CFG->coursecontact)) {
3609
                $croles = explode(',', $CFG->coursecontact);
3603
                $croles = explode(',', $CFG->coursecontact);
3610
                [$sort, $sortparams] = users_order_by_sql('u');
3604
                [$sort] = users_order_by_sql('u');
3611
                // We only use the first user.
3605
                // We only use the first user.
3612
                $i = 0;
3606
                $i = 0;
3613
                do {
3607
                do {
3614
                    $userfieldsapi = \core_user\fields::for_name();
3608
                    $userfieldsapi = \core_user\fields::for_name();
3615
                    $allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
3609
                    $allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
3616
                    $rusers = get_role_users($croles[$i], $context, true, 'u.id,  u.confirmed, u.username, '. $allnames . ',
3610
                    $rusers = get_role_users($croles[$i], $context, true,
3617
                    u.email, r.sortorder, ra.id AS raid', 'r.sortorder, ra.id ASC, ' . $sort, null, '', '', '', '', $sortparams);
3611
                        "u.id, u.confirmed, u.username, {$allnames}, u.email, u.maildisplay, r.sortorder, ra.id AS raid",
-
 
3612
                        "r.sortorder, ra.id ASC, {$sort}");
3618
                    $i++;
3613
                    $i++;
3619
                } while (empty($rusers) && !empty($croles[$i]));
3614
                } while (empty($rusers) && !empty($croles[$i]));
3620
            }
3615
            }
3621
            if ($rusers) {
3616
            if ($rusers) {
3622
                $contact = array_values($rusers)[0];
3617
                $contact = array_values($rusers)[0];
Línea 3642... Línea 3637...
3642
     *
3637
     *
3643
     * @param stdClass $instance Enrol instance.
3638
     * @param stdClass $instance Enrol instance.
3644
     * @param int $userid User ID.
3639
     * @param int $userid User ID.
3645
     * @param int $sendoption Send email from constant ENROL_SEND_EMAIL_FROM_*
3640
     * @param int $sendoption Send email from constant ENROL_SEND_EMAIL_FROM_*
3646
     * @param null|string $message Message to send to the user.
3641
     * @param null|string $message Message to send to the user.
-
 
3642
     * @param int|null $roleid The assigned role ID
3647
     */
3643
     */
3648
    public function send_course_welcome_message_to_user(
3644
    public function send_course_welcome_message_to_user(
3649
        stdClass $instance,
3645
        stdClass $instance,
3650
        int $userid,
3646
        int $userid,
3651
        int $sendoption,
3647
        int $sendoption,
3652
        ?string $message = '',
3648
        ?string $message = '',
-
 
3649
        ?int $roleid = null,
3653
    ): void {
3650
    ): void {
3654
        global $DB;
3651
        global $DB, $CFG;
-
 
3652
        require_once($CFG->dirroot . '/course/lib.php');
-
 
3653
 
3655
        $context = context_course::instance($instance->courseid);
3654
        $context = context_course::instance($instance->courseid);
3656
        $user = core_user::get_user($userid);
3655
        $user = core_user::get_user($userid);
3657
        $course = get_course($instance->courseid);
3656
        $course = get_course($instance->courseid);
-
 
3657
 
3658
        $courserole = $DB->get_field(
3658
        // Fallback to the instance role ID if parameter not specified.
3659
            table: 'role',
-
 
3660
            return: 'shortname',
3659
        $courseroleid = $roleid ?: $instance->roleid;
3661
            conditions: ['id' => $instance->roleid],
3660
        $courserole = $DB->get_record('role', ['id' => $courseroleid]);
3662
        );
-
 
Línea 3663... Línea 3661...
3663
 
3661
 
3664
        $a = new stdClass();
3662
        $a = new stdClass();
-
 
3663
        $a->coursename = format_string($course->fullname, true, ['context' => $context, 'escape' => false]);
3665
        $a->coursename = format_string($course->fullname, true, ['context' => $context, 'escape' => false]);
3664
        $a->courselink = course_get_url($course)->out();
3666
        $a->profileurl = (new moodle_url(
3665
        $a->profileurl = (new moodle_url(
3667
            url: '/user/view.php',
3666
            url: '/user/view.php',
3668
            params: [
3667
            params: [
3669
                'id' => $user->id,
3668
                'id' => $user->id,
3670
                'course' => $instance->courseid,
3669
                'course' => $instance->courseid,
3671
            ],
3670
            ],
-
 
3671
        ))->out();
-
 
3672
 
-
 
3673
        $placeholders = \core_user::get_name_placeholders($user);
3672
        ))->out();
3674
        foreach ($placeholders as $field => $value) {
-
 
3675
            $a->{$field} = $value;
Línea 3673... Línea 3676...
3673
        $a->fullname = fullname($user);
3676
        }
3674
 
3677
 
3675
        if ($message && trim($message) !== '') {
3678
        if ($message && trim($message) !== '') {
-
 
3679
            $placeholders = [
3676
            $placeholders = [
3680
                '{$a->coursename}',
3677
                '{$a->coursename}',
3681
                '{$a->courselink}',
3678
                '{$a->profileurl}',
3682
                '{$a->profileurl}',
3679
                '{$a->fullname}',
3683
                '{$a->fullname}',
3680
                '{$a->email}',
3684
                '{$a->email}',
3681
                '{$a->firstname}',
3685
                '{$a->firstname}',
3682
                '{$a->lastname}',
3686
                '{$a->lastname}',
3683
                '{$a->courserole}',
3687
                '{$a->courserole}',
3684
            ];
3688
            ];
-
 
3689
            $values = [
3685
            $values = [
3690
                $a->coursename,
3686
                $a->coursename,
3691
                $a->courselink,
3687
                $a->profileurl,
3692
                $a->profileurl,
3688
                fullname($user),
3693
                fullname($user),
3689
                $user->email,
3694
                $user->email,
3690
                $user->firstname,
3695
                $user->firstname,
3691
                $user->lastname,
3696
                $user->lastname,
3692
                $courserole,
3697
                role_get_name($courserole, $context),
3693
            ];
3698
            ];
3694
            $message = str_replace($placeholders, $values, $message);
3699
            $message = str_replace($placeholders, $values, $message);
3695
            if (strpos($message, '<') === false) {
3700
            if (strpos($message, '<') === false) {
Línea 3726... Línea 3731...
3726
        $message->fullmessage = $messagetext;
3731
        $message->fullmessage = $messagetext;
3727
        $message->fullmessageformat = FORMAT_MARKDOWN;
3732
        $message->fullmessageformat = FORMAT_MARKDOWN;
3728
        $message->fullmessagehtml = $messagehtml;
3733
        $message->fullmessagehtml = $messagehtml;
3729
        $message->notification = 1;
3734
        $message->notification = 1;
3730
        $message->contexturl = $a->profileurl;
3735
        $message->contexturl = $a->profileurl;
3731
        $message->contexturlname = $course->fullname;
3736
        $message->contexturlname = $a->coursename;
Línea 3732... Línea 3737...
3732
 
3737
 
3733
        message_send($message);
3738
        message_send($message);
Línea 3734... Línea 3739...
3734
    }
3739
    }