Skip to content

Commit

Permalink
Major refactoring of checklist to use classes
Browse files Browse the repository at this point in the history
  • Loading branch information
davosmith committed Sep 9, 2016
1 parent 379b166 commit ccdb25d
Show file tree
Hide file tree
Showing 14 changed files with 2,157 additions and 1,268 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Students are presented with a simple chart showing how far they have progressed

==Changes==

* 2016-09-09 - Major restructuring of checklist code, to aid future maintenance; dropping of pre-Moodle 2.7 support.
* 2016-05-20 - Minor behat fixes for Moodle 3.1 compatibility
* 2016-03-15 - Show/hide multiple activity items at once when editing the checklist (Tony Butler)
* 2015-12-23 - Handle missing calendar events + fix deprecated 'get_referer' warning.
Expand Down
128 changes: 128 additions & 0 deletions classes/local/checklist_check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Holds the checkmark information
*
* @package mod_checklist
* @copyright 2016 Davo Smith, Synergy Learning
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_checklist\local;

use data_object;

defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/completion/data_object.php');

class checklist_check extends data_object {
public $table = 'checklist_check';
public $required_fields = [
'id', 'item', 'userid', 'usertimestamp', 'teachermark', 'teachertimestamp', 'teacherid'
];

// DB fields.
public $item;
public $userid;
public $usertimestamp = 0;
public $teachermark = CHECKLIST_TEACHERMARK_UNDECIDED;
public $teachertimestamp = 0;
public $teacherid = null;

public static function fetch($params) {
return self::fetch_helper('checklist_check', __CLASS__, $params);
}

public static function fetch_all($params, $sort = false) {
$ret = self::fetch_all_helper('checklist_check', __CLASS__, $params);
if (!$ret) {
$ret = [];
}
return $ret;
}

/**
* @param $userid
* @param $itemids
* @return checklist_check[] $itemid => $check
*/
public static function fetch_by_userid_itemids($userid, $itemids) {
global $DB;

$ret = [];
if (!$itemids) {
return $ret;
}

list($isql, $params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
$params['userid'] = $userid;
$checks = $DB->get_records_select('checklist_check', "userid = :userid AND item $isql", $params);
foreach ($checks as $check) {
$ret[$check->item] = new checklist_check();
self::set_properties($ret[$check->item], $check);
}
return $ret;
}

public static function teachermark_valid($teachermark) {
return in_array($teachermark, [CHECKLIST_TEACHERMARK_YES, CHECKLIST_TEACHERMARK_NO, CHECKLIST_TEACHERMARK_UNDECIDED]);
}

protected function check_fields_valid() {
if (!self::teachermark_valid($this->teachermark)) {
debugging('Unexpected teachermark value: '.$this->teachermark);
$this->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED;
}
}

public function save() {
if ($this->id) {
$this->update();
} else {
$this->insert();
}
}

public function insert() {
$this->check_fields_valid();
return parent::insert();
}

public function update() {
$this->check_fields_valid();
return parent::update();
}

public function is_checked_student() {
return $this->usertimestamp > 0;
}

public function is_checked_teacher() {
return ($this->teachermark == CHECKLIST_TEACHERMARK_YES);
}

public function set_teachermark($teachermark, $teacherid) {
$this->teachermark = $teachermark;
$this->teacherid = $teacherid;
$this->teachertimestamp = time();
}

public function set_checked_student($checked) {
$this->usertimestamp = $checked ? time() : 0;
}
}
130 changes: 130 additions & 0 deletions classes/local/checklist_comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* A comment added, by a teacher, to a checklist item
*
* @package mod_checklist
* @copyright 2016 Davo Smith, Synergy Learning
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_checklist\local;

use data_object;
use moodle_url;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot.'/completion/data_object.php');

class checklist_comment extends data_object {
public $table = 'checklist_comment';
public $required_fields = [
'id', 'itemid', 'userid', 'commentby', 'text'
];

// DB fields.
public $itemid;
public $userid;
public $commentby;
public $text;

// Extra data.
protected $commentbyname = null;

protected static $courseid = null;

public static function fetch($params) {
return self::fetch_helper('checklist_comment', __CLASS__, $params);
}

public static function fetch_all($params, $sort = false) {
$ret = self::fetch_all_helper('checklist_comment', __CLASS__, $params);
if (!$ret) {
$ret = [];
}
return $ret;
}

/**
* @param int $userid
* @param int[] $itemids
* @return checklist_comment[] $itemid => $check
*/
public static function fetch_by_userid_itemids($userid, $itemids) {
global $DB;

$ret = [];
if (!$itemids) {
return $ret;
}

list($isql, $params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
$params['userid'] = $userid;
$comments = $DB->get_records_select('checklist_comment', "userid = :userid AND itemid $isql", $params);
foreach ($comments as $comment) {
$ret[$comment->itemid] = new checklist_comment();
self::set_properties($ret[$comment->itemid], $comment);
}
return $ret;
}

/**
* @return string|null
*/
public function get_commentby_name() {
return $this->commentbyname;
}

/**
* @return moodle_url
*/
public function get_commentby_url() {
return new moodle_url('/user/view.php', ['id' => $this->commentby, 'course' => self::$courseid]);
}

/**
* @param checklist_comment[] $comments
*/
public static function add_commentby_names($comments) {
global $DB;

$userids = [];
foreach ($comments as $comment) {
if ($comment->commentby) {
$userids[] = $comment->commentby;
}
}
if (!$userids) {
return;
}

$commentusers = $DB->get_records_list('user', 'id', $userids, '', 'id,'.get_all_user_name_fields(true));
foreach ($comments as $comment) {
if ($comment->commentby) {
if (isset($commentusers[$comment->commentby])) {
$comment->commentbyname = fullname($commentusers[$comment->commentby]);
}
}
}
}

public static function set_courseid($courseid) {
self::$courseid = $courseid;
}
}
Loading

0 comments on commit ccdb25d

Please sign in to comment.