require_sesskey(); switch ($mode) { case FORUM_CHOOSESUBSCRIBE: // 0 \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_CHOOSESUBSCRIBE); redirect($returnto, get_string("everyonecannowchoose", "twf"), 1); break; case FORUM_FORCESUBSCRIBE: // 1 \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_FORCESUBSCRIBE); redirect($returnto, get_string("everyoneisnowsubscribed", "twf"), 1); break; case FORUM_INITIALSUBSCRIBE: // 2 if ($twf->forcesubscribe != FORUM_INITIALSUBSCRIBE) { $users = \mod_twf\subscriptions::get_potential_subscribers($context, 0, 'u.id, u.email', ''); foreach ($users as $user) { \mod_twf\subscriptions::subscribe_user($user->id, $twf, $context); } } \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_INITIALSUBSCRIBE); redirect($returnto, get_string("everyoneisnowsubscribed", "twf"), 1); break; case FORUM_DISALLOWSUBSCRIBE: // 3 \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_DISALLOWSUBSCRIBE); redirect($returnto, get_string("noonecansubscribenow", "twf"), 1); break; default: print_error(get_string('invalidforcesubscribe', 'twf')); }
/** * Get the list of potential subscribers to a twf. * * @param object $twfcontext the twf context. * @param integer $groupid the id of a group, or 0 for all groups. * @param string $fields the list of fields to return for each user. As for get_users_by_capability. * @param string $sort sort order. As for get_users_by_capability. * @return array list of users. * @deprecated since Moodle 2.8 use \mod_twf\subscriptions::get_potential_subscribers() instead */ function twf_get_potential_subscribers($twfcontext, $groupid, $fields, $sort = '') { debugging("twf_get_potential_subscribers() has been deprecated, please use \\mod_twf\\subscriptions::get_potential_subscribers() instead.", DEBUG_DEVELOPER); \mod_twf\subscriptions::get_potential_subscribers($twfcontext, $groupid, $fields, $sort); }
/** * Returns a list of user objects who are subscribed to this twf. * * @param stdClass $twf The twf record. * @param int $groupid The group id if restricting subscriptions to a group of users, or 0 for all. * @param context_module $context the twf context, to save re-fetching it where possible. * @param string $fields requested user fields (with "u." table prefix). * @param boolean $includediscussionsubscriptions Whether to take discussion subscriptions and unsubscriptions into consideration. * @return array list of users. */ public static function fetch_subscribed_users($twf, $groupid = 0, $context = null, $fields = null, $includediscussionsubscriptions = false) { global $CFG, $DB; if (empty($fields)) { $allnames = get_all_user_name_fields(true, 'u'); $fields = "u.id,\n u.username,\n {$allnames},\n u.maildisplay,\n u.mailformat,\n u.maildigest,\n u.imagealt,\n u.email,\n u.emailstop,\n u.city,\n u.country,\n u.lastaccess,\n u.lastlogin,\n u.picture,\n u.timezone,\n u.theme,\n u.lang,\n u.tracktwfs,\n u.mnethostid"; } // Retrieve the twf context if it wasn't specified. $context = twf_get_context($twf->id, $context); if (self::is_forcesubscribed($twf)) { $results = \mod_twf\subscriptions::get_potential_subscribers($context, $groupid, $fields, "u.email ASC"); } else { // Only active enrolled users or everybody on the frontpage. list($esql, $params) = get_enrolled_sql($context, '', $groupid, true); $params['twfid'] = $twf->id; if ($includediscussionsubscriptions) { $params['stwfid'] = $twf->id; $params['dstwfid'] = $twf->id; $params['unsubscribed'] = self::FORUM_DISCUSSION_UNSUBSCRIBED; $sql = "SELECT {$fields}\n FROM (\n SELECT userid FROM {twf_subscriptions} s\n WHERE\n s.twf = :stwfid\n UNION\n SELECT userid FROM {twf_discussion_subs} ds\n WHERE\n ds.twf = :dstwfid AND ds.preference <> :unsubscribed\n ) subscriptions\n JOIN {user} u ON u.id = subscriptions.userid\n JOIN ({$esql}) je ON je.id = u.id\n ORDER BY u.email ASC"; } else { $sql = "SELECT {$fields}\n FROM {user} u\n JOIN ({$esql}) je ON je.id = u.id\n JOIN {twf_subscriptions} s ON s.userid = u.id\n WHERE\n s.twf = :twfid\n ORDER BY u.email ASC"; } $results = $DB->get_records_sql($sql, $params); } // Guest user should never be subscribed to a twf. unset($results[$CFG->siteguest]); // Apply the activity module availability resetrictions. $cm = get_coursemodule_from_instance('twf', $twf->id, $twf->course); $modinfo = get_fast_modinfo($twf->course); $info = new \core_availability\info_module($modinfo->get_cm($cm->id)); $results = $info->filter_user_list($results); return $results; }
/** * 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 $twf twf instance (with magic quotes) * @return bool success */ function twf_update_instance($twf, $mform) { global $DB, $OUTPUT, $USER; $twf->timemodified = time(); $twf->id = $twf->instance; if (empty($twf->assessed)) { $twf->assessed = 0; } if (empty($twf->ratingtime) or empty($twf->assessed)) { $twf->assesstimestart = 0; $twf->assesstimefinish = 0; } $oldtwf = $DB->get_record('twf', array('id' => $twf->id)); // MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then recalculate the grades for the entire twf // 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 ($oldtwf->assessed != $twf->assessed or $oldtwf->scale != $twf->scale) { twf_update_grades($twf); // recalculate grades for the twf } if ($twf->type == 'single') { // Update related discussion and post. $discussions = $DB->get_records('twf_discussions', array('twf' => $twf->id), 'timemodified ASC'); if (!empty($discussions)) { if (count($discussions) > 1) { echo $OUTPUT->notification(get_string('warnformorepost', 'twf')); } $discussion = array_pop($discussions); } else { // try to recover by creating initial discussion - MDL-16262 $discussion = new stdClass(); $discussion->course = $twf->course; $discussion->twf = $twf->id; $discussion->name = $twf->name; $discussion->assessed = $twf->assessed; $discussion->message = $twf->intro; $discussion->messageformat = $twf->introformat; $discussion->messagetrust = true; $discussion->mailnow = false; $discussion->groupid = -1; $message = ''; twf_add_discussion($discussion, null, $message); if (!($discussion = $DB->get_record('twf_discussions', array('twf' => $twf->id)))) { print_error('cannotadd', 'twf'); } } if (!($post = $DB->get_record('twf_posts', array('id' => $discussion->firstpost)))) { print_error('cannotfindfirstpost', 'twf'); } $cm = get_coursemodule_from_instance('twf', $twf->id); $modcontext = context_module::instance($cm->id, MUST_EXIST); $post = $DB->get_record('twf_posts', array('id' => $discussion->firstpost), '*', MUST_EXIST); $post->subject = $twf->name; $post->message = $twf->intro; $post->messageformat = $twf->introformat; $post->messagetrust = trusttext_trusted($modcontext); $post->modified = $twf->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_twf', 'post', $post->id, $options, $post->message); } $DB->update_record('twf_posts', $post); $discussion->name = $twf->name; $DB->update_record('twf_discussions', $discussion); } $DB->update_record('twf', $twf); $modcontext = context_module::instance($twf->coursemodule); if ($twf->forcesubscribe == FORUM_INITIALSUBSCRIBE && $oldtwf->forcesubscribe != $twf->forcesubscribe) { $users = \mod_twf\subscriptions::get_potential_subscribers($modcontext, 0, 'u.id, u.email', ''); foreach ($users as $user) { \mod_twf\subscriptions::subscribe_user($user->id, $twf, $modcontext); } } twf_grade_item_update($twf); return true; }