Пример #1
0
            $timeend = 0;
        } else {
            $timeend = $timestart + $duration * 24 * 60 * 60;
        }
        $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
        $instances = $manager->get_enrolment_instances();
        $plugins = $manager->get_enrolment_plugins(true);
        // Do not allow actions on disabled plugins.
        if (!array_key_exists($enrolid, $instances)) {
            throw new enrol_ajax_exception('invalidenrolinstance');
        }
        $instance = $instances[$enrolid];
        if (!isset($plugins[$instance->enrol])) {
            throw new enrol_ajax_exception('enrolnotpermitted');
        }
        $plugin = $plugins[$instance->enrol];
        if ($plugin->allow_enrol($instance) && has_capability('enrol/' . $plugin->get_name() . ':enrol', $context)) {
            $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
            if ($recovergrades) {
                require_once $CFG->libdir . '/gradelib.php';
                grade_recover_history_grades($user->id, $instance->courseid);
            }
        } else {
            throw new enrol_ajax_exception('enrolnotpermitted');
        }
        $outcome->success = true;
        break;
    default:
        throw new enrol_ajax_exception('unknowajaxaction');
}
echo json_encode($outcome);
Пример #2
0
 /**
  * Enrol user into course via enrol instance.
  *
  * @param stdClass $instance
  * @param int $userid
  * @param int $roleid optional role id
  * @param int $timestart 0 means unknown
  * @param int $timeend 0 means forever
  * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates
  * @param bool $recovergrades restore grade history
  * @return void
  */
 public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null)
 {
     global $DB, $USER, $CFG;
     // CFG necessary!!!
     if ($instance->courseid == SITEID) {
         throw new coding_exception('invalid attempt to enrol into frontpage course!');
     }
     $name = $this->get_name();
     $courseid = $instance->courseid;
     if ($instance->enrol !== $name) {
         throw new coding_exception('invalid enrol instance!');
     }
     $context = context_course::instance($instance->courseid, MUST_EXIST);
     if (!isset($recovergrades)) {
         $recovergrades = $CFG->recovergradesdefault;
     }
     $inserted = false;
     $updated = false;
     if ($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $userid))) {
         //only update if timestart or timeend or status are different.
         if ($ue->timestart != $timestart or $ue->timeend != $timeend or !is_null($status) and $ue->status != $status) {
             $ue->timestart = $timestart;
             $ue->timeend = $timeend;
             if (!is_null($status)) {
                 $ue->status = $status;
             }
             $ue->modifierid = $USER->id;
             $ue->timemodified = time();
             $DB->update_record('user_enrolments', $ue);
             $updated = true;
         }
     } else {
         $ue = new stdClass();
         $ue->enrolid = $instance->id;
         $ue->status = is_null($status) ? ENROL_USER_ACTIVE : $status;
         $ue->userid = $userid;
         $ue->timestart = $timestart;
         $ue->timeend = $timeend;
         $ue->modifierid = $USER->id;
         $ue->timecreated = time();
         $ue->timemodified = $ue->timecreated;
         $ue->id = $DB->insert_record('user_enrolments', $ue);
         $inserted = true;
     }
     if ($inserted) {
         // add extra info and trigger event
         $ue->courseid = $courseid;
         $ue->enrol = $name;
         events_trigger('user_enrolled', $ue);
     } else {
         if ($updated) {
             $ue->courseid = $courseid;
             $ue->enrol = $name;
             events_trigger('user_enrol_modified', $ue);
             // resets current enrolment caches
             $context->mark_dirty();
         }
     }
     if ($roleid) {
         // this must be done after the enrolment event so that the role_assigned event is triggered afterwards
         if ($this->roles_protected()) {
             role_assign($roleid, $userid, $context->id, 'enrol_' . $name, $instance->id);
         } else {
             role_assign($roleid, $userid, $context->id);
         }
     }
     // Recover old grades if present.
     if ($recovergrades) {
         require_once "{$CFG->libdir}/gradelib.php";
         grade_recover_history_grades($userid, $courseid);
     }
     // reset current user enrolment caching
     if ($userid == $USER->id) {
         if (isset($USER->enrol['enrolled'][$courseid])) {
             unset($USER->enrol['enrolled'][$courseid]);
         }
         if (isset($USER->enrol['tempguest'][$courseid])) {
             unset($USER->enrol['tempguest'][$courseid]);
             remove_temp_course_roles($context);
         }
     }
 }
 /**
  * Enrol user into course via enrol instance.
  *
  * @param stdClass $instance
  * @param int $userid
  * @param int $roleid optional role id
  * @param int $timestart 0 means unknown
  * @param int $timeend 0 means forever
  * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates
  * @param bool $recovergrades restore grade history
  * @return void
  */
 public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null)
 {
     global $DB, $USER, $CFG;
     // CFG necessary!!!
     if ($instance->courseid == SITEID) {
         throw new coding_exception('invalid attempt to enrol into frontpage course!');
     }
     $name = $this->get_name();
     $courseid = $instance->courseid;
     if ($instance->enrol !== $name) {
         throw new coding_exception('invalid enrol instance!');
     }
     $context = context_course::instance($instance->courseid, MUST_EXIST);
     if (!isset($recovergrades)) {
         $recovergrades = $CFG->recovergradesdefault;
     }
     $inserted = false;
     $updated = false;
     if ($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $userid))) {
         //only update if timestart or timeend or status are different.
         if ($ue->timestart != $timestart or $ue->timeend != $timeend or !is_null($status) and $ue->status != $status) {
             $this->update_user_enrol($instance, $userid, $status, $timestart, $timeend);
         }
     } else {
         $ue = new stdClass();
         $ue->enrolid = $instance->id;
         $ue->status = is_null($status) ? ENROL_USER_ACTIVE : $status;
         $ue->userid = $userid;
         $ue->timestart = $timestart;
         $ue->timeend = $timeend;
         $ue->modifierid = $USER->id;
         $ue->timecreated = time();
         $ue->timemodified = $ue->timecreated;
         $ue->id = $DB->insert_record('user_enrolments', $ue);
         $catalog_app_name = explode(".", $_SERVER["HTTP_HOST"]);
         $catalog_app_short_name = $catalog_app_name[0];
         $catalog_app_short_name = str_replace("http://", "", $catalog_app_short_name);
         $catalog_app_short_name = str_replace("https://", "", $catalog_app_short_name);
         $mdl_user = $CFG->current_app->getCurrentUser();
         $mhr_user = $mdl_user->getUserOnInstitution();
         $institution = $mdl_user->getInstitution();
         if (!$institution) {
             $institution = $CFG->current_app->getInstitution();
         }
         $wwwroot = $institution->getAppUrl(false);
         $current_app_name = explode(".", $wwwroot);
         $current_app_short_name = $current_app_name[0];
         $current_app_short_name = str_replace("http://", "", $current_app_short_name);
         $current_app_short_name = str_replace("https://", "", $current_app_short_name);
         $course_name = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
         $platform_user_id = $mhr_user->getObject()->id;
         $product_short_name = $_GET["product"];
         $enrol_obj = new GcrCourseEnrolments();
         $enrol_obj->setPlatformShortName($current_app_short_name);
         $enrol_obj->setProductShortName($product_short_name);
         $enrol_obj->setCatalogShortName($catalog_app_short_name);
         $enrol_obj->setCourseName($course_name->fullname);
         $enrol_obj->setUserId($platform_user_id);
         $enrol_obj->setCourseId($courseid);
         $enrol_obj->setEnrolledDate(time());
         $enrol_obj->save();
         $inserted = true;
     }
     if ($inserted) {
         // Trigger event.
         $event = \core\event\user_enrolment_created::create(array('objectid' => $ue->id, 'courseid' => $courseid, 'context' => $context, 'relateduserid' => $ue->userid, 'other' => array('enrol' => $name)));
         $event->trigger();
     }
     if ($roleid) {
         // this must be done after the enrolment event so that the role_assigned event is triggered afterwards
         if ($this->roles_protected()) {
             role_assign($roleid, $userid, $context->id, 'enrol_' . $name, $instance->id);
         } else {
             role_assign($roleid, $userid, $context->id);
         }
     }
     // Recover old grades if present.
     if ($recovergrades) {
         require_once "{$CFG->libdir}/gradelib.php";
         grade_recover_history_grades($userid, $courseid);
     }
     // reset current user enrolment caching
     if ($userid == $USER->id) {
         if (isset($USER->enrol['enrolled'][$courseid])) {
             unset($USER->enrol['enrolled'][$courseid]);
         }
         if (isset($USER->enrol['tempguest'][$courseid])) {
             unset($USER->enrol['tempguest'][$courseid]);
             remove_temp_course_roles($context);
         }
     }
 }
Пример #4
0
    /**
     * Enrol user into course via enrol instance.
     *
     * @param stdClass $instance
     * @param int $userid
     * @param int $roleid optional role id
     * @param int $timestart 0 means unknown
     * @param int $timeend 0 means forever
     * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates
     * @param bool $recovergrades restore grade history
     * @return void
     */
    public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null) {
        global $DB, $USER, $CFG; // CFG necessary!!!
        
        if ($instance->courseid == SITEID) {
            throw new coding_exception('invalid attempt to enrol into frontpage course!');
        }

        $name = $this->get_name();
        $courseid = $instance->courseid;

        if ($instance->enrol !== $name) {
            throw new coding_exception('invalid enrol instance!');
        }
        $context = context_course::instance($instance->courseid, MUST_EXIST);
        if (!isset($recovergrades)) {
            $recovergrades = $CFG->recovergradesdefault;
        }

        $inserted = false;
        $updated  = false;
        if ($ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
            //only update if timestart or timeend or status are different.
            if ($ue->timestart != $timestart or $ue->timeend != $timeend or (!is_null($status) and $ue->status != $status)) {
                $this->update_user_enrol($instance, $userid, $status, $timestart, $timeend);
            }
        } else {
            $ue = new stdClass();
            $ue->enrolid      = $instance->id;
            $ue->status       = is_null($status) ? ENROL_USER_ACTIVE : $status;
            $ue->userid       = $userid;
            $ue->timestart    = $timestart;
            $ue->timeend      = $timeend;
            $ue->modifierid   = $USER->id;
            $ue->timecreated  = time();
            $ue->timemodified = $ue->timecreated;
            $ue->id = $DB->insert_record('user_enrolments', $ue);

            $inserted = true;
        }
                 
        if ($inserted) {
   
                        /* Code stared by Raju T */
               //Code lines added for send email to manual enroled users when enrolment is manual and Send course welcome message is cheked 
               //The path is moodle2.8/enrol/manual/edit.php?courseid=
               
               if($instance->enrol =='manual'){
                   require_once($CFG->dirroot.'/lib/moodlelib.php');
                   require_once $CFG->dirroot.'/mod/facetoface/lib.php';
                   $local_batch_course=$DB->get_record_sql("SELECT lb.* FROM {local_batch_users} as lb where lb.userid=$userid and lb.courseid=$instance->courseid group by lb.id");
                    
                     if(empty($local_batch_course)){
                        
                            $sql ="SELECT course.*,
                                 ue.timecreated as time_enroled
                                 FROM 
                                  {course} AS course 
                                 JOIN {enrol} as en ON en.courseid=course.id and en.enrol='manual'
                                 JOIN {user_enrolments} AS ue ON ue.enrolid=en.id and ue.userid=$userid 
                                 WHERE  course.id=$instance->courseid";
                
                            $courses = $DB->get_record_sql($sql);
                            if(!empty($courses)){
                              $to_userids=$DB->get_record('user',array('id'=>$userid));
                              $userenrolments=array();
                              $userenrolments[]=$DB->get_record_sql("SELECT * FROM {user_enrolments}
                                                            where userid=$userid
                                                            and enrolid in (SELECT id FROM {enrol} where courseid=$instance->courseid and enrol='manual')");
                                $emai_users=email_send_users_list($userenrolments,$instance->courseid);
                                $to_emailid=$emai_users[0];
                                $to_userid=$emai_users[1];
           
                                if((!empty($to_emailid))&&(!empty($to_userid))){
                                    e_learning_and_request_course_emails($instance,$to_emailid,'manual','', $to_userid);
                                }
                            }
                    }
                }elseif($instance->enrol =='apply'){
                        require_once($CFG->dirroot.'/lib/moodlelib.php');
                        require_once $CFG->dirroot.'/mod/facetoface/lib.php';
                        $local_batch_course=$DB->get_record_sql("SELECT lb.* FROM {local_batch_users} as lb where lb.userid=$userid and lb.courseid=$instance->courseid group by lb.id");
                    
                     if(empty($local_batch_course)){
                          //print_object($instance);exit;
                        $sql ="SELECT course.*,
                                 ue.timecreated as time_enroled
                                 FROM 
                                  {course} AS course 
                                 JOIN {enrol} as en ON en.courseid=course.id and en.enrol='apply'
                                 JOIN {user_enrolments} AS ue ON ue.enrolid=en.id and ue.userid=$userid 
                                 WHERE  course.id=$instance->courseid";
                
                        $courses = $DB->get_record_sql($sql);
                        if(!empty($courses)){
                              $to_userids=$DB->get_record('user',array('id'=>$userid));  
                                $userenrolments=array();
                                $userenrolments[]=$DB->get_record_sql("SELECT * FROM {user_enrolments}
                                                            where userid=$userid
                                                            and enrolid in (SELECT id FROM {enrol} where courseid=$instance->courseid and enrol='apply')");
                                $emai_users=email_send_users_list($userenrolments,$instance->courseid);
                                $to_emailid=$emai_users[0];
                                $to_userid=$emai_users[1];
           
                                if((!empty($to_emailid))&&(!empty($to_userid))){
                                  e_learning_and_request_course_emails($instance,$to_emailid,'apply','',$to_userid);
                                   //e_learning_and_request_course_emails($instance,$userid,'apply');
                                } 
                        }
                     }
               }
               /* Code ends by Raju T */
      
            // Trigger event.
            $event = \core\event\user_enrolment_created::create(
                    array(
                        'objectid' => $ue->id,
                        'courseid' => $courseid,
                        'context' => $context,
                        'relateduserid' => $ue->userid,
                        'other' => array('enrol' => $name)
                        )
                    );
            $event->trigger();
        }

        if ($roleid) {
            // this must be done after the enrolment event so that the role_assigned event is triggered afterwards
            if ($this->roles_protected()) {
                role_assign($roleid, $userid, $context->id, 'enrol_'.$name, $instance->id);
            } else {
                role_assign($roleid, $userid, $context->id);
            }
        }

        // Recover old grades if present.
        if ($recovergrades) {
            require_once("$CFG->libdir/gradelib.php");
            grade_recover_history_grades($userid, $courseid);
        }

        // reset current user enrolment caching
        if ($userid == $USER->id) {
            if (isset($USER->enrol['enrolled'][$courseid])) {
                unset($USER->enrol['enrolled'][$courseid]);
            }
            if (isset($USER->enrol['tempguest'][$courseid])) {
                unset($USER->enrol['tempguest'][$courseid]);
                remove_temp_course_roles($context);
            }
        }
    }
Пример #5
0
 /**
  * Enrol user into course via enrol instance. Modified from enrollib due to check where $instance->enrol has to match the plugin name.
  *
  * @param stdClass $instance
  * @param int $userid
  * @param int $roleid optional role id
  * @param int $timestart 0 means unknown
  * @param int $timeend 0 means forever
  * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates
  * @param bool $recovergrades restore grade history
  * @return void
  */
 public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null)
 {
     global $DB, $USER, $CFG;
     // CFG necessary!!!
     if ($instance->courseid == SITEID) {
         throw new coding_exception('invalid attempt to enrol into frontpage course!');
     }
     $name = "self";
     // Have to force the name to return as 'self' in order to pass the check three lines down.
     $courseid = $instance->courseid;
     if ($instance->enrol !== $name) {
         throw new coding_exception('invalid enrol instance!');
     }
     $context = context_course::instance($instance->courseid, MUST_EXIST);
     if (!isset($recovergrades)) {
         $recovergrades = $CFG->recovergradesdefault;
     }
     $inserted = false;
     $updated = false;
     if ($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $userid))) {
         //only update if timestart or timeend or status are different.
         if ($ue->timestart != $timestart or $ue->timeend != $timeend or !is_null($status) and $ue->status != $status) {
             $this->update_user_enrol($instance, $userid, $status, $timestart, $timeend);
         }
     } else {
         $ue = new stdClass();
         $ue->enrolid = $instance->id;
         $ue->status = is_null($status) ? ENROL_USER_ACTIVE : $status;
         $ue->userid = $userid;
         $ue->timestart = $timestart;
         $ue->timeend = $timeend;
         $ue->modifierid = $USER->id;
         $ue->timecreated = time();
         $ue->timemodified = $ue->timecreated;
         $ue->id = $DB->insert_record('user_enrolments', $ue);
         $inserted = true;
     }
     if ($inserted) {
         // Trigger event.
         $event = \core\event\user_enrolment_created::create(array('objectid' => $ue->id, 'courseid' => $courseid, 'context' => $context, 'relateduserid' => $ue->userid, 'other' => array('enrol' => $name)));
         $event->trigger();
     }
     if ($roleid) {
         // this must be done after the enrolment event so that the role_assigned event is triggered afterwards
         if ($this->roles_protected()) {
             role_assign($roleid, $userid, $context->id, '');
         } else {
             role_assign($roleid, $userid, $context->id, '');
         }
     }
     // Recover old grades if present.
     if ($recovergrades) {
         require_once "{$CFG->libdir}/gradelib.php";
         grade_recover_history_grades($userid, $courseid);
     }
     // reset current user enrolment caching
     if ($userid == $USER->id) {
         if (isset($USER->enrol['enrolled'][$courseid])) {
             unset($USER->enrol['enrolled'][$courseid]);
         }
         if (isset($USER->enrol['tempguest'][$courseid])) {
             unset($USER->enrol['tempguest'][$courseid]);
             remove_temp_course_roles($context);
         }
     }
 }