Skip to content

Commit

Permalink
Testing new structure: replies works
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Aug 3, 2023
1 parent 0eed562 commit ebc4be9
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 523 deletions.
74 changes: 49 additions & 25 deletions classes/discussion/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class discussion {
/** @var int The moodleoverflow ID where the discussion is located*/
private $moodleoverflow;

/** @var char The title of the discussion, the titel of the parent post*/
/** @var string The title of the discussion, the titel of the parent post*/
public $name;

/** @var int The id of the parent/first post*/
Expand Down Expand Up @@ -129,47 +129,47 @@ public function __construct($id, $course, $moodleoverflow, $name, $firstpost,
*/
public static function from_record($record) {
$id = null;
if (object__property_exists($record, 'id') && $record->id) {
if (object_property_exists($record, 'id') && $record->id) {
$id = $record->id;
}

$course = null;
if (object__property_exists($record, 'course') && $record->course) {
if (object_property_exists($record, 'course') && $record->course) {
$course = $record->course;
}

$moodleoverflow = null;
if (object__property_exists($record, 'moodleoverflow') && $record->moodleoverflow) {
if (object_property_exists($record, 'moodleoverflow') && $record->moodleoverflow) {
$moodleoverflow = $record->moodleoverflow;
}

$name = null;
if (object__property_exists($record, 'name') && $record->name) {
if (object_property_exists($record, 'name') && $record->name) {
$name = $record->name;
}

$firstpost = null;
if (object__property_exists($record, 'firstpost') && $record->firstpost) {
if (object_property_exists($record, 'firstpost') && $record->firstpost) {
$firstpost = $record->firstpost;
}

$userid = null;
if (object__property_exists($record, 'userid') && $record->userid) {
if (object_property_exists($record, 'userid') && $record->userid) {
$userid = $record->userid;
}

$timemodified = null;
if (object__property_exists($record, 'timemodified') && $record->timemodified) {
if (object_property_exists($record, 'timemodified') && $record->timemodified) {
$timemodified = $record->timemodified;
}

$timestart = null;
if (object__property_exists($record, 'timestart') && $record->timestart) {
if (object_property_exists($record, 'timestart') && $record->timestart) {
$timestart = $record->timestart;
}

$usermodified = null;
if (object__property_exists($record, 'usermodified') && $record->usermodified) {
if (object_property_exists($record, 'usermodified') && $record->usermodified) {
$usermodified = $record->usermodified;
}

Expand Down Expand Up @@ -212,7 +212,7 @@ public function moodleoverflow_add_discussion($prepost) {
global $DB;

// Add the discussion to the Database.
$this->id = $DB->insert_record('moodleoverflow_discussions', $this);
$this->id = $DB->insert_record('moodleoverflow_discussions', $this->build_db_object());

// Create the first/parent post for the new discussion and add it do the DB.
$post = post::construct_without_id($this->id, 0, $prepost->userid, $prepost->timenow, $prepost->timenow, $preposts->message,
Expand Down Expand Up @@ -301,7 +301,7 @@ public function moodleoverflow_delete_discussion($prepost) {
public function moodleoverflow_add_post_to_discussion($prepost) {
global $DB;
$this->existence_check();
$this->post_check();
$this->posts_check();

// Create the post that will be added to the new discussion.
$post = post::construct_without_id($this->id, $prepost->parentid, $prepost->userid, $prepost->timenow, $prepost->timenow,
Expand All @@ -312,9 +312,9 @@ public function moodleoverflow_add_post_to_discussion($prepost) {

// Add the post to the $posts array and update the timemodified in the DB.
$this->posts[$postid] = $post;
$this->timemodified = $timenow;
$this->timemodified = $prepost->timenow;
$this->usermodified = $prepost->userid;
$DB->update_record('moodleoverflow_discussions', $this);
$DB->update_record('moodleoverflow_discussions', $this->build_db_object());

// Return the id of the added post.
return $postid;
Expand Down Expand Up @@ -362,16 +362,16 @@ public function moodleoverflow_edit_post_from_discussion($prepost) {
$this->post_exists_check($prepost->postid);

// Access the post.
$post = $this->post[$prepost->postid];
$post = $this->posts[$prepost->postid];

// If the post is the firstpost, then update the name of this discussion and the post. If not, only update the post.
if ($prepost->id == array_key_first($posts)) {
if ($prepost->postid == array_key_first($this->posts)) {
$this->name = $prepost->subject;
$this->usermodified = $prepost->userid;
$this->timemodified = $prepost->timenow;
$DB->update_record('moodleoverflow_discussions', $this);
$DB->update_record('moodleoverflow_discussions', $this->build_db_object());
}
$post->moodleoverflow_edit_post($timenow, $prepost->message, $prepost->messageformat, $prepost->formattachment);
$post->moodleoverflow_edit_post($prepost->timenow, $prepost->message, $prepost->messageformat, $prepost->formattachment);

// The post has been edited successfully.
return true;
Expand Down Expand Up @@ -400,7 +400,7 @@ public function moodleoverflow_discussion_adapt_to_last_post() {
if ($lastpost->modified != $this->timemodified || $lastpost->get_userid() != $this->usermodified) {
$this->timemodified = $lastpost->modified;
$this->usermodified = $lastpost->get_userid();
$DB->update_record('moodleoverflow_discussions', $this);
$DB->update_record('moodleoverflow_discussions', $this->build_db_object());

// Return that the discussion needed an update.
return true;
Expand Down Expand Up @@ -487,13 +487,13 @@ public function moodleoverflow_get_discussion_posts() {
$otherpostssql = 'SELECT * FROM {moodleoverflow_posts} posts
WHERE posts.discussion = ' . $this->id . ' AND posts.parent != 0;';
$firstpostrecord = $DB->get_record_sql($firstpostsql);
$otherpostsrecords = $DB->get_records_sql($otherpostssql);
$otherpostsrecord = $DB->get_records_sql($otherpostssql);

// Add the first/parent post to the array, then add the other posts.
$firstpost = post::from_record($firstpostrecord);
$this->posts[$firstpost->get_id()] = $firstpost;

foreach ($otherpostrecords as $postrecord) {
foreach ($otherpostsrecord as $postrecord) {
$post = post::from_record($postrecord);
$this->posts[$post->get_id()] = $post;
}
Expand Down Expand Up @@ -534,13 +534,37 @@ public function get_coursemodule() {
if (empty($this->cmobject)) {
if (!$this->cmobject = $DB->get_coursemodule_from_instance('moodleoverflow', $this->get_moodleoverflow()->id,
$this->get_moodleoverflow()->course)) {
throw new moodle_exception('invalidcoursemodule');
throw new \moodle_exception('invalidcoursemodule');
}
}

return $this->cmobject;
}

// Helper functions.

/**
* Builds an object from this instance that has only DB-relevant attributes.
* @return object $dbobject
*/
private function build_db_object() {
$this->existence_check();
$this->posts_check();

$dbobject = new \stdClass();
$dbobject->id = $this->id;
$dbobject->course = $this->course;
$dbobject->moodleoverflow = $this->moodleoverflow;
$dbobject->name = $this->name;
$dbobject->firstpost = $this->firstpost;
$dbobject->userid = $this->userid;
$dbobject->timemodified = $this->timemodified;
$dbobject->timestart = $this->timestart;
$dbobject->usermodified = $this->usermodified;

return $dbobject;
}

// Security.

/**
Expand All @@ -552,7 +576,7 @@ public function get_coursemodule() {
*/
private function existence_check() {
if (empty($this->id) || $this->id == false || $this->id == null) {
throw new moodle_exception('noexistingdiscussion', 'moodleoverflow');
throw new \moodle_exception('noexistingdiscussion', 'moodleoverflow');
}
return true;
}
Expand All @@ -565,7 +589,7 @@ private function existence_check() {
*/
private function posts_check() {
if (!$this->postsbuild) {
throw new moodle_exception('notallpostsavailable', 'moodleoverflow');
throw new \moodle_exception('notallpostsavailable', 'moodleoverflow');
}
return true;
}
Expand All @@ -579,7 +603,7 @@ private function posts_check() {
*/
private function post_exists_check($postid) {
if (!$this->posts[$postid]) {
throw new moodle_exception('postnotpartofdiscussion', 'moodleoverflow');
throw new \moodle_exception('postnotpartofdiscussion', 'moodleoverflow');
}

return true;
Expand Down
Loading

0 comments on commit ebc4be9

Please sign in to comment.