Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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