Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 68... Línea 68...
68
     * @param string|null $wheresql Where conditions for the SQL
68
     * @param string|null $wheresql Where conditions for the SQL
69
     * @param string|null $sortsql Order by conditions for the SQL
69
     * @param string|null $sortsql Order by conditions for the SQL
70
     * @param int|null $userid The user ID
70
     * @param int|null $userid The user ID
71
     * @return string
71
     * @return string
72
     */
72
     */
73
    protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null): string {
73
    protected function generate_get_records_sql(?string $wheresql = null, ?string $sortsql = null, ?int $userid = null): string {
74
        $table = self::TABLE;
74
        $table = self::TABLE;
75
        $alias = $this->get_table_alias();
75
        $alias = $this->get_table_alias();
76
        $fields = $alias . '.*';
76
        $fields = $alias . '.*';
77
        $tables = "{{$table}} {$alias}";
77
        $tables = "{{$table}} {$alias}";
Línea 109... Línea 109...
109
     */
109
     */
110
    public function get_from_discussion_id(
110
    public function get_from_discussion_id(
111
        stdClass $user,
111
        stdClass $user,
112
        int $discussionid,
112
        int $discussionid,
113
        bool $canseeprivatereplies,
113
        bool $canseeprivatereplies,
114
        string $orderby = 'created ASC'
114
        string $orderby = 'created ASC, id ASC'
115
    ): array {
115
    ): array {
116
        return $this->get_from_discussion_ids($user, [$discussionid], $canseeprivatereplies, $orderby);
116
        return $this->get_from_discussion_ids($user, [$discussionid], $canseeprivatereplies, $orderby);
117
    }
117
    }
Línea 118... Línea 118...
118
 
118
 
Línea 230... Línea 230...
230
     */
230
     */
231
    public function get_replies_to_post(
231
    public function get_replies_to_post(
232
        stdClass $user,
232
        stdClass $user,
233
        post_entity $post,
233
        post_entity $post,
234
        bool $canseeprivatereplies,
234
        bool $canseeprivatereplies,
235
        string $orderby = 'created ASC'
235
        string $orderby = 'created ASC, id ASC'
236
    ): array {
236
    ): array {
237
        $alias = $this->get_table_alias();
237
        $alias = $this->get_table_alias();
Línea 238... Línea 238...
238
 
238
 
239
        [
239
        [
Línea 435... Línea 435...
435
 
435
 
436
        [
436
        [
437
            'where' => $privatewhere,
437
            'where' => $privatewhere,
438
            'params' => $privateparams,
438
            'params' => $privateparams,
-
 
439
        ] = $this->get_private_reply_sql($user, $canseeprivatereplies, "mp");
-
 
440
        [
-
 
441
            'where' => $privatewhere2,
-
 
442
            'params' => $privateparams2,
Línea 439... Línea 443...
439
        ] = $this->get_private_reply_sql($user, $canseeprivatereplies, "mp");
443
        ] = $this->get_private_reply_sql($user, $canseeprivatereplies, "p");
440
 
444
 
441
        $sql = "
445
        $sql = "
442
            SELECT posts.*
446
            SELECT posts.*
443
            FROM {" . self::TABLE . "} posts
447
            FROM {" . self::TABLE . "} posts
444
            JOIN (
448
            JOIN (
445
                SELECT p.discussion, MAX(p.id) as latestpostid
449
                SELECT p.discussion, MAX(p.id) as latestpostid
446
                FROM {" . self::TABLE . "} p
450
                FROM {" . self::TABLE . "} p
447
                JOIN (
451
                JOIN (
448
                    SELECT mp.discussion, MAX(mp.created) AS created
452
                    SELECT mp.discussion, MAX(mp.created) AS created
449
                      FROM {" . self::TABLE . "} mp
453
                    FROM {" . self::TABLE . "} mp
450
                     WHERE mp.discussion {$insql} {$privatewhere}
454
                    WHERE mp.discussion {$insql} {$privatewhere}
-
 
455
                    GROUP BY mp.discussion
451
                  GROUP BY mp.discussion
456
                ) lp ON lp.discussion = p.discussion AND lp.created = p.created
452
                ) lp ON lp.discussion = p.discussion AND lp.created = p.created
457
                WHERE 1=1 {$privatewhere2}
Línea 453... Línea 458...
453
            GROUP BY p.discussion
458
                GROUP BY p.discussion
454
          ) plp on plp.discussion = posts.discussion AND plp.latestpostid = posts.id";
459
            ) plp on plp.discussion = posts.discussion AND plp.latestpostid = posts.id";
Línea 455... Línea 460...
455
 
460
 
456
        $records = $this->get_db()->get_records_sql($sql, array_merge($params, $privateparams));
461
        $records = $this->get_db()->get_records_sql($sql, array_merge($params, $privateparams, $privateparams2));
457
        $entities = $this->transform_db_records_to_entities($records);
462
        $entities = $this->transform_db_records_to_entities($records);
Línea 471... Línea 476...
471
     */
476
     */
472
    private function get_private_reply_sql(stdClass $user, bool $canseeprivatereplies, $posttablealias = "p") {
477
    private function get_private_reply_sql(stdClass $user, bool $canseeprivatereplies, $posttablealias = "p") {
473
        $params = [];
478
        $params = [];
474
        $privatewhere = '';
479
        $privatewhere = '';
475
        if (!$canseeprivatereplies) {
480
        if (!$canseeprivatereplies) {
476
            $privatewhere = " AND ({$posttablealias}.privatereplyto = :privatereplyto OR " .
481
            $privatewhere = " AND ({$posttablealias}.privatereplyto = :{$posttablealias}_privatereplyto OR " .
477
                "{$posttablealias}.userid = :privatereplyfrom OR {$posttablealias}.privatereplyto = 0)";
482
                "{$posttablealias}.userid = :{$posttablealias}_privatereplyfrom OR {$posttablealias}.privatereplyto = 0)";
478
            $params['privatereplyto'] = $user->id;
483
            $params[$posttablealias . '_privatereplyto'] = $user->id;
479
            $params['privatereplyfrom'] = $user->id;
484
            $params[$posttablealias . '_privatereplyfrom'] = $user->id;
480
        }
485
        }
Línea 481... Línea 486...
481
 
486
 
482
        return [
487
        return [
483
            'where' => $privatewhere,
488
            'where' => $privatewhere,
Línea 498... Línea 503...
498
        }
503
        }
Línea 499... Línea 504...
499
 
504
 
Línea 500... Línea 505...
500
        list($insql, $params) = $this->get_db()->get_in_or_equal($discussionids, SQL_PARAMS_NAMED);
505
        list($insql, $params) = $this->get_db()->get_in_or_equal($discussionids, SQL_PARAMS_NAMED);
501
 
506
 
-
 
507
        $sql = "
-
 
508
            SELECT posts.*
-
 
509
            FROM {" . self::TABLE . "} posts
502
        $sql = "
510
            JOIN (
503
            SELECT p.*
511
                SELECT p.discussion, MIN(p.id) as firstpostid
504
              FROM {" . self::TABLE . "} p
512
                FROM {" . self::TABLE . "} p
505
              JOIN (
513
                JOIN (
506
                SELECT mp.discussion, MIN(mp.created) AS created
514
                    SELECT mp.discussion, MIN(mp.created) AS created
507
                  FROM {" . self::TABLE . "} mp
515
                    FROM {" . self::TABLE . "} mp
508
                 WHERE mp.discussion {$insql}
516
                    WHERE mp.discussion {$insql}
-
 
517
                    GROUP BY mp.discussion
-
 
518
                ) lp ON lp.discussion = p.discussion AND lp.created = p.created
Línea 509... Línea 519...
509
              GROUP BY mp.discussion
519
                GROUP BY p.discussion
510
              ) lp ON lp.discussion = p.discussion AND lp.created = p.created";
520
            ) plp on plp.discussion = posts.discussion AND plp.firstpostid = posts.id";
511
 
521
 
Línea 524... Línea 534...
524
     */
534
     */
525
    public function get_posts_in_discussion_for_user_id(
535
    public function get_posts_in_discussion_for_user_id(
526
        int $discussionid,
536
        int $discussionid,
527
        int $userid,
537
        int $userid,
528
        bool $canseeprivatereplies,
538
        bool $canseeprivatereplies,
529
        string $orderby = 'created ASC'
539
        string $orderby = 'created ASC, id ASC'
530
    ): array {
540
    ): array {
531
        $user = $this->get_db()->get_record('user', ['id' => (int)$userid], '*', IGNORE_MISSING);
541
        $user = $this->get_db()->get_record('user', ['id' => (int)$userid], '*', IGNORE_MISSING);
Línea 532... Línea 542...
532
 
542
 
533
        $alias = $this->get_table_alias();
543
        $alias = $this->get_table_alias();