Block Code

Block Code

by Josh Coombs -
Number of replies: 0

Hiya All smile

I have recently, made a very small hack, to block forum access completely to guests. It works fine web resources etc, I have added a checkbox, and presents the user with a login screen as intended, and updates the field I added nicely.

Now the problem is when I have done the editing on the forum module lib.php. I have put in placement the update code for the change of value, however it is not blocking to guests! I wondered if someone could kindly just look at my small part of code, to make sure it is correct. It updates the field in mdl_forum fine but guests can still gain access...!

The code is as follows: (Extract from lib.php - under forum mod)

/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////

function forum_add_instance($forum) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.

    global $CFG;

    $forum->timemodified = time();

    if (!$forum->userating) {
        $forum->assessed = 0;
    }

    if (!empty($forum->ratingtime)) {
        $forum->assesstimestart  = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
                                                  $forum->starthour, $forum->startminute, 0);
        $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
                                                  $forum->finishhour, $forum->finishminute, 0);
    } else {
        $forum->assesstimestart  = 0;
        $forum->assesstimefinish = 0;
    }
 
      if ($forum->blockguests == 1) {
        require_login($course->id);
        if (isguest($USER->id)) {
            error(get_string('guestsno', 'forum').".");
        }
    }
 
    //sanitize given values a bit
    $forum->warnafter = clean_param($forum->warnafter, PARAM_INT);
    $forum->blockafter = clean_param($forum->blockafter, PARAM_INT);

    if (! $forum->id = insert_record('forum', $forum)) {
        return false;
    }

    if ($forum->type == 'single') {  // Create related discussion.
        $discussion->course   = $forum->course;
        $discussion->forum    = $forum->id;
        $discussion->name     = $forum->name;
        $discussion->intro    = $forum->intro;
        $discussion->assessed = $forum->assessed;
        $discussion->format   = $forum->format;
        $discussion->mailnow  = false;

        if (! forum_add_discussion($discussion, $discussion->intro)) {
            error('Could not add the discussion for this forum');
        }
    }

    if ($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE) { // all users should be subscribed initially
        $users = get_course_users($forum->course);
        foreach ($users as $user) {
            forum_subscribe($user->id, $forum->id);
        }
    }
 
    return $forum->id;
}


function forum_update_instance($forum) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.

    $forum->timemodified = time();
    $forum->id = $forum->instance;

    if (empty($forum->userating)) {
        $forum->assessed = 0;
    }

    if (!empty($forum->ratingtime)) {
        $forum->assesstimestart  = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
                                                  $forum->starthour, $forum->startminute, 0);
        $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
                                                  $forum->finishhour, $forum->finishminute, 0);
    } else {
        $forum->assesstimestart  = 0;
        $forum->assesstimefinish = 0;
    }
 
    if ($forum->type == 'single') {  // Update related discussion and post.
        if (! $discussion = get_record('forum_discussions', 'forum', $forum->id)) {
            if ($discussions = get_records('forum_discussions', 'forum', $forum->id, 'timemodified ASC')) {
                notify('Warning! There is more than one discussion in this forum - using the most recent');
                $discussion = array_pop($discussions);
            } else {
                error('Could not find the discussion in this forum');
            }
        }
        if (! $post = get_record('forum_posts', 'id', $discussion->firstpost)) {
            error('Could not find the first post in this forum discussion');
        }

        $post->subject  = $forum->name;
        $post->message  = $forum->intro;
        $post->modified = $forum->timemodified;

        if (! update_record('forum_posts', $post)) {
            error('Could not update the first post');
        }

        $discussion->name = $forum->name;

        if (! update_record('forum_discussions', $discussion)) {
            error('Could not update the discussion');
        }
    }
 
 if (isset($forum->blockguests)) {
    $forum ->blockguests = 1;
  } else {
    $forum->blockguests = 0;
  }

    return update_record('forum', $forum);
}

function forum_delete_instance($id) {

 

Hope someone can help, sorry its slightly long winded the two parts of code are:

      if ($forum->blockguests == 1) {
        require_login($course->id);
        if (isguest($USER->id)) {
            error(get_string('guestsno', 'forum').".");
        }
    }


 if (isset($forum->blockguests)) {
    $forum ->blockguests = 1;
  } else {
    $forum->blockguests = 0;
  }


 

Many Thanks, I am just stumped! - BTW I am not an expert on php coding, just a newbie.

Josh

Average of ratings: -