diff --git a/classes/question/essay.php b/classes/question/essay.php index c3697184..dd208258 100644 --- a/classes/question/essay.php +++ b/classes/question/essay.php @@ -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; @@ -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; @@ -159,4 +159,4 @@ protected function form_precise(\MoodleQuickForm $mform, $helptext = '') { $mform->setType('length', PARAM_INT); return $mform; } -} \ No newline at end of file +} diff --git a/classes/question/question.php b/classes/question/question.php index b3270f96..a0d4b7e0 100644 --- a/classes/question/question.php +++ b/classes/question/question.php @@ -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. * @@ -1603,4 +1623,4 @@ public function get_mobile_response_data($response) { return $resultdata; } -} \ No newline at end of file +} diff --git a/classes/questions_form.php b/classes/questions_form.php index 76dcf00f..2f0f8bd1 100644 --- a/classes/questions_form.php +++ b/classes/questions_form.php @@ -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) { @@ -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, '', ''); @@ -372,4 +390,4 @@ public function validation($data, $files) { return $errors; } -} \ No newline at end of file +} diff --git a/classes/responsetype/text.php b/classes/responsetype/text.php index 07cab584..b144b111 100644 --- a/classes/responsetype/text.php +++ b/classes/responsetype/text.php @@ -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)) { @@ -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 . @@ -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 ' . @@ -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; } /** diff --git a/lang/en/questionnaire.php b/lang/en/questionnaire.php index e4e86c17..81e3c15d 100644 --- a/lang/en/questionnaire.php +++ b/lang/en/questionnaire.php @@ -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'; @@ -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'; @@ -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'; @@ -653,4 +656,4 @@ $string['yesno_help'] = 'Simple Yes/No question.'; $string['yourresponse'] = 'Your response'; $string['yourresponses'] = 'Your responses'; -$string['crontask'] = 'Questionnaire cleanup job'; \ No newline at end of file +$string['crontask'] = 'Questionnaire cleanup job'; diff --git a/questions.php b/questions.php index 2f9fa9b2..72511d0e 100644 --- a/questions.php +++ b/questions.php @@ -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. @@ -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 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(); @@ -405,4 +419,4 @@ $questionnaire->page->add_to_page('formarea', $questionsform->render()); } echo $questionnaire->renderer->render($questionnaire->page); -echo $questionnaire->renderer->footer(); \ No newline at end of file +echo $questionnaire->renderer->footer();