NOBUG: 1 more whitespace, was hurting my eyes all the time.
[moodle.git] / lib / plagiarismlib.php
CommitLineData
bce59524
DM
1<?php
2
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.
14//
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/>.
17
18/**
19 * plagiarismlib.php - Contains core Plagiarism related functions.
20 *
21 * @since 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
26 */
27
28///// GENERIC PLAGIARISM FUNCTIONS ////////////////////////////////////////////////////
29
30if (!defined('MOODLE_INTERNAL')) {
31 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32}
33
34/**
35 * displays the similarity score and provides a link to the full report if allowed.
36 *
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
39 */
40function plagiarism_get_links($linkarray) {
41 global $CFG;
42 if (empty($CFG->enableplagiarism)) {
43 return '';
44 }
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);
52 }
53 return $output;
54}
55
bfad2f2f
PB
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 */
65function 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}
80
bce59524
DM
81/**
82 * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
83 *
84 * @param object $data - form data
85 */
86function plagiarism_save_form_elements($data) {
87 global $CFG;
88 if (empty($CFG->enableplagiarism)) {
89 return '';
90 }
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);
97 }
98}
99
100/**
101 * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
102 *
103 * @param object $mform - Moodle form object
104 * @param object $context - context object
67fbfe8b 105 * @param string $modulename - Name of the module
bce59524 106 */
67fbfe8b 107function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
bce59524
DM
108 global $CFG;
109 if (empty($CFG->enableplagiarism)) {
110 return '';
111 }
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;
67fbfe8b 117 $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
bce59524
DM
118 }
119}
120/**
121 * updates the status of all files within a module
122 *
123 * @param object $course - full Course object
124 * @param object $cm - full cm object
266540eb 125 * @return string
bce59524
DM
126 */
127function plagiarism_update_status($course, $cm) {
128 global $CFG;
129 if (empty($CFG->enableplagiarism)) {
130 return '';
131 }
132 $plagiarismplugins = plagiarism_load_available_plugins();
266540eb 133 $output = '';
bce59524
DM
134 foreach($plagiarismplugins as $plugin => $dir) {
135 require_once($dir.'/lib.php');
136 $plagiarismclass = "plagiarism_plugin_$plugin";
137 $plagiarismplugin = new $plagiarismclass;
266540eb 138 $output .= $plagiarismplugin->update_status($course, $cm);
bce59524 139 }
266540eb 140 return $output;
bce59524
DM
141}
142
143/**
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
266540eb 146* @return string
bce59524
DM
147*/
148function plagiarism_print_disclosure($cmid) {
149 global $CFG;
150 if (empty($CFG->enableplagiarism)) {
151 return '';
152 }
153 $plagiarismplugins = plagiarism_load_available_plugins();
266540eb 154 $output = '';
bce59524
DM
155 foreach($plagiarismplugins as $plugin => $dir) {
156 require_once($dir.'/lib.php');
157 $plagiarismclass = "plagiarism_plugin_$plugin";
158 $plagiarismplugin = new $plagiarismclass;
266540eb 159 $output .= $plagiarismplugin->print_disclosure($cmid);
bce59524 160 }
266540eb 161 return $output;
bce59524
DM
162}
163/**
164 * used by admin/cron.php to get similarity scores from submitted files.
165 *
166 */
167function plagiarism_cron() {
168 global $CFG;
169 if (empty($CFG->enableplagiarism)) {
170 return '';
171 }
172 $plagiarismplugins = plagiarism_load_available_plugins();
173 foreach($plagiarismplugins as $plugin => $dir) {
174 require_once($dir.'/lib.php');
175 $plagiarismclass = "plagiarism_plugin_$plugin";
176 $plagiarismplugin = new $plagiarismclass;
177 $plagiarismplugin->cron();
178 }
179}
fcdc9b77 180/**
bce59524
DM
181 * helper function - also loads lib file of plagiarism plugin
182 * @return array of available plugins
183 */
184function plagiarism_load_available_plugins() {
185 global $CFG;
186 if (empty($CFG->enableplagiarism)) {
187 return array();
188 }
189 $plagiarismplugins = get_plugin_list('plagiarism');
190 $availableplugins = array();
191 foreach($plagiarismplugins as $plugin => $dir) {
192 //check this plugin is enabled and a lib file exists.
193 if (get_config('plagiarism', $plugin."_use") && file_exists($dir."/lib.php")) {
194 require_once($dir.'/lib.php');
195 $plagiarismclass = "plagiarism_plugin_$plugin";
196 if (class_exists($plagiarismclass)) {
197 $availableplugins[$plugin] = $dir;
198 }
199 }
200 }
201 return $availableplugins;
202}