function check_files() { 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")); $files = new InstructureCanvasAPI\Courses\Files\Files(); $files->setAPI($API); $course_id = get_post_meta($_POST['course'], "courseID", true); $files->setCourseID($course_id); $data = $files->getFiles(); $diff = false; $files_diff = array(); foreach ($data->content as $item) { global $wpdb; $data = $wpdb->get_results("select post_id from " . $wpdb->prefix . "postmeta where meta_key ='CanvasCourseIDFileID' and meta_value like '%" . $course_id . "," . $item->id . "%'"); if (count($data) == 0) { $diff = true; array_push($files_diff, $item); } } if ($diff) { echo json_encode(array($diff, $files_diff)); } else { echo json_encode(array($diff, $files_diff)); } } else { echo "Nonce failed"; } wp_die(); }
function media_added($post_id) { $post = get_post($post_id); $linked = get_post_meta($post->post_parent, "CanvasLinked", true); if ($linked == 1) { if (get_post_meta($post_id, "CanvasCourseIDFileID", true) == "") { $course_id = get_post_meta($post->post_parent, "CanvasCourse", true); global $wpdb; $course_post = $wpdb->get_results("select post_id from " . $wpdb->prefix . "postmeta where meta_key='CourseID' and meta_value = '" . $course_id . "'"); $course_post = get_post($course_post[0]->post_id); $file_system = get_post_meta($course_post->ID, "courseFileSystem", true); foreach ($file_system as $folder_id => $file_data) { if (strpos($file_data['actual_path'], $course_id . "/course files/") === FALSE) { $folder = $folder_id; $actual_path = $file_data['actual_path']; } } $file = get_attached_file($post_id); $parts = explode("/", $file); $new_name = $actual_path . "/" . array_pop($parts); rename($file, $new_name); $dir = wp_upload_dir(); $change_path = str_replace("\\", "/", $dir['basedir']) . "/"; $new_path = str_replace($change_path, "", $new_name); $data = wp_generate_attachment_metadata($post_id, $new_name); update_post_meta($post_id, "_wp_attached_file", $new_path); update_post_meta($post_id, "_wp_attachment_metadata", $data, 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" => get_post_meta($course_post->ID, "courseURL", true), "token" => get_post_meta($course_post->ID, "courseToken", true), "webService" => "CURL")); $files = new InstructureCanvasAPI\Courses\Files\Files(); $files->setAPI($API); $files->setCourseID($course_id); $parts = explode("/", $post->guid); $name = array_pop($parts); $data = $files->requestUpload(array("name" => $name, "content_type" => $post->post_mime_type, "parent_folder_id" => $folder)); $file_data = array("file" => $change_path . $new_path); $file_data = $files->upload($data->content->upload_url, $data->content->upload_params, $file_data); $header_parts = explode("\n", $file_data->header); foreach ($header_parts as $part) { if (strpos($part, "Location:") !== FALSE) { $confirm_url = trim(substr($part, strpos($part, ":") + 1)); } } $confirm_data = $files->confirm($confirm_url); $parts = explode("verifier=", $confirm_data->content->url); $verifier = array_pop($parts); update_post_meta($course_post->ID, "molie_file_" . $confirm_data->content->id, $post_id); update_post_meta($post_id, "CanvasCourse", $course_id); update_post_meta($post_id, "CanvasFileVerifier", $verifier); update_post_meta($post_id, "CanvasCourseIDFileID", $course_id . "," . $_POST['item']); update_post_meta($post_id, "CanvasFileURL", $confirm_data->content->url); } } }
private function get_file($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")); $file = new InstructureCanvasAPI\Courses\Files\Files(); $file->setAPI($API); $file->setCourseID(get_post_meta($post->ID, "courseID", true)); $file_data = $file->getFile($_POST['item']); return $file_data->content; }
function files() { if (!isset($_POST['molie-files-nonce'])) { $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-files"); foreach ($courses as $course) { ?> <form method="post" action="<?php echo admin_url("admin.php?page=molie_files"); ?> "> <p><?php echo $course->post_title; ?> </p> <input type="hidden" name="molie-files-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 Files"); ?> " /> </form> <?php } } } else { if (wp_verify_nonce($_POST['molie-files-nonce'], "molie-files")) { $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")); $folders = new InstructureCanvasAPI\Courses\Folders\Folders(); $folders->setAPI($API); $folders->setCourseID($course_id); $data = $folders->getFolders(); $folders = $data->content; $this->file_system = array(); foreach ($folders as $folder) { if ($folder->name == "course files") { $base_id = $folder->id; } if (!isset($this->file_system[$folder->id])) { $this->file_system[$folder->id] = array("name" => $folder->name, "id" => $folder->id, "url" => $folder->files_url); $this->file_system[$folder->id]['children'] = array(); if ($folder->parent_folder_id != "") { if (!isset($this->file_system[$folder->parent_folder_id])) { $this->file_system[$folder->parent_folder_id] = array(); $this->file_system[$folder->parent_folder_id]["children"] = array(); } array_push($this->file_system[$folder->parent_folder_id]['children'], $folder->id); } } else { $this->file_system[$folder->id]["name"] = $folder->name; $this->file_system[$folder->id]["id"] = $folder->id; $this->file_system[$folder->id]["url"] = $folder->files_url; if ($folder->parent_folder_id != "") { if (!isset($this->file_system[$folder->parent_folder_id])) { $this->file_system[$folder->parent_folder_id] = array(); $this->file_system[$folder->parent_folder_id]["children"] = array(); } array_push($this->file_system[$folder->parent_folder_id]['children'], $folder->id); } } } $upload_dir = wp_upload_dir(); $base_path = $upload_dir['basedir']; if (!is_dir($base_path . "/" . $course_id)) { mkdir($base_path . "/" . $course_id); } $this->make_folders($base_id, $base_path . "/" . $course_id); foreach ($this->file_system as $index => $id) { unset($this->file_system[$index]['name']); unset($this->file_system[$index]['id']); unset($this->file_system[$index]['children']); } delete_post_meta($_POST['course_ID'], "courseFileSystem"); add_post_meta($_POST['course_ID'], "courseFileSystem", $this->file_system, true); $files = new InstructureCanvasAPI\Courses\Files\Files(); $files->setAPI($API); $files->setCourseID($course_id); $data = $files->getFiles(); $files = $data->content; if ($data) { if (count($data->content) != 0) { echo "<div id='molie_choose'>"; echo "<h2>" . __("Files in this course") . "</h2>"; echo "<p>" . __("You should import all files") . "</p>"; 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_file_submit' value='" . __("Import files") . "' />"; echo "<input type='submit' id='molie_choose_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 $file) { echo "<li>"; if (isset($file->thumbnail_url)) { echo "<img src='" . $file->thumbnail_url . "' />"; } $file_size = $file->size / 1000; $file_size_parts = explode(".", $file_size); if (strlen($file_size_parts[0]) >= 4) { $file_size = $file->size / 1000000; $file_size = substr($file_size, 0, 4) . "MB"; } else { $file_size = substr($file_size, 0, 4) . "KB"; } if ($this->is_file_downloaded($_POST['course_ID'], $file)) { $checked = ""; $downloaded = __("Already Downloaded"); } else { $checked = "checked"; $downloaded = ""; } $parts = explode("verifier=", $file->url); $verifier = array_pop($parts); echo "<input verifier='" . $verifier . "' type='checkbox' " . $checked . " filename='" . $file->display_name . "' folder='" . $file->folder_id . "' url='" . $file->url . "' id='" . $file->id . "' course_post='" . $_POST['course_ID'] . "'>" . $file->display_name . " (" . $file_size . ")<span id='update" . $file->id . "'>" . $downloaded . "</span></li>"; } echo "</ul>"; echo "<input type='submit' id='molie_file_submit' value='" . __("Import files") . "' />"; echo "</form>"; echo "</div>"; echo "<div id='molie_quiz_assignments'>"; echo '<form method="post" action="' . admin_url("admin.php?page=molie_quiz") . '">'; echo "<input name='course_id' type='hidden' value='" . $_POST['course_ID'] . "' />"; echo wp_nonce_field("molie-quiz-nonce", "molie-quiz-nonce"); echo "<input type='submit' value='" . __("Now, Quizzes") . "' />"; echo "</form>"; echo "</div>"; } } $loader->unregister(); } } }