function get_discussion($post)
 {
     require_once __DIR__ . "/../../API/Psr4AutoloaderClass.php";
     $loader = new Psr4AutoloaderClass();
     $loader->register();
     $loader->addNamespace('InstructureCanvasAPI', __DIR__ . "/../../API/InstructureCanvasAPI/src");
     $API = new InstructureCanvasAPI\InstructureCanvasAPI(array("site" => get_post_meta($post->ID, "courseURL", true), "token" => get_post_meta($post->ID, "courseToken", true), "webService" => "CURL"));
     $discussion = new InstructureCanvasAPI\Courses\DiscussionTopics\DiscussionTopics();
     $discussion->setAPI($API);
     $discussion->setCourseID(get_post_meta($post->ID, "courseID", true));
     $data = $discussion->getDiscussionTopic($_POST['item']);
     $discussion = $data->content;
     $loader->unregister();
     return $discussion;
 }
 function check_discussions()
 {
     if (wp_verify_nonce($_POST['nonce'], "molie_admin_check")) {
         require_once __DIR__ . "/../../API/Psr4AutoloaderClass.php";
         $loader = new Psr4AutoloaderClass();
         $loader->register();
         $loader->addNamespace('InstructureCanvasAPI', __DIR__ . "/../../API/InstructureCanvasAPI/src");
         $API = new InstructureCanvasAPI\InstructureCanvasAPI(array("site" => get_post_meta($_POST['course'], "courseURL", true), "token" => get_post_meta($_POST['course'], "courseToken", true), "webService" => "CURL"));
         $course_id = get_post_meta($_POST['course'], "courseID", true);
         $discussions = new InstructureCanvasAPI\Courses\DiscussionTopics\DiscussionTopics();
         $discussions->setAPI($API);
         $discussions->setCourseID($course_id);
         $data = $discussions->getDiscussionTopics();
         $diff = false;
         $diss_diff = array();
         foreach ($data->content as $item) {
             global $wpdb;
             $data = $wpdb->get_results("select post_id from " . $wpdb->prefix . "postmeta where meta_key ='canvasDiscussionURL' and meta_value like '%" . $item->html_url . "%'");
             if (count($data) == 0) {
                 $diff = true;
                 array_push($diss_diff, $item);
             }
         }
         if ($diff) {
             echo json_encode(array($diff, $diss_diff));
         } else {
             echo json_encode(array($diff, $diss_diff));
         }
     } else {
         echo "Nonce failed";
     }
     wp_die();
 }
    function discussion()
    {
        if (isset($_POST['molie-discussion-nonce'])) {
            if (wp_verify_nonce($_POST['molie-discussion-nonce'], "molie-discussion-nonce")) {
                $course_id = get_post_meta($_POST['course_id'], "courseID", true);
                $course_token = get_post_meta($_POST['course_id'], "courseToken", true);
                $course_url = get_post_meta($_POST['course_id'], "courseURL", true);
                require_once __DIR__ . "/../../API/Psr4AutoloaderClass.php";
                $loader = new Psr4AutoloaderClass();
                $loader->register();
                $loader->addNamespace('InstructureCanvasAPI', __DIR__ . "/../../API/InstructureCanvasAPI/src");
                $API = new InstructureCanvasAPI\InstructureCanvasAPI(array("site" => $course_url, "token" => $course_token, "webService" => "CURL"));
                $discussions = new InstructureCanvasAPI\Courses\DiscussionTopics\DiscussionTopics();
                $discussions->setAPI($API);
                $discussions->setCourseID($course_id);
                $data = $discussions->getDiscussionTopics();
                if ($data) {
                    if (count($data->content) != 0) {
                        echo "<div id='molie_choose'>";
                        echo "<h2>" . __("Discussions in this course") . "</h2>";
                        echo "<div id='importProgress'><p><strong>" . __("Import Progress") . " <span id='importTotal'></span></strong></p><div id='importProgressBar'></div></div>";
                        echo '<form id="molie_choose_form" action="javascript:function connect(){return false;};">';
                        echo "<input type='submit' id='molie_discussion_submit' value='" . __("Link discussions") . "' />";
                        echo "<input type='submit' id='molie_discussion_skip' value='" . __("Skip step") . "' />";
                        echo "<p><span><a href='javascript:molie_select_all()'>" . __("Select All") . "</a></span> <span><a href='javascript:molie_unselect_all()'>" . __("Unselect All") . "</a></span></p>";
                        echo "<ul>";
                        foreach ($data->content as $discussion) {
                            echo "<li>";
                            if (get_post_meta($_POST['course_id'], "canvasDiscussion_" . $discussion->id, true) != "") {
                                $link_checked = "";
                                $link_info = __("discussion already linked");
                            } else {
                                $link_checked = "checked";
                                $link_info = "";
                            }
                            echo "<input type='checkbox' " . $link_checked . " id='" . $discussion->id . "' course_post='" . $_POST['course_id'] . "'>" . $discussion->title . "<span id='update" . $discussion->id . "'>" . $link_info . "</span></li>";
                        }
                        echo "</ul>";
                        echo "<input type='submit' id='molie_discussion_submit' value='" . __("Link discussions") . "' />";
                        echo "</form>";
                        echo "</div>";
                        echo "<div id='molie_roster' style='display:none'>";
                        echo '<form method="post" action="' . admin_url("admin.php?page=molie_roster") . '">';
                        echo "<input name='course_id' type='hidden' value='" . $_POST['course_id'] . "' />";
                        echo wp_nonce_field("molie-roster-nonce", "molie-roster-nonce");
                        echo "<input type='submit' value='" . __("Link roster") . "' />";
                        echo "</form>";
                        echo "</div>";
                    }
                }
                $loader->unregister();
            } else {
                print_r($_POST);
            }
        } else {
            $args = array("post_type" => "linkedcanvascourse", "post_status" => "publish");
            $courses = get_posts($args);
            if (count($courses) != 0) {
                echo "<h2>" . __("Choose a course") . "</h2>";
                $nonce = wp_create_nonce("molie-discussion-nonce");
                foreach ($courses as $course) {
                    ?>
						<form method="post" action='<?php 
                    echo admin_url("admin.php?page=molie_discussions");
                    ?>
'>
							<p><?php 
                    echo $course->post_title;
                    ?>
</p>
							<input type="hidden" name="molie-discussion-nonce" value="<?php 
                    echo $nonce;
                    ?>
"/>
							<input type="hidden" name="url" value="<?php 
                    echo get_post_meta($course->ID, "courseURL", true);
                    ?>
" />
							<input type="hidden" name="token" value="<?php 
                    echo get_post_meta($course->ID, "courseToken", true);
                    ?>
" />
							<input name="course_id" type="hidden" value="<?php 
                    echo $course->ID;
                    ?>
" />
							<input type="submit" value="<?php 
                    echo __("Get Discussions");
                    ?>
" />
						</form>
					<?php 
                }
            }
        }
    }