
jbe@102: BEGIN;
jbe@102:  
jbe@102: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
jbe@102:   SELECT * FROM (VALUES ('1.3.1', 1, 3, 1))
jbe@102:   AS "subquery"("string", "major", "minor", "revision");
jbe@102: 
jbe@102: ALTER TABLE "member" ADD COLUMN "locked" BOOLEAN NOT NULL DEFAULT FALSE;
jbe@102: 
jbe@102: COMMENT ON COLUMN "member"."locked" IS 'Locked members can not log in.';
jbe@102: COMMENT ON COLUMN "member"."active" IS 'Memberships, support and votes are taken into account when corresponding members are marked as active. When the user does not log in for an extended period of time, this flag may be set to FALSE. If the user is not locked, he/she may reset the active flag by logging in.';
jbe@102: 
jbe@102: CREATE OR REPLACE FUNCTION "create_interest_snapshot"
jbe@102:   ( "issue_id_p" "issue"."id"%TYPE )
jbe@102:   RETURNS VOID
jbe@102:   LANGUAGE 'plpgsql' VOLATILE AS $$
jbe@102:     DECLARE
jbe@102:       "member_id_v" "member"."id"%TYPE;
jbe@102:     BEGIN
jbe@102:       DELETE FROM "direct_interest_snapshot"
jbe@102:         WHERE "issue_id" = "issue_id_p"
jbe@102:         AND "event" = 'periodic';
jbe@102:       DELETE FROM "delegating_interest_snapshot"
jbe@102:         WHERE "issue_id" = "issue_id_p"
jbe@102:         AND "event" = 'periodic';
jbe@102:       DELETE FROM "direct_supporter_snapshot"
jbe@102:         WHERE "issue_id" = "issue_id_p"
jbe@102:         AND "event" = 'periodic';
jbe@102:       INSERT INTO "direct_interest_snapshot"
jbe@102:         ("issue_id", "event", "member_id", "voting_requested")
jbe@102:         SELECT
jbe@102:           "issue_id_p"  AS "issue_id",
jbe@102:           'periodic'    AS "event",
jbe@102:           "member"."id" AS "member_id",
jbe@102:           "interest"."voting_requested"
jbe@102:         FROM "interest" JOIN "member"
jbe@102:         ON "interest"."member_id" = "member"."id"
jbe@102:         WHERE "interest"."issue_id" = "issue_id_p"
jbe@102:         AND "member"."active";
jbe@102:       FOR "member_id_v" IN
jbe@102:         SELECT "member_id" FROM "direct_interest_snapshot"
jbe@102:         WHERE "issue_id" = "issue_id_p"
jbe@102:         AND "event" = 'periodic'
jbe@102:       LOOP
jbe@102:         UPDATE "direct_interest_snapshot" SET
jbe@102:           "weight" = 1 +
jbe@102:             "weight_of_added_delegations_for_interest_snapshot"(
jbe@102:               "issue_id_p",
jbe@102:               "member_id_v",
jbe@102:               '{}'
jbe@102:             )
jbe@102:           WHERE "issue_id" = "issue_id_p"
jbe@102:           AND "event" = 'periodic'
jbe@102:           AND "member_id" = "member_id_v";
jbe@102:       END LOOP;
jbe@102:       INSERT INTO "direct_supporter_snapshot"
jbe@102:         ( "issue_id", "initiative_id", "event", "member_id",
jbe@102:           "informed", "satisfied" )
jbe@102:         SELECT
jbe@102:           "issue_id_p"            AS "issue_id",
jbe@102:           "initiative"."id"       AS "initiative_id",
jbe@102:           'periodic'              AS "event",
jbe@102:           "supporter"."member_id" AS "member_id",
jbe@102:           "supporter"."draft_id" = "current_draft"."id" AS "informed",
jbe@102:           NOT EXISTS (
jbe@102:             SELECT NULL FROM "critical_opinion"
jbe@102:             WHERE "initiative_id" = "initiative"."id"
jbe@102:             AND "member_id" = "supporter"."member_id"
jbe@102:           ) AS "satisfied"
jbe@102:         FROM "initiative"
jbe@102:         JOIN "supporter"
jbe@102:         ON "supporter"."initiative_id" = "initiative"."id"
jbe@102:         JOIN "current_draft"
jbe@102:         ON "initiative"."id" = "current_draft"."initiative_id"
jbe@102:         JOIN "direct_interest_snapshot"
jbe@102:         ON "supporter"."member_id" = "direct_interest_snapshot"."member_id"
jbe@102:         AND "initiative"."issue_id" = "direct_interest_snapshot"."issue_id"
jbe@102:         AND "event" = 'periodic'
jbe@102:         WHERE "initiative"."issue_id" = "issue_id_p";
jbe@102:       RETURN;
jbe@102:     END;
jbe@102:   $$;
jbe@102: 
jbe@102: COMMIT;




