Skip to content

Commit

Permalink
Add option to hide response to students in essay question
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Perez committed Jun 11, 2021
1 parent bd188fa commit 1428abe
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
10 changes: 5 additions & 5 deletions classes/question/essay.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ protected function question_survey_display($response, $descendantsdata, $blankqu
$editor = editors_get_preferred_editor();
$editor->use_editor($name, questionnaire_get_editor_options($this->context));
$texteditor = html_writer::tag('textarea', $value,
array('id' => $name, 'name' => $name, 'rows' => $rows, 'cols' => $cols));
array('id' => $name, 'name' => $name, 'rows' => $rows, 'cols' => $cols));
} else {
$editor = FORMAT_PLAIN;
$texteditor = html_writer::tag('textarea', $value,
array('id' => $name, 'name' => $name, 'rows' => $rows, 'cols' => $cols));
array('id' => $name, 'name' => $name, 'rows' => $rows, 'cols' => $cols));
}
$output .= $texteditor;

Expand Down Expand Up @@ -128,8 +128,8 @@ protected function response_survey_display($response) {
*/
protected function form_length(\MoodleQuickForm $mform, $helptext = '') {
$responseformats = array(
"0" => get_string('formateditor', 'questionnaire'),
"1" => get_string('formatplain', 'questionnaire'));
"0" => get_string('formateditor', 'questionnaire'),
"1" => get_string('formatplain', 'questionnaire'));
$mform->addElement('select', 'precise', get_string('responseformat', 'questionnaire'), $responseformats);
$mform->setType('precise', PARAM_INT);
return $mform;
Expand Down Expand Up @@ -159,4 +159,4 @@ protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
$mform->setType('length', PARAM_INT);
return $mform;
}
}
}
22 changes: 21 additions & 1 deletion classes/question/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,26 @@ public function set_required($required) {
return $DB->set_field('questionnaire_question', 'required', $rval, ['id' => $qid]);
}

/**
* Set the question extradata field in the object and database.
*
* @param boolean $hiddenresponsetostudents Whether question should be visible to students or not.
*/
public function set_extradata($hiddenresponsetostudents) {
global $DB;

// Need to fix this messed-up qid/id issue.
if (isset($this->qid) && ($this->qid > 0)) {
$qid = $this->qid;
} else {
$qid = $this->id;
}

$this->extradata = json_encode(['hiddenresponsetostudents' => $hiddenresponsetostudents]);

return $DB->set_field('questionnaire_question', 'extradata', $this->extradata, ['id' => $qid]);
}

/**
* Question specific display method.
*
Expand Down Expand Up @@ -1603,4 +1623,4 @@ public function get_mobile_response_data($response) {

return $resultdata;
}
}
}
20 changes: 19 additions & 1 deletion classes/questions_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function definition() {
$tid = $question->type_id;
$qtype = $question->type;
$required = $question->required;
$extradata = json_decode($question->extradata);

// Get displayable list of parents for the questions in questions_form.
if ($questionnairehasdependencies) {
Expand Down Expand Up @@ -260,6 +261,23 @@ public function definition() {
'alt' => $strrequired,
'title' => $strrequired);
$manageqgroup[] =& $mform->createElement('image', 'requiredbutton['.$question->id.']', $reqsrc, $reqextra);

// Implement logic to show and hide responses to students in essay questions.
if ($question->type_id == QUESESSAY) {
if ($extradata->hiddenresponsetostudents) {
$reqsrc = $questionnaire->renderer->image_url('t/show');
$strrequired = get_string('hiddenresponsetostudents', 'questionnaire');
} else {
$reqsrc = $questionnaire->renderer->image_url('t/hide');
$strrequired = get_string('nothiddenresponsetostudents', 'questionnaire');
}

$strrequired .= ' ' . get_string('clicktoswitch', 'questionnaire');
$reqextra = array('value' => $question->id,
'alt' => $strrequired,
'title' => $strrequired);
$manageqgroup[] =& $mform->createElement('image', 'hiddentostudentsbutton[' . $question->id . ']', $reqsrc, $reqextra);
}
}
$manageqgroup[] =& $mform->createElement('static', 'closetag_'.$question->id, '', '');

Expand Down Expand Up @@ -372,4 +390,4 @@ public function validation($data, $files) {
return $errors;
}

}
}
26 changes: 22 additions & 4 deletions classes/responsetype/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function insert_response($responsedata) {
* @throws \dml_exception
*/
public function get_results($rids=false, $anonymous=false) {
global $DB;
global $COURSE, $DB, $USER;

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

if ($anonymous) {
$sql = 'SELECT t.id, t.response, r.submitted AS submitted, ' .
'r.questionnaireid, r.id AS rid ' .
'r.questionnaireid, r.id AS rid, t.question_id AS questionid ' .
'FROM {'.static::response_table().'} t, ' .
'{questionnaire_response} r ' .
'WHERE question_id=' . $this->question->id . $rsql .
Expand All @@ -115,7 +115,7 @@ public function get_results($rids=false, $anonymous=false) {
} else {
$sql = 'SELECT t.id, t.response, r.submitted AS submitted, r.userid, u.username AS username, ' .
'u.id as usrid, ' .
'r.questionnaireid, r.id AS rid ' .
'r.questionnaireid, r.id AS rid, t.question_id AS questionid ' .
'FROM {'.static::response_table().'} t, ' .
'{questionnaire_response} r, ' .
'{user} u ' .
Expand All @@ -124,7 +124,25 @@ public function get_results($rids=false, $anonymous=false) {
' AND u.id = r.userid ' .
'ORDER BY u.lastname, u.firstname, r.submitted';
}
return $DB->get_records_sql($sql, $params);

$responses = $DB->get_records_sql($sql, $params);

// We're using this field to hide the question response to students.
if (\ltiservice_gradebookservices\local\service\gradebookservices::is_user_gradable_in_course($COURSE->id, $USER->id)) {
foreach ($responses as $response) {
$userid = $DB->get_field('questionnaire_response', 'userid', ['id' => $response->rid]);

$extradata = $DB->get_field('questionnaire_question', 'extradata', ['id' => $response->questionid]);
$extradata = json_decode($extradata);

$ownresponse = $USER->id == $userid;
if (!$ownresponse && $extradata->hiddenresponsetostudents) {
$responses[$response->id]->response = get_string('cannotviewquestionresponse', 'questionnaire');
}
}
}

return $responses;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lang/en/questionnaire.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
$string['missingrequired'] = 'Question {$a} cannot be used in this feedback section because it is not required.';
$string['missingnameandrequired'] = 'Question {$a} cannot be used in this feedback section because it does not have a name and it is not required.';
$string['cannotviewpublicresponses'] = 'You cannot view responses to this public questionnaire.';
$string['cannotviewquestionresponse'] = 'You can\'t view this response.';
$string['chart:bipolar'] = 'Bipolar bars';
$string['chart:hbar'] = 'Horizontal bars';
$string['chart:radar'] = 'Radar';
Expand Down Expand Up @@ -250,6 +251,7 @@
$string['grade'] = 'Submission grade';
$string['gradesdeleted'] = 'Questionnaire grades deleted';
$string['headingtext'] = 'Heading text';
$string['hiddenresponsetostudents'] = 'Response is hidden to students';
$string['horizontal'] = 'Horizontal';
$string['id'] = 'ID';
$string['includerankaverages'] = 'Include rank question averages';
Expand Down Expand Up @@ -336,6 +338,7 @@
$string['notificationsimple'] = 'Notification only';
$string['notifications_help'] = 'Notify roles with the "mod/questionnaire:submissionnotification" capability when a submission is made.';
$string['notifications_link'] = 'mod/questionnaire/mod#Submission_Notifications';
$string['nothiddenresponsetostudents'] = 'Response is visible to students';
$string['notopen'] = 'This questionnaire will not open until {$a}.';
$string['notrequired'] = 'Response is not required';
$string['notset'] = 'not set';
Expand Down Expand Up @@ -653,4 +656,4 @@
$string['yesno_help'] = 'Simple Yes/No question.';
$string['yourresponse'] = 'Your response';
$string['yourresponses'] = 'Your responses';
$string['crontask'] = 'Questionnaire cleanup job';
$string['crontask'] = 'Questionnaire cleanup job';
16 changes: 15 additions & 1 deletion questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@
$qformdata->removebutton = $exformdata->removebutton;
} else if (isset($exformdata->requiredbutton)) {
$qformdata->requiredbutton = $exformdata->requiredbutton;
} else if (isset($exformdata->hiddentostudentsbutton)) {
$qformdata->hiddentostudentsbutton = $exformdata->hiddentostudentsbutton;
}

// Insert a section break.
Expand Down Expand Up @@ -206,7 +208,19 @@
}

$reload = true;
} else if (isset($qformdata->hiddentostudentsbutton)) {
// Need to use the key, since IE returns the image position as the value rather than the specified
// value in the <input> tag.
$qid = key($qformdata->hiddentostudentsbutton);

$extradata = json_decode($questionnaire->questions[$qid]->extradata);
if ($extradata->hiddenresponsetostudents) {
$questionnaire->questions[$qid]->set_extradata(false);
} else {
$questionnaire->questions[$qid]->set_extradata(true);
}

$reload = true;
} else if (isset($qformdata->addqbutton)) {
if ($qformdata->type_id == QUESPAGEBREAK) { // Adding section break is handled right away....
$questionrec = new stdClass();
Expand Down Expand Up @@ -405,4 +419,4 @@
$questionnaire->page->add_to_page('formarea', $questionsform->render());
}
echo $questionnaire->renderer->render($questionnaire->page);
echo $questionnaire->renderer->footer();
echo $questionnaire->renderer->footer();

0 comments on commit 1428abe

Please sign in to comment.