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
namespace mod_questionnaire\responsetype;
18
 
19
/**
20
 * Class for multiple response types.
21
 *
22
 * @author Mike Churchward
23
 * @copyright 2016 onward Mike Churchward (mike.churchward@poetopensource.org)
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
25
 * @package mod_questionnaire
26
 */
27
class multiple extends single {
28
    /**
29
     * The only differences between multuple and single responses are the
30
     * response table and the insert logic.
31
     */
32
    public static function response_table() {
33
        return 'questionnaire_resp_multiple';
34
    }
35
 
36
    /**
37
     * Provide an array of answer objects from web form data for the question.
38
     *
39
     * @param \stdClass $responsedata All of the responsedata as an object.
40
     * @param \mod_questionnaire\question\question $question
41
     * @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects.
42
     * @throws \coding_exception
43
     */
44
    public static function answers_from_webform($responsedata, $question) {
45
        $answers = [];
46
        if (isset($responsedata->{'q'.$question->id})) {
47
            foreach ($responsedata->{'q' . $question->id} as $cid => $cvalue) {
48
                $cid = clean_param($cid, PARAM_CLEAN);
49
                if (isset($question->choices[$cid])) {
50
                    $record = new \stdClass();
51
                    $record->responseid = $responsedata->rid;
52
                    $record->questionid = $question->id;
53
                    $record->choiceid = $cid;
54
                    // If this choice is an "other" choice, look for the added input.
55
                    if ($question->choices[$cid]->is_other_choice()) {
56
                        $cname = \mod_questionnaire\question\choice::id_other_choice_name($cid);
57
                        $record->value = isset($responsedata->{'q' . $question->id}[$cname]) ?
58
                            $responsedata->{'q' . $question->id}[$cname] : '';
59
                    }
60
                    $answers[$cid] = answer\answer::create_from_data($record);
61
                }
62
            }
63
        }
64
        return $answers;
65
    }
66
 
67
    /**
68
     * Provide an array of answer objects from mobile data for the question.
69
     *
70
     * @param \stdClass $responsedata All of the responsedata as an object.
71
     * @param \mod_questionnaire\question\question $question
72
     * @return array \mod_questionnaire\responsetype\answer\answer An array of answer objects.
73
     */
74
    public static function answers_from_appdata($responsedata, $question) {
75
        // Need to override "single" class' implementation.
76
        $answers = [];
77
        $qname = 'q'.$question->id;
78
        if (isset($responsedata->{$qname}) && !empty($responsedata->{$qname})) {
79
            foreach ($responsedata->{$qname} as $choiceid => $choicevalue) {
80
                if ($choicevalue) {
81
                    $record = new \stdClass();
82
                    $record->responseid = $responsedata->rid;
83
                    $record->questionid = $question->id;
84
                    $record->choiceid = $choiceid;
85
                    // If this choice is an "other" choice, look for the added input.
86
                    if (isset($question->choices[$choiceid]) && $question->choices[$choiceid]->is_other_choice()) {
87
                        $cname = \mod_questionnaire\question\choice::id_other_choice_name($choiceid);
88
                        $record->value =
89
                            isset($responsedata->{$qname}[$cname]) ? $responsedata->{$qname}[$cname] : '';
90
                    } else {
91
                        $record->value = $choicevalue;
92
                    }
93
                    $answers[] = answer\answer::create_from_data($record);
94
                }
95
            }
96
        }
97
        return $answers;
98
    }
99
 
100
    /**
101
     * Return an array of answers by question/choice for the given response. Must be implemented by the subclass.
102
     * Array is indexed by question, and contains an array by choice code of selected choices.
103
     *
104
     * @param int $rid The response id.
105
     * @return array
106
     */
107
    public static function response_select($rid) {
108
        global $DB;
109
 
110
        $values = [];
111
        $sql = 'SELECT a.id, q.id as qid, q.content, c.content as ccontent, c.id as cid, o.response ' .
112
            'FROM {'.static::response_table().'} a ' .
113
            'INNER JOIN {questionnaire_question} q ON a.question_id = q.id ' .
114
            'INNER JOIN {questionnaire_quest_choice} c ON a.choice_id = c.id ' .
115
            'LEFT JOIN {questionnaire_response_other} o ON a.response_id = o.response_id AND c.id = o.choice_id ' .
116
            'WHERE a.response_id = ? ';
117
        $records = $DB->get_records_sql($sql, [$rid]);
118
        if (!empty($records)) {
119
            $qid = 0;
120
            $newrow = [];
121
            foreach ($records as $row) {
122
                if ($qid == 0) {
123
                    $qid = $row->qid;
124
                    $newrow['content'] = $row->content;
125
                    $newrow['ccontent'] = $row->ccontent;
126
                    $newrow['responses'] = [];
127
                } else if ($qid != $row->qid) {
128
                    $values[$qid] = $newrow;
129
                    $qid = $row->qid;
130
                    $newrow = [];
131
                    $newrow['content'] = $row->content;
132
                    $newrow['ccontent'] = $row->ccontent;
133
                    $newrow['responses'] = [];
134
                }
135
                $newrow['responses'][$row->cid] = $row->cid;
136
                if (\mod_questionnaire\question\choice::content_is_other_choice($row->ccontent)) {
137
                    $newrow['responses'][\mod_questionnaire\question\choice::id_other_choice_name($row->cid)] =
138
                        $row->response;
139
                }
140
            }
141
            $values[$qid] = $newrow;
142
        }
143
 
144
        return $values;
145
    }
146
 
147
    /**
148
     * Return sql and params for getting responses in bulk.
149
     * @param int|array $questionnaireids One id, or an array of ids.
150
     * @param bool|int $responseid
151
     * @param bool|int $userid
152
     * @param bool $groupid
153
     * @param int $showincompletes
154
     * @return array
155
     * author Guy Thomas
156
     */
157
    public function get_bulk_sql($questionnaireids, $responseid = false, $userid = false, $groupid = false, $showincompletes = 0) {
158
        global $DB;
159
 
160
        $sql = $this->bulk_sql();
161
 
162
        if (($groupid !== false) && ($groupid > 0)) {
163
            $groupsql = ' INNER JOIN {groups_members} gm ON gm.groupid = ? AND gm.userid = qr.userid ';
164
            $gparams = [$groupid];
165
        } else {
166
            $groupsql = '';
167
            $gparams = [];
168
        }
169
 
170
        if (is_array($questionnaireids)) {
171
            list($qsql, $params) = $DB->get_in_or_equal($questionnaireids);
172
        } else {
173
            $qsql = ' = ? ';
174
            $params = [$questionnaireids];
175
        }
176
        if ($showincompletes == 1) {
177
            $showcompleteonly = '';
178
        } else {
179
            $showcompleteonly = 'AND qr.complete = ? ';
180
            $params[] = 'y';
181
        }
182
 
183
        $sql .= "
184
            AND qr.questionnaireid $qsql $showcompleteonly
185
      LEFT JOIN {questionnaire_response_other} qro ON qro.response_id = qr.id AND qro.choice_id = qrm.choice_id
186
      LEFT JOIN {user} u ON u.id = qr.userid
187
      $groupsql
188
        ";
189
        $params = array_merge($params, $gparams);
190
 
191
        if ($responseid) {
192
            $sql .= " WHERE qr.id = ?";
193
            $params[] = $responseid;
194
        } else if ($userid) {
195
            $sql .= " WHERE qr.userid = ?";
196
            $params[] = $userid;
197
        }
198
        return [$sql, $params];
199
    }
200
 
201
    /**
202
     * Return sql for getting responses in bulk.
203
     * @return string
204
     * author Guy Thomas
205
     */
206
    protected function bulk_sql() {
207
        global $DB;
208
 
209
        $userfields = $this->user_fields_sql();
210
        $alias = 'qrm';
211
        $extraselect = '';
212
        $extraselect .= 'qrm.choice_id, ' . $DB->sql_order_by_text('qro.response', 1000) . ' AS response, 0 AS rankvalue';
213
 
214
        return "
215
            SELECT " . $DB->sql_concat_join("'_'", ['qr.id', "'".$this->question->helpname()."'", $alias.'.id']) . " AS id,
216
                   qr.submitted, qr.complete, qr.grade, qr.userid, $userfields, qr.id AS rid, $alias.question_id,
217
                   $extraselect
218
              FROM {questionnaire_response} qr
219
              JOIN {".static::response_table()."} $alias ON $alias.response_id = qr.id
220
        ";
221
    }
222
}