MDL-76843 quiz: add test to verify random essay stats now work
[moodle.git] / lib / plagiarismlib.php
blobae89d2558ed55bd38dfd2486e3a3b3d16716c2cf
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.
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/>.
17 /**
18 * plagiarismlib.php - Contains core Plagiarism related functions.
20 * @since Moodle 2.0
21 * @package core
22 * @subpackage plagiarism
23 * @copyright 2010 Dan Marsden http://danmarsden.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.');
31 /**
32 * displays the similarity score and provides a link to the full report if allowed.
34 * @param object $linkarray contains all relevant information for the plugin to generate a link
35 * @return string - url to allow login/viewing of a similarity report
37 function plagiarism_get_links($linkarray) {
38 global $CFG;
39 if (empty($CFG->enableplagiarism)) {
40 return '';
42 $plagiarismplugins = plagiarism_load_available_plugins();
43 $output = '';
44 foreach ($plagiarismplugins as $plugin => $dir) {
45 require_once($dir.'/lib.php');
46 $plagiarismclass = "plagiarism_plugin_$plugin";
47 $plagiarismplugin = new $plagiarismclass;
48 $output .= $plagiarismplugin->get_links($linkarray);
50 if (!empty($output)) {
51 return html_writer::span($output, 'core_plagiarism_links');
53 return '';
56 /**
57 * returns array of plagiarism details about specified file
59 * @deprecated Since Moodle 4.0. - this function was a placeholder and not used in core.
60 * @todo MDL-71326 This is to be moved from here to deprecatedlib.php in Moodle 4.4
61 * @param int $cmid
62 * @param int $userid
63 * @param object $file moodle file object
64 * @return array - sets of details about specified file, one array of details per plagiarism plugin
65 * - each set contains at least 'analyzed', 'score', 'reporturl'
67 function plagiarism_get_file_results($cmid, $userid, $file) {
68 global $CFG;
69 $text = 'plagiarism_get_file_results is deprecated, please use plagiarism_get_links() or plugin specific functions.';
70 debugging($text, DEBUG_DEVELOPER);
71 $allresults = array();
72 if (empty($CFG->enableplagiarism)) {
73 return $allresults;
75 $plagiarismplugins = plagiarism_load_available_plugins();
76 foreach ($plagiarismplugins as $plugin => $dir) {
77 require_once($dir.'/lib.php');
78 $plagiarismclass = "plagiarism_plugin_$plugin";
79 $plagiarismplugin = new $plagiarismclass;
80 $allresults[] = $plagiarismplugin->get_file_results($cmid, $userid, $file);
82 return $allresults;
85 /**
86 * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
88 * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_edit_post_actions() instead.
89 * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.1
90 * @param object $data - form data
92 function plagiarism_save_form_elements($data) {
93 global $CFG;
94 if (empty($CFG->enableplagiarism)) {
95 return '';
97 $plagiarismplugins = plagiarism_load_available_plugins();
98 foreach ($plagiarismplugins as $plugin => $dir) {
99 require_once($dir.'/lib.php');
100 $plagiarismclass = "plagiarism_plugin_$plugin";
101 $plagiarismplugin = new $plagiarismclass;
103 $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'save_form_elements');
104 if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
105 $text = 'plagiarism_plugin::save_form_elements() is deprecated.';
106 $text .= ' Use plagiarism_' . $plugin . '_coursemodule_edit_post_actions() instead';
107 debugging($text, DEBUG_DEVELOPER);
110 $plagiarismplugin->save_form_elements($data);
115 * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
117 * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_standard_elements() instead.
118 * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.1
119 * @param object $mform - Moodle form object
120 * @param object $context - context object
121 * @param string $modulename - Name of the module
123 function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
124 global $CFG;
125 if (empty($CFG->enableplagiarism)) {
126 return '';
128 $plagiarismplugins = plagiarism_load_available_plugins();
129 foreach ($plagiarismplugins as $plugin => $dir) {
130 require_once($dir.'/lib.php');
131 $plagiarismclass = "plagiarism_plugin_$plugin";
132 $plagiarismplugin = new $plagiarismclass;
134 $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'get_form_elements_module');
135 if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
136 $text = 'plagiarism_plugin::get_form_elements_module() is deprecated.';
137 $text .= ' Use plagiarism_' . $plugin . '_coursemodule_standard_elements() instead';
138 debugging($text, DEBUG_DEVELOPER);
141 $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
145 * Allows a plagiarism plugin to print a button/link at the top of activity overview report pages.
147 * @deprecated Since Moodle 4.0 - Please use {plugin name}_before_standard_top_of_body_html instead.
148 * @todo MDL-71326 Remove this method.
149 * @param object $course - full Course object
150 * @param object $cm - full cm object
151 * @return string
153 function plagiarism_update_status($course, $cm) {
154 global $CFG;
155 if (empty($CFG->enableplagiarism)) {
156 return '';
158 $plagiarismplugins = plagiarism_load_available_plugins();
159 $output = '';
160 foreach ($plagiarismplugins as $plugin => $dir) {
161 require_once($dir.'/lib.php');
162 $plagiarismclass = "plagiarism_plugin_$plugin";
163 $plagiarismplugin = new $plagiarismclass;
165 $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'update_status');
166 if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
167 $text = 'plagiarism_plugin::update_status() is deprecated.';
168 $text .= ' Use plagiarism_' . $plugin . '_before_standard_top_of_body_html() instead';
169 debugging($text, DEBUG_DEVELOPER);
171 $output .= $plagiarismplugin->update_status($course, $cm);
173 return $output;
177 * Function that prints the student disclosure notifying that the files will be checked for plagiarism
178 * @param integer $cmid - the cmid of this module
179 * @return string
181 function plagiarism_print_disclosure($cmid) {
182 global $CFG;
183 if (empty($CFG->enableplagiarism)) {
184 return '';
186 $plagiarismplugins = plagiarism_load_available_plugins();
187 $output = '';
188 foreach ($plagiarismplugins as $plugin => $dir) {
189 require_once($dir.'/lib.php');
190 $plagiarismclass = "plagiarism_plugin_$plugin";
191 $plagiarismplugin = new $plagiarismclass;
192 $output .= $plagiarismplugin->print_disclosure($cmid);
194 return $output;
198 * Helper function - also loads lib file of plagiarism plugin
200 * @todo MDL-67872 the deprecated code in this function to be removed in Moodle 4.1
201 * @return array of available plugins
203 function plagiarism_load_available_plugins() {
204 global $CFG;
205 static $showndeprecatedmessage = array(); // Only show message once per page load.
207 if (empty($CFG->enableplagiarism)) {
208 return array();
210 $plagiarismplugins = core_component::get_plugin_list('plagiarism');
211 $availableplugins = array();
212 foreach ($plagiarismplugins as $plugin => $dir) {
213 // Check this plugin is enabled and a lib file exists.
214 if (get_config('plagiarism', $plugin."_use")) {
215 // Deprecated Since Moodle 3.9.
216 $pluginenabled = true;
217 if (empty($showndeprecatedmessage[$plugin])) {
218 $text = 'The setting plagiarism:'.$plugin.'_use is deprecated.';
219 $text .= ' Use plagiarism_' . $plugin . ':enabled instead';
220 debugging($text, DEBUG_DEVELOPER);
221 $showndeprecatedmessage[$plugin] = true;
223 } else {
224 $pluginenabled = get_config('plagiarism_'.$plugin, 'enabled');
226 if ($pluginenabled && file_exists($dir."/lib.php")) {
227 require_once($dir.'/lib.php');
228 $plagiarismclass = "plagiarism_plugin_$plugin";
229 if (class_exists($plagiarismclass)) {
230 $availableplugins[$plugin] = $dir;
234 return $availableplugins;