protected function after_execute()
 {
     global $DB;
     // Add quora related files, no need to match by itemname (just internally handled context)
     $this->add_related_files('mod_quora', 'intro', null);
     // If the quora is of type 'single' and no discussion has been ignited
     // (non-userinfo backup/restore) create the discussion here, using quora
     // information as base for the initial post.
     $quoraid = $this->task->get_activityid();
     $quorarec = $DB->get_record('quora', array('id' => $quoraid));
     if ($quorarec->type == 'single' && !$DB->record_exists('quora_discussions', array('quora' => $quoraid))) {
         // Create single discussion/lead post from quora data
         $sd = new stdclass();
         $sd->course = $quorarec->course;
         $sd->quora = $quorarec->id;
         $sd->name = $quorarec->name;
         $sd->assessed = $quorarec->assessed;
         $sd->message = $quorarec->intro;
         $sd->messageformat = $quorarec->introformat;
         $sd->messagetrust = true;
         $sd->mailnow = false;
         $sdid = quora_add_discussion($sd, null, null, $this->task->get_userid());
         // Mark the post as mailed
         $DB->set_field('quora_posts', 'mailed', '1', array('discussion' => $sdid));
         // Copy all the files from mod_foum/intro to mod_quora/post
         $fs = get_file_storage();
         $files = $fs->get_area_files($this->task->get_contextid(), 'mod_quora', 'intro');
         foreach ($files as $file) {
             $newfilerecord = new stdclass();
             $newfilerecord->filearea = 'post';
             $newfilerecord->itemid = $DB->get_field('quora_discussions', 'firstpost', array('id' => $sdid));
             $fs->create_file_from_storedfile($newfilerecord, $file);
         }
     }
     // Add post related files, matching by itemname = 'quora_post'
     $this->add_related_files('mod_quora', 'post', 'quora_post');
     $this->add_related_files('mod_quora', 'attachment', 'quora_post');
 }
Ejemplo n.º 2
0
 /**
  * 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 quora discussion count.
     $this->quoradiscussioncount++;
     $record = (array) $record;
     if (!isset($record['course'])) {
         throw new coding_exception('course must be present in phpunit_util::create_discussion() $record');
     }
     if (!isset($record['quora'])) {
         throw new coding_exception('quora 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->quoradiscussioncount;
     }
     if (!isset($record['subject'])) {
         $record['subject'] = "Subject for discussion " . $this->quoradiscussioncount;
     }
     if (!isset($record['message'])) {
         $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->quoradiscussioncount);
     }
     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";
     }
     $record = (object) $record;
     // Add the discussion.
     $record->id = quora_add_discussion($record, null, null, $record->userid);
     return $record;
 }
Ejemplo n.º 3
0
/**
 * 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 $quora quora instance (with magic quotes)
 * @return bool success
 */
function quora_update_instance($quora, $mform)
{
    global $DB, $OUTPUT, $USER;
    $quora->timemodified = time();
    $quora->id = $quora->instance;
    if (empty($quora->assessed)) {
        $quora->assessed = 0;
    }
    if (empty($quora->ratingtime) or empty($quora->assessed)) {
        $quora->assesstimestart = 0;
        $quora->assesstimefinish = 0;
    }
    $oldquora = $DB->get_record('quora', array('id' => $quora->id));
    // MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then recalculate the grades for the entire quora
    // 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 ($oldquora->assessed != $quora->assessed or $oldquora->scale != $quora->scale) {
        quora_update_grades($quora);
        // recalculate grades for the quora
    }
    if ($quora->type == 'single') {
        // Update related discussion and post.
        $discussions = $DB->get_records('quora_discussions', array('quora' => $quora->id), 'timemodified ASC');
        if (!empty($discussions)) {
            if (count($discussions) > 1) {
                echo $OUTPUT->notification(get_string('warnformorepost', 'quora'));
            }
            $discussion = array_pop($discussions);
        } else {
            // try to recover by creating initial discussion - MDL-16262
            $discussion = new stdClass();
            $discussion->course = $quora->course;
            $discussion->quora = $quora->id;
            $discussion->name = $quora->name;
            $discussion->assessed = $quora->assessed;
            $discussion->message = $quora->intro;
            $discussion->messageformat = $quora->introformat;
            $discussion->messagetrust = true;
            $discussion->mailnow = false;
            $discussion->groupid = -1;
            $message = '';
            quora_add_discussion($discussion, null, $message);
            if (!($discussion = $DB->get_record('quora_discussions', array('quora' => $quora->id)))) {
                print_error('cannotadd', 'quora');
            }
        }
        if (!($post = $DB->get_record('quora_posts', array('id' => $discussion->firstpost)))) {
            print_error('cannotfindfirstpost', 'quora');
        }
        $cm = get_coursemodule_from_instance('quora', $quora->id);
        $modcontext = context_module::instance($cm->id, MUST_EXIST);
        $post = $DB->get_record('quora_posts', array('id' => $discussion->firstpost), '*', MUST_EXIST);
        $post->subject = $quora->name;
        $post->message = $quora->intro;
        $post->messageformat = $quora->introformat;
        $post->messagetrust = trusttext_trusted($modcontext);
        $post->modified = $quora->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_quora', 'post', $post->id, $options, $post->message);
        }
        $DB->update_record('quora_posts', $post);
        $discussion->name = $quora->name;
        $DB->update_record('quora_discussions', $discussion);
    }
    $DB->update_record('quora', $quora);
    $modcontext = context_module::instance($quora->coursemodule);
    if ($quora->forcesubscribe == FORUM_INITIALSUBSCRIBE && $oldquora->forcesubscribe != $quora->forcesubscribe) {
        $users = \mod_quora\subscriptions::get_potential_subscribers($modcontext, 0, 'u.id, u.email', '');
        foreach ($users as $user) {
            \mod_quora\subscriptions::subscribe_user($user->id, $quora, $modcontext);
        }
    }
    quora_grade_item_update($quora);
    return true;
}
Ejemplo n.º 4
0
         $fromform->groupid = $fromform->groupinfo;
     }
     if (empty($fromform->groupid)) {
         $fromform->groupid = -1;
     }
     $groupstopostto = array($fromform->groupid);
 }
 // Before we post this we must check that the user will not exceed the blocking threshold.
 quora_check_blocking_threshold($thresholdwarning);
 foreach ($groupstopostto as $group) {
     if (!quora_user_can_post_discussion($quora, $group, -1, $cm, $modcontext)) {
         print_error('cannotcreatediscussion', 'quora');
     }
     $discussion->groupid = $group;
     $message = '';
     if ($discussion->id = quora_add_discussion($discussion, $mform_post, $message)) {
         $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('quoraid' => $quora->id));
         $event = \mod_quora\event\discussion_created::create($params);
         $event->add_record_snapshot('quora_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", "quora");
             $timemessage = 4;
         } else {
             $message .= '<p>' . get_string("postaddedsuccess", "quora") . '</p>';
             $message .= '<p>' . get_string("postaddedtimeleft", "quora", format_time($CFG->maxeditingtime)) . '</p>';