예제 #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);
            }
        }
    }
}
 function validation($data, $files)
 {
     global $DB, $CFG, $COURSE;
     $errors = parent::validation($data, $files);
     require_once "{$CFG->dirroot}/enrol/jwc/locallib.php";
     $jwc = new jwc_helper();
     $errormsg = '';
     if (!$jwc->get_all_courses($data['coursenumber'], get_config('enrol_jwc', 'semester'), $errormsg)) {
         $errors['coursenumber'] = '在教务处查询此课程编号出错:' . $errormsg;
     }
     if ($DB->record_exists('enrol', array('enrol' => 'jwc', 'courseid' => $COURSE->id, 'customchar1' => $data['coursenumber']))) {
         $errors['coursenumber'] = '本课程已有一个教务处同步选课实例使用此课程编号';
     }
     return $errors;
 }
예제 #3
0
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
define('CLI_SCRIPT', true);
require dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php';
require_once $CFG->libdir . '/clilib.php';
// cli only functions
require_once "{$CFG->dirroot}/enrol/jwc/locallib.php";
$longoptions = array('xkid' => true);
$shortoptions = array('x' => 'xkid');
list($options, $unrecognized) = cli_get_params($longoptions, $shortoptions);
if (empty($unrecognized)) {
    $help = "Debug enrol jwc\n\nExample:\n\$sudo -u www-data /usr/bin/php enrol/jwc/cli/debug.php COURSE_NUMBER\n\$sudo -u www-data /usr/bin/php enrol/jwc/cli/debug.php -x xkid\n";
    echo $help;
    die;
}
$jwc = new jwc_helper();
$jwc_enrol = enrol_get_plugin('jwc');
if ($options['xkid']) {
    foreach ($unrecognized as $xkid) {
        $students = $jwc->get_all_students($xkid, $return_msg);
        foreach ($students as $student) {
            // print_r can not enum xml object
            $userid = $DB->get_field('user', 'id', array('auth' => 'cas', 'username' => $student->code, 'lastname' => $student->name));
            mtrace("{$student->code}\t{$student->name}\t[{$userid}]");
        }
        mtrace('Total: ' . count($students));
        mtrace($return_msg);
    }
} else {
    foreach ($unrecognized as $course_number) {
        $courses = $jwc->get_all_courses($course_number, $jwc_enrol->get_config('semester'), $return_msg);
예제 #4
0
 /**
  * Validates course edit form data
  *
  * @param object $instance enrol instance or null if does not exist yet
  * @param array $data
  * @param object $context context of existing course or parent category if course does not exist
  * @return array errors array
  */
 public function course_edit_validation($instance, array $data, $context)
 {
     global $CFG;
     $errors = array();
     if (!empty($data['idnumber'])) {
         require_once "{$CFG->dirroot}/enrol/jwc/locallib.php";
         $jwc = new jwc_helper();
         $errormsg = '';
         if (!$jwc->get_all_courses($data['idnumber'], $this->get_config('semester'), $errormsg)) {
             $errors['idnumber'] = '在教务处查询此课程编号出错:' . $errormsg . '(留空可跳过此检查)';
         }
     }
     return $errors;
 }
예제 #5
0
$action = optional_param('action', '', PARAM_ACTION);
$dryrun = optional_param('dryrun', 1, PARAM_BOOL);
$PAGE->set_url('/grade/export/jwc/index.php', array('id' => $id));
if (!($course = $DB->get_record('course', array('id' => $id)))) {
    print_error('nocourseid');
}
require_login($course);
$context = get_context_instance(CONTEXT_COURSE, $id);
require_capability('moodle/grade:export', $context);
require_capability('gradeexport/jwc:view', $context);
print_grade_page_head($COURSE->id, 'export', 'jwc', get_string('exportto', 'grades') . ' ' . get_string('pluginname', 'gradeexport_jwc'));
if (!empty($CFG->gradepublishing)) {
    $CFG->gradepublishing = has_capability('gradeexport/jwc:publish', $context);
}
$output = $PAGE->get_renderer('gradeexport_jwc');
$jwc = new jwc_helper();
if (!($semester = get_config('enrol_jwc', 'semester'))) {
    die('管理员未设置学期,请立刻通知他!');
}
// CAS教师?
if ($USER->auth != 'cas' or $USER->address != 1) {
    echo $output->require_cas();
    echo $output->footer();
    die;
}
// 课程编号必须有效
$errormsg = '';
$jwc_courses = $jwc->get_matched_courses($course->idnumber, array($USER), $semester, $errormsg);
if (empty($jwc_courses)) {
    echo $output->require_idnumber();
    echo $output->footer();