Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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   * This file contains the definition for the library class for onlinetext submission plugin
  19   *
  20   * This class provides all the functionality for the new assign module.
  21   *
  22   * @package assignsubmission_onlinetext
  23   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  use core_external\external_single_structure;
  28  use core_external\external_value;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  // File area for online text submission assignment.
  32  define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
  33  
  34  /**
  35   * library class for onlinetext submission plugin extending submission plugin base class
  36   *
  37   * @package assignsubmission_onlinetext
  38   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class assign_submission_onlinetext extends assign_submission_plugin {
  42  
  43      /**
  44       * Get the name of the online text submission plugin
  45       * @return string
  46       */
  47      public function get_name() {
  48          return get_string('onlinetext', 'assignsubmission_onlinetext');
  49      }
  50  
  51  
  52      /**
  53       * Get onlinetext submission information from the database
  54       *
  55       * @param  int $submissionid
  56       * @return mixed
  57       */
  58      private function get_onlinetext_submission($submissionid) {
  59          global $DB;
  60  
  61          return $DB->get_record('assignsubmission_onlinetext', array('submission'=>$submissionid));
  62      }
  63  
  64      /**
  65       * Remove a submission.
  66       *
  67       * @param stdClass $submission The submission
  68       * @return boolean
  69       */
  70      public function remove(stdClass $submission) {
  71          global $DB;
  72  
  73          $submissionid = $submission ? $submission->id : 0;
  74          if ($submissionid) {
  75              $DB->delete_records('assignsubmission_onlinetext', array('submission' => $submissionid));
  76          }
  77          return true;
  78      }
  79  
  80      /**
  81       * Get the settings for onlinetext submission plugin
  82       *
  83       * @param MoodleQuickForm $mform The form to add elements to
  84       * @return void
  85       */
  86      public function get_settings(MoodleQuickForm $mform) {
  87          global $CFG, $COURSE;
  88  
  89          $defaultwordlimit = $this->get_config('wordlimit') == 0 ? '' : $this->get_config('wordlimit');
  90          $defaultwordlimitenabled = $this->get_config('wordlimitenabled');
  91  
  92          $options = array('size' => '6', 'maxlength' => '6');
  93          $name = get_string('wordlimit', 'assignsubmission_onlinetext');
  94  
  95          // Create a text box that can be enabled/disabled for onlinetext word limit.
  96          $wordlimitgrp = array();
  97          $wordlimitgrp[] = $mform->createElement('text', 'assignsubmission_onlinetext_wordlimit', '', $options);
  98          $wordlimitgrp[] = $mform->createElement('checkbox', 'assignsubmission_onlinetext_wordlimit_enabled',
  99                  '', get_string('enable'));
 100          $mform->addGroup($wordlimitgrp, 'assignsubmission_onlinetext_wordlimit_group', $name, ' ', false);
 101          $mform->addHelpButton('assignsubmission_onlinetext_wordlimit_group',
 102                                'wordlimit',
 103                                'assignsubmission_onlinetext');
 104          $mform->disabledIf('assignsubmission_onlinetext_wordlimit',
 105                             'assignsubmission_onlinetext_wordlimit_enabled',
 106                             'notchecked');
 107          $mform->hideIf('assignsubmission_onlinetext_wordlimit',
 108                         'assignsubmission_onlinetext_enabled',
 109                         'notchecked');
 110  
 111          // Add numeric rule to text field.
 112          $wordlimitgrprules = array();
 113          $wordlimitgrprules['assignsubmission_onlinetext_wordlimit'][] = array(null, 'numeric', null, 'client');
 114          $mform->addGroupRule('assignsubmission_onlinetext_wordlimit_group', $wordlimitgrprules);
 115  
 116          // Rest of group setup.
 117          $mform->setDefault('assignsubmission_onlinetext_wordlimit', $defaultwordlimit);
 118          $mform->setDefault('assignsubmission_onlinetext_wordlimit_enabled', $defaultwordlimitenabled);
 119          $mform->setType('assignsubmission_onlinetext_wordlimit', PARAM_INT);
 120          $mform->hideIf('assignsubmission_onlinetext_wordlimit_group',
 121                         'assignsubmission_onlinetext_enabled',
 122                         'notchecked');
 123      }
 124  
 125      /**
 126       * Save the settings for onlinetext submission plugin
 127       *
 128       * @param stdClass $data
 129       * @return bool
 130       */
 131      public function save_settings(stdClass $data) {
 132          if (empty($data->assignsubmission_onlinetext_wordlimit) || empty($data->assignsubmission_onlinetext_wordlimit_enabled)) {
 133              $wordlimit = 0;
 134              $wordlimitenabled = 0;
 135          } else {
 136              $wordlimit = $data->assignsubmission_onlinetext_wordlimit;
 137              $wordlimitenabled = 1;
 138          }
 139  
 140          $this->set_config('wordlimit', $wordlimit);
 141          $this->set_config('wordlimitenabled', $wordlimitenabled);
 142  
 143          return true;
 144      }
 145  
 146      /**
 147       * Add form elements for settings
 148       *
 149       * @param mixed $submission can be null
 150       * @param MoodleQuickForm $mform
 151       * @param stdClass $data
 152       * @return true if elements were added to the form
 153       */
 154      public function get_form_elements($submission, MoodleQuickForm $mform, stdClass $data) {
 155          $elements = array();
 156  
 157          $editoroptions = $this->get_edit_options();
 158          $submissionid = $submission ? $submission->id : 0;
 159  
 160          if (!isset($data->onlinetext)) {
 161              $data->onlinetext = '';
 162          }
 163          if (!isset($data->onlinetextformat)) {
 164              $data->onlinetextformat = editors_get_preferred_format();
 165          }
 166  
 167          if ($submission) {
 168              $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 169              if ($onlinetextsubmission) {
 170                  $data->onlinetext = $onlinetextsubmission->onlinetext;
 171                  $data->onlinetextformat = $onlinetextsubmission->onlineformat;
 172              }
 173  
 174          }
 175  
 176          $data = file_prepare_standard_editor($data,
 177                                               'onlinetext',
 178                                               $editoroptions,
 179                                               $this->assignment->get_context(),
 180                                               'assignsubmission_onlinetext',
 181                                               ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 182                                               $submissionid);
 183          $mform->addElement('editor', 'onlinetext_editor', $this->get_name(), null, $editoroptions);
 184  
 185          return true;
 186      }
 187  
 188      /**
 189       * Editor format options
 190       *
 191       * @return array
 192       */
 193      private function get_edit_options() {
 194          $editoroptions = array(
 195              'noclean' => false,
 196              'maxfiles' => EDITOR_UNLIMITED_FILES,
 197              'maxbytes' => $this->assignment->get_course()->maxbytes,
 198              'context' => $this->assignment->get_context(),
 199              'return_types' => (FILE_INTERNAL | FILE_EXTERNAL | FILE_CONTROLLED_LINK),
 200              'removeorphaneddrafts' => true // Whether or not to remove any draft files which aren't referenced in the text.
 201          );
 202          return $editoroptions;
 203      }
 204  
 205      /**
 206       * Save data to the database and trigger plagiarism plugin,
 207       * if enabled, to scan the uploaded content via events trigger
 208       *
 209       * @param stdClass $submission
 210       * @param stdClass $data
 211       * @return bool
 212       */
 213      public function save(stdClass $submission, stdClass $data) {
 214          global $USER, $DB;
 215  
 216          $editoroptions = $this->get_edit_options();
 217  
 218          $data = file_postupdate_standard_editor($data,
 219                                                  'onlinetext',
 220                                                  $editoroptions,
 221                                                  $this->assignment->get_context(),
 222                                                  'assignsubmission_onlinetext',
 223                                                  ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 224                                                  $submission->id);
 225  
 226          $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 227  
 228          $fs = get_file_storage();
 229  
 230          $files = $fs->get_area_files($this->assignment->get_context()->id,
 231                                       'assignsubmission_onlinetext',
 232                                       ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 233                                       $submission->id,
 234                                       'id',
 235                                       false);
 236  
 237          // Check word count before submitting anything.
 238          $exceeded = $this->check_word_count(trim($data->onlinetext));
 239          if ($exceeded) {
 240              $this->set_error($exceeded);
 241              return false;
 242          }
 243  
 244          $params = array(
 245              'context' => context_module::instance($this->assignment->get_course_module()->id),
 246              'courseid' => $this->assignment->get_course()->id,
 247              'objectid' => $submission->id,
 248              'other' => array(
 249                  'pathnamehashes' => array_keys($files),
 250                  'content' => trim($data->onlinetext),
 251                  'format' => $data->onlinetext_editor['format']
 252              )
 253          );
 254          if (!empty($submission->userid) && ($submission->userid != $USER->id)) {
 255              $params['relateduserid'] = $submission->userid;
 256          }
 257          if ($this->assignment->is_blind_marking()) {
 258              $params['anonymous'] = 1;
 259          }
 260          $event = \assignsubmission_onlinetext\event\assessable_uploaded::create($params);
 261          $event->trigger();
 262  
 263          $groupname = null;
 264          $groupid = 0;
 265          // Get the group name as other fields are not transcribed in the logs and this information is important.
 266          if (empty($submission->userid) && !empty($submission->groupid)) {
 267              $groupname = $DB->get_field('groups', 'name', array('id' => $submission->groupid), MUST_EXIST);
 268              $groupid = $submission->groupid;
 269          } else {
 270              $params['relateduserid'] = $submission->userid;
 271          }
 272  
 273          $count = count_words($data->onlinetext);
 274  
 275          // Unset the objectid and other field from params for use in submission events.
 276          unset($params['objectid']);
 277          unset($params['other']);
 278          $params['other'] = array(
 279              'submissionid' => $submission->id,
 280              'submissionattempt' => $submission->attemptnumber,
 281              'submissionstatus' => $submission->status,
 282              'onlinetextwordcount' => $count,
 283              'groupid' => $groupid,
 284              'groupname' => $groupname
 285          );
 286  
 287          if ($onlinetextsubmission) {
 288  
 289              $onlinetextsubmission->onlinetext = $data->onlinetext;
 290              $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
 291              $params['objectid'] = $onlinetextsubmission->id;
 292              $updatestatus = $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
 293              $event = \assignsubmission_onlinetext\event\submission_updated::create($params);
 294              $event->set_assign($this->assignment);
 295              $event->trigger();
 296              return $updatestatus;
 297          } else {
 298  
 299              $onlinetextsubmission = new stdClass();
 300              $onlinetextsubmission->onlinetext = $data->onlinetext;
 301              $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
 302  
 303              $onlinetextsubmission->submission = $submission->id;
 304              $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
 305              $onlinetextsubmission->id = $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
 306              $params['objectid'] = $onlinetextsubmission->id;
 307              $event = \assignsubmission_onlinetext\event\submission_created::create($params);
 308              $event->set_assign($this->assignment);
 309              $event->trigger();
 310              return $onlinetextsubmission->id > 0;
 311          }
 312      }
 313  
 314      /**
 315       * Return a list of the text fields that can be imported/exported by this plugin
 316       *
 317       * @return array An array of field names and descriptions. (name=>description, ...)
 318       */
 319      public function get_editor_fields() {
 320          return array('onlinetext' => get_string('pluginname', 'assignsubmission_onlinetext'));
 321      }
 322  
 323      /**
 324       * Get the saved text content from the editor
 325       *
 326       * @param string $name
 327       * @param int $submissionid
 328       * @return string
 329       */
 330      public function get_editor_text($name, $submissionid) {
 331          if ($name == 'onlinetext') {
 332              $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
 333              if ($onlinetextsubmission) {
 334                  return $onlinetextsubmission->onlinetext;
 335              }
 336          }
 337  
 338          return '';
 339      }
 340  
 341      /**
 342       * Get the content format for the editor
 343       *
 344       * @param string $name
 345       * @param int $submissionid
 346       * @return int
 347       */
 348      public function get_editor_format($name, $submissionid) {
 349          if ($name == 'onlinetext') {
 350              $onlinetextsubmission = $this->get_onlinetext_submission($submissionid);
 351              if ($onlinetextsubmission) {
 352                  return $onlinetextsubmission->onlineformat;
 353              }
 354          }
 355  
 356          return 0;
 357      }
 358  
 359  
 360       /**
 361        * Display onlinetext word count in the submission status table
 362        *
 363        * @param stdClass $submission
 364        * @param bool $showviewlink - If the summary has been truncated set this to true
 365        * @return string
 366        */
 367      public function view_summary(stdClass $submission, & $showviewlink) {
 368          global $CFG;
 369  
 370          $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 371          // Always show the view link.
 372          $showviewlink = true;
 373  
 374          if ($onlinetextsubmission) {
 375              // This contains the shortened version of the text plus an optional 'Export to portfolio' button.
 376              $text = $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 377                                                               $onlinetextsubmission->submission,
 378                                                               $this->get_type(),
 379                                                               'onlinetext',
 380                                                               'assignsubmission_onlinetext', true);
 381  
 382              // The actual submission text.
 383              $onlinetext = trim($onlinetextsubmission->onlinetext);
 384              // The shortened version of the submission text.
 385              $shorttext = shorten_text($onlinetext, 140);
 386  
 387              $plagiarismlinks = '';
 388  
 389              if (!empty($CFG->enableplagiarism)) {
 390                  require_once($CFG->libdir . '/plagiarismlib.php');
 391  
 392                  $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
 393                      'content' => $onlinetext,
 394                      'cmid' => $this->assignment->get_course_module()->id,
 395                      'course' => $this->assignment->get_course()->id,
 396                      'assignment' => $submission->assignment));
 397              }
 398              // We compare the actual text submission and the shortened version. If they are not equal, we show the word count.
 399              if ($onlinetext != $shorttext) {
 400                  $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($onlinetext));
 401  
 402                  return $plagiarismlinks . $wordcount . $text;
 403              } else {
 404                  return $plagiarismlinks . $text;
 405              }
 406          }
 407          return '';
 408      }
 409  
 410      /**
 411       * Produce a list of files suitable for export that represent this submission.
 412       *
 413       * @param stdClass $submission - For this is the submission data
 414       * @param stdClass $user - This is the user record for this submission
 415       * @return array - return an array of files indexed by filename
 416       */
 417      public function get_files(stdClass $submission, stdClass $user) {
 418          global $DB;
 419  
 420          $files = array();
 421          $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 422  
 423          // Note that this check is the same logic as the result from the is_empty function but we do
 424          // not call it directly because we already have the submission record.
 425          if ($onlinetextsubmission) {
 426              // Do not pass the text through format_text. The result may not be displayed in Moodle and
 427              // may be passed to external services such as document conversion or portfolios.
 428              $formattedtext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
 429              $head = '<head><meta charset="UTF-8"></head>';
 430              $submissioncontent = '<!DOCTYPE html><html>' . $head . '<body>'. $formattedtext . '</body></html>';
 431  
 432              $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
 433              $files[$filename] = array($submissioncontent);
 434  
 435              $fs = get_file_storage();
 436  
 437              $fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
 438                                             'assignsubmission_onlinetext',
 439                                             ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 440                                             $submission->id,
 441                                             'timemodified',
 442                                             false);
 443  
 444              foreach ($fsfiles as $file) {
 445                  $files[$file->get_filename()] = $file;
 446              }
 447          }
 448  
 449          return $files;
 450      }
 451  
 452      /**
 453       * Display the saved text content from the editor in the view table
 454       *
 455       * @param stdClass $submission
 456       * @return string
 457       */
 458      public function view(stdClass $submission) {
 459          global $CFG;
 460          $result = '';
 461          $plagiarismlinks = '';
 462  
 463          $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 464  
 465          if ($onlinetextsubmission) {
 466  
 467              // Render for portfolio API.
 468              $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 469                                                                  $onlinetextsubmission->submission,
 470                                                                  $this->get_type(),
 471                                                                  'onlinetext',
 472                                                                  'assignsubmission_onlinetext');
 473  
 474              if (!empty($CFG->enableplagiarism)) {
 475                  require_once($CFG->libdir . '/plagiarismlib.php');
 476  
 477                  $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
 478                      'content' => trim($onlinetextsubmission->onlinetext),
 479                      'cmid' => $this->assignment->get_course_module()->id,
 480                      'course' => $this->assignment->get_course()->id,
 481                      'assignment' => $submission->assignment));
 482              }
 483          }
 484  
 485          return $plagiarismlinks . $result;
 486      }
 487  
 488      /**
 489       * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
 490       *
 491       * @param string $type old assignment subtype
 492       * @param int $version old assignment version
 493       * @return bool True if upgrade is possible
 494       */
 495      public function can_upgrade($type, $version) {
 496          if ($type == 'online' && $version >= 2011112900) {
 497              return true;
 498          }
 499          return false;
 500      }
 501  
 502  
 503      /**
 504       * Upgrade the settings from the old assignment to the new plugin based one
 505       *
 506       * @param context $oldcontext - the database for the old assignment context
 507       * @param stdClass $oldassignment - the database for the old assignment instance
 508       * @param string $log record log events here
 509       * @return bool Was it a success?
 510       */
 511      public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
 512          // No settings to upgrade.
 513          return true;
 514      }
 515  
 516      /**
 517       * Upgrade the submission from the old assignment to the new one
 518       *
 519       * @param context $oldcontext - the database for the old assignment context
 520       * @param stdClass $oldassignment The data record for the old assignment
 521       * @param stdClass $oldsubmission The data record for the old submission
 522       * @param stdClass $submission The data record for the new submission
 523       * @param string $log Record upgrade messages in the log
 524       * @return bool true or false - false will trigger a rollback
 525       */
 526      public function upgrade(context $oldcontext,
 527                              stdClass $oldassignment,
 528                              stdClass $oldsubmission,
 529                              stdClass $submission,
 530                              & $log) {
 531          global $DB;
 532  
 533          $onlinetextsubmission = new stdClass();
 534          $onlinetextsubmission->onlinetext = $oldsubmission->data1;
 535          $onlinetextsubmission->onlineformat = $oldsubmission->data2;
 536  
 537          $onlinetextsubmission->submission = $submission->id;
 538          $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
 539  
 540          if ($onlinetextsubmission->onlinetext === null) {
 541              $onlinetextsubmission->onlinetext = '';
 542          }
 543  
 544          if ($onlinetextsubmission->onlineformat === null) {
 545              $onlinetextsubmission->onlineformat = editors_get_preferred_format();
 546          }
 547  
 548          if (!$DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0) {
 549              $log .= get_string('couldnotconvertsubmission', 'mod_assign', $submission->userid);
 550              return false;
 551          }
 552  
 553          // Now copy the area files.
 554          $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
 555                                                          'mod_assignment',
 556                                                          'submission',
 557                                                          $oldsubmission->id,
 558                                                          $this->assignment->get_context()->id,
 559                                                          'assignsubmission_onlinetext',
 560                                                          ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
 561                                                          $submission->id);
 562          return true;
 563      }
 564  
 565      /**
 566       * The assignment has been deleted - cleanup
 567       *
 568       * @return bool
 569       */
 570      public function delete_instance() {
 571          global $DB;
 572          $DB->delete_records('assignsubmission_onlinetext',
 573                              array('assignment'=>$this->assignment->get_instance()->id));
 574  
 575          return true;
 576      }
 577  
 578      /**
 579       * No text is set for this plugin
 580       *
 581       * @param stdClass $submission
 582       * @return bool
 583       */
 584      public function is_empty(stdClass $submission) {
 585          $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 586          $wordcount = 0;
 587          $hasinsertedresources = false;
 588  
 589          if (isset($onlinetextsubmission->onlinetext)) {
 590              $wordcount = count_words(trim($onlinetextsubmission->onlinetext));
 591              // Check if the online text submission contains video, audio or image elements
 592              // that can be ignored and stripped by count_words().
 593              $hasinsertedresources = preg_match('/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>(.*?))/',
 594                      trim($onlinetextsubmission->onlinetext));
 595          }
 596  
 597          return $wordcount == 0 && !$hasinsertedresources;
 598      }
 599  
 600      /**
 601       * Determine if a submission is empty
 602       *
 603       * This is distinct from is_empty in that it is intended to be used to
 604       * determine if a submission made before saving is empty.
 605       *
 606       * @param stdClass $data The submission data
 607       * @return bool
 608       */
 609      public function submission_is_empty(stdClass $data) {
 610          if (!isset($data->onlinetext_editor)) {
 611              return true;
 612          }
 613          $wordcount = 0;
 614          $hasinsertedresources = false;
 615  
 616          if (isset($data->onlinetext_editor['text'])) {
 617              $wordcount = count_words(trim((string)$data->onlinetext_editor['text']));
 618              // Check if the online text submission contains video, audio or image elements
 619              // that can be ignored and stripped by count_words().
 620              $hasinsertedresources = preg_match('/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>(.*?))/',
 621                      trim((string)$data->onlinetext_editor['text']));
 622          }
 623  
 624          return $wordcount == 0 && !$hasinsertedresources;
 625      }
 626  
 627      /**
 628       * Get file areas returns a list of areas this plugin stores files
 629       * @return array - An array of fileareas (keys) and descriptions (values)
 630       */
 631      public function get_file_areas() {
 632          return array(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA=>$this->get_name());
 633      }
 634  
 635      /**
 636       * Copy the student's submission from a previous submission. Used when a student opts to base their resubmission
 637       * on the last submission.
 638       * @param stdClass $sourcesubmission
 639       * @param stdClass $destsubmission
 640       */
 641      public function copy_submission(stdClass $sourcesubmission, stdClass $destsubmission) {
 642          global $DB;
 643  
 644          // Copy the files across (attached via the text editor).
 645          $contextid = $this->assignment->get_context()->id;
 646          $fs = get_file_storage();
 647          $files = $fs->get_area_files($contextid, 'assignsubmission_onlinetext',
 648                                       ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $sourcesubmission->id, 'id', false);
 649          foreach ($files as $file) {
 650              $fieldupdates = array('itemid' => $destsubmission->id);
 651              $fs->create_file_from_storedfile($fieldupdates, $file);
 652          }
 653  
 654          // Copy the assignsubmission_onlinetext record.
 655          $onlinetextsubmission = $this->get_onlinetext_submission($sourcesubmission->id);
 656          if ($onlinetextsubmission) {
 657              unset($onlinetextsubmission->id);
 658              $onlinetextsubmission->submission = $destsubmission->id;
 659              $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
 660          }
 661          return true;
 662      }
 663  
 664      /**
 665       * Return a description of external params suitable for uploading an onlinetext submission from a webservice.
 666       *
 667       * @return \core_external\external_description|null
 668       */
 669      public function get_external_parameters() {
 670          $editorparams = array('text' => new external_value(PARAM_RAW, 'The text for this submission.'),
 671                                'format' => new external_value(PARAM_INT, 'The format for this submission'),
 672                                'itemid' => new external_value(PARAM_INT, 'The draft area id for files attached to the submission'));
 673          $editorstructure = new external_single_structure($editorparams, 'Editor structure', VALUE_OPTIONAL);
 674          return array('onlinetext_editor' => $editorstructure);
 675      }
 676  
 677      /**
 678       * Compare word count of onlinetext submission to word limit, and return result.
 679       *
 680       * @param string $submissiontext Onlinetext submission text from editor
 681       * @return string Error message if limit is enabled and exceeded, otherwise null
 682       */
 683      public function check_word_count($submissiontext) {
 684          global $OUTPUT;
 685  
 686          $wordlimitenabled = $this->get_config('wordlimitenabled');
 687          $wordlimit = $this->get_config('wordlimit');
 688  
 689          if ($wordlimitenabled == 0) {
 690              return null;
 691          }
 692  
 693          // Count words and compare to limit.
 694          $wordcount = count_words($submissiontext);
 695          if ($wordcount <= $wordlimit) {
 696              return null;
 697          } else {
 698              $errormsg = get_string('wordlimitexceeded', 'assignsubmission_onlinetext',
 699                      array('limit' => $wordlimit, 'count' => $wordcount));
 700              return $OUTPUT->error_text($errormsg);
 701          }
 702      }
 703  
 704      /**
 705       * Return the plugin configs for external functions.
 706       *
 707       * @return array the list of settings
 708       * @since Moodle 3.2
 709       */
 710      public function get_config_for_external() {
 711          return (array) $this->get_config();
 712      }
 713  }