Пример #1
0
/**
 * @global object
 * @global object
 * @uses DEBUG_DEVELOPER
 * @param object $anonforum
 * @param object $discussion
 * @param object $context
 * @param object $user
 * @return bool
 */
function anonforum_user_can_see_discussion($anonforum, $discussion, $context, $user = NULL)
{
    global $USER, $DB;
    if (empty($user) || empty($user->id)) {
        $user = $USER;
    }
    // retrieve objects (yuk)
    if (is_numeric($anonforum)) {
        debugging('missing full anonforum', DEBUG_DEVELOPER);
        if (!($anonforum = $DB->get_record('anonforum', array('id' => $anonforum)))) {
            return false;
        }
    }
    if (is_numeric($discussion)) {
        debugging('missing full discussion', DEBUG_DEVELOPER);
        if (!($discussion = $DB->get_record('anonforum_discussions', array('id' => $discussion)))) {
            return false;
        }
    }
    if (!($cm = get_coursemodule_from_instance('anonforum', $anonforum->id, $anonforum->course))) {
        print_error('invalidcoursemodule');
    }
    if (!has_capability('mod/anonforum:viewdiscussion', $context)) {
        return false;
    }
    if (!anonforum_user_can_see_timed_discussion($discussion, $user, $context)) {
        return false;
    }
    if (!anonforum_user_can_see_group_discussion($discussion, $cm, $context)) {
        return false;
    }
    if ($anonforum->type == 'qanda' && !anonforum_user_has_posted($anonforum->id, $discussion->id, $user->id) && !has_capability('mod/anonforum:viewqandawithoutposting', $context)) {
        return false;
    }
    return true;
}
Пример #2
0
 /**
  * Returns a list of anonforum discussions as well as a summary of the discussion
  * in a provided list of anonforums.
  *
  * @param array $anonforumids the anonforum ids
  * @return array the anonforum discussion details
  * @since Moodle 2.5
  */
 public static function get_anonforum_discussions($anonforumids)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . "/mod/anonforum/lib.php";
     // Validate the parameter.
     $params = self::validate_parameters(self::get_anonforum_discussions_parameters(), array('anonforumids' => $anonforumids));
     $anonforumids = $params['anonforumids'];
     // Array to store the anonymous forum discussions to return.
     $arrdiscussions = array();
     // Keep track of the course ids we have performed a require_course_login check on to avoid repeating.
     $arrcourseschecked = array();
     // Store the modinfo for the anonymous forums in an individual courses.
     $arrcoursesanonforuminfo = array();
     // Keep track of the users we have looked up in the DB.
     $arrusers = array();
     // Loop through them.
     foreach ($anonforumids as $id) {
         // Get the anonymous forum object.
         $anonforum = $DB->get_record('anonforum', array('id' => $id), '*', MUST_EXIST);
         // Check that that user can view this course if check not performed yet.
         if (!in_array($anonforum->course, $arrcourseschecked)) {
             // Check the user can function in this context.
             self::validate_context(context_course::instance($anonforum->course));
             // Add to the array.
             $arrcourseschecked[] = $anonforum->course;
         }
         // Get the modinfo for the course if we haven't already.
         if (!isset($arrcoursesanonforuminfo[$anonforum->course])) {
             $modinfo = get_fast_modinfo($anonforum->course);
             $arrcoursesanonforuminfo[$anonforum->course] = $modinfo->get_instances_of('anonforum');
         }
         // Check if this anonymous forum does not exist in the modinfo array, should always be false unless DB is borked.
         if (empty($arrcoursesanonforuminfo[$anonforum->course][$anonforum->id])) {
             throw new moodle_exception('invalidmodule', 'error');
         }
         // We now have the course module.
         $cm = $arrcoursesanonforuminfo[$anonforum->course][$anonforum->id];
         // If the anonymous forum is not visible throw an exception.
         if (!$cm->uservisible) {
             throw new moodle_exception('nopermissiontoshow', 'error');
         }
         // Get the module context.
         $modcontext = context_module::instance($cm->id);
         // Check they have the view anonymous forum capability.
         require_capability('mod/anonforum:viewdiscussion', $modcontext);
         // Check if they can view full names.
         $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
         // Get the unreads array, this takes a anonymous forum id and returns data for all discussions.
         $unreads = array();
         if ($cantrack = anonforum_tp_can_track_anonforums($anonforum)) {
             if ($anonforumtracked = anonforum_tp_is_tracked($anonforum)) {
                 $unreads = anonforum_get_discussions_unread($cm);
             }
         }
         // The anonforum function returns the replies for all the discussions in a given anonymous forum.
         $replies = anonforum_count_discussion_replies($id);
         // Get the discussions for this anonymous forum.
         if ($discussions = $DB->get_records('anonforum_discussions', array('anonforum' => $id))) {
             foreach ($discussions as $discussion) {
                 // If the anonymous forum is of type qanda and the user has not posted in the discussion
                 // we need to ensure that they have the required capability.
                 if ($anonforum->type == 'qanda' && !anonforum_user_has_posted($discussion->anonforum, $discussion->id, $USER->id)) {
                     require_capability('mod/anonforum:viewqandawithoutposting', $modcontext);
                 }
                 $usernamefields = user_picture::fields();
                 // If we don't have the users details then perform DB call.
                 if (empty($arrusers[$discussion->userid])) {
                     $arrusers[$discussion->userid] = $DB->get_record('user', array('id' => $discussion->userid), $usernamefields, MUST_EXIST);
                 }
                 // Get the subject.
                 $subject = $DB->get_field('anonforum_posts', 'subject', array('id' => $discussion->firstpost), MUST_EXIST);
                 // Create object to return.
                 $return = new stdClass();
                 $return->id = (int) $discussion->id;
                 $return->course = $discussion->course;
                 $return->anonforum = $discussion->anonforum;
                 $return->name = $discussion->name;
                 $return->userid = $discussion->userid;
                 $return->groupid = $discussion->groupid;
                 $return->assessed = $discussion->assessed;
                 $return->timemodified = (int) $discussion->timemodified;
                 $return->usermodified = $discussion->usermodified;
                 $return->timestart = $discussion->timestart;
                 $return->timeend = $discussion->timeend;
                 $return->firstpost = (int) $discussion->firstpost;
                 $return->firstuserfullname = fullname($arrusers[$discussion->userid], $canviewfullname);
                 $return->firstuserimagealt = $arrusers[$discussion->userid]->imagealt;
                 $return->firstuserpicture = $arrusers[$discussion->userid]->picture;
                 $return->firstuseremail = $arrusers[$discussion->userid]->email;
                 $return->subject = $subject;
                 $return->numunread = '';
                 if ($cantrack && $anonforumtracked) {
                     if (isset($unreads[$discussion->id])) {
                         $return->numunread = (int) $unreads[$discussion->id];
                     }
                 }
                 // Check if there are any replies to this discussion.
                 if (!empty($replies[$discussion->id])) {
                     $return->numreplies = (int) $replies[$discussion->id]->replies;
                     $return->lastpost = (int) $replies[$discussion->id]->lastpostid;
                 } else {
                     // No replies, so the last post will be the first post.
                     $return->numreplies = 0;
                     $return->lastpost = (int) $discussion->firstpost;
                 }
                 // Get the last post as well as the user who made it.
                 $lastpost = $DB->get_record('anonforum_posts', array('id' => $return->lastpost), '*', MUST_EXIST);
                 if (empty($arrusers[$lastpost->userid])) {
                     $arrusers[$lastpost->userid] = $DB->get_record('user', array('id' => $lastpost->userid), $usernamefields, MUST_EXIST);
                 }
                 $return->lastuserid = $lastpost->userid;
                 $return->lastuserfullname = fullname($arrusers[$lastpost->userid], $canviewfullname);
                 $return->lastuserimagealt = $arrusers[$lastpost->userid]->imagealt;
                 $return->lastuserpicture = $arrusers[$lastpost->userid]->picture;
                 $return->lastuseremail = $arrusers[$lastpost->userid]->email;
                 // Add the discussion statistics to the array to return.
                 $arrdiscussions[$return->id] = (array) $return;
             }
         }
     }
     return $arrdiscussions;
 }
Пример #3
0
            if ($anonforumidcompare and $anonforumtypecheck) {
                $url = "/mod/anonforum/discuss.php?d={$discussion->id}&move={$anonforumcm->instance}&sesskey=" . sesskey();
                $anonforummenu[$section][$sectionname][$url] = format_string($anonforumcm->name);
            }
        }
        if (!empty($anonforummenu)) {
            echo '<div class="movediscussionoption">';
            $select = new url_select($anonforummenu, '', array('' => get_string("movethisdiscussionto", "anonforum")), 'anonforummenu', get_string('move'));
            echo $OUTPUT->render($select);
            echo "</div>";
        }
    }
    echo "</div>";
}
echo '<div class="clearfloat">&nbsp;</div>';
echo "</div>";
if (!empty($anonforum->blockafter) && !empty($anonforum->blockperiod)) {
    $a = new stdClass();
    $a->blockafter = $anonforum->blockafter;
    $a->blockperiod = get_string('secondstotime' . $anonforum->blockperiod);
    echo $OUTPUT->notification(get_string('thisanonforumisthrottled', 'anonforum', $a));
}
if ($anonforum->type == 'qanda' && !has_capability('mod/anonforum:viewqandawithoutposting', $modcontext) && !anonforum_user_has_posted($anonforum->id, $discussion->id, $USER->id)) {
    echo $OUTPUT->notification(get_string('qandanotify', 'anonforum'));
}
if ($move == -1 and confirm_sesskey()) {
    echo $OUTPUT->notification(get_string('discussionmoved', 'anonforum', format_string($anonforum->name, true)));
}
$canrate = has_capability('mod/anonforum:rate', $modcontext);
anonforum_print_discussion($course, $cm, $anonforum, $discussion, $post, $displaymode, $canreply, $canrate);
echo $OUTPUT->footer();