PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/portfolio/boxnet/lib.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 230 lines | 176 code | 27 blank | 27 comment | 38 complexity | b6c73914517606d0af1b8d69233c24fd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. require_once($CFG->libdir.'/portfolio/plugin.php');
  3. require_once($CFG->libdir.'/filelib.php');
  4. require_once($CFG->libdir.'/boxlib.php');
  5. class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
  6. public $boxclient;
  7. private $ticket;
  8. private $authtoken;
  9. private $folders;
  10. private $accounttree;
  11. public static function get_name() {
  12. return get_string('pluginname', 'portfolio_boxnet');
  13. }
  14. public function prepare_package() {
  15. // don't do anything for this plugin, we want to send all files as they are.
  16. }
  17. public function send_package() {
  18. // if we need to create the folder, do it now
  19. if ($newfolder = $this->get_export_config('newfolder')) {
  20. $created = $this->boxclient->create_folder($newfolder);
  21. if (empty($created->id)) {
  22. throw new portfolio_plugin_exception('foldercreatefailed', 'portfolio_boxnet');
  23. }
  24. $this->folders[$created->id] = $created->name;
  25. $this->set_export_config(array('folder' => $created->id));
  26. }
  27. foreach ($this->exporter->get_tempfiles() as $file) {
  28. $return = $this->boxclient->upload_file($file, $this->get_export_config('folder'));
  29. if (!empty($result->type) && $result->type == 'error') {
  30. throw new portfolio_plugin_exception('sendfailed', 'portfolio_boxnet', $result->message);
  31. }
  32. $createdfile = reset($return->entries);
  33. if (!empty($createdfile->id)) {
  34. $result = $this->rename_file($createdfile->id, $file->get_filename());
  35. // If this fails, the file was sent but not renamed.
  36. }
  37. }
  38. }
  39. public function get_export_summary() {
  40. $allfolders = $this->get_folder_list();
  41. if ($newfolder = $this->get_export_config('newfolder')) {
  42. $foldername = $newfolder . ' (' . get_string('tobecreated', 'portfolio_boxnet') . ')';
  43. } else if ($this->get_export_config('folder')) {
  44. $foldername = $allfolders[$this->get_export_config('folder')];
  45. } else {
  46. $foldername = '/';
  47. }
  48. return array(
  49. get_string('targetfolder', 'portfolio_boxnet') => s($foldername)
  50. );
  51. }
  52. public function get_interactive_continue_url() {
  53. return 'https://app.box.net/files/0/f/' . $this->get_export_config('folder') . '/';
  54. }
  55. public function expected_time($callertime) {
  56. // We're forcing this to be run 'interactively' because the plugin
  57. // does not support running in cron.
  58. return PORTFOLIO_TIME_LOW;
  59. }
  60. public static function has_admin_config() {
  61. return true;
  62. }
  63. public static function get_allowed_config() {
  64. return array('clientid', 'clientsecret');
  65. }
  66. public function has_export_config() {
  67. return true;
  68. }
  69. public function get_allowed_export_config() {
  70. return array('folder', 'newfolder');
  71. }
  72. public function export_config_form(&$mform) {
  73. $folders = $this->get_folder_list();
  74. $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
  75. $mform->setType('plugin_newfolder', PARAM_RAW);
  76. $folders[0] = '/';
  77. ksort($folders);
  78. $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
  79. }
  80. public function export_config_validation(array $data) {
  81. $allfolders = $this->get_folder_list();
  82. if (in_array($data['plugin_newfolder'], $allfolders)) {
  83. return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
  84. }
  85. }
  86. public static function admin_config_form(&$mform) {
  87. global $CFG;
  88. $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_boxnet'));
  89. $mform->addRule('clientid', get_string('required'), 'required', null, 'client');
  90. $mform->setType('clientid', PARAM_RAW_TRIMMED);
  91. $mform->addElement('text', 'clientsecret', get_string('clientsecret', 'portfolio_boxnet'));
  92. $mform->addRule('clientsecret', get_string('required'), 'required', null, 'client');
  93. $mform->setType('clientsecret', PARAM_RAW_TRIMMED);
  94. $a = new stdClass();
  95. $a->servicesurl = 'https://app.box.com/developers/services';
  96. $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_boxnet'),
  97. get_string('setupinfodetails', 'portfolio_boxnet', $a));
  98. if (strpos($CFG->wwwroot, 'https') !== 0) {
  99. $mform->addElement('static', 'warninghttps', '', get_string('warninghttps', 'portfolio_boxnet'));
  100. }
  101. }
  102. public function steal_control($stage) {
  103. if ($stage != PORTFOLIO_STAGE_CONFIG) {
  104. return false;
  105. }
  106. if (empty($this->boxclient)) {
  107. $returnurl = new moodle_url('/portfolio/add.php', array('postcontrol' => 1, 'type' => 'boxnet',
  108. 'sesskey' => sesskey()));
  109. $this->boxclient = new boxnet_client($this->get_config('clientid'), $this->get_config('clientsecret'), $returnurl, '');
  110. }
  111. if ($this->boxclient->is_logged_in()) {
  112. return false;
  113. }
  114. return $this->boxclient->get_login_url();
  115. }
  116. public function post_control($stage, $params) {
  117. if ($stage != PORTFOLIO_STAGE_CONFIG) {
  118. return;
  119. }
  120. if (!$this->boxclient->is_logged_in()) {
  121. throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
  122. }
  123. }
  124. /**
  125. * Get the folder list.
  126. *
  127. * This is limited to the folders in the root folder.
  128. *
  129. * @return array of folders.
  130. */
  131. protected function get_folder_list() {
  132. if (empty($this->folders)) {
  133. $folders = array();
  134. $result = $this->boxclient->get_folder_items();
  135. foreach ($result->entries as $item) {
  136. if ($item->type != 'folder') {
  137. continue;
  138. }
  139. $folders[$item->id] = $item->name;
  140. if (!empty($item->shared)) {
  141. $folders[$item->id] .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
  142. }
  143. }
  144. $this->folders = $folders;
  145. }
  146. return $this->folders;
  147. }
  148. /**
  149. * Rename a file.
  150. *
  151. * If the name is already taken, we append the current date to the file
  152. * to prevent name conflicts.
  153. *
  154. * @param int $fileid The file ID.
  155. * @param string $newname The new name.
  156. * @return bool Whether it succeeded or not.
  157. */
  158. protected function rename_file($fileid, $newname) {
  159. $result = $this->boxclient->rename_file($fileid, $newname);
  160. if (!empty($result->type) && $result->type == 'error') {
  161. $bits = explode('.', $newname);
  162. $suffix = '';
  163. if (count($bits) == 1) {
  164. $prefix = $newname;
  165. } else {
  166. $suffix = '.' . array_pop($bits);
  167. $prefix = implode('.', $bits);
  168. }
  169. $newname = $prefix . ' (' . date('Y-m-d H-i-s') . ')' . $suffix;
  170. $result = $this->boxclient->rename_file($fileid, $newname);
  171. if (empty($result->type) || $result->type != 'error') {
  172. return true;
  173. } else {
  174. // We could not rename the file for some reason...
  175. debugging('Error while renaming the file on Box.net', DEBUG_DEVELOPER);
  176. }
  177. } else {
  178. return true;
  179. }
  180. return false;
  181. }
  182. public function instance_sanity_check() {
  183. global $CFG;
  184. if (!$this->get_config('clientid') || !$this->get_config('clientsecret')) {
  185. return 'missingoauthkeys';
  186. } else if (strpos($CFG->wwwroot, 'https') !== 0) {
  187. return 'missinghttps';
  188. }
  189. }
  190. public static function allows_multiple_instances() {
  191. return false;
  192. }
  193. public function supported_formats() {
  194. return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
  195. }
  196. /*
  197. * for now , boxnet doesn't support this,
  198. * because we can't dynamically construct return urls.
  199. */
  200. public static function allows_multiple_exports() {
  201. return false;
  202. }
  203. }