Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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  /**
  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   */
  26  
  27  if (!defined('MOODLE_INTERNAL')) {
  28      die('Direct access to this script is forbidden.');
  29  }
  30  
  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  }
  55  
  56  /**
  57   * returns array of plagiarism details about specified file
  58   *
  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'
  66   */
  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;
  74      }
  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);
  81      }
  82      return $allresults;
  83  }
  84  
  85  /**
  86   * saves/updates plagiarism settings from a modules config page - called by course/modedit.php
  87   *
  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
  91   */
  92  function plagiarism_save_form_elements($data) {
  93      global $CFG;
  94      if (empty($CFG->enableplagiarism)) {
  95          return '';
  96      }
  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;
 102  
 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);
 108          }
 109  
 110          $plagiarismplugin->save_form_elements($data);
 111      }
 112  }
 113  
 114  /**
 115   * adds the list of plagiarism settings to a form - called inside modules that have enabled plagiarism
 116   *
 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
 122   */
 123  function plagiarism_get_form_elements_module($mform, $context, $modulename = "") {
 124      global $CFG;
 125      if (empty($CFG->enableplagiarism)) {
 126          return '';
 127      }
 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;
 133  
 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);
 139          }
 140  
 141          $plagiarismplugin->get_form_elements_module($mform, $context, $modulename);
 142      }
 143  }
 144  /**
 145   * Allows a plagiarism plugin to print a button/link at the top of activity overview report pages.
 146   *
 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
 152   */
 153  function plagiarism_update_status($course, $cm) {
 154      global $CFG;
 155      if (empty($CFG->enableplagiarism)) {
 156          return '';
 157      }
 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;
 164  
 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);
 170          }
 171          $output .= $plagiarismplugin->update_status($course, $cm);
 172      }
 173      return $output;
 174  }
 175  
 176  /**
 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
 180   */
 181  function plagiarism_print_disclosure($cmid) {
 182      global $CFG;
 183      if (empty($CFG->enableplagiarism)) {
 184          return '';
 185      }
 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);
 193      }
 194      return $output;
 195  }
 196  
 197  /**
 198   * Helper function - also loads lib file of plagiarism plugin
 199   *
 200   * @todo MDL-67872 the deprecated code in this function to be removed in Moodle 4.1
 201   * @return array of available plugins
 202   */
 203  function plagiarism_load_available_plugins() {
 204      global $CFG;
 205      static $showndeprecatedmessage = array(); // Only show message once per page load.
 206  
 207      if (empty($CFG->enableplagiarism)) {
 208          return array();
 209      }
 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;
 222              }
 223          } else {
 224              $pluginenabled = get_config('plagiarism_'.$plugin, 'enabled');
 225          }
 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;
 231              }
 232          }
 233      }
 234      return $availableplugins;
 235  }