Skip to content

Commit 26b28dc

Browse files
mchurchwardclaude
andcommitted
Phase 4: Refactor question/choice classes to use persistent records
Replace raw \$DB calls in question.php and choice.php with the core\persistent-based record classes from classes/local/db/: - Constructor: question_record::get_record() replaces $DB->get_record() - get_choices(): choice_record::get_for_question() replaces $DB->get_records() - get_dependencies(): dependency_record::get_records() replaces $DB->get_records() - add(): new question_record()->save() replaces $DB->insert_record() - add_choice(): new choice_record()->save() replaces $DB->insert_record() - update_choice(): choice_record get+set+save replaces $DB->update_record() - add_dependency(): new dependency_record()->save() replaces $DB->insert_record() - update_dependency(): dependency_record get+set+save replaces $DB->update_record() - delete_dependency(): dependency_record->delete() replaces $DB->delete_records() - choice::create_from_id(): choice_record::get_record() replaces $DB->get_record() - choice::delete_from_db_by_id(): choice_record->delete() replaces $DB->delete_records() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 87c3463 commit 26b28dc

File tree

2 files changed

+69
-56
lines changed

2 files changed

+69
-56
lines changed

classes/local/question/choice.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ public function __construct($id = null, $questionid = null, $content = null, $va
6363
* @return choice
6464
*/
6565
public static function create_from_id($id) {
66-
global $DB;
67-
68-
if ($record = $DB->get_record(self::tablename(), ['id' => $id], 'id,questionid,content,value')) {
69-
return new choice($id, $record->questionid, $record->content, $record->value);
70-
} else {
71-
return new choice();
66+
$choicerec = \mod_questionnaire\local\db\choice_record::get_record(['id' => $id]);
67+
if ($choicerec) {
68+
return new choice($id, $choicerec->get('questionid'), $choicerec->get('content'), $choicerec->get('value'));
7269
}
70+
return new choice();
7371
}
7472

7573
/**
@@ -106,8 +104,12 @@ public static function tablename() {
106104
* @return bool
107105
*/
108106
public static function delete_from_db_by_id($id) {
109-
global $DB;
110-
return $DB->delete_records(self::tablename(), ['id' => $id]);
107+
$choicerec = \mod_questionnaire\local\db\choice_record::get_record(['id' => $id]);
108+
if ($choicerec) {
109+
$choicerec->delete();
110+
return true;
111+
}
112+
return false;
111113
}
112114

113115
/**

classes/local/question/question.php

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ public function __construct($id = 0, $question = null, $context = null, $params
156156
}
157157

158158
if ($id) {
159-
$question = $DB->get_record('questionnaire_question', ['id' => $id]);
159+
$questionrec = \mod_questionnaire\local\db\question_record::get_record(['id' => $id]);
160+
$question = $questionrec ? $questionrec->to_record() : null;
160161
}
161162

162163
if (is_object($question)) {
@@ -252,11 +253,11 @@ public function has_choices() {
252253
* @throws \dml_exception
253254
*/
254255
private function get_choices() {
255-
global $DB;
256-
257-
if ($choices = $DB->get_records('questionnaire_quest_choice', ['questionid' => $this->id], 'id ASC')) {
258-
foreach ($choices as $choice) {
259-
$this->choices[$choice->id] = \mod_questionnaire\local\question\choice::create_from_data($choice);
256+
$choicerecs = \mod_questionnaire\local\db\choice_record::get_for_question($this->id);
257+
if ($choicerecs) {
258+
foreach ($choicerecs as $choicerec) {
259+
$this->choices[$choicerec->get('id')] =
260+
\mod_questionnaire\local\question\choice::create_from_data($choicerec->to_record());
260261
}
261262
} else {
262263
$this->choices = [];
@@ -291,23 +292,17 @@ public function allows_dependents() {
291292
* Load any dependencies.
292293
*/
293294
private function get_dependencies() {
294-
global $DB;
295-
296295
$this->dependencies = [];
297-
if (
298-
$dependencies = $DB->get_records(
299-
'questionnaire_dependency',
300-
['questionid' => $this->id, 'surveyid' => $this->surveyid],
301-
'id ASC'
302-
)
303-
) {
304-
foreach ($dependencies as $dependency) {
305-
$this->dependencies[$dependency->id] = new \stdClass();
306-
$this->dependencies[$dependency->id]->dependquestionid = $dependency->dependquestionid;
307-
$this->dependencies[$dependency->id]->dependchoiceid = $dependency->dependchoiceid;
308-
$this->dependencies[$dependency->id]->dependlogic = $dependency->dependlogic;
309-
$this->dependencies[$dependency->id]->dependandor = $dependency->dependandor;
310-
}
296+
$deprecs = \mod_questionnaire\local\db\dependency_record::get_records(
297+
['questionid' => $this->id, 'surveyid' => $this->surveyid]
298+
);
299+
foreach ($deprecs as $deprec) {
300+
$did = $deprec->get('id');
301+
$this->dependencies[$did] = new \stdClass();
302+
$this->dependencies[$did]->dependquestionid = $deprec->get('dependquestionid');
303+
$this->dependencies[$did]->dependchoiceid = $deprec->get('dependchoiceid');
304+
$this->dependencies[$did]->dependlogic = $deprec->get('dependlogic');
305+
$this->dependencies[$did]->dependandor = $deprec->get('dependandor');
311306
}
312307
}
313308

@@ -699,7 +694,9 @@ public function add($questionrecord, ?array $choicerecords = null, ?bool $calcpo
699694
$questionrecord->typeid = $this->typeid;
700695
}
701696

702-
$this->qid = $DB->insert_record('questionnaire_question', $questionrecord);
697+
$questionpersistent = new \mod_questionnaire\local\db\question_record(0, $questionrecord);
698+
$questionpersistent->save();
699+
$this->qid = $questionpersistent->get('id');
703700

704701
if ($this->has_choices() && !empty($choicerecords)) {
705702
foreach ($choicerecords as $choicerecord) {
@@ -740,8 +737,15 @@ public function update_choices() {
740737
* @return bool
741738
*/
742739
public function update_choice($choicerecord) {
743-
global $DB;
744-
return $DB->update_record('questionnaire_quest_choice', $choicerecord);
740+
$choicepersistent = \mod_questionnaire\local\db\choice_record::get_record(['id' => $choicerecord->id]);
741+
if (!$choicepersistent) {
742+
return false;
743+
}
744+
$choicepersistent->set('questionid', $choicerecord->questionid);
745+
$choicepersistent->set('content', $choicerecord->content);
746+
$choicepersistent->set('value', $choicerecord->value ?? null);
747+
$choicepersistent->save();
748+
return true;
745749
}
746750

747751
/**
@@ -750,16 +754,16 @@ public function update_choice($choicerecord) {
750754
* @return bool
751755
*/
752756
public function add_choice($choicerecord) {
753-
global $DB;
754-
$retvalue = true;
755-
if ($cid = $DB->insert_record('questionnaire_quest_choice', $choicerecord)) {
757+
$choicepersistent = new \mod_questionnaire\local\db\choice_record(0, $choicerecord);
758+
$choicepersistent->save();
759+
$cid = $choicepersistent->get('id');
760+
if ($cid) {
756761
$this->choices[$cid] = new \stdClass();
757762
$this->choices[$cid]->content = $choicerecord->content;
758-
$this->choices[$cid]->value = isset($choicerecord->value) ? $choicerecord->value : null;
759-
} else {
760-
$retvalue = false;
763+
$this->choices[$cid]->value = $choicerecord->value ?? null;
764+
return true;
761765
}
762-
return $retvalue;
766+
return false;
763767
}
764768

765769
/**
@@ -797,8 +801,18 @@ public function insert_extradata($extradata) {
797801
* @return bool
798802
*/
799803
public function update_dependency($dependencyrecord) {
800-
global $DB;
801-
return $DB->update_record('questionnaire_dependency', $dependencyrecord);
804+
$deppersistent = \mod_questionnaire\local\db\dependency_record::get_record(['id' => $dependencyrecord->id]);
805+
if (!$deppersistent) {
806+
return false;
807+
}
808+
$deppersistent->set('questionid', $dependencyrecord->questionid);
809+
$deppersistent->set('surveyid', $dependencyrecord->surveyid);
810+
$deppersistent->set('dependquestionid', $dependencyrecord->dependquestionid);
811+
$deppersistent->set('dependchoiceid', $dependencyrecord->dependchoiceid);
812+
$deppersistent->set('dependlogic', $dependencyrecord->dependlogic);
813+
$deppersistent->set('dependandor', $dependencyrecord->dependandor);
814+
$deppersistent->save();
815+
return true;
802816
}
803817

804818
/**
@@ -807,40 +821,37 @@ public function update_dependency($dependencyrecord) {
807821
* @return bool
808822
*/
809823
public function add_dependency($dependencyrecord) {
810-
global $DB;
811-
812-
$retvalue = true;
813-
if ($did = $DB->insert_record('questionnaire_dependency', $dependencyrecord)) {
824+
$deppersistent = new \mod_questionnaire\local\db\dependency_record(0, $dependencyrecord);
825+
$deppersistent->save();
826+
$did = $deppersistent->get('id');
827+
if ($did) {
814828
$this->dependencies[$did] = new \stdClass();
815829
$this->dependencies[$did]->dependquestionid = $dependencyrecord->dependquestionid;
816830
$this->dependencies[$did]->dependchoiceid = $dependencyrecord->dependchoiceid;
817831
$this->dependencies[$did]->dependlogic = $dependencyrecord->dependlogic;
818832
$this->dependencies[$did]->dependandor = $dependencyrecord->dependandor;
819-
} else {
820-
$retvalue = false;
833+
return true;
821834
}
822-
return $retvalue;
835+
return false;
823836
}
824837

825838
/**
826839
* Delete the dependency from the question object and the database.
827840
* @param int|\stdClass $dependency Either the integer id of the dependency, or the dependency record.
828841
*/
829842
public function delete_dependency($dependency) {
830-
global $DB;
831-
832-
$retvalue = true;
833843
if (is_int($dependency)) {
834844
$did = $dependency;
835845
} else {
836846
$did = $dependency->id;
837847
}
838-
if ($DB->delete_records('questionnaire_dependency', ['id' => $did])) {
848+
$deppersistent = \mod_questionnaire\local\db\dependency_record::get_record(['id' => $did]);
849+
if ($deppersistent) {
850+
$deppersistent->delete();
839851
unset($this->dependencies[$did]);
840-
} else {
841-
$retvalue = false;
852+
return true;
842853
}
843-
return $retvalue;
854+
return false;
844855
}
845856

846857
/**

0 commit comments

Comments
 (0)