MDL-49684 timezones: rewrite timezone support
[moodle.git] / lib / plagiarismlib.php
blob1c28f17b301f49271097ac5fdd05fd59970dd1ab
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * plagiarismlib.php - Contains core Plagiarism related functions.
21 * @since Moodle 2.0
22 * @package moodlecore
23 * @subpackage plagiarism
24 * @copyright 2010 Dan Marsden http://danmarsden.com
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 ///// GENERIC PLAGIARISM FUNCTIONS ////////////////////////////////////////////////////
30 if (!defined('MOODLE_INTERNAL')) {
31 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
34 /**
35 * displays the similarity score and provides a link to the full report if allowed.
37 * @param object $linkarray contains all relevant information for the plugin to generate a link
38 * @return string - url to allow login/viewing of a similarity report
40 function plagiarism_get_links($linkarray) {
41 global $CFG;
42 if (empty($CFG->enableplagiarism)) {
43 return '';
45 $plagiarismplugins = plagiarism_load_available_plugins();
46 $output = '';
47 foreach($plagiarismplugins as $plugin => $dir) {
48 require_once($dir.'/lib.php');
49 $plagiarismclass = "plagiarism_plugin_$plugin";
50 $plagiarismplugin = new $plagiarismclass;
51 $output .= $plagiarismplugin->get_links($linkarray);
53 return $output;
56 /**
57 * returns array of plagiarism details about specified file
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'
65 function plagiarism_get_file_results($cmid, $userid, $file) {
66 global $CFG;
67 $allresults = array();
68 if (empty($CFG->enableplagiarism)) {
69 return $allresults;
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);
78 return $allresults;
81 /**
82 * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
84 * @param object $data - form data
86 function plagiarism_save_form_elements($data) {
87 global $CFG;
88 if (empty($CFG->enableplagiarism)) {
89 return '';
91 $plagiarismplugins = plagiarism_load_available_plugins();
92 foreach($plagiarismplugins as $plugin => $dir) {
93 require_once($dir.'/lib.php');
94 $plagiarismclass = "plagiarism_plugin_$plugin";
95 $plagiarismplugin = new $plagiarismclass;
96 $plagiarismplugin->save_form_elements($data);
101 * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
103 * @param object $mform - Moodle form object
104 * @param object $context - context object
105 * @param string $modulename - Name of the module
107 function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
108 global $CFG;
109 if (empty($CFG->enableplagiarism)) {
110 return '';
112 $plagiarismplugins = plagiarism_load_available_plugins();
113 foreach($plagiarismplugins as $plugin => $dir) {
114 require_once($dir.'/lib.php');
115 $plagiarismclass = "plagiarism_plugin_$plugin";
116 $plagiarismplugin = new $plagiarismclass;
117 $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
121 * updates the status of all files within a module
123 * @param object $course - full Course object
124 * @param object $cm - full cm object
125 * @return string
127 function plagiarism_update_status($course, $cm) {
128 global $CFG;
129 if (empty($CFG->enableplagiarism)) {
130 return '';
132 $plagiarismplugins = plagiarism_load_available_plugins();
133 $output = '';
134 foreach($plagiarismplugins as $plugin => $dir) {
135 require_once($dir.'/lib.php');
136 $plagiarismclass = "plagiarism_plugin_$plugin";
137 $plagiarismplugin = new $plagiarismclass;
138 $output .= $plagiarismplugin->update_status($course, $cm);
140 return $output;
144 * Function that prints the student disclosure notifying that the files will be checked for plagiarism
145 * @param integer $cmid - the cmid of this module
146 * @return string
148 function plagiarism_print_disclosure($cmid) {
149 global $CFG;
150 if (empty($CFG->enableplagiarism)) {
151 return '';
153 $plagiarismplugins = plagiarism_load_available_plugins();
154 $output = '';
155 foreach($plagiarismplugins as $plugin => $dir) {
156 require_once($dir.'/lib.php');
157 $plagiarismclass = "plagiarism_plugin_$plugin";
158 $plagiarismplugin = new $plagiarismclass;
159 $output .= $plagiarismplugin->print_disclosure($cmid);
161 return $output;
164 * used by admin/cron.php to get similarity scores from submitted files.
167 function plagiarism_cron() {
168 global $CFG;
169 if (empty($CFG->enableplagiarism)) {
170 return '';
172 $plagiarismplugins = plagiarism_load_available_plugins();
173 foreach($plagiarismplugins as $plugin => $dir) {
174 mtrace('Processing cron function for plagiarism_plugin_' . $plugin . '...', '');
175 cron_trace_time_and_memory();
176 require_once($dir.'/lib.php');
177 $plagiarismclass = "plagiarism_plugin_$plugin";
178 $plagiarismplugin = new $plagiarismclass;
179 $plagiarismplugin->cron();
183 * helper function - also loads lib file of plagiarism plugin
184 * @return array of available plugins
186 function plagiarism_load_available_plugins() {
187 global $CFG;
188 if (empty($CFG->enableplagiarism)) {
189 return array();
191 $plagiarismplugins = core_component::get_plugin_list('plagiarism');
192 $availableplugins = array();
193 foreach($plagiarismplugins as $plugin => $dir) {
194 //check this plugin is enabled and a lib file exists.
195 if (get_config('plagiarism', $plugin."_use") && file_exists($dir."/lib.php")) {
196 require_once($dir.'/lib.php');
197 $plagiarismclass = "plagiarism_plugin_$plugin";
198 if (class_exists($plagiarismclass)) {
199 $availableplugins[$plugin] = $dir;
203 return $availableplugins;