PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plagiarismlib.php

https://bitbucket.org/moodle/moodle
PHP | 235 lines | 143 code | 14 blank | 78 comment | 21 complexity | aa8209ab76e44153d1e4e2654c7e8a60 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * plagiarismlib.php - Contains core Plagiarism related functions.
  18. *
  19. * @since Moodle 2.0
  20. * @package core
  21. * @subpackage plagiarism
  22. * @copyright 2010 Dan Marsden http://danmarsden.com
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. if (!defined('MOODLE_INTERNAL')) {
  26. die('Direct access to this script is forbidden.');
  27. }
  28. /**
  29. * displays the similarity score and provides a link to the full report if allowed.
  30. *
  31. * @param object $linkarray contains all relevant information for the plugin to generate a link
  32. * @return string - url to allow login/viewing of a similarity report
  33. */
  34. function plagiarism_get_links($linkarray) {
  35. global $CFG;
  36. if (empty($CFG->enableplagiarism)) {
  37. return '';
  38. }
  39. $plagiarismplugins = plagiarism_load_available_plugins();
  40. $output = '';
  41. foreach ($plagiarismplugins as $plugin => $dir) {
  42. require_once($dir.'/lib.php');
  43. $plagiarismclass = "plagiarism_plugin_$plugin";
  44. $plagiarismplugin = new $plagiarismclass;
  45. $output .= $plagiarismplugin->get_links($linkarray);
  46. }
  47. if (!empty($output)) {
  48. return html_writer::span($output, 'core_plagiarism_links');
  49. }
  50. return '';
  51. }
  52. /**
  53. * returns array of plagiarism details about specified file
  54. *
  55. * @deprecated Since Moodle 4.0. - this function was a placeholder and not used in core.
  56. * @todo MDL-71326 This is to be moved from here to deprecatedlib.php in Moodle 4.4
  57. * @param int $cmid
  58. * @param int $userid
  59. * @param object $file moodle file object
  60. * @return array - sets of details about specified file, one array of details per plagiarism plugin
  61. * - each set contains at least 'analyzed', 'score', 'reporturl'
  62. */
  63. function plagiarism_get_file_results($cmid, $userid, $file) {
  64. global $CFG;
  65. $text = 'plagiarism_get_file_results is deprecated, please use plagiarism_get_links() or plugin specific functions.';
  66. debugging($text, DEBUG_DEVELOPER);
  67. $allresults = array();
  68. if (empty($CFG->enableplagiarism)) {
  69. return $allresults;
  70. }
  71. $plagiarismplugins = plagiarism_load_available_plugins();
  72. foreach ($plagiarismplugins as $plugin => $dir) {
  73. require_once($dir.'/lib.php');
  74. $plagiarismclass = "plagiarism_plugin_$plugin";
  75. $plagiarismplugin = new $plagiarismclass;
  76. $allresults[] = $plagiarismplugin->get_file_results($cmid, $userid, $file);
  77. }
  78. return $allresults;
  79. }
  80. /**
  81. * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
  82. *
  83. * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_edit_post_actions() instead.
  84. * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.1
  85. * @param object $data - form data
  86. */
  87. function plagiarism_save_form_elements($data) {
  88. global $CFG;
  89. if (empty($CFG->enableplagiarism)) {
  90. return '';
  91. }
  92. $plagiarismplugins = plagiarism_load_available_plugins();
  93. foreach ($plagiarismplugins as $plugin => $dir) {
  94. require_once($dir.'/lib.php');
  95. $plagiarismclass = "plagiarism_plugin_$plugin";
  96. $plagiarismplugin = new $plagiarismclass;
  97. $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'save_form_elements');
  98. if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
  99. $text = 'plagiarism_plugin::save_form_elements() is deprecated.';
  100. $text .= ' Use plagiarism_' . $plugin . '_coursemodule_edit_post_actions() instead';
  101. debugging($text, DEBUG_DEVELOPER);
  102. }
  103. $plagiarismplugin->save_form_elements($data);
  104. }
  105. }
  106. /**
  107. * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
  108. *
  109. * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_standard_elements() instead.
  110. * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.1
  111. * @param object $mform - Moodle form object
  112. * @param object $context - context object
  113. * @param string $modulename - Name of the module
  114. */
  115. function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
  116. global $CFG;
  117. if (empty($CFG->enableplagiarism)) {
  118. return '';
  119. }
  120. $plagiarismplugins = plagiarism_load_available_plugins();
  121. foreach ($plagiarismplugins as $plugin => $dir) {
  122. require_once($dir.'/lib.php');
  123. $plagiarismclass = "plagiarism_plugin_$plugin";
  124. $plagiarismplugin = new $plagiarismclass;
  125. $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'get_form_elements_module');
  126. if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
  127. $text = 'plagiarism_plugin::get_form_elements_module() is deprecated.';
  128. $text .= ' Use plagiarism_' . $plugin . '_coursemodule_standard_elements() instead';
  129. debugging($text, DEBUG_DEVELOPER);
  130. }
  131. $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
  132. }
  133. }
  134. /**
  135. * Allows a plagiarism plugin to print a button/link at the top of activity overview report pages.
  136. *
  137. * @deprecated Since Moodle 4.0 - Please use {plugin name}_before_standard_top_of_body_html instead.
  138. * @todo MDL-71326 Remove this method.
  139. * @param object $course - full Course object
  140. * @param object $cm - full cm object
  141. * @return string
  142. */
  143. function plagiarism_update_status($course, $cm) {
  144. global $CFG;
  145. if (empty($CFG->enableplagiarism)) {
  146. return '';
  147. }
  148. $plagiarismplugins = plagiarism_load_available_plugins();
  149. $output = '';
  150. foreach ($plagiarismplugins as $plugin => $dir) {
  151. require_once($dir.'/lib.php');
  152. $plagiarismclass = "plagiarism_plugin_$plugin";
  153. $plagiarismplugin = new $plagiarismclass;
  154. $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'update_status');
  155. if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
  156. $text = 'plagiarism_plugin::update_status() is deprecated.';
  157. $text .= ' Use plagiarism_' . $plugin . '_before_standard_top_of_body_html() instead';
  158. debugging($text, DEBUG_DEVELOPER);
  159. }
  160. $output .= $plagiarismplugin->update_status($course, $cm);
  161. }
  162. return $output;
  163. }
  164. /**
  165. * Function that prints the student disclosure notifying that the files will be checked for plagiarism
  166. * @param integer $cmid - the cmid of this module
  167. * @return string
  168. */
  169. function plagiarism_print_disclosure($cmid) {
  170. global $CFG;
  171. if (empty($CFG->enableplagiarism)) {
  172. return '';
  173. }
  174. $plagiarismplugins = plagiarism_load_available_plugins();
  175. $output = '';
  176. foreach ($plagiarismplugins as $plugin => $dir) {
  177. require_once($dir.'/lib.php');
  178. $plagiarismclass = "plagiarism_plugin_$plugin";
  179. $plagiarismplugin = new $plagiarismclass;
  180. $output .= $plagiarismplugin->print_disclosure($cmid);
  181. }
  182. return $output;
  183. }
  184. /**
  185. * Helper function - also loads lib file of plagiarism plugin
  186. *
  187. * @todo MDL-67872 the deprecated code in this function to be removed in Moodle 4.1
  188. * @return array of available plugins
  189. */
  190. function plagiarism_load_available_plugins() {
  191. global $CFG;
  192. static $showndeprecatedmessage = array(); // Only show message once per page load.
  193. if (empty($CFG->enableplagiarism)) {
  194. return array();
  195. }
  196. $plagiarismplugins = core_component::get_plugin_list('plagiarism');
  197. $availableplugins = array();
  198. foreach ($plagiarismplugins as $plugin => $dir) {
  199. // Check this plugin is enabled and a lib file exists.
  200. if (get_config('plagiarism', $plugin."_use")) {
  201. // Deprecated Since Moodle 3.9.
  202. $pluginenabled = true;
  203. if (empty($showndeprecatedmessage[$plugin])) {
  204. $text = 'The setting plagiarism:'.$plugin.'_use is deprecated.';
  205. $text .= ' Use plagiarism_' . $plugin . ':enabled instead';
  206. debugging($text, DEBUG_DEVELOPER);
  207. $showndeprecatedmessage[$plugin] = true;
  208. }
  209. } else {
  210. $pluginenabled = get_config('plagiarism_'.$plugin, 'enabled');
  211. }
  212. if ($pluginenabled && file_exists($dir."/lib.php")) {
  213. require_once($dir.'/lib.php');
  214. $plagiarismclass = "plagiarism_plugin_$plugin";
  215. if (class_exists($plagiarismclass)) {
  216. $availableplugins[$plugin] = $dir;
  217. }
  218. }
  219. }
  220. return $availableplugins;
  221. }