[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/feedback/classes/ -> structure.php (source)

   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  /**
  18   * Contains class mod_feedback_structure
  19   *
  20   * @package   mod_feedback
  21   * @copyright 2016 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Stores and manipulates the structure of the feedback or template (items, pages, etc.)
  29   *
  30   * @package   mod_feedback
  31   * @copyright 2016 Marina Glancy
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_feedback_structure {
  35      /** @var stdClass */
  36      protected $feedback;
  37      /** @var cm_info */
  38      protected $cm;
  39      /** @var int */
  40      protected $courseid = 0;
  41      /** @var int */
  42      protected $templateid;
  43      /** @var array */
  44      protected $allitems;
  45      /** @var array */
  46      protected $allcourses;
  47  
  48      /**
  49       * Constructor
  50       *
  51       * @param stdClass $feedback feedback object, in case of the template
  52       *     this is the current feedback the template is accessed from
  53       * @param cm_info $cm course module object corresponding to the $feedback
  54       * @param int $courseid current course (for site feedbacks only)
  55       * @param int $templateid template id if this class represents the template structure
  56       */
  57      public function __construct($feedback, $cm, $courseid = 0, $templateid = null) {
  58          $this->feedback = $feedback;
  59          $this->cm = $cm;
  60          $this->courseid = ($feedback->course == SITEID) ? $courseid : 0;
  61          $this->templateid = $templateid;
  62      }
  63  
  64      /**
  65       * Current feedback
  66       * @return stdClass
  67       */
  68      public function get_feedback() {
  69          return $this->feedback;
  70      }
  71  
  72      /**
  73       * Current course module
  74       * @return stdClass
  75       */
  76      public function get_cm() {
  77          return $this->cm;
  78      }
  79  
  80      /**
  81       * Id of the current course (for site feedbacks only)
  82       * @return stdClass
  83       */
  84      public function get_courseid() {
  85          return $this->courseid;
  86      }
  87  
  88      /**
  89       * Template id
  90       * @return int
  91       */
  92      public function get_templateid() {
  93          return $this->templateid;
  94      }
  95  
  96      /**
  97       * Is this feedback open (check timeopen and timeclose)
  98       * @return bool
  99       */
 100      public function is_open() {
 101          $checktime = time();
 102          return (!$this->feedback->timeopen || $this->feedback->timeopen <= $checktime) &&
 103              (!$this->feedback->timeclose || $this->feedback->timeclose >= $checktime);
 104      }
 105  
 106      /**
 107       * Get all items in this feedback or this template
 108       * @param bool $hasvalueonly only count items with a value.
 109       * @return array of objects from feedback_item with an additional attribute 'itemnr'
 110       */
 111      public function get_items($hasvalueonly = false) {
 112          global $DB;
 113          if ($this->allitems === null) {
 114              if ($this->templateid) {
 115                  $this->allitems = $DB->get_records('feedback_item', ['template' => $this->templateid], 'position');
 116              } else {
 117                  $this->allitems = $DB->get_records('feedback_item', ['feedback' => $this->feedback->id], 'position');
 118              }
 119              $idx = 1;
 120              foreach ($this->allitems as $id => $item) {
 121                  $this->allitems[$id]->itemnr = $item->hasvalue ? ($idx++) : null;
 122              }
 123          }
 124          if ($hasvalueonly && $this->allitems) {
 125              return array_filter($this->allitems, function($item) {
 126                  return $item->hasvalue;
 127              });
 128          }
 129          return $this->allitems;
 130      }
 131  
 132      /**
 133       * Is the items list empty?
 134       * @return bool
 135       */
 136      public function is_empty() {
 137          $items = $this->get_items();
 138          $displayeditems = array_filter($items, function($item) {
 139              return $item->typ !== 'pagebreak';
 140          });
 141          return !$displayeditems;
 142      }
 143  
 144      /**
 145       * Is this feedback anonymous?
 146       * @return bool
 147       */
 148      public function is_anonymous() {
 149          return $this->feedback->anonymous == FEEDBACK_ANONYMOUS_YES;
 150      }
 151  
 152      /**
 153       * Returns the formatted text of the page after submit or null if it is not set
 154       *
 155       * @return string|null
 156       */
 157      public function page_after_submit() {
 158          global $CFG;
 159          require_once($CFG->libdir . '/filelib.php');
 160  
 161          $pageaftersubmit = $this->get_feedback()->page_after_submit;
 162          if (empty($pageaftersubmit)) {
 163              return null;
 164          }
 165          $pageaftersubmitformat = $this->get_feedback()->page_after_submitformat;
 166  
 167          $context = context_module::instance($this->get_cm()->id);
 168          $output = file_rewrite_pluginfile_urls($pageaftersubmit,
 169                  'pluginfile.php', $context->id, 'mod_feedback', 'page_after_submit', 0);
 170  
 171          return format_text($output, $pageaftersubmitformat, array('overflowdiv' => true));
 172      }
 173  
 174      /**
 175       * Checks if current user is able to view feedback on this course.
 176       *
 177       * @return bool
 178       */
 179      public function can_view_analysis() {
 180          $context = context_module::instance($this->cm->id);
 181          if (has_capability('mod/feedback:viewreports', $context)) {
 182              return true;
 183          }
 184  
 185          if (intval($this->feedback->publish_stats) != 1 ||
 186                  !has_capability('mod/feedback:viewanalysepage', $context)) {
 187              return false;
 188          }
 189  
 190          if (!isloggedin() || isguestuser()) {
 191              // There is no tracking for the guests, assume that they can view analysis if condition above is satisfied.
 192              return $this->feedback->course == SITEID;
 193          }
 194  
 195          return $this->is_already_submitted(true);
 196      }
 197  
 198      /**
 199       * check for multiple_submit = false.
 200       * if the feedback is global so the courseid must be given
 201       *
 202       * @param bool $anycourseid if true checks if this feedback was submitted in any course, otherwise checks $this->courseid .
 203       *     Applicable to frontpage feedbacks only
 204       * @return bool true if the feedback already is submitted otherwise false
 205       */
 206      public function is_already_submitted($anycourseid = false) {
 207          global $USER, $DB;
 208  
 209          if (!isloggedin() || isguestuser()) {
 210              return false;
 211          }
 212  
 213          $params = array('userid' => $USER->id, 'feedback' => $this->feedback->id);
 214          if (!$anycourseid && $this->courseid) {
 215              $params['courseid'] = $this->courseid;
 216          }
 217          return $DB->record_exists('feedback_completed', $params);
 218      }
 219  
 220      /**
 221       * Check whether the feedback is mapped to the given courseid.
 222       */
 223      public function check_course_is_mapped() {
 224          global $DB;
 225          if ($this->feedback->course != SITEID) {
 226              return true;
 227          }
 228          if ($DB->get_records('feedback_sitecourse_map', array('feedbackid' => $this->feedback->id))) {
 229              $params = array('feedbackid' => $this->feedback->id, 'courseid' => $this->courseid);
 230              if (!$DB->get_record('feedback_sitecourse_map', $params)) {
 231                  return false;
 232              }
 233          }
 234          // No mapping means any course is mapped.
 235          return true;
 236      }
 237  
 238      /**
 239       * If there are any new responses to the anonymous feedback, re-shuffle all
 240       * responses and assign response number to each of them.
 241       */
 242      public function shuffle_anonym_responses() {
 243          global $DB;
 244          $params = array('feedback' => $this->feedback->id,
 245              'random_response' => 0,
 246              'anonymous_response' => FEEDBACK_ANONYMOUS_YES);
 247  
 248          if ($DB->count_records('feedback_completed', $params, 'random_response')) {
 249              // Get all of the anonymous records, go through them and assign a response id.
 250              unset($params['random_response']);
 251              $feedbackcompleteds = $DB->get_records('feedback_completed', $params, 'id');
 252              shuffle($feedbackcompleteds);
 253              $num = 1;
 254              foreach ($feedbackcompleteds as $compl) {
 255                  $compl->random_response = $num++;
 256                  $DB->update_record('feedback_completed', $compl);
 257              }
 258          }
 259      }
 260  
 261      /**
 262       * Counts records from {feedback_completed} table for a given feedback
 263       *
 264       * If $groupid or $this->courseid is set, the records are filtered by the group/course
 265       *
 266       * @param int $groupid
 267       * @return mixed array of found completeds otherwise false
 268       */
 269      public function count_completed_responses($groupid = 0) {
 270          global $DB;
 271          if (intval($groupid) > 0) {
 272              $query = "SELECT COUNT(DISTINCT fbc.id)
 273                          FROM {feedback_completed} fbc, {groups_members} gm
 274                          WHERE fbc.feedback = :feedback
 275                              AND gm.groupid = :groupid
 276                              AND fbc.userid = gm.userid";
 277          } else if ($this->courseid) {
 278              $query = "SELECT COUNT(fbc.id)
 279                          FROM {feedback_completed} fbc
 280                          WHERE fbc.feedback = :feedback
 281                              AND fbc.courseid = :courseid";
 282          } else {
 283              $query = "SELECT COUNT(fbc.id) FROM {feedback_completed} fbc WHERE fbc.feedback = :feedback";
 284          }
 285          $params = ['feedback' => $this->feedback->id, 'groupid' => $groupid, 'courseid' => $this->courseid];
 286          return $DB->get_field_sql($query, $params);
 287      }
 288  
 289      /**
 290       * For the frontpage feedback returns the list of courses with at least one completed feedback
 291       *
 292       * @return array id=>name pairs of courses
 293       */
 294      public function get_completed_courses() {
 295          global $DB;
 296  
 297          if ($this->get_feedback()->course != SITEID) {
 298              return [];
 299          }
 300  
 301          if ($this->allcourses !== null) {
 302              return $this->allcourses;
 303          }
 304  
 305          $courseselect = "SELECT fbc.courseid
 306              FROM {feedback_completed} fbc
 307              WHERE fbc.feedback = :feedbackid";
 308  
 309          $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
 310  
 311          $sql = 'SELECT c.id, c.shortname, c.fullname, c.idnumber, c.visible, '. $ctxselect. '
 312                  FROM {course} c
 313                  JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
 314                  WHERE c.id IN ('. $courseselect.') ORDER BY c.sortorder';
 315          $list = $DB->get_records_sql($sql, ['contextcourse' => CONTEXT_COURSE, 'feedbackid' => $this->get_feedback()->id]);
 316  
 317          $this->allcourses = array();
 318          foreach ($list as $course) {
 319              context_helper::preload_from_record($course);
 320              if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
 321                  // Do not return courses that current user can not see.
 322                  continue;
 323              }
 324              $label = get_course_display_name_for_list($course);
 325              $this->allcourses[$course->id] = $label;
 326          }
 327          return $this->allcourses;
 328      }
 329  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1