Skip to content
Snippets Groups Projects
locallib.php 87.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • function pdfannotator_timeago($timestamp) {
        $strtime = array(get_string('second', 'pdfannotator'), get_string('minute', 'pdfannotator'), get_string('hour', 'pdfannotator'));
        $strtime[] = get_string('day', 'pdfannotator');
        $strtime[] = get_string('month', 'pdfannotator');
        $strtime[] = get_string('year', 'pdfannotator');
        $strtimeplural = array(get_string('seconds', 'pdfannotator'), get_string('minutes', 'pdfannotator'));
        $strtimeplural[] = get_string('hours', 'pdfannotator');
        $strtimeplural[] = get_string('days', 'pdfannotator');
        $strtimeplural[] = get_string('months', 'pdfannotator');
        $strtimeplural[] = get_string('years', 'pdfannotator');
        $length = array("60", "60", "24", "30", "12", "10");
        $currenttime = time();
        if ($currenttime >= $timestamp) {
            $diff = time() - $timestamp;
            if ($diff < 60) {
                return get_string('justnow', 'pdfannotator');
            }
            for ($i = 0; $diff >= $length[$i] && $i < count($length) - 1; $i++) {
                $diff = $diff / $length[$i];
            }
            $diff = intval(round($diff));
            if ($diff === 1) {
                $diff = $diff . ' ' . $strtime[$i];
            } else {
                $diff = $diff . ' ' . $strtimeplural[$i];
            }
            return get_string('ago', 'pdfannotator', $diff);
        }
    }
    
    /**
     * Function takes a moodle timestamp, calculates how much time has since elapsed
     * and returns this information as a string. If the timestamp is older than 2 days,
     * the ecaxt datetime is returned. Otherwise, the string looks like '3 days ago'.
     *
     * @param type $timestamp
     * @return string
     */
    function pdfannotator_optional_timeago($timestamp) {
        $currenttime = time();
        // For entries older than 2 days, display the exact time.
        if ($currenttime - $timestamp > 172799) {
            return pdfannotator_get_user_datetime_shortformat($timestamp);
        } else {
            return pdfannotator_timeago($timestamp);
        }
    }
    
    function pdfannotator_is_mobile_device() {
        $param = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_DEFAULT); // XXX How to filter, here?
        return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $param);
    }
    
    function pdfannotator_is_phone() {
        $param = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_DEFAULT); // XXX How to filter, here?
        return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $param);
    }
    
    
    function pdfannotator_get_last_answer($annotationid, $context) {
        global $DB;
        $params = array('isquestion' => 0, 'annotationid' => $annotationid);
        $answers = $DB->get_records('pdfannotator_comments', $params, 'timecreated DESC' );
    
        foreach ($answers as $answer) {
            if (!pdfannotator_can_see_comment($answer, $context)) {
                continue;
            } else {
    
                $answer->content = pdfannotator_get_relativelink($answer->content, $answer->id, $context);
    
                return $answer;
            }
        }
        return null;
    }
    
    function pdfannotator_can_see_comment($comment, $context) {
        global $USER, $DB;
        if (is_array($comment)) {
            $comment = (object)$comment;
        }
    
        // If the comment is an answer, it is always saved as public. So, we check the visibility of the corresponding question.
        if (!$comment->isquestion) {
            $question = $DB->get_record('pdfannotator_comments', array('annotationid' => $comment->annotationid, 'isquestion' => '1'));
            $question = (object)$question;
        } else {
            $question = $comment;
        }
    
        // Private Comments are only displayed for the author.
        if ($question->visibility == "private" && $USER->id != $question->userid) {
            return false;
        }
    
        // Protected Comments are only displayed for the author and for the managers.
        if ($question->visibility == "protected" && $USER->id != $question->userid && !has_capability('mod/pdfannotator:viewprotectedcomments', $context)) {
            return false;
        }
        return true;
    }
    
    /**
     * Count how many answers has a question with $annotationid
     * return only answers that the user can see
     */
    function pdfannotator_count_answers($annotationid, $context) {
        global $DB;
        $params = array('isquestion' => 0, 'annotationid' => $annotationid);
        $answers = $DB->get_records('pdfannotator_comments', $params);
        $count = 0;
    
        foreach ($answers as $answer) {
    
            if (!pdfannotator_can_see_comment($answer, $context)) {
                continue;
            }
            $count++;
        }
        return $count;
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
    }