Skip to content

Commit

Permalink
WIP: fixing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Jul 28, 2023
1 parent b71565e commit 0eed562
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 248 deletions.
1 change: 0 additions & 1 deletion classes/discussion/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

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

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

/**
Expand Down
203 changes: 0 additions & 203 deletions classes/post/post.drawio

This file was deleted.

1 change: 0 additions & 1 deletion classes/post/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

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

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

/**
Expand Down
79 changes: 40 additions & 39 deletions classes/post/post_control.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
use mod_moodleoverflow\capabilities;
use mod_moodleoverflow\review;

use mod_moodleoverflow\post\post;
use mod_moodleoverflow\post;
use mod_moodleoverflow\discussion;

defined('MOODLE_INTERNAL') || die();
global $CFG;

require_once(dirname(__FILE__) . '/lib.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once($CFG->dirroot . '/mod/moodleoverflow/locallib.php');
require_once($CFG->libdir . '/completionlib.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');

/**
* This Class controls the manipulation of posts and acts as controller of interactions with the post.php
Expand Down Expand Up @@ -85,13 +84,14 @@ class post_control {
* Constructor
*/
public function __construct() {
$this->info = new \stdClass;
$this->info = new \stdClass();
}

/**
* Detects the interaction and builds the prepost.
* @param object $urlparamter parameter from the post.php
* @throws moodle_exception if the interaction is not correct.
* @throws \moodle_exception
*/
public function detect_interaction($urlparameter) {
$count = 0;
Expand All @@ -100,7 +100,7 @@ public function detect_interaction($urlparameter) {
$count += $urlparameter->edit ? 1 : 0;
$count += $urlparameter->delete ? 1 : 0;
if ($count !== 1) {
throw new coding_exception('Exactly one parameter should be specified!');
throw new \coding_exception('Exactly one parameter should be specified!');
}

if ($urlparameter->create) {
Expand All @@ -115,27 +115,28 @@ public function detect_interaction($urlparameter) {

} else if ($urlparameter->reply) {
$this->interaction = 'reply';
$this->info->replypostid = $urlparameter->edit;
$this->info->replypostid = $urlparameter->reply;
$this->build_prepost_reply($this->info->replypostid);

} else if ($urlparameter->delete) {
$this->interaction = 'delete';
$this->info->deletepostid = $urlparameter->edit;
$this->build_prepost_delete($this->info->deletepostid);
} else {
throw new moodle_exception('unknownaction');
throw new \moodle_exception('unknownaction');
}
}

/**
* Controls the execution of an interaction.
* @param object $form The results from the post_form.
* @return bool if the execution succeded
* @return bool if the execution succeeded
*/
public function execute_interaction($form) {
// Redirect url in case of occuring errors.
global $CFG;
// Redirect url in case of occurring errors.
if (empty($SESSION->fromurl)) {
$errordestination = '$CFG->wwwroot/mod/moodleoverflow/view.php?m=' . $this->prepost->moodleoverflowid;
$errordestination = $CFG->wwwroot . '/mod/moodleoverflow/view.php?m=' . $this->prepost->moodleoverflowid;
} else {
$errordestination = $SESSION->fromurl;
}
Expand All @@ -157,7 +158,7 @@ public function execute_interaction($form) {
} else if ($this->interaction == 'edit' && $form->edit === $this->prepost->postid) {
$this->execute_edit($form, $errordestination);
} else {
throw new moodle_exception('unexpectedinteractionerror', 'moodleoverflow', $errordestination);
throw new \moodle_exception('unexpectedinteractionerror', 'moodleoverflow', $errordestination);
}
}

Expand All @@ -171,14 +172,14 @@ public function execute_interaction($form) {
public function catch_guest($postid = false, $moodleoverflowid = false) {
global $PAGE;
if ((!$postid && !$moodleoverflowid) || ($postid && $moodleoverflowid)) {
throw new moodle_exception('inaccurateparameter', 'moodleoverflow');
throw new \moodle_exception('inaccurateparameter', 'moodleoverflow');
}
if ($postid) {
$this->collect_information($postid, false);
} else if ($moodleoverflowid) {
$this->collect_information(false, $moodleoverflowid);
}
$this->info->modulecontext = context_module::instance($this->info->cm->id);
$this->info->modulecontext = \context_module::instance($this->info->cm->id);

// Set the parameters for the page.
$PAGE->set_cm($this->info->cm, $this->info->course, $this->info->moodleoverflow);
Expand Down Expand Up @@ -218,7 +219,7 @@ private function build_prepost_create($moodleoverflowid) {
}
}
// Notify the user, that he can not post a new discussion.
throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
throw new \moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
}

// Where is the user coming from?
Expand Down Expand Up @@ -266,11 +267,11 @@ private function build_prepost_reply($replypostid) {
get_string('youneedtoenrol'));
}
// Print the error message.
throw new moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
throw new \moodle_exception('nopostmoodleoverflow', 'moodleoverflow');
}
// Make sure the user can post here.
if (!$this->info->cm->visible && !has_capability('moodle/course:viewhiddenactivities', $this->info->modulecontext)) {
throw new moodle_exception('activityiscurrentlyhidden');
throw new \moodle_exception('activityiscurrentlyhidden');
}

// Prepare a post.
Expand Down Expand Up @@ -312,7 +313,7 @@ private function build_prepost_edit($editpostid) {
if (($beyondtime || $alreadyreviewed) && !has_capability('mod/moodleoverflow:editanypost',
$this->info->modulecontext)) {

throw new moodle_exception('maxtimehaspassed', 'moodleoverflow', '',
throw new \moodle_exception('maxtimehaspassed', 'moodleoverflow', '',
format_time(get_config('moodleoverflow', 'maxeditingtime')));
}

Expand All @@ -323,7 +324,7 @@ private function build_prepost_edit($editpostid) {
if (!has_capability('mod/moodleoverflow:editanypost', $this->info->modulecontext)) {

// Display the error. Capabilities are missing.
throw new moodle_exception('cannoteditposts', 'moodleoverflow');
throw new \moodle_exception('cannoteditposts', 'moodleoverflow');
}
}

Expand Down Expand Up @@ -354,7 +355,7 @@ private function build_prepost_delete($deletepostid) {
if (!(($this->info->relatedpost->get_userid() == $USER->id && $this->info->deleteownpost)
|| $this->info->deleteanypost)) {

throw new moodle_exception('cannotdeletepost', 'moodleoverflow');
throw new \moodle_exception('cannotdeletepost', 'moodleoverflow');
}

// Count all replies of this post.
Expand Down Expand Up @@ -384,7 +385,7 @@ private function execute_create($form, $errordestination) {
$this->prepost->subject, null, $this->prepost->userid,
$this->prepost->timenow, $this->prepost->timenow, $this->prepost->userid);
if (!$discussion->moodleoverflow_add_discussion($this->prepost)) {
throw new moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
throw new \moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
}

// The creation was successful.
Expand Down Expand Up @@ -418,7 +419,7 @@ private function execute_reply($form, $errordestination) {

// Create the new post.
if (!$newpostid = $this->info->discussion->moodleoverflow_add_post_to_discussion($this->prepost)) {
throw new moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
throw new \moodle_exception('couldnotadd', 'moodleoverflow', $errordestination);
}

// The creation was successful.
Expand Down Expand Up @@ -452,7 +453,7 @@ private function execute_edit($form, $errordestination) {

// Update the post.
if (!$this->info->discussion->moodleoverflow_edit_post_from_discussion($this->prepost)) {
throw new moodle_exception('couldnotupdate', 'moodleoverflow', $errordestination);
throw new \moodle_exception('couldnotupdate', 'moodleoverflow', $errordestination);
}

// The edit was successful.
Expand Down Expand Up @@ -491,12 +492,12 @@ public function execute_delete() {
$timepassed = time() - $this->info->relatedpost->created;
$url = new moodle_url('/mod/moodleoverflow/discussion.php', array('d' => $this->info->discussion->get_id()));
if (($timepassed > get_config('moodleoverflow', 'maxeditingtime')) && !$this->info->deleteanypost) {
throw new moodle_exception('cannotdeletepost', 'moodleoverflow', moodleoverflow_go_back_to($url));
throw new \moodle_exception('cannotdeletepost', 'moodleoverflow', moodleoverflow_go_back_to($url));
}

// A normal user cannot delete his post if there are direct replies.
if ($this->infro->replycount && !$this->info->deleteanypost) {
throw new moodle_exception('cannotdeletereplies', 'moodleoverflow', moodleoverflow_go_back_to($url));
throw new \moodle_exception('cannotdeletereplies', 'moodleoverflow', moodleoverflow_go_back_to($url));
}

// Check if the post is a parent post or not.
Expand Down Expand Up @@ -631,10 +632,6 @@ public function get_prepost() {
* @return bool, if object could be build or not.
*/
private function collect_information($postid = false, $moodleoverflowid = false) {
if (!($postid xor $moodleoverflowid)) {
throw new moodle_exception('inaccurateparameter', 'moodleoverflow');
return false;
}
if ($postid) {
// The related post is the post that is being answered, edited, or deleted.
$this->info->relatedpost = $this->check_post_exists($postid);
Expand All @@ -643,6 +640,11 @@ private function collect_information($postid = false, $moodleoverflowid = false)
} else {
$localmoodleoverflowid = $moodleoverflowid;
}
var_dump($moodleoverflowid);
var_dump($postid);
var_dump($this->info->relatedpost);
var_dump($this->info->discussion);
var_dump($localmoodleoverflowid);
$this->info->moodleoverflow = $this->check_moodleoverflow_exists($localmoodleoverflowid);
$this->info->course = $this->check_course_exists($this->info->moodleoverflow->course);
$this->info->cm = $this->check_coursemodule_exists($this->info->moodleoverflow->id, $this->info->course->id);
Expand Down Expand Up @@ -684,7 +686,7 @@ private function assemble_prepost() {
*/
private function check_interaction($interaction) {
if ($this->interaction != $interaction) {
throw new moodle_exception('wronginteraction' , 'moodleoverflow');
throw new \moodle_exception('wronginteraction' , 'moodleoverflow');
}
return true;
}
Expand All @@ -699,7 +701,7 @@ private function check_interaction($interaction) {
private function check_course_exists($courseid) {
global $DB;
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
throw new moodle_exception('invalidcourseid');
throw new \moodle_exception('invalidcourseid');
}
return $course;
}
Expand All @@ -713,7 +715,7 @@ private function check_course_exists($courseid) {
private function check_coursemodule_exists($moodleoverflowid, $courseid) {
if (!$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflowid,
$courseid)) {
throw new moodle_exception('invalidcoursemodule');
throw new \moodle_exception('invalidcoursemodule');
}
return $cm;
}
Expand All @@ -727,7 +729,7 @@ private function check_moodleoverflow_exists($moodleoverflowid) {
// Get the related moodleoverflow instance.
global $DB;
if (!$moodleoverflow = $DB->get_record('moodleoverflow', array('id' => $moodleoverflowid))) {
throw new moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
throw new \moodle_exception('invalidmoodleoverflowid', 'moodleoverflow');
}
return $moodleoverflow;
}
Expand All @@ -740,7 +742,7 @@ private function check_moodleoverflow_exists($moodleoverflowid) {
private function check_discussion_exists($discussionid) {
global $DB;
if (!$discussionrecord = $DB->get_record('moodleoverflow_discussions', array('id' => $discussionid))) {
throw new moodle_exception('invaliddiscussionid', 'moodleoverflow');
throw new \moodle_exception('invaliddiscussionid', 'moodleoverflow');
}
$discussion = discussion::from_record($discussionrecord);
return $discussion;
Expand All @@ -754,13 +756,12 @@ private function check_discussion_exists($discussionid) {
private function check_post_exists($postid) {
global $DB;
if (!$postrecord = $DB->get_record('moodleoverflow_posts', array('id' => $postid))) {
throw new moodle_exception('invalidpostid', 'moodleoverflow');
throw new \moodle_exception('invalidpostid', 'moodleoverflow');
}
$post = post::from_record($postrecord);
return $post;
}


// Capability checks.

/**
Expand All @@ -770,7 +771,7 @@ private function check_post_exists($postid) {
*/
private function check_user_can_create_discussion() {
if (!has_capability('mod/moodleoverflow:startdiscussion', $this->info->modulecontext)) {
throw new moodle_exception('cannotcreatediscussion', 'moodleoverflow');
throw new \moodle_exception('cannotcreatediscussion', 'moodleoverflow');
}
return true;
}
Expand All @@ -782,7 +783,7 @@ private function check_user_can_create_discussion() {
*/
private function check_user_can_create_reply() {
if (!has_capability('mod/moodleoverflow:replypost', $this->info->modulecontext, $this->prepost->userid)) {
throw new moodle_exception('cannotreply', 'moodleoverflow');
throw new \moodle_exception('cannotreply', 'moodleoverflow');
}
return true;
}
Expand All @@ -801,7 +802,7 @@ private function check_user_can_edit_post() {
$startdiscussion = has_capability('mod/moodleoverflow:startdiscussion', $this->info->modulecontext);
$ownpost = ($this->prepost->userid == $USER->id);
if (!(($ownpost && ($replypost || $startdiscussion)) || $editanypost)) {
throw new moodle_exception('cannotupdatepost', 'moodleoverflow');
throw new \moodle_exception('cannotupdatepost', 'moodleoverflow');
}
return true;
}
Expand Down
1 change: 0 additions & 1 deletion lang/en/moodleoverflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
$string['unexpectedinteractionerror'] = 'An unexpected error occured, please try again';

// String for the classes/post/post_control.php.
$string['inaccurateparameter'] = 'Please check your parameter and give exactly 1 parameter to the function';
$string['wronginteraction'] = 'Wrong interaction detected, please choose the right function';

// Strings for the classes/mod_form.php.
Expand Down
7 changes: 4 additions & 3 deletions post.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use mod_moodleoverflow\post\post_control;
use mod_moodleoverflow\review;

require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once($CFG->libdir . '/completionlib.php');

global $CFG, $USER, $DB, $PAGE, $SESSION, $OUTPUT;

require_once($CFG->libdir . '/completionlib.php');
// Declare optional url parameters.
$moodleoverflow = optional_param('moodleoverflow', 0, PARAM_INT);
$reply = optional_param('reply', 0, PARAM_INT);
Expand All @@ -52,8 +53,8 @@
$systemcontext = context_system::instance();

// Create a post_control object to control and lead the process.
$postcontrol = new \mod_moodleoverflow\post\post_control();
$postcontrol = new post_control();
//$postcontrol = new post_control();

// Put all interaction parameters in one object for the post_control.
$urlparameter = new \stdClass();
Expand Down

0 comments on commit 0eed562

Please sign in to comment.