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 2, 2021
1 parent bd188fa commit de07fb1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
13 changes: 12 additions & 1 deletion classes/question/essay.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,15 @@ protected function form_precise(\MoodleQuickForm $mform, $helptext = '') {
$mform->setType('length', PARAM_INT);
return $mform;
}
}

/**
* Overridden from classes/question/question.php to provide a new question option to hide the response to students.
* @param \MoodleQuickForm $mform
* @param string $helpname
* @return \MoodleQuickForm
*/
protected function form_extradata(\MoodleQuickForm $mform, $helpname = '') {
$mform->addElement('selectyesno', 'extradata', get_string('hideresponsetostudents', 'questionnaire'));
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 $hidetostudents Whether question should be visible to students or not.
*/
public function set_extradata($hidetostudents) {
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 = $hidetostudents;

return $DB->set_field('questionnaire_question', 'extradata', $hidetostudents, ['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;
$hiddentostudents = $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 ($hiddentostudents == 1) {
$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;
}

}
}
24 changes: 20 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,23 @@ 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]);

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

return $responses;
}

/**
Expand Down
6 changes: 5 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,8 @@
$string['grade'] = 'Submission grade';
$string['gradesdeleted'] = 'Questionnaire grades deleted';
$string['headingtext'] = 'Heading text';
$string['hideresponsetostudents'] = 'Hide response to students';
$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 +339,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 +657,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';
15 changes: 14 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,18 @@
}

$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);

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

$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 +418,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 de07fb1

Please sign in to comment.