Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questionnaire: Error adding/deleting question with branch/no branch (… #484

Open
wants to merge 1 commit into
base: MOODLE_311_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions classes/feedback/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,14 @@ public function delete() {
$DB->delete_records(self::TABLE, ['id' => $this->id]);

// Resequence the section numbers as necessary.
$allsections = $DB->get_records(self::TABLE, ['surveyid' => $this->surveyid], 'section ASC');
$count = 1;
foreach ($allsections as $id => $section) {
if ($section->section != $count) {
$DB->set_field(self::TABLE, 'section', $count, ['id' => $id]);
if ($allsections = $DB->get_records(self::TABLE, ['surveyid' => $this->surveyid], 'section ASC')) {
$count = 1;
foreach ($allsections as $id => $section) {
if ($section->section != $count) {
$DB->set_field(self::TABLE, 'section', $count, ['id' => $id]);
}
$count++;
}
$count++;
}
}

Expand Down
19 changes: 10 additions & 9 deletions classes/question/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function __construct($id = 0, $question = null, $context = null, $params

if ($qtypes === null) {
$qtypes = $DB->get_records('questionnaire_question_type', [], 'typeid',
'typeid, type, has_choices, response_table');
'typeid, type, has_choices, response_table') ?? [];
}

if ($id) {
Expand Down Expand Up @@ -280,14 +280,15 @@ private function get_dependencies() {
global $DB;

$this->dependencies = [];
$dependencies = $DB->get_records('questionnaire_dependency',
['questionid' => $this->id , 'surveyid' => $this->surveyid], 'id ASC');
foreach ($dependencies as $dependency) {
$this->dependencies[$dependency->id] = new \stdClass();
$this->dependencies[$dependency->id]->dependquestionid = $dependency->dependquestionid;
$this->dependencies[$dependency->id]->dependchoiceid = $dependency->dependchoiceid;
$this->dependencies[$dependency->id]->dependlogic = $dependency->dependlogic;
$this->dependencies[$dependency->id]->dependandor = $dependency->dependandor;
if ($dependencies = $DB->get_records('questionnaire_dependency',
['questionid' => $this->id , 'surveyid' => $this->surveyid], 'id ASC')) {
foreach ($dependencies as $dependency) {
$this->dependencies[$dependency->id] = new \stdClass();
$this->dependencies[$dependency->id]->dependquestionid = $dependency->dependquestionid;
$this->dependencies[$dependency->id]->dependchoiceid = $dependency->dependchoiceid;
$this->dependencies[$dependency->id]->dependlogic = $dependency->dependlogic;
$this->dependencies[$dependency->id]->dependandor = $dependency->dependandor;
}
}
}

Expand Down
11 changes: 6 additions & 5 deletions classes/question/sectiontext.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ protected function question_survey_display($response, $descendantsdata, $blankqu
return '';
}

$fbsections = $DB->get_records('questionnaire_fb_sections', ['surveyid' => $this->surveyid]);
$filteredsections = [];

// In which section(s) is this question?
foreach ($fbsections as $key => $fbsection) {
if ($scorecalculation = section::decode_scorecalculation($fbsection->scorecalculation)) {
if (array_key_exists($this->id, $scorecalculation)) {
array_push($filteredsections, $fbsection->section);
if ($fbsections = $DB->get_records('questionnaire_fb_sections', ['surveyid' => $this->surveyid])) {
foreach ($fbsections as $key => $fbsection) {
if ($scorecalculation = section::decode_scorecalculation($fbsection->scorecalculation)) {
if (array_key_exists($this->id, $scorecalculation)) {
array_push($filteredsections, $fbsection->section);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ function questionnaire_get_user_grades($questionnaire, $userid=0) {
$sql = "SELECT r.id, u.id AS userid, r.grade AS rawgrade, r.submitted AS dategraded, r.submitted AS datesubmitted
FROM {user} u, {questionnaire_response} r
WHERE u.id = r.userid AND r.questionnaireid = $questionnaire->id AND r.complete = 'y' $usersql";
return $DB->get_records_sql($sql, $params);
return $DB->get_records_sql($sql, $params) ?? [];
}

/**
Expand Down Expand Up @@ -1061,7 +1061,7 @@ function questionnaire_print_overview($courses, &$htmlarray) {
// Deadline.
$str .= $OUTPUT->box(get_string('closeson', 'questionnaire', userdate($questionnaire->closedate)), 'info');
$attempts = $DB->get_records('questionnaire_response',
['questionnaireid' => $questionnaire->id, 'userid' => $USER->id, 'complete' => 'y']);
['questionnaireid' => $questionnaire->id, 'userid' => $USER->id, 'complete' => 'y']) ?? [];
$nbattempts = count($attempts);

// Do not display a questionnaire as due if it can only be sumbitted once and it has already been submitted!
Expand Down
91 changes: 52 additions & 39 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function questionnaire_get_user_responses($questionnaireid, $userid, $complete=t
WHERE questionnaireid = ?
AND userid = ?
".$andcomplete."
ORDER BY submitted ASC ", array($questionnaireid, $userid));
ORDER BY submitted ASC ", array($questionnaireid, $userid)) ?? [];
}

/**
Expand Down Expand Up @@ -416,7 +416,7 @@ function questionnaire_get_survey_list($courseid=0, $type='') {
$params = [$courseid];
}
}
return $DB->get_records_sql($sql, $params);
return $DB->get_records_sql($sql, $params) ?? [];
}

/**
Expand Down Expand Up @@ -738,72 +738,86 @@ function questionnaire_check_page_breaks($questionnaire) {
$newpbids = array();
$delpb = 0;
$sid = $questionnaire->survey->id;
$questions = $DB->get_records('questionnaire_question', ['surveyid' => $sid, 'deleted' => 'n'], 'id');
$positions = array();
foreach ($questions as $key => $qu) {
$positions[$qu->position]['question_id'] = $key;
$positions[$qu->position]['type_id'] = $qu->type_id;
$positions[$qu->position]['qname'] = $qu->name;
$positions[$qu->position]['qpos'] = $qu->position;

$dependencies = $DB->get_records('questionnaire_dependency', ['questionid' => $key , 'surveyid' => $sid],
'id ASC', 'id, dependquestionid, dependchoiceid, dependlogic');
$positions[$qu->position]['dependencies'] = $dependencies ?? [];
if ($questions = $DB->get_records('questionnaire_question', ['surveyid' => $sid, 'deleted' => 'n'], 'position')) {
foreach ($questions as $key => $qu) {
$newqu = new stdClass();
$newqu->question_id = $key;
$newqu->type_id = $qu->type_id;
$newqu->qname = $qu->name;
$newqu->qpos = $qu->position;

$dependencies = $DB->get_records('questionnaire_dependency', ['questionid' => $key, 'surveyid' => $sid],
'id ASC', 'id, dependquestionid, dependchoiceid, dependlogic');
$newqu->dependencies = $dependencies ?? [];
$positions[] = (array)$newqu;
}
}
$count = count($positions);

for ($i = $count; $i > 0; $i--) {
for ($i = $count - 1; $i >= 0; $i--) {
$qu = $positions[$i];
$questionnb = $i;
$prevqu = null;
$prevtypeid = null;
if ($i > 0) {
$prevqu = $positions[$i - 1];
$prevtypeid = $prevqu['type_id'];
}
if ($qu['type_id'] == QUESPAGEBREAK) {
$questionnb--;
// If more than one consecutive page breaks, remove extra one(s).
$prevqu = null;
$prevtypeid = null;
if ($i > 1) {
$prevqu = $positions[$i - 1];
$prevtypeid = $prevqu['type_id'];
}
// If $i == $count then remove that extra page break in last position.
if ($prevtypeid == QUESPAGEBREAK || $i == $count || $qu['qpos'] == 1) {
// Remove that extra page break in 1st position.
if ($prevtypeid == QUESPAGEBREAK || $i == $count - 1 || $qu['qpos'] == 1) {
$qid = $qu['question_id'];
$delpb ++;
$msg .= get_string("checkbreaksremoved", "questionnaire", $delpb).'<br />';
// Need to reload questions.
$questions = $DB->get_records('questionnaire_question', ['surveyid' => $sid, 'deleted' => 'n'], 'id');
$DB->set_field('questionnaire_question', 'deleted', 'y', ['id' => $qid, 'surveyid' => $sid]);
$select = 'surveyid = '.$sid.' AND deleted = \'n\' AND position > '.
$questions[$qid]->position;
if ($records = $DB->get_records_select('questionnaire_question', $select, null, 'position ASC')) {
foreach ($records as $record) {
$DB->set_field('questionnaire_question', 'position', $record->position - 1, ['id' => $record->id]);
if ($questions = $DB->get_records('questionnaire_question', ['surveyid' => $sid, 'deleted' => 'n'], 'id')) {
$DB->set_field('questionnaire_question', 'deleted', 'y', ['id' => $qid, 'surveyid' => $sid]);
$select = 'surveyid = ' . $sid . ' AND deleted = \'n\' AND position > ' .
$questions[$qid]->position;
if ($records = $DB->get_records_select('questionnaire_question', $select, null, 'position ASC')) {
foreach ($records as $record) {
$DB->set_field('questionnaire_question', 'position', $record->position - 1, ['id' => $record->id]);
}
}
}
}
}
// Add pagebreak between question child and not dependent question that follows.
if ($qu['type_id'] != QUESPAGEBREAK) {
$j = $i - 1;
if ($j != 0) {
$prevtypeid = $positions[$j]['type_id'];
$prevdependencies = $positions[$j]['dependencies'];

if ($prevqu) {
$prevdependencies = $prevqu['dependencies'];
$outerdependencies = count($qu['dependencies']) >= count($prevdependencies) ?
$qu['dependencies'] : $prevdependencies;
$innerdependencies = count($qu['dependencies']) < count($prevdependencies) ?
$qu['dependencies'] : $prevdependencies;

$okeys = [];
$ikeys = [];
foreach ($outerdependencies as $okey => $outerdependency) {
foreach ($innerdependencies as $ikey => $innerdependency) {
if ($outerdependency->dependquestionid === $innerdependency->dependquestionid &&
$outerdependency->dependchoiceid === $innerdependency->dependchoiceid &&
$outerdependency->dependlogic === $innerdependency->dependlogic) {
unset($outerdependencies[$okey]);
unset($innerdependencies[$ikey]);
$outerdependency->dependchoiceid === $innerdependency->dependchoiceid &&
$outerdependency->dependlogic === $innerdependency->dependlogic) {
$okeys[] = $okey;
$ikeys[] = $ikey;
}
}
}

foreach ($okeys as $key) {
if (key_exists($key, $outerdependencies)) {
unset($outerdependencies[$key]);
}
}
foreach ($ikeys as $key) {
if (key_exists($key, $innerdependencies)) {
unset($innerdependencies[$key]);
}
}

$diffdependencies = count($outerdependencies) + count($innerdependencies);

if (($prevtypeid != QUESPAGEBREAK && $diffdependencies != 0)
Expand All @@ -825,9 +839,8 @@ function questionnaire_check_page_breaks($questionnaire) {
return (false);
}
$newpbids[] = $newqid;
$movetopos = $i;
$questionnaire = new questionnaire($course, $cm, $questionnaire->id, null);
$questionnaire->move_question($newqid, $movetopos);
$questionnaire->move_question($newqid, $qu['qpos']);
}
}
}
Expand Down
Loading