/**
  * Function to create a dummy discussion.
  *
  * @param array|stdClass $record
  * @return stdClass the discussion object
  */
 public function create_discussion($record = null)
 {
     global $DB;
     // Increment the forum discussion count.
     $this->forumdiscussioncount++;
     $record = (array) $record;
     if (!isset($record['course'])) {
         throw new coding_exception('course must be present in phpunit_util::create_discussion() $record');
     }
     if (!isset($record['forum'])) {
         throw new coding_exception('forum must be present in phpunit_util::create_discussion() $record');
     }
     if (!isset($record['userid'])) {
         throw new coding_exception('userid must be present in phpunit_util::create_discussion() $record');
     }
     if (!isset($record['name'])) {
         $record['name'] = "Discussion " . $this->forumdiscussioncount;
     }
     if (!isset($record['subject'])) {
         $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
     }
     if (!isset($record['message'])) {
         $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
     }
     if (!isset($record['messageformat'])) {
         $record['messageformat'] = editors_get_preferred_format();
     }
     if (!isset($record['messagetrust'])) {
         $record['messagetrust'] = "";
     }
     if (!isset($record['assessed'])) {
         $record['assessed'] = '1';
     }
     if (!isset($record['groupid'])) {
         $record['groupid'] = "-1";
     }
     if (!isset($record['timestart'])) {
         $record['timestart'] = "0";
     }
     if (!isset($record['timeend'])) {
         $record['timeend'] = "0";
     }
     if (!isset($record['mailnow'])) {
         $record['mailnow'] = "0";
     }
     if (!isset($record['reveal'])) {
         $record['reveal'] = 0;
     }
     $record = (object) $record;
     // Add the discussion.
     $record->id = hsuforum_add_discussion($record, null, null, $record->userid);
     return $record;
 }
 protected function after_execute()
 {
     global $DB;
     // Add forum related files, no need to match by itemname (just internally handled context)
     $this->add_related_files('mod_hsuforum', 'intro', null);
     // If the forum is of type 'single' and no discussion has been ignited
     // (non-userinfo backup/restore) create the discussion here, using forum
     // information as base for the initial post.
     $forumid = $this->task->get_activityid();
     $forumrec = $DB->get_record('hsuforum', array('id' => $forumid));
     if ($forumrec->type == 'single' && !$DB->record_exists('hsuforum_discussions', array('forum' => $forumid))) {
         // Create single discussion/lead post from forum data
         $sd = new stdclass();
         $sd->course = $forumrec->course;
         $sd->forum = $forumrec->id;
         $sd->name = $forumrec->name;
         $sd->assessed = $forumrec->assessed;
         $sd->message = $forumrec->intro;
         $sd->messageformat = $forumrec->introformat;
         $sd->messagetrust = true;
         $sd->mailnow = false;
         $sd->reveal = 0;
         $sdid = hsuforum_add_discussion($sd, null, null, $this->task->get_userid());
         // Mark the post as mailed
         $DB->set_field('hsuforum_posts', 'mailed', '1', array('discussion' => $sdid));
         // Copy all the files from mod_foum/intro to mod_hsuforum/post
         $fs = get_file_storage();
         $files = $fs->get_area_files($this->task->get_contextid(), 'mod_hsuforum', 'intro');
         foreach ($files as $file) {
             $newfilerecord = new stdclass();
             $newfilerecord->filearea = 'post';
             $newfilerecord->itemid = $DB->get_field('hsuforum_discussions', 'firstpost', array('id' => $sdid));
             $fs->create_file_from_storedfile($newfilerecord, $file);
         }
     }
     // Add post related files, matching by itemname = 'hsuforum_post'
     $this->add_related_files('mod_hsuforum', 'post', 'hsuforum_post');
     $this->add_related_files('mod_hsuforum', 'attachment', 'hsuforum_post');
 }
 }
 if (empty($fromform->groupid)) {
     $fromform->groupid = -1;
 }
 $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
 $fromform->reveal = empty($fromform->reveal) ? 0 : 1;
 $discussion = $fromform;
 $discussion->name = $fromform->subject;
 $newstopic = false;
 if ($forum->type == 'news' && !$fromform->parent) {
     $newstopic = true;
 }
 $discussion->timestart = $fromform->timestart;
 $discussion->timeend = $fromform->timeend;
 $message = '';
 if ($discussion->id = hsuforum_add_discussion($discussion, $mform_post, $message)) {
     $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id));
     $event = \mod_hsuforum\event\discussion_created::create($params);
     $event->add_record_snapshot('hsuforum_discussions', $discussion);
     $event->trigger();
     $timemessage = 2;
     if (!empty($message)) {
         // if we're printing stuff about the file upload
         $timemessage = 4;
     }
     if ($fromform->mailnow) {
         $message .= get_string("postmailnow", "hsuforum");
         $timemessage = 4;
     } else {
         $message .= '<p>' . get_string("postaddedsuccess", "hsuforum") . '</p>';
         $message .= '<p>' . get_string("postaddedtimeleft", "hsuforum", format_time($CFG->maxeditingtime)) . '</p>';
 /**
  * Save the discussion to the DB
  *
  * @param object $discussion
  * @param upload_file $uploader
  */
 public function save_discussion($discussion, upload_file $uploader)
 {
     $message = '';
     $discussion->id = hsuforum_add_discussion($discussion, null, $message);
     $file = $uploader->process_file_upload($discussion->firstpost);
     if (!is_null($file)) {
         $this->db->set_field('hsuforum_posts', 'attachment', 1, array('id' => $discussion->firstpost));
     }
 }
/**
 * Given an object containing all the necessary data,
 * (defined by the form in mod_form.php) this function
 * will update an existing instance with new data.
 *
 * @global object
 * @param object $forum forum instance (with magic quotes)
 * @return bool success
 */
function hsuforum_update_instance($forum, $mform)
{
    global $DB, $OUTPUT, $USER;
    $forum->timemodified = time();
    $forum->id = $forum->instance;
    if ($forum->gradetype != HSUFORUM_GRADETYPE_MANUAL) {
        foreach ($forum as $name => $value) {
            if (strpos($name, 'advancedgradingmethod_') !== false) {
                $forum->{$name} = '';
            }
        }
    }
    if (empty($forum->assessed)) {
        $forum->assessed = 0;
    }
    if (empty($forum->ratingtime) or empty($forum->assessed)) {
        $forum->assesstimestart = 0;
        $forum->assesstimefinish = 0;
    }
    $oldforum = $DB->get_record('hsuforum', array('id' => $forum->id));
    // MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then recalculate the grades for the entire forum
    // if  scale changes - do we need to recheck the ratings, if ratings higher than scale how do we want to respond?
    // for count and sum aggregation types the grade we check to make sure they do not exceed the scale (i.e. max score) when calculating the grade
    if ($oldforum->assessed != $forum->assessed or $oldforum->scale != $forum->scale) {
        hsuforum_update_grades($forum);
        // recalculate grades for the forum
    }
    if ($forum->type == 'single') {
        // Update related discussion and post.
        $discussions = $DB->get_records('hsuforum_discussions', array('forum' => $forum->id), 'timemodified ASC');
        if (!empty($discussions)) {
            if (count($discussions) > 1) {
                echo $OUTPUT->notification(get_string('warnformorepost', 'hsuforum'));
            }
            $discussion = array_pop($discussions);
        } else {
            // try to recover by creating initial discussion - MDL-16262
            $discussion = new stdClass();
            $discussion->course = $forum->course;
            $discussion->forum = $forum->id;
            $discussion->name = $forum->name;
            $discussion->assessed = $forum->assessed;
            $discussion->message = $forum->intro;
            $discussion->messageformat = $forum->introformat;
            $discussion->messagetrust = true;
            $discussion->mailnow = false;
            $discussion->groupid = -1;
            $discussion->reveal = 0;
            $message = '';
            hsuforum_add_discussion($discussion, null, $message);
            if (!($discussion = $DB->get_record('hsuforum_discussions', array('forum' => $forum->id)))) {
                print_error('cannotadd', 'hsuforum');
            }
        }
        if (!($post = $DB->get_record('hsuforum_posts', array('id' => $discussion->firstpost)))) {
            print_error('cannotfindfirstpost', 'hsuforum');
        }
        $cm = get_coursemodule_from_instance('hsuforum', $forum->id);
        $modcontext = context_module::instance($cm->id, MUST_EXIST);
        $post = $DB->get_record('hsuforum_posts', array('id' => $discussion->firstpost), '*', MUST_EXIST);
        $post->subject = $forum->name;
        $post->message = $forum->intro;
        $post->messageformat = $forum->introformat;
        $post->messagetrust = trusttext_trusted($modcontext);
        $post->modified = $forum->timemodified;
        $post->userid = $USER->id;
        // MDL-18599, so that current teacher can take ownership of activities.
        if ($mform and $draftid = file_get_submitted_draft_itemid('introeditor')) {
            // Ugly hack - we need to copy the files somehow.
            $options = array('subdirs' => true);
            // Use the same options as intro field!
            $post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_hsuforum', 'post', $post->id, $options, $post->message);
        }
        $DB->update_record('hsuforum_posts', $post);
        $discussion->name = $forum->name;
        $DB->update_record('hsuforum_discussions', $discussion);
    }
    $DB->update_record('hsuforum', $forum);
    $modcontext = context_module::instance($forum->coursemodule);
    if ($forum->forcesubscribe == HSUFORUM_INITIALSUBSCRIBE && $oldforum->forcesubscribe != $forum->forcesubscribe) {
        $users = hsuforum_get_potential_subscribers($modcontext, 0, 'u.id, u.email', '');
        foreach ($users as $user) {
            hsuforum_subscribe($user->id, $forum->id, $modcontext);
        }
    }
    hsuforum_grade_item_update($forum);
    return true;
}