1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17namespace tool_brickfield\local\areas\mod_lesson;
18
19use tool_brickfield\area_base;
20
21/**
22 * Lesson page base.
23 *
24 * @package    tool_brickfield
25 * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
26 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28abstract class page_base extends area_base {
29
30    /**
31     * Get table name reference.
32     * @return string
33     */
34    public function get_ref_tablename(): string {
35        return 'lesson';
36    }
37
38    /**
39     * Find recordset of the relevant areas.
40     * @param \core\event\base $event
41     * @return \moodle_recordset|null
42     * @throws \coding_exception
43     * @throws \dml_exception
44     */
45    public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
46        if ($event->eventname == '\mod_lesson\event\page_created' || $event->eventname == '\mod_lesson\event\page_updated') {
47            if ($event->component === 'mod_lesson') {
48                return $this->find_areas(['itemid' => $event->objectid]);
49            }
50        }
51        return null;
52    }
53
54    /**
55     * Find recordset of the course areas.
56     * @param int $courseid
57     * @return \moodle_recordset
58     * @throws \coding_exception
59     * @throws \dml_exception
60     */
61    public function find_course_areas(int $courseid): ?\moodle_recordset {
62        return $this->find_areas(['courseid' => $courseid]);
63    }
64
65    /**
66     * Find recordset of areas.
67     * @param array $params
68     * @return \moodle_recordset
69     * @throws \coding_exception
70     * @throws \dml_exception
71     */
72    protected function find_areas(array $params = []): \moodle_recordset {
73        global $DB;
74
75        $where = [];
76        if (!empty($params['itemid'])) {
77            $where[] = 'co.id = :itemid';
78        }
79        if (!empty($params['courseid'])) {
80            $where[] = 'cm.course = :courseid';
81        }
82
83        // Filter against approved / non-approved course category listings.
84        $this->filterfieldname = 'cm.course';
85        $this->get_courseid_filtering();
86        if ($this->filter != '') {
87            $params = $params + $this->filterparams;
88        }
89
90        $rs = $DB->get_recordset_sql('SELECT
91          ' . $this->get_type() . ' AS type,
92          ctx.id AS contextid,
93          ' . $this->get_standard_area_fields_sql() . '
94          co.id AS itemid,
95          ' . $this->get_reftable_field_sql() . '
96          t.id AS refid,
97          cm.id AS cmid,
98          cm.course AS courseid,
99          co.'.$this->get_fieldname().' AS content
100        FROM {lesson} t
101        JOIN {course_modules} cm ON cm.instance = t.id
102        JOIN {modules} m ON m.id = cm.module AND m.name = :preftablename2
103        JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :pctxlevelmodule
104        JOIN {'.$this->get_tablename().'} co ON co.lessonid = t.id ' .
105        (!empty($where) ? 'WHERE ' . join(' AND ', $where) : '') . $this->filter . '
106        ORDER BY t.id, co.id',
107            ['pctxlevelmodule' => CONTEXT_MODULE,
108                'preftablename2' => $this->get_ref_tablename(),
109            ] + $params);
110
111        return $rs;
112    }
113}
114