Skip to content
Snippets Groups Projects
locallib.php 87.2 KiB
Newer Older
  • Learn to ignore specific revisions
  •     $capabilities->viewposts = has_capability('mod/pdfannotator:viewposts', $context);
        $capabilities->viewreports = has_capability('mod/pdfannotator:viewreports', $context);
    
        $params = array($pdfannotator->id, $cmid, $capabilities, $action['action']);
        $PAGE->requires->js_init_call('startOverview', $params, true); // 1. name of JS function, 2. parameters.
    }
    
    /**
     * Function serves as subcontroller that tells the annotator model to collect
     * all or all unsolved/solved questions asked in this course.
     *
     * @param int $openannotator
     * @param int $courseid
     * @param type $questionfilter
     * @return type
     */
    function pdfannotator_get_questions($courseid, $context, $questionfilter) {
    
        global $DB;
    
        $cminfo = pdfannotator_instance::get_cm_info($courseid);
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cminfo));
    
        $sql = "SELECT a.id as annoid, a.page, a.pdfannotatorid, p.name AS pdfannotatorname, p.usevotes, cm.id AS cmid, c.isquestion, "
            . "c.id as commentid, c.content, c.userid, c.visibility, c.timecreated, c.isdeleted, c.ishidden, "
            . "SUM(vote) AS votes, MAX(answ.timecreated) AS lastanswered "
            . "FROM {pdfannotator_annotations} a "
            . "JOIN {pdfannotator_comments} c ON c.annotationid = a.id "
            . "JOIN {pdfannotator} p ON a.pdfannotatorid = p.id "
            . "JOIN {course_modules} cm ON p.id = cm.instance "
            . "LEFT JOIN {pdfannotator_votes} v ON c.id=v.commentid "
            . "LEFT JOIN {pdfannotator_comments} answ ON answ.annotationid = a.id "
            . "WHERE c.isquestion = 1 AND p.course = ? AND cm.id $insql";
        if ($questionfilter == 0) {
            $sql = $sql . ' AND c.solved = 0 ';
        }
        if ($questionfilter == 1) {
            $sql = $sql . ' AND NOT c.solved = 0 ';
        }
        $sql = $sql . "GROUP BY a.id, p.name, p.usevotes, cm.id, c.id, a.page, a.pdfannotatorid, c.content, c.userid, c.visibility,"
            . "c.timecreated, c.isdeleted, c.ishidden, c.isquestion";
        $params = array_merge([$courseid], $inparams);
        $questions = $DB->get_records_sql($sql, $params);
    
        $seehidden = has_capability('mod/pdfannotator:seehiddencomments', $context);
        $labelhidden = "<br><span class='tag tag-info'>" . get_string('hiddenfromstudents') . "</span>"; // XXX use moodle method if exists.
        $labelunavailable = "<br><span class='tag tag-info'>" . get_string('restricted') . "</span>";
    
        $res = [];
        foreach ($questions as $key => $question) {
    
            if (!pdfannotator_can_see_comment($question, $context)) {
                continue;
            }
    
            if (empty($question->votes)) {
                $question->votes = 0;
            }
            if ($question->usevotes == 0) {
                $question->votes = '-';
            }
            $question->answercount = pdfannotator_count_answers($question->annoid, $context);
    
            $lastanswer = pdfannotator_get_last_answer($question->annoid, $context);
            if ($lastanswer) {
                $question->lastuser = $lastanswer->userid;
                $question->lastuservisibility = $lastanswer->visibility;
            } else {
                $question->lastanswered = false;
            }
    
            if ($question->isdeleted == 1) {
                $question->content = "<em>" . get_string('deletedQuestion', 'pdfannotator') . "</em>";
            } else if ($question->ishidden) {
                switch ($seehidden) {
                    case 0:
                        $question->content = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                        break;
                    case 1:
                        $question->content = $question->content . $labelhidden;
                        $question->displayhidden = true;
                        break;
                    default:
                        $question->content = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                }
            }
    
            if (!$cminfo[$question->cmid]['visible']) { // Annotator is not visible for students.
                $question->content = $question->content . $labelhidden;
                $question->displayhidden = true;
            }
            if ($cminfo[$question->cmid]['availableinfo']) { // Annotator is restricted.
                $question->content = $question->content . $labelunavailable . " " . $cminfo[$question->cmid]['availableinfo'];
                $question->displayhidden = true;
            }
    
    
            $question->content = pdfannotator_get_relativelink($question->content, $question->commentid, $context);
    
            $question->content = format_text($question->content, $options = ['filter' => true]);
            $question->link = (new moodle_url('/mod/pdfannotator/view.php', array('id' => $question->cmid,
                'page' => $question->page, 'annoid' => $question->annoid, 'commid' => $question->commentid)))->out();
    
            $res[] = $question;
    
        }
        return $res;
    }
    
    /**
     * Function serves as subcontroller that tells the annotator model to collect all
     * questions and answers this user posted in the course.
     *
     * @param int $courseid
     * @return type
     */
    function pdfannotator_get_posts_by_this_user($courseid, $context) {
    
        global $DB, $USER;
    
        $cminfo = pdfannotator_instance::get_cm_info($courseid);
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cminfo));
    
        $seehidden = has_capability('mod/pdfannotator:seehiddencomments', $context);
        $labelhidden = "<br><span class='tag tag-info'>" . get_string('hiddenforparticipants', 'pdfannotator') . "</span>";
        $labelunavailable = "<br><span class='tag tag-info'>" . get_string('restricted') . "</span>";
    
        $sql = "SELECT c.id as commid, c.annotationid, c.content, c.timemodified, c.ishidden, a.id AS annoid, "
            . "a.page, a.pdfannotatorid, p.name AS pdfannotatorname, p.usevotes, cm.id AS cmid, "
            . "SUM(v.vote) AS votes "
            . "FROM {pdfannotator_comments} c "
            . "JOIN {pdfannotator_annotations} a ON c.annotationid = a.id "
            . "JOIN {pdfannotator} p ON a.pdfannotatorid = p.id "
            . "JOIN {course_modules} cm ON p.id = cm.instance "
            . "LEFT JOIN {pdfannotator_votes} v ON c.id = v.commentid "
            . "WHERE c.userid = ? AND p.course = ? AND cm.id $insql "
            . "GROUP BY a.id, p.name, p.usevotes, cm.id, c.id, c.annotationid, c.content, c.timemodified, c.ishidden, a.page, a.pdfannotatorid";
    
        $params = array_merge([$USER->id, $courseid], $inparams);
    
        $posts = $DB->get_records_sql($sql, $params);
    
        foreach ($posts as $key => $post) {
            if (empty($post->votes)) {
                $post->votes = 0;
            }
            if ($post->usevotes == 0) {
                $post->votes = '-';
            }
    
            if ($post->ishidden) { // Post in annotator is hidden.
                switch ($seehidden) {
                    case 0:
                        $post->content = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                        break;
                    case 1:
                        $post->content = $post->content . $labelhidden;
                        $post->displayhidden = true;
                        break;
                    default:
                        $post->content = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                }
            }
    
            if (!$cminfo[$post->cmid]['visible']) {  // Annotator is hidden.
                $post->content = $post->content . $labelhidden;
                $post->displayhidden = true;
            }
            if ($cminfo[$post->cmid]['availableinfo']) {  // Annotator is restricted.
                $post->content = $post->content . $labelunavailable . " " . $cminfo[$post->cmid]['availableinfo'];
                $post->displayhidden = true;
            }
    
            $params = array('id' => $post->cmid, 'page' => $post->page, 'annoid' => $post->annotationid, 'commid' => $post->commid);
            $post->link = (new moodle_url('/mod/pdfannotator/view.php', $params))->out();
    
            $post->content = pdfannotator_get_relativelink($post->content, $post->commid, $context);
    
            $post->content = format_text($post->content, $options = ['filter' => true]);
        }
        return $posts;
    }
    
    /**
     * Function serves as subcontroller that tells the annotator model to collect
     * all answers given to questions that the current user asked or subscribed to
     * in this course.
     *
     * @param int $courseid
     * @param Moodle object? $context
     * @param int $answerfilter
     * @return array of stdClass objects
     */
    function pdfannotator_get_answers_for_this_user($courseid, $context, $answerfilter = 1) {
    
        global $DB, $USER;
    
        $cminfo = pdfannotator_instance::get_cm_info($courseid);
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cminfo));
    
        $seehidden = has_capability('mod/pdfannotator:seehiddencomments', $context);
        $labelhidden = "<br><span class='tag tag-info'>" . get_string('hiddenforparticipants', 'pdfannotator') . "</span>";
        $labelunavailable = "<br><span class='tag tag-info'>" . get_string('restricted') . "</span>";
    
        if ($answerfilter == 0) { // Either: get all answers in this annotator.
            $sql = "SELECT c.id AS answerid, c.content AS answer, c.userid AS userid, c.visibility, "
                . "c.timemodified, c.solved AS correct, c.ishidden AS answerhidden, a.id AS annoid, a.page, q.id AS questionid,"
                . "q.userid AS questionuserid, c.isquestion, c.annotationid, "
                . "q.visibility AS questionvisibility, "
                . "q.content AS answeredquestion, q.isdeleted AS questiondeleted, q.ishidden AS questionhidden, p.id AS annotatorid, "
                . "p.name AS pdfannotatorname, cm.id AS cmid, s.id AS issubscribed "
                . "FROM {pdfannotator_annotations} a "
                . "LEFT JOIN {pdfannotator_subscriptions} s ON a.id = s.annotationid AND s.userid = ? "
                . "JOIN {pdfannotator_comments} q ON q.annotationid = a.id " // Question comment.
                . "JOIN {pdfannotator_comments} c ON c.annotationid = a.id " // Answer comment.
                . "JOIN {pdfannotator} p ON a.pdfannotatorid = p.id "
                . "JOIN {course_modules} cm ON p.id = cm.instance "
                . "WHERE p.course = ? AND q.isquestion = 1 AND NOT c.isquestion = 1 AND NOT c.isdeleted = 1 AND cm.id $insql "
                . "ORDER BY annoid ASC";
        } else { // Or: get answers to those questions the user subscribed to.
            $sql = "SELECT c.id AS answerid, c.content AS answer, c.userid AS userid, c.visibility, "
                . "c.timemodified, c.solved AS correct, c.ishidden AS answerhidden, a.id AS annoid, a.page, q.id AS questionid, "
                . "q.userid AS questionuserid, c.isquestion, c.annotationid, "
                . "q.visibility AS questionvisibility, "
                . "q.content AS answeredquestion, q.isdeleted AS questiondeleted, q.ishidden AS questionhidden, p.id AS annotatorid, "
                . "p.name AS pdfannotatorname, cm.id AS cmid "
                . "FROM {pdfannotator_subscriptions} s "
                . "JOIN {pdfannotator_annotations} a ON a.id = s.annotationid "
                . "JOIN {pdfannotator_comments} q ON q.annotationid = a.id " // Question comment.
                . "JOIN {pdfannotator_comments} c ON c.annotationid = a.id " // Answer comment.
                . "JOIN {pdfannotator} p ON a.pdfannotatorid = p.id "
                . "JOIN {course_modules} cm ON p.id = cm.instance "
                . "WHERE s.userid = ? AND p.course = ? AND q.isquestion = 1 AND NOT c.isquestion = 1 AND NOT c.isdeleted = 1 AND cm.id $insql "
                . "ORDER BY annoid ASC";
        }
    
        $params = array_merge([$USER->id, $courseid], $inparams);
    
        $entries = $DB->get_records_sql($sql, $params);
    
        $res = [];
        foreach ($entries as $key => $entry) {
            if (!pdfannotator_can_see_comment($entry, $context)) {
                continue;
            }
            $entry->link = (new moodle_url('/mod/pdfannotator/view.php',
                array('id' => $entry->cmid, 'page' => $entry->page, 'annoid' => $entry->annoid, 'commid' => $entry->answerid)))->out();
            $entry->questionlink = (new moodle_url('/mod/pdfannotator/view.php',
                array('id' => $entry->cmid, 'page' => $entry->page, 'annoid' => $entry->annoid, 'commid' => $entry->questionid)))->out();
    
            if ($entry->questiondeleted == 1) {
                $entry->answeredquestion = get_string('deletedComment', 'pdfannotator');
            } else if ($entry->questionhidden) {
                switch ($seehidden) {
                    case 0:
                        $entry->answeredquestion = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                        break;
                    case 1:
                        $entry->displayquestionhidden = true;
                        $entry->answeredquestion = $entry->answeredquestion . $labelhidden;
                        break;
                    default:
                        $entry->answeredquestion = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                }
            }
    
            if ($entry->answerhidden) {
                switch ($seehidden) {
                    case 0:
                        $entry->answer = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                        break;
                    case 1:
                        $entry->answer = $entry->answer . $labelhidden;
                        $entry->displayhidden = true;
                        break;
                    default:
                        $entry->answer = "<em>" . get_string('hiddenComment', 'pdfannotator') . "</em>";
                }
            }
    
            if (!$cminfo[$entry->cmid]['visible']) {  // Annotator is hidden.
                $entry->answeredquestion = $entry->answeredquestion . $labelhidden;
                $entry->answer = $entry->answer . $labelhidden;
                $entry->displayhidden = true;
            }
            if ($cminfo[$entry->cmid]['availableinfo']) {  // Annotator is restricted.
                $entry->answeredquestion = $entry->answeredquestion . $labelunavailable . " ". $cminfo[$entry->cmid]['availableinfo'];;
                $entry->answer = $entry->answer . $labelunavailable . " ". $cminfo[$entry->cmid]['availableinfo'];
                $entry->displayhidden = true;
            }
    
            $res[] = $entry;
        }
    
        return $res;
    }
    
    /**
     * Function retrieves reports and their respective reported comments from db.
     * Depending on the reportfilter, only read/unread reports or all reports are retrieved.
     *
     * @param int $courseid
     * @param int $reportfilter: 0 for unread, 1 for read, 2 for all
     * @return array of report objects
     */
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
    function pdfannotator_get_reports($courseid, $context, $reportfilter = 0) {
    
    
        global $DB;
    
        $cminfo = pdfannotator_instance::get_cm_info($courseid);
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cminfo));
    
        // Retrieve reports from db as an array of stdClass objects, representing a report record each.
        $sql = "SELECT r.id as reportid, r.commentid, r.message as report, r.userid AS reportinguser, r.timecreated, r.seen, "
            . "a.page, c.id AS commentid, c.annotationid, c.userid AS commentauthor, c.content AS reportedcomment, c.timecreated AS commenttime, c.visibility, "
            . "p.id AS annotatorid, p.name AS pdfannotatorname, cm.id AS cmid, cm.visible AS cmvisible "
            . "FROM {pdfannotator_reports} r "
            . "JOIN {pdfannotator_comments} c ON r.commentid = c.id "
            . "JOIN {pdfannotator_annotations} a ON c.annotationid = a.id "
            . "JOIN {pdfannotator} p ON a.pdfannotatorid = p.id "
            . "JOIN {course_modules} cm ON p.id = cm.instance "
            . "WHERE cm.id $insql AND r.courseid = ?"; // Be careful with order of parameters!
    
        if ($reportfilter != 2) {
            $sql = $sql . ' AND r.seen = ?';
            $params = array($courseid, $reportfilter);
        } else {
            $params = array($courseid);
        }
        $params = array_merge($inparams, $params); // Be careful with order of parameters!
        $reports = $DB->get_records_sql($sql, $params);
    
        foreach ($reports as $report) {
            $report->link = (new moodle_url('/mod/pdfannotator/view.php',
                array('id' => $report->cmid, 'page' => $report->page, 'annoid' => $report->annotationid, 'commid' => $report->commentid)))->out();
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
            $report->reportedcomment = pdfannotator_get_relativelink($report->reportedcomment, $report->commentid, $context);
    
            $report->reportedcomment = format_text($report->reportedcomment, $options = ['filter' => true]);
    
            $questionid = $DB->get_record('pdfannotator_comments', ['annotationid' => $report->annotationid, 'isquestion' => 1], 'id');
            $report->report = pdfannotator_get_relativelink($report->report, $questionid, $context);
    
    1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
            $report->report = format_text($report->report, $options = ['filter' => true]);
        }
        return $reports;
    }
    
    /**
     * Comparison functions (for sorting tables on overview tab).
     */
    class pdfannotator_compare {
    
        public static function compare_votes_ascending($a, $b) {
            if ($a->usevotes == 0 && $b->usevotes == 0 && $a->votes == $b->votes) {
                return 0;
            }
            return ($a->usevotes != 1 || ($a->votes < $b->votes)) ? -1 : 1;
        }
    
        public static function compare_votes_descending($a, $b) {
            if ($a->usevotes == 0 && $b->usevotes == 0 && $a->votes == $b->votes) {
                return 0;
            }
            return ($b->usevotes != 1 || ($a->votes > $b->votes)) ? -1 : 1;
        }
    
        public static function compare_answers_ascending($a, $b) {
            if ($a->answercount == $b->answercount) {
                return 0;
            }
            return ($a->answercount < $b->answercount) ? -1 : 1;
        }
    
        public static function compare_answers_descending($a, $b) {
            if ($a->answercount == $b->answercount) {
                return 0;
            }
            return ($a->answercount > $b->answercount) ? -1 : 1;
        }
    
        public static function compare_time_ascending($a, $b) {
            if ($a->timemodified == $b->timemodified) {
                return 0;
            }
            return ($a->timemodified < $b->timemodified) ? -1 : 1;
        }
    
        public static function compare_time_descending($a, $b) {
            if ($a->timemodified == $b->timemodified) {
                return 0;
            }
            return ($a->timemodified > $b->timemodified) ? -1 : 1;
        }
    
        public static function compare_lastanswertime_ascending($a, $b) {
            if ($a->lastanswered == $b->lastanswered) {
                return 0;
            }
            return ($a->lastanswered < $b->lastanswered) ? -1 : 1;
        }
    
        public static function compare_lastanswertime_descending($a, $b) {
            if ($a->lastanswered == $b->lastanswered) {
                return 0;
            }
            return ($a->lastanswered > $b->lastanswered) ? -1 : 1;
        }
    
        public static function compare_commenttime_ascending($a, $b) {
            if ($a->commenttime == $b->commenttime) {
                return 0;
            }
            return ($a->commenttime < $b->commenttime) ? -1 : 1;
        }
    
        public static function compare_commenttime_descending($a, $b) {
            if ($a->commenttime == $b->commenttime) {
                return 0;
            }
            return ($a->commenttime > $b->commenttime) ? -1 : 1;
        }
    
        public static function compare_creationtime_ascending($a, $b) {
            if ($a->timecreated == $b->timecreated) {
                return 0;
            }
            return ($a->timecreated < $b->timecreated) ? -1 : 1;
        }
    
        public static function compare_creationtime_descending($a, $b) {
            if ($a->timecreated == $b->timecreated) {
                return 0;
            }
            return ($a->timecreated > $b->timecreated) ? -1 : 1;
        }
    
        public static function compare_alphabetically_ascending($a, $b) {
            if ($a->pdfannotatorname == $b->pdfannotatorname) {
                return 0;
            }
            if (strcasecmp($a->pdfannotatorname, $b->pdfannotatorname) < 0) {
                return -1;
            } else {
                return 1;
            }
        }
    
        public static function compare_alphabetically_descending($a, $b) {
            if ($a->pdfannotatorname == $b->pdfannotatorname) {
                return 0;
            }
            if (strcasecmp($a->pdfannotatorname, $b->pdfannotatorname) > 0) {
                return -1;
            } else {
                return 1;
            }
        }
    
        public static function compare_question_ascending($a, $b) {
            if ($a->answeredquestion == $b->answeredquestion) {
                return 0;
            }
            if (strcasecmp($a->answeredquestion, $b->answeredquestion) < 0) {
                return -1;
            } else {
                return 1;
            }
        }
    
        public static function compare_question_descending($a, $b) {
            if ($a->answeredquestion == $b->answeredquestion) {
                return 0;
            }
            if (strcasecmp($a->answeredquestion, $b->answeredquestion) > 0) {
                return -1;
            } else {
                return 1;
            }
        }
    
    }
    
    /**
     * Function sorts entries in a table according to time, number of votes or annotator.
     * Function is applicable to 'unsolved questions' and 'my posts' category on overview page.
     *
     * @param array $questions
     * @param string $sortcriterium The column according to which the table should be sorted
     * @param int $sortorder 3 for descending, 4 for ascending
     */
    function pdfannotator_sort_entries($questions, $sortcriterium, $sortorder) {
        switch ($sortcriterium) {
            case 'col1':
                if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_time_ascending');
                } else if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_time_descending');
                }
                break;
            case 'col2':
                if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_votes_ascending');
                } else if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_votes_descending');
                }
                break;
            case 'col3':
                if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_alphabetically_ascending');
                } else if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_alphabetically_descending');
                }
                break;
            default:
        }
        return $questions;
    }
    
    function pdfannotator_sort_questions($questions, $sortcriterium, $sortorder) {
        switch ($sortcriterium) {
            case 'col1':
                if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_creationtime_ascending');
                } else if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_creationtime_descending');
                }
                break;
            case 'col2':
                if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_votes_ascending');
                } else if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_votes_descending');
                }
                break;
            case 'col3':
                if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_answers_ascending');
                } else if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_answers_descending');
                }
                break;
            case 'col4':
                if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_lastanswertime_ascending');
                } else if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_lastanswertime_descending');
                }
                break;
            case 'col5':
                if ($sortorder === 4) {
                    usort($questions, 'pdfannotator_compare::compare_alphabetically_ascending');
                } else if ($sortorder === 3) {
                    usort($questions, 'pdfannotator_compare::compare_alphabetically_descending');
                }
                break;
            default:
        }
        return $questions;
    }
    
    /**
     * Function sorts entries in a table according to annotator or time.
     * Applicable for overview answers category.
     *
     * XXX Maybe rename 'colx' into something like 'time', so as to avoid code redundancy.
     *
     * @param array $answers
     * @param int $sortcriterium
     * @param int $sortorder
     * @return array $answers
     */
    function pdfannotator_sort_answers($answers, $sortcriterium, $sortorder) {
        switch ($sortcriterium) {
            case 'col4':
                if ($sortorder === 4) {
                    usort($answers, 'pdfannotator_compare::compare_alphabetically_ascending');
                } else if ($sortorder === 3) {
                    usort($answers, 'pdfannotator_compare::compare_alphabetically_descending');
                }
                break;
            case 'col2':
                if ($sortorder === 4) {
                    usort($answers, 'pdfannotator_compare::compare_time_ascending');
                } else if ($sortorder === 3) {
                    usort($answers, 'pdfannotator_compare::compare_time_descending');
                }
                break;
            case 'col3':
                if ($sortorder === 4) {
                    usort($answers, 'pdfannotator_compare::compare_question_ascending');
                } else if ($sortorder === 3) {
                    usort($answers, 'pdfannotator_compare::compare_question_descending');
                }
                break;
            default:
        }
        return $answers;
    }
    
    /**
     *
     * @param array $reports
     * @param string $sortcriterium
     * @param int $sortorder
     * @return array $reports (sorted)
     */
    function pdfannotator_sort_reports($reports, $sortcriterium, $sortorder) {
        switch ($sortcriterium) {
            case 'col1':
                if ($sortorder === 4) {
                    usort($reports, 'pdfannotator_compare::compare_creationtime_ascending');
                } else if ($sortorder === 3) {
                    usort($reports, 'pdfannotator_compare::compare_creationtime_descending');
                }
                break;
            case 'col3':
                if ($sortorder === 4) {
                    usort($reports, 'pdfannotator_compare::compare_commenttime_ascending');
                } else if ($sortorder === 3) {
                    usort($reports, 'pdfannotator_compare::compare_commenttime_descending');
                }
                break;
            default:
        }
        return $reports;
    }
    
    /**
     * Function takes an array and returns its first key.
     *
     * @param array $array
     * @return mixed
     */
    function pdfannotator_get_first_key_in_array($array) {
    
        if (!function_exists('array_key_first')) { // Function exists in PHP version 7.3 and later.
            /**
             * Gets the first key of an array
             *
             * @param array $array
             * @return mixed
             */
    
            function array_key_first(array $array) {
                if (count($array)) {
                    reset($array);
                    return key($array);
                }
                return null;
            }
    
        }
        return array_key_first($array);
    }
    
    /**
     * This function renders the table of unsolved questions on the overview page.
     *
     * @param array $questions
     * @param int $thiscourse
     * @param Moodle url object $url
     * @param int $currentpage
     */
    function pdfannotator_print_questions($questions, $thiscourse, $urlparams, $currentpage, $itemsperpage, $context) {
    
        global $CFG, $OUTPUT;
        require_once("$CFG->dirroot/mod/pdfannotator/model/overviewtable.php");
    
        $showdropdown = has_capability('mod/pdfannotator:forwardquestions', $context);
        $questioncount = count($questions);
        $usepagination = !($itemsperpage == -1 || $itemsperpage >= $questioncount);
        $offset = $currentpage * $itemsperpage;
    
        if ($usepagination == 1 && ($offset >= $questioncount)) {
            $offset = 0;
            $urlparams['page'] = 0;
        }
        $url = new moodle_url($CFG->wwwroot . '/mod/pdfannotator/view.php', $urlparams);
    
        // Define flexible table.
        $table = new questionstable($url, $showdropdown);
        $table->setup();
        // $table->pageable(false);
        // Sort the entries of the table according to time or number of votes.
        if (!empty($sortinfo = $table->get_sort_columns())) {
            $sortcriterium = pdfannotator_get_first_key_in_array($sortinfo); // Returns the name (e.g. col2) of the column which was clicked for sorting.
            $sortorder = $sortinfo[$sortcriterium]; // 3 for descending, 4 for ascending.
            $questions = pdfannotator_sort_questions($questions, $sortcriterium, $sortorder);
        }
    
        // Add data to the table and print the requested table (page).
        if (pdfannotator_is_phone() || $itemsperpage == -1 || $itemsperpage >= $questioncount) { // No pagination.
            foreach ($questions as $question) {
                pdfannotator_questionstable_add_row($thiscourse, $table, $question, $urlparams, $showdropdown);
            }
        } else {
            $table->pagesize($itemsperpage, $questioncount);
            for ($i = $offset; $i < $questioncount; $i++) {
                $question = $questions[$i];
                if ($itemsperpage === 0) {
                    break;
                }
                pdfannotator_questionstable_add_row($thiscourse, $table, $question, $urlparams, $showdropdown);
                $itemsperpage--;
            }
        }
        $table->finish_html();
    }
    
    /**
     * Function prints a table view of all answers to questions the current
     * user asked or subscribed to.
     *
     * @param int $annotator
     * @param Moodle url object $url
     * @param int $thiscourse
     */
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
    function pdfannotator_print_answers($data, $thiscourse, $url, $currentpage, $itemsperpage, $cmid, $answerfilter, $context) {
    
    
        global $CFG, $OUTPUT;
        require_once("$CFG->dirroot/mod/pdfannotator/model/overviewtable.php");
    
        $table = new answerstable($url);
        $table->setup();
    
        // Sort the entries of the table according to time or number of votes.
        if (!empty($sortinfo = $table->get_sort_columns())) {
            $sortcriterium = pdfannotator_get_first_key_in_array($sortinfo); // Returns the name (e.g. col2) of the column which was clicked for sorting.
            $sortorder = $sortinfo[$sortcriterium]; // 3 for descending, 4 for ascending.
            $data = pdfannotator_sort_answers($data, $sortcriterium, $sortorder);
        }
    
        // Add data to the table and print the requested table page.
        if ($itemsperpage == -1) { // No pagination.
            foreach ($data as $answer) {
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
                pdfannotator_answerstable_add_row($thiscourse, $table, $answer, $cmid, $currentpage, $itemsperpage, $answerfilter, $context);
    
            }
        } else {
            $answercount = count($data);
            $table->pagesize($itemsperpage, $answercount);
            $offset = $currentpage * $itemsperpage;
            $rowstoprint = $itemsperpage;
            for ($i = $offset; $i < $answercount; $i++) {
                $answer = $data[$i];
                if ($rowstoprint === 0) {
                    break;
                }
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
                pdfannotator_answerstable_add_row($thiscourse, $table, $answer, $cmid, $currentpage, $itemsperpage, $answerfilter, $context);
    
                $rowstoprint--;
            }
        }
        $table->finish_html();
    }
    
    /**
     *
     * @param type $posts
     * @param type $url
     * @param type $thiscourse
     */
    function pdfannotator_print_this_users_posts($posts, $thiscourse, $url, $currentpage, $itemsperpage) {
    
        global $CFG;
        require_once("$CFG->dirroot/mod/pdfannotator/model/overviewtable.php");
    
        $table = new userspoststable($url);
        $table->setup();
    
        // Sort the entries of the table according to time or number of votes.
        if (!empty($sortinfo = $table->get_sort_columns())) {
            $sortcriterium = pdfannotator_get_first_key_in_array($sortinfo); // Returns the name (e.g. col2) of the column which was clicked for sorting.
            $sortorder = $sortinfo[$sortcriterium]; // 3 for descending, 4 for ascending.
            $posts = pdfannotator_sort_entries($posts, $sortcriterium, $sortorder);
        }
    
        // Add data to the table and print the requested table page.
        if ($itemsperpage == -1) {
            foreach ($posts as $post) {
                pdfannotator_userspoststable_add_row($table, $post);
            }
        } else {
            $postcount = count($posts);
            $table->pagesize($itemsperpage, $postcount);
            $offset = $currentpage * $itemsperpage;
            for ($i = $offset; $i < $postcount; $i++) {
                $post = $posts[$i];
                if ($itemsperpage === 0) {
                    break;
                }
                pdfannotator_userspoststable_add_row($table, $post);
                $itemsperpage--;
            }
        }
        $table->finish_html();
    }
    
    /**
     * Function prints a table view of all comments that were reported as inappropriate.
     *
     * @param array of objects $reports
     * @param int $thiscourse
     * @param Moodle url object $url
     * @param int $currentpage
     */
    
    function pdfannotator_print_reports($reports, $thiscourse, $url, $currentpage, $itemsperpage, $cmid, $reportfilter, $context) {
    
    
        global $CFG, $OUTPUT;
        require_once("$CFG->dirroot/mod/pdfannotator/model/overviewtable.php");
    
        $table = new reportstable($url);
        $table->setup();
        // Sort the entries of the table according to time or number of votes.
        if (!empty($sortinfo = $table->get_sort_columns())) {
            $sortcriterium = pdfannotator_get_first_key_in_array($sortinfo); // Returns the name (e.g. col2) of the column which was clicked for sorting.
            $sortorder = $sortinfo[$sortcriterium]; // 3 for descending, 4 for ascending.
            $reports = pdfannotator_sort_reports($reports, $sortcriterium, $sortorder);
        }
        // Add data to the table and print the requested table page.
        if ($itemsperpage == -1) {
            foreach ($reports as $report) {
    
                pdfannotator_reportstable_add_row($thiscourse, $table, $report, $cmid, $itemsperpage, $reportfilter, $currentpage, $context);
    
            }
        } else {
            $reportcount = count($reports);
            $table->pagesize($itemsperpage, $reportcount);
            $offset = $currentpage * $itemsperpage;
            $rowstoprint = $itemsperpage;
            for ($i = $offset; $i < $reportcount; $i++) {
                $report = $reports[$i];
                if ($rowstoprint === 0) {
                    break;
                }
    
                pdfannotator_reportstable_add_row($thiscourse, $table, $report, $cmid, $itemsperpage, $reportfilter, $currentpage, $context);
    
                $rowstoprint--;
            }
        }
        $table->finish_html();
    }
    
    /**
     * This function adds a row of data to the overview table that displays all
     * unsolved questions in the course.
     *
     * @param int $thiscourse
     * @param questionstable $table
     * @param object $question
     */
    function pdfannotator_questionstable_add_row($thiscourse, $table, $question, $urlparams, $showdropdown) {
    
        global $CFG, $PAGE;
        if ($question->visibility == 'anonymous') {
            $author = get_string('anonymous', 'pdfannotator');
        } else {
            $author = "<a href=" . $CFG->wwwroot . "/user/view.php?id=$question->userid&course=$thiscourse>" . pdfannotator_get_username($question->userid) . "</a>";
        }
        $time = pdfannotator_get_user_datetime_shortformat($question->timecreated);
        if (!empty($question->lastanswered)) { // ! ($question->lastanswered != $question->timecreated) {
            if ($question->lastuservisibility == 'anonymous') {
                $lastresponder = get_string('anonymous', 'pdfannotator');
            } else {
                $lastresponder = "<a href=" . $CFG->wwwroot . "/user/view.php?id=$question->lastuser&course=$thiscourse>" . pdfannotator_get_username($question->lastuser) . "</a>";
            }
            $answertime = pdfannotator_timeago($question->lastanswered);
            $lastanswered = $lastresponder . "<br>" . $answertime;
        } else {
            $lastanswered = '-';
        }
        $classname = '';
        if (isset($question->displayhidden)) {
            $classname = 'dimmed_text';
        }
        $content = "<a href=$question->link class='more'>$question->content</a>";
        $pdfannotatorname = $question->pdfannotatorname;
    
        $data = array($content, $author . '<br>' . $time, $question->votes, $question->answercount, $lastanswered, $pdfannotatorname);
    
        if ($showdropdown) {
            $myrenderer = $PAGE->get_renderer('mod_pdfannotator');
            $dropdown = $myrenderer->render_dropdownmenu(new questionmenu($question->commentid, $urlparams));
            $data[] = $dropdown;
        }
        $table->add_data($data, $classname);
    }
    
    /**
     * This function adds a row of data to the overview table that displays
     * answers to any question the user subscribed to.
     *
     * @param int $thiscourse
     * @param answerstable $table
     * @param object $answer
     */
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
    function pdfannotator_answerstable_add_row($thiscourse, $table, $answer, $cmid, $currentpage, $itemsperpage, $answerfilter, $context) {
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
        $answer->answer = pdfannotator_get_relativelink($answer->answer, $answer->answerid, $context);
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
        $answer->answer = format_text($answer->answer, $options = ['filter' => true]);
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
        $answer->answeredquestion = pdfannotator_get_relativelink($answer->answeredquestion, $answer->questionid, $context);
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
        $answer->answeredquestion = format_text($answer->answeredquestion, $options = ['filter' => true]);
    
    
    anisa kusumadewi's avatar
    anisa kusumadewi committed
    
    
        if (isset($answer->displayquestionhidden)) {
            $question = "<a class='" . $answer->annoid . " more dimmed' href=$answer->questionlink>$answer->answeredquestion</a>";
        } else {
            $question = "<a class='" . $answer->annoid . " more' href=$answer->questionlink>$answer->answeredquestion</a>";
        }
        $pdfannotatorname = $answer->pdfannotatorname;
        if ($answer->correct) {
            $checked = "<i class='icon fa fa-check fa-fw' style='color:green;'></i>";
        } else {
            $checked = "";
        }
        $answerid = 'answer_' . $answer->answerid;
        $answerlink = "<a id=$answerid data-question=$answer->questionid href=$answer->link class='more'>$answer->answer</a>";
    
        if ($answer->visibility == 'anonymous') {
            $answeredby = get_string('anonymous', 'pdfannotator');
        } else {
            $answeredby = "<a href=" . $CFG->wwwroot . "/user/view.php?id=$answer->userid&course=$thiscourse>" . pdfannotator_get_username($answer->userid) . "</a>";
        }
        $answertime = pdfannotator_get_user_datetime_shortformat($answer->timemodified);
    
        if (empty($answer->issubscribed)) {
            $issubscribed = null;
        } else {
            $issubscribed = $answer->issubscribed;
        }
    
        $classname = '';
        if (isset($answer->displayhidden)) {
            $classname = 'dimmed_text';
        }
    
        $myrenderer = $PAGE->get_renderer('mod_pdfannotator');
        $dropdown = $myrenderer->render_dropdownmenu(new answermenu($answer->annoid, $issubscribed, $cmid, $currentpage, $itemsperpage, $answerfilter));
    
        $table->add_data(array($answerlink, $checked, $answeredby . '<br>' . $answertime, $question, $pdfannotatorname, $dropdown), $classname);
    }
    
    /**
     * This function adds a row of data to the overview table that displays all
     * comments the current user posted in this course.
     *
     * @param userspoststable $table
     * @param object $post
     */
    function pdfannotator_userspoststable_add_row($table, $post) {
        $time = pdfannotator_get_user_datetime_shortformat($post->timemodified);
        $content = "<a href=$post->link class='more'>$post->content</a>";
    
        $classname = '';
        if (isset($post->displayhidden)) {
            $classname = 'dimmed_text';
        }
        $pdfannotatorname = $post->pdfannotatorname;
        $table->add_data(array($content, $time, $post->votes, $pdfannotatorname), $classname);
    }
    
    /**
     * This function adds a row of data to the overview table that displays all
     * comments reported in this course.
     *
     * @param int $thiscourse
     * @param reportstable $table
     * @param object $report
     * @param int $cmid
     * @param int $itemsperpage
     * @param int $reportfilter
     * @param int $currentpage
     */
    
    function pdfannotator_reportstable_add_row($thiscourse, $table, $report, $cmid, $itemsperpage, $reportfilter, $currentpage, $context) {
        global $CFG, $PAGE, $DB;
        
        $questionid = $DB->get_record('pdfannotator_comments', ['annotationid' => $report->annotationid, 'isquestion' => 1], 'id');
        $report->report = pdfannotator_get_relativelink($report->report, $questionid, $context);
        $report->reportedcomment = pdfannotator_get_relativelink($report->reportedcomment, $report->commentid, $context);
    
    
        // Prepare report data for display.
        $reportid = 'report_' . $report->reportid;
        $reportedcommmentlink = "<a id=$reportid href=$report->link class='more'>$report->reportedcomment</a>";
        $writtenby = "<a href=" . $CFG->wwwroot . "/user/view.php?id=$report->commentauthor&course=$thiscourse>" . pdfannotator_get_username($report->commentauthor) . "</a>";
        $commenttime = pdfannotator_get_user_datetime_shortformat($report->commenttime);
        $reportedby = "<a href=" . $CFG->wwwroot . "/user/view.php?id=$report->reportinguser&course=$thiscourse>" . pdfannotator_get_username($report->reportinguser) . "</a>";
        $reporttime = pdfannotator_get_user_datetime_shortformat($report->timecreated);
        $report->report = "<div class='more'>$report->report</div>";
    
        $classname = '';
        if (!($report->cmvisible)) {
            $classname = 'dimmed_text';
        }
    
        // Create action dropdown menu.
        $myrenderer = $PAGE->get_renderer('mod_pdfannotator');
        $dropdown = $myrenderer->render_dropdownmenu(new reportmenu($report, $cmid, $currentpage, $itemsperpage, $reportfilter));
    
        // Add a new row to the reports table.
        $table->add_data(array($report->report, $reportedby . '<br>' . $reporttime, $reportedcommmentlink, $writtenby . '<br>' . $commenttime, $dropdown), $classname);
    }
    
    
    /**
     * Function takes a moodle timestamp, calculates how much time has since elapsed
     * and returns this information as a string (e.g.: '3 days ago').
     *
     * @param int $timestamp
     * @return string
     */