/**
  * Form definition
  *
  * @return void
  */
 function definition()
 {
     global $CFG, $OUTPUT, $USER;
     $mform =& $this->_form;
     $course = $this->_customdata['course'];
     $cm = $this->_customdata['cm'];
     $coursecontext = $this->_customdata['coursecontext'];
     $modcontext = $this->_customdata['modcontext'];
     $anonforum = $this->_customdata['anonforum'];
     $post = $this->_customdata['post'];
     $edit = $this->_customdata['edit'];
     $thresholdwarning = $this->_customdata['thresholdwarning'];
     $mform->addElement('header', 'general', '');
     //fill in the data depending on page params later using set_data
     // If there is a warning message and we are not editing a post we need to handle the warning.
     if (!empty($thresholdwarning) && !$edit) {
         // Here we want to display a warning if they can still post but have reached the warning threshold.
         if ($thresholdwarning->canpost) {
             $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
             $mform->addElement('html', $OUTPUT->notification($message));
         }
     }
     $mform->addElement('text', 'subject', get_string('subject', 'anonforum'), 'size="48"');
     $mform->setType('subject', PARAM_TEXT);
     $mform->addRule('subject', get_string('required'), 'required', null, 'client');
     $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
     $mform->addElement('editor', 'message', get_string('message', 'anonforum'), null, self::editor_options($modcontext, empty($post->id) ? null : $post->id));
     $mform->setType('message', PARAM_RAW);
     $mform->addRule('message', get_string('required'), 'required', null, 'client');
     $mform->addElement('checkbox', 'anonymouspost', get_string('anonymouspost', 'anonforum'));
     $mform->addHelpButton('anonymouspost', 'anonymouspost', 'anonforum');
     $mform->setDefault('anonymouspost', 1);
     if (isset($anonforum->id) && anonforum_is_forcesubscribed($anonforum)) {
         $mform->addElement('static', 'subscribemessage', get_string('subscription', 'anonforum'), get_string('everyoneissubscribed', 'anonforum'));
         $mform->addElement('hidden', 'subscribe');
         $mform->setType('subscribe', PARAM_INT);
         $mform->addHelpButton('subscribemessage', 'subscription', 'anonforum');
     } else {
         if (isset($anonforum->forcesubscribe) && $anonforum->forcesubscribe != ANONFORUM_DISALLOWSUBSCRIBE || has_capability('moodle/course:manageactivities', $coursecontext)) {
             $options = array();
             $options[0] = get_string('subscribestop', 'anonforum');
             $options[1] = get_string('subscribestart', 'anonforum');
             $mform->addElement('select', 'subscribe', get_string('subscription', 'anonforum'), $options);
             $mform->addHelpButton('subscribe', 'subscription', 'anonforum');
         } else {
             if ($anonforum->forcesubscribe == ANONFORUM_DISALLOWSUBSCRIBE) {
                 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'anonforum'), get_string('disallowsubscribe', 'anonforum'));
                 $mform->addElement('hidden', 'subscribe');
                 $mform->setType('subscribe', PARAM_INT);
                 $mform->addHelpButton('subscribemessage', 'subscription', 'anonforum');
             }
         }
     }
     if (!empty($anonforum->maxattachments) && $anonforum->maxbytes != 1 && has_capability('mod/anonforum:createattachment', $modcontext)) {
         //  1 = No attachments at all
         $mform->addElement('filemanager', 'attachments', get_string('attachment', 'anonforum'), null, self::attachment_options($anonforum));
         $mform->addHelpButton('attachments', 'attachment', 'anonforum');
     }
     if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) {
         // hack alert
         $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'anonforum'));
     }
     if (!empty($CFG->anonforum_enabletimedposts) && !$post->parent && has_capability('mod/anonforum:viewhiddentimedposts', $coursecontext)) {
         // hack alert
         $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'anonforum'));
         $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'anonforum'), array('optional' => true));
         $mform->addHelpButton('timestart', 'displaystart', 'anonforum');
         $mform->addElement('date_selector', 'timeend', get_string('displayend', 'anonforum'), array('optional' => true));
         $mform->addHelpButton('timeend', 'displayend', 'anonforum');
     } else {
         $mform->addElement('hidden', 'timestart');
         $mform->setType('timestart', PARAM_INT);
         $mform->addElement('hidden', 'timeend');
         $mform->setType('timeend', PARAM_INT);
         $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
     }
     if (groups_get_activity_groupmode($cm, $course)) {
         // hack alert
         $groupdata = groups_get_activity_allowed_groups($cm);
         $groupcount = count($groupdata);
         $modulecontext = context_module::instance($cm->id);
         $contextcheck = has_capability('mod/anonforum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount;
         if ($contextcheck) {
             $groupinfo = array('0' => get_string('allparticipants'));
             foreach ($groupdata as $grouptemp) {
                 $groupinfo[$grouptemp->id] = $grouptemp->name;
             }
             $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo);
             $mform->setDefault('groupinfo', $post->groupid);
             $mform->setType('groupinfo', PARAM_INT);
         } else {
             if (empty($post->groupid)) {
                 $groupname = get_string('allparticipants');
             } else {
                 $groupname = format_string($groupdata[$post->groupid]->name);
             }
             $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
         }
     }
     //-------------------------------------------------------------------------------
     // buttons
     if (isset($post->edit)) {
         // hack alert
         $submit_string = get_string('savechanges');
     } else {
         $submit_string = get_string('posttoanonforum', 'anonforum');
     }
     $this->add_action_buttons(false, $submit_string);
     $mform->addElement('hidden', 'course');
     $mform->setType('course', PARAM_INT);
     $mform->addElement('hidden', 'anonforum');
     $mform->setType('anonforum', PARAM_INT);
     $mform->addElement('hidden', 'discussion');
     $mform->setType('discussion', PARAM_INT);
     $mform->addElement('hidden', 'parent');
     $mform->setType('parent', PARAM_INT);
     $mform->addElement('hidden', 'userid');
     $mform->setType('userid', PARAM_INT);
     $mform->addElement('hidden', 'groupid');
     $mform->setType('groupid', PARAM_INT);
     $mform->addElement('hidden', 'edit');
     $mform->setType('edit', PARAM_INT);
     $mform->addElement('hidden', 'reply');
     $mform->setType('reply', PARAM_INT);
 }
示例#2
0
 if (isguestuser() or !$can_subscribe) {
     // there should not be any links leading to this place, just redirect
     redirect(new moodle_url('/mod/anonforum/index.php', array('id' => $id)), get_string('subscribeenrolledonly', 'anonforum'));
 }
 // Can proceed now, the user is not guest and is enrolled
 foreach ($modinfo->get_instances_of('anonforum') as $anonforumid => $cm) {
     $anonforum = $anonforums[$anonforumid];
     $modcontext = context_module::instance($cm->id);
     $cansub = false;
     if (has_capability('mod/anonforum:viewdiscussion', $modcontext)) {
         $cansub = true;
     }
     if ($cansub && $cm->visible == 0 && !has_capability('mod/anonforum:managesubscriptions', $modcontext)) {
         $cansub = false;
     }
     if (!anonforum_is_forcesubscribed($anonforum)) {
         $subscribed = anonforum_is_subscribed($USER->id, $anonforum);
         if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $anonforum->forcesubscribe != ANONFORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
             anonforum_subscribe($USER->id, $anonforumid);
         } else {
             if (!$subscribe && $subscribed) {
                 anonforum_unsubscribe($USER->id, $anonforumid);
             }
         }
     }
 }
 $returnto = anonforum_go_back_to("index.php?id={$course->id}");
 $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
 if ($subscribe) {
     redirect($returnto, get_string('nowallsubscribed', 'anonforum', $shortname), 1);
 } else {
示例#3
0
/**
 * Generate and return the subscribe or unsubscribe link for a anonforum.
 *
 * @param object $anonforum the anonforum. Fields used are $anonforum->id and $anonforum->forcesubscribe.
 * @param object $context the context object for this anonforum.
 * @param array $messages text used for the link in its various states
 *      (subscribed, unsubscribed, forcesubscribed or cantsubscribe).
 *      Any strings not passed in are taken from the $defaultmessages array
 *      at the top of the function.
 * @param bool $cantaccessagroup
 * @param bool $fakelink
 * @param bool $backtoindex
 * @param array $subscribed_anonforums
 * @return string
 */
function anonforum_get_subscribe_link($anonforum, $context, $messages = array(), $cantaccessagroup = false, $fakelink = true, $backtoindex = false, $subscribed_anonforums = null)
{
    global $CFG, $USER, $PAGE, $OUTPUT;
    $defaultmessages = array('subscribed' => get_string('unsubscribe', 'anonforum'), 'unsubscribed' => get_string('subscribe', 'anonforum'), 'cantaccessgroup' => get_string('no'), 'forcesubscribed' => get_string('everyoneissubscribed', 'anonforum'), 'cantsubscribe' => get_string('disallowsubscribe', 'anonforum'));
    $messages = $messages + $defaultmessages;
    if (anonforum_is_forcesubscribed($anonforum)) {
        return $messages['forcesubscribed'];
    } else {
        if ($anonforum->forcesubscribe == ANONFORUM_DISALLOWSUBSCRIBE && !has_capability('mod/anonforum:managesubscriptions', $context)) {
            return $messages['cantsubscribe'];
        } else {
            if ($cantaccessagroup) {
                return $messages['cantaccessgroup'];
            } else {
                if (!is_enrolled($context, $USER, '', true)) {
                    return '';
                }
                if (is_null($subscribed_anonforums)) {
                    $subscribed = anonforum_is_subscribed($USER->id, $anonforum);
                } else {
                    $subscribed = !empty($subscribed_anonforums[$anonforum->id]);
                }
                if ($subscribed) {
                    $linktext = $messages['subscribed'];
                    $linktitle = get_string('subscribestop', 'anonforum');
                } else {
                    $linktext = $messages['unsubscribed'];
                    $linktitle = get_string('subscribestart', 'anonforum');
                }
                $options = array();
                if ($backtoindex) {
                    $backtoindexlink = '&backtoindex=1';
                    $options['backtoindex'] = 1;
                } else {
                    $backtoindexlink = '';
                }
                $link = '';
                if ($fakelink) {
                    $PAGE->requires->js('/mod/anonforum/anonforum.js');
                    $PAGE->requires->js_function_call('anonforum_produce_subscribe_link', array($anonforum->id, $backtoindexlink, $linktext, $linktitle));
                    $link = "<noscript>";
                }
                $options['id'] = $anonforum->id;
                $options['sesskey'] = sesskey();
                $url = new moodle_url('/mod/anonforum/subscribe.php', $options);
                $link .= $OUTPUT->single_button($url, $linktext, 'get', array('title' => $linktitle));
                if ($fakelink) {
                    $link .= '</noscript>';
                }
                return $link;
            }
        }
    }
}