Skip to content

Commit f0f8672

Browse files
author
Adrian Perez
committed
Add option to hide response to students in essay question
1 parent bd188fa commit f0f8672

File tree

6 files changed

+84
-11
lines changed

6 files changed

+84
-11
lines changed

classes/question/essay.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ protected function response_survey_display($response) {
128128
*/
129129
protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
130130
$responseformats = array(
131-
"0" => get_string('formateditor', 'questionnaire'),
132-
"1" => get_string('formatplain', 'questionnaire'));
131+
"0" => get_string('formateditor', 'questionnaire'),
132+
"1" => get_string('formatplain', 'questionnaire'));
133133
$mform->addElement('select', 'precise', get_string('responseformat', 'questionnaire'), $responseformats);
134134
$mform->setType('precise', PARAM_INT);
135135
return $mform;
@@ -159,4 +159,4 @@ protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
159159
$mform->setType('length', PARAM_INT);
160160
return $mform;
161161
}
162-
}
162+
}

classes/question/question.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,26 @@ public function set_required($required) {
794794
return $DB->set_field('questionnaire_question', 'required', $rval, ['id' => $qid]);
795795
}
796796

797+
/**
798+
* Set the question extradata field in the object and database.
799+
*
800+
* @param boolean $hiddenresponsetostudents Whether question should be visible to students or not.
801+
*/
802+
public function set_extradata($hiddenresponsetostudents) {
803+
global $DB;
804+
805+
// Need to fix this messed-up qid/id issue.
806+
if (isset($this->qid) && ($this->qid > 0)) {
807+
$qid = $this->qid;
808+
} else {
809+
$qid = $this->id;
810+
}
811+
812+
$this->extradata = json_encode(['hiddenresponsetostudents' => $hiddenresponsetostudents]);
813+
814+
return $DB->set_field('questionnaire_question', 'extradata', $this->extradata, ['id' => $qid]);
815+
}
816+
797817
/**
798818
* Question specific display method.
799819
*
@@ -1603,4 +1623,4 @@ public function get_mobile_response_data($response) {
16031623

16041624
return $resultdata;
16051625
}
1606-
}
1626+
}

classes/questions_form.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function definition() {
114114
$tid = $question->type_id;
115115
$qtype = $question->type;
116116
$required = $question->required;
117+
$extradata = json_decode($question->extradata);
117118

118119
// Get displayable list of parents for the questions in questions_form.
119120
if ($questionnairehasdependencies) {
@@ -260,6 +261,23 @@ public function definition() {
260261
'alt' => $strrequired,
261262
'title' => $strrequired);
262263
$manageqgroup[] =& $mform->createElement('image', 'requiredbutton['.$question->id.']', $reqsrc, $reqextra);
264+
265+
// Implement logic to show and hide responses to students in essay questions.
266+
if ($question->type_id == QUESESSAY) {
267+
if ($extradata->hiddenresponsetostudents) {
268+
$reqsrc = $questionnaire->renderer->image_url('t/show');
269+
$strrequired = get_string('hiddenresponsetostudents', 'questionnaire');
270+
} else {
271+
$reqsrc = $questionnaire->renderer->image_url('t/hide');
272+
$strrequired = get_string('nothiddenresponsetostudents', 'questionnaire');
273+
}
274+
275+
$strrequired .= ' ' . get_string('clicktoswitch', 'questionnaire');
276+
$reqextra = array('value' => $question->id,
277+
'alt' => $strrequired,
278+
'title' => $strrequired);
279+
$manageqgroup[] =& $mform->createElement('image', 'hiddentostudentsbutton[' . $question->id . ']', $reqsrc, $reqextra);
280+
}
263281
}
264282
$manageqgroup[] =& $mform->createElement('static', 'closetag_'.$question->id, '', '');
265283

@@ -372,4 +390,4 @@ public function validation($data, $files) {
372390
return $errors;
373391
}
374392

375-
}
393+
}

classes/responsetype/text.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function insert_response($responsedata) {
9696
* @throws \dml_exception
9797
*/
9898
public function get_results($rids=false, $anonymous=false) {
99-
global $DB;
99+
global $COURSE, $DB, $USER;
100100

101101
$rsql = '';
102102
if (!empty($rids)) {
@@ -106,7 +106,7 @@ public function get_results($rids=false, $anonymous=false) {
106106

107107
if ($anonymous) {
108108
$sql = 'SELECT t.id, t.response, r.submitted AS submitted, ' .
109-
'r.questionnaireid, r.id AS rid ' .
109+
'r.questionnaireid, r.id AS rid, t.question_id AS questionid ' .
110110
'FROM {'.static::response_table().'} t, ' .
111111
'{questionnaire_response} r ' .
112112
'WHERE question_id=' . $this->question->id . $rsql .
@@ -115,7 +115,7 @@ public function get_results($rids=false, $anonymous=false) {
115115
} else {
116116
$sql = 'SELECT t.id, t.response, r.submitted AS submitted, r.userid, u.username AS username, ' .
117117
'u.id as usrid, ' .
118-
'r.questionnaireid, r.id AS rid ' .
118+
'r.questionnaireid, r.id AS rid, t.question_id AS questionid ' .
119119
'FROM {'.static::response_table().'} t, ' .
120120
'{questionnaire_response} r, ' .
121121
'{user} u ' .
@@ -124,7 +124,25 @@ public function get_results($rids=false, $anonymous=false) {
124124
' AND u.id = r.userid ' .
125125
'ORDER BY u.lastname, u.firstname, r.submitted';
126126
}
127-
return $DB->get_records_sql($sql, $params);
127+
128+
$responses = $DB->get_records_sql($sql, $params);
129+
130+
// We're using this field to hide the question response to students.
131+
if (\ltiservice_gradebookservices\local\service\gradebookservices::is_user_gradable_in_course($COURSE->id, $USER->id)) {
132+
foreach ($responses as $response) {
133+
$userid = $DB->get_field('questionnaire_response', 'userid', ['id' => $response->rid]);
134+
135+
$extradata = $DB->get_field('questionnaire_question', 'extradata', ['id' => $response->questionid]);
136+
$extradata = json_decode($extradata);
137+
138+
$ownresponse = $USER->id == $userid;
139+
if (!$ownresponse && $extradata->hiddenresponsetostudents) {
140+
$responses[$response->id]->response = get_string('cannotviewquestionresponse', 'questionnaire');
141+
}
142+
}
143+
}
144+
145+
return $responses;
128146
}
129147

130148
/**

lang/en/questionnaire.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
$string['missingrequired'] = 'Question {$a} cannot be used in this feedback section because it is not required.';
7171
$string['missingnameandrequired'] = 'Question {$a} cannot be used in this feedback section because it does not have a name and it is not required.';
7272
$string['cannotviewpublicresponses'] = 'You cannot view responses to this public questionnaire.';
73+
$string['cannotviewquestionresponse'] = 'You can\'t view this response.';
7374
$string['chart:bipolar'] = 'Bipolar bars';
7475
$string['chart:hbar'] = 'Horizontal bars';
7576
$string['chart:radar'] = 'Radar';
@@ -250,6 +251,7 @@
250251
$string['grade'] = 'Submission grade';
251252
$string['gradesdeleted'] = 'Questionnaire grades deleted';
252253
$string['headingtext'] = 'Heading text';
254+
$string['hiddenresponsetostudents'] = 'Response is hidden to students';
253255
$string['horizontal'] = 'Horizontal';
254256
$string['id'] = 'ID';
255257
$string['includerankaverages'] = 'Include rank question averages';
@@ -336,6 +338,7 @@
336338
$string['notificationsimple'] = 'Notification only';
337339
$string['notifications_help'] = 'Notify roles with the "mod/questionnaire:submissionnotification" capability when a submission is made.';
338340
$string['notifications_link'] = 'mod/questionnaire/mod#Submission_Notifications';
341+
$string['nothiddenresponsetostudents'] = 'Response is visible to students';
339342
$string['notopen'] = 'This questionnaire will not open until {$a}.';
340343
$string['notrequired'] = 'Response is not required';
341344
$string['notset'] = 'not set';
@@ -653,4 +656,4 @@
653656
$string['yesno_help'] = 'Simple Yes/No question.';
654657
$string['yourresponse'] = 'Your response';
655658
$string['yourresponses'] = 'Your responses';
656-
$string['crontask'] = 'Questionnaire cleanup job';
659+
$string['crontask'] = 'Questionnaire cleanup job';

questions.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@
161161
$qformdata->removebutton = $exformdata->removebutton;
162162
} else if (isset($exformdata->requiredbutton)) {
163163
$qformdata->requiredbutton = $exformdata->requiredbutton;
164+
} else if (isset($exformdata->hiddentostudentsbutton)) {
165+
$qformdata->hiddentostudentsbutton = $exformdata->hiddentostudentsbutton;
164166
}
165167

166168
// Insert a section break.
@@ -206,7 +208,19 @@
206208
}
207209

208210
$reload = true;
211+
} else if (isset($qformdata->hiddentostudentsbutton)) {
212+
// Need to use the key, since IE returns the image position as the value rather than the specified
213+
// value in the <input> tag.
214+
$qid = key($qformdata->hiddentostudentsbutton);
215+
216+
$extradata = json_decode($questionnaire->questions[$qid]->extradata);
217+
if ($extradata->hiddenresponsetostudents) {
218+
$questionnaire->questions[$qid]->set_extradata(false);
219+
} else {
220+
$questionnaire->questions[$qid]->set_extradata(true);
221+
}
209222

223+
$reload = true;
210224
} else if (isset($qformdata->addqbutton)) {
211225
if ($qformdata->type_id == QUESPAGEBREAK) { // Adding section break is handled right away....
212226
$questionrec = new stdClass();
@@ -405,4 +419,4 @@
405419
$questionnaire->page->add_to_page('formarea', $questionsform->render());
406420
}
407421
echo $questionnaire->renderer->render($questionnaire->page);
408-
echo $questionnaire->renderer->footer();
422+
echo $questionnaire->renderer->footer();

0 commit comments

Comments
 (0)