|
| 1 | +DELIMITER $$ |
| 2 | +DROP trigger IF EXISTS `jol`.`tri_moodle` $$ |
| 3 | +create trigger tri_moodle |
| 4 | +after update on solution |
| 5 | +for each row |
| 6 | +begin |
| 7 | + declare mark int; |
| 8 | + declare total int; |
| 9 | + |
| 10 | + select count(1) into total from contest_problem where contest_id=new.contest_id; |
| 11 | + if total>0 then |
| 12 | + select sum(ac)/total*100 into mark |
| 13 | + from (select max(pass_rate) ac from solution where user_id=new.user_id and contest_id=new.contest_id and problem_id>0 group by problem_id) s; |
| 14 | + |
| 15 | + call update_moodle(new.contest_id,new.user_id,mark); |
| 16 | + |
| 17 | + end if; |
| 18 | +end $$ |
| 19 | + |
| 20 | + |
| 21 | +CREATE PROCEDURE `update_moodle`(IN `cid` INT, IN `user_id` VARCHAR(20), IN `mark` INT) |
| 22 | +top:BEGIN |
| 23 | + declare as_id int; |
| 24 | + declare u_id int; |
| 25 | + declare nowtime int; |
| 26 | + declare oldid int; |
| 27 | + set nowtime=UNIX_TIMESTAMP(now()); |
| 28 | + set as_id=0; |
| 29 | + select m.id into as_id from |
| 30 | + moodle.mdl_assign m |
| 31 | + where m.name = concat('[OJ]-C',cid); |
| 32 | + if as_id=0 then |
| 33 | + leave top; |
| 34 | + end if; |
| 35 | + set u_id =-1; |
| 36 | + select m.id into u_id from moodle.mdl_user m where username=user_id; |
| 37 | + select mag.grade into oldid from moodle.mdl_assign_grades mag |
| 38 | + where assignment=as_id and userid=u_id; |
| 39 | + |
| 40 | + set oldid=-1; |
| 41 | + |
| 42 | + select id into oldid from moodle.mdl_assign_submission m |
| 43 | + where assignment=as_id and userid=u_id; |
| 44 | + if oldid =-1 then |
| 45 | + |
| 46 | + insert into moodle.mdl_assign_submission |
| 47 | + (assignment,userid,timecreated,timemodified,status,attemptnumber) |
| 48 | + values( as_id ,u_id ,nowtime ,nowtime ,'new' ,0); |
| 49 | + insert into moodle.mdl_assign_grades(assignment,userid,timecreated,timemodified,grader,grade,attemptnumber) |
| 50 | + select ma.id,mas.userid,UNIX_TIMESTAMP( NOW( ) ),UNIX_TIMESTAMP( NOW( ) ),2,mark,0 |
| 51 | + from moodle.mdl_assign ma |
| 52 | + inner join moodle.mdl_assign_submission mas on |
| 53 | + mas.assignment=ma.id and mas.status='new' |
| 54 | + where concat(ma.id,',',mas.userid) not in (select concat(assignment,',',userid) from moodle.mdl_assign_grades); |
| 55 | + |
| 56 | + else |
| 57 | + |
| 58 | + |
| 59 | + update moodle.mdl_assign_grades |
| 60 | + set grade=mark,timemodified=nowtime where |
| 61 | + assignment=as_id and userid=u_id; |
| 62 | + |
| 63 | + end if; |
| 64 | + |
| 65 | + |
| 66 | +END$$ |
| 67 | + |
| 68 | + |
| 69 | +DELIMITER ; |
0 commit comments