Merge branch 'MDL-71174' of git://github.com/danmarsden/moodle
[moodle.git] / lib / plagiarismlib.php
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/>.
17 /**
18  * plagiarismlib.php - Contains core Plagiarism related functions.
19  *
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
25  */
27 if (!defined('MOODLE_INTERNAL')) {
28     die('Direct access to this script is forbidden.');
29 }
31 /**
32  * displays the similarity score and provides a link to the full report if allowed.
33  *
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
36  */
37 function plagiarism_get_links($linkarray) {
38     global $CFG;
39     if (empty($CFG->enableplagiarism)) {
40         return '';
41     }
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);
49     }
50     if (!empty($output)) {
51         return html_writer::span($output, 'core_plagiarism_links');
52     }
53     return '';
54 }
56 /**
57  * returns array of plagiarism details about specified file
58  *
59  * @param int $cmid
60  * @param int $userid
61  * @param object $file moodle file object
62  * @return array - sets of details about specified file, one array of details per plagiarism plugin
63  *  - each set contains at least 'analyzed', 'score', 'reporturl'
64  */
65 function plagiarism_get_file_results($cmid, $userid, $file) {
66     global $CFG;
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 }
81 /**
82  * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
83  *
84  * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_edit_post_actions() instead.
85  * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.1
86  * @param object $data - form data
87  */
88 function plagiarism_save_form_elements($data) {
89     global $CFG;
90     if (empty($CFG->enableplagiarism)) {
91         return '';
92     }
93     $plagiarismplugins = plagiarism_load_available_plugins();
94     foreach ($plagiarismplugins as $plugin => $dir) {
95         require_once($dir.'/lib.php');
96         $plagiarismclass = "plagiarism_plugin_$plugin";
97         $plagiarismplugin = new $plagiarismclass;
99         $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'save_form_elements');
100         if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
101             $text = 'plagiarism_plugin::save_form_elements() is deprecated.';
102             $text .= ' Use plagiarism_' . $plugin . '_coursemodule_edit_post_actions() instead';
103             debugging($text, DEBUG_DEVELOPER);
104         }
106         $plagiarismplugin->save_form_elements($data);
107     }
110 /**
111  * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
112  *
113  * @deprecated Since Moodle 3.9. MDL-65835 Please use {plugin name}_coursemodule_standard_elements() instead.
114  * @todo MDL-67526 This is to be moved from here to deprecatedlib.php in Moodle 4.1
115  * @param object $mform - Moodle form object
116  * @param object $context - context object
117  * @param string $modulename - Name of the module
118  */
119 function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
120     global $CFG;
121     if (empty($CFG->enableplagiarism)) {
122         return '';
123     }
124     $plagiarismplugins = plagiarism_load_available_plugins();
125     foreach ($plagiarismplugins as $plugin => $dir) {
126         require_once($dir.'/lib.php');
127         $plagiarismclass = "plagiarism_plugin_$plugin";
128         $plagiarismplugin = new $plagiarismclass;
130         $reflectionmethod = new ReflectionMethod($plagiarismplugin, 'get_form_elements_module');
131         if ($reflectionmethod->getDeclaringClass()->getName() == get_class($plagiarismplugin)) {
132             $text = 'plagiarism_plugin::get_form_elements_module() is deprecated.';
133             $text .= ' Use plagiarism_' . $plugin . '_coursemodule_standard_elements() instead';
134             debugging($text, DEBUG_DEVELOPER);
135         }
137         $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
138     }
140 /**
141  * Allows a plagiarism plugin to print a button/link at the top of activity overview report pages.
142  *
143  * @param object $course - full Course object
144  * @param object $cm - full cm object
145  * @return string
146  */
147 function plagiarism_update_status($course, $cm) {
148     global $CFG;
149     if (empty($CFG->enableplagiarism)) {
150         return '';
151     }
152     $plagiarismplugins = plagiarism_load_available_plugins();
153     $output = '';
154     foreach ($plagiarismplugins as $plugin => $dir) {
155         require_once($dir.'/lib.php');
156         $plagiarismclass = "plagiarism_plugin_$plugin";
157         $plagiarismplugin = new $plagiarismclass;
158         $output .= $plagiarismplugin->update_status($course, $cm);
159     }
160     return $output;
163 /**
164  * Function that prints the student disclosure notifying that the files will be checked for plagiarism
165  * @param integer $cmid - the cmid of this module
166  * @return string
167  */
168 function plagiarism_print_disclosure($cmid) {
169     global $CFG;
170     if (empty($CFG->enableplagiarism)) {
171         return '';
172     }
173     $plagiarismplugins = plagiarism_load_available_plugins();
174     $output = '';
175     foreach ($plagiarismplugins as $plugin => $dir) {
176         require_once($dir.'/lib.php');
177         $plagiarismclass = "plagiarism_plugin_$plugin";
178         $plagiarismplugin = new $plagiarismclass;
179         $output .= $plagiarismplugin->print_disclosure($cmid);
180     }
181     return $output;
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.
194     if (empty($CFG->enableplagiarism)) {
195         return array();
196     }
197     $plagiarismplugins = core_component::get_plugin_list('plagiarism');
198     $availableplugins = array();
199     foreach ($plagiarismplugins as $plugin => $dir) {
200         // Check this plugin is enabled and a lib file exists.
201         if (get_config('plagiarism', $plugin."_use")) {
202             // Deprecated Since Moodle 3.9.
203             $pluginenabled = true;
204             if (empty($showndeprecatedmessage[$plugin])) {
205                 $text = 'The setting plagiarism:'.$plugin.'_use is deprecated.';
206                 $text .= ' Use plagiarism_' . $plugin . ':enabled instead';
207                 debugging($text, DEBUG_DEVELOPER);
208                 $showndeprecatedmessage[$plugin] = true;
209             }
210         } else {
211             $pluginenabled = get_config('plagiarism_'.$plugin, 'enabled');
212         }
213         if ($pluginenabled && file_exists($dir."/lib.php")) {
214             require_once($dir.'/lib.php');
215             $plagiarismclass = "plagiarism_plugin_$plugin";
216             if (class_exists($plagiarismclass)) {
217                 $availableplugins[$plugin] = $dir;
218             }
219         }
220     }
221     return $availableplugins;