Exemplo n.º 1
0
/**
 * Sync all jwc course links.
 * @param int $courseid one course, empty mean all
 * @return void
 */
function enrol_jwc_sync($courseid = NULL)
{
    global $CFG, $DB;
    // unfortunately this may take a long time
    @set_time_limit(0);
    //if this fails during upgrade we can continue from cron, no big deal
    $jwc = new jwc_helper();
    $jwc_enrol = enrol_get_plugin('jwc');
    if (enrol_is_enabled('jwc')) {
        $params = array();
        $onecourse = "";
        if ($courseid) {
            $params['courseid'] = $courseid;
            $onecourse = "AND courseid = :courseid";
        }
        $select = "enrol = :jwc AND status = :status {$onecourse}";
        $params['jwc'] = 'jwc';
        $params['status'] = ENROL_INSTANCE_ENABLED;
        $instances = $DB->get_records_select('enrol', $select, $params);
        foreach ($instances as $instance) {
            // 课程必须有cas认证的教师
            $teachers = enrol_jwc_get_cas_teachers($instance->courseid);
            if (empty($teachers)) {
                $DB->set_field('enrol', 'customchar2', '此课程没有使用HITID的教师', array('id' => $instance->id));
                continue;
            }
            // 从教务处获取所有选修该课程的学生
            $return_msg = '';
            $students = $jwc->get_students($instance->customchar1, $teachers, $jwc_enrol->get_config('semester'), $return_msg);
            $DB->set_field('enrol', 'customchar2', $return_msg, array('id' => $instance->id));
            if (!$students) {
                // 出错
                continue;
                // skip this instance. 就算出错,也别清理选课,以免意外。管理员更改学期名时再清理所有选课
            }
            // 开始同步
            // 选课
            foreach ($students as $userid) {
                $jwc_enrol->enrol_user($instance, $userid, $instance->roleid);
            }
            // 取消教务处删除的选课
            if (empty($students)) {
                $where = "enrolid = {$instance->id}";
            } else {
                $where = "enrolid = {$instance->id} AND userid NOT IN (" . implode(',', $students) . ')';
            }
            $ues = $DB->get_records_select('user_enrolments', $where);
            foreach ($ues as $ue) {
                $jwc_enrol->unenrol_user($instance, $ue->userid);
            }
        }
    }
}
Exemplo n.º 2
0
 /**
  * Given a courseid this function returns true if the user is able to enrol or configure jwc
  *
  * @param int $courseid
  * @return bool
  */
 protected function can_add_new_instances($courseid)
 {
     global $DB, $CFG;
     $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
     if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/jwc:config', $coursecontext)) {
         return false;
     }
     require_once "{$CFG->dirroot}/enrol/jwc/locallib.php";
     if (!enrol_jwc_get_cas_teachers($courseid)) {
         // 课程中必须有使用cas认证的教师
         return false;
     }
     return true;
 }