PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/plagiarismlib.php

http://github.com/moodle/moodle
PHP | 219 lines | 132 code | 13 blank | 74 comment | 18 complexity | 4e67fa9ef31c07a91b1d11dcfb4cb29f MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. return $output;
  48. }
  49. /**
  50. * returns array of plagiarism details about specified file
  51. *
  52. * @param int $cmid
  53. * @param int $userid
  54. * @param object $file moodle file object
  55. * @return array - sets of details about specified file, one array of details per plagiarism plugin
  56. * - each set contains at least 'analyzed', 'score', 'reporturl'
  57. */
  58. function plagiarism_get_file_results($cmid, $userid, $file) {
  59. global $CFG;
  60. $allresults = array();
  61. if (empty($CFG->enableplagiarism)) {
  62. return $allresults;
  63. }
  64. $plagiarismplugins = plagiarism_load_available_plugins();
  65. foreach ($plagiarismplugins as $plugin => $dir) {
  66. require_once($dir.'/lib.php');
  67. $plagiarismclass = "plagiarism_plugin_$plugin";
  68. $plagiarismplugin = new $plagiarismclass;
  69. $allresults[] = $plagiarismplugin->get_file_results($cmid, $userid, $file);
  70. }
  71. return $allresults;
  72. }
  73. /**
  74. * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
  75. *
  76. * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_edit_post_actions() instead.
  77. * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.3
  78. * @param object $data - form data
  79. */
  80. function plagiarism_save_form_elements($data) {
  81. global $CFG;
  82. if (empty($CFG->enableplagiarism)) {
  83. return '';
  84. }
  85. $plagiarismplugins = plagiarism_load_available_plugins();
  86. foreach ($plagiarismplugins as $plugin => $dir) {
  87. require_once($dir.'/lib.php');
  88. $plagiarismclass = "plagiarism_plugin_$plugin";
  89. $plagiarismplugin = new $plagiarismclass;
  90. $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'save_form_elements');
  91. if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
  92. $text = 'plagiarism_plugin::save_form_elements() is deprecated.';
  93. $text .= ' Use plagiarism_' . $plugin . '_coursemodule_edit_post_actions() instead';
  94. debugging($text, DEBUG_DEVELOPER);
  95. }
  96. $plagiarismplugin->save_form_elements($data);
  97. }
  98. }
  99. /**
  100. * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
  101. *
  102. * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_standard_elements() instead.
  103. * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.3
  104. * @param object $mform - Moodle form object
  105. * @param object $context - context object
  106. * @param string $modulename - Name of the module
  107. */
  108. function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
  109. global $CFG;
  110. if (empty($CFG->enableplagiarism)) {
  111. return '';
  112. }
  113. $plagiarismplugins = plagiarism_load_available_plugins();
  114. foreach ($plagiarismplugins as $plugin => $dir) {
  115. require_once($dir.'/lib.php');
  116. $plagiarismclass = "plagiarism_plugin_$plugin";
  117. $plagiarismplugin = new $plagiarismclass;
  118. $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'get_form_elements_module');
  119. if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
  120. $text = 'plagiarism_plugin::get_form_elements_module() is deprecated.';
  121. $text .= ' Use plagiarism_' . $plugin . '_coursemodule_standard_elements() instead';
  122. debugging($text, DEBUG_DEVELOPER);
  123. }
  124. $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
  125. }
  126. }
  127. /**
  128. * updates the status of all files within a module
  129. *
  130. * @param object $course - full Course object
  131. * @param object $cm - full cm object
  132. * @return string
  133. */
  134. function plagiarism_update_status($course, $cm) {
  135. global $CFG;
  136. if (empty($CFG->enableplagiarism)) {
  137. return '';
  138. }
  139. $plagiarismplugins = plagiarism_load_available_plugins();
  140. $output = '';
  141. foreach ($plagiarismplugins as $plugin => $dir) {
  142. require_once($dir.'/lib.php');
  143. $plagiarismclass = "plagiarism_plugin_$plugin";
  144. $plagiarismplugin = new $plagiarismclass;
  145. $output .= $plagiarismplugin->update_status($course, $cm);
  146. }
  147. return $output;
  148. }
  149. /**
  150. * Function that prints the student disclosure notifying that the files will be checked for plagiarism
  151. * @param integer $cmid - the cmid of this module
  152. * @return string
  153. */
  154. function plagiarism_print_disclosure($cmid) {
  155. global $CFG;
  156. if (empty($CFG->enableplagiarism)) {
  157. return '';
  158. }
  159. $plagiarismplugins = plagiarism_load_available_plugins();
  160. $output = '';
  161. foreach ($plagiarismplugins as $plugin => $dir) {
  162. require_once($dir.'/lib.php');
  163. $plagiarismclass = "plagiarism_plugin_$plugin";
  164. $plagiarismplugin = new $plagiarismclass;
  165. $output .= $plagiarismplugin->print_disclosure($cmid);
  166. }
  167. return $output;
  168. }
  169. /**
  170. * Helper function - also loads lib file of plagiarism plugin
  171. *
  172. * @todo MDL-67872 the deprecated code in this function to be removed in Moodle 4.3
  173. * @return array of available plugins
  174. */
  175. function plagiarism_load_available_plugins() {
  176. global $CFG;
  177. static $showndeprecatedmessage = array(); // Only show message once per page load.
  178. if (empty($CFG->enableplagiarism)) {
  179. return array();
  180. }
  181. $plagiarismplugins = core_component::get_plugin_list('plagiarism');
  182. $availableplugins = array();
  183. foreach ($plagiarismplugins as $plugin => $dir) {
  184. // Check this plugin is enabled and a lib file exists.
  185. if (get_config('plagiarism', $plugin."_use")) {
  186. // Deprecated Since Moodle 3.9.
  187. $pluginenabled = true;
  188. if (empty($showndeprecatedmessage[$plugin])) {
  189. $text = 'The setting plagiarism:'.$plugin.'_use is deprecated.';
  190. $text .= ' Use plagiarism_' . $plugin . ':enabled instead';
  191. debugging($text, DEBUG_DEVELOPER);
  192. $showndeprecatedmessage[$plugin] = true;
  193. }
  194. } else {
  195. $pluginenabled = get_config('plagiarism_'.$plugin, 'enabled');
  196. }
  197. if ($pluginenabled && file_exists($dir."/lib.php")) {
  198. require_once($dir.'/lib.php');
  199. $plagiarismclass = "plagiarism_plugin_$plugin";
  200. if (class_exists($plagiarismclass)) {
  201. $availableplugins[$plugin] = $dir;
  202. }
  203. }
  204. }
  205. return $availableplugins;
  206. }