Skip to content

Commit

Permalink
first testing completed and successful. Test classes still needed
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Aug 9, 2023
1 parent ebc4be9 commit 8961316
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 104 deletions.
51 changes: 31 additions & 20 deletions classes/discussion/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/mod/moodleoverflow/locallib.php');

/**
Expand Down Expand Up @@ -133,42 +134,42 @@ public static function from_record($record) {
$id = $record->id;
}

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

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

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

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

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

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

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

$usermodified = null;
$usermodified = 0;
if (object_property_exists($record, 'usermodified') && $record->usermodified) {
$usermodified = $record->usermodified;
}
Expand Down Expand Up @@ -215,8 +216,8 @@ public function moodleoverflow_add_discussion($prepost) {
$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,
$prepost->messageformat, "", 0, $prepost->review, null, $prepost->formattachments);
$post = post::construct_without_id($this->id, 0, $prepost->userid, $prepost->timenow, $prepost->timenow, $prepost->message,
$prepost->messageformat, "", 0, $prepost->reviewed, null, $prepost->formattachments);
// Add it to the DB and save the id of the first/parent post.
$this->firstpost = $post->moodleoverflow_add_new_post();

Expand All @@ -230,7 +231,7 @@ public function moodleoverflow_add_discussion($prepost) {
// Trigger event.
$params = array(
'context' => $prepost->modulecontext,
'objectid' => $post->discussion,
'objectid' => $this->id,
);
$event = \mod_moodleoverflow\event\discussion_viewed::create($params);
$event->trigger();
Expand All @@ -256,7 +257,7 @@ public function moodleoverflow_delete_discussion($prepost) {
$transaction = $DB->start_delegated_transaction();

// Delete every post of this discussion.
foreach ($posts as $post) {
foreach ($this->posts as $post) {
$post->moodleoverflow_delete_post(false);
}

Expand Down Expand Up @@ -371,7 +372,7 @@ public function moodleoverflow_edit_post_from_discussion($prepost) {
$this->timemodified = $prepost->timenow;
$DB->update_record('moodleoverflow_discussions', $this->build_db_object());
}
$post->moodleoverflow_edit_post($prepost->timenow, $prepost->message, $prepost->messageformat, $prepost->formattachment);
$post->moodleoverflow_edit_post($prepost->timenow, $prepost->message, $prepost->messageformat, $prepost->formattachments);

// The post has been edited successfully.
return true;
Expand All @@ -388,11 +389,13 @@ public function moodleoverflow_discussion_adapt_to_last_post() {
$this->existence_check();

// Find the last reviewed post of the discussion (even if the user has review capability, because it's written to DB).
$sql = 'SELECT id, userid, modified
$sql = 'SELECT *
FROM {moodleoverflow_posts}
WHERE discussion = ' . $this->id .
' AND reviewed = 1
AND modified = (SELECT MAX(modified) as modified FROM {moodleoverflow_posts})';
AND modified = (SELECT MAX(modified) as modified
FROM {moodleoverflow_posts}
WHERE discussion = ' . $this->id . ');';
$record = $DB->get_record_sql($sql);
$lastpost = post::from_record($record);

Expand Down Expand Up @@ -483,9 +486,9 @@ public function moodleoverflow_get_discussion_posts() {
if (!$this->postsbuild) {
// Get the posts from the DB. Get the parent post first.
$firstpostsql = 'SELECT * FROM {moodleoverflow_posts} posts
WHERE posts.discussion = ' . $this->id . ' AND posts.parent = 0;';
WHERE discussion = ' . $this->id . ' AND parent = 0;';
$otherpostssql = 'SELECT * FROM {moodleoverflow_posts} posts
WHERE posts.discussion = ' . $this->id . ' AND posts.parent != 0;';
WHERE discussion = ' . $this->id . ' AND parent != 0;';
$firstpostrecord = $DB->get_record_sql($firstpostsql);
$otherpostsrecord = $DB->get_records_sql($otherpostssql);

Expand Down Expand Up @@ -541,16 +544,24 @@ public function get_coursemodule() {
return $this->cmobject;
}

/**
* This getter works as an help function in case another file/function needs the db-object of this instance (as the function
* is not adapted/refactored to the new way of working with discussion).
* @return object
*/
public function get_db_object() {
$this->existence_check();
return $this->build_db_object();
}

// Helper functions.

/**
* Builds an object from this instance that has only DB-relevant attributes.
* As this is an private function, it doesn't need an existence check.
* @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;
Expand Down
66 changes: 34 additions & 32 deletions classes/post/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function __construct($id, $discussion, $parent, $userid, $created, $modif

/**
* Builds a Post from a DB record.
*
* Look up database structure for standard values.
* @param object $record Data object.
* @return object post instance
*/
Expand All @@ -161,52 +161,52 @@ public static function from_record($record) {
$id = $record->id;
}

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

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

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

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

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

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

$messageformat = null;
$messageformat = 0;
if (object_property_exists($record, 'messageformat') && $record->messageformat) {
$message = $record->messageformat;
$messageformat = $record->messageformat;
}

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

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

$reviewed = null;
$reviewed = 1;
if (object_property_exists($record, 'reviewed') && $record->reviewed) {
$reviewed = $record->reviewed;
}
Expand All @@ -216,10 +216,8 @@ public static function from_record($record) {
$timereviewed = $record->timereviewed;
}

$instance = new self($id, $discussion, $parent, $userid, $created, $modified, $message,
$messageformat, $attachment, $mailed, $reviewed, $timereviewed);

return $instance;
return new self($id, $discussion, $parent, $userid, $created, $modified, $message, $messageformat, $attachment, $mailed,
$reviewed, $timereviewed);
}

/**
Expand All @@ -243,9 +241,8 @@ public static function from_record($record) {
public static function construct_without_id($discussion, $parent, $userid, $created, $modified, $message,
$messageformat, $attachment, $mailed, $reviewed, $timereviewed, $formattachments = false) {
$id = null;
$instance = new self($id, $discussion, $parent, $userid, $created, $modified, $message,
$messageformat, $attachment, $mailed, $reviewed, $timereviewed, $formattachments);
return $instance;
return new self($id, $discussion, $parent, $userid, $created, $modified, $message, $messageformat, $attachment, $mailed,
$reviewed, $timereviewed, $formattachments);
}

// Post Functions.
Expand Down Expand Up @@ -297,11 +294,13 @@ public function moodleoverflow_delete_post($deletechildren) {
try {
$transaction = $DB->start_delegated_transaction();

// Get the coursemoduleid for later use.
$coursemoduleid = $this->get_coursemodule()->id;
$childposts = $this->moodleoverflow_get_childposts();
if ($deletechildren && $childposts) {
foreach ($childposts as $childpost) {
$child = $this->from_record($childpost);
$child->moodleoverflow_delete_post();
$child->moodleoverflow_delete_post($deletechildren);
}
}

Expand All @@ -315,7 +314,7 @@ public function moodleoverflow_delete_post($deletechildren) {

// Delete the attachments.
$fs = get_file_storage();
$context = \context_module::instance($this->get_coursemodule()->id);
$context = \context_module::instance($coursemoduleid);
$attachments = $fs->get_area_files($context->id, 'mod_moodleoverflow', 'attachment',
$this->id, "filename", true);
foreach ($attachments as $attachment) {
Expand All @@ -328,12 +327,9 @@ public function moodleoverflow_delete_post($deletechildren) {
}
}

// Get the context module.
$modulecontext = \context_module::instance($this->get_coursemodule()->id);

// Trigger the post deletion event.
$params = array(
'context' => $modulecontext,
'context' => $context,
'objectid' => $this->id,
'other' => array(
'discussionid' => $this->discussion,
Expand All @@ -343,7 +339,7 @@ public function moodleoverflow_delete_post($deletechildren) {
if ($this->userid !== $USER->id) {
$params['relateduserid'] = $this->userid;
}
$event = post_deleted::create($params);
$event = \mod_moodleoverflow\event\post_deleted::create($params);
$event->trigger();

// Set the id of this instance to null, so that working with it is not possible anymore.
Expand All @@ -370,15 +366,15 @@ public function moodleoverflow_delete_post($deletechildren) {
*
* @return true if the post has been edited successfully
*/
public function moodleoverflow_edit_post($time, $postmessage, $messageformat, $formattachment) {
public function moodleoverflow_edit_post($time, $postmessage, $messageformat, $formattachments) {
global $DB;
$this->existence_check();

// Update the attributes.
$this->modified = $time;
$this->message = $postmessage;
$this->messageformat = $messageformat;
$this->formattachment = $formattachment; // PLEASE CHECK LATER IF THIS IS NEEDED AFTER WORKING WITH THE POST_FORM CLASS.
$this->formattachments = $formattachments;

// Update the record in the database.
$DB->update_record('moodleoverflow_posts', $this->build_db_object());
Expand Down Expand Up @@ -431,10 +427,6 @@ public function moodleoverflow_add_attachment() {
global $DB;
$this->existence_check();

if (!$this->formattachments) {
throw new \moodle_exception('missingformattachments', 'moodleoverflow');
}

if (empty($this->formattachments)) {
return true; // Nothing to do.
}
Expand Down Expand Up @@ -615,6 +607,16 @@ public function moodleoverflow_get_childposts() {
return false;
}

/**
* This getter works as an help function in case another file/function needs the db-object of this instance (as the function
* is not adapted/refactored to the new way of working with discussion).
* @return object
*/
public function get_db_object() {
$this->existence_check();
return $this->build_db_object;
}

// Helper Functions.

/**
Expand Down
Loading

0 comments on commit 8961316

Please sign in to comment.