Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * A type of forum.
19
 *
20
 * @package    mod_forum
21
 * @copyright  2014 Andrew Robert Nicols <andrew@nicols.co.uk>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once($CFG->dirroot.'/user/selector/lib.php');
28
 
29
/**
30
 * A user selector control for potential subscribers to the selected forum
31
 * @package   mod_forum
32
 * @copyright 2009 Sam Hemelryk
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class mod_forum_potential_subscriber_selector extends mod_forum_subscriber_selector_base {
36
    /**
37
     * If set to true EVERYONE in this course is force subscribed to this forum
38
     * @var bool
39
     */
40
    protected $forcesubscribed = false;
41
    /**
42
     * Can be used to store existing subscribers so that they can be removed from
43
     * the potential subscribers list
44
     */
45
    protected $existingsubscribers = array();
46
 
47
    /**
48
     * Constructor method
49
     * @param string $name
50
     * @param array $options
51
     */
52
    public function __construct($name, $options) {
53
        parent::__construct($name, $options);
54
        if (isset($options['forcesubscribed'])) {
55
            $this->forcesubscribed=true;
56
        }
57
    }
58
 
59
    /**
60
     * Returns an arary of options for this control
61
     * @return array
62
     */
63
    protected function get_options() {
64
        $options = parent::get_options();
65
        if ($this->forcesubscribed===true) {
66
            $options['forcesubscribed']=1;
67
        }
68
        return $options;
69
    }
70
 
71
    /**
72
     * Finds all potential users
73
     *
74
     * Potential subscribers are all enroled users who are not already subscribed.
75
     *
76
     * @param string $search
77
     * @return array
78
     */
79
    public function find_users($search) {
80
        global $DB;
81
 
82
        $whereconditions = array();
83
        list($wherecondition, $params) = $this->search_sql($search, 'u');
84
        if ($wherecondition) {
85
            $whereconditions[] = $wherecondition;
86
        }
87
 
88
        if (!$this->forcesubscribed) {
89
            $existingids = array();
90
            foreach ($this->existingsubscribers as $group) {
91
                foreach ($group as $user) {
92
                    $existingids[$user->id] = 1;
93
                }
94
            }
95
            if ($existingids) {
96
                list($usertest, $userparams) = $DB->get_in_or_equal(
97
                        array_keys($existingids), SQL_PARAMS_NAMED, 'existing', false);
98
                $whereconditions[] = 'u.id ' . $usertest;
99
                $params = array_merge($params, $userparams);
100
            }
101
        }
102
 
103
        if ($whereconditions) {
104
            $wherecondition = 'WHERE ' . implode(' AND ', $whereconditions);
105
        }
106
 
107
        list($esql, $eparams) = get_enrolled_sql($this->context, '', $this->currentgroup, true);
108
        $params = array_merge($params, $eparams);
109
 
110
        $fields      = 'SELECT ' . $this->required_fields_sql('u');
111
 
112
        $sql = " FROM {user} u
113
                 JOIN ($esql) je ON je.id = u.id
114
                      $wherecondition";
115
 
116
        list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
117
        $order = ' ORDER BY ' . $sort;
118
 
119
        $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
120
 
121
        $cm = get_coursemodule_from_instance('forum', $this->forumid);
122
        $modinfo = get_fast_modinfo($cm->course);
123
        $info = new \core_availability\info_module($modinfo->get_cm($cm->id));
124
        $availableusers = $info->filter_user_list($availableusers);
125
 
126
        if (empty($availableusers)) {
127
            return array();
128
        }
129
 
130
        // Check to see if there are too many to show sensibly.
131
        if (!$this->is_validating()) {
132
            $potentialmemberscount = count($availableusers);
133
            if ($potentialmemberscount > $this->maxusersperpage) {
134
                return $this->too_many_results($search, $potentialmemberscount);
135
            }
136
        }
137
 
138
        if ($this->forcesubscribed) {
139
            return array(get_string("existingsubscribers", 'forum') => $availableusers);
140
        } else {
141
            return array(get_string("potentialsubscribers", 'forum') => $availableusers);
142
        }
143
    }
144
 
145
    /**
146
     * Sets the existing subscribers
147
     * @param array $users
148
     */
149
    public function set_existing_subscribers(array $users) {
150
        $this->existingsubscribers = $users;
151
    }
152
 
153
    /**
154
     * Sets this forum as force subscribed or not
155
     */
156
    public function set_force_subscribed($setting=true) {
157
        $this->forcesubscribed = true;
158
    }
159
}