1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Group observers.
19 *
20 * @package    mod_quiz
21 * @copyright  2013 Frédéric Massart
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace mod_quiz;
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->dirroot . '/mod/quiz/locallib.php');
29
30/**
31 * Group observers class.
32 *
33 * @package    mod_quiz
34 * @copyright  2013 Frédéric Massart
35 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class group_observers {
38
39    /**
40     * Flag whether a course reset is in progress or not.
41     *
42     * @var int The course ID.
43     */
44    protected static $resetinprogress = false;
45
46    /**
47     * A course reset has started.
48     *
49     * @param \core\event\base $event The event.
50     * @return void
51     */
52    public static function course_reset_started($event) {
53        self::$resetinprogress = $event->courseid;
54    }
55
56    /**
57     * A course reset has ended.
58     *
59     * @param \core\event\base $event The event.
60     * @return void
61     */
62    public static function course_reset_ended($event) {
63        if (!empty(self::$resetinprogress)) {
64            if (!empty($event->other['reset_options']['reset_groups_remove'])) {
65                quiz_process_group_deleted_in_course($event->courseid);
66            }
67            if (!empty($event->other['reset_options']['reset_groups_members'])) {
68                quiz_update_open_attempts(array('courseid' => $event->courseid));
69            }
70        }
71
72        self::$resetinprogress = null;
73    }
74
75    /**
76     * A group was deleted.
77     *
78     * @param \core\event\base $event The event.
79     * @return void
80     */
81    public static function group_deleted($event) {
82        if (!empty(self::$resetinprogress)) {
83            // We will take care of that once the course reset ends.
84            return;
85        }
86        quiz_process_group_deleted_in_course($event->courseid);
87    }
88
89    /**
90     * A group member was removed.
91     *
92     * @param \core\event\base $event The event.
93     * @return void
94     */
95    public static function group_member_added($event) {
96        quiz_update_open_attempts(array('userid' => $event->relateduserid, 'groupid' => $event->objectid));
97    }
98
99    /**
100     * A group member was deleted.
101     *
102     * @param \core\event\base $event The event.
103     * @return void
104     */
105    public static function group_member_removed($event) {
106        if (!empty(self::$resetinprogress)) {
107            // We will take care of that once the course reset ends.
108            return;
109        }
110        quiz_update_open_attempts(array('userid' => $event->relateduserid, 'groupid' => $event->objectid));
111    }
112
113}
114