Merge branch 'MDL-37030-master' of git://github.com/damyon/moodle
[moodle.git] / mod / assign / submission / onlinetext / locallib.php
index 4162157..edbe85a 100644 (file)
@@ -25,9 +25,7 @@
  */
 
 defined('MOODLE_INTERNAL') || die();
  */
 
 defined('MOODLE_INTERNAL') || die();
-/**
- * File area for online text submission assignment
- */
+// File area for online text submission assignment.
 define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
 
 /**
 define('ASSIGNSUBMISSION_ONLINETEXT_FILEAREA', 'submissions_onlinetext');
 
 /**
@@ -48,12 +46,12 @@ class assign_submission_onlinetext extends assign_submission_plugin {
     }
 
 
     }
 
 
-   /**
-    * Get onlinetext submission information from the database
-    *
-    * @param  int $submissionid
-    * @return mixed
-    */
+    /**
+     * Get onlinetext submission information from the database
+     *
+     * @param  int $submissionid
+     * @return mixed
+     */
     private function get_onlinetext_submission($submissionid) {
         global $DB;
 
     private function get_onlinetext_submission($submissionid) {
         global $DB;
 
@@ -90,8 +88,13 @@ class assign_submission_onlinetext extends assign_submission_plugin {
 
         }
 
 
         }
 
-
-        $data = file_prepare_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submissionid);
+        $data = file_prepare_standard_editor($data,
+                                             'onlinetext',
+                                             $editoroptions,
+                                             $this->assignment->get_context(),
+                                             'assignsubmission_onlinetext',
+                                             ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
+                                             $submissionid);
         $mform->addElement('editor', 'onlinetext_editor', '', null, $editoroptions);
         return true;
     }
         $mform->addElement('editor', 'onlinetext_editor', '', null, $editoroptions);
         return true;
     }
@@ -112,32 +115,51 @@ class assign_submission_onlinetext extends assign_submission_plugin {
         return $editoroptions;
     }
 
         return $editoroptions;
     }
 
-     /**
-      * Save data to the database and trigger plagiarism plugin, if enabled, to scan the uploaded content via events trigger
-      *
-      * @param stdClass $submission
-      * @param stdClass $data
-      * @return bool
-      */
-     public function save(stdClass $submission, stdClass $data) {
+    /**
+     * Save data to the database and trigger plagiarism plugin,
+     * if enabled, to scan the uploaded content via events trigger
+     *
+     * @param stdClass $submission
+     * @param stdClass $data
+     * @return bool
+     */
+    public function save(stdClass $submission, stdClass $data) {
         global $USER, $DB;
 
         $editoroptions = $this->get_edit_options();
 
         global $USER, $DB;
 
         $editoroptions = $this->get_edit_options();
 
-        $data = file_postupdate_standard_editor($data, 'onlinetext', $editoroptions, $this->assignment->get_context(), 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id);
+        $data = file_postupdate_standard_editor($data,
+                                                'onlinetext',
+                                                $editoroptions,
+                                                $this->assignment->get_context(),
+                                                'assignsubmission_onlinetext',
+                                                ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
+                                                $submission->id);
 
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 
 
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 
+        $text = format_text($data->onlinetext,
+                            $data->onlinetext_editor['format'],
+                            array('context'=>$this->assignment->get_context()));
+
         $fs = get_file_storage();
         $fs = get_file_storage();
-        $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "id", false);
-        // Let Moodle know that an assessable content was uploaded (eg for plagiarism detection)
+
+        $files = $fs->get_area_files($this->assignment->get_context()->id,
+                                     'assignsubmission_onlinetext',
+                                     ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
+                                     $submission->id,
+                                     'id',
+                                     false);
+
+        // Let Moodle know that an assessable content was uploaded (eg for plagiarism detection).
         $eventdata = new stdClass();
         $eventdata->modulename = 'assign';
         $eventdata->cmid = $this->assignment->get_course_module()->id;
         $eventdata->itemid = $submission->id;
         $eventdata->courseid = $this->assignment->get_course()->id;
         $eventdata->userid = $USER->id;
         $eventdata = new stdClass();
         $eventdata->modulename = 'assign';
         $eventdata->cmid = $this->assignment->get_course_module()->id;
         $eventdata->itemid = $submission->id;
         $eventdata->courseid = $this->assignment->get_course()->id;
         $eventdata->userid = $USER->id;
-        $eventdata->content = trim(format_text($data->onlinetext, $data->onlinetext_editor['format'], array('context'=>$this->assignment->get_context())));
+        $eventdata->content = trim($text);
+
         if ($files) {
             $eventdata->pathnamehashes = array_keys($files);
         }
         if ($files) {
             $eventdata->pathnamehashes = array_keys($files);
         }
@@ -148,7 +170,6 @@ class assign_submission_onlinetext extends assign_submission_plugin {
             $onlinetextsubmission->onlinetext = $data->onlinetext;
             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
 
             $onlinetextsubmission->onlinetext = $data->onlinetext;
             $onlinetextsubmission->onlineformat = $data->onlinetext_editor['format'];
 
-
             return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
         } else {
 
             return $DB->update_record('assignsubmission_onlinetext', $onlinetextsubmission);
         } else {
 
@@ -160,8 +181,6 @@ class assign_submission_onlinetext extends assign_submission_plugin {
             $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
             return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
         }
             $onlinetextsubmission->assignment = $this->assignment->get_instance()->id;
             return $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission) > 0;
         }
-
-
     }
 
     /**
     }
 
     /**
@@ -206,8 +225,7 @@ class assign_submission_onlinetext extends assign_submission_plugin {
             }
         }
 
             }
         }
 
-
-         return 0;
+        return 0;
     }
 
 
     }
 
 
@@ -222,7 +240,7 @@ class assign_submission_onlinetext extends assign_submission_plugin {
         global $CFG;
 
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
         global $CFG;
 
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
-        // always show the view link
+        // Always show the view link.
         $showviewlink = true;
 
         if ($onlinetextsubmission) {
         $showviewlink = true;
 
         if ($onlinetextsubmission) {
@@ -234,16 +252,20 @@ class assign_submission_onlinetext extends assign_submission_plugin {
 
             $shorttext = shorten_text($text, 140);
             $plagiarismlinks = '';
 
             $shorttext = shorten_text($text, 140);
             $plagiarismlinks = '';
+
             if (!empty($CFG->enableplagiarism)) {
                 require_once($CFG->libdir . '/plagiarismlib.php');
             if (!empty($CFG->enableplagiarism)) {
                 require_once($CFG->libdir . '/plagiarismlib.php');
+
                 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
                 $plagiarismlinks .= plagiarism_get_links(array('userid' => $submission->userid,
-                    'content' => trim(format_text($onlinetextsubmission->onlinetext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context()))),
+                    'content' => trim($text),
                     'cmid' => $this->assignment->get_course_module()->id,
                     'course' => $this->assignment->get_course()->id,
                     'assignment' => $submission->assignment));
             }
             if ($text != $shorttext) {
                     'cmid' => $this->assignment->get_course_module()->id,
                     'course' => $this->assignment->get_course()->id,
                     'assignment' => $submission->assignment));
             }
             if ($text != $shorttext) {
-                return $shorttext . $plagiarismlinks . get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
+                $wordcount = get_string('numwords', 'assignsubmission_onlinetext', count_words($text));
+
+                return $shorttext . $plagiarismlinks . $wordcount;
             } else {
                 return $shorttext . $plagiarismlinks;
             }
             } else {
                 return $shorttext . $plagiarismlinks;
             }
@@ -252,7 +274,7 @@ class assign_submission_onlinetext extends assign_submission_plugin {
     }
 
     /**
     }
 
     /**
-     * Produce a list of files suitable for export that represent this submission
+     * Produce a list of files suitable for export that represent this submission.
      *
      * @param stdClass $submission - For this is the submission data
      * @param stdClass $user - This is the user record for this submission
      *
      * @param stdClass $submission - For this is the submission data
      * @param stdClass $user - This is the user record for this submission
@@ -260,17 +282,28 @@ class assign_submission_onlinetext extends assign_submission_plugin {
      */
     public function get_files(stdClass $submission, stdClass $user) {
         global $DB;
      */
     public function get_files(stdClass $submission, stdClass $user) {
         global $DB;
+
         $files = array();
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
         $files = array();
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
+
         if ($onlinetextsubmission) {
             $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
         if ($onlinetextsubmission) {
             $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
-            $submissioncontent = "<html><body>". format_text($finaltext, $onlinetextsubmission->onlineformat, array('context'=>$this->assignment->get_context())). "</body></html>";
+            $formattedtext = format_text($finaltext,
+                                         $onlinetextsubmission->onlineformat,
+                                         array('context'=>$this->assignment->get_context()));
+            $submissioncontent = '<html><body>'. $formattedtext . '</body></html>';
 
 
-            $files[get_string('onlinetextfilename', 'assignsubmission_onlinetext')] = array($submissioncontent);
+            $filename = get_string('onlinetextfilename', 'assignsubmission_onlinetext');
+            $files[$filename] = array($submissioncontent);
 
             $fs = get_file_storage();
 
 
             $fs = get_file_storage();
 
-            $fsfiles = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_onlinetext', ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $submission->id, "timemodified", false);
+            $fsfiles = $fs->get_area_files($this->assignment->get_context()->id,
+                                           'assignsubmission_onlinetext',
+                                           ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
+                                           $submission->id,
+                                           'timemodified',
+                                           false);
 
             foreach ($fsfiles as $file) {
                 $files[$file->get_filename()] = $file;
 
             foreach ($fsfiles as $file) {
                 $files[$file->get_filename()] = $file;
@@ -291,18 +324,21 @@ class assign_submission_onlinetext extends assign_submission_plugin {
 
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 
 
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
 
-
         if ($onlinetextsubmission) {
 
         if ($onlinetextsubmission) {
 
-            // render for portfolio API
-            $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA, $onlinetextsubmission->submission, $this->get_type(), 'onlinetext', 'assignsubmission_onlinetext');
+            // Render for portfolio API.
+            $result .= $this->assignment->render_editor_content(ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
+                                                                $onlinetextsubmission->submission,
+                                                                $this->get_type(),
+                                                                'onlinetext',
+                                                                'assignsubmission_onlinetext');
 
         }
 
         return $result;
     }
 
 
         }
 
         return $result;
     }
 
-     /**
+    /**
      * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
      *
      * @param string $type old assignment subtype
      * Return true if this plugin can upgrade an old Moodle 2.2 assignment of this type and version.
      *
      * @param string $type old assignment subtype
@@ -326,7 +362,7 @@ class assign_submission_onlinetext extends assign_submission_plugin {
      * @return bool Was it a success?
      */
     public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
      * @return bool Was it a success?
      */
     public function upgrade_settings(context $oldcontext, stdClass $oldassignment, & $log) {
-        // first upgrade settings (nothing to do)
+        // No settings to upgrade.
         return true;
     }
 
         return true;
     }
 
@@ -340,7 +376,11 @@ class assign_submission_onlinetext extends assign_submission_plugin {
      * @param string $log Record upgrade messages in the log
      * @return bool true or false - false will trigger a rollback
      */
      * @param string $log Record upgrade messages in the log
      * @return bool true or false - false will trigger a rollback
      */
-    public function upgrade(context $oldcontext, stdClass $oldassignment, stdClass $oldsubmission, stdClass $submission, & $log) {
+    public function upgrade(context $oldcontext,
+                            stdClass $oldassignment,
+                            stdClass $oldsubmission,
+                            stdClass $submission,
+                            & $log) {
         global $DB;
 
         $onlinetextsubmission = new stdClass();
         global $DB;
 
         $onlinetextsubmission = new stdClass();
@@ -363,12 +403,11 @@ class assign_submission_onlinetext extends assign_submission_plugin {
             return false;
         }
 
             return false;
         }
 
-        // now copy the area files
+        // Now copy the area files.
         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
                                                         'mod_assignment',
                                                         'submission',
                                                         $oldsubmission->id,
         $this->assignment->copy_area_files_for_upgrade($oldcontext->id,
                                                         'mod_assignment',
                                                         'submission',
                                                         $oldsubmission->id,
-                                                        // New file area
                                                         $this->assignment->get_context()->id,
                                                         'assignsubmission_onlinetext',
                                                         ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
                                                         $this->assignment->get_context()->id,
                                                         'assignsubmission_onlinetext',
                                                         ASSIGNSUBMISSION_ONLINETEXT_FILEAREA,
@@ -383,13 +422,15 @@ class assign_submission_onlinetext extends assign_submission_plugin {
      * @return string
      */
     public function format_for_log(stdClass $submission) {
      * @return string
      */
     public function format_for_log(stdClass $submission) {
-        // format the info for each submission plugin add_to_log
+        // Format the info for each submission plugin (will be logged).
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
         $onlinetextloginfo = '';
         $text = format_text($onlinetextsubmission->onlinetext,
                             $onlinetextsubmission->onlineformat,
                             array('context'=>$this->assignment->get_context()));
         $onlinetextsubmission = $this->get_onlinetext_submission($submission->id);
         $onlinetextloginfo = '';
         $text = format_text($onlinetextsubmission->onlinetext,
                             $onlinetextsubmission->onlineformat,
                             array('context'=>$this->assignment->get_context()));
-        $onlinetextloginfo .= get_string('numwordsforlog', 'assignsubmission_onlinetext', count_words($text));
+        $onlinetextloginfo .= get_string('numwordsforlog',
+                                         'assignsubmission_onlinetext',
+                                         count_words($text));
 
         return $onlinetextloginfo;
     }
 
         return $onlinetextloginfo;
     }
@@ -401,8 +442,8 @@ class assign_submission_onlinetext extends assign_submission_plugin {
      */
     public function delete_instance() {
         global $DB;
      */
     public function delete_instance() {
         global $DB;
-        // will throw exception on failure
-        $DB->delete_records('assignsubmission_onlinetext', array('assignment'=>$this->assignment->get_instance()->id));
+        $DB->delete_records('assignsubmission_onlinetext',
+                            array('assignment'=>$this->assignment->get_instance()->id));
 
         return true;
     }
 
         return true;
     }