| 1 | 
           efrain | 
           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 | 
              | 
        
        
            | 
            | 
           17 | 
           namespace tool_brickfield\local\areas;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use core\event\course_module_created;
  | 
        
        
            | 
            | 
           20 | 
           use core\event\course_module_updated;
  | 
        
        
            | 
            | 
           21 | 
           use tool_brickfield\area_base;
  | 
        
        
            | 
            | 
           22 | 
              | 
        
        
            | 
            | 
           23 | 
           /**
  | 
        
        
            | 
            | 
           24 | 
            * Base class for all areas that represent a field from the module table (such as 'intro' or 'name')
  | 
        
        
            | 
            | 
           25 | 
            *
  | 
        
        
            | 
            | 
           26 | 
            * @package    tool_brickfield
  | 
        
        
            | 
            | 
           27 | 
            * @copyright  2020 Brickfield Education Labs https://www.brickfield.ie
  | 
        
        
            | 
            | 
           28 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           29 | 
            */
  | 
        
        
            | 
            | 
           30 | 
           abstract class module_area_base extends area_base {
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               /**
  | 
        
        
            | 
            | 
           33 | 
                * Find recordset of the relevant areas.
  | 
        
        
            | 
            | 
           34 | 
                * @param \core\event\base $event
  | 
        
        
            | 
            | 
           35 | 
                * @return \moodle_recordset|null
  | 
        
        
            | 
            | 
           36 | 
                * @throws \coding_exception
  | 
        
        
            | 
            | 
           37 | 
                * @throws \dml_exception
  | 
        
        
            | 
            | 
           38 | 
                */
  | 
        
        
            | 
            | 
           39 | 
               public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  | 
        
        
            | 
            | 
           40 | 
                   if ($event instanceof course_module_updated || $event instanceof course_module_created) {
  | 
        
        
            | 
            | 
           41 | 
                       if ($event->other['modulename'] === $this->get_tablename()) {
  | 
        
        
            | 
            | 
           42 | 
                           return $this->find_fields_in_module_table(['itemid' => $event->other['instanceid']]);
  | 
        
        
            | 
            | 
           43 | 
                       }
  | 
        
        
            | 
            | 
           44 | 
                   }
  | 
        
        
            | 
            | 
           45 | 
                   return null;
  | 
        
        
            | 
            | 
           46 | 
               }
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /**
  | 
        
        
            | 
            | 
           49 | 
                * Find recordset of the course areas.
  | 
        
        
            | 
            | 
           50 | 
                * @param int $courseid
  | 
        
        
            | 
            | 
           51 | 
                * @return \moodle_recordset
  | 
        
        
            | 
            | 
           52 | 
                * @throws \coding_exception
  | 
        
        
            | 
            | 
           53 | 
                * @throws \dml_exception
  | 
        
        
            | 
            | 
           54 | 
                */
  | 
        
        
            | 
            | 
           55 | 
               public function find_course_areas(int $courseid): ?\moodle_recordset {
  | 
        
        
            | 
            | 
           56 | 
                   return $this->find_fields_in_module_table(['courseid' => $courseid]);
  | 
        
        
            | 
            | 
           57 | 
               }
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
               /**
  | 
        
        
            | 
            | 
           60 | 
                * Helper method that can be used by the classes that define a field in the respective module table
  | 
        
        
            | 
            | 
           61 | 
                *
  | 
        
        
            | 
            | 
           62 | 
                * @param array $params
  | 
        
        
            | 
            | 
           63 | 
                * @return \moodle_recordset
  | 
        
        
            | 
            | 
           64 | 
                * @throws \coding_exception
  | 
        
        
            | 
            | 
           65 | 
                * @throws \dml_exception
  | 
        
        
            | 
            | 
           66 | 
                */
  | 
        
        
            | 
            | 
           67 | 
               protected function find_fields_in_module_table(array $params = []): \moodle_recordset {
  | 
        
        
            | 
            | 
           68 | 
                   global $DB;
  | 
        
        
            | 
            | 
           69 | 
                   $where = [];
  | 
        
        
            | 
            | 
           70 | 
                   if (!empty($params['itemid'])) {
  | 
        
        
            | 
            | 
           71 | 
                       $where[] = 't.id = :itemid';
  | 
        
        
            | 
            | 
           72 | 
                   }
  | 
        
        
            | 
            | 
           73 | 
                   if (!empty($params['courseid'])) {
  | 
        
        
            | 
            | 
           74 | 
                       $where[] = 'cm.course = :courseid';
  | 
        
        
            | 
            | 
           75 | 
                   }
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   // Filter against approved / non-approved course category listings.
  | 
        
        
            | 
            | 
           78 | 
                   $this->filterfieldname = 'cm.course';
  | 
        
        
            | 
            | 
           79 | 
                   $this->get_courseid_filtering();
  | 
        
        
            | 
            | 
           80 | 
                   if ($this->filter != '') {
  | 
        
        
            | 
            | 
           81 | 
                       $params = $params + $this->filterparams;
  | 
        
        
            | 
            | 
           82 | 
                   }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   $rs = $DB->get_recordset_sql('SELECT
  | 
        
        
            | 
            | 
           85 | 
                     ' . $this->get_type() . ' AS type,
  | 
        
        
            | 
            | 
           86 | 
                     ctx.id AS contextid,
  | 
        
        
            | 
            | 
           87 | 
                     ' . $this->get_standard_area_fields_sql() . '
  | 
        
        
            | 
            | 
           88 | 
                     t.id AS itemid,
  | 
        
        
            | 
            | 
           89 | 
                     cm.id AS cmid,
  | 
        
        
            | 
            | 
           90 | 
                     cm.course AS courseid,
  | 
        
        
            | 
            | 
           91 | 
                     t.'.$this->get_fieldname().' AS content
  | 
        
        
            | 
            | 
           92 | 
                   FROM {'.$this->get_tablename().'} t
  | 
        
        
            | 
            | 
           93 | 
                   JOIN {course_modules} cm ON cm.instance = t.id
  | 
        
        
            | 
            | 
           94 | 
                   JOIN {modules} m ON m.id = cm.module AND m.name = :ptablename2
  | 
        
        
            | 
            | 
           95 | 
                   JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :pctxlevelmodule '.
  | 
        
        
            | 
            | 
           96 | 
                       ($where ? 'WHERE ' . join(' AND ', $where) : '') . $this->filter . '
  | 
        
        
            | 
            | 
           97 | 
                   ORDER BY t.id',
  | 
        
        
            | 
            | 
           98 | 
                       ['pctxlevelmodule' => CONTEXT_MODULE,
  | 
        
        
            | 
            | 
           99 | 
                           'ptablename2' => $this->get_tablename(),
  | 
        
        
            | 
            | 
           100 | 
                       ] + $params);
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   return $rs;
  | 
        
        
            | 
            | 
           103 | 
               }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
               /**
  | 
        
        
            | 
            | 
           106 | 
                * Returns the moodle_url of the page to edit the error.
  | 
        
        
            | 
            | 
           107 | 
                * @param \stdClass $componentinfo
  | 
        
        
            | 
            | 
           108 | 
                * @return \moodle_url
  | 
        
        
            | 
            | 
           109 | 
                */
  | 
        
        
            | 
            | 
           110 | 
               public static function get_edit_url(\stdClass $componentinfo): \moodle_url {
  | 
        
        
            | 
            | 
           111 | 
                   return new \moodle_url('/course/mod.php', ['update' => $componentinfo->cmid, 'sr' => null, 'sesskey' => sesskey()]);
  | 
        
        
            | 
            | 
           112 | 
               }
  | 
        
        
            | 
            | 
           113 | 
           }
  |